MIRROR: javascript for 馃悳's, a tiny runtime with big ambitions
1const assert = require("node:assert");
2
3let getCalls = 0;
4
5const proxy = new Proxy({}, {
6 ownKeys() {
7 return ["kimi-for-coding", "hidden", Symbol("skip")];
8 },
9 getOwnPropertyDescriptor(_target, prop) {
10 if (prop === "kimi-for-coding") {
11 return { enumerable: true, configurable: true };
12 }
13 if (prop === "hidden") {
14 return { enumerable: false, configurable: true };
15 }
16 if (typeof prop === "symbol") {
17 return { enumerable: true, configurable: true };
18 }
19 },
20 get(_target, prop) {
21 getCalls++;
22 if (prop === "kimi-for-coding") {
23 return { apiKeyEnvVar: "KIMI_API_KEY" };
24 }
25 if (prop === "hidden") {
26 return { apiKeyEnvVar: "HIDDEN_API_KEY" };
27 }
28 return { apiKeyEnvVar: "SYMBOL_API_KEY" };
29 }
30});
31
32assert.deepStrictEqual(Object.keys(proxy), ["kimi-for-coding"]);
33assert.deepStrictEqual(Object.values(proxy), [{ apiKeyEnvVar: "KIMI_API_KEY" }]);
34assert.deepStrictEqual(Object.entries(proxy), [["kimi-for-coding", { apiKeyEnvVar: "KIMI_API_KEY" }]]);
35assert.strictEqual(getCalls, 2);
36
37console.log("proxy Object.keys/Object.values/Object.entries respect proxy traps");