From cd6c8b174166c98a633d1dd163762feaaf08186e Mon Sep 17 00:00:00 2001 From: Phil Pluckthun Date: Thu, 14 Oct 2021 19:10:42 +0100 Subject: [PATCH] (core) - Prevent Buffer from being auto-polyfilled --- .changeset/lucky-pens-end.md | 5 +++++ packages/core/src/internal/fetchSource.ts | 8 +++++--- 2 files changed, 10 insertions(+), 3 deletions(-) create mode 100644 .changeset/lucky-pens-end.md diff --git a/.changeset/lucky-pens-end.md b/.changeset/lucky-pens-end.md new file mode 100644 index 0000000000..a881a7e149 --- /dev/null +++ b/.changeset/lucky-pens-end.md @@ -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. diff --git a/packages/core/src/internal/fetchSource.ts b/packages/core/src/internal/fetchSource.ts index ff07181d60..e5e8b3b9fb 100644 --- a/packages/core/src/internal/fetchSource.ts +++ b/packages/core/src/internal/fetchSource.ts @@ -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 // See: https://github.com/maraisr/meros/blob/219fe95/src/browser.ts