Skip to content

Commit 2d5c032

Browse files
authored
feat(rpc): return objects for every protocol command (#2950)
For future extensibility, returning objects with fields instead of plain strings or channels.
1 parent 46a625d commit 2d5c032

34 files changed

+420
-370
lines changed

src/rpc/channels.ts

Lines changed: 89 additions & 89 deletions
Large diffs are not rendered by default.

src/rpc/client/accessibility.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ export class Accessibility {
2626
this._channel = channel;
2727
}
2828

29-
snapshot(options: { interestingOnly?: boolean; root?: ElementHandle } = {}): Promise<types.SerializedAXNode | null> {
29+
async snapshot(options: { interestingOnly?: boolean; root?: ElementHandle } = {}): Promise<types.SerializedAXNode | null> {
3030
const root = options.root ? options.root._elementChannel : undefined;
31-
return this._channel.accessibilitySnapshot({ interestingOnly: options.interestingOnly, root });
31+
const result = await this._channel.accessibilitySnapshot({ interestingOnly: options.interestingOnly, root });
32+
return result.rootAXNode;
3233
}
3334
}

src/rpc/client/browser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export class Browser extends ChannelOwner<BrowserChannel, BrowserInitializer> {
5353
async newContext(options: types.BrowserContextOptions & { logger?: LoggerSink } = {}): Promise<BrowserContext> {
5454
const logger = options.logger;
5555
options = { ...options, logger: undefined };
56-
const context = BrowserContext.from(await this._channel.newContext(options));
56+
const context = BrowserContext.from((await this._channel.newContext(options)).context);
5757
this._contexts.add(context);
5858
context._logger = logger || this._logger;
5959
return context;

src/rpc/client/browserContext.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ export class BrowserContext extends ChannelOwner<BrowserContextChannel, BrowserC
5353
this._browser = parent;
5454
this._browserName = browserName;
5555

56-
this._channel.on('bindingCall', bindingCall => this._onBinding(BindingCall.from(bindingCall)));
56+
this._channel.on('bindingCall', ({binding}) => this._onBinding(BindingCall.from(binding)));
5757
this._channel.on('close', () => this._onClose());
58-
this._channel.on('page', page => this._onPage(Page.from(page)));
58+
this._channel.on('page', ({page}) => this._onPage(Page.from(page)));
5959
this._channel.on('route', ({ route, request }) => this._onRoute(network.Route.from(route), network.Request.from(request)));
6060
this._closedPromise = new Promise(f => this.once(Events.BrowserContext.Close, f));
6161
}
@@ -99,15 +99,15 @@ export class BrowserContext extends ChannelOwner<BrowserContextChannel, BrowserC
9999
async newPage(): Promise<Page> {
100100
if (this._ownerPage)
101101
throw new Error('Please use browser.newContext()');
102-
return Page.from(await this._channel.newPage());
102+
return Page.from((await this._channel.newPage()).page);
103103
}
104104

105105
async cookies(urls?: string | string[]): Promise<network.NetworkCookie[]> {
106106
if (!urls)
107107
urls = [];
108108
if (urls && typeof urls === 'string')
109109
urls = [ urls ];
110-
return this._channel.cookies({ urls: urls as string[] });
110+
return (await this._channel.cookies({ urls: urls as string[] })).cookies;
111111
}
112112

113113
async addCookies(cookies: network.SetNetworkCookieParam[]): Promise<void> {

src/rpc/client/browserType.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,28 +43,29 @@ export class BrowserType extends ChannelOwner<BrowserTypeChannel, BrowserTypeIni
4343
async launch(options: types.LaunchOptions & { logger?: LoggerSink } = {}): Promise<Browser> {
4444
const logger = options.logger;
4545
options = { ...options, logger: undefined };
46-
const browser = Browser.from(await this._channel.launch(options));
46+
const browser = Browser.from((await this._channel.launch(options)).browser);
4747
browser._logger = logger;
4848
return browser;
4949
}
5050

5151
async launchServer(options: types.LaunchServerOptions & { logger?: LoggerSink } = {}): Promise<BrowserServer> {
5252
options = { ...options, logger: undefined };
53-
return BrowserServer.from(await this._channel.launchServer(options));
53+
return BrowserServer.from((await this._channel.launchServer(options)).server);
5454
}
5555

5656
async launchPersistentContext(userDataDir: string, options: types.LaunchOptions & types.BrowserContextOptions & { logger?: LoggerSink } = {}): Promise<BrowserContext> {
5757
const logger = options.logger;
5858
options = { ...options, logger: undefined };
59-
const context = BrowserContext.from(await this._channel.launchPersistentContext({ userDataDir, ...options }));
59+
const result = await this._channel.launchPersistentContext({ userDataDir, ...options });
60+
const context = BrowserContext.from(result.context);
6061
context._logger = logger;
6162
return context;
6263
}
6364

6465
async connect(options: types.ConnectOptions & { logger?: LoggerSink }): Promise<Browser> {
6566
const logger = options.logger;
6667
options = { ...options, logger: undefined };
67-
const browser = Browser.from(await this._channel.connect(options));
68+
const browser = Browser.from((await this._channel.connect(options)).browser);
6869
browser._logger = logger;
6970
return browser;
7071
}

src/rpc/client/cdpSession.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export class CDPSession extends ChannelOwner<CDPSessionChannel, CDPSessionInitia
4747
params?: Protocol.CommandParameters[T]
4848
): Promise<Protocol.CommandReturnValues[T]> {
4949
const result = await this._channel.send({ method, params });
50-
return result as Protocol.CommandReturnValues[T];
50+
return result.result as Protocol.CommandReturnValues[T];
5151
}
5252

5353
async detach() {

src/rpc/client/chromiumBrowser.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ import { Browser } from './browser';
2020

2121
export class ChromiumBrowser extends Browser {
2222
async newBrowserCDPSession(): Promise<CDPSession> {
23-
return CDPSession.from(await this._channel.crNewBrowserCDPSession());
23+
return CDPSession.from((await this._channel.crNewBrowserCDPSession()).session);
2424
}
2525

2626
async startTracing(page?: Page, options: { path?: string; screenshots?: boolean; categories?: string[]; } = {}) {
2727
await this._channel.crStartTracing({ ...options, page: page ? page._channel : undefined });
2828
}
2929

3030
async stopTracing(): Promise<Buffer> {
31-
return Buffer.from(await this._channel.crStopTracing(), 'base64');
31+
return Buffer.from((await this._channel.crStopTracing()).binary, 'base64');
3232
}
3333
}

src/rpc/client/chromiumBrowserContext.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,16 @@ export class ChromiumBrowserContext extends BrowserContext {
2929

3030
constructor(parent: ChannelOwner, type: string, guid: string, initializer: BrowserContextInitializer) {
3131
super(parent, type, guid, initializer, 'chromium');
32-
this._channel.on('crBackgroundPage', pageChannel => {
33-
const page = Page.from(pageChannel);
34-
this._backgroundPages.add(page);
35-
this.emit(ChromiumEvents.CRBrowserContext.BackgroundPage, page);
32+
this._channel.on('crBackgroundPage', ({ page }) => {
33+
const backgroundPage = Page.from(page);
34+
this._backgroundPages.add(backgroundPage);
35+
this.emit(ChromiumEvents.CRBrowserContext.BackgroundPage, backgroundPage);
3636
});
37-
this._channel.on('crServiceWorker', serviceWorkerChannel => {
38-
const worker = Worker.from(serviceWorkerChannel);
39-
worker._context = this;
40-
this._serviceWorkers.add(worker);
41-
this.emit(ChromiumEvents.CRBrowserContext.ServiceWorker, worker);
37+
this._channel.on('crServiceWorker', ({worker}) => {
38+
const serviceWorker = Worker.from(worker);
39+
serviceWorker._context = this;
40+
this._serviceWorkers.add(serviceWorker);
41+
this.emit(ChromiumEvents.CRBrowserContext.ServiceWorker, serviceWorker);
4242
});
4343
}
4444

@@ -51,6 +51,7 @@ export class ChromiumBrowserContext extends BrowserContext {
5151
}
5252

5353
async newCDPSession(page: Page): Promise<CDPSession> {
54-
return CDPSession.from(await this._channel.crNewCDPSession({ page: page._channel }));
54+
const result = await this._channel.crNewCDPSession({ page: page._channel });
55+
return CDPSession.from(result.session);
5556
}
5657
}

src/rpc/client/coverage.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ export class Coverage {
2929
}
3030

3131
async stopJSCoverage(): Promise<types.JSCoverageEntry[]> {
32-
return await this._channel.crStopJSCoverage();
32+
return (await this._channel.crStopJSCoverage()).entries;
3333
}
3434

3535
async startCSSCoverage(options: types.CSSCoverageOptions = {}) {
3636
await this._channel.crStartCSSCoverage(options);
3737
}
3838

3939
async stopCSSCoverage(): Promise<types.CSSCoverageEntry[]> {
40-
return await this._channel.crStopCSSCoverage();
40+
return (await this._channel.crStopCSSCoverage()).entries;
4141
}
4242
}

src/rpc/client/download.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,18 @@ export class Download extends ChannelOwner<DownloadChannel, DownloadInitializer>
3737
}
3838

3939
async path(): Promise<string | null> {
40-
return this._channel.path();
40+
return (await this._channel.path()).value;
4141
}
4242

4343
async failure(): Promise<string | null> {
44-
return this._channel.failure();
44+
return (await this._channel.failure()).error;
4545
}
4646

4747
async createReadStream(): Promise<Readable | null> {
48-
const s = await this._channel.stream();
49-
if (!s)
48+
const result = await this._channel.stream();
49+
if (!result.stream)
5050
return null;
51-
const stream = Stream.from(s);
51+
const stream = Stream.from(result.stream);
5252
return stream.stream();
5353
}
5454

0 commit comments

Comments
 (0)