@remix_labs/machine-starter 2.1999.0-dev → 2.2002.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 CHANGED
@@ -1,45 +1,18 @@
1
1
  import { GROOVEBOX_BUILD } from "./groovebox_build.js";
2
- import { nanoid } from "nanoid";
3
2
 
4
- let workers = {};
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
- };
3
+ let terminate = function(f) {};
4
+ if (globalThis.ThisIsNode) {
5
+ terminate = function(f) {
6
+ Process.on("exit", () => {
7
+ console.log("terminating machine worker");
8
+ f()
9
+ })
24
10
  }
25
- }
26
-
27
- if (globalThis.Process) {
28
- Process.on("exit", () => {
29
- console.log("terminating machine workers");
30
- terminateAll()
31
- })
32
11
  };
33
12
 
34
13
  let initialMask = 64 + 1024; // default for web
35
-
36
14
  globalThis.MixSetMask = (m => {
37
15
  initialMask = m;
38
- for (let [vmID, worker] of Object.entries(workers)) {
39
- worker.postMessage({ "_rmx_type": "msg_vm_logMask",
40
- "mask": m
41
- })
42
- }
43
16
  });
44
17
 
45
18
  function StartWASM(hub, baseURL, org, workspace, vmID, user, token, noOutputViaMQTT) {
@@ -58,24 +31,16 @@ function StartWASM2(hub, config) {
58
31
  return hub.newChannel().then(channel => {
59
32
  // injected by either index.js or node.js
60
33
  let worker = globalThis.GetMachineWASMWorker();
61
- workers[config.vmID] = worker;
62
- let localFFIkind = {};
63
- let localFFIfuns = {};
34
+ let localFFIs = {};
64
35
  if (config.localFFIs) {
65
36
  for (const [k, v] of Object.entries(config.localFFIs)) {
66
- localFFIfuns[k] = {...v};
67
37
  if (v.isRaw && (v.canFail || v.useJsonDecoder)) {
68
38
  console.error("A raw, local FFI must neither be failing nor use the JSON decoder: " + k);
69
39
  } else {
70
- localFFIkind[k] = {...v};
71
- delete localFFIkind[k].run;
40
+ localFFIs[k] = {...v};
41
+ delete localFFIs[k].run;
72
42
  }
73
- };
74
- let vm_kind = { isRaw:false, canFail:false, useJsonDecoder:false};
75
- localFFIkind["$vm_start"] = vm_kind;
76
- localFFIfuns["$vm_start"] = ffi_vm_start;
77
- localFFIkind["$vm_stop"] = vm_kind;
78
- localFFIfuns["$vm_stop"] = ffi_vm_stop;
43
+ }
79
44
  }
80
45
  let config_msg =
81
46
  { "_rmx_type": "msg_vm_configure",
@@ -89,43 +54,24 @@ function StartWASM2(hub, config) {
89
54
  "outputViaMQTT": !(config.noOutputViaMQTT),
90
55
  "machType": config.machType,
91
56
  "debugMask": initialMask,
92
- "localFFIs": localFFIkind,
57
+ "localFFIs": localFFIs,
93
58
  "allowInsecureHttp": globalThis.GROOVEBOX_ALLOW_INSECURE_HTTP,
94
59
  "interceptFFI": config.interceptFFI,
95
60
  "interceptDynloadFFI": config.interceptDynloadFFI,
96
61
  };
97
62
  worker.postMessage(config_msg, [ channel.port ]);
63
+ terminate(() => worker.terminate());
64
+ globalThis.MixSetMask = (m => {
65
+ worker.postMessage({ "_rmx_type": "msg_vm_logMask",
66
+ "mask": m
67
+ })
68
+ });
98
69
  if (config.localFFIs)
99
- setupLocalFFIs(hub, config, localFFIfuns); // don't await!
70
+ setupLocalFFIs(hub, config); // don't await!
100
71
  return worker;
101
72
  })
102
73
  }
103
74
 
104
- async function ffi_vm_start(conn, args) {
105
- let vm_id = "embedded." + nanoid();
106
- let config = {};
107
- for (const [k, v] of Object.entries(conn.config)) {
108
- config[k] = v;
109
- }
110
- config["vmID"] = vm_id;
111
- parents[vm_id] = conn.config.vmID;
112
- let arg_config = args[0];
113
- if (arg_config.interceptFFI !== undefined)
114
- config["interceptFFI"] = arg_config.interceptFFI;
115
- if (arg_config.interceptDynloadFFI !== undefined)
116
- config["interceptDynloadFFI"] = arg_config.interceptDynloadFFI;
117
- let worker = await StartWASM2(conn.hub, config);
118
- return vm_id;
119
- }
120
-
121
- async function ffi_vm_stop(conn, args) {
122
- let vm_id = args[0];
123
- let worker = workers[vm_id];
124
- if (!worker) throw new Error("no such VM: " + vm_id);
125
- terminateVM(vm_id);
126
- return null;
127
- }
128
-
129
75
  class Token {
130
76
  constructor(data, enabled, extras) {
131
77
  // data: must be an Uint8Array
@@ -241,13 +187,13 @@ function decode(val) {
241
187
  }
242
188
  }
243
189
 
244
- async function setupLocalFFIs(hub, config, localFFIs) {
245
- let vmID = config.vmID;
190
+ async function setupLocalFFIs(hub, config) {
246
191
  let channel = await hub.newChannel();
247
- let comm = new FFIComm(vmID, hub, channel);
248
- await channel.setLocalSubTopic("/local/" + vmID);
249
- await channel.setLocalPubTopic("/local/" + vmID);
250
- let sub = await channel.subscribe("/local/" + vmID + "/ffi/call");
192
+ let comm = new FFIComm(hub, channel);
193
+ let localFFIs = config.localFFIs;
194
+ await channel.setLocalSubTopic("/local/ffi/call");
195
+ await channel.setLocalPubTopic("/local/ffi/return");
196
+ let sub = await channel.subscribe("/local/ffi/call");
251
197
  while (true) {
252
198
  let msg = await sub.next();
253
199
  let name = msg.payload.name;
@@ -261,15 +207,14 @@ async function setupLocalFFIs(hub, config, localFFIs) {
261
207
  { call_id: call_id,
262
208
  hub: hub,
263
209
  channel: channel,
264
- config: config,
265
210
  state: config.state,
266
211
  };
267
212
  let r = fun instanceof Function ? fun(connector, args) : fun.run(connector, args);
268
213
  if (r instanceof Promise) {
269
- // r.catch(_ => null); // prevent "unhandled rejection"
270
214
  await comm.later(call_id);
271
- let value = await r;
272
- await comm.returnOrFail(fun, call_id, value);
215
+ r.then(
216
+ (value) => comm.returnOrFail(fun, call_id, value),
217
+ (reason) => comm.error(call_id, reason.message, reason.stack));
273
218
  } else {
274
219
  await comm.returnOrFail(fun, call_id, r);
275
220
  }
@@ -280,8 +225,7 @@ async function setupLocalFFIs(hub, config, localFFIs) {
280
225
  }
281
226
 
282
227
  class FFIComm {
283
- constructor(vmID, hub, channel) {
284
- this.vmID = vmID;
228
+ constructor(hub, channel) {
285
229
  this.hub = hub;
286
230
  this.channel = channel;
287
231
  }
@@ -290,7 +234,7 @@ class FFIComm {
290
234
  { call_id: call_id,
291
235
  };
292
236
  let response = this.hub.newLocalMessage("msg_ffi_later", "starter", r_payload);
293
- return this.channel.publish("/local/" + this.vmID + "/ffi/return", response, false);
237
+ return this.channel.publish("/local/ffi/return", response, false);
294
238
  }
295
239
  return_(fun, call_id, result) {
296
240
  let r_payload =
@@ -298,7 +242,7 @@ class FFIComm {
298
242
  value: fun.useJsonDecoder ? result : encode(result)
299
243
  };
300
244
  let response = this.hub.newLocalMessage("msg_ffi_return", "starter", r_payload);
301
- return this.channel.publish("/local/" + this.vmID + "/ffi/return", response, false);
245
+ return this.channel.publish("/local/ffi/return", response, false);
302
246
  }
303
247
  error(call_id, message, stack) {
304
248
  let r_payload =
@@ -307,7 +251,7 @@ class FFIComm {
307
251
  stack: stack === undefined ? [] : stack,
308
252
  };
309
253
  let response = this.hub.newLocalMessage("msg_ffi_error", "starter", r_payload);
310
- this.channel.publish("/local/" + this.vmID + "/ffi/return", response, false);
254
+ this.channel.publish("/local/ffi/return", response, false);
311
255
  }
312
256
  returnOrFail(fun, call_id, result) {
313
257
  if (fun.canFail) {
@@ -337,6 +281,4 @@ class FFIComm {
337
281
 
338
282
  // use worker.terminate() to shut a worker down!
339
283
 
340
- export { StartWASM, StartWASM2, terminateAll, terminateVM,
341
- Token, Case, Opaque
342
- }
284
+ export { StartWASM, StartWASM2, Token, Case, Opaque }
@@ -1,2 +1,2 @@
1
- var GROOVEBOX_BUILD = "2129";
1
+ var GROOVEBOX_BUILD = "2133";
2
2
  export { GROOVEBOX_BUILD }
package/index.js CHANGED
@@ -17,3 +17,4 @@ function GetMachineWASMWorker() {
17
17
  globalThis.GetMachineWASMWorker = GetMachineWASMWorker;
18
18
 
19
19
  export * from './common.js';
20
+