Skip to content

Commit 7f34f6a

Browse files
fix(encoder): cast uint8arrays to a buffer
Closes #13
1 parent b92c4e3 commit 7f34f6a

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/encoder.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ class Encoder {
399399
case 'Array':
400400
return this._pushArray(this, obj)
401401
case 'Uint8Array':
402-
return this._pushBuffer(this, obj)
402+
return this._pushBuffer(this, Buffer.isBuffer(obj) ? obj : new Buffer(obj))
403403
case 'Null':
404404
return this._pushUInt8(NULL)
405405
case 'Undefined':

test/fixtures/cases.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,19 @@ exports.encodeGood = [
686686
82 -- Array, 2 items
687687
01 -- [0], 1
688688
02 -- [1], 2
689-
0x820102`] // TODO: move back to good cases, once decode into set is figured out
689+
0x820102`], // TODO: move back to good cases, once decode into set is figured out
690+
[new Uint8Array(new Buffer(0)), "h''", `
691+
40 -- Bytes, length: 0
692+
0x40`],
693+
[new Uint8Array(new Buffer('01020304', 'hex')), "h'01020304'", `
694+
44 -- Bytes, length: 4
695+
01020304 -- 01020304
696+
0x4401020304`],
697+
[new Uint8Array(new Buffer('000102030405060708090a0b0c0d0e0f101112131415161718', 'hex')), "h'000102030405060708090a0b0c0d0e0f101112131415161718'", `
698+
58 -- Bytes, length next 1 byte
699+
19 -- Bytes, length: 25
700+
000102030405060708090a0b0c0d0e0f101112131415161718 -- 000102030405060708090a0b0c0d0e0f101112131415161718
701+
0x5819000102030405060708090a0b0c0d0e0f101112131415161718`]
690702
]
691703

692704
exports.decodeGood = [

0 commit comments

Comments
 (0)