Skip to content

Commit

Permalink
chore: Use devtools-protocol package
Browse files Browse the repository at this point in the history
Rather than maintain our own protocol we can instead use the devtools-protocol package and pin it to the version of Chromium that Puppeteer is shipping with.

The only changes are naming changes between the bespoke protocol that Puppeteer created and the devtools-protocol one.
  • Loading branch information
jackfranklin committed Jul 7, 2020
1 parent 2256b8d commit 72a603c
Show file tree
Hide file tree
Showing 25 changed files with 93 additions and 15,861 deletions.
2 changes: 0 additions & 2 deletions .eslintignore
Expand Up @@ -6,8 +6,6 @@ node6/*
node6-test/*
experimental/
lib/
src/externs.d.ts
src/protocol.d.ts
/index.d.ts
# We ignore this file because it uses ES imports which we don't yet use
# in the Puppeteer src, so it trips up the ESLint-TypeScript parser.
Expand Down
1 change: 0 additions & 1 deletion .travis.yml
Expand Up @@ -63,7 +63,6 @@ jobs:
env:
- CHROMIUM=true
script:
- npm run compare-protocol-d-ts
- npm run lint
- npm run test-doclint
- npm run ensure-new-docs-up-to-date
Expand Down
7 changes: 3 additions & 4 deletions package.json
Expand Up @@ -25,12 +25,10 @@
"doc": "node utils/doclint/cli.js",
"clean-lib": "rm -rf lib",
"tsc": "npm run clean-lib && tsc --version && npm run tsc-cjs && npm run tsc-esm",
"tsc-cjs": "tsc -p . && cp src/protocol.d.ts lib/cjs",
"tsc-esm": "tsc --build tsconfig-esm.json && cp src/protocol.d.ts lib/esm",
"tsc-cjs": "tsc -p .",
"tsc-esm": "tsc --build tsconfig-esm.json",
"typecheck": "tsc -p . --noEmit",
"apply-next-version": "node utils/apply_next_version.js",
"update-protocol-d-ts": "node utils/protocol-types-generator update",
"compare-protocol-d-ts": "node utils/protocol-types-generator compare",
"test-install": "scripts/test-install.sh",
"generate-docs": "npm run tsc && api-extractor run --local --verbose && api-documenter markdown -i temp -o new-docs",
"ensure-new-docs-up-to-date": "npm run generate-docs && exit `git status --porcelain | head -255 | wc -l`",
Expand All @@ -47,6 +45,7 @@
"license": "Apache-2.0",
"dependencies": {
"debug": "^4.1.0",
"devtools-protocol": "0.0.758979",
"extract-zip": "^2.0.0",
"https-proxy-agent": "^4.0.0",
"mime": "^2.0.3",
Expand Down
2 changes: 1 addition & 1 deletion src/common/Accessibility.ts
Expand Up @@ -16,7 +16,7 @@

import { CDPSession } from './Connection';
import { ElementHandle } from './JSHandle';
import Protocol from '../protocol';
import { Protocol } from 'devtools-protocol';

/**
* Represents a Node and the properties of it that are relevant to Accessibility.
Expand Down
8 changes: 4 additions & 4 deletions src/common/Browser.ts
Expand Up @@ -19,7 +19,7 @@ import { helper } from './helper';
import { Target } from './Target';
import { EventEmitter } from './EventEmitter';
import { Events } from './Events';
import Protocol from '../protocol';
import { Protocol } from 'devtools-protocol';
import { Connection } from './Connection';
import { Page } from './Page';
import { ChildProcess } from 'child_process';
Expand Down Expand Up @@ -273,7 +273,7 @@ export class Browser extends EventEmitter {
}

private async _targetCreated(
event: Protocol.Target.targetCreatedPayload
event: Protocol.Target.TargetCreatedEvent
): Promise<void> {
const targetInfo = event.targetInfo;
const { browserContextId } = targetInfo;
Expand Down Expand Up @@ -315,7 +315,7 @@ export class Browser extends EventEmitter {
}

private _targetInfoChanged(
event: Protocol.Target.targetInfoChangedPayload
event: Protocol.Target.TargetInfoChangedEvent
): void {
const target = this._targets.get(event.targetInfo.targetId);
assert(target, 'target should exist before targetInfoChanged');
Expand Down Expand Up @@ -501,7 +501,7 @@ export class Browser extends EventEmitter {
return !this._connection._closed;
}

private _getVersion(): Promise<Protocol.Browser.getVersionReturnValue> {
private _getVersion(): Promise<Protocol.Browser.GetVersionResponse> {
return this._connection.send('Browser.getVersion');
}
}
Expand Down
15 changes: 8 additions & 7 deletions src/common/Connection.ts
Expand Up @@ -19,7 +19,8 @@ import { debug } from './Debug';
const debugProtocolSend = debug('puppeteer:protocol:SEND ►');
const debugProtocolReceive = debug('puppeteer:protocol:RECV ◀');

import Protocol from '../protocol';
import { Protocol } from 'devtools-protocol';
import { ProtocolMapping } from 'devtools-protocol/types/protocol-mapping';
import { ConnectionTransport } from './ConnectionTransport';
import { EventEmitter } from './EventEmitter';

Expand Down Expand Up @@ -66,10 +67,10 @@ export class Connection extends EventEmitter {
return this._url;
}

send<T extends keyof Protocol.CommandParameters>(
send<T extends keyof ProtocolMapping.Commands>(
method: T,
params?: Protocol.CommandParameters[T]
): Promise<Protocol.CommandReturnValues[T]> {
params?: ProtocolMapping.Commands[T]['paramsType'][0]
): Promise<ProtocolMapping.Commands[T]['returnType']> {
const id = this._rawSend({ method, params });
return new Promise((resolve, reject) => {
this._callbacks.set(id, { resolve, reject, error: new Error(), method });
Expand Down Expand Up @@ -212,10 +213,10 @@ export class CDPSession extends EventEmitter {
this._sessionId = sessionId;
}

send<T extends keyof Protocol.CommandParameters>(
send<T extends keyof ProtocolMapping.Commands>(
method: T,
params?: Protocol.CommandParameters[T]
): Promise<Protocol.CommandReturnValues[T]> {
params?: ProtocolMapping.Commands[T]['paramsType'][0]
): Promise<ProtocolMapping.Commands[T]['returnType']> {
if (!this._connection)
return Promise.reject(
new Error(
Expand Down
16 changes: 7 additions & 9 deletions src/common/Coverage.ts
Expand Up @@ -16,7 +16,7 @@

import { assert } from './assert';
import { helper, debugError, PuppeteerEventListener } from './helper';
import Protocol from '../protocol';
import { Protocol } from 'devtools-protocol';
import { CDPSession } from './Connection';

import { EVALUATION_SCRIPT_URL } from './ExecutionContext';
Expand Down Expand Up @@ -223,7 +223,7 @@ class JSCoverage {
}

async _onScriptParsed(
event: Protocol.Debugger.scriptParsedPayload
event: Protocol.Debugger.ScriptParsedEvent
): Promise<void> {
// Ignore puppeteer-injected scripts
if (event.url === EVALUATION_SCRIPT_URL) return;
Expand All @@ -246,10 +246,10 @@ class JSCoverage {
this._enabled = false;

const result = await Promise.all<
Protocol.Profiler.takePreciseCoverageReturnValue,
Protocol.Profiler.stopPreciseCoverageReturnValue,
Protocol.Profiler.disableReturnValue,
Protocol.Debugger.disableReturnValue
Protocol.Profiler.TakePreciseCoverageResponse,
void,
void,
void
>([
this._client.send('Profiler.takePreciseCoverage'),
this._client.send('Profiler.stopPreciseCoverage'),
Expand Down Expand Up @@ -322,9 +322,7 @@ class CSSCoverage {
this._stylesheetSources.clear();
}

async _onStyleSheet(
event: Protocol.CSS.styleSheetAddedPayload
): Promise<void> {
async _onStyleSheet(event: Protocol.CSS.StyleSheetAddedEvent): Promise<void> {
const header = event.header;
// Ignore anonymous scripts
if (!header.sourceURL) return;
Expand Down
2 changes: 1 addition & 1 deletion src/common/Dialog.ts
Expand Up @@ -16,7 +16,7 @@

import { assert } from './assert';
import { CDPSession } from './Connection';
import Protocol from '../protocol';
import { Protocol } from 'devtools-protocol';

/**
* Dialog instances are dispatched by the {@link Page} via the `dialog` event.
Expand Down
2 changes: 1 addition & 1 deletion src/common/EmulationManager.ts
Expand Up @@ -15,7 +15,7 @@
*/
import { CDPSession } from './Connection';
import { Viewport } from './PuppeteerViewport';
import Protocol from '../protocol';
import { Protocol } from 'devtools-protocol';

