@remix_labs/machine-starter 1.1494.0-dev → 1.1580.0-dev
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +32 -94
- package/package.json +2 -2
- package/start.js +1 -2
package/index.js
CHANGED
|
@@ -1,34 +1,14 @@
|
|
|
1
1
|
import { nanoid } from "nanoid";
|
|
2
2
|
import { GROOVEBOX_BUILD } from "./groovebox_build.js";
|
|
3
3
|
|
|
4
|
-
let
|
|
5
|
-
let parents = {};
|
|
6
|
-
|
|
7
|
-
function terminateAll() {
|
|
8
|
-
for (let [vmID, worker] of Object.entries(workers)) {
|
|
9
|
-
worker.terminate();
|
|
10
|
-
};
|
|
11
|
-
workers = {};
|
|
12
|
-
parents = {};
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
function terminateVM(vmID) {
|
|
16
|
-
let worker = workers[vmID];
|
|
17
|
-
if (worker) {
|
|
18
|
-
delete workers[vmID];
|
|
19
|
-
delete parents[vmID];
|
|
20
|
-
worker.terminate();
|
|
21
|
-
for (let [subID, parentID] of Object.entries(parents)) {
|
|
22
|
-
terminateVM(subID);
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
4
|
+
let terminate = function(f) {};
|
|
27
5
|
if (globalThis.ThisIsNode) {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
6
|
+
terminate = function(f) {
|
|
7
|
+
Process.on("exit", () => {
|
|
8
|
+
console.log("terminating machine worker");
|
|
9
|
+
f()
|
|
10
|
+
})
|
|
11
|
+
}
|
|
32
12
|
};
|
|
33
13
|
|
|
34
14
|
function CreateVMID() {
|
|
@@ -36,14 +16,8 @@ function CreateVMID() {
|
|
|
36
16
|
}
|
|
37
17
|
|
|
38
18
|
let initialMask = 64 + 1024; // default for web
|
|
39
|
-
|
|
40
19
|
globalThis.MixSetMask = (m => {
|
|
41
20
|
initialMask = m;
|
|
42
|
-
for (let [vmID, worker] of Object.entries(workers)) {
|
|
43
|
-
worker.postMessage({ "_rmx_type": "msg_vm_logMask",
|
|
44
|
-
"mask": m
|
|
45
|
-
})
|
|
46
|
-
}
|
|
47
21
|
});
|
|
48
22
|
|
|
49
23
|
function StartWASM(hub, baseURL, org, workspace, vmID, user, token, noOutputViaMQTT) {
|
|
@@ -77,29 +51,20 @@ function StartWASM2(hub, config) {
|
|
|
77
51
|
fetch(code_url, {cache:"default"}).then(resp => { return resp.arrayBuffer() });
|
|
78
52
|
bundle = new URL("/g/machine-wasm-core.js", window.location.href);
|
|
79
53
|
}
|
|
80
|
-
let
|
|
81
|
-
let localFFIfuns = {};
|
|
54
|
+
let localFFIs = {};
|
|
82
55
|
if (config.localFFIs) {
|
|
83
56
|
for (const [k, v] of Object.entries(config.localFFIs)) {
|
|
84
|
-
localFFIfuns[k] = {...v};
|
|
85
57
|
if (v.isRaw && (v.canFail || v.useJsonDecoder)) {
|
|
86
58
|
console.error("A raw, local FFI must neither be failing nor use the JSON decoder: " + k);
|
|
87
59
|
} else {
|
|
88
|
-
|
|
89
|
-
delete
|
|
60
|
+
localFFIs[k] = {...v};
|
|
61
|
+
delete localFFIs[k].run;
|
|
90
62
|
}
|
|
91
|
-
}
|
|
92
|
-
let vm_kind = { isRaw:false, canFail:false, useJsonDecoder:false};
|
|
93
|
-
localFFIkind["$vm_start"] = vm_kind;
|
|
94
|
-
localFFIfuns["$vm_start"] = ffi_vm_start;
|
|
95
|
-
localFFIkind["$vm_stop"] = vm_kind;
|
|
96
|
-
localFFIfuns["$vm_stop"] = ffi_vm_stop;
|
|
63
|
+
}
|
|
97
64
|
}
|
|
98
65
|
let worker = new Worker(bundle);
|
|
99
|
-
workers[config.vmID] = worker;
|
|
100
66
|
let config_msg =
|
|
101
67
|
{ "_rmx_type": "msg_vm_configure",
|
|
102
|
-
"workerURL": bundle.toString(),
|
|
103
68
|
"baseURL": config.baseURL,
|
|
104
69
|
"org": config.org,
|
|
105
70
|
"workspace": config.workspace,
|
|
@@ -110,47 +75,23 @@ function StartWASM2(hub, config) {
|
|
|
110
75
|
"outputViaMQTT": !(config.noOutputViaMQTT),
|
|
111
76
|
"machType": config.machType,
|
|
112
77
|
"debugMask": initialMask,
|
|
113
|
-
"localFFIs":
|
|
78
|
+
"localFFIs": localFFIs,
|
|
114
79
|
"grooveboxUrlPrefix": globalThis.GROOVEBOX_URL_PREFIX,
|
|
115
80
|
"allowInsecureHttp": globalThis.GROOVEBOX_ALLOW_INSECURE_HTTP,
|
|
116
|
-
"jsonFFI": config.jsonFFI,
|
|
117
|
-
"interceptFFI": config.interceptFFI,
|
|
118
|
-
"interceptDynloadFFI": config.interceptDynloadFFI,
|
|
119
81
|
};
|
|
120
82
|
worker.postMessage(config_msg, [ channel.port ]);
|
|
83
|
+
terminate(() => worker.terminate());
|
|
84
|
+
globalThis.MixSetMask = (m => {
|
|
85
|
+
worker.postMessage({ "_rmx_type": "msg_vm_logMask",
|
|
86
|
+
"mask": m
|
|
87
|
+
})
|
|
88
|
+
});
|
|
121
89
|
if (config.localFFIs)
|
|
122
|
-
setupLocalFFIs(hub, config
|
|
90
|
+
setupLocalFFIs(hub, config); // don't await!
|
|
123
91
|
return worker;
|
|
124
92
|
})
|
|
125
93
|
}
|
|
126
94
|
|
|
127
|
-
async function ffi_vm_start(conn, args) {
|
|
128
|
-
let vm_id = "embedded." + nanoid();
|
|
129
|
-
let config = {};
|
|
130
|
-
for (const [k, v] of Object.entries(conn.config)) {
|
|
131
|
-
config[k] = v;
|
|
132
|
-
}
|
|
133
|
-
config["vmID"] = vm_id;
|
|
134
|
-
parents[vm_id] = conn.config.vmID;
|
|
135
|
-
let arg_config = args[0];
|
|
136
|
-
if (arg_config.jsonFFI !== undefined)
|
|
137
|
-
config["jsonFFI"] = arg_config.jsonFFI;
|
|
138
|
-
if (arg_config.interceptFFI !== undefined)
|
|
139
|
-
config["interceptFFI"] = arg_config.interceptFFI;
|
|
140
|
-
if (arg_config.interceptDynloadFFI !== undefined)
|
|
141
|
-
config["interceptDynloadFFI"] = arg_config.interceptDynloadFFI;
|
|
142
|
-
let worker = await StartWASM2(conn.hub, config);
|
|
143
|
-
return vm_id;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
async function ffi_vm_stop(conn, args) {
|
|
147
|
-
let vm_id = args[0];
|
|
148
|
-
let worker = workers[vm_id];
|
|
149
|
-
if (!worker) throw new Error("no such VM: " + vm_id);
|
|
150
|
-
terminateVM(vm_id);
|
|
151
|
-
return null;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
95
|
class Token {
|
|
155
96
|
constructor(data, enabled, extras) {
|
|
156
97
|
// data: must be an Uint8Array
|
|
@@ -266,13 +207,13 @@ function decode(val) {
|
|
|
266
207
|
}
|
|
267
208
|
}
|
|
268
209
|
|
|
269
|
-
async function setupLocalFFIs(hub, config
|
|
270
|
-
let vmID = config.vmID;
|
|
210
|
+
async function setupLocalFFIs(hub, config) {
|
|
271
211
|
let channel = await hub.newChannel();
|
|
272
|
-
let comm = new FFIComm(
|
|
273
|
-
|
|
274
|
-
await channel.
|
|
275
|
-
|
|
212
|
+
let comm = new FFIComm(hub, channel);
|
|
213
|
+
let localFFIs = config.localFFIs;
|
|
214
|
+
await channel.setLocalSubTopic("/local/ffi/call");
|
|
215
|
+
await channel.setLocalPubTopic("/local/ffi/return");
|
|
216
|
+
let sub = await channel.subscribe("/local/ffi/call");
|
|
276
217
|
while (true) {
|
|
277
218
|
let msg = await sub.next();
|
|
278
219
|
let name = msg.payload.name;
|
|
@@ -286,15 +227,14 @@ async function setupLocalFFIs(hub, config, localFFIs) {
|
|
|
286
227
|
{ call_id: call_id,
|
|
287
228
|
hub: hub,
|
|
288
229
|
channel: channel,
|
|
289
|
-
config: config,
|
|
290
230
|
state: config.state,
|
|
291
231
|
};
|
|
292
232
|
let r = fun instanceof Function ? fun(connector, args) : fun.run(connector, args);
|
|
293
233
|
if (r instanceof Promise) {
|
|
294
|
-
r.catch(_ => null); // prevent "unhandled rejection"
|
|
295
234
|
await comm.later(call_id);
|
|
296
|
-
|
|
297
|
-
|
|
235
|
+
r.then(
|
|
236
|
+
(value) => comm.returnOrFail(fun, call_id, value),
|
|
237
|
+
(reason) => comm.error(call_id, reason.message, reason.stack));
|
|
298
238
|
} else {
|
|
299
239
|
await comm.returnOrFail(fun, call_id, r);
|
|
300
240
|
}
|
|
@@ -305,8 +245,7 @@ async function setupLocalFFIs(hub, config, localFFIs) {
|
|
|
305
245
|
}
|
|
306
246
|
|
|
307
247
|
class FFIComm {
|
|
308
|
-
constructor(
|
|
309
|
-
this.vmID = vmID;
|
|
248
|
+
constructor(hub, channel) {
|
|
310
249
|
this.hub = hub;
|
|
311
250
|
this.channel = channel;
|
|
312
251
|
}
|
|
@@ -315,7 +254,7 @@ class FFIComm {
|
|
|
315
254
|
{ call_id: call_id,
|
|
316
255
|
};
|
|
317
256
|
let response = this.hub.newLocalMessage("msg_ffi_later", "starter", r_payload);
|
|
318
|
-
return this.channel.publish("/local/
|
|
257
|
+
return this.channel.publish("/local/ffi/return", response, false);
|
|
319
258
|
}
|
|
320
259
|
return_(fun, call_id, result) {
|
|
321
260
|
let r_payload =
|
|
@@ -323,7 +262,7 @@ class FFIComm {
|
|
|
323
262
|
value: fun.useJsonDecoder ? result : encode(result)
|
|
324
263
|
};
|
|
325
264
|
let response = this.hub.newLocalMessage("msg_ffi_return", "starter", r_payload);
|
|
326
|
-
return this.channel.publish("/local/
|
|
265
|
+
return this.channel.publish("/local/ffi/return", response, false);
|
|
327
266
|
}
|
|
328
267
|
error(call_id, message, stack) {
|
|
329
268
|
let r_payload =
|
|
@@ -332,7 +271,7 @@ class FFIComm {
|
|
|
332
271
|
stack: stack === undefined ? [] : stack,
|
|
333
272
|
};
|
|
334
273
|
let response = this.hub.newLocalMessage("msg_ffi_error", "starter", r_payload);
|
|
335
|
-
this.channel.publish("/local/
|
|
274
|
+
this.channel.publish("/local/ffi/return", response, false);
|
|
336
275
|
}
|
|
337
276
|
returnOrFail(fun, call_id, result) {
|
|
338
277
|
if (fun.canFail) {
|
|
@@ -363,6 +302,5 @@ class FFIComm {
|
|
|
363
302
|
// use worker.terminate() to shut a worker down!
|
|
364
303
|
|
|
365
304
|
export { CreateVMID, StartWASM, StartWASM2,
|
|
366
|
-
terminateAll, terminateVM,
|
|
367
305
|
Token, Case, Opaque
|
|
368
306
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@remix_labs/machine-starter",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1580.0-dev",
|
|
4
4
|
"description": "start the groove",
|
|
5
5
|
"main": "node.js",
|
|
6
6
|
"browser": "index.js",
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"author": "Remixlabs staff",
|
|
12
12
|
"license": "ISC",
|
|
13
13
|
"dependencies": {
|
|
14
|
-
"@remix_labs/hub-client": "1.
|
|
14
|
+
"@remix_labs/hub-client": "1.1580.0-dev",
|
|
15
15
|
"nanoid": "^3.1.12",
|
|
16
16
|
"web-worker": "^1.2.0"
|
|
17
17
|
},
|
package/start.js
CHANGED