Skip to content

Commit 1243466

Browse files
authoredJul 9, 2020
chore: remove installAsyncStackHooks helper (#6186)
* chore: remove `installAsyncStackHooks` helper This code was written when browsers/Node didn't support errors in async functions very well. They now do a much better job of this, so we can lose the additonal complexity from our codebase and leave it to the host environment :) * lazy launcher is private * remove async stack test
1 parent 19f188a commit 1243466

8 files changed

+8
-88
lines changed
 

Diff for: ‎new-docs/puppeteer.puppeteer._lazylauncher.md

-11
This file was deleted.

Diff for: ‎new-docs/puppeteer.puppeteer.md

-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ const puppeteer = require('puppeteer');
4040
| [\_\_productName](./puppeteer.puppeteer.__productname.md) | | string | |
4141
| [\_changedProduct](./puppeteer.puppeteer._changedproduct.md) | | boolean | |
4242
| [\_isPuppeteerCore](./puppeteer.puppeteer._ispuppeteercore.md) | | boolean | |
43-
| [\_lazyLauncher](./puppeteer.puppeteer._lazylauncher.md) | | [ProductLauncher](./puppeteer.productlauncher.md) | |
4443
| [\_preferredRevision](./puppeteer.puppeteer._preferredrevision.md) | | string | |
4544
| [devices](./puppeteer.puppeteer.devices.md) | | [DevicesMap](./puppeteer.devicesmap.md) | |
4645
| [errors](./puppeteer.puppeteer.errors.md) | | [PuppeteerErrors](./puppeteer.puppeteererrors.md) | |

Diff for: ‎src/api.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,10 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
// @ts-nocheck
1716

18-
/* This file is used in two places:
17+
/* This file is used in one place:
1918
* 1) the coverage-utils use it to gain a list of all methods we check for test
2019
* coverage on
21-
* 2) index.js uses it to iterate through all methods and call
22-
* helper.installAsyncStackHooks on
2320
*/
2421
module.exports = {
2522
Accessibility: require('./common/Accessibility').Accessibility,

Diff for: ‎src/common/Puppeteer.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export class Puppeteer {
6161
_isPuppeteerCore: boolean;
6262
_changedProduct = false;
6363
__productName: string;
64-
_lazyLauncher: ProductLauncher;
64+
private _lazyLauncher: ProductLauncher;
6565

6666
/**
6767
* @internal

Diff for: ‎src/common/helper.ts

-38
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@ const closeAsync = promisify(fs.close);
2828

2929
export const debugError = debug('puppeteer:error');
3030

31-
interface AnyClass {
32-
prototype: object;
33-
}
34-
3531
function getExceptionMessage(
3632
exceptionDetails: Protocol.Runtime.ExceptionDetails
3733
): string {
@@ -95,39 +91,6 @@ async function releaseObject(
9591
});
9692
}
9793

98-
function installAsyncStackHooks(classType: AnyClass): void {
99-
for (const methodName of Reflect.ownKeys(classType.prototype)) {
100-
const method = Reflect.get(classType.prototype, methodName);
101-
if (
102-
methodName === 'constructor' ||
103-
typeof methodName !== 'string' ||
104-
methodName.startsWith('_') ||
105-
typeof method !== 'function' ||
106-
method.constructor.name !== 'AsyncFunction'
107-
)
108-
continue;
109-
Reflect.set(classType.prototype, methodName, function (...args) {
110-
const syncStack = {
111-
stack: '',
112-
};
113-
Error.captureStackTrace(syncStack);
114-
return method.call(this, ...args).catch((error) => {
115-
const stack = syncStack.stack.substring(
116-
syncStack.stack.indexOf('\n') + 1
117-
);
118-
const clientStack = stack.substring(stack.indexOf('\n'));
119-
if (
120-
error instanceof Error &&
121-
error.stack &&
122-
!error.stack.includes(clientStack)
123-
)
124-
error.stack += '\n -- ASYNC --\n' + stack;
125-
throw error;
126-
});
127-
});
128-
}
129-
}
130-
13194
export interface PuppeteerEventListener {
13295
emitter: CommonEventEmitter;
13396
eventName: string | symbol;
@@ -277,7 +240,6 @@ export const helper = {
277240
addEventListener,
278241
removeEventListeners,
279242
valueFromRemoteObject,
280-
installAsyncStackHooks,
281243
getExceptionMessage,
282244
releaseObject,
283245
};

Diff for: ‎src/initialize.ts

+1-18
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,13 @@
1414
* limitations under the License.
1515
*/
1616

17-
// api.ts has to use module.exports as it's also consumed by DocLint which runs
18-
// on Node.
19-
// eslint-disable-next-line @typescript-eslint/no-var-requires
20-
const api = require('./api');
21-
22-
import { helper } from './common/helper';
2317
import { Puppeteer } from './common/Puppeteer';
2418
import { PUPPETEER_REVISIONS } from './revisions';
2519
import pkgDir from 'pkg-dir';
2620

2721
export const initializePuppeteer = (packageName: string): Puppeteer => {
2822
const puppeteerRootDirectory = pkgDir.sync(__dirname);
2923

30-
for (const className in api) {
31-
if (typeof api[className] === 'function')
32-
helper.installAsyncStackHooks(api[className]);
33-
}
34-
3524
let preferredRevision = PUPPETEER_REVISIONS.chromium;
3625
const isPuppeteerCore = packageName === 'puppeteer-core';
3726
// puppeteer-core ignores environment variables
@@ -43,16 +32,10 @@ export const initializePuppeteer = (packageName: string): Puppeteer => {
4332
if (!isPuppeteerCore && product === 'firefox')
4433
preferredRevision = PUPPETEER_REVISIONS.firefox;
4534

46-
const puppeteer = new Puppeteer(
35+
return new Puppeteer(
4736
puppeteerRootDirectory,
4837
preferredRevision,
4938
isPuppeteerCore,
5039
product
5140
);
52-
53-
// The introspection in `Helper.installAsyncStackHooks` references
54-
// `Puppeteer._launcher` before the Puppeteer ctor is called, such that an
55-
// invalid Launcher is selected at import, so we reset it.
56-
puppeteer._lazyLauncher = undefined;
57-
return puppeteer;
5841
};

Diff for: ‎test/launcher.spec.ts

+5
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,11 @@ describe('Launcher specs', function () {
441441

442442
after(async () => {
443443
const { puppeteer } = getTestState();
444+
/* launcher is a private property so we don't want our users doing this
445+
* but we need to reset the state fully here for testing different
446+
* browser launchers
447+
*/
448+
// @ts-expect-error
444449
puppeteer._lazyLauncher = undefined;
445450
puppeteer._productName = productName;
446451
});

Diff for: ‎test/page.spec.ts

-15
Original file line numberDiff line numberDiff line change
@@ -119,21 +119,6 @@ describe('Page', function () {
119119
});
120120
});
121121

122-
describeFailsFirefox('Async stacks', () => {
123-
it('should work', async () => {
124-
const { page, server } = getTestState();
125-
126-
server.setRoute('/empty.html', (req, res) => {
127-
res.statusCode = 204;
128-
res.end();
129-
});
130-
let error = null;
131-
await page.goto(server.EMPTY_PAGE).catch((error_) => (error = error_));
132-
expect(error).not.toBe(null);
133-
expect(error.stack).toContain(__filename);
134-
});
135-
});
136-
137122
// This test fails on Firefox on CI consistently but cannot be replicated
138123
// locally. Skipping for now to unblock the Mitt release and given FF support
139124
// isn't fully done yet but raising an issue to ask the FF folks to have a

0 commit comments

Comments
 (0)
Please sign in to comment.