Skip to content

Commit

Permalink
Avoid using deprecated Buffer API on newer Node.js
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 validation that it's
not the same method as Uint8Array.from.

Refs:
https://nodejs.org/api/deprecations.html#deprecations_dep0005_buffer_constructor
  • Loading branch information
ChALkeR committed Mar 22, 2018
1 parent 9908a22 commit 7c1855d
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,14 @@ function decodefield (str) {
value = getlatin1(binary)
break
case 'utf-8':
value = new Buffer(binary, 'binary').toString('utf8')
if (Buffer.from && Buffer.from !== Uint8Array.from) {
// Node.js 4.5.0 or newer
value = Buffer.from(binary, 'binary').toString('utf8')
} else {
// Old Node.js versions
// `binary` is always a string here, so this is safe
value = new Buffer(binary, 'binary').toString('utf8')
}
break
default:
throw new TypeError('unsupported charset in extended field')
Expand Down

0 comments on commit 7c1855d

Please sign in to comment.