summaryrefslogtreecommitdiff
path: root/engine/utils/coding.ts
blob: d0904fe95c1a4849cddcc9acb010e4d71e322552 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { compatto } from './compatto';
import dictionary from './dictionary';

const { compress, decompress } = compatto({ dictionary });

const replacer = (_key: any, value: any) => {
  if (value instanceof Map) {
    return {
      dataType: 'Map',
      value: Array.from(value.entries())
    };
  } else {
    return value;
  }
};

const sortObj = (obj: any): any =>
  obj === null || typeof obj !== 'object'
    ? obj
    : Array.isArray(obj)
    ? obj.map(sortObj)
    : Object.assign(
        {},
        ...Object.entries(obj)
          .sort(([keyA], [keyB]) => keyA.localeCompare(keyB))
          .map(([k, v]) => ({ [k]: sortObj(v) }))
      );

const reviver = (_key: any, value: any) => {
  if (typeof value === 'object' && value !== null) {
    if (value.dataType === 'Map') {
      return new Map(value.value);
    }
  }
  return value;
};

// "deterministic" stringify

export const stringify = (obj: any): string => {
  return JSON.stringify(sortObj(obj), replacer);
};

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;
};