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(agnostification): common/helper.ts #6515

Merged
merged 3 commits into from
Oct 19, 2020
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
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ jobs:

- node_js: "12.16.3"
name: 'Browser tests: Linux/Chromium'
addons:
chrome: stable
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

needed to ensure Travis has an up to date chrome that web test runner can use

Copy link
Contributor

Choose a reason for hiding this comment

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

We could also point it to the bundled chromium which puppeteer already brings. Let me know if I can help here :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@LarsDenBakker thanks! I'm quite keen to see if it's stable on a regular Chrome, but if not we'll reach for that and I expect I'll ping you for help :D

env:
- CHROMIUM=true
script:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"@types/ws": "^7.2.4",
"@typescript-eslint/eslint-plugin": "^4.4.0",
"@typescript-eslint/parser": "^4.4.0",
"@web/test-runner": "^0.8.4",
"@web/test-runner": "^0.9.2",
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

the old version started timing out on CI...upgrading to this fixes it. Not entirely sure why!

"commonmark": "^0.28.1",
"cross-env": "^7.0.2",
"dependency-cruiser": "^9.7.0",
Expand Down
17 changes: 13 additions & 4 deletions src/common/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
*/
import { TimeoutError } from './Errors.js';
import { debug } from './Debug.js';
import * as fs from 'fs';
import { CDPSession } from './Connection.js';
import { Protocol } from 'devtools-protocol';
import { CommonEventEmitter } from './EventEmitter.js';
import { assert } from './assert.js';
import { isNode } from '../environment.js';

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

Expand Down Expand Up @@ -309,9 +309,16 @@ async function readProtocolStream(
handle: string,
path?: string
): Promise<Buffer> {
if (!isNode && path) {
throw new Error('Cannot write to a path outside of Node.js environment.');
}

Copy link
Member

Choose a reason for hiding this comment

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

Can't we do let fs; and then fs = ... inside one of the ifs here? (It seems fine to rely on Node.js' caching regardless, but if we can avoid it and simplify the code at the same time...)

Copy link
Member

Choose a reason for hiding this comment

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

I meant let fs — updated comment to clarify

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

👍 PTAL!

const fs = isNode ? await import('fs') : null;

let eof = false;
let fileHandle: fs.promises.FileHandle;
if (path) {
let fileHandle: import('fs').promises.FileHandle;
jackfranklin marked this conversation as resolved.
Show resolved Hide resolved

if (path && fs) {
fileHandle = await fs.promises.open(path, 'w');
}
const bufs = [];
Expand All @@ -323,7 +330,9 @@ async function readProtocolStream(
response.base64Encoded ? 'base64' : undefined
);
bufs.push(buf);
if (path) await fs.promises.writeFile(fileHandle, buf);
if (path && fs) {
await fs.promises.writeFile(fileHandle, buf);
}
}
if (path) await fileHandle.close();
await client.send('IO.close', { handle });
Expand Down