Skip to content

Commit

Permalink
fix(core): make isBuffer browser compliant
Browse files Browse the repository at this point in the history
Closes: #2480
  • Loading branch information
Romakita committed Oct 26, 2023
1 parent 4f7bf57 commit 9f93348
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
4 changes: 3 additions & 1 deletion packages/core/src/utils/objects/isBuffer.spec.ts
Original file line number Diff line number Diff line change
@@ -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();
Expand Down
13 changes: 12 additions & 1 deletion packages/core/src/utils/objects/isBuffer.ts
Original file line number Diff line number Diff line change
@@ -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));
}
2 changes: 1 addition & 1 deletion packages/engines/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,4 @@
"view"
],
"peerDependencies": {}
}
}

0 comments on commit 9f93348

Please sign in to comment.