-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvm_runner.js
205 lines (170 loc) · 5.89 KB
/
vm_runner.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
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
'use strict';
const vm = require('vm');
const{ parentPort, workerData } = require("worker_threads")
// import { exit } from "process"
const { exit } = require("process")
const process = require("process")
// import { readFile, readFileSync } from "fs"
const fs = require("fs")
const { readFile } = fs
var path_module = require('path');
const { spawn } = require("child_process")
// const http = require('http')
// const wasmer_wasi = require('@wasmer/wasi');
// const wasmer_fs = require("@wasmer/wasmfs")
// const wasm_transformer = require("@wasmer/wasm-transformer")
// const wasm_binding = require("@wasmer/wasi/lib/bindings/node")
// const fetch = require("node-fetch")
const context = {
animal: 'cat',
count: 2,
// performance: { now: (z) => console.log(z) },
// setTimeout,TextDecoder, fetch: (f) => { console.log(f); return fetch}, process,
require: (mdl) => {
console.log(">><", mdl)
return require(mdl)
// switch (mdl) {
// case "@wasmer/wasmfs":
// return wasmer_fs
// case "@wasmer/wasi":
// return wasmer_wasi
// case "@wasmer/wasm-transformer":
// return wasm_transformer
// case "@wasmer/wasi/lib/bindings/node":
// return wasm_binding
// case "fsx":
// return fs
// default:
// return require(mdl)
// }
},
"module": module,
};
// const myVM = getScriptObject("./wasmer.generated.js")
function getScriptObject(scriptFilename) {
var sandbox = {
"module": module, "result": false,
"require": requireShim, "console": console
};
var script = vm.createScript(fs.readFileSync(scriptFilename));
script.runInNewContext(sandbox);
return sandbox.result;
}
function requireShim(x) {
console.log("requireShim: " + x);
return require(x);
}
// const code = `
// ((http) => {
// //const http = require('http');
// http.createServer((request, response) => {
// response.writeHead(200, { 'Content-Type': 'text/plain' });
// response.end('Hello World\\n');
// }).listen(1234);
// console.log('Server running at http://127.0.0.1:1234/');
// })`;
// vm.runInThisContext(code)(http);
// parentPort.postMessage(context)
readFile("/home/asanka/Documents/learn/webAssemblyPalygroud/wacms/dist/wasmer.generated.js", 'utf8', (err, code) => {
if (err) {
console.log(err)
exit()
throw Error("file read error")
}
if (code) {
// const script = new vm.Script(code);
// vm.createContext(context);
// script.runInContext(context);
// console.log("xcv", context)
vm.createContext(context)
vm.runInContext(code, context)
console.log("context", context)
}
})
const readCommand = (callback) => {
const ls = spawn("echo", ["console.log('hi')"])
ls.stdout.on("data", data => {
console.log({ data })
callback(data.toString() + JSON.stringify(context))
})
ls.on("close", code => {
console.log(`child process exited with code ${code}`)
callback(code)
})
}
const filePath = "/home/asanka/Documents/learn/webAssemblyPalygroud/wacms/dist/wasmer.generated.js"
console.log(" parentPort", parentPort)
// parentPort.postMessage({ msg: "worker started" })
function LoadModules(path) {
fs.lstat(path, function(err, stat) {
if (stat?.isDirectory()) {
// we have a directory: do a tree walk
fs.readdir(path, function(err, files) {
var f, l = files.length;
for (var i = 0; i < l; i++) {
f = path_module.join(path, files[i]);
// console.log(files[i], f)
LoadModules(f);
}
});
} else {
// we have a file: load it
// console.log({ path })
if (path === "/home/asanka/Documents/learn/webAssemblyPalygroud/wacms/dist/wasmer.generated.js") {
// require(path)(module_holder);
}
}
});
}
const DIR = path_module.join(__dirname);
// LoadModules(DIR);
// parentPort.on("message", async (msg) => {
// if (msg === "terminate") {
// exit(1)
// }
// if (msg === "next") {
// parentPort.postMessage({ msg: {}})
// }
// if (msg === "fire") {
// console.log(" worker got fire")
// parentPort.postMessage(getScriptObject(__dirname + "/wasmer.generated.js"))
// filePath()
// readCommand(x => parentPort.postMessage(x))
// await readFile(filePath, 'utf8', (err, data) => {
// if (err) {
// console.log(err)
// exit()
// throw Error("file read error")
// }
// if (data) {
// const script = new vm.Script('1222 ');
// vm.createContext(context);
// script.runInContext(context);
// console.log({ context })
// parentPort.postMessage(context)
// }
// // console.log("worker data", data.toString())
// // data.then(strm => {
// // parentPort.postMessage({ msg: data })
// // }).catch(e => {
// // throw Error("file read error")
// // })
// // console.log("data",data)
// })
// }
// })
// dynamic require with VM :https://nodejs.org/docs/latest-v15.x/api/vm.html#vm_script_runincontext_contextifiedobject_options
const code = `
((require) => {
const http = require('http');
http.createServer((request, response) => {
response.writeHead(200, { 'Content-Type': 'text/plain' });
response.write("hi");
response.write("hi2");
response.write("hi8");
response.write("hi3");
setTimeout(() =>response.end('Hello World\\n'), 3000)
}).listen(8124);
console.log('Server running at http://127.0.0.1:8124/');
})`;
// vm.runInThisContext(code)(require);