File size: 3,409 Bytes
0bfe2e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import { compressedConfigMap } from './details';

export const getTimeTakenSincePoint = (point: number) => {
  const timeNow = new Date().getTime();
  const duration = timeNow - point;
  // format duration and choose unit and return
  const nanos = duration * 1_000_000; // Convert to nanoseconds
  const micros = duration * 1_000; // Convert to microseconds

  if (nanos < 1) {
    return `${nanos.toFixed(2)}ns`;
  } else if (micros < 1) {
    return `${micros.toFixed(2)}µs`;
  } else if (duration < 1000) {
    return `${duration.toFixed(2)}ms`;
  } else {
    return `${(duration / 1000).toFixed(2)}s`;
  }
};
const MINIFY_SUFFIX = '__ignore';
const MAX_DEPTH = 50;
const MAX_KEYS = 200;

export function minifyConfig(config: any) {
  // if already minified, return
  if (config.a) return config;
  const minifiedConfig = minifyObj(config);
  return minifiedConfig;
}

export function unminifyConfig(config: any) {
  // if not minified, return

  if (config.a === undefined) return config;
  const unminifiedConfig = unminifyObj(config);
  return unminifiedConfig;
}

export function minifyObj(obj: any, depth: number = 0): any {
  if (depth > MAX_DEPTH) {
    throw new Error('Max depth reached');
  }
  if (typeof obj !== 'object' || obj === null) {
    let valueToCheck = obj;
    if (Object.values(compressedConfigMap).includes(valueToCheck)) {
      return `${obj}${MINIFY_SUFFIX}`;
    }
    const minifiedValue = compressedConfigMap[obj] ?? obj;
    if (typeof obj === 'string' && (obj === 'true' || obj === 'false')) {
      return `${obj}${MINIFY_SUFFIX}`;
    }
    return minifiedValue;
  }

  if (Array.isArray(obj)) {
    if (obj.length > MAX_KEYS) {
      throw new Error('Max keys reached');
    }
    return obj.map((item) => minifyObj(item, depth + 1));
  }
  if (Object.keys(obj)?.length > MAX_KEYS) {
    throw new Error('Max keys reached');
  }

  const entries = Object.entries(obj).map(([key, value]) => {
    const newKey = compressedConfigMap[key] ?? key;
    const newValue = minifyObj(value, depth + 1);
    return [newKey, newValue];
  });
  const newObj = Object.fromEntries(entries);
  return newObj;
}

export function unminifyObj(obj: any, depth: number = 0): any {
  if (depth > MAX_DEPTH) {
    throw new Error('Max depth reached');
  }
  const reversedMap = reverseConfigMap();

  if (typeof obj !== 'object' || obj === null) {
    if (typeof obj === 'string' && obj.endsWith(MINIFY_SUFFIX)) {
      return obj.slice(0, -MINIFY_SUFFIX.length);
    }
    const originalKey = reversedMap[obj] ?? obj;
    if (originalKey === 'true' || originalKey === 'false') {
      return originalKey === 'true';
    }
    if (originalKey === 'null') {
      return null;
    }
    return originalKey;
  }

  if (Array.isArray(obj)) {
    if (obj.length > MAX_KEYS) {
      throw new Error('Max keys reached');
    }
    return obj.map((item) => unminifyObj(item, depth + 1));
  }

  if (Object.keys(obj)?.length > MAX_KEYS) {
    throw new Error('Max keys reached');
  }

  return Object.fromEntries(
    Object.entries(obj).map(([key, value]) => {
      const originalKey = reversedMap[key] ?? key;
      const originalValue = unminifyObj(value, depth + 1);
      return [originalKey, originalValue];
    })
  );
}

const reverseConfigMap = (): Record<string, any> => {
  return Object.fromEntries(
    Object.entries(compressedConfigMap).map(([key, value]) => [value, key])
  );
};