Skip to content

Commit b2d7454

Browse files
authored
use msgpackr instead of @msgpack/msgpack (#350)
## Why its fast! ## What changed use msgpackr instead of @msgpack/msgpack ## Versioning - [ ] Breaking protocol change - [ ] Breaking ts/js API change <!-- Kind reminder to add tests and updated documentation if needed -->
1 parent 8b83f8e commit b2d7454

File tree

3 files changed

+223
-47
lines changed

3 files changed

+223
-47
lines changed

codec/binary.ts

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,25 @@
1-
import { DecodeError, ExtensionCodec, decode, encode } from '@msgpack/msgpack';
1+
import { Options, 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-
},
30-
});
4+
const packr = new Packr({
5+
useRecords: false,
6+
moreTypes: true,
7+
bundleStrings: true,
8+
useTimestamp32: false,
9+
useBigIntExtension: true,
10+
skipValues: [undefined],
11+
} as Options);
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: unknown = 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: 209 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@replit/river",
33
"description": "It's like tRPC but... with JSON Schema Support, duplex streaming and support for service multiplexing. Transport agnostic!",
4-
"version": "0.210.1",
4+
"version": "0.211.0",
55
"type": "module",
66
"exports": {
77
".": {
@@ -40,7 +40,7 @@
4040
"dist"
4141
],
4242
"dependencies": {
43-
"@msgpack/msgpack": "^3.0.0-beta2",
43+
"msgpackr": "^1.11.2",
4444
"nanoid": "^5.0.9",
4545
"ws": "^8.17.0"
4646
},

0 commit comments

Comments
 (0)