@remix_labs/machine-starter 2.13024.0-dev → 2.13026.0
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/common.js +163 -122
- package/groovebox_build.js +1 -1
- package/package.json +2 -2
package/common.js
CHANGED
|
@@ -12,8 +12,25 @@ let children = {};
|
|
|
12
12
|
let ffiLoops = {};
|
|
13
13
|
let blobs = {};
|
|
14
14
|
let blobsLoops = {};
|
|
15
|
+
let coordination = {};
|
|
15
16
|
|
|
16
|
-
|
|
17
|
+
class TaskQueue {
|
|
18
|
+
constructor() {
|
|
19
|
+
this.tail = Promise.resolve();
|
|
20
|
+
this.length = 0;
|
|
21
|
+
}
|
|
22
|
+
run(fn) { // fn shall return a promise. run also returns a promise
|
|
23
|
+
this.length++;
|
|
24
|
+
const result = this.tail.then(() => fn()).finally(() => { this.length--; });
|
|
25
|
+
this.tail = result.catch(() => {});
|
|
26
|
+
return result;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function terminateAllOnExit() {
|
|
31
|
+
// this is a simplified termination function that kills all VMs, but
|
|
32
|
+
// doesn't clean everything properly up. Use this only for the "on-exit"
|
|
33
|
+
// callback of node.
|
|
17
34
|
console.log("[machine-starter] terminate all VMs");
|
|
18
35
|
for (let [vmID, worker] of Object.entries(workers)) {
|
|
19
36
|
worker.terminate();
|
|
@@ -31,59 +48,79 @@ function terminateAll() {
|
|
|
31
48
|
blobsLoop.terminate();
|
|
32
49
|
}
|
|
33
50
|
workers = {};
|
|
51
|
+
worker_topics = {};
|
|
34
52
|
children = {};
|
|
35
53
|
ffiLoops = {};
|
|
36
54
|
blobs = {};
|
|
37
55
|
blobsLoops = {};
|
|
56
|
+
coordination = {};
|
|
38
57
|
}
|
|
39
58
|
|
|
40
|
-
function
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
let blobList = blobs[vmID] || [];
|
|
46
|
-
let ffiLoop = ffiLoops[vmID];
|
|
47
|
-
let blobsLoop = blobsLoops[vmID];
|
|
48
|
-
delete children[vmID];
|
|
49
|
-
delete ffiLoops[vmID];
|
|
50
|
-
delete blobsLoops[vmID];
|
|
51
|
-
killVM(vmID);
|
|
52
|
-
ffiLoop.terminate();
|
|
53
|
-
if (blobsLoop) {
|
|
54
|
-
blobsLoop.terminate();
|
|
55
|
-
}
|
|
56
|
-
for (let blob_url of blobList) {
|
|
57
|
-
console.log("[machine-starter] revoke ", blob_url);
|
|
58
|
-
URL.revokeObjectURL(blob_url);
|
|
59
|
-
};
|
|
60
|
-
if (vmChildren) {
|
|
61
|
-
for (let subID of vmChildren) {
|
|
62
|
-
terminateVM(subID)
|
|
63
|
-
}
|
|
64
|
-
}
|
|
59
|
+
function getCoord(vmID) {
|
|
60
|
+
let coord = coordination[vmID];
|
|
61
|
+
if (!coord) {
|
|
62
|
+
coord = { queue: new TaskQueue() };
|
|
63
|
+
coordination[vmID] = coord;
|
|
65
64
|
}
|
|
65
|
+
return coord;
|
|
66
66
|
}
|
|
67
67
|
|
|
68
|
-
function
|
|
69
|
-
let
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
68
|
+
async function terminateVM(vmID) {
|
|
69
|
+
let coord = getCoord(vmID);
|
|
70
|
+
try {
|
|
71
|
+
await coord.queue.run(async () => {
|
|
72
|
+
if (!workers[vmID]) return;
|
|
73
|
+
console.log("[machine-starter] terminate VM:", vmID);
|
|
74
|
+
// make the VM first unknown to the outside and disconnect it,
|
|
75
|
+
// then clean up resources (including stopping the VM)
|
|
76
|
+
let channel = await last_hub.newChannel();
|
|
77
|
+
let topic = worker_topics[vmID];
|
|
78
|
+
let payload = { _rmx_type:"msg_vm_register", id: vmID, alive: false };
|
|
76
79
|
let msg = channel.newJSONMessage("msg_vm_register", "vm", payload);
|
|
77
|
-
channel.publish(topic + "/announce", msg, true)
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
80
|
+
await channel.publish(topic + "/announce", msg, true);
|
|
81
|
+
// NB. This msg is a leak - it will never be removed.
|
|
82
|
+
await channel.disconnect("vm_" + vmID);
|
|
83
|
+
// now the VM is isolated and cannot communicate with the
|
|
84
|
+
// hub anymore
|
|
85
|
+
let worker = workers[vmID];
|
|
86
|
+
let vmChildren = children[vmID] || [];
|
|
87
|
+
let blobList = blobs[vmID] || [];
|
|
88
|
+
let ffiLoop = ffiLoops[vmID];
|
|
89
|
+
let blobsLoop = blobsLoops[vmID];
|
|
90
|
+
delete children[vmID];
|
|
91
|
+
delete ffiLoops[vmID];
|
|
92
|
+
delete blobsLoops[vmID];
|
|
93
|
+
delete worker_topics[vmID];
|
|
94
|
+
delete workers[vmID];
|
|
95
|
+
// terminate our endpoints of communication
|
|
96
|
+
if (ffiLoop) {
|
|
97
|
+
ffiLoop.terminate();
|
|
98
|
+
}
|
|
99
|
+
if (blobsLoop) {
|
|
100
|
+
blobsLoop.terminate();
|
|
101
|
+
}
|
|
102
|
+
for (let blob_url of blobList) {
|
|
103
|
+
console.log("[machine-starter] revoke ", blob_url);
|
|
104
|
+
URL.revokeObjectURL(blob_url);
|
|
105
|
+
}
|
|
106
|
+
// kill the worker:
|
|
107
|
+
console.log("[machine-starter] terminate web worker for VM:", vmID);
|
|
108
|
+
worker.terminate();
|
|
109
|
+
// go on with sub VMs
|
|
110
|
+
for (let subID of vmChildren) {
|
|
111
|
+
terminateVM(subID) // do not wait
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
} finally {
|
|
115
|
+
if (coord.queue.length == 0 && coordination[vmID] === coord) {
|
|
116
|
+
delete coordination[vmID];
|
|
117
|
+
}
|
|
81
118
|
}
|
|
82
119
|
}
|
|
83
120
|
|
|
84
121
|
if (globalThis.Process) {
|
|
85
122
|
Process.on("exit", () => {
|
|
86
|
-
|
|
123
|
+
terminateAllOnExit()
|
|
87
124
|
})
|
|
88
125
|
};
|
|
89
126
|
|
|
@@ -133,84 +170,88 @@ async function StartWASM2(hub, config, getWorker) {
|
|
|
133
170
|
console.log("[machine-starter] Call MixExplain() for explaining debug levels, and MixSetMask(m) for changing these")
|
|
134
171
|
};
|
|
135
172
|
last_hub = hub;
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
let starterFFIs = config.localFFIs || {};
|
|
148
|
-
// from now on, the "local" FFIs are called "starter" FFIs, because this
|
|
149
|
-
// distinguishes these from other locally defined FFIs
|
|
150
|
-
let mixcore = await createStarterMixcore(config.mixcore);
|
|
151
|
-
if (mixcore) {
|
|
152
|
-
// merge the ffis and store them back into the config so sub vms also get them
|
|
153
|
-
config.localFFIs = Object.assign(starterFFIs, await mixcoreStarterFFIs(mixcore));
|
|
154
|
-
// delete the mixcore config to avoid being handled further by the worker
|
|
155
|
-
delete config.mixcore;
|
|
156
|
-
}
|
|
157
|
-
let starterFFIkind = {};
|
|
158
|
-
let starterFFIfuns = {};
|
|
159
|
-
for (const [k, v] of Object.entries(starterFFIs)) {
|
|
160
|
-
starterFFIfuns[k] = {...v};
|
|
161
|
-
if (v.isRaw && (v.canFail || v.useJsonDecoder)) {
|
|
162
|
-
console.error("A raw, starter FFI must neither be failing nor use the JSON decoder: " + k);
|
|
173
|
+
// get the coord object - perhaps the ID was already used for a VM
|
|
174
|
+
let coord = getCoord(config.vmID);
|
|
175
|
+
return await coord.queue.run(async () => {
|
|
176
|
+
if (workers[config.vmID])
|
|
177
|
+
throw new Error("[machine-starter] Cannot start VM, the ID is used: " + config.vmID);
|
|
178
|
+
let channel = await hub.newChannel("vm_" + config.vmID);
|
|
179
|
+
// injected by either index.js or node.js
|
|
180
|
+
let worker = await getWorker();
|
|
181
|
+
workers[config.vmID] = worker;
|
|
182
|
+
if (config.topicPrefix != undefined && config.topicPrefix != "") {
|
|
183
|
+
worker_topics[config.vmID] = config.topicPrefix + "/vm/" + config.vmID;
|
|
163
184
|
} else {
|
|
164
|
-
|
|
165
|
-
delete starterFFIkind[k].run;
|
|
185
|
+
worker_topics[config.vmID] = "/org/" + config.org + "/ws/" + config.workspace + "/vm/" + config.vmID;
|
|
166
186
|
}
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
// startup times
|
|
177
|
-
if (globalThis.mixrtCodeModule && globalThis.mixrtCodeURL == mixrtCode) {
|
|
178
|
-
console.log("[machine-starter] reusing mixrt");
|
|
179
|
-
mixrtCode = globalThis.mixrtCodeModule;
|
|
180
|
-
} else {
|
|
181
|
-
let url = mixrtCode;
|
|
182
|
-
console.log("[machine-starter] pre-compiling mixrt");
|
|
183
|
-
mixrtCode = await WebAssembly.compileStreaming(fetch(url));
|
|
184
|
-
globalThis.mixrtCodeURL = url;
|
|
185
|
-
globalThis.mixrtCodeModule = mixrtCode;
|
|
187
|
+
let starterFFIs = config.localFFIs || {};
|
|
188
|
+
// from now on, the "local" FFIs are called "starter" FFIs, because this
|
|
189
|
+
// distinguishes these from other locally defined FFIs
|
|
190
|
+
let mixcore = await createStarterMixcore(config.mixcore);
|
|
191
|
+
if (mixcore) {
|
|
192
|
+
// merge the ffis and store them back into the config so sub vms also get them
|
|
193
|
+
config.localFFIs = Object.assign(starterFFIs, await mixcoreStarterFFIs(mixcore));
|
|
194
|
+
// delete the mixcore config to avoid being handled further by the worker
|
|
195
|
+
delete config.mixcore;
|
|
186
196
|
}
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
"user": config.user,
|
|
198
|
-
"token": config.token,
|
|
199
|
-
"hub": channel.port,
|
|
200
|
-
"outputViaMQTT": !(config.noOutputViaMQTT),
|
|
201
|
-
"machType": config.machType,
|
|
202
|
-
"debugMask": initialMask,
|
|
203
|
-
"starterFFIs": starterFFIkind,
|
|
204
|
-
"allowInsecureHttp": globalThis.GROOVEBOX_ALLOW_INSECURE_HTTP,
|
|
205
|
-
"interceptFFI": config.interceptFFI,
|
|
206
|
-
"interceptDynloadFFI": config.interceptDynloadFFI,
|
|
207
|
-
"mixcore": config.mixcore,
|
|
208
|
-
"mixrtCode": mixrtCode
|
|
209
|
-
// can be: base64-encoded string, Uint8Array, or a URL as string
|
|
210
|
-
// (http, https, data)
|
|
197
|
+
let starterFFIkind = {};
|
|
198
|
+
let starterFFIfuns = {};
|
|
199
|
+
for (const [k, v] of Object.entries(starterFFIs)) {
|
|
200
|
+
starterFFIfuns[k] = {...v};
|
|
201
|
+
if (v.isRaw && (v.canFail || v.useJsonDecoder)) {
|
|
202
|
+
console.error("A raw, starter FFI must neither be failing nor use the JSON decoder: " + k);
|
|
203
|
+
} else {
|
|
204
|
+
starterFFIkind[k] = {...v};
|
|
205
|
+
delete starterFFIkind[k].run;
|
|
206
|
+
}
|
|
211
207
|
};
|
|
212
|
-
|
|
213
|
-
|
|
208
|
+
let vm_kind = { isRaw:false, canFail:false, useJsonDecoder:false};
|
|
209
|
+
starterFFIkind["$vm_start"] = vm_kind;
|
|
210
|
+
starterFFIfuns["$vm_start"] = (conn, args) => ffi_vm_start(conn, args, getWorker);
|
|
211
|
+
starterFFIkind["$vm_stop"] = vm_kind;
|
|
212
|
+
starterFFIfuns["$vm_stop"] = ffi_vm_stop;
|
|
213
|
+
let mixrtCode = config.mixrtCode;
|
|
214
|
+
if (typeof(mixrtCode) == "string" && mixrtCode.startsWith("remix://")) {
|
|
215
|
+
// for the desktop only: precompile the mixrt module to get faster
|
|
216
|
+
// startup times
|
|
217
|
+
if (globalThis.mixrtCodeModule && globalThis.mixrtCodeURL == mixrtCode) {
|
|
218
|
+
console.log("[machine-starter] reusing mixrt");
|
|
219
|
+
mixrtCode = globalThis.mixrtCodeModule;
|
|
220
|
+
} else {
|
|
221
|
+
let url = mixrtCode;
|
|
222
|
+
console.log("[machine-starter] pre-compiling mixrt");
|
|
223
|
+
mixrtCode = await WebAssembly.compileStreaming(fetch(url));
|
|
224
|
+
globalThis.mixrtCodeURL = url;
|
|
225
|
+
globalThis.mixrtCodeModule = mixrtCode;
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
ffiLoops[config.vmID] = setupStarterFFIs(hub, config, starterFFIfuns);
|
|
229
|
+
blobsLoops[config.vmID] = setupBlobs(hub, config);
|
|
230
|
+
let config_msg =
|
|
231
|
+
{ "_rmx_type": "msg_vm_configure",
|
|
232
|
+
"baseURL": config.baseURL,
|
|
233
|
+
"org": config.org,
|
|
234
|
+
"workspace": config.workspace,
|
|
235
|
+
"topicPrefix": config.topicPrefix,
|
|
236
|
+
"vmID": config.vmID,
|
|
237
|
+
"user": config.user,
|
|
238
|
+
"token": config.token,
|
|
239
|
+
"hub": channel.port,
|
|
240
|
+
"outputViaMQTT": !(config.noOutputViaMQTT),
|
|
241
|
+
"machType": config.machType,
|
|
242
|
+
"debugMask": initialMask,
|
|
243
|
+
"starterFFIs": starterFFIkind,
|
|
244
|
+
"allowInsecureHttp": globalThis.GROOVEBOX_ALLOW_INSECURE_HTTP,
|
|
245
|
+
"interceptFFI": config.interceptFFI,
|
|
246
|
+
"interceptDynloadFFI": config.interceptDynloadFFI,
|
|
247
|
+
"mixcore": config.mixcore,
|
|
248
|
+
"mixrtCode": mixrtCode
|
|
249
|
+
// can be: base64-encoded string, Uint8Array, or a URL as string
|
|
250
|
+
// (http, https, data)
|
|
251
|
+
};
|
|
252
|
+
worker.postMessage(config_msg, [ channel.port ]);
|
|
253
|
+
return worker;
|
|
254
|
+
});
|
|
214
255
|
}
|
|
215
256
|
|
|
216
257
|
async function mixcoreStarterFFIs(mixcore) {
|
|
@@ -387,7 +428,7 @@ async function starterFFIsMessageLoop(hub, config, starterFFIs, terminate) {
|
|
|
387
428
|
sub.next().then((msg) => ({msg})),
|
|
388
429
|
terminate.then(() => ({terminated: true})),
|
|
389
430
|
]);
|
|
390
|
-
if (terminated) {
|
|
431
|
+
if (terminated || msg.eof) {
|
|
391
432
|
// console.log("[machine-starter] terminate FFI loop", vmID);
|
|
392
433
|
return;
|
|
393
434
|
}
|
|
@@ -396,7 +437,7 @@ async function starterFFIsMessageLoop(hub, config, starterFFIs, terminate) {
|
|
|
396
437
|
let args = decode(msg.payload.args);
|
|
397
438
|
let fun = starterFFIs[name];
|
|
398
439
|
if (initialMask & DEBUG_STATE)
|
|
399
|
-
console.debug("[
|
|
440
|
+
console.debug("[machine-starter] starter FFI call", vmID, call_id, name);
|
|
400
441
|
try {
|
|
401
442
|
if (!fun) throw new Error("No such starter FFI in groovebox starter: " + name);
|
|
402
443
|
let connector =
|
|
@@ -410,21 +451,21 @@ async function starterFFIsMessageLoop(hub, config, starterFFIs, terminate) {
|
|
|
410
451
|
if (r instanceof Promise) {
|
|
411
452
|
// r.catch(_ => null); // prevent "unhandled rejection"
|
|
412
453
|
if (initialMask & DEBUG_STATE)
|
|
413
|
-
console.debug("[
|
|
454
|
+
console.debug("[machine-starter] running FFI call asynchronously", vmID, call_id, name);
|
|
414
455
|
await comm.later(call_id);
|
|
415
456
|
let value = await r;
|
|
416
457
|
if (initialMask & DEBUG_STATE)
|
|
417
|
-
console.debug("[
|
|
458
|
+
console.debug("[machine-starter] async FFI call returned", vmID, call_id, name);
|
|
418
459
|
await comm.returnOrFail(fun, call_id, value);
|
|
419
460
|
} else {
|
|
420
461
|
if (initialMask & DEBUG_STATE)
|
|
421
|
-
console.debug("[
|
|
462
|
+
console.debug("[machine-starter] sync FFI call returned", vmID, call_id, name);
|
|
422
463
|
await comm.returnOrFail(fun, call_id, r);
|
|
423
464
|
}
|
|
424
465
|
} catch (reason) {
|
|
425
466
|
if (initialMask & DEBUG_STATE) {
|
|
426
|
-
console.debug("[
|
|
427
|
-
console.debug("[
|
|
467
|
+
console.debug("[machine-starter] FFI call exception", vmID, call_id, name);
|
|
468
|
+
console.debug("[machine-starter] exception", reason);
|
|
428
469
|
};
|
|
429
470
|
await comm.error(call_id, reason.message, reason.stack);
|
|
430
471
|
}
|
|
@@ -447,7 +488,7 @@ async function blobsMessageLoop(hub, config, terminate) {
|
|
|
447
488
|
sub.next().then((msg) => ({msg})),
|
|
448
489
|
terminate.then(() => ({terminated: true})),
|
|
449
490
|
]);
|
|
450
|
-
if (terminated) {
|
|
491
|
+
if (terminated || msg.eof) {
|
|
451
492
|
// console.log("[machine-starter] terminate blobs loop", vmID);
|
|
452
493
|
return;
|
|
453
494
|
}
|
|
@@ -517,6 +558,6 @@ class FFIComm {
|
|
|
517
558
|
}
|
|
518
559
|
|
|
519
560
|
// use terminateVM to shut a worker down!
|
|
520
|
-
export { StartWASM, StartWASM2,
|
|
561
|
+
export { StartWASM, StartWASM2, terminateVM,
|
|
521
562
|
Token, Case, Opaque
|
|
522
563
|
}
|
package/groovebox_build.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var GROOVEBOX_BUILD = "
|
|
1
|
+
var GROOVEBOX_BUILD = "13026";
|
|
2
2
|
export { GROOVEBOX_BUILD }
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@remix_labs/machine-starter",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.13026.0",
|
|
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": "2.
|
|
14
|
+
"@remix_labs/hub-client": "2.13026.0",
|
|
15
15
|
"nanoid": "^5.0.2",
|
|
16
16
|
"web-worker": "^1.2.0"
|
|
17
17
|
},
|