Skip to content

Commit

Permalink
nodejsUtils: don't use deprecated Buffer api
Browse files Browse the repository at this point in the history
This avoids using Buffer constructor API on newer Node.js versions.

To achieve that, Buffer.from presence is checked, with validating that it's
not the same method as Uint8Array.from.

Refs:
https://nodejs.org/api/deprecations.html#deprecations_dep0005_buffer_constructor

Signed-off-by: Robin Andersson <[email protected]>
  • Loading branch information
ChALkeR authored and robinwassen committed Jan 9, 2019
1 parent 3b3ada4 commit eb1d0d7
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions lib/nodejsUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@ module.exports = {
* @return {Buffer} a new Buffer.
*/
newBufferFrom: function(data, encoding) {
// XXX We can't use `Buffer.from` which comes from `Uint8Array.from`
// in nodejs v4 (< v.4.5). It's not the expected implementation (and
// has a different signature).
// see https://github.com/nodejs/node/issues/8053
// A condition on nodejs' version won't solve the issue as we don't
// control the Buffer polyfills that may or may not be used.
return new Buffer(data, encoding);
if (Buffer.from && Buffer.from !== Uint8Array.from) {
return Buffer.from(data, encoding);
} else {
return new Buffer(data, encoding);
}
},
/**
* Create a new nodejs Buffer with the specified size.
Expand Down

0 comments on commit eb1d0d7

Please sign in to comment.