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

Handle Buffer.from TypeError on node versions prior to 4.5.x #365

Merged
merged 1 commit into from
Aug 3, 2017
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
15 changes: 14 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,5 +162,18 @@ function asBuffer(thing) {
return thing;
}

return Buffer.from ? Buffer.from(thing) : new Buffer(thing);
if (Buffer.from) {
// Buffer.from fix for node versions prior to 4.5.x
try {
return Buffer.from(thing);
} catch (e) {
if (e instanceof TypeError) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for trying to minimize the scope of errors that we'll try to catch.

return new Buffer(thing);
} else {
throw e;
}
}
} else {
return new Buffer(thing);
}
}