Skip to content

Commit

Permalink
chore(rpc): use channels types in dispatchers (#3549)
Browse files Browse the repository at this point in the history
This ensures we actually implement channels as intended.
For example, this change found an issue with Route.fulfill.
  • Loading branch information
dgozman authored Aug 20, 2020
1 parent e32a496 commit eab5ff4
Show file tree
Hide file tree
Showing 17 changed files with 173 additions and 170 deletions.
9 changes: 7 additions & 2 deletions src/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,15 @@ export class Route {
await this._delegate.abort(errorCode);
}

async fulfill(response: types.NormalizedFulfillResponse) {
async fulfill(response: { status?: number, headers?: types.HeadersArray, body?: string, isBase64?: boolean }) {
assert(!this._handled, 'Route is already handled!');
this._handled = true;
await this._delegate.fulfill(response);
await this._delegate.fulfill({
status: response.status === undefined ? 200 : response.status,
headers: response.headers || [],
body: response.body || '',
isBase64: response.isBase64 || false,
});
}

async continue(overrides: types.NormalizedContinueOverrides = {}) {
Expand Down
14 changes: 7 additions & 7 deletions src/rpc/server/browserDispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@

import { Browser } from '../../browser';
import { Events } from '../../events';
import { BrowserChannel, BrowserContextChannel, BrowserInitializer, CDPSessionChannel, Binary, BrowserNewContextParams } from '../channels';
import * as channels from '../channels';
import { BrowserContextDispatcher } from './browserContextDispatcher';
import { CDPSessionDispatcher } from './cdpSessionDispatcher';
import { Dispatcher, DispatcherScope } from './dispatcher';
import { CRBrowser } from '../../chromium/crBrowser';
import { PageDispatcher } from './pageDispatcher';

export class BrowserDispatcher extends Dispatcher<Browser, BrowserInitializer> implements BrowserChannel {
export class BrowserDispatcher extends Dispatcher<Browser, channels.BrowserInitializer> implements channels.BrowserChannel {
constructor(scope: DispatcherScope, browser: Browser, guid?: string) {
super(scope, browser, 'Browser', { version: browser.version() }, true, guid);
browser.on(Events.Browser.Disconnected, () => this._didClose());
Expand All @@ -34,25 +34,25 @@ export class BrowserDispatcher extends Dispatcher<Browser, BrowserInitializer> i
this._dispose();
}

async newContext(params: BrowserNewContextParams): Promise<{ context: BrowserContextChannel }> {
async newContext(params: channels.BrowserNewContextParams): Promise<channels.BrowserNewContextResult> {
return { context: new BrowserContextDispatcher(this._scope, await this._object.newContext(params)) };
}

async close(): Promise<void> {
await this._object.close();
}

async crNewBrowserCDPSession(): Promise<{ session: CDPSessionChannel }> {
async crNewBrowserCDPSession(): Promise<channels.BrowserCrNewBrowserCDPSessionResult> {
const crBrowser = this._object as CRBrowser;
return { session: new CDPSessionDispatcher(this._scope, await crBrowser.newBrowserCDPSession()) };
}

async crStartTracing(params: { page?: PageDispatcher, path?: string, screenshots?: boolean, categories?: string[] }): Promise<void> {
async crStartTracing(params: channels.BrowserCrStartTracingParams): Promise<void> {
const crBrowser = this._object as CRBrowser;
await crBrowser.startTracing(params.page ? params.page._object : undefined, params);
await crBrowser.startTracing(params.page ? (params.page as PageDispatcher)._object : undefined, params);
}

async crStopTracing(): Promise<{ binary: Binary }> {
async crStopTracing(): Promise<channels.BrowserCrStopTracingResult> {
const crBrowser = this._object as CRBrowser;
const buffer = await crBrowser.stopTracing();
return { binary: buffer.toString('base64') };
Expand Down
8 changes: 4 additions & 4 deletions src/rpc/server/browserTypeDispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,24 @@

import { BrowserTypeBase, BrowserType } from '../../server/browserType';
import { BrowserDispatcher } from './browserDispatcher';
import { BrowserChannel, BrowserTypeChannel, BrowserContextChannel, BrowserTypeInitializer, BrowserTypeLaunchParams, BrowserTypeLaunchPersistentContextParams } from '../channels';
import * as channels from '../channels';
import { Dispatcher, DispatcherScope } from './dispatcher';
import { BrowserContextDispatcher } from './browserContextDispatcher';

export class BrowserTypeDispatcher extends Dispatcher<BrowserType, BrowserTypeInitializer> implements BrowserTypeChannel {
export class BrowserTypeDispatcher extends Dispatcher<BrowserType, channels.BrowserTypeInitializer> implements channels.BrowserTypeChannel {
constructor(scope: DispatcherScope, browserType: BrowserTypeBase) {
super(scope, browserType, 'BrowserType', {
executablePath: browserType.executablePath(),
name: browserType.name()
}, true);
}

async launch(params: BrowserTypeLaunchParams): Promise<{ browser: BrowserChannel }> {
async launch(params: channels.BrowserTypeLaunchParams): Promise<channels.BrowserTypeLaunchResult> {
const browser = await this._object.launch(params);
return { browser: new BrowserDispatcher(this._scope, browser) };
}

async launchPersistentContext(params: BrowserTypeLaunchPersistentContextParams): Promise<{ context: BrowserContextChannel }> {
async launchPersistentContext(params: channels.BrowserTypeLaunchPersistentContextParams): Promise<channels.BrowserTypeLaunchPersistentContextResult> {
const browserContext = await this._object.launchPersistentContext(params.userDataDir, params);
return { context: new BrowserContextDispatcher(this._scope, browserContext) };
}
Expand Down
6 changes: 3 additions & 3 deletions src/rpc/server/cdpSessionDispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
*/

import { CRSession, CRSessionEvents } from '../../chromium/crConnection';
import { CDPSessionChannel, CDPSessionInitializer } from '../channels';
import * as channels from '../channels';
import { Dispatcher, DispatcherScope } from './dispatcher';

export class CDPSessionDispatcher extends Dispatcher<CRSession, CDPSessionInitializer> implements CDPSessionChannel {
export class CDPSessionDispatcher extends Dispatcher<CRSession, channels.CDPSessionInitializer> implements channels.CDPSessionChannel {
constructor(scope: DispatcherScope, crSession: CRSession) {
super(scope, crSession, 'CDPSession', {}, true);
crSession._eventListener = (method, params) => {
Expand All @@ -27,7 +27,7 @@ export class CDPSessionDispatcher extends Dispatcher<CRSession, CDPSessionInitia
crSession.on(CRSessionEvents.Disconnected, () => this._dispose());
}

async send(params: { method: string, params?: any }): Promise<{ result: any }> {
async send(params: channels.CDPSessionSendParams): Promise<channels.CDPSessionSendResult> {
return { result: await this._object.send(params.method as any, params.params) };
}

Expand Down
4 changes: 2 additions & 2 deletions src/rpc/server/consoleMessageDispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
*/

import { ConsoleMessage } from '../../console';
import { ConsoleMessageChannel, ConsoleMessageInitializer } from '../channels';
import * as channels from '../channels';
import { Dispatcher, DispatcherScope } from './dispatcher';
import { createHandle } from './elementHandlerDispatcher';

export class ConsoleMessageDispatcher extends Dispatcher<ConsoleMessage, ConsoleMessageInitializer> implements ConsoleMessageChannel {
export class ConsoleMessageDispatcher extends Dispatcher<ConsoleMessage, channels.ConsoleMessageInitializer> implements channels.ConsoleMessageChannel {
constructor(scope: DispatcherScope, message: ConsoleMessage) {
super(scope, message, 'ConsoleMessage', {
type: message.type(),
Expand Down
4 changes: 2 additions & 2 deletions src/rpc/server/dialogDispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
*/

import { Dialog } from '../../dialog';
import { DialogChannel, DialogInitializer } from '../channels';
import * as channels from '../channels';
import { Dispatcher, DispatcherScope } from './dispatcher';

export class DialogDispatcher extends Dispatcher<Dialog, DialogInitializer> implements DialogChannel {
export class DialogDispatcher extends Dispatcher<Dialog, channels.DialogInitializer> implements channels.DialogChannel {
constructor(scope: DispatcherScope, dialog: Dialog) {
super(scope, dialog, 'Dialog', {
type: dialog.type(),
Expand Down
4 changes: 2 additions & 2 deletions src/rpc/server/dispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import { EventEmitter } from 'events';
import { helper, debugAssert, assert } from '../../helper';
import { Channel } from '../channels';
import * as channels from '../channels';
import { serializeError } from '../serializers';
import { createScheme, Validator, ValidationError } from '../validator';

Expand All @@ -36,7 +36,7 @@ export function lookupNullableDispatcher<DispatcherType>(object: any | null): Di
return object ? lookupDispatcher(object) : undefined;
}

export class Dispatcher<Type, Initializer> extends EventEmitter implements Channel {
export class Dispatcher<Type, Initializer> extends EventEmitter implements channels.Channel {
private _connection: DispatcherConnection;
private _isScope: boolean;
// Parent is always "isScope".
Expand Down
12 changes: 6 additions & 6 deletions src/rpc/server/downloadDispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,36 @@
*/

import { Download } from '../../download';
import { DownloadChannel, DownloadInitializer, StreamChannel } from '../channels';
import * as channels from '../channels';
import { Dispatcher, DispatcherScope } from './dispatcher';
import { StreamDispatcher } from './streamDispatcher';

export class DownloadDispatcher extends Dispatcher<Download, DownloadInitializer> implements DownloadChannel {
export class DownloadDispatcher extends Dispatcher<Download, channels.DownloadInitializer> implements channels.DownloadChannel {
constructor(scope: DispatcherScope, download: Download) {
super(scope, download, 'Download', {
url: download.url(),
suggestedFilename: download.suggestedFilename(),
});
}

async path(): Promise<{ value?: string }> {
async path(): Promise<channels.DownloadPathResult> {
const path = await this._object.path();
return { value: path || undefined };
}

async saveAs(params: { path: string }): Promise<void> {
async saveAs(params: channels.DownloadSaveAsParams): Promise<void> {
await this._object.saveAs(params.path);
}

async stream(): Promise<{ stream?: StreamChannel }> {
async stream(): Promise<channels.DownloadStreamResult> {
const stream = await this._object.createReadStream();
if (!stream)
return {};
await new Promise(f => stream.on('readable', f));
return { stream: new StreamDispatcher(this._scope, stream) };
}

async failure(): Promise<{ error?: string }> {
async failure(): Promise<channels.DownloadFailureResult> {
const error = await this._object.failure();
return { error: error || undefined };
}
Expand Down
16 changes: 8 additions & 8 deletions src/rpc/server/electronDispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,24 @@

import { Dispatcher, DispatcherScope, lookupDispatcher } from './dispatcher';
import { Electron, ElectronApplication, ElectronEvents, ElectronPage } from '../../server/electron';
import { ElectronApplicationChannel, ElectronApplicationInitializer, PageChannel, JSHandleChannel, ElectronInitializer, ElectronChannel, SerializedArgument, ElectronLaunchParams, SerializedValue } from '../channels';
import * as channels from '../channels';
import { BrowserContextDispatcher } from './browserContextDispatcher';
import { PageDispatcher } from './pageDispatcher';
import { parseArgument, serializeResult } from './jsHandleDispatcher';
import { createHandle } from './elementHandlerDispatcher';

export class ElectronDispatcher extends Dispatcher<Electron, ElectronInitializer> implements ElectronChannel {
export class ElectronDispatcher extends Dispatcher<Electron, channels.ElectronInitializer> implements channels.ElectronChannel {
constructor(scope: DispatcherScope, electron: Electron) {
super(scope, electron, 'Electron', {}, true);
}

async launch(params: ElectronLaunchParams): Promise<{ electronApplication: ElectronApplicationChannel }> {
async launch(params: channels.ElectronLaunchParams): Promise<channels.ElectronLaunchResult> {
const electronApplication = await this._object.launch(params.executablePath, params);
return { electronApplication: new ElectronApplicationDispatcher(this._scope, electronApplication) };
}
}

export class ElectronApplicationDispatcher extends Dispatcher<ElectronApplication, ElectronApplicationInitializer> implements ElectronApplicationChannel {
export class ElectronApplicationDispatcher extends Dispatcher<ElectronApplication, channels.ElectronApplicationInitializer> implements channels.ElectronApplicationChannel {
constructor(scope: DispatcherScope, electronApplication: ElectronApplication) {
super(scope, electronApplication, 'ElectronApplication', {}, true);
this._dispatchEvent('context', { context: new BrowserContextDispatcher(this._scope, electronApplication.context()) });
Expand All @@ -49,17 +49,17 @@ export class ElectronApplicationDispatcher extends Dispatcher<ElectronApplicatio
});
}

async newBrowserWindow(params: { arg: SerializedArgument }): Promise<{ page: PageChannel }> {
async newBrowserWindow(params: channels.ElectronApplicationNewBrowserWindowParams): Promise<channels.ElectronApplicationNewBrowserWindowResult> {
const page = await this._object.newBrowserWindow(parseArgument(params.arg));
return { page: lookupDispatcher<PageChannel>(page) };
return { page: lookupDispatcher<PageDispatcher>(page) };
}

async evaluateExpression(params: { expression: string, isFunction: boolean, arg: SerializedArgument }): Promise<{ value: SerializedValue }> {
async evaluateExpression(params: channels.ElectronApplicationEvaluateExpressionParams): Promise<channels.ElectronApplicationEvaluateExpressionResult> {
const handle = this._object._nodeElectronHandle!;
return { value: serializeResult(await handle._evaluateExpression(params.expression, params.isFunction, true /* returnByValue */, parseArgument(params.arg))) };
}

async evaluateExpressionHandle(params: { expression: string, isFunction: boolean, arg: SerializedArgument }): Promise<{ handle: JSHandleChannel }> {
async evaluateExpressionHandle(params: channels.ElectronApplicationEvaluateExpressionHandleParams): Promise<channels.ElectronApplicationEvaluateExpressionHandleResult> {
const handle = this._object._nodeElectronHandle!;
const result = await handle._evaluateExpression(params.expression, params.isFunction, false /* returnByValue */, parseArgument(params.arg));
return { handle: createHandle(this._scope, result) };
Expand Down
Loading

0 comments on commit eab5ff4

Please sign in to comment.