Skip to content

Commit a10269d

Browse files
achingbrainrvagg
andauthored
feat: support decoding ArrayBuffers (#114)
Expands supported input types to include `ArrayBuffer`s to make it easiser for users to use modules like `fetch` that don't return `Uint8Array`s. --------- Co-authored-by: Rod Vagg <[email protected]>
1 parent d6d1def commit a10269d

File tree

4 files changed

+44
-3
lines changed

4 files changed

+44
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@
162162
},
163163
"dependencies": {
164164
"cborg": "^4.0.0",
165-
"multiformats": "^13.0.0"
165+
"multiformats": "^13.1.0"
166166
},
167167
"devDependencies": {
168168
"@ipld/garbage": "^6.0.0",

src/index.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,24 @@ const CID_CBOR_TAG = 42
99
* @typedef {import('multiformats/codecs/interface').ByteView<T>} ByteView
1010
*/
1111

12+
/**
13+
* @template T
14+
* @typedef {import('multiformats/codecs/interface').ArrayBufferView<T>} ArrayBufferView
15+
*/
16+
17+
/**
18+
* @template T
19+
* @param {ByteView<T> | ArrayBufferView<T>} buf
20+
* @returns {ByteView<T>}
21+
*/
22+
export function toByteView (buf) {
23+
if (buf instanceof ArrayBuffer) {
24+
return new Uint8Array(buf, 0, buf.byteLength)
25+
}
26+
27+
return buf
28+
}
29+
1230
/**
1331
* cidEncoder will receive all Objects during encode, it needs to filter out
1432
* anything that's not a CID and return `null` for that so it's encoded as
@@ -123,7 +141,7 @@ export const encode = (node) => cborg.encode(node, _encodeOptions)
123141

124142
/**
125143
* @template T
126-
* @param {ByteView<T>} data
144+
* @param {ByteView<T> | ArrayBufferView<T>} data
127145
* @returns {T}
128146
*/
129-
export const decode = (data) => cborg.decode(data, _decodeOptions)
147+
export const decode = (data) => cborg.decode(toByteView(data), _decodeOptions)

test/test-basics.spec.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,20 @@ describe('dag-cbor', () => {
3939
same(deserializedObj, obj)
4040
})
4141

42+
test('.serialize and .deserialize with ArrayBuffer', () => {
43+
same(bytes.isBinary(serializedObj), true)
44+
45+
// Check for the tag 42
46+
// d8 = tag, 2a = 42
47+
same(bytes.toHex(serializedObj).match(/d82a/g)?.length, 4)
48+
49+
const deserializedObj = decode(serializedObj.buffer.slice(
50+
serializedObj.byteOffset,
51+
serializedObj.byteOffset + serializedObj.byteLength)
52+
)
53+
same(deserializedObj, obj)
54+
})
55+
4256
test('.serialize and .deserialize large objects', () => {
4357
// larger than the default borc heap size, should auto-grow the heap
4458
const dataSize = 128 * 1024

test/ts-use/src/main.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ function useCodecFeature (codec: BlockCodec<0x71, any>) {
1515
// use only as a BlockDecoder
1616
useDecoder(codec)
1717

18+
// use with ArrayBuffer input type
19+
useDecoderWithArrayBuffer(codec)
20+
1821
// use as a full BlockCodec which does both BlockEncoder & BlockDecoder
1922
useBlockCodec(codec)
2023
}
@@ -32,6 +35,12 @@ function useDecoder<Codec extends number> (decoder: BlockDecoder<Codec, Uint8Arr
3235
console.log('[TS] ✓ { decoder: BlockDecoder }')
3336
}
3437

38+
function useDecoderWithArrayBuffer<Codec extends number> (decoder: BlockDecoder<Codec, Uint8Array>) {
39+
deepStrictEqual(decoder.code, 0x70)
40+
deepStrictEqual(decoder.decode(Uint8Array.from([100, 98, 108, 105, 112]).buffer), 'blip')
41+
console.log('[TS] ✓ { decoder: BlockDecoder }')
42+
}
43+
3544
function useBlockCodec<Codec extends number> (blockCodec: BlockCodec<Codec, string>) {
3645
deepStrictEqual(blockCodec.code, 0x71)
3746
deepStrictEqual(blockCodec.name, 'dag-cbor')

0 commit comments

Comments
 (0)