Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 12 additions & 31 deletions codec/binary.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,25 @@
import { DecodeError, ExtensionCodec, decode, encode } from '@msgpack/msgpack';
import { Options, Packr } from 'msgpackr';
import { Codec } from './types';

const BIGINT_EXT_TYPE = 0;
const extensionCodec = new ExtensionCodec();
extensionCodec.register({
type: BIGINT_EXT_TYPE,
encode(input: unknown): Uint8Array | null {
if (typeof input === 'bigint') {
if (
input <= Number.MAX_SAFE_INTEGER &&
input >= Number.MIN_SAFE_INTEGER
) {
return encode(Number(input));
} else {
return encode(String(input));
}
} else {
return null;
}
},
decode(data: Uint8Array): bigint {
const val = decode(data);
if (!(typeof val === 'string' || typeof val === 'number')) {
throw new DecodeError(`unexpected BigInt source: ${typeof val}`);
}

return BigInt(val);
},
});
const packr = new Packr({
useRecords: false,
moreTypes: true,
bundleStrings: true,
useTimestamp32: false,
useBigIntExtension: true,
skipValues: [undefined],
} as Options);

/**
* Binary codec, uses [msgpack](https://www.npmjs.com/package/@msgpack/msgpack) under the hood
* Binary codec, uses [msgpackr](https://www.npmjs.com/package/msgpackr) under the hood
* @type {Codec}
*/
export const BinaryCodec: Codec = {
toBuffer(obj) {
return encode(obj, { ignoreUndefined: true, extensionCodec });
return packr.pack(obj);
},
fromBuffer: (buff: Uint8Array) => {
const res = decode(buff, { extensionCodec });
const res: unknown = packr.unpack(buff);
if (typeof res !== 'object' || res === null) {
throw new Error('unpacked msg is not an object');
}
Expand Down
223 changes: 209 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@replit/river",
"description": "It's like tRPC but... with JSON Schema Support, duplex streaming and support for service multiplexing. Transport agnostic!",
"version": "0.210.1",
"version": "0.211.0",
"type": "module",
"exports": {
".": {
Expand Down Expand Up @@ -40,7 +40,7 @@
"dist"
],
"dependencies": {
"@msgpack/msgpack": "^3.0.0-beta2",
"msgpackr": "^1.11.2",
"nanoid": "^5.0.9",
"ws": "^8.17.0"
},
Expand Down
Loading