Skip to content

Commit 894e2f2

Browse files
jscissrbrianc
authored andcommitted
Support Uint8Array values
1 parent 32537d5 commit 894e2f2

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

lib/utils.js

+7
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ var prepareValue = function (val, seen) {
5050
if (val instanceof Buffer) {
5151
return val
5252
}
53+
if (ArrayBuffer.isView(val)) {
54+
var buf = Buffer.from(val.buffer, val.byteOffset, val.byteLength)
55+
if (buf.length === val.byteLength) {
56+
return buf
57+
}
58+
return buf.slice(val.byteOffset, val.byteOffset + val.byteLength) // Node.js v4 does not support those Buffer.from params
59+
}
5360
if (val instanceof Date) {
5461
if (defaults.parseInputDatesAsUTC) {
5562
return dateToStringUTC(val)

test/unit/utils-tests.js

+7
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ test('prepareValues: buffer prepared properly', function () {
5353
assert.strictEqual(buf, out)
5454
})
5555

56+
test('prepareValues: Uint8Array prepared properly', function () {
57+
var buf = new Uint8Array([1, 2, 3]).subarray(1, 2)
58+
var out = utils.prepareValue(buf)
59+
assert.ok(Buffer.isBuffer(out))
60+
assert.deepEqual(out, [2])
61+
})
62+
5663
test('prepareValues: date prepared properly', function () {
5764
helper.setTimezoneOffset(-330)
5865

0 commit comments

Comments
 (0)