Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Only resolve hasNewBufferAPI flag once #22

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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