Skip to content

Commit

Permalink
chore(types): generate a d.ts file for protocol types (#2325)
Browse files Browse the repository at this point in the history
This uses the `/json/protocol` endpoint to generate type definitions for the protocol.

Currently it is lacking protocol events and commands, but I will add those later.
  • Loading branch information
JoelEinbinder authored and aslushnikov committed Apr 7, 2018
1 parent 35e34db commit 8c54f41
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -10,3 +10,4 @@
package-lock.json
yarn.lock
/node6
/lib/protocol.d.ts
4 changes: 4 additions & 0 deletions .npmignore
Expand Up @@ -2,6 +2,10 @@
test
utils/node6-transform

# exclude internal type definition files
/lib/*.d.ts
/node6/lib/*.d.ts

# repeats from .gitignore
node_modules
.local-chromium
Expand Down
2 changes: 1 addition & 1 deletion lib/ElementHandle.js
Expand Up @@ -21,7 +21,7 @@ class ElementHandle extends JSHandle {
/**
* @param {!Puppeteer.ExecutionContext} context
* @param {!Puppeteer.CDPSession} client
* @param {!Object} remoteObject
* @param {!Protocol.Runtime.RemoteObject} remoteObject
* @param {!Puppeteer.Page} page
* @param {!Puppeteer.FrameManager} frameManager
*/
Expand Down
4 changes: 2 additions & 2 deletions lib/ExecutionContext.js
Expand Up @@ -19,7 +19,7 @@ const {helper} = require('./helper');
class ExecutionContext {
/**
* @param {!Puppeteer.CDPSession} client
* @param {!Object} contextPayload
* @param {!Protocol.Runtime.ExecutionContextDescription} contextPayload
* @param {function(*):!JSHandle} objectHandleFactory
* @param {?Puppeteer.Frame} frame
*/
Expand Down Expand Up @@ -127,7 +127,7 @@ class JSHandle {
/**
* @param {!ExecutionContext} context
* @param {!Puppeteer.CDPSession} client
* @param {!Object} remoteObject
* @param {!Protocol.Runtime.RemoteObject} remoteObject
*/
constructor(context, client, remoteObject) {
this._context = context;
Expand Down
4 changes: 2 additions & 2 deletions lib/FrameManager.js
Expand Up @@ -111,7 +111,7 @@ class FrameManager extends EventEmitter {
}

/**
* @param {!Object} framePayload
* @param {!Protocol.Page.Frame} framePayload
*/
_onFrameNavigated(framePayload) {
const isMainFrame = !framePayload.parentId;
Expand Down Expand Up @@ -743,7 +743,7 @@ class Frame {
}

/**
* @param {!Object} framePayload
* @param {!Protocol.Page.Frame} framePayload
*/
_navigated(framePayload) {
this._name = framePayload.name;
Expand Down
6 changes: 3 additions & 3 deletions lib/NetworkManager.js
Expand Up @@ -202,7 +202,7 @@ class NetworkManager extends EventEmitter {
* @param {?string} interceptionId
* @param {string} url
* @param {string} resourceType
* @param {!Object} requestPayload
* @param {!Protocol.Network.Request} requestPayload
* @param {?string} frameId
* @param {!Array<!Request>} redirectChain
*/
Expand Down Expand Up @@ -306,7 +306,7 @@ class Request {
* @param {boolean} allowInterception
* @param {string} url
* @param {string} resourceType
* @param {!Object} payload
* @param {!Protocol.Network.Request} payload
* @param {?Puppeteer.Frame} frame
* @param {!Array<!Request>} redirectChain
*/
Expand Down Expand Up @@ -630,7 +630,7 @@ class Response {
helper.tracePublicAPI(Response);

/**
* @param {!Object} request
* @param {!Protocol.Network.Request} request
* @return {string}
*/
function generateRequestHash(request) {
Expand Down
2 changes: 1 addition & 1 deletion lib/Page.js
Expand Up @@ -391,7 +391,7 @@ class Page extends EventEmitter {
}

/**
* @param {?Array<!{name: string, value: number}>} metrics
* @param {?Array<!Protocol.Performance.Metric>} metrics
* @return {!Object}
*/
_buildMetricsObject(metrics) {
Expand Down
6 changes: 3 additions & 3 deletions lib/helper.js
Expand Up @@ -56,7 +56,7 @@ class Helper {
}

/**
* @param {!Object} exceptionDetails
* @param {!Protocol.Runtime.ExceptionDetails} exceptionDetails
* @return {string}
*/
static getExceptionMessage(exceptionDetails) {
Expand All @@ -74,7 +74,7 @@ class Helper {
}

/**
* @param {!Object} remoteObject
* @param {!Protocol.Runtime.RemoteObject} remoteObject
* @return {*}
*/
static valueFromRemoteObject(remoteObject) {
Expand All @@ -98,7 +98,7 @@ class Helper {

/**
* @param {!Puppeteer.CDPSession} client
* @param {!Object} remoteObject
* @param {!Protocol.Runtime.RemoteObject} remoteObject
*/
static async releaseObject(client, remoteObject) {
if (!remoteObject.objectId)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -19,7 +19,7 @@
"test-node6-transformer": "node utils/node6-transform/test/test.js",
"build": "node utils/node6-transform/index.js",
"unit-node6": "node node6/test/test.js",
"tsc": "tsc -p .",
"tsc": "node utils/protocol-types-generator && tsc -p .",
"prepublishOnly": "npm run build",
"apply-next-version": "node utils/apply_next_version.js"
},
Expand Down
54 changes: 54 additions & 0 deletions utils/protocol-types-generator/index.js
@@ -0,0 +1,54 @@
const puppeteer = require('../..');
puppeteer.launch({
pipe: false,
executablePath: process.env.CHROME,
args: ['--no-sandbox', '--disable-dev-shm-usage']
}).then(async browser => {
const origin = browser.wsEndpoint().match(/ws:\/\/([0-9A-Za-z:\.]*)\//)[1];
const page = await browser.newPage();
await page.goto(`http://${origin}/json/protocol`);
const json = JSON.parse(await page.evaluate(() => document.documentElement.innerText));
await browser.close();
const output = `// This is generated from /utils/protocol-types-generator/index.js
declare global {
module Protocol {
${json.domains.map(domain => `${domain.description ? `
/**
* ${domain.description}
*/` : ''}
export module ${domain.domain} {
${(domain.types || []).map(type => `${type.description ? `
/**
* ${type.description}
*/` : ''}${type.properties ? `
export interface ${type.id} {
${(type.properties || []).map(property => `${property.description ? `
/**
* ${property.description}
*/` : ''}
${property.name}${property.optional ? '?' : ''}: ${typeOfProperty(property)};
`).join(``)}
}` : `
export type ${type.id} = ${typeOfProperty(type)};`}
`).join('')}
}
`).join('')}
}
}
// empty export to keep file a module
export {}
`;
require('fs').writeFileSync(require('path').join(__dirname, '..', '..', 'lib', 'protocol.d.ts'), output);
});

function typeOfProperty(property) {
if (property.$ref) return property.$ref;
if (property.enum) return property.enum.map(value => JSON.stringify(value)).join('|');
switch (property.type) {
case 'array':
return typeOfProperty(property.items) + '[]';
case 'integer':
return 'number';
}
return property.type;
}

0 comments on commit 8c54f41

Please sign in to comment.