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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
const nodejsBuiltinModules = [
"assert",
"async_hooks",
"buffer",
"child_process",
"cluster",
"console",
"constants",
"crypto",
"dgram",
"diagnostics_channel",
"dns",
"domain",
"events",
"fs",
"http",
"http2",
"https",
"inspector",
"module",
"net",
"os",
"path",
"perf_hooks",
"process",
"punycode",
"querystring",
"readline",
"repl",
"stream",
"string_decoder",
"timers",
"tls",
"trace_events",
"tty",
"url",
"util",
"v8",
"vm",
"wasi",
"worker_threads",
"zlib",
];
function getModuleKeys(moduleName: string): string[] {
const script = `import('${moduleName}').then(mod=>console.log(JSON.stringify(Object.keys(mod))))`;
const nodeProc = Bun.spawnSync(["node", "-e", script], {
stderr: "ignore",
});
const nodeKeys: string[] = JSON.parse(nodeProc.stdout.toString());
return nodeKeys;
}
function getAllProperties(obj = {}) {
const allKeys = new Set();
do {
Reflect.ownKeys(obj).forEach(key => allKeys.add(key));
} while ((obj = Object.getPrototypeOf(obj)));
return [...allKeys];
}
function getPrototypeKeys(moduleName: string, className: string): string[] {
// const script = `import('${moduleName}').then(mod=>console.log(JSON.stringify(Object.keys(mod.${className}.prototype))))`;
const script = `
import("${moduleName}").then((mod) => {
const lines = new Set();
let obj = mod.${className}.prototype;
do {
Reflect.ownKeys(obj).forEach((key) => lines.add(key));
} while ((obj = Object.getPrototypeOf(obj)));
console.log("[", [...lines].map(k => \`"\${String(k)}"\`).join(","), "]");
});`.replace(/\n/g, "");
// remove whitespace
// .replace(/\s+/g, "");
// console.log(script);
const nodeProc = Bun.spawnSync(["node", "-e", script], {
// stderr: "inherit",
// stdout: "inherit",
});
// console.log(nodeProc.stdout.toString());
const nodeKeys: string[] = JSON.parse(nodeProc.stdout.toString());
return nodeKeys;
}
const SKIP: Record<string, boolean> = {
"buffer.File": true,
"process.abort": true,
"process.exit": true,
"process.kill": true,
"process.reallyExit": true,
"vm.Script": true,
"zlib.deflate": true,
"zlib.inflate": true,
"zlib.unzip": true,
"zlib.deflateRaw": true,
"zlib.gunzip": true,
"zlib.gzip": true,
"zlib.inflateRaw": true,
"console.assert": true,
"console.count": true,
// "fs.mkdtempSync": true,
// "fs.read": true,
// "fs.readv": true,
// "fs.writev": true,
// "fs.writeSync": true,
// "fs.writeFileSync": true,
// "fs.writeFile": true,
// "fs.write": true,
// "fs.writevSync": true,
// "fs.watchFile": true,
// "fs.watch": true,
// "fs.utimesSync": true,
// "fs.utimes": true,
// "fs.unwatchFile": true,
// "fs.unlinkSync": true,
// "fs.unlink": true,
// "fs.truncateSync": true,
// "fs.truncate": true,
// "fs.symlinkSync": true,
};
for (const moduleName of nodejsBuiltinModules) {
const heading = `======== ${moduleName} ========`;
// print equals sign to match the length of heading
console.log("\n\n" + "=".repeat(heading.length));
console.log(heading);
console.log("=".repeat(heading.length));
const mod = await import(moduleName);
const bunKeys: string[] = Object.keys(mod);
const nodeKeys = getModuleKeys(moduleName);
// print top-level elements that are missing
// const missingKeys = nodeKeys
// .filter((key) => !bunKeys.includes(key))
// .filter((k) => !k.startsWith("_"));
// const notMissing = nodeKeys.filter((key) => bunKeys.includes(key));
// if (missingKeys.length === 0) {
// console.log(`Fully implemented.`);
// } else {
// console.log(`Missing ${missingKeys.map((k) => `\`${k}\``).join(" ")}`);
// }
console.log();
// check for prototype compatibility
let missing = false;
for (const k of nodeKeys) {
if (k.startsWith("_")) continue;
if (!bunKeys.includes(k)) {
missing = true;
console.log(` [${moduleName}.${k}] Not implemented.`);
continue;
}
if (mod[k] && typeof mod[k] === "function") {
if (!!mod[k].prototype) {
const className = `${moduleName}.${k}`;
const bunProtoKeys = getAllProperties(mod[k].prototype);
// console.log(mod[k].prototype);
// getAllProperties;
// for (const l in mod[k].prototype) {
// bunProtoKeys.push(l);
// }
const nodeProtoKeys = getPrototypeKeys(moduleName, k);
// console.log("nodeProtoKeys", nodeProtoKeys);
// console.log("bunProtoKeys", bunProtoKeys);
// console.log("nodeProtoKeys", nodeProtoKeys);
// console.log("bunProtoKeys", bunProtoKeys);
const missingProtoKeys = nodeProtoKeys.filter(key => !bunProtoKeys.includes(key));
// const notMissingProtoKeys = nodeProtoKeys.filter((key) =>
// bunProtoKeys.includes(key)
// );
if (missingProtoKeys.length === 0) {
console.log(` [${className}] Fully implemented.`);
} else {
missing = true;
console.log(
` [${className}] Missing ${missingProtoKeys
.filter(k => !k.startsWith("_"))
.map(k => `\`${k}\``)
.join(" ")}`,
);
}
} else {
if (moduleName === "console") continue;
if (moduleName === "fs") continue;
if (SKIP[`${moduleName}.${k}`]) continue;
try {
// console.log(`trying ${moduleName}.${k}...`);
await mod[k]();
await Bun.sleep(1);
} catch (err: any) {
if ((err?.message as string).includes("not yet implemented")) {
missing = true;
console.log(` [${moduleName}.${k}] Not implemented.`);
}
}
}
}
}
if (!missing) {
console.log(`[${moduleName}] Fully implemented.`);
}
}
console.log("\n\n================\nDONE.");
process.exit();
|