Skip to content

Commit

Permalink
chore: migrate src/pipetransport to typescript (#5692)
Browse files Browse the repository at this point in the history
* chore: migrate `src/pipetransport` to typescript

Hit one bump in the fact that I want to share an interface across files.
TypeScript only lets you import/export these if you're using ESM, not
CommonJS. So the two options are:

- Migrate to ESM on a per file basis as we do this migration. This won't
affect the output as we output as CommonJS.
- Create a global `types.d.ts` file that we'll use and then migrate to
ESM after.

Right now I've gone for the second option in order to not introduce more
changes in one go. But if we end up finding we have lots of
interfaces/types/etc that we want modules to expose, we might decide
slowly introducing ESM might be a better way forwards.

* Update src/types.d.ts

Co-Authored-By: Mathias Bynens <mathias@qiwi.be>
  • Loading branch information
jackfranklin and mathiasbynens committed Apr 20, 2020
1 parent 4134b54 commit e7a32a8
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/Launcher.js
Expand Up @@ -29,7 +29,7 @@ const {helper, assert, debugError} = require('./helper');
const debugLauncher = require('debug')(`puppeteer:launcher`);
const {TimeoutError} = require('./Errors');
const WebSocketTransport = require('./WebSocketTransport');
const PipeTransport = require('./PipeTransport');
const {PipeTransport} = require('./PipeTransport');

const mkdtempAsync = helper.promisify(fs.mkdtemp);
const removeFolderAsync = helper.promisify(removeFolder);
Expand Down
35 changes: 15 additions & 20 deletions src/PipeTransport.js → src/PipeTransport.ts
Expand Up @@ -13,17 +13,18 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const {helper, debugError} = require('./helper');
import helperUtils = require('./helper');
const {helper, debugError} = helperUtils;

/**
* @implements {!Puppeteer.ConnectionTransport}
*/
class PipeTransport {
/**
* @param {!NodeJS.WritableStream} pipeWrite
* @param {!NodeJS.ReadableStream} pipeRead
*/
constructor(pipeWrite, pipeRead) {
class PipeTransport implements Puppeteer.ConnectionTransport {
_pipeWrite: NodeJS.WritableStream;
_pendingMessage: string;
_eventListeners: PuppeteerEventListener[];

onclose?: () => void;
onmessage?: () => void;

constructor(pipeWrite: NodeJS.WritableStream, pipeRead: NodeJS.ReadableStream) {
this._pipeWrite = pipeWrite;
this._pendingMessage = '';
this._eventListeners = [
Expand All @@ -39,18 +40,12 @@ class PipeTransport {
this.onclose = null;
}

/**
* @param {string} message
*/
send(message) {
send(message: string): void {
this._pipeWrite.write(message);
this._pipeWrite.write('\0');
}

/**
* @param {!Buffer} buffer
*/
_dispatch(buffer) {
_dispatch(buffer: Buffer): void {
let end = buffer.indexOf('\0');
if (end === -1) {
this._pendingMessage += buffer.toString();
Expand All @@ -71,10 +66,10 @@ class PipeTransport {
this._pendingMessage = buffer.toString(undefined, start);
}

close() {
close(): void {
this._pipeWrite = null;
helper.removeEventListeners(this._eventListeners);
}
}

module.exports = PipeTransport;
export = {PipeTransport};
2 changes: 1 addition & 1 deletion src/helper.ts
Expand Up @@ -103,7 +103,7 @@ function installAsyncStackHooks(classType: AnyClass): void {
}


function addEventListener(emitter: NodeJS.EventEmitter, eventName: string|symbol, handler: (...args: any[]) => void): { emitter: NodeJS.EventEmitter; eventName: string|symbol; handler: (...args: any[]) => void} {
function addEventListener(emitter: NodeJS.EventEmitter, eventName: string|symbol, handler: (...args: any[]) => void): PuppeteerEventListener {
emitter.on(eventName, handler);
return { emitter, eventName, handler };
}
Expand Down
25 changes: 25 additions & 0 deletions src/types.d.ts
@@ -0,0 +1,25 @@
/**
* Copyright 2020 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/* These types exist here until we migrate over to ESM where we can
* import / export them properly from modules - TS doesn't support
* exposing interfaces in CommonJS land.
*/
interface PuppeteerEventListener {
emitter: NodeJS.EventEmitter;
eventName: string | symbol;
handler: (...args: any[]) => void;
}

0 comments on commit e7a32a8

Please sign in to comment.