mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
chore: use ts-doc and factor out importFSModule
(#8449)
This commit is contained in:
parent
3f4451eff7
commit
30438e6532
@ -124,7 +124,10 @@ module.exports = {
|
|||||||
'plugin:@typescript-eslint/eslint-recommended',
|
'plugin:@typescript-eslint/eslint-recommended',
|
||||||
'plugin:@typescript-eslint/recommended',
|
'plugin:@typescript-eslint/recommended',
|
||||||
],
|
],
|
||||||
|
plugins: ['eslint-plugin-tsdoc'],
|
||||||
rules: {
|
rules: {
|
||||||
|
// Error if comments do not adhere to `tsdoc`.
|
||||||
|
'tsdoc/syntax': 2,
|
||||||
'no-unused-vars': 0,
|
'no-unused-vars': 0,
|
||||||
'@typescript-eslint/no-unused-vars': [
|
'@typescript-eslint/no-unused-vars': [
|
||||||
'error',
|
'error',
|
||||||
|
@ -120,6 +120,7 @@
|
|||||||
"eslint-plugin-import": "2.26.0",
|
"eslint-plugin-import": "2.26.0",
|
||||||
"eslint-plugin-mocha": "10.0.5",
|
"eslint-plugin-mocha": "10.0.5",
|
||||||
"eslint-plugin-prettier": "4.0.0",
|
"eslint-plugin-prettier": "4.0.0",
|
||||||
|
"eslint-plugin-tsdoc": "0.2.16",
|
||||||
"eslint-plugin-unicorn": "42.0.0",
|
"eslint-plugin-unicorn": "42.0.0",
|
||||||
"esprima": "4.0.1",
|
"esprima": "4.0.1",
|
||||||
"expect": "25.2.7",
|
"expect": "25.2.7",
|
||||||
|
@ -27,7 +27,7 @@
|
|||||||
* not true that each Chromium revision will have an exact matching revision
|
* not true that each Chromium revision will have an exact matching revision
|
||||||
* version of devtools-protocol. To ensure we're using a devtools-protocol that
|
* version of devtools-protocol. To ensure we're using a devtools-protocol that
|
||||||
* is aligned with our revision, we want to find the largest package number
|
* is aligned with our revision, we want to find the largest package number
|
||||||
* that's <= the revision that Puppeteer is using.
|
* that's \<= the revision that Puppeteer is using.
|
||||||
*
|
*
|
||||||
* This script uses npm's `view` function to list all versions in a range and
|
* This script uses npm's `view` function to list all versions in a range and
|
||||||
* find the one closest to our Chromium revision.
|
* find the one closest to our Chromium revision.
|
||||||
|
@ -6,7 +6,7 @@ module.exports = {
|
|||||||
* All available rules: http://eslint.org/docs/rules/
|
* All available rules: http://eslint.org/docs/rules/
|
||||||
*
|
*
|
||||||
* Rules take the following form:
|
* Rules take the following form:
|
||||||
* "rule-name", [severity, { opts }]
|
* "rule-name", [severity, \{ opts \}]
|
||||||
* Severity: 2 == error, 1 == warning, 0 == off.
|
* Severity: 2 == error, 1 == warning, 0 == off.
|
||||||
*/
|
*/
|
||||||
rules: {
|
rules: {
|
||||||
|
@ -363,12 +363,6 @@ export class CDPSession extends EventEmitter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param {!Error} error
|
|
||||||
* @param {string} method
|
|
||||||
* @param {{error: {message: string, data: any}}} object
|
|
||||||
* @returns {!Error}
|
|
||||||
*/
|
|
||||||
function createProtocolError(
|
function createProtocolError(
|
||||||
error: ProtocolError,
|
error: ProtocolError,
|
||||||
method: string,
|
method: string,
|
||||||
@ -379,11 +373,6 @@ function createProtocolError(
|
|||||||
return rewriteError(error, message, object.error.message);
|
return rewriteError(error, message, object.error.message);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param {!Error} error
|
|
||||||
* @param {string} message
|
|
||||||
* @returns {!Error}
|
|
||||||
*/
|
|
||||||
function rewriteError(
|
function rewriteError(
|
||||||
error: ProtocolError,
|
error: ProtocolError,
|
||||||
message: string,
|
message: string,
|
||||||
|
@ -335,7 +335,7 @@ export class DOMWorld {
|
|||||||
'Cannot pass a filepath to addScriptTag in the browser environment.'
|
'Cannot pass a filepath to addScriptTag in the browser environment.'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
const fs = await helper.importFSModule();
|
const fs = await import('fs');
|
||||||
let contents = await fs.promises.readFile(path, 'utf8');
|
let contents = await fs.promises.readFile(path, 'utf8');
|
||||||
contents += '//# sourceURL=' + path.replace(/\n/g, '');
|
contents += '//# sourceURL=' + path.replace(/\n/g, '');
|
||||||
const context = await this.executionContext();
|
const context = await this.executionContext();
|
||||||
@ -442,7 +442,7 @@ export class DOMWorld {
|
|||||||
'Cannot pass a filepath to addStyleTag in the browser environment.'
|
'Cannot pass a filepath to addStyleTag in the browser environment.'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
const fs = await helper.importFSModule();
|
const fs = await import('fs');
|
||||||
let contents = await fs.promises.readFile(path, 'utf8');
|
let contents = await fs.promises.readFile(path, 'utf8');
|
||||||
contents += '/*# sourceURL=' + path.replace(/\n/g, '') + '*/';
|
contents += '/*# sourceURL=' + path.replace(/\n/g, '') + '*/';
|
||||||
const context = await this.executionContext();
|
const context = await this.executionContext();
|
||||||
@ -985,9 +985,6 @@ async function waitForPredicatePageFunction(
|
|||||||
return await pollInterval(polling);
|
return await pollInterval(polling);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @returns {!Promise<*>}
|
|
||||||
*/
|
|
||||||
async function pollMutation(): Promise<unknown> {
|
async function pollMutation(): Promise<unknown> {
|
||||||
const success = predicateAcceptsContextElement
|
const success = predicateAcceptsContextElement
|
||||||
? await predicate(root, ...args)
|
? await predicate(root, ...args)
|
||||||
|
@ -24,7 +24,7 @@
|
|||||||
* add a new Page event, you should update the PageEmittedEvents enum in
|
* add a new Page event, you should update the PageEmittedEvents enum in
|
||||||
* src/common/Page.ts.
|
* src/common/Page.ts.
|
||||||
*
|
*
|
||||||
* Chat to @jackfranklin if you're unsure.
|
* Chat to \@jackfranklin if you're unsure.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export const Events = {
|
export const Events = {
|
||||||
|
@ -829,7 +829,7 @@ export class ElementHandle<
|
|||||||
avoid paying the cost unnecessarily.
|
avoid paying the cost unnecessarily.
|
||||||
*/
|
*/
|
||||||
const path = await import('path');
|
const path = await import('path');
|
||||||
const fs = await helper.importFSModule();
|
const fs = await import('fs');
|
||||||
// Locate all files and confirm that they exist.
|
// Locate all files and confirm that they exist.
|
||||||
const files = await Promise.all(
|
const files = await Promise.all(
|
||||||
filePaths.map(async (filePath) => {
|
filePaths.map(async (filePath) => {
|
||||||
|
@ -236,11 +236,6 @@ export class LifecycleWatcher {
|
|||||||
if (this._swapped || this._newDocumentNavigation)
|
if (this._swapped || this._newDocumentNavigation)
|
||||||
this._newDocumentNavigationCompleteCallback();
|
this._newDocumentNavigationCompleteCallback();
|
||||||
|
|
||||||
/**
|
|
||||||
* @param {!Frame} frame
|
|
||||||
* @param {!Array<string>} expectedLifecycle
|
|
||||||
* @returns {boolean}
|
|
||||||
*/
|
|
||||||
function checkLifecycle(
|
function checkLifecycle(
|
||||||
frame: Frame,
|
frame: Frame,
|
||||||
expectedLifecycle: ProtocolLifeCycleEvent[]
|
expectedLifecycle: ProtocolLifeCycleEvent[]
|
||||||
|
@ -2830,8 +2830,8 @@ export class Page extends EventEmitter {
|
|||||||
'Screenshots can only be written to a file path in a Node environment.'
|
'Screenshots can only be written to a file path in a Node environment.'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
const fs = await helper.importFSModule();
|
const fs = (await import('fs')).promises;
|
||||||
await fs.promises.writeFile(options.path, buffer);
|
await fs.writeFile(options.path, buffer);
|
||||||
}
|
}
|
||||||
return buffer;
|
return buffer;
|
||||||
|
|
||||||
@ -3416,9 +3416,9 @@ function convertPrintParameterToInches(
|
|||||||
let pixels;
|
let pixels;
|
||||||
if (helper.isNumber(parameter)) {
|
if (helper.isNumber(parameter)) {
|
||||||
// Treat numbers as pixel values to be aligned with phantom's paperSize.
|
// Treat numbers as pixel values to be aligned with phantom's paperSize.
|
||||||
pixels = /** @type {number} */ parameter;
|
pixels = parameter;
|
||||||
} else if (helper.isString(parameter)) {
|
} else if (helper.isString(parameter)) {
|
||||||
const text = /** @type {string} */ parameter;
|
const text = parameter;
|
||||||
let unit = text.substring(text.length - 2).toLowerCase();
|
let unit = text.substring(text.length - 2).toLowerCase();
|
||||||
let valueText = '';
|
let valueText = '';
|
||||||
if (unit in unitToPixels) {
|
if (unit in unitToPixels) {
|
||||||
|
@ -16,7 +16,7 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Asserts that the given value is truthy.
|
* Asserts that the given value is truthy.
|
||||||
* @param value
|
* @param value - some conditional statement
|
||||||
* @param message - the error message to throw if the value is not truthy.
|
* @param message - the error message to throw if the value is not truthy.
|
||||||
*/
|
*/
|
||||||
export const assert: (value: unknown, message?: string) => asserts value = (
|
export const assert: (value: unknown, message?: string) => asserts value = (
|
||||||
|
@ -322,18 +322,18 @@ async function getReadableAsBuffer(
|
|||||||
throw new Error('Cannot write to a path outside of Node.js environment.');
|
throw new Error('Cannot write to a path outside of Node.js environment.');
|
||||||
}
|
}
|
||||||
|
|
||||||
const fs = isNode ? await importFSModule() : null;
|
const fs = isNode ? (await import('fs')).promises : null;
|
||||||
|
|
||||||
let fileHandle: import('fs').promises.FileHandle | undefined;
|
let fileHandle: import('fs').promises.FileHandle | undefined;
|
||||||
|
|
||||||
if (path && fs) {
|
if (path && fs) {
|
||||||
fileHandle = await fs.promises.open(path, 'w');
|
fileHandle = await fs.open(path, 'w');
|
||||||
}
|
}
|
||||||
const buffers = [];
|
const buffers = [];
|
||||||
for await (const chunk of readable) {
|
for await (const chunk of readable) {
|
||||||
buffers.push(chunk);
|
buffers.push(chunk);
|
||||||
if (fileHandle && fs) {
|
if (fileHandle && fs) {
|
||||||
await fs.promises.writeFile(fileHandle, chunk);
|
await fs.writeFile(fileHandle, chunk);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -376,30 +376,6 @@ async function getReadableFromProtocolStream(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Loads the Node fs promises API. Needed because on Node 10.17 and below,
|
|
||||||
* fs.promises is experimental, and therefore not marked as enumerable. That
|
|
||||||
* means when TypeScript compiles an `import('fs')`, its helper doesn't spot the
|
|
||||||
* promises declaration and therefore on Node <10.17 you get an error as
|
|
||||||
* fs.promises is undefined in compiled TypeScript land.
|
|
||||||
*
|
|
||||||
* See https://github.com/puppeteer/puppeteer/issues/6548 for more details.
|
|
||||||
*
|
|
||||||
* Once Node 10 is no longer supported (April 2021) we can remove this and use
|
|
||||||
* `(await import('fs')).promises`.
|
|
||||||
*/
|
|
||||||
async function importFSModule(): Promise<typeof import('fs')> {
|
|
||||||
if (!isNode) {
|
|
||||||
throw new Error('Cannot load the fs module API outside of Node.');
|
|
||||||
}
|
|
||||||
|
|
||||||
const fs = await import('fs');
|
|
||||||
if (fs.promises) {
|
|
||||||
return fs;
|
|
||||||
}
|
|
||||||
return fs.default;
|
|
||||||
}
|
|
||||||
|
|
||||||
export const helper = {
|
export const helper = {
|
||||||
evaluationString,
|
evaluationString,
|
||||||
pageBindingInitString,
|
pageBindingInitString,
|
||||||
@ -413,7 +389,6 @@ export const helper = {
|
|||||||
waitForEvent,
|
waitForEvent,
|
||||||
isString,
|
isString,
|
||||||
isNumber,
|
isNumber,
|
||||||
importFSModule,
|
|
||||||
addEventListener,
|
addEventListener,
|
||||||
removeEventListeners,
|
removeEventListeners,
|
||||||
valueFromRemoteObject,
|
valueFromRemoteObject,
|
||||||
|
@ -277,9 +277,6 @@ function waitForWSEndpoint(
|
|||||||
];
|
];
|
||||||
const timeoutId = timeout ? setTimeout(onTimeout, timeout) : 0;
|
const timeoutId = timeout ? setTimeout(onTimeout, timeout) : 0;
|
||||||
|
|
||||||
/**
|
|
||||||
* @param {!Error=} error
|
|
||||||
*/
|
|
||||||
function onClose(error?: Error): void {
|
function onClose(error?: Error): void {
|
||||||
cleanup();
|
cleanup();
|
||||||
reject(
|
reject(
|
||||||
|
@ -687,8 +687,8 @@ class FirefoxLauncher implements ProductLauncher {
|
|||||||
* able to restore the original values of preferences a backup of prefs.js
|
* able to restore the original values of preferences a backup of prefs.js
|
||||||
* will be created.
|
* will be created.
|
||||||
*
|
*
|
||||||
* @param prefs List of preferences to add.
|
* @param prefs - List of preferences to add.
|
||||||
* @param profilePath Firefox profile to write the preferences to.
|
* @param profilePath - Firefox profile to write the preferences to.
|
||||||
*/
|
*/
|
||||||
async writePreferences(
|
async writePreferences(
|
||||||
prefs: { [x: string]: unknown },
|
prefs: { [x: string]: unknown },
|
||||||
|
@ -480,14 +480,14 @@ describeChromeOnly('NetworkManager', () => {
|
|||||||
* This sequence was taken from an actual CDP session produced by the following
|
* This sequence was taken from an actual CDP session produced by the following
|
||||||
* test script:
|
* test script:
|
||||||
*
|
*
|
||||||
* const browser = await puppeteer.launch({ headless: false });
|
* const browser = await puppeteer.launch(\{ headless: false \});
|
||||||
* const page = await browser.newPage();
|
* const page = await browser.newPage();
|
||||||
* await page.setCacheEnabled(false);
|
* await page.setCacheEnabled(false);
|
||||||
*
|
*
|
||||||
* await page.setRequestInterception(true)
|
* await page.setRequestInterception(true)
|
||||||
* page.on('request', (interceptedRequest) => {
|
* page.on('request', (interceptedRequest) =\> \{
|
||||||
* interceptedRequest.continue();
|
* interceptedRequest.continue();
|
||||||
* });
|
* \});
|
||||||
*
|
*
|
||||||
* await page.goto('https://www.google.com');
|
* await page.goto('https://www.google.com');
|
||||||
* await browser.close();
|
* await browser.close();
|
||||||
|
@ -419,9 +419,6 @@ describeChromeOnly('OOPIF', function () {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
|
||||||
* @param {!BrowserContext} context
|
|
||||||
*/
|
|
||||||
function oopifs(context) {
|
function oopifs(context) {
|
||||||
return context
|
return context
|
||||||
.targets()
|
.targets()
|
||||||
|
@ -806,11 +806,7 @@ describe('request interception', function () {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
function pathToFileURL(path: string): string {
|
||||||
* @param {string} path
|
|
||||||
* @returns {string}
|
|
||||||
*/
|
|
||||||
function pathToFileURL(path) {
|
|
||||||
let pathName = path.replace(/\\/g, '/');
|
let pathName = path.replace(/\\/g, '/');
|
||||||
// Windows drive letter must be prefixed with a slash.
|
// Windows drive letter must be prefixed with a slash.
|
||||||
if (!pathName.startsWith('/')) pathName = '/' + pathName;
|
if (!pathName.startsWith('/')) pathName = '/' + pathName;
|
||||||
|
Loading…
Reference in New Issue
Block a user