@@ -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
176193Most 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';
195212Tag .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
200227JavaScript "boxed" types, such as those created with ` new Number(3) ` , are
@@ -231,6 +258,22 @@ encode(encodedNumber(4)); // 0xf94400
231258Note that the default format for ` encodedNumber ` is the preferred floating
232259point 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
236279The tests for this package use a set of test vectors from RFC 8949 appendix A
0 commit comments