Skip to content

Commit

Permalink
(core) - Prevent Buffer from being auto-polyfilled
Browse files Browse the repository at this point in the history
  • Loading branch information
kitten committed Oct 14, 2021
1 parent 83f5155 commit cd6c8b1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
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'
? (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

0 comments on commit cd6c8b1

Please sign in to comment.