@remix_labs/machine-starter 2.13010.0-dev → 2.13014.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/common.js +122 -163
- package/groovebox_build.js +1 -1
- package/package.json +2 -2
package/common.js
CHANGED
|
@@ -12,25 +12,8 @@ let children = {};
|
|
|
12
12
|
let ffiLoops = {};
|
|
13
13
|
let blobs = {};
|
|
14
14
|
let blobsLoops = {};
|
|
15
|
-
let coordination = {};
|
|
16
15
|
|
|
17
|
-
|
|
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.
|
|
16
|
+
function terminateAll() {
|
|
34
17
|
console.log("[machine-starter] terminate all VMs");
|
|
35
18
|
for (let [vmID, worker] of Object.entries(workers)) {
|
|
36
19
|
worker.terminate();
|
|
@@ -48,79 +31,59 @@ function terminateAllOnExit() {
|
|
|
48
31
|
blobsLoop.terminate();
|
|
49
32
|
}
|
|
50
33
|
workers = {};
|
|
51
|
-
worker_topics = {};
|
|
52
34
|
children = {};
|
|
53
35
|
ffiLoops = {};
|
|
54
36
|
blobs = {};
|
|
55
37
|
blobsLoops = {};
|
|
56
|
-
coordination = {};
|
|
57
38
|
}
|
|
58
39
|
|
|
59
|
-
function
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
40
|
+
function terminateVM(vmID) {
|
|
41
|
+
console.log("[machine-starter] terminate VM", vmID);
|
|
42
|
+
let worker = workers[vmID];
|
|
43
|
+
if (worker) {
|
|
44
|
+
let vmChildren = children[vmID];
|
|
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
|
+
}
|
|
64
65
|
}
|
|
65
|
-
return coord;
|
|
66
66
|
}
|
|
67
67
|
|
|
68
|
-
|
|
69
|
-
let
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
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 };
|
|
68
|
+
function killVM(vmID) {
|
|
69
|
+
let worker = workers[vmID];
|
|
70
|
+
let topic = worker_topics[vmID];
|
|
71
|
+
delete workers[vmID];
|
|
72
|
+
delete worker_topics[vmID];
|
|
73
|
+
if (topic && last_hub) {
|
|
74
|
+
let payload = { _rmx_type:"msg_vm_register", id: vmID, alive: false };
|
|
75
|
+
last_hub.newChannel().then(channel => {
|
|
79
76
|
let msg = channel.newJSONMessage("msg_vm_register", "vm", payload);
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
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
|
-
}
|
|
77
|
+
channel.publish(topic + "/announce", msg, true)
|
|
78
|
+
}).then(_ => worker.terminate())
|
|
79
|
+
} else {
|
|
80
|
+
worker.terminate()
|
|
118
81
|
}
|
|
119
82
|
}
|
|
120
83
|
|
|
121
84
|
if (globalThis.Process) {
|
|
122
85
|
Process.on("exit", () => {
|
|
123
|
-
|
|
86
|
+
terminateAll()
|
|
124
87
|
})
|
|
125
88
|
};
|
|
126
89
|
|
|
@@ -170,88 +133,84 @@ async function StartWASM2(hub, config, getWorker) {
|
|
|
170
133
|
console.log("[machine-starter] Call MixExplain() for explaining debug levels, and MixSetMask(m) for changing these")
|
|
171
134
|
};
|
|
172
135
|
last_hub = hub;
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
136
|
+
let channel = await hub.newChannel();
|
|
137
|
+
// injected by either index.js or node.js
|
|
138
|
+
let worker = await getWorker();
|
|
139
|
+
if (workers[config.vmID] || ffiLoops[config.vmID])
|
|
140
|
+
throw new Error("Cannot start VM, the ID is used: " + config.vmID);
|
|
141
|
+
workers[config.vmID] = worker;
|
|
142
|
+
if (config.topicPrefix != undefined && config.topicPrefix != "") {
|
|
143
|
+
worker_topics[config.vmID] = config.topicPrefix + "/vm/" + config.vmID;
|
|
144
|
+
} else {
|
|
145
|
+
worker_topics[config.vmID] = "/org/" + config.org + "/ws/" + config.workspace + "/vm/" + config.vmID;
|
|
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);
|
|
184
163
|
} else {
|
|
185
|
-
|
|
164
|
+
starterFFIkind[k] = {...v};
|
|
165
|
+
delete starterFFIkind[k].run;
|
|
186
166
|
}
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
167
|
+
};
|
|
168
|
+
let vm_kind = { isRaw:false, canFail:false, useJsonDecoder:false};
|
|
169
|
+
starterFFIkind["$vm_start"] = vm_kind;
|
|
170
|
+
starterFFIfuns["$vm_start"] = (conn, args) => ffi_vm_start(conn, args, getWorker);
|
|
171
|
+
starterFFIkind["$vm_stop"] = vm_kind;
|
|
172
|
+
starterFFIfuns["$vm_stop"] = ffi_vm_stop;
|
|
173
|
+
let mixrtCode = config.mixrtCode;
|
|
174
|
+
if (typeof(mixrtCode) == "string" && mixrtCode.startsWith("remix://")) {
|
|
175
|
+
// for the desktop only: precompile the mixrt module to get faster
|
|
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;
|
|
196
186
|
}
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
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
|
-
}
|
|
187
|
+
};
|
|
188
|
+
ffiLoops[config.vmID] = setupStarterFFIs(hub, config, starterFFIfuns);
|
|
189
|
+
blobsLoops[config.vmID] = setupBlobs(hub, config);
|
|
190
|
+
let config_msg =
|
|
191
|
+
{ "_rmx_type": "msg_vm_configure",
|
|
192
|
+
"baseURL": config.baseURL,
|
|
193
|
+
"org": config.org,
|
|
194
|
+
"workspace": config.workspace,
|
|
195
|
+
"topicPrefix": config.topicPrefix,
|
|
196
|
+
"vmID": config.vmID,
|
|
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)
|
|
227
211
|
};
|
|
228
|
-
|
|
229
|
-
|
|
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
|
-
});
|
|
212
|
+
worker.postMessage(config_msg, [ channel.port ]);
|
|
213
|
+
return worker;
|
|
255
214
|
}
|
|
256
215
|
|
|
257
216
|
async function mixcoreStarterFFIs(mixcore) {
|
|
@@ -428,7 +387,7 @@ async function starterFFIsMessageLoop(hub, config, starterFFIs, terminate) {
|
|
|
428
387
|
sub.next().then((msg) => ({msg})),
|
|
429
388
|
terminate.then(() => ({terminated: true})),
|
|
430
389
|
]);
|
|
431
|
-
if (terminated
|
|
390
|
+
if (terminated) {
|
|
432
391
|
// console.log("[machine-starter] terminate FFI loop", vmID);
|
|
433
392
|
return;
|
|
434
393
|
}
|
|
@@ -437,7 +396,7 @@ async function starterFFIsMessageLoop(hub, config, starterFFIs, terminate) {
|
|
|
437
396
|
let args = decode(msg.payload.args);
|
|
438
397
|
let fun = starterFFIs[name];
|
|
439
398
|
if (initialMask & DEBUG_STATE)
|
|
440
|
-
console.debug("[
|
|
399
|
+
console.debug("[groovebox-starter] starter FFI call", vmID, call_id, name);
|
|
441
400
|
try {
|
|
442
401
|
if (!fun) throw new Error("No such starter FFI in groovebox starter: " + name);
|
|
443
402
|
let connector =
|
|
@@ -451,21 +410,21 @@ async function starterFFIsMessageLoop(hub, config, starterFFIs, terminate) {
|
|
|
451
410
|
if (r instanceof Promise) {
|
|
452
411
|
// r.catch(_ => null); // prevent "unhandled rejection"
|
|
453
412
|
if (initialMask & DEBUG_STATE)
|
|
454
|
-
console.debug("[
|
|
413
|
+
console.debug("[groovebox-starter] running FFI call asynchronously", vmID, call_id, name);
|
|
455
414
|
await comm.later(call_id);
|
|
456
415
|
let value = await r;
|
|
457
416
|
if (initialMask & DEBUG_STATE)
|
|
458
|
-
console.debug("[
|
|
417
|
+
console.debug("[groovebox-starter] async FFI call returned", vmID, call_id, name);
|
|
459
418
|
await comm.returnOrFail(fun, call_id, value);
|
|
460
419
|
} else {
|
|
461
420
|
if (initialMask & DEBUG_STATE)
|
|
462
|
-
console.debug("[
|
|
421
|
+
console.debug("[groovebox-starter] sync FFI call returned", vmID, call_id, name);
|
|
463
422
|
await comm.returnOrFail(fun, call_id, r);
|
|
464
423
|
}
|
|
465
424
|
} catch (reason) {
|
|
466
425
|
if (initialMask & DEBUG_STATE) {
|
|
467
|
-
console.debug("[
|
|
468
|
-
console.debug("[
|
|
426
|
+
console.debug("[groovebox-starter] FFI call exception", vmID, call_id, name);
|
|
427
|
+
console.debug("[groovebox-starter] exception", reason);
|
|
469
428
|
};
|
|
470
429
|
await comm.error(call_id, reason.message, reason.stack);
|
|
471
430
|
}
|
|
@@ -488,7 +447,7 @@ async function blobsMessageLoop(hub, config, terminate) {
|
|
|
488
447
|
sub.next().then((msg) => ({msg})),
|
|
489
448
|
terminate.then(() => ({terminated: true})),
|
|
490
449
|
]);
|
|
491
|
-
if (terminated
|
|
450
|
+
if (terminated) {
|
|
492
451
|
// console.log("[machine-starter] terminate blobs loop", vmID);
|
|
493
452
|
return;
|
|
494
453
|
}
|
|
@@ -558,6 +517,6 @@ class FFIComm {
|
|
|
558
517
|
}
|
|
559
518
|
|
|
560
519
|
// use terminateVM to shut a worker down!
|
|
561
|
-
export { StartWASM, StartWASM2, terminateVM,
|
|
520
|
+
export { StartWASM, StartWASM2, terminateAll, terminateVM,
|
|
562
521
|
Token, Case, Opaque
|
|
563
522
|
}
|
package/groovebox_build.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var GROOVEBOX_BUILD = "
|
|
1
|
+
var GROOVEBOX_BUILD = "13014";
|
|
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.13014.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": "2.
|
|
14
|
+
"@remix_labs/hub-client": "2.13014.0-dev",
|
|
15
15
|
"nanoid": "^5.0.2",
|
|
16
16
|
"web-worker": "^1.2.0"
|
|
17
17
|
},
|