From fb1409fe1c9603990c893e7af1d5ea0c18eb99b5 Mon Sep 17 00:00:00 2001 From: Romain Lenzotti Date: Wed, 25 Oct 2023 09:14:52 +0200 Subject: [PATCH] fix(core): make isBuffer browser compliant Closes: #2480 --- packages/core/src/utils/objects/isBuffer.spec.ts | 4 +++- packages/core/src/utils/objects/isBuffer.ts | 13 ++++++++++++- packages/engines/package.json | 2 +- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/packages/core/src/utils/objects/isBuffer.spec.ts b/packages/core/src/utils/objects/isBuffer.spec.ts index 8f7129b60d9..1a87349602d 100644 --- a/packages/core/src/utils/objects/isBuffer.spec.ts +++ b/packages/core/src/utils/objects/isBuffer.spec.ts @@ -1,9 +1,11 @@ -import {isBuffer} from "./isBuffer"; +import {isBuffer, isUint8Array} from "./isBuffer"; describe("isBuffer", () => { it("should return true", () => { expect(isBuffer(Buffer.from(""))).toBeTruthy(); expect(isBuffer(Buffer)).toBeTruthy(); + expect(isUint8Array(Uint8Array)).toBeTruthy(); + expect(isUint8Array(Uint8Array.from([0, 1]))).toBeTruthy(); }); it("should return false", () => { expect(isBuffer({})).toBeFalsy(); diff --git a/packages/core/src/utils/objects/isBuffer.ts b/packages/core/src/utils/objects/isBuffer.ts index 1665dae062b..9b26f58c324 100644 --- a/packages/core/src/utils/objects/isBuffer.ts +++ b/packages/core/src/utils/objects/isBuffer.ts @@ -1,8 +1,19 @@ +import {nameOf} from "./nameOf"; + /** * Tests to see if the object is Buffer * @param target * @returns {boolean} */ export function isBuffer(target: any): target is Buffer { - return !!(target && (target === Buffer || target instanceof Buffer)); + // is Class + if (target && "isBuffer" in target && typeof target.isBuffer === "function") { + return true; + } + + return isUint8Array(target); +} + +export function isUint8Array(target: any): target is Uint8Array { + return !!(target && (target === Uint8Array || target instanceof Uint8Array)); } diff --git a/packages/engines/package.json b/packages/engines/package.json index 3e4124ec972..671f0824b76 100644 --- a/packages/engines/package.json +++ b/packages/engines/package.json @@ -95,4 +95,4 @@ "view" ], "peerDependencies": {} -} \ No newline at end of file +}