Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(types): generate a d.ts file for protocol types #2325

Merged
merged 2 commits into from
Apr 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
package-lock.json
yarn.lock
/node6
/lib/protocol.d.ts
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shall we add this to .npmignore?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

4 changes: 4 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 @@ -732,7 +732,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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;
}