( store: Map<string, unknown> )
| 5 | * the full Object prototype chain. |
| 6 | */ |
| 7 | export function createVariablesProxy( |
| 8 | store: Map<string, unknown> |
| 9 | ): Record<string, unknown> { |
| 10 | const target = { |
| 11 | hasOwnProperty: (key: string) => store.has(key), |
| 12 | }; |
| 13 | |
| 14 | return new Proxy(target, { |
| 15 | get(_t, prop: string | symbol) { |
| 16 | if (prop === "hasOwnProperty") return target.hasOwnProperty; |
| 17 | if (typeof prop !== "string") return undefined; |
| 18 | return store.get(prop); |
| 19 | }, |
| 20 | set(_t, prop: string | symbol, value) { |
| 21 | if (typeof prop !== "string") return true; |
| 22 | store.set(prop, value); |
| 23 | return true; |
| 24 | }, |
| 25 | deleteProperty(_t, prop: string | symbol) { |
| 26 | if (typeof prop !== "string") return true; |
| 27 | store.delete(prop); |
| 28 | return true; |
| 29 | }, |
| 30 | has(_t, prop: string | symbol) { |
| 31 | return typeof prop === "string" && store.has(prop); |
| 32 | }, |
| 33 | ownKeys() { |
| 34 | return Array.from(store.keys()); |
| 35 | }, |
| 36 | getOwnPropertyDescriptor(_t, prop: string | symbol) { |
| 37 | if (prop === "hasOwnProperty") { |
| 38 | return { |
| 39 | // Must stay consistent with the actual target property, which |
| 40 | // is a normal (configurable + writable) own property of the |
| 41 | // object literal. Reporting configurable:false here violates |
| 42 | // the [[GetOwnProperty]] proxy invariant and makes |
| 43 | // Object.getOwnPropertyDescriptor(proxy, "hasOwnProperty") throw. |
| 44 | value: target.hasOwnProperty, |
| 45 | writable: true, |
| 46 | configurable: true, |
| 47 | enumerable: false, |
| 48 | }; |
| 49 | } |
| 50 | if (typeof prop !== "string" || !store.has(prop)) return undefined; |
| 51 | return { |
| 52 | value: store.get(prop), |
| 53 | writable: true, |
| 54 | configurable: true, |
| 55 | enumerable: true, |
| 56 | }; |
| 57 | }, |
| 58 | }); |
| 59 | } |
no outgoing calls
no test coverage detected