From 6708160cec15ccd4a79979ed8ba79310fbd9285f Mon Sep 17 00:00:00 2001 From: Elizabeth Hunt Date: Tue, 5 Sep 2023 22:28:11 -0600 Subject: add compression to engine messages --- engine/utils/coding.ts | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'engine/utils/coding.ts') diff --git a/engine/utils/coding.ts b/engine/utils/coding.ts index 283844f..d0904fe 100644 --- a/engine/utils/coding.ts +++ b/engine/utils/coding.ts @@ -1,3 +1,8 @@ +import { compatto } from './compatto'; +import dictionary from './dictionary'; + +const { compress, decompress } = compatto({ dictionary }); + const replacer = (_key: any, value: any) => { if (value instanceof Map) { return { @@ -31,10 +36,17 @@ const reviver = (_key: any, value: any) => { }; // "deterministic" stringify -export const stringify = (obj: any) => { + +export const stringify = (obj: any): string => { return JSON.stringify(sortObj(obj), replacer); }; -export const parse = (str: string) => { - return JSON.parse(str, reviver) as unknown as T; +export const serialize = (obj: any): Uint8Array => { + //return new Uint8Array(new TextEncoder().encode(stringify(obj))); + return compress(stringify(obj)); +}; + +export const parse = (serialized: Uint8Array): T => { + //return JSON.parse(new TextDecoder().decode(serialized), reviver) as T; + return JSON.parse(decompress(serialized), reviver) as T; }; -- cgit v1.3