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

(core) - Prevent Buffer from being auto-polyfilled #2027

Merged
merged 1 commit into from
Oct 15, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/lucky-pens-end.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@urql/core': patch
---

Prevent `Buffer` from being polyfilled by an automatic detection in Webpack. Instead of referencing the `Buffer` global we now simply check the constructor name.
8 changes: 5 additions & 3 deletions packages/core/src/internal/fetchSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ const boundaryHeaderRe = /boundary="?([^=";]+)"?/i;

type ChunkData = { done: false; value: Buffer | Uint8Array } | { done: true };

// NOTE: We're avoiding referencing the `Buffer` global here to prevent
// auto-polyfilling in Webpack
const toString = (input: Buffer | ArrayBuffer): string =>
typeof Buffer !== 'undefined' && Buffer.isBuffer(input)
? input.toString()
: decoder!.decode(input);
input.constructor.name === 'Buffer'
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a tricky check without referencing Buffer directly!

I was thinking of a potential alternate of just detecting isBuffer and using it -- also not 100%, but would allow subclasses of Buffer...

typeof input.constructor.isBuffer === 'function' && input.constructor.isBuffer(input)

No big deal either way, but just one other thought!

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm fine with either 👍 I double checked the Node reference implementation before sending in this change and they seem to just be doing a constructor check, so either should be alright 😄

Copy link
Contributor

Choose a reason for hiding this comment

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

I double checked the Node reference implementation before sending in this change and they seem to just be doing a constructor check

Oh haha, that sounds like a good approach then 😂

Copy link
Member Author

Choose a reason for hiding this comment

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

? (input as Buffer).toString()
: decoder!.decode(input as ArrayBuffer);

// DERIVATIVE: Copyright (c) 2021 Marais Rossouw <[email protected]>
// See: https://github.com/maraisr/meros/blob/219fe95/src/browser.ts
Expand Down