Skip to content

Commit 7f8a881

Browse files
ChALkeRljharb
authored andcommitted
[Fix] handle non-Uint8Arrays in node < 3
1 parent b45247e commit 7f8a881

File tree

3 files changed

+44
-29
lines changed

3 files changed

+44
-29
lines changed

.eslintrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,9 @@
77
"Uint8Array": "readonly",
88
"Uint16Array": "readonly",
99
},
10+
11+
"rules": {
12+
"complexity": "off",
13+
"max-lines-per-function": "off",
14+
},
1015
}

index.js

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ var isArray = require('isarray');
66
var useUint8Array = typeof Uint8Array !== 'undefined';
77
var useArrayBuffer = typeof ArrayBuffer !== 'undefined'
88
&& typeof Uint8Array !== 'undefined'
9-
&& ArrayBuffer.isView
10-
&& (Buffer.prototype instanceof Uint8Array || Buffer.TYPED_ARRAY_SUPPORT);
9+
&& ArrayBuffer.isView;
10+
var useFromArrayBuffer = useArrayBuffer && (Buffer.prototype instanceof Uint8Array || Buffer.TYPED_ARRAY_SUPPORT);
1111

1212
module.exports = function toBuffer(data, encoding) {
1313
/*
@@ -18,7 +18,6 @@ module.exports = function toBuffer(data, encoding) {
1818
return data;
1919
}
2020

21-
// Convert strings to Buffer
2221
if (typeof data === 'string') {
2322
return Buffer.from(data, encoding);
2423
}
@@ -33,13 +32,28 @@ module.exports = function toBuffer(data, encoding) {
3332
return Buffer.alloc(0);
3433
}
3534

36-
var res = Buffer.from(data.buffer, data.byteOffset, data.byteLength);
35+
// When Buffer is based on Uint8Array, we can just construct it from ArrayBuffer
36+
if (useFromArrayBuffer) {
37+
var res = Buffer.from(data.buffer, data.byteOffset, data.byteLength);
38+
/*
39+
* Recheck result size, as offset/length doesn't work on Node.js <5.10
40+
* We just go to Uint8Array case if this fails
41+
*/
42+
if (res.byteLength === data.byteLength) {
43+
return res;
44+
}
45+
}
46+
47+
// Convert to Uint8Array bytes and then to Buffer
48+
var uint8 = data instanceof Uint8Array ? data : new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
49+
var result = Buffer.from(uint8);
50+
3751
/*
38-
* Recheck result size, as offset/length doesn't work on Node.js <5.10
39-
* We just go to Uint8Array case if this fails
52+
* Let's recheck that conversion succeeded
53+
* We have .length but not .byteLength when useFromArrayBuffer is false
4054
*/
41-
if (res.byteLength === data.byteLength) {
42-
return res;
55+
if (result.length === data.byteLength) {
56+
return result;
4357
}
4458
}
4559

test/index.js

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -46,30 +46,26 @@ test('other input throws', function (t) {
4646
});
4747

4848
test('handle all TA types', function (t) {
49-
if (ArrayBuffer.isView && (Buffer.prototype instanceof Uint8Array || Buffer.TYPED_ARRAY_SUPPORT)) {
50-
forEach(availableTypedArrays, function (type) {
51-
var TA = global[type];
52-
if (!(type in fixtures)) {
53-
t.fail('No fixtures for ' + type);
54-
return;
55-
}
49+
forEach(availableTypedArrays, function (type) {
50+
var TA = global[type];
51+
if (!(type in fixtures)) {
52+
t.fail('No fixtures for ' + type);
53+
return;
54+
}
5655

57-
var input = fixtures[type].input;
58-
if (typeof input[0] === 'string') {
59-
for (var i = 0; i < input.length; i++) {
60-
input[i] = BigInt(input[i]);
61-
}
56+
var input = fixtures[type].input;
57+
if (typeof input[0] === 'string') {
58+
for (var i = 0; i < input.length; i++) {
59+
input[i] = BigInt(input[i]);
6260
}
61+
}
6362

64-
t.deepEqual(
65-
toBuffer(new TA(input)),
66-
new Buffer(fixtures[type].output),
67-
type + ' should be converted to Buffer correctly'
68-
);
69-
});
70-
} else {
71-
t.skip('ArrayBuffer.isView and/or TypedArray not fully supported');
72-
}
63+
t.deepEqual(
64+
toBuffer(new TA(input)),
65+
new Buffer(fixtures[type].output),
66+
type + ' should be converted to Buffer correctly'
67+
);
68+
});
7369

7470
t.end();
7571
});

0 commit comments

Comments
 (0)