chrome: co-locate transport types (#236)

This commit is contained in:
Pavel Feldman 2019-12-12 21:30:49 -08:00 committed by GitHub
parent 97c50c22ab
commit bae8cd3fae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 61 additions and 155 deletions

View File

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

View File

@ -14,4 +14,4 @@
* limitations under the License.
*/
module.exports = require('./lib/Errors');
module.exports = require('./lib/errors');

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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 {

View File

@ -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<WebSocketTransport> {
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;

View File

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

View File

@ -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<WebSocketTransport> {
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();
}
}

View File

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

View File

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

View File

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

View File

@ -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']);
});
});