From bae8cd3fae60d59246c01fc7cb4d0bf2ab133ceb Mon Sep 17 00:00:00 2001 From: Pavel Feldman Date: Thu, 12 Dec 2019 21:30:49 -0800 Subject: [PATCH] chrome: co-locate transport types (#236) --- DeviceDescriptors.js | 2 +- Errors.js | 2 +- src/chromium/Connection.ts | 2 +- src/chromium/Launcher.ts | 4 +- src/chromium/Playwright.ts | 2 +- src/firefox/Connection.ts | 2 +- src/firefox/Playwright.ts | 2 +- src/firefox/WebSocketTransport.ts | 2 +- .../PipeTransport.ts => transport.ts} | 50 ++++++++++++- src/types.ts | 7 -- src/webSocketTransport.ts | 58 --------------- src/webkit/Connection.ts | 4 +- src/webkit/Launcher.ts | 2 +- src/webkit/PipeTransport.ts | 73 ------------------- test/launcher.spec.js | 4 +- 15 files changed, 61 insertions(+), 155 deletions(-) rename src/{chromium/PipeTransport.ts => transport.ts} (63%) delete mode 100644 src/webSocketTransport.ts delete mode 100644 src/webkit/PipeTransport.ts diff --git a/DeviceDescriptors.js b/DeviceDescriptors.js index b5f92b43ea..2ac295c3ba 100644 --- a/DeviceDescriptors.js +++ b/DeviceDescriptors.js @@ -15,7 +15,7 @@ * limitations under the License. */ -const {DeviceDescriptors} = require('./lib/DeviceDescriptors'); +const {DeviceDescriptors} = require('./lib/deviceDescriptors'); const descriptors = DeviceDescriptors.slice(); module.exports = descriptors; diff --git a/Errors.js b/Errors.js index 4779e1d850..5475d2223d 100644 --- a/Errors.js +++ b/Errors.js @@ -14,4 +14,4 @@ * limitations under the License. */ -module.exports = require('./lib/Errors'); +module.exports = require('./lib/errors'); diff --git a/src/chromium/Connection.ts b/src/chromium/Connection.ts index 77821ef175..59a2780236 100644 --- a/src/chromium/Connection.ts +++ b/src/chromium/Connection.ts @@ -17,7 +17,7 @@ import * as debug from 'debug'; import { EventEmitter } from 'events'; -import { ConnectionTransport } from '../types'; +import { ConnectionTransport } from '../transport'; import { assert } from '../helper'; import { Protocol } from './protocol'; diff --git a/src/chromium/Launcher.ts b/src/chromium/Launcher.ts index b2b4ad4e42..84418af2eb 100644 --- a/src/chromium/Launcher.ts +++ b/src/chromium/Launcher.ts @@ -27,9 +27,7 @@ import { Connection } from './Connection'; import { TimeoutError } from '../errors'; import { assert, debugError, helper } from '../helper'; import * as types from '../types'; -import { PipeTransport } from './PipeTransport'; -import { WebSocketTransport } from '../webSocketTransport'; -import { ConnectionTransport } from '../types'; +import { ConnectionTransport, WebSocketTransport, PipeTransport } from '../transport'; import * as util from 'util'; import { launchProcess, waitForLine } from '../processLauncher'; diff --git a/src/chromium/Playwright.ts b/src/chromium/Playwright.ts index ce98c34c0e..86e856734c 100644 --- a/src/chromium/Playwright.ts +++ b/src/chromium/Playwright.ts @@ -17,7 +17,7 @@ import { Browser } from './Browser'; import { BrowserFetcher, BrowserFetcherOptions, BrowserFetcherRevisionInfo, OnProgressCallback } from '../browserFetcher'; -import { ConnectionTransport } from '../types'; +import { ConnectionTransport } from '../transport'; import { DeviceDescriptors, DeviceDescriptor } from '../deviceDescriptors'; import * as Errors from '../errors'; import { Launcher, LauncherBrowserOptions, LauncherChromeArgOptions, LauncherLaunchOptions, createBrowserFetcher } from './Launcher'; diff --git a/src/firefox/Connection.ts b/src/firefox/Connection.ts index 5a69a08a5e..447c6a5243 100644 --- a/src/firefox/Connection.ts +++ b/src/firefox/Connection.ts @@ -18,7 +18,7 @@ import {assert} from '../helper'; import {EventEmitter} from 'events'; import * as debug from 'debug'; -import { ConnectionTransport } from '../types'; +import { ConnectionTransport } from '../transport'; import { Protocol } from './protocol'; const debugProtocol = debug('playwright:protocol'); diff --git a/src/firefox/Playwright.ts b/src/firefox/Playwright.ts index 8c64c387f0..2fa46e8e5e 100644 --- a/src/firefox/Playwright.ts +++ b/src/firefox/Playwright.ts @@ -16,7 +16,7 @@ */ import { Browser } from './Browser'; import { BrowserFetcher, BrowserFetcherOptions, OnProgressCallback, BrowserFetcherRevisionInfo } from '../browserFetcher'; -import { ConnectionTransport } from '../types'; +import { ConnectionTransport } from '../transport'; import { DeviceDescriptors, DeviceDescriptor } from '../deviceDescriptors'; import * as Errors from '../errors'; import { Launcher, createBrowserFetcher } from './Launcher'; diff --git a/src/firefox/WebSocketTransport.ts b/src/firefox/WebSocketTransport.ts index 93dcf6b631..8846b7bf81 100644 --- a/src/firefox/WebSocketTransport.ts +++ b/src/firefox/WebSocketTransport.ts @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { ConnectionTransport } from '../types'; +import { ConnectionTransport } from '../transport'; import * as WebSocket from 'ws'; export class WebSocketTransport implements ConnectionTransport { diff --git a/src/chromium/PipeTransport.ts b/src/transport.ts similarity index 63% rename from src/chromium/PipeTransport.ts rename to src/transport.ts index 74db2f8188..a3374bf310 100644 --- a/src/chromium/PipeTransport.ts +++ b/src/transport.ts @@ -15,8 +15,54 @@ * limitations under the License. */ -import { ConnectionTransport } from '../types'; -import { debugError, helper, RegisteredListener } from '../helper'; +import * as WebSocket from 'ws'; +import { debugError, helper, RegisteredListener } from './helper'; + +export interface ConnectionTransport { + send(s: string): void; + close(): void; + onmessage?: (message: string) => void, + onclose?: () => void, +} + +export class WebSocketTransport implements ConnectionTransport { + private _ws: WebSocket; + onmessage?: (message: string) => void; + onclose?: () => void; + + static create(url: string): Promise { + return new Promise((resolve, reject) => { + const ws = new WebSocket(url, [], { + perMessageDeflate: false, + maxPayload: 256 * 1024 * 1024, // 256Mb + }); + ws.addEventListener('open', () => resolve(new WebSocketTransport(ws))); + ws.addEventListener('error', reject); + }); + } + + constructor(ws: WebSocket) { + this._ws = ws; + this._ws.addEventListener('message', event => { + if (this.onmessage) + this.onmessage.call(null, event.data); + }); + this._ws.addEventListener('close', event => { + if (this.onclose) + this.onclose.call(null); + }); + // Silently ignore all errors - we don't know what to do with them. + this._ws.addEventListener('error', () => {}); + } + + send(message: string) { + this._ws.send(message); + } + + close() { + this._ws.close(); + } +} export class PipeTransport implements ConnectionTransport { private _pipeWrite: NodeJS.WritableStream; diff --git a/src/types.ts b/src/types.ts index 0122dca471..fd7839c39a 100644 --- a/src/types.ts +++ b/src/types.ts @@ -64,10 +64,3 @@ export type Viewport = { isLandscape?: boolean; hasTouch?: boolean; }; - -export interface ConnectionTransport { - send(s: string): void; - close(): void; - onmessage?: (message: string) => void, - onclose?: () => void, -} diff --git a/src/webSocketTransport.ts b/src/webSocketTransport.ts deleted file mode 100644 index 478b08eae6..0000000000 --- a/src/webSocketTransport.ts +++ /dev/null @@ -1,58 +0,0 @@ -/** - * Copyright 2018 Google Inc. All rights reserved. - * Modifications copyright (c) Microsoft Corporation. - * - * 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. - */ - -import * as WebSocket from 'ws'; -import { ConnectionTransport } from './types'; - -export class WebSocketTransport implements ConnectionTransport { - private _ws: WebSocket; - onmessage?: (message: string) => void; - onclose?: () => void; - - static create(url: string): Promise { - return new Promise((resolve, reject) => { - const ws = new WebSocket(url, [], { - perMessageDeflate: false, - maxPayload: 256 * 1024 * 1024, // 256Mb - }); - ws.addEventListener('open', () => resolve(new WebSocketTransport(ws))); - ws.addEventListener('error', reject); - }); - } - - constructor(ws: WebSocket) { - this._ws = ws; - this._ws.addEventListener('message', event => { - if (this.onmessage) - this.onmessage.call(null, event.data); - }); - this._ws.addEventListener('close', event => { - if (this.onclose) - this.onclose.call(null); - }); - // Silently ignore all errors - we don't know what to do with them. - this._ws.addEventListener('error', () => {}); - } - - send(message: string) { - this._ws.send(message); - } - - close() { - this._ws.close(); - } -} diff --git a/src/webkit/Connection.ts b/src/webkit/Connection.ts index 2ac3cdb1f2..e946aa5400 100644 --- a/src/webkit/Connection.ts +++ b/src/webkit/Connection.ts @@ -17,8 +17,8 @@ import {assert, debugError} from '../helper'; import * as debug from 'debug'; -import {EventEmitter} from 'events'; -import { ConnectionTransport } from '../types'; +import { EventEmitter } from 'events'; +import { ConnectionTransport } from '../transport'; import { Protocol } from './protocol'; const debugProtocol = debug('playwright:protocol'); diff --git a/src/webkit/Launcher.ts b/src/webkit/Launcher.ts index 4cfc997865..226cb89adc 100644 --- a/src/webkit/Launcher.ts +++ b/src/webkit/Launcher.ts @@ -20,7 +20,7 @@ import { Browser } from './Browser'; import { BrowserFetcher, BrowserFetcherOptions } from '../browserFetcher'; import { Connection } from './Connection'; import * as types from '../types'; -import { PipeTransport } from './PipeTransport'; +import { PipeTransport } from '../transport'; import { execSync } from 'child_process'; import * as path from 'path'; import * as util from 'util'; diff --git a/src/webkit/PipeTransport.ts b/src/webkit/PipeTransport.ts deleted file mode 100644 index 74db2f8188..0000000000 --- a/src/webkit/PipeTransport.ts +++ /dev/null @@ -1,73 +0,0 @@ -/** - * Copyright 2018 Google Inc. All rights reserved. - * Modifications copyright (c) Microsoft Corporation. - * - * 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. - */ - -import { ConnectionTransport } from '../types'; -import { debugError, helper, RegisteredListener } from '../helper'; - -export class PipeTransport implements ConnectionTransport { - private _pipeWrite: NodeJS.WritableStream; - private _pendingMessage = ''; - private _eventListeners: RegisteredListener[]; - onmessage?: (message: string) => void; - onclose?: () => void; - - constructor(pipeWrite: NodeJS.WritableStream, pipeRead: NodeJS.ReadableStream) { - this._pipeWrite = pipeWrite; - this._eventListeners = [ - helper.addEventListener(pipeRead, 'data', buffer => this._dispatch(buffer)), - helper.addEventListener(pipeRead, 'close', () => { - if (this.onclose) - this.onclose.call(null); - }), - helper.addEventListener(pipeRead, 'error', debugError), - helper.addEventListener(pipeWrite, 'error', debugError), - ]; - this.onmessage = null; - this.onclose = null; - } - - send(message: string) { - this._pipeWrite.write(message); - this._pipeWrite.write('\0'); - } - - _dispatch(buffer: Buffer) { - let end = buffer.indexOf('\0'); - if (end === -1) { - this._pendingMessage += buffer.toString(); - return; - } - const message = this._pendingMessage + buffer.toString(undefined, 0, end); - if (this.onmessage) - this.onmessage.call(null, message); - - let start = end + 1; - end = buffer.indexOf('\0', start); - while (end !== -1) { - if (this.onmessage) - this.onmessage.call(null, buffer.toString(undefined, start, end)); - start = end + 1; - end = buffer.indexOf('\0', start); - } - this._pendingMessage = buffer.toString(undefined, start); - } - - close() { - this._pipeWrite = null; - helper.removeEventListeners(this._eventListeners); - } -} diff --git a/test/launcher.spec.js b/test/launcher.spec.js index 7ef8c05820..346dcb08b4 100644 --- a/test/launcher.spec.js +++ b/test/launcher.spec.js @@ -116,11 +116,11 @@ module.exports.addTests = function({testRunner, expect, defaultBrowserOptions, p describe('Top-level requires', function() { it('should require top-level Errors', async() => { - const Errors = require(path.join(utils.projectRoot(), '/errors')); + const Errors = require(path.join(utils.projectRoot(), '/Errors')); expect(Errors.TimeoutError).toBe(playwright.errors.TimeoutError); }); it('should require top-level DeviceDescriptors', async() => { - const Devices = require(path.join(utils.projectRoot(), '/deviceDescriptors')); + const Devices = require(path.join(utils.projectRoot(), '/DeviceDescriptors')); expect(Devices['iPhone 6']).toBe(playwright.devices['iPhone 6']); }); });