Skip to content

Commit

Permalink
Only resolve hasNewBufferAPI flag once
Browse files Browse the repository at this point in the history
  • Loading branch information
kibertoad committed Jan 21, 2024
1 parent b4d474a commit 8816e18
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,15 @@ var CRC_TABLE = [
0x2d02ef8d
];

var hasNewBufferAPI =
typeof Buffer.alloc === "function" &&
typeof Buffer.from === "function";

if (typeof Int32Array !== 'undefined') {
CRC_TABLE = new Int32Array(CRC_TABLE);
}

function newEmptyBuffer(length) {
function newEmptyBufferLegacy(length) {
var buffer = new Buffer(length);
buffer.fill(0x00);
return buffer;
Expand All @@ -70,12 +74,8 @@ function ensureBuffer(input) {
return input;
}

var hasNewBufferAPI =
typeof Buffer.alloc === "function" &&
typeof Buffer.from === "function";

if (typeof input === "number") {
return hasNewBufferAPI ? Buffer.alloc(input) : newEmptyBuffer(input);
return hasNewBufferAPI ? Buffer.alloc(input) : newEmptyBufferLegacy(input);
}
else if (typeof input === "string") {
return hasNewBufferAPI ? Buffer.from(input) : new Buffer(input);
Expand Down

0 comments on commit 8816e18

Please sign in to comment.