Skip to content

Commit d652e54

Browse files
jasnellljharb
authored andcommitted
[Fix] Replace Buffer.from with buffer-from
Restores node < 4 support
1 parent 66a5548 commit d652e54

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22

3+
var bufferFrom = require('buffer-from');
34
var isArray = require('isarray');
45

56
var useUint8Array = typeof Uint8Array !== 'undefined';
@@ -19,7 +20,7 @@ module.exports = function toBuffer(data, encoding) {
1920

2021
// Convert strings to Buffer
2122
if (typeof data === 'string') {
22-
return Buffer.from(data, encoding);
23+
return bufferFrom(data, encoding);
2324
}
2425

2526
/*
@@ -32,7 +33,7 @@ module.exports = function toBuffer(data, encoding) {
3233
return Buffer.alloc(0);
3334
}
3435

35-
var res = Buffer.from(data.buffer, data.byteOffset, data.byteLength);
36+
var res = bufferFrom(data.buffer, data.byteOffset, data.byteLength);
3637
/*
3738
* Recheck result size, as offset/length doesn't work on Node.js <5.10
3839
* We just go to Uint8Array case if this fails
@@ -47,7 +48,7 @@ module.exports = function toBuffer(data, encoding) {
4748
* Doesn't make sense with other TypedArray instances
4849
*/
4950
if (useUint8Array && data instanceof Uint8Array) {
50-
return Buffer.from(data);
51+
return bufferFrom(data);
5152
}
5253

5354
/*
@@ -64,7 +65,7 @@ module.exports = function toBuffer(data, encoding) {
6465
&& data.constructor.isBuffer(data)
6566
)
6667
) {
67-
return Buffer.from(data);
68+
return bufferFrom(data);
6869
}
6970

7071
throw new TypeError('The "data" argument must be a string, an Array, a Buffer, a TypedArray, or a DataView.');

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
},
2424
"homepage": "https://github.com/mafintosh/to-buffer",
2525
"dependencies": {
26+
"buffer-from": "^1.1.2",
2627
"isarray": "^2.0.5"
2728
},
2829
"devDependencies": {

0 commit comments

Comments
 (0)