File size: 1,810 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// @flow

// Deep freeze an object by using a Proxy.
// This is better than Object.freeze() as we can create coherent error messages
// and easily only deliver frozen subobjects when they are accessed. We can
// even add custom logic, like warning if you access a property that doesn't exist.
/* eslint-disable no-console */
export default function deepFreeze<T: { [key: string]: any }>(
  inputObj: T,
  options: { get: boolean, set: boolean } = { get: true, set: true }
): $ReadOnly<{| ...T |}> {
  // Our handler that rejects any change to the object and any nested objects inside it
  const deepFreezer = {};
  if (options.get) {
    deepFreezer.get = function get(
      obj: T,
      prop: $Keys<T>,
      _receiver: Proxy<T>
    ): any {
      // Clone w/o Proxy
      if (prop === "toJSON") return () => obj;
      // If dealing with nested object, nest the proxy untill it reaches the direct property of it's parent proxy
      if (typeof obj[prop] === "object" && obj[prop] !== null) {
        return new Proxy(obj[prop], deepFreezer);
      }
      // If prop is directly accessible, just do the default operation
      else {
        if (
          !(prop in obj) &&
          prop !== "length" &&
          prop !== "__esModule" &&
          typeof prop !== "symbol"
        ) {
          throw new Error(
            `Can not get unknown prop "${String(prop)}" on frozen object.`
          );
        }
        return obj[prop];
      }
    };
  }
  if (options.set) {
    deepFreezer.set = function set(
      obj: T,
      prop: string,
      _val: any,
      _rec: Proxy<T>
    ): boolean {
      throw new Error(`Can not set unknown prop "${prop}" on frozen object.`);
    };
  }
  // $FlowIgnore for all useful purposes the output type is T here
  return new Proxy(inputObj, deepFreezer);
}