Skip to content

Commit 729ad6d

Browse files
authored
Merge pull request #168 from Kapsonfire-DE/patch-1
Update README.md
2 parents ecdda8f + 2732911 commit 729ad6d

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

README.md

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -361,17 +361,21 @@ import { encode, decode, ExtensionCodec } from "@msgpack/msgpack";
361361
const BIGINT_EXT_TYPE = 0; // Any in 0-127
362362
const extensionCodec = new ExtensionCodec();
363363
extensionCodec.register({
364-
type: BIGINT_EXT_TYPE,
365-
encode: (input: unknown) => {
366-
if (typeof input === "bigint") {
367-
return encode(input.toString());
368-
} else {
369-
return null;
370-
}
371-
},
372-
decode: (data: Uint8Array) => {
373-
return BigInt(decode(data));
374-
},
364+
type: BIGINT_EXT_TYPE,
365+
encode: (input: unknown) => {
366+
if (typeof input === "bigint") {
367+
if (input <= Number.MAX_SAFE_INTEGER && input >= Number.MIN_SAFE_INTEGER) {
368+
return encode(parseInt(input.toString(), 10));
369+
} else {
370+
return encode(input.toString());
371+
}
372+
} else {
373+
return null;
374+
}
375+
},
376+
decode: (data: Uint8Array) => {
377+
return BigInt(decode(data));
378+
},
375379
});
376380

377381
const value = BigInt(Number.MAX_SAFE_INTEGER) + BigInt(1);

test/codec-bigint.test.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@ const extensionCodec = new ExtensionCodec();
55
extensionCodec.register({
66
type: 0,
77
encode: (input: unknown) => {
8-
if (typeof input === "bigint") {
9-
return encode(input.toString());
10-
} else {
11-
return null;
12-
}
8+
if (typeof input === "bigint") {
9+
if (input <= Number.MAX_SAFE_INTEGER && input >= Number.MIN_SAFE_INTEGER) {
10+
return encode(parseInt(input.toString(), 10));
11+
} else {
12+
return encode(input.toString());
13+
}
14+
} else {
15+
return null;
16+
}
1317
},
1418
decode: (data: Uint8Array) => {
1519
return BigInt(decode(data));

0 commit comments

Comments
 (0)