Skip to content

Commit

Permalink
Fixes #52, but uses Uint8Array not ArrayBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
hildjj committed Jan 12, 2021
1 parent 50077a2 commit 871e0f7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
17 changes: 16 additions & 1 deletion lib/decoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ class Decoder extends BinaryParseStream {
* function returns the correctly-created value for that tag.
* @param {boolean} [options.bigint=true] generate JavaScript BigInt's
* instead of BigNumbers, when possible.
* @param {boolean} [options.preferWeb=false] if true, prefer Uint8Arrays to
* be generated instead of node Buffers. This might turn on some more
* changes in the future, so forward-compatibility is not guaranteed yet.
*/
constructor(options) {
options = options || {}
Expand All @@ -89,11 +92,14 @@ class Decoder extends BinaryParseStream {
delete options.max_depth
const bigI = (options.bigint != null) ? options.bigint : true
delete options.bigint
const preferWeb = !!options.preferWeb
delete options.preferWeb
super(options)

this.running = true
this.max_depth = max_depth
this.tags = tags
this.preferWeb = preferWeb
this.bigint = bigI
if (bigI) {
if (this.tags == null) {
Expand Down Expand Up @@ -435,7 +441,11 @@ class Decoder extends BinaryParseStream {
switch (val) {
case 0:
this.emit('start-string', mt, val, parent_major, parent_length)
val = (mt === MT.BYTE_STRING) ? Buffer.allocUnsafe(0) : ''
if (mt === MT.UTF8_STRING) {
val = ''
} else {
val = this.preferWeb ? new Uint8Array(0) : Buffer.allocUnsafe(0)
}
break
case -1:
this.emit('start', mt, SYMS.STREAM, parent_major, parent_length)
Expand All @@ -447,6 +457,8 @@ class Decoder extends BinaryParseStream {
val = yield val
if (mt === MT.UTF8_STRING) {
val = utils.utf8(val)
} else if (this.preferWeb) {
val = new Uint8Array(val.buffer, val.byteOffset, val.length)
}
}
break
Expand Down Expand Up @@ -552,6 +564,9 @@ class Decoder extends BinaryParseStream {
switch (parent[MAJOR]) {
case MT.BYTE_STRING:
val = parent.slice()
if (this.preferWeb) {
val = new Uint8Array(val.buffer, val.byteOffset, val.length)
}
break
case MT.UTF8_STRING:
val = parent.toString('utf-8')
Expand Down
4 changes: 1 addition & 3 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,7 @@ exports.guessEncoding = function guessEncoding(input, encoding) {
return new NoFilter(input)
} else if (ArrayBuffer.isView(input)) {
return new NoFilter(
Buffer.from(input.buffer.slice(
input.byteOffset,
input.byteOffset + input.byteLength)))
Buffer.from(input.buffer, input.byteOffset, input.byteLength))
} else if (input instanceof ArrayBuffer) {
return new NoFilter(Buffer.from(input))
} else if (input instanceof stream.Readable) {
Expand Down
11 changes: 11 additions & 0 deletions test/decoder.ava.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,14 @@ test('typed arrays', t => {
t.is(cbor.decode(new Uint32Array(u8abc.buffer)), 'abc')
t.is(cbor.decode(new DataView(u8abc.buffer)), 'abc')
})

test('preferWeb', t => {
t.deepEqual(cbor.decodeFirstSync('40', {preferWeb: true}),
new Uint8Array([]))
t.deepEqual(cbor.decodeFirstSync('4141', {preferWeb: true}),
new Uint8Array([0x41]))
t.deepEqual(cbor.decodeFirstSync('5fff', {preferWeb: true}),
new Uint8Array([]))
t.deepEqual(cbor.decodeFirstSync('5f42010243030405ff', {preferWeb: true}),
new Uint8Array([0x01, 0x02, 0x03, 0x04, 0x05]))
})

0 comments on commit 871e0f7

Please sign in to comment.