export class EmulationManager {
_client: CDPSession;
Expand Down
4 changes: 2 additions & 2 deletions src/common/ExecutionContext.ts
Expand Up @@ -20,7 +20,7 @@ import { createJSHandle, JSHandle, ElementHandle } from './JSHandle';
import { CDPSession } from './Connection';
import { DOMWorld } from './DOMWorld';
import { Frame } from './FrameManager';
import Protocol from '../protocol';
import { Protocol } from 'devtools-protocol';
import { EvaluateHandleFn, SerializableOrJSHandle } from './EvalTypes';

export const EVALUATION_SCRIPT_URL = '__puppeteer_evaluation_script__';
Expand Down Expand Up @@ -301,7 +301,7 @@ export class ExecutionContext {
return { value: arg };
}

function rewriteError(error: Error): Protocol.Runtime.evaluateReturnValue {
function rewriteError(error: Error): Protocol.Runtime.EvaluateResponse {
if (error.message.includes('Object reference chain is too long'))
return { result: { type: 'undefined' } };
if (error.message.includes("Object couldn't be returned by value"))
Expand Down
4 changes: 2 additions & 2 deletions src/common/FileChooser.ts
Expand Up @@ -15,7 +15,7 @@
*/

import { ElementHandle } from './JSHandle';
import Protocol from '../protocol';
import { Protocol } from 'devtools-protocol';
import { assert } from './assert';

/**
Expand Down Expand Up @@ -45,7 +45,7 @@ export class FileChooser {
*/
constructor(
element: ElementHandle,
event: Protocol.Page.fileChooserOpenedPayload
event: Protocol.Page.FileChooserOpenedEvent
) {
this._element = element;
this._multiple = event.mode !== 'selectSingle';
Expand Down
11 changes: 4 additions & 7 deletions src/common/FrameManager.ts
Expand Up @@ -28,7 +28,7 @@ import { JSHandle, ElementHandle } from './JSHandle';
import { MouseButton } from './Input';
import { Page } from './Page';
import { HTTPResponse } from './HTTPResponse';
import Protocol from '../protocol';
import { Protocol } from 'devtools-protocol';
import {
SerializableOrJSHandle,
EvaluateHandleFn,
Expand Down Expand Up @@ -91,10 +91,7 @@ export class FrameManager extends EventEmitter {
}

async initialize(): Promise<void> {
const result = await Promise.all<
Protocol.Page.enableReturnValue,
Protocol.Page.getFrameTreeReturnValue
>([
const result = await Promise.all<{}, Protocol.Page.GetFrameTreeResponse>([
this._client.send('Page.enable'),
this._client.send('Page.getFrameTree'),
]);
Expand All @@ -104,7 +101,7 @@ export class FrameManager extends EventEmitter {
await Promise.all([
this._client.send('Page.setLifecycleEventsEnabled', { enabled: true }),
this._client
.send('Runtime.enable', {})
.send('Runtime.enable')
.then(() => this._ensureIsolatedWorld(UTILITY_WORLD_NAME)),
this._networkManager.initialize(),
]);
Expand Down Expand Up @@ -193,7 +190,7 @@ export class FrameManager extends EventEmitter {
return watcher.navigationResponse();
}

_onLifecycleEvent(event: Protocol.Page.lifecycleEventPayload): void {
_onLifecycleEvent(event: Protocol.Page.LifecycleEventEvent): void {
const frame = this._frames.get(event.frameId);
if (!frame) return;
frame._onLifecycleEvent(event.loaderId, event.name);
Expand Down
4 changes: 2 additions & 2 deletions src/common/HTTPRequest.ts
Expand Up @@ -18,7 +18,7 @@ import { Frame } from './FrameManager';
import { HTTPResponse } from './HTTPResponse';
import { assert } from './assert';
import { helper, debugError } from './helper';
import Protocol from '../protocol';
import { Protocol } from 'devtools-protocol';

/**
* @public
Expand Down Expand Up @@ -123,7 +123,7 @@ export class HTTPRequest {
frame: Frame,
interceptionId: string,
allowInterception: boolean,
event: Protocol.Network.requestWillBeSentPayload,
event: Protocol.Network.RequestWillBeSentEvent,
redirectChain: HTTPRequest[]
) {
this._client = client;
Expand Down
2 changes: 1 addition & 1 deletion src/common/HTTPResponse.ts
Expand Up @@ -17,7 +17,7 @@ import { CDPSession } from './Connection';
import { Frame } from './FrameManager';
import { HTTPRequest } from './HTTPRequest';
import { SecurityDetails } from './SecurityDetails';
import Protocol from '../protocol';
import { Protocol } from 'devtools-protocol';

/**
* @public
Expand Down
11 changes: 6 additions & 5 deletions src/common/JSHandle.ts
Expand Up @@ -22,7 +22,7 @@ import { CDPSession } from './Connection';
import { KeyInput } from './USKeyboardLayout';
import { FrameManager, Frame } from './FrameManager';
import { getQueryHandlerAndSelector } from './QueryHandler';
import Protocol from '../protocol';
import { Protocol } from 'devtools-protocol';
import {
EvaluateFn,
SerializableOrJSHandle,
Expand Down Expand Up @@ -445,11 +445,12 @@ export class ElementHandle<
};
}

private _getBoxModel(): Promise<void | Protocol.DOM.getBoxModelReturnValue> {
private _getBoxModel(): Promise<void | Protocol.DOM.GetBoxModelResponse> {
const params: Protocol.DOM.GetBoxModelRequest = {
objectId: this._remoteObject.objectId,
};
return this._client
.send('DOM.getBoxModel', {
objectId: this._remoteObject.objectId,
})
.send('DOM.getBoxModel', params)
.catch((error) => debugError(error));
}

Expand Down

0 comments on commit 72a603c

Please sign in to comment.