From e7a32a885139f896aa6e8f2b63da8c86547a327e Mon Sep 17 00:00:00 2001 From: Jack Franklin Date: Mon, 20 Apr 2020 15:05:58 +0100 Subject: [PATCH] chore: migrate `src/pipetransport` to typescript (#5692) * 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 --- src/Launcher.js | 2 +- src/{PipeTransport.js => PipeTransport.ts} | 35 ++++++++++------------ src/helper.ts | 2 +- src/types.d.ts | 25 ++++++++++++++++ 4 files changed, 42 insertions(+), 22 deletions(-) rename src/{PipeTransport.js => PipeTransport.ts} (78%) create mode 100644 src/types.d.ts diff --git a/src/Launcher.js b/src/Launcher.js index b046f8c33b55c..85f217c93f577 100644 --- a/src/Launcher.js +++ b/src/Launcher.js @@ -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); diff --git a/src/PipeTransport.js b/src/PipeTransport.ts similarity index 78% rename from src/PipeTransport.js rename to src/PipeTransport.ts index fb87a97d2193e..d23ef0965173b 100644 --- a/src/PipeTransport.js +++ b/src/PipeTransport.ts @@ -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 = [ @@ -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(); @@ -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}; diff --git a/src/helper.ts b/src/helper.ts index f81db28e1d29e..c5788b8083d6a 100644 --- a/src/helper.ts +++ b/src/helper.ts @@ -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 }; } diff --git a/src/types.d.ts b/src/types.d.ts new file mode 100644 index 0000000000000..8be41bae854cd --- /dev/null +++ b/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; +}