@@ -112,13 +112,14 @@ console.log(buffer);
112
112
Name|Type|Default
113
113
----|----|----
114
114
extensionCodec | ExtensionCodec | ` ExtensionCodec.defaultCodec `
115
+ context | user-defined | -
116
+ useBigInt64 | boolean | false
115
117
maxDepth | number | ` 100 `
116
118
initialBufferSize | number | ` 2048 `
117
119
sortKeys | boolean | false
118
120
forceFloat32 | boolean | false
119
121
forceIntegerToFloat | boolean | false
120
122
ignoreUndefined | boolean | false
121
- context | user-defined | -
122
123
123
124
### ` decode(buffer: ArrayLike<number> | BufferSource, options?: DecoderOptions): unknown `
124
125
@@ -145,12 +146,13 @@ NodeJS `Buffer` is also acceptable because it is a subclass of `Uint8Array`.
145
146
Name|Type|Default
146
147
----|----|----
147
148
extensionCodec | ExtensionCodec | ` ExtensionCodec.defaultCodec `
149
+ context | user-defined | -
150
+ useBigInt64 | boolean | false
148
151
maxStrLength | number | ` 4_294_967_295 ` (UINT32_MAX)
149
152
maxBinLength | number | ` 4_294_967_295 ` (UINT32_MAX)
150
153
maxArrayLength | number | ` 4_294_967_295 ` (UINT32_MAX)
151
154
maxMapLength | number | ` 4_294_967_295 ` (UINT32_MAX)
152
155
maxExtLength | number | ` 4_294_967_295 ` (UINT32_MAX)
153
- context | user-defined | -
154
156
155
157
You can use ` max${Type}Length ` to limit the length of each type decoded.
156
158
@@ -350,32 +352,48 @@ const decoded = decode(encoded, { extensionCodec, context });
350
352
351
353
#### Handling BigInt with ExtensionCodec
352
354
353
- This library does not handle BigInt by default, but you can handle it with ` ExtensionCodec ` like this:
355
+ This library does not handle BigInt by default, but you have two options to handle it:
356
+
357
+ * Set ` useBigInt64: true ` to map bigint to MessagePack's int64/uint64
358
+ * Define a custom ` ExtensionCodec ` to map bigint to a MessagePack's extension type
359
+
360
+ ` useBigInt64: true ` is the simplest way to handle bigint, but it has limitations:
361
+
362
+ * A bigint is encoded in 8 byte binaries even if it's a small integer
363
+ * A bigint must be smaller than the max value of the uint64 and larger than the min value of the int64. Otherwise the behavior is undefined.
364
+
365
+ So you might want to define a custom codec to handle bigint like this:
354
366
355
367
``` typescript
356
368
import { deepStrictEqual } from " assert" ;
357
369
import { encode , decode , ExtensionCodec } from " @msgpack/msgpack" ;
358
370
371
+ // to define a custom codec:
359
372
const BIGINT_EXT_TYPE = 0 ; // Any in 0-127
360
373
const extensionCodec = new ExtensionCodec ();
361
374
extensionCodec .register ({
362
- type: BIGINT_EXT_TYPE ,
363
- encode : (input : unknown ) => {
364
- if (typeof input === " bigint" ) {
365
- if (input <= Number .MAX_SAFE_INTEGER && input >= Number .MIN_SAFE_INTEGER) {
366
- return encode (parseInt (input .toString (), 10 ));
367
- } else {
368
- return encode (input .toString ());
369
- }
370
- } else {
371
- return null ;
372
- }
373
- },
374
- decode : (data : Uint8Array ) => {
375
- return BigInt (decode (data ));
376
- },
375
+ type: BIGINT_EXT_TYPE ,
376
+ encode(input : unknown ): Uint8Array | null {
377
+ if (typeof input === " bigint" ) {
378
+ if (input <= Number .MAX_SAFE_INTEGER && input >= Number .MIN_SAFE_INTEGER) {
379
+ return encode (Number (input ));
380
+ } else {
381
+ return encode (String (input ));
382
+ }
383
+ } else {
384
+ return null ;
385
+ }
386
+ },
387
+ decode(data : Uint8Array ): bigint {
388
+ const val = decode (data );
389
+ if (! (typeof val === " string" || typeof val === " number" )) {
390
+ throw new DecodeError (` unexpected BigInt source: ${val } (${typeof val }) ` );
391
+ }
392
+ return BigInt (val );
393
+ },
377
394
});
378
395
396
+ // to use it:
379
397
const value = BigInt (Number .MAX_SAFE_INTEGER) + BigInt (1 );
380
398
const encoded: = encode (value , { extensionCodec });
381
399
deepStrictEqual (decode (encoded , { extensionCodec }), value );
@@ -401,10 +419,11 @@ import {
401
419
decodeTimestampToTimeSpec ,
402
420
} from " @msgpack/msgpack" ;
403
421
422
+ // to define a custom codec
404
423
const extensionCodec = new ExtensionCodec ();
405
424
extensionCodec .register ({
406
425
type: EXT_TIMESTAMP , // override the default behavior!
407
- encode : (input : any ) => {
426
+ encode(input : unknown ) : Uint8Array | null {
408
427
if (input instanceof Instant ) {
409
428
const sec = input .seconds ;
410
429
const nsec = Number (input .nanoseconds - BigInt (sec ) * BigInt (1e9 ));
@@ -413,14 +432,15 @@ extensionCodec.register({
413
432
return null ;
414
433
}
415
434
},
416
- decode : (data : Uint8Array ) => {
435
+ decode(data : Uint8Array ): Instant {
417
436
const timeSpec = decodeTimestampToTimeSpec (data );
418
437
const sec = BigInt (timeSpec .sec );
419
438
const nsec = BigInt (timeSpec .nsec );
420
439
return Instant .fromEpochNanoseconds (sec * BigInt (1e9 ) + nsec );
421
440
},
422
441
});
423
442
443
+ // to use it
424
444
const instant = Instant .fromEpochMilliseconds (Date .now ());
425
445
const encoded = encode (instant , { extensionCodec });
426
446
const decoded = decode (encoded , { extensionCodec });
@@ -518,6 +538,7 @@ This is a universal JavaScript library that supports major browsers and NodeJS.
518
538
* Typed arrays (ES2015)
519
539
* Async iterations (ES2018)
520
540
* Features added in ES2015-ES2022
541
+ * whatwg encodings (` TextEncoder ` and ` TextDecoder ` )
521
542
522
543
ES2022 standard library used in this library can be polyfilled with [ core-js] ( https://github.com/zloirock/core-js ) .
523
544
0 commit comments