Skip to content

Commit cec370d

Browse files
authored
Merge pull request #80 from hildjj/per-call-tags
per call tags
2 parents 6206274 + 513a9ae commit cec370d

16 files changed

+459
-215
lines changed

README.md

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ The following types are supported for encoding:
5858

5959
- boolean
6060
- number (including -0, NaN, and ±Infinity)
61-
- string
61+
- string (Set the wtf8 option to enable encoding invalid UTF-16 strings to tag 273)
6262
- bigint
6363
- Array
6464
- Set
@@ -171,6 +171,23 @@ registerEncoder(Buffer, b => [
171171
]);
172172
```
173173

174+
If you want to have different encoders for different calls to `encode`, you
175+
can pass a `types` parameter to `encode`:
176+
177+
```js
178+
import {TypeEncoderMap, encode} from 'cbor2';
179+
import {Buffer} from 'node:buffer';
180+
181+
const types = new TypeEncoderMap();
182+
types.registerEncoder(Buffer, b => [
183+
// Don't write a tag
184+
NaN,
185+
// New view on the ArrayBuffer, without copying bytes
186+
new Uint8Array(b.buffer, b.byteOffset, b.byteLength),
187+
]);
188+
encode(Buffer.from('foo'), {types});
189+
```
190+
174191
## Adding new decoders
175192

176193
Most of the time, you will want to add support for decoding a new tag type. If
@@ -195,6 +212,16 @@ import {Tag} from 'cbor2/tag';
195212
Tag.registerDecoder(0, ({contents}) => Temporal.Instant.from(contents));
196213
```
197214

215+
Finally, you can pass a `tags` parameter to `decode` to specify tag decoding
216+
for a single call to `decode`:
217+
218+
```js
219+
const tags = new Map([
220+
[64000, ({contents}) => new Foo(contents[0], contents[1])],
221+
]);
222+
decode('d9fa00820102', {tags});
223+
```
224+
198225
## Boxed Types
199226

200227
JavaScript "boxed" types, such as those created with `new Number(3)`, are
@@ -231,6 +258,22 @@ encode(encodedNumber(4)); // 0xf94400
231258
Note that the default format for `encodedNumber` is the preferred floating
232259
point representation, which can be explicitly selected with an encoding of `'f'`.
233260

261+
## CBOR Sequences
262+
263+
If you would like to decode a
264+
[CBOR Sequence](https://www.rfc-editor.org/rfc/rfc8742.html),
265+
use the `decodeSequence` method, which returns an iterator:
266+
267+
```js
268+
import {decodeSequence} from 'cbor';
269+
270+
for (const item of decodeSequence('0102')) {
271+
console.log(item); // First 1, then 2
272+
}
273+
// Or
274+
const seq = [...decodeSequence('0102')]; // [1, 2]
275+
```
276+
234277
## Developers
235278

236279
The tests for this package use a set of test vectors from RFC 8949 appendix A

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,12 @@
7474
"c8": "10.1.3",
7575
"cbor-edn": "0.2.2",
7676
"eslint": "9.26.0",
77-
"eslint-plugin-jsdoc": "50.6.11",
77+
"eslint-plugin-jsdoc": "50.6.14",
7878
"package-extract": "3.1.0",
7979
"rimraf": "^5.0.10",
8080
"tsup": "8.4.0",
8181
"typedoc": "0.28.4",
82-
"typescript-eslint": "8.32.0"
82+
"typescript-eslint": "8.32.1"
8383
},
8484
"pnpm": {
8585
"overrides": {
@@ -89,7 +89,7 @@
8989
"esbuild"
9090
]
9191
},
92-
"packageManager": "pnpm@10.10.0",
92+
"packageManager": "pnpm@10.11.0",
9393
"engines": {
9494
"node": ">=20"
9595
}

0 commit comments

Comments
 (0)