@remix_labs/machine-starter 2.3239.0 → 2.3241.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
@@ -2,61 +2,34 @@ import { GROOVEBOX_BUILD } from "./groovebox_build.js";
2
2
  import { nanoid } from "nanoid";
3
3
  import { MixcoreTauri, filterFFINames } from "./mixcore.lib.mjs";
4
4
 
5
- const DEBUG_STATE = 4;
6
-
7
- let last_hub = null;
8
-
9
5
  let workers = {};
10
6
  let children = {};
11
7
  let ffiLoops = {};
12
- let blobs = {};
13
- let blobsLoops = {};
14
8
 
15
9
  function terminateAll() {
16
- console.log("[machine-starter] terminate all VMs");
10
+ console.log("[groovebox-starter] terminate all VMs");
17
11
  for (let [vmID, worker] of Object.entries(workers)) {
18
12
  worker.terminate();
19
13
  };
20
- for (let [vmID, blist] of Object.entries(blobs)) {
21
- for (let blob_url of blist) {
22
- console.log("[machine-starter] revoke ", blob_url);
23
- URL.revokeObjectURL(blob_url);
24
- }
25
- };
26
14
  for (let ffiLoop of Object.values(ffiLoops)) {
27
15
  ffiLoop.terminate();
28
16
  }
29
- for (let blobsLoop of Object.values(blobsLoops)) {
30
- blobsLoop.terminate();
31
- }
32
17
  workers = {};
33
18
  children = {};
34
19
  ffiLoops = {};
35
- blobs = {};
36
- blobsLoops = {};
37
20
  }
38
21
 
