Skip to content

Commit

Permalink
Avoid using deprecated Buffer API
Browse files Browse the repository at this point in the history
Use Buffer.from when it's available instead of 'new Bufer(string)'

Fixes: npm#60
Refs: https://nodejs.org/api/deprecations.html#deprecations_dep0005_buffer_constructor
  • Loading branch information
ChALkeR committed Mar 22, 2018
1 parent 1e4527f commit 1b843e8
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/collect.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,15 @@ function collect (stream) {
stream.on('end', save)
var buf = []
function save (b) {
if (typeof b === 'string') b = new Buffer(b)
if (typeof b === 'string') {
if (Buffer.from && Buffer.from !== Uint8Array.from) {
b = Buffer.from(b)
} else {
// Outdated Node.js versions (<4.5.0 or 5.x <5.10)
// b is already type-checked to be a string just above
b = new Buffer(b)
}
}
if (Buffer.isBuffer(b) && !b.length) return
buf.push(b)
}
Expand Down

0 comments on commit 1b843e8

Please sign in to comment.