-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnode_repl.js
85 lines (71 loc) · 2.49 KB
/
node_repl.js
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
// This REPL evaluates JavaScript in a Node.js process.
/*jslint node */
import child_process from "node:child_process";
import console from "node:console";
import process from "node:process";
import url from "node:url";
import make_cmdl from "./cmdl.js";
import make_cmdl_repl from "./cmdl_repl.js";
import fileify from "./fileify.js";
const loader_url = new URL("./node_loader.js", import.meta.url);
const padawan_url = new URL("./node_padawan.js", import.meta.url);
function spawn_node_padawan(tcp_port, which, args = [], env = {}) {
// Make sure we have "file:" URLs for the loader and padawan scripts. By
// default, Node.js is not capable of importing modules over HTTP. We specify a
// file extension to force Node.js to interpret the source as a module.
return Promise.all([
fileify(loader_url, ".mjs"),
fileify(padawan_url, ".mjs")
]).then(function ([
loader_file_url,
padawan_file_url
]) {
return child_process.spawn(
which,
args.concat(
// Imbue the padawan process with the ability to import modules over HTTP. The
// loader specifier must be a fully qualified URL on Windows.
"--experimental-loader",
loader_file_url.href,
// Suppress the "experimental feature" warnings. We know we are experimenting!
"--no-warnings",
// The program entry point must be specified as a path.
url.fileURLToPath(padawan_file_url),
String(tcp_port)
),
{env}
);
});
}
function make_node_repl(capabilities, which, args, env) {
return make_cmdl_repl(capabilities, function spawn_padawan(tcp_port) {
return spawn_node_padawan(tcp_port, which, args, env);
});
}
if (import.meta.main) {
const cmdl = make_cmdl(
function spawn_padawan(tcp_port) {
return spawn_node_padawan(tcp_port, process.argv[0]);
},
function on_stdout(chunk) {
return process.stdout.write(chunk);
},
function on_stderr(chunk) {
return process.stderr.write(chunk);
}
);
cmdl.create().then(function () {
return cmdl.eval(
// `
// (function isStrictMode() {
// return this === undefined;
// }());
// `,
"$imports[0].default.tmpdir();",
["node:os"]
).then(
console.log
);
}).then(cmdl.destroy);
}
export default Object.freeze(make_node_repl);