summaryrefslogtreecommitdiff
path: root/engine/utils
diff options
context:
space:
mode:
Diffstat (limited to 'engine/utils')
-rw-r--r--engine/utils/coding.ts18
m---------engine/utils/compatto0
-rw-r--r--engine/utils/dictionary.ts56
3 files changed, 71 insertions, 3 deletions
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 = <T>(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 = <T>(serialized: Uint8Array): T => {
+ //return JSON.parse(new TextDecoder().decode(serialized), reviver) as T;
+ return JSON.parse(decompress(serialized), reviver) as T;
};
diff --git a/engine/utils/compatto b/engine/utils/compatto
new file mode 160000
+Subproject 68628ac6f173f50b81cc2d83a75de176484b2c7
diff --git a/engine/utils/dictionary.ts b/engine/utils/dictionary.ts
new file mode 100644
index 0000000..5a19c6d
--- /dev/null
+++ b/engine/utils/dictionary.ts
@@ -0,0 +1,56 @@
+// basically a list of common strings between network components to help in compression
+// can't be longer than 254 "words"
+export default [
+ '{',
+ '}',
+ ',',
+ '"',
+ "'",
+ ',',
+ '1',
+ '2',
+ '3',
+ '4',
+ '5',
+ '6',
+ '7',
+ '8',
+ '9',
+ '0',
+ '.',
+ 'Map',
+ '[',
+ ']',
+ 'BoundingBox',
+ 'args',
+ 'height',
+ 'width',
+ 'name',
+ 'body',
+ 'Velocity',
+ 'Control',
+ 'Component',
+ 'NEW_ENTITIES',
+ 'REMOVE_ENTITIES',
+ 'UPDATE_ENTITIES',
+ 'NEW_INPUT',
+ 'REMOVE_INPUT',
+ 'boundingBox',
+ 'dTheta',
+ 'id',
+ 'Forces',
+ ':',
+ 'control',
+ 'controllableBy',
+ 'dimension',
+ 'rotation',
+ 'forces',
+ 'velocity',
+ 'center',
+ 'dCartesian',
+ 'x',
+ 'y',
+ '"body":[{"args":{',
+ 'controlVelocityComponent',
+ '"boundingBox":{"center":'
+];