Skip to content

Commit 6b8fc06

Browse files
committed
actually use msgpackr
1 parent 69bb032 commit 6b8fc06

File tree

3 files changed

+219
-44
lines changed

3 files changed

+219
-44
lines changed

codec/binary.ts

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,25 @@
1-
import { DecodeError, ExtensionCodec, decode, encode } from '@msgpack/msgpack';
1+
import { Packr } from 'msgpackr';
22
import { Codec } from './types';
33

4-
const BIGINT_EXT_TYPE = 0;
5-
const extensionCodec = new ExtensionCodec();
6-
extensionCodec.register({
7-
type: BIGINT_EXT_TYPE,
8-
encode(input: unknown): Uint8Array | null {
9-
if (typeof input === 'bigint') {
10-
if (
11-
input <= Number.MAX_SAFE_INTEGER &&
12-
input >= Number.MIN_SAFE_INTEGER
13-
) {
14-
return encode(Number(input));
15-
} else {
16-
return encode(String(input));
17-
}
18-
} else {
19-
return null;
20-
}
21-
},
22-
decode(data: Uint8Array): bigint {
23-
const val = decode(data);
24-
if (!(typeof val === 'string' || typeof val === 'number')) {
25-
throw new DecodeError(`unexpected BigInt source: ${typeof val}`);
26-
}
27-
28-
return BigInt(val);
29-
},
4+
const packr = new Packr({
5+
useRecords: false,
6+
moreTypes: true,
7+
bundleStrings: true,
8+
useTimestamp32: false,
9+
useBigIntExtension: true,
10+
skipValues: [undefined],
3011
});
3112

3213
/**
33-
* Binary codec, uses [msgpack](https://www.npmjs.com/package/@msgpack/msgpack) under the hood
14+
* Binary codec, uses [msgpackr](https://www.npmjs.com/package/msgpackr) under the hood
3415
* @type {Codec}
3516
*/
3617
export const BinaryCodec: Codec = {
3718
toBuffer(obj) {
38-
return encode(obj, { ignoreUndefined: true, extensionCodec });
19+
return packr.pack(obj);
3920
},
4021
fromBuffer: (buff: Uint8Array) => {
41-
const res = decode(buff, { extensionCodec });
22+
const res = packr.unpack(buff);
4223
if (typeof res !== 'object' || res === null) {
4324
throw new Error('unpacked msg is not an object');
4425
}

package-lock.json

Lines changed: 207 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
"dist"
4141
],
4242
"dependencies": {
43-
"@msgpack/msgpack": "^3.1.2",
43+
"msgpackr": "^1.11.2",
4444
"nanoid": "^5.0.9",
4545
"ws": "^8.17.0"
4646
},

0 commit comments

Comments
 (0)