@remix_labs/machine-starter 2.13012.0 → 2.13022.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 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
- 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.
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 getCoord(vmID) {
60
- let coord = coordination[vmID];
61
- if (!coord) {
62
- coord = { queue: new TaskQueue() };
63
- coordination[vmID] = coord;
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
- 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 };
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
- 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
- }
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
- terminateAllOnExit()
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
- // 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;
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
- worker_topics[config.vmID] = "/org/" + config.org + "/ws/" + config.workspace + "/vm/" + config.vmID;
164
+ starterFFIkind[k] = {...v};
165
+ delete starterFFIkind[k].run;
186
166
  }
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;
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
- 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
- }
207
- };
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
- }
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
- 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
- });
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 || msg.eof) {
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("[machine-starter] starter FFI call", vmID, call_id, name);
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("[machine-starter] running FFI call asynchronously", vmID, call_id, name);
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("[machine-starter] async FFI call returned", vmID, call_id, name);
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("[machine-starter] sync FFI call returned", vmID, call_id, name);
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("[machine-starter] FFI call exception", vmID, call_id, name);
468
- console.debug("[machine-starter] exception", reason);
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 || msg.eof) {
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
  }
@@ -1,2 +1,2 @@
1
- var GROOVEBOX_BUILD = "13012";
1
+ var GROOVEBOX_BUILD = "13022";
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.13012.0",
3
+ "version": "2.13022.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.13012.0",
14
+ "@remix_labs/hub-client": "2.13022.0-dev",
15
15
  "nanoid": "^5.0.2",
16
16
  "web-worker": "^1.2.0"
17
17
  },