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 <mathias@qiwi.be>
This commit is contained in:
Jack Franklin 2020-04-20 15:05:58 +01:00 committed by GitHub
parent 4134b540ca
commit e7a32a8851
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 42 additions and 22 deletions

View File

@ -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);

View File

@ -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};

View File

@ -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 };
}

25
src/types.d.ts vendored Normal file
View File

@ -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;
}