cborg 4.1.0 → 4.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ ## [4.1.2](https://github.com/rvagg/cborg/compare/v4.1.1...v4.1.2) (2024-03-16)
2
+
3
+
4
+ ### Trivial Changes
5
+
6
+ * **deps-dev:** bump @semantic-release/github from 9.2.6 to 10.0.0 ([accb364](https://github.com/rvagg/cborg/commit/accb364f14361f2f5536276b7139d44137ef7ba8))
7
+
8
+ ## [4.1.1](https://github.com/rvagg/cborg/compare/v4.1.0...v4.1.1) (2024-03-11)
9
+
10
+
11
+ ### Trivial Changes
12
+
13
+ * add byte strings example using tags to retain typedarray types ([5bf989b](https://github.com/rvagg/cborg/commit/5bf989bf7dde0a6124fb1605f3caf899273c6d4e))
14
+
1
15
  ## [4.1.0](https://github.com/rvagg/cborg/compare/v4.0.9...v4.1.0) (2024-02-29)
2
16
 
3
17
 
package/README.md CHANGED
@@ -36,6 +36,7 @@
36
36
  * [Round-trip consistency](#round-trip-consistency)
37
37
  * [JSON mode](#json-mode)
38
38
  * [Example](#example-1)
39
+ * [Advanced types and tags](#advanced-types-and-tags)
39
40
  * [License and Copyright](#license-and-copyright)
40
41
 
41
42
  ## Example
@@ -501,6 +502,10 @@ encoded: Uint8Array(34) [
501
502
  encoded (string): {"this":{"is":"JSON!","yay":true}}
502
503
  ```
503
504
 
505
+ ## Advanced types and tags
506
+
507
+ As demonstrated above, the ability to provide custom `typeEncoders` to `encode()`, `tags` and even a custom `tokenizer` to `decode()` allow for quite a bit of flexibility in manipulating both the encode and decode process. An advanced example that uses all of these features can be found in [example-bytestrings.js](./example-bytestrings.js) which demonstrates how one might implement [RFC 8746](https://www.rfc-editor.org/rfc/rfc8746.html) to allow typed arrays to round-trip through CBOR and retain their original types. Since cborg is designed to speak purely in terms of `Uint8Array`s, its default behaviour will squash all typed arrays down to their byte array forms and materialise them as plain `Uint8Arrays`. Where round-trip fidelity is important and CBOR tags are an option, this form of usage is an option.
508
+
504
509
  ## License and Copyright
505
510
 
506
511
  Copyright 2020 Rod Vagg
@@ -0,0 +1,180 @@
1
+ /*
2
+ RFC 8746 defines a set of tags to use for typed arrays. Out of the box, cborg doesn't care about
3
+ tags and just squashes all concerns around byte arrays to Uint8Array with major type 2. This is
4
+ fine for most use cases, but it is lossy, you can't round-trip and retain your original type.
5
+
6
+ This example shows how to use cborg to round-trip a typed array with tags.
7
+
8
+ https://www.rfc-editor.org/rfc/rfc8746.html
9
+ */
10
+
11
+ import { encode, decode, Token, Tokenizer, Type } from 'cborg.js'
12
+
13
+ const tagUint8Array = 64
14
+ const tagUint64Array = 71
15
+ // etc... see https://www.rfc-editor.org/rfc/rfc8746.html#name-iana-considerations
16
+
17
+ /* ENCODERS */
18
+
19
+ /**
20
+ * @param {any} obj
21
+ * @returns {[Token]}
22
+ */
23
+ function uint8ArrayEncoder (obj) {
24
+ if (!(obj instanceof Uint8Array)) {
25
+ throw new Error('expected Uint8Array')
26
+ }
27
+ return [
28
+ new Token(Type.tag, tagUint8Array),
29
+ new Token(Type.bytes, obj)
30
+ ]
31
+ }
32
+
33
+ /**
34
+ * @param {any} obj
35
+ * @returns {[Token]}
36
+ */
37
+ function uint64ArrayEncoder (obj) {
38
+ if (!(obj instanceof BigUint64Array)) {
39
+ throw new Error('expected BigUint64Array')
40
+ }
41
+ return [
42
+ new Token(Type.tag, tagUint64Array),
43
+ // BigUint64Array to a Uint8Array, but we have to pay attention to the possibility of it being
44
+ // a view of a larger ArrayBuffer.
45
+ new Token(Type.bytes, new Uint8Array(obj.buffer, obj.byteOffset, obj.byteLength))
46
+ ]
47
+ }
48
+
49
+ // etc...
50
+
51
+ const typeEncoders = {
52
+ Uint8Array: uint8ArrayEncoder,
53
+ BigUint64Array: uint64ArrayEncoder
54
+ }
55
+
56
+ /* DECODERS */
57
+
58
+ /**
59
+ * @param {ArrayBuffer} bytes
60
+ * @returns {any}
61
+ */
62
+ function uint8ArrayDecoder (bytes) {
63
+ if (!(bytes instanceof ArrayBuffer)) {
64
+ throw new Error('expected ArrayBuffer')
65
+ }
66
+ return new Uint8Array(bytes)
67
+ }
68
+
69
+ /**
70
+ * @param {ArrayBuffer} bytes
71
+ * @returns {any}
72
+ */
73
+ function uint64ArrayDecoder (bytes) {
74
+ if (!(bytes instanceof ArrayBuffer)) {
75
+ throw new Error('expected ArrayBuffer')
76
+ }
77
+ return new BigUint64Array(bytes)
78
+ }
79
+
80
+ // etc...
81
+
82
+ const tags = []
83
+ tags[tagUint8Array] = uint8ArrayDecoder
84
+ tags[tagUint64Array] = uint64ArrayDecoder
85
+
86
+ /* TOKENIZER */
87
+
88
+ // We have to deal with the fact that cborg talks in Uint8Arrays but we now want it to treat major 2
89
+ // as ArrayBuffers, so we have to transform the token stream to replace the Uint8Array with an
90
+ // ArrayBuffer.
91
+
92
+ class ArrayBufferTransformingTokeniser extends Tokenizer {
93
+ next () {
94
+ const nextToken = super.next()
95
+ if (nextToken.type === Type.bytes) {
96
+ // Transform the (assumed) Uint8Array value to an ArrayBuffer of the same bytes, note though
97
+ // that all tags we care about are going to be <tag><bytes>, so we're also transforming those
98
+ // into ArrayBuffers, so our tag decoders need to also assume they are getting ArrayBuffers
99
+ // now. An alternative would be to watch the token stream for <tag> and not transform the next
100
+ // token if it's <bytes>, but that's a bit more complicated for demo purposes.
101
+ nextToken.value = nextToken.value.buffer
102
+ }
103
+ return nextToken
104
+ }
105
+ }
106
+
107
+ // Optional: a new decode() wrapper, mainly so we don't have to deal with the complications of\
108
+ // instantiating a Tokenizer which needs both data and the options.
109
+ function byteStringDecoder (data, options) {
110
+ options = Object.assign({}, options, {
111
+ tags,
112
+ tokenizer: new ArrayBufferTransformingTokeniser(data, options)
113
+ })
114
+ return decode(data, options)
115
+ }
116
+
117
+ /* ROUND-TRIP */
118
+
119
+ const original = {
120
+ u8: new Uint8Array([1, 2, 3, 4, 5]),
121
+ u64: new BigUint64Array([10000000000000000n, 20000000000000000n, 30000000000000000n, 40000000000000000n, 50000000000000000n]),
122
+ ab: new Uint8Array([6, 7, 8, 9, 10]).buffer
123
+ }
124
+
125
+ const encoded = encode(original, { typeEncoders })
126
+
127
+ const decoded = byteStringDecoder(encoded)
128
+
129
+ console.log('Original:', original)
130
+ console.log('Encoded:', Buffer.from(encoded).toString('hex')) // excuse the Buffer, sorry browser peeps
131
+ console.log('Decoded:', decoded)
132
+
133
+ /* Output:
134
+
135
+ Original: {
136
+ u8: Uint8Array(5) [ 1, 2, 3, 4, 5 ],
137
+ u64: BigUint64Array(5) [
138
+ 10000000000000000n,
139
+ 20000000000000000n,
140
+ 30000000000000000n,
141
+ 40000000000000000n,
142
+ 50000000000000000n
143
+ ],
144
+ ab: ArrayBuffer { [Uint8Contents]: <06 07 08 09 0a>, byteLength: 5 }
145
+ }
146
+ Encoded: a362616245060708090a627538d84045010203040563753634d84758280000c16ff2862300000082dfe40d47000000434fd7946a00000004bfc91b8e000000c52ebca2b100
147
+ Decoded: {
148
+ ab: ArrayBuffer { [Uint8Contents]: <06 07 08 09 0a>, byteLength: 5 },
149
+ u8: Uint8Array(5) [ 1, 2, 3, 4, 5 ],
150
+ u64: BigUint64Array(5) [
151
+ 10000000000000000n,
152
+ 20000000000000000n,
153
+ 30000000000000000n,
154
+ 40000000000000000n,
155
+ 50000000000000000n
156
+ ]
157
+ }
158
+
159
+ */
160
+
161
+ /* Diagnostic:
162
+
163
+ $ cborg hex2diag a362616245060708090a627538d84045010203040563753634d84758280000c16ff2862300000082dfe40d47000000434fd7946a00000004bfc91b8e000000c52ebca2b100
164
+ a3 # map(3)
165
+ 62 # string(2)
166
+ 6162 # "ab"
167
+ 45 # bytes(5)
168
+ 060708090a # "\x06\x07\x08\x09\x0a"
169
+ 62 # string(2)
170
+ 7538 # "u8"
171
+ d8 40 # tag(64)
172
+ 45 # bytes(5)
173
+ 0102030405 # "\x01\x02\x03\x04\x05"
174
+ 63 # string(3)
175
+ 753634 # "u64"
176
+ d8 47 # tag(71)
177
+ 58 28 # bytes(40)
178
+ 0000c16ff2862300000082dfe40d47000000434fd7 # "\x00\x00Áoò\x86#\x00\x00\x00\x82ßä\x0dG\x00\x00\x00CO×"
179
+ 946a00000004bfc91b8e000000c52ebca2b100 # "\x94j\x00\x00\x00\x04¿É\x1b\x8e\x00\x00\x00Å.¼¢±\x00
180
+ */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cborg",
3
- "version": "4.1.0",
3
+ "version": "4.1.2",
4
4
  "description": "Fast CBOR with a focus on strictness",
5
5
  "main": "cborg.js",
6
6
  "type": "module",
@@ -31,7 +31,7 @@
31
31
  "@semantic-release/changelog": "^6.0.3",
32
32
  "@semantic-release/commit-analyzer": "^11.1.0",
33
33
  "@semantic-release/git": "^10.0.1",
34
- "@semantic-release/github": "^9.2.6",
34
+ "@semantic-release/github": "^10.0.0",
35
35
  "@semantic-release/npm": "^11.0.2",
36
36
  "@semantic-release/release-notes-generator": "^12.1.0",
37
37
  "@types/chai": "^4.3.11",