Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: migrate src/pipetransport to typescript #5692

Merged
merged 2 commits into from Apr 20, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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');
mathiasbynens marked this conversation as resolved.
Show resolved Hide resolved

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.
jackfranklin marked this conversation as resolved.
Show resolved Hide resolved
*/
interface PuppeteerEventListener {
emitter: NodeJS.EventEmitter;
eventName: string | symbol;
handler: (...args: any[]) => void;
}