-
Notifications
You must be signed in to change notification settings - Fork 31
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
fix(warning): Supress warnings when importing experimental web stream from NodeJS #125
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,51 @@ | ||
/* c8 ignore start */ | ||
// 64 KiB (same size chrome slice theirs blob into Uint8array's) | ||
const POOL_SIZE = 65536; | ||
const POOL_SIZE = 65536 | ||
|
||
if (!globalThis.ReadableStream) { | ||
// `node:stream/web` got introduced in v16.5.0 as experimental | ||
// and it's preferred over the polyfilled version. So we also | ||
// suppress the warning that gets emitted by NodeJS for using it. | ||
try { | ||
Object.assign(globalThis, require('node:stream/web')) | ||
const process = require('node:process') | ||
const { emitWarning } = process | ||
try { | ||
process.emitWarning = () => {} | ||
Object.assign(globalThis, require('node:stream/web')) | ||
process.emitWarning = emitWarning | ||
} catch (error) { | ||
process.emitWarning = emitWarning | ||
throw error | ||
} | ||
} catch (error) { | ||
// TODO: Remove when only supporting node >= 16.5.0 | ||
// fallback to polyfill implementation | ||
Object.assign(globalThis, require('web-streams-polyfill/dist/ponyfill.es2018.js')) | ||
} | ||
} | ||
|
||
try { | ||
const {Blob} = require('buffer') | ||
// Don't use node: prefix for this, require+node: is not supported until node v14.14 | ||
// Only `import()` can use prefix in 12.20 and later | ||
const { Blob } = require('buffer') | ||
if (Blob && !Blob.prototype.stream) { | ||
Blob.prototype.stream = function name(params) { | ||
let position = 0; | ||
const blob = this; | ||
Blob.prototype.stream = function name (params) { | ||
let position = 0 | ||
const blob = this | ||
|
||
return new ReadableStream({ | ||
type: 'bytes', | ||
async pull(ctrl) { | ||
const chunk = blob.slice(position, Math.min(blob.size, position + POOL_SIZE)); | ||
const buffer = await chunk.arrayBuffer(); | ||
position += buffer.byteLength; | ||
ctrl.enqueue(new Uint8Array(buffer)) | ||
return new ReadableStream({ | ||
type: 'bytes', | ||
async pull (ctrl) { | ||
const chunk = blob.slice(position, Math.min(blob.size, position + POOL_SIZE)) | ||
const buffer = await chunk.arrayBuffer() | ||
position += buffer.byteLength | ||
ctrl.enqueue(new Uint8Array(buffer)) | ||
|
||
if (position === blob.size) { | ||
ctrl.close() | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
if (position === blob.size) { | ||
ctrl.close() | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
} catch (error) {} | ||
/* c8 ignore end */ |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know if I'd necessarily prefix everything with
node:
as it introduces a small performance overhead. It is useful for the import ofstream/web
which is not available on all versions of Node though so that it can be more easily detected as a Node import by static analysis tools run by Netlify so that their deployments succeedThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, do you have a source on this? I think that it's the new recommended way since the Node.js documentation is switching over to the
node:
-style.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was basing that statement off the fact that the docs say it skips the require cache: https://nodejs.org/api/modules.html#modules_core_modules
I don't know what if any performance impact that has, but I had assumed there would be some small impact
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the node: prefix...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@benmccann Bypassing the
require
cache makes it faster, not slower, since core modules are not added to therequire
cache either way.