Skip to content

Commit 5848ed8

Browse files
authored
feat(rpc): introduce protocol.pdl (#3054)
We now generate channels.ts from the protocol definition. There are still some shortcomings, like union types - these will be addressed in follow ups.
1 parent 726f636 commit 5848ed8

28 files changed

+3373
-447
lines changed

src/rpc/channels.ts

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

src/rpc/client/accessibility.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ export class Accessibility {
2929
async snapshot(options: { interestingOnly?: boolean; root?: ElementHandle } = {}): Promise<types.SerializedAXNode | null> {
3030
const root = options.root ? options.root._elementChannel : undefined;
3131
const result = await this._channel.accessibilitySnapshot({ interestingOnly: options.interestingOnly, root });
32-
return result.rootAXNode;
32+
return result.rootAXNode || null;
3333
}
3434
}

src/rpc/client/browser.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
import * as types from '../../types';
18-
import { BrowserChannel, BrowserInitializer, BrowserContextOptions } from '../channels';
18+
import { BrowserChannel, BrowserInitializer, BrowserNewContextParams } from '../channels';
1919
import { BrowserContext } from './browserContext';
2020
import { Page } from './page';
2121
import { ChannelOwner } from './channelOwner';
@@ -55,7 +55,7 @@ export class Browser extends ChannelOwner<BrowserChannel, BrowserInitializer> {
5555
const logger = options.logger;
5656
options = { ...options, logger: undefined };
5757
return this._wrapApiCall('browser.newContext', async () => {
58-
const contextOptions: BrowserContextOptions = {
58+
const contextOptions: BrowserNewContextParams = {
5959
...options,
6060
extraHTTPHeaders: options.extraHTTPHeaders ? headersObjectToArray(options.extraHTTPHeaders) : undefined,
6161
};

src/rpc/client/browserContext.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ export class BrowserContext extends ChannelOwner<BrowserContextChannel, BrowserC
141141

142142
async setGeolocation(geolocation: types.Geolocation | null): Promise<void> {
143143
return this._wrapApiCall('browserContext.setGeolocation', async () => {
144-
await this._channel.setGeolocation({ geolocation });
144+
await this._channel.setGeolocation({ geolocation: geolocation || undefined });
145145
});
146146
}
147147

@@ -159,7 +159,7 @@ export class BrowserContext extends ChannelOwner<BrowserContextChannel, BrowserC
159159

160160
async setHTTPCredentials(httpCredentials: types.Credentials | null): Promise<void> {
161161
return this._wrapApiCall('browserContext.setHTTPCredentials', async () => {
162-
await this._channel.setHTTPCredentials({ httpCredentials });
162+
await this._channel.setHTTPCredentials({ httpCredentials: httpCredentials || undefined });
163163
});
164164
}
165165

src/rpc/client/browserType.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
import * as types from '../../types';
18-
import { BrowserTypeChannel, BrowserTypeInitializer, LaunchPersistentContextOptions, LaunchOptions, LaunchServerOptions } from '../channels';
18+
import { BrowserTypeChannel, BrowserTypeInitializer, BrowserTypeLaunchParams, BrowserTypeLaunchServerParams, BrowserTypeLaunchPersistentContextParams } from '../channels';
1919
import { Browser } from './browser';
2020
import { BrowserContext } from './browserContext';
2121
import { ChannelOwner } from './channelOwner';
@@ -45,7 +45,7 @@ export class BrowserType extends ChannelOwner<BrowserTypeChannel, BrowserTypeIni
4545
const logger = options.logger;
4646
options = { ...options, logger: undefined };
4747
return this._wrapApiCall('browserType.launch', async () => {
48-
const launchOptions: LaunchOptions = {
48+
const launchOptions: BrowserTypeLaunchParams = {
4949
...options,
5050
ignoreDefaultArgs: Array.isArray(options.ignoreDefaultArgs) ? options.ignoreDefaultArgs : undefined,
5151
ignoreAllDefaultArgs: !!options.ignoreDefaultArgs && !Array.isArray(options.ignoreDefaultArgs),
@@ -61,7 +61,7 @@ export class BrowserType extends ChannelOwner<BrowserTypeChannel, BrowserTypeIni
6161
const logger = options.logger;
6262
options = { ...options, logger: undefined };
6363
return this._wrapApiCall('browserType.launchServer', async () => {
64-
const launchServerOptions: LaunchServerOptions = {
64+
const launchServerOptions: BrowserTypeLaunchServerParams = {
6565
...options,
6666
ignoreDefaultArgs: Array.isArray(options.ignoreDefaultArgs) ? options.ignoreDefaultArgs : undefined,
6767
ignoreAllDefaultArgs: !!options.ignoreDefaultArgs && !Array.isArray(options.ignoreDefaultArgs),
@@ -75,7 +75,7 @@ export class BrowserType extends ChannelOwner<BrowserTypeChannel, BrowserTypeIni
7575
const logger = options.logger;
7676
options = { ...options, logger: undefined };
7777
return this._wrapApiCall('browserType.launchPersistentContext', async () => {
78-
const persistentOptions: LaunchPersistentContextOptions = {
78+
const persistentOptions: BrowserTypeLaunchPersistentContextParams = {
7979
...options,
8080
ignoreDefaultArgs: Array.isArray(options.ignoreDefaultArgs) ? options.ignoreDefaultArgs : undefined,
8181
ignoreAllDefaultArgs: !!options.ignoreDefaultArgs && !Array.isArray(options.ignoreDefaultArgs),

src/rpc/client/download.ts

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

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

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

4747
async createReadStream(): Promise<Readable | null> {

src/rpc/client/electron.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,18 @@
1515
*/
1616

1717
import * as types from '../../types';
18-
import { ElectronChannel, ElectronInitializer, ElectronLaunchOptions, ElectronApplicationChannel, ElectronApplicationInitializer } from '../channels';
18+
import { ElectronChannel, ElectronInitializer, ElectronApplicationChannel, ElectronApplicationInitializer, ElectronLaunchParams } from '../channels';
1919
import { BrowserContext } from './browserContext';
2020
import { ChannelOwner } from './channelOwner';
2121
import { Page } from './page';
2222
import { serializeArgument, FuncOn, parseResult, SmartHandle, JSHandle } from './jsHandle';
23-
import { ElectronEvents } from '../../server/electron';
23+
import { ElectronEvents, ElectronLaunchOptionsBase } from '../../server/electron';
2424
import { TimeoutSettings } from '../../timeoutSettings';
2525
import { Waiter } from './waiter';
2626
import { TimeoutError } from '../../errors';
2727
import { Events } from '../../events';
2828
import { LoggerSink } from '../../loggerSink';
29+
import { envObjectToArray } from '../serializers';
2930

3031
export class Electron extends ChannelOwner<ElectronChannel, ElectronInitializer> {
3132
static from(electron: ElectronChannel): Electron {
@@ -36,11 +37,16 @@ export class Electron extends ChannelOwner<ElectronChannel, ElectronInitializer>
3637
super(parent, type, guid, initializer, true);
3738
}
3839

39-
async launch(executablePath: string, options: ElectronLaunchOptions & { logger?: LoggerSink } = {}): Promise<ElectronApplication> {
40+
async launch(executablePath: string, options: ElectronLaunchOptionsBase & { logger?: LoggerSink } = {}): Promise<ElectronApplication> {
4041
const logger = options.logger;
4142
options = { ...options, logger: undefined };
4243
return this._wrapApiCall('electron.launch', async () => {
43-
return ElectronApplication.from((await this._channel.launch({ executablePath, ...options })).electronApplication);
44+
const params: ElectronLaunchParams = {
45+
...options,
46+
env: options.env ? envObjectToArray(options.env) : undefined,
47+
executablePath,
48+
};
49+
return ElectronApplication.from((await this._channel.launch(params)).electronApplication);
4450
}, logger);
4551
}
4652
}

src/rpc/client/elementHandle.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export class ElementHandle<T extends Node = Node> extends JSHandle<T> {
2929
return (handle as any)._object;
3030
}
3131

32-
static fromNullable(handle: ElementHandleChannel | null): ElementHandle | null {
32+
static fromNullable(handle: ElementHandleChannel | undefined): ElementHandle | null {
3333
return handle ? ElementHandle.from(handle) : null;
3434
}
3535

@@ -56,13 +56,15 @@ export class ElementHandle<T extends Node = Node> extends JSHandle<T> {
5656

5757
async getAttribute(name: string): Promise<string | null> {
5858
return this._wrapApiCall('elementHandle.getAttribute', async () => {
59-
return (await this._elementChannel.getAttribute({ name })).value;
59+
const value = (await this._elementChannel.getAttribute({ name })).value;
60+
return value === undefined ? null : value;
6061
});
6162
}
6263

6364
async textContent(): Promise<string | null> {
6465
return this._wrapApiCall('elementHandle.textContent', async () => {
65-
return (await this._elementChannel.textContent()).value;
66+
const value = (await this._elementChannel.textContent()).value;
67+
return value === undefined ? null : value;
6668
});
6769
}
6870

@@ -165,7 +167,8 @@ export class ElementHandle<T extends Node = Node> extends JSHandle<T> {
165167

166168
async boundingBox(): Promise<types.Rect | null> {
167169
return this._wrapApiCall('elementHandle.boundingBox', async () => {
168-
return (await this._elementChannel.boundingBox()).value;
170+
const value = (await this._elementChannel.boundingBox()).value;
171+
return value === undefined ? null : value;
169172
});
170173
}
171174

src/rpc/client/frame.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export class Frame extends ChannelOwner<FrameChannel, FrameInitializer> {
5353
return (frame as any)._object;
5454
}
5555

56-
static fromNullable(frame: FrameChannel | null): Frame | null {
56+
static fromNullable(frame: FrameChannel | undefined): Frame | null {
5757
return frame ? Frame.from(frame) : null;
5858
}
5959

@@ -132,7 +132,7 @@ export class Frame extends ChannelOwner<FrameChannel, FrameInitializer> {
132132
});
133133
}
134134

135-
const request = navigatedEvent.newDocument ? network.Request.fromNullable(navigatedEvent.newDocument.request || null) : null;
135+
const request = navigatedEvent.newDocument ? network.Request.fromNullable(navigatedEvent.newDocument.request) : null;
136136
const response = request ? await waiter.waitForPromise(request._finalRequest().response()) : null;
137137
waiter.dispose();
138138
return response;
@@ -306,7 +306,8 @@ export class Frame extends ChannelOwner<FrameChannel, FrameInitializer> {
306306

307307
async textContent(selector: string, options: types.TimeoutOptions = {}): Promise<null|string> {
308308
return this._wrapApiCall(this._apiName('textContent'), async () => {
309-
return (await this._channel.textContent({ selector, ...options })).value;
309+
const value = (await this._channel.textContent({ selector, ...options })).value;
310+
return value === undefined ? null : value;
310311
});
311312
}
312313

@@ -324,7 +325,8 @@ export class Frame extends ChannelOwner<FrameChannel, FrameInitializer> {
324325

325326
async getAttribute(selector: string, name: string, options: types.TimeoutOptions = {}): Promise<string | null> {
326327
return this._wrapApiCall(this._apiName('getAttribute'), async () => {
327-
return (await this._channel.getAttribute({ selector, name, ...options })).value;
328+
const value = (await this._channel.getAttribute({ selector, name, ...options })).value;
329+
return value === undefined ? null : value;
328330
});
329331
}
330332

src/rpc/client/jsHandle.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { JSHandleChannel, JSHandleInitializer, SerializedArgument, Channel } from '../channels';
17+
import { JSHandleChannel, JSHandleInitializer, SerializedArgument, SerializedValue, Channel } from '../channels';
1818
import { ElementHandle } from './elementHandle';
1919
import { ChannelOwner } from './channelOwner';
20-
import { serializeAsCallArgument, parseEvaluationResultValue, SerializedValue } from '../../common/utilityScriptSerializers';
20+
import { serializeAsCallArgument, parseEvaluationResultValue } from '../../common/utilityScriptSerializers';
2121

2222
type NoHandles<Arg> = Arg extends JSHandle ? never : (Arg extends object ? { [Key in keyof Arg]: NoHandles<Arg[Key]> } : Arg);
2323
type Unboxed<Arg> =
@@ -42,10 +42,6 @@ export class JSHandle<T = any> extends ChannelOwner<JSHandleChannel, JSHandleIni
4242
return (handle as any)._object;
4343
}
4444

45-
static fromNullable(handle: JSHandleChannel | null): JSHandle | null {
46-
return handle ? JSHandle.from(handle) : null;
47-
}
48-
4945
constructor(parent: ChannelOwner, type: string, guid: string, initializer: JSHandleInitializer) {
5046
super(parent, type, guid, initializer);
5147
this._preview = this._initializer.preview;
@@ -112,5 +108,5 @@ export function serializeArgument(arg: any): SerializedArgument {
112108
}
113109

114110
export function parseResult(arg: SerializedValue): any {
115-
return parseEvaluationResultValue(arg, []);
111+
return parseEvaluationResultValue(arg as any, []);
116112
}

0 commit comments

Comments
 (0)