39
22
  function terminateVM(vmID) {
40
- console.log("[machine-starter] terminate VM", vmID);
23
+ console.log("[groovebox-starter] terminate VM", vmID);
41
24
  let worker = workers[vmID];
42
25
  if (worker) {
43
26
  let vmChildren = children[vmID];
44
- let blobList = blobs[vmID] || [];
45
27
  let ffiLoop = ffiLoops[vmID];
46
- let blobsLoop = blobsLoops[vmID];
47
28
  delete workers[vmID];
48
29
  delete children[vmID];
49
30
  delete ffiLoops[vmID];
50
- delete blobsLoops[vmID];
51
31
  worker.terminate();
52
32
  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
33
  if (vmChildren) {
61
34
  for (let subID of vmChildren) {
62
35
  terminateVM(subID)
@@ -67,37 +40,19 @@ function terminateVM(vmID) {
67
40
 
68
41
  if (globalThis.Process) {
69
42
  Process.on("exit", () => {
43
+ console.log("terminating machine workers");
70
44
  terminateAll()
71
45
  })
72
46
  };
73
47
 
74
- let initialMask = 0;
75
- const logMaskTopic = "/logMask";
76
-
77
- globalThis.MixExplain = (_ => {
78
- console.debug("Debug levels (call MixSetMask to change):");
79
- console.debug("DEBUG_MEMORY 1");
80
- console.debug("DEBUG_SCHEDULER 2");
81
- console.debug("DEBUG_STATE 4");
82
- console.debug("DEBUG_STACK 8");
83
- console.debug("DEBUG_LOOP 16");
84
- console.debug("DEBUG_MEMORY_OG 32");
85
- console.debug("DEBUG_GC 64");
86
- console.debug("DEBUG_INTERPRETER 128");
87
- console.debug("DEBUG_MIXERROR 256");
88
- console.debug("DEBUG_MESSAGES 1024");
89
- console.debug("DEBUG_QUEUES 2048");
90
- console.debug("DEBUG_OUTPUT 4096");
91
- console.debug("DEBUG_COMPILER 8192");
92
- });
48
+ let initialMask = 1024; // default for web
93
49
 
94
50
  globalThis.MixSetMask = (m => {
95
51
  initialMask = m;
96
- if (last_hub) {
97
- last_hub.newChannel().then(channel => {
98
- let msg = channel.newJSONMessage("_rmx_setLogMask", "MixSetMask", { mask: m });
99
- channel.set(logMaskTopic, msg)
100
- });
52
+ for (let [vmID, worker] of Object.entries(workers)) {
53
+ worker.postMessage({ "_rmx_type": "msg_vm_logMask",
54
+ "mask": m
55
+ })
101
56
  }
102
57
  });
103
58
 
@@ -113,10 +68,7 @@ function StartWASM(hub, baseURL, org, workspace, vmID, user, token, noOutputViaM
113
68
  }
114
69
 
115
70
  async function StartWASM2(hub, config) {
116
- if (!last_hub) {
117
- console.log("[machine-starter] Call MixExplain() for explaining debug levels, and MixSetMask(m) for changing these")
118
- };
119
- last_hub = hub;
71
+ console.log("[groovebox-starter] start VM", GROOVEBOX_BUILD, config.vmID, config.mixcore);
120
72
  let channel = await hub.newChannel();
121
73
  // injected by either index.js or node.js
122
74
  let worker = globalThis.GetMachineWASMWorker();
@@ -126,7 +78,7 @@ async function StartWASM2(hub, config) {
126
78
  let localFFIs = config.localFFIs || {};
127
79
  let mixcoreFFIs = await getMixcoreFFIs(config.mixcore);
128
80
  if (mixcoreFFIs) {
129
- console.log("[machine-starter] mixcore FFIs", Object.keys(mixcoreFFIs));
81
+ console.log("[groovebox-starter] mixcore FFIs", Object.keys(mixcoreFFIs));
130
82
  // merge the ffis and store them back into the config so sub vms also get them
131
83
  config.localFFIs = Object.assign(localFFIs, mixcoreFFIs);
132
84
  // delete the mixcore config to avoid being handled further by the worker
@@ -148,23 +100,7 @@ async function StartWASM2(hub, config) {
148
100
  localFFIfuns["$vm_start"] = ffi_vm_start;
149
101
  localFFIkind["$vm_stop"] = vm_kind;
150
102
  localFFIfuns["$vm_stop"] = ffi_vm_stop;
151
- let mixrtCode = config.mixrtCode;
152
- if (typeof(mixrtCode) == "string" && mixrtCode.startsWith("remix://")) {
153
- // for the desktop only: precompile the mixrt module to get faster
154
- // startup times
155
- if (globalThis.mixrtCodeModule && globalThis.mixrtCodeURL == mixrtCode) {
156
- console.log("[machine-starter] reusing mixrt");
157
- mixrtCode = globalThis.mixrtCodeModule;
158
- } else {
159
- let url = mixrtCode;
160
- console.log("[machine-starter] pre-compiling mixrt");
161
- mixrtCode = await WebAssembly.compileStreaming(fetch(url));
162
- globalThis.mixrtCodeURL = url;
163
- globalThis.mixrtCodeModule = mixrtCode;
164
- }
165
- };
166
103
  ffiLoops[config.vmID] = setupLocalFFIs(hub, config, localFFIfuns);
167
- blobsLoops[config.vmID] = setupBlobs(hub, config);
168
104
  let config_msg =
169
105
  { "_rmx_type": "msg_vm_configure",
170
106
  "baseURL": config.baseURL,
@@ -182,7 +118,7 @@ async function StartWASM2(hub, config) {
182
118
  "interceptFFI": config.interceptFFI,
183
119
  "interceptDynloadFFI": config.interceptDynloadFFI,
184
120
  "mixcore": config.mixcore,
185
- "mixrtCode": mixrtCode
121
+ "mixrtCode": config.mixrtCode
186
122
  // can be: base64-encoded string, Uint8Array, or a URL as string
187
123
  // (http, https, data)
188
124
  };
@@ -192,7 +128,6 @@ async function StartWASM2(hub, config) {
192
128
 
193
129
  async function ffi_vm_start(conn, args) {
194
130
  let vm_id = "embedded." + nanoid();
195
- console.log("[machine-starter] starting VM: ", vm_id);
196
131
  let config = {};
197
132
  for (const [k, v] of Object.entries(conn.config)) {
198
133
  config[k] = v;
@@ -355,15 +290,14 @@ async function localFFIsMessageLoop(hub, config, localFFIs, terminate) {
355
290
  terminate.then(() => ({terminated: true})),
356
291
  ]);
357
292
  if (terminated) {
358
- // console.log("[machine-starter] terminate FFI loop", vmID);
293
+ console.log("[groovebox-starter] terminate FFI loop", vmID);
359
294
  return;
360
295
  }
361
296
  let name = msg.payload.name;
362
297
  let call_id = msg.payload.call_id;
363
298
  let args = decode(msg.payload.args);
364
299
  let fun = localFFIs[name];
365
- if (initialMask & DEBUG_STATE)
366
- console.debug("[groovebox-starter] local FFI call", vmID, call_id, name);
300
+ console.log("[groovebox-starter] local FFI call", vmID, call_id, name);
367
301
  try {
368
302
  if (!fun) throw new Error("No such local FFI in groovebox starter: " + name);
369
303
  let connector =
@@ -376,57 +310,18 @@ async function localFFIsMessageLoop(hub, config, localFFIs, terminate) {
376
310
  let r = fun instanceof Function ? fun(connector, args) : fun.run(connector, args);
377
311
  if (r instanceof Promise) {
378
312
  // r.catch(_ => null); // prevent "unhandled rejection"
379
- if (initialMask & DEBUG_STATE)
380
- console.debug("[groovebox-starter] running FFI call asynchronously", vmID, call_id, name);
381
313
  await comm.later(call_id);
382
314
  let value = await r;
383
- if (initialMask & DEBUG_STATE)
384
- console.debug("[groovebox-starter] async FFI call returned", vmID, call_id, name);
385
315
  await comm.returnOrFail(fun, call_id, value);
386
316
  } else {
387
- if (initialMask & DEBUG_STATE)
388
- console.debug("[groovebox-starter] sync FFI call returned", vmID, call_id, name);
389
317
  await comm.returnOrFail(fun, call_id, r);
390
318
  }
391
319
  } catch (reason) {
392
- if (initialMask & DEBUG_STATE) {
393
- console.debug("[groovebox-starter] FFI call exception", vmID, call_id, name);
394
- console.debug("[groovebox-starter] exception", reason);
395
- };
396
320
  await comm.error(call_id, reason.message, reason.stack);
397
321
  }
398
322
  }
399
323
  }
400
324
 
401
- function setupBlobs(hub, config) {
402
- let vmID = config.vmID;
403
- let terminate = Promise.withResolvers();
404
- blobsMessageLoop(hub, config, terminate.promise); // don't wait
405
- return {terminate: terminate.resolve};
406
- }
407
- async function blobsMessageLoop(hub, config, terminate) {
408
- let vmID = config.vmID;
409
- let channel = await hub.newChannel();
410
- let comm = new FFIComm(vmID, hub, channel);
411
- let sub = await channel.subscribe("/local/" + vmID + "/blobs");
412
- while (true) {
413
- let {msg, terminated} = await Promise.race([
414
- sub.next().then((msg) => ({msg})),
415
- terminate.then(() => ({terminated: true})),
416
- ]);
417
- if (terminated) {
418
- // console.log("[machine-starter] terminate blobs loop", vmID);
419
- return;
420
- }
421
- let name = msg.payload.name;
422
- let url = msg.payload.url;
423
- let blobList = blobs[vmID];
424
- if (!blobList) blobList = [];
425
- blobList.push(url);
426
- blobs[vmID] = blobList;
427
- }
428
- }
429
-
430
325
  class FFIComm {
431
326
  constructor(vmID, hub, channel) {
432
327
  this.vmID = vmID;
@@ -493,7 +388,6 @@ async function getMixcoreFFIs(config) {
493
388
  mixcore = await MixcoreTauri.create(config.workspace, config.app);
494
389
  break;
495
390
  default:
496
- console.warn("[machine-starter] not going to configure mixcore - kind: ", config?.kind);
497
391
  return;
498
392
  }
499
393
 
@@ -1,2 +1,2 @@
1
- var GROOVEBOX_BUILD = "3239";
1
+ var GROOVEBOX_BUILD = "3610";
2
2
  export { GROOVEBOX_BUILD }
package/index.js CHANGED
@@ -1,10 +1,12 @@
1
- import MachineWASMWorker from './machine-wasm.worker.mjs'
1
+ function workerify(code) {
2
+ let blob = new Blob([code], {type: 'application/javascript'});
3
+ return new Worker(URL.createObjectURL(blob));
4
+ }
2
5
 
3
- const blob = new Blob([MachineWASMWorker], {type: 'application/javascript'});
4
- const url = URL.createObjectURL(blob);
6
+ import MachineWASMWorker from './machine-wasm.worker.mjs'
5
7
 
6
8
  function GetMachineWASMWorker() {
7
- return new Worker(url);
9
+ return workerify(MachineWASMWorker);
8
10
  }
9
11
 
10
12
  globalThis.GetMachineWASMWorker = GetMachineWASMWorker;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@remix_labs/machine-starter",
3
- "version": "2.3239.0",
3
+ "version": "2.3241.0-dev",
4
4
  "description": "start the groove",
5
5
  "main": "node.js",
6
6
  "browser": "index.js",
@@ -11,11 +11,9 @@
11
11
  "author": "Remixlabs staff",
12
12
  "license": "ISC",
13
13
  "dependencies": {
14
- "@remix_labs/hub-client": "2.3239.0",
14
+ "@remix_labs/hub-client": "2.3241.0-dev",
15
15
  "nanoid": "^5.0.2",
16
16
  "web-worker": "^1.2.0"
17
17
  },
18
- "repository": {
19
- "url": "git+https://github.com/remixlabs/groovebox.git"
20
- }
18
+ "repository": "https://github.com/remixlabs/groovebox.git"
21
19
  }
package/package_pub.json CHANGED
@@ -15,7 +15,5 @@
15
15
  "nanoid": "^5.0.2",
16
16
  "web-worker": "^1.2.0"
17
17
  },
18
- "repository": {
19
- "url": "git+https://github.com/remixlabs/groovebox.git"
20
- }
18
+ "repository": "https://github.com/remixlabs/groovebox.git"
21
19
  }
package/start.js CHANGED
@@ -1,16 +1,6 @@
1
1
  import Process from "process";
2
2
  import { StartWASM2, Hub } from "./node.js";
3
- import { MixcoreWasm, MixcoreWasmApi } from "./mixcore.lib.mjs";
4
- import { mixcoreWasmCode } from './mixcore-wasm.js';
5
- import { WASI, OpenFile, File, Directory, PreopenDirectory, ConsoleStdout } from "@bjorn3/browser_wasi_shim";
6
- function importCode(base64) {
7
- let binstr = atob(base64);
8
- let code = new Uint8Array(binstr.length);
9
- for (let i = 0; i < binstr.length; i++)
10
- code[i] = binstr.charCodeAt(i);
11
- return code;
12
- }
13
- let mixcoreWasm = importCode(mixcoreWasmCode);
3
+ import { mixcoreWasmBase64, mixcoreJsBase64 } from './mixcore-wasm.js';
14
4
 
15
5
  if (Process.stdout._handle)
16
6
  Process.stdout._handle.setBlocking(true);
@@ -42,46 +32,47 @@ let noOutputViaMQTT = Process.env["MIX_MQTT_NO_OUTPUT"] !== undefined;
42
32
  console.log("Machine params: variant=" + variant + " baseURL=" + baseURL, " mqttURL=" + mqttURL, " mqttUser=" + mqttUser, " mqttToken=" + mqttToken, " id=" + id, " noOutputViaMQTT=" + noOutputViaMQTT);
43
33
  let w = new Hub.Worker();
44
34
  globalThis.GROOVEBOX_ALLOW_INSECURE_HTTP = true;
45
-
46
- let DB_DIR = "db";
47
- let wasi = new WASI([], [], [
48
- new OpenFile(new File([])),
49
- ConsoleStdout.lineBuffered(msg => console.log(`[WASI stdout] ${msg}`)),
50
- ConsoleStdout.lineBuffered(msg => console.warn(`[WASI stderr] ${msg}`)),
51
- new PreopenDirectory(".", new Map([[DB_DIR, new Directory(new Map())]]))
52
- ]);
53
- MixcoreWasmApi.create(mixcoreWasm, wasi).then(mixcoreWasmApi => {
54
- let mixcore = MixcoreWasm.create(DB_DIR, "compiler-testing", "test@user", mixcoreWasmApi);
55
- let mixcoreConfig = { kind: "mixcore", mixcore };
56
- w.configure({wsURL: mqttURL, user:mqttUser, token:mqttToken}).then(resp => {
57
- if (variant == "BYTE") variant = "QCODE";
58
- switch(variant) {
59
- case "WASM":
60
- case "WAT":
61
- case "QCODE":
62
- let startVM = (_ => {
63
- MixSetMask(64 + 256 + 512 + 1024);
64
- StartWASM2(w, {
65
- baseURL: baseURL,
66
- org: "local",
67
- workspace: "local",
68
- vmID: id,
69
- user: mqttUser,
70
- token: mqttToken,
71
- noOutputViaMQTT: noOutputViaMQTT,
72
- machType: variant,
73
- localFFIs: {},
74
- mixcore: mixcoreConfig,
75
- });
35
+ let mixcoreWasmBlob = new Blob([mixcoreWasm], { type: "application/wasm" });
36
+ let mixcoreWasmUrl = Url.createObjectURL(mixcoreWasmBlob); // leaks, but hey we are on node
37
+ let mixcoreJsBlob = new Blob([MixcoreJs], { type: "text/javascript" });
38
+ let mixcoreJsUrl = Url.createObjectURL(mixcoreJsBlob);
39
+ let mixcore = {
40
+ kind: "wasm-opfs",
41
+ mixcoreWasmUrl,
42
+ mixcoreJsUrl,
43
+ dbDir: "/databases",
44
+ dbUser: "test@user",
45
+ dbName: "compiler-testing",
46
+ createDbs: ["compiler-testing"],
47
+ };
48
+ w.configure({wsURL: mqttURL, user:mqttUser, token:mqttToken}).then(resp => {
49
+ if (variant == "BYTE") variant = "QCODE";
50
+ switch(variant) {
51
+ case "WASM":
52
+ case "WAT":
53
+ case "QCODE":
54
+ let startVM = (_ => {
55
+ MixSetMask(64 + 256 + 512 + 1024);
56
+ StartWASM2(w, {
57
+ baseURL: baseURL,
58
+ org: "local",
59
+ workspace: "local",
60
+ vmID: id,
61
+ user: mqttUser,
62
+ token: mqttToken,
63
+ noOutputViaMQTT: noOutputViaMQTT,
64
+ machType: variant,
65
+ localFFIs: {},
66
+ mixcore,
76
67
  });
77
- if (globalThis.EnableAndStartMixCompiler) {
78
- EnableAndStartMixCompiler(w).then(startVM);
79
- } else {
80
- startVM()
81
- };
82
- break
83
- default:
84
- throw(new Error("unknown MIX_MACHINE_VARIANT: " + variant))
85
- }
86
- })
68
+ });
69
+ if (globalThis.EnableAndStartMixCompiler) {
70
+ EnableAndStartMixCompiler(w).then(startVM);
71
+ } else {
72
+ startVM()
73
+ };
74
+ break
75
+ default:
76
+ throw(new Error("unknown MIX_MACHINE_VARIANT: " + variant))
77
+ }
87
78
  });