gm-plugkit 2.0.1110 → 2.0.1112
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/package.json +1 -1
- package/plugkit-wasm-wrapper.js +95 -8
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "gm-plugkit",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.1112",
|
|
4
4
|
"description": "Bootstrap and daemon-spawn tool for gm plugkit binary. Downloads the correct platform binary, verifies SHA256, and starts the spool watcher daemon. Includes plugkit-wasm-wrapper for WASM-based spool watching.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"bin": {
|
package/plugkit-wasm-wrapper.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import fs from 'fs';
|
|
2
2
|
import path from 'path';
|
|
3
3
|
import os from 'os';
|
|
4
|
+
import crypto from 'crypto';
|
|
4
5
|
import { watch } from 'fs';
|
|
5
6
|
import { spawn, spawnSync } from 'child_process';
|
|
6
7
|
|
|
@@ -23,14 +24,64 @@ function cosineSim(a, b) {
|
|
|
23
24
|
const browserSessions = new Map();
|
|
24
25
|
let nextBrowserSessionId = 1;
|
|
25
26
|
|
|
26
|
-
function createWasiShim() {
|
|
27
|
-
|
|
27
|
+
function createWasiShim(instanceRef) {
|
|
28
|
+
const getMemory = () => instanceRef.value.exports.memory.buffer;
|
|
29
|
+
const shim = {
|
|
30
|
+
proc_exit: (code) => process.exit(code),
|
|
31
|
+
fd_write: (fd, iovs_ptr, iovs_len, nwritten_ptr) => {
|
|
32
|
+
try {
|
|
33
|
+
const buf = getMemory();
|
|
34
|
+
const dv = new DataView(buf);
|
|
35
|
+
const chunks = [];
|
|
36
|
+
let total = 0;
|
|
37
|
+
for (let i = 0; i < iovs_len; i++) {
|
|
38
|
+
const base = iovs_ptr + i * 8;
|
|
39
|
+
const ptr = dv.getUint32(base, true);
|
|
40
|
+
const len = dv.getUint32(base + 4, true);
|
|
41
|
+
if (len > 0) {
|
|
42
|
+
chunks.push(new Uint8Array(buf, ptr, len).slice());
|
|
43
|
+
total += len;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
const merged = new Uint8Array(total);
|
|
47
|
+
let off = 0;
|
|
48
|
+
for (const c of chunks) { merged.set(c, off); off += c.length; }
|
|
49
|
+
const text = new TextDecoder('utf-8').decode(merged);
|
|
50
|
+
if (fd === 2) process.stderr.write(text);
|
|
51
|
+
else process.stdout.write(text);
|
|
52
|
+
new DataView(getMemory()).setUint32(nwritten_ptr, total, true);
|
|
53
|
+
return 0;
|
|
54
|
+
} catch (e) {
|
|
55
|
+
return 28;
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
random_get: (buf_ptr, buf_len) => {
|
|
59
|
+
try {
|
|
60
|
+
crypto.randomFillSync(new Uint8Array(getMemory(), buf_ptr, buf_len));
|
|
61
|
+
return 0;
|
|
62
|
+
} catch (e) {
|
|
63
|
+
return 28;
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
clock_time_get: (clock_id, precision, time_ptr) => {
|
|
67
|
+
try {
|
|
68
|
+
const ns = BigInt(Date.now()) * 1000000n;
|
|
69
|
+
new DataView(getMemory()).setBigUint64(time_ptr, ns, true);
|
|
70
|
+
return 0;
|
|
71
|
+
} catch (e) {
|
|
72
|
+
return 28;
|
|
73
|
+
}
|
|
74
|
+
},
|
|
75
|
+
environ_get: () => 0,
|
|
76
|
+
environ_sizes_get: () => 0,
|
|
77
|
+
};
|
|
78
|
+
return new Proxy(shim, {
|
|
28
79
|
get(target, prop) {
|
|
29
|
-
if (prop
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
80
|
+
if (prop in target) return target[prop];
|
|
81
|
+
return (...args) => {
|
|
82
|
+
console.error(`[plugkit-wasm] unimplemented WASI call: ${String(prop)} args=${args.length}`);
|
|
83
|
+
return 52;
|
|
84
|
+
};
|
|
34
85
|
}
|
|
35
86
|
});
|
|
36
87
|
}
|
|
@@ -374,6 +425,42 @@ async function runSpoolWatcher(instance, spoolDir) {
|
|
|
374
425
|
fs.mkdirSync(inDir, { recursive: true });
|
|
375
426
|
fs.mkdirSync(outDir, { recursive: true });
|
|
376
427
|
|
|
428
|
+
const LOCK_PATH = path.join(spoolDir, '.watcher.lock');
|
|
429
|
+
function acquireLock() {
|
|
430
|
+
try {
|
|
431
|
+
if (fs.existsSync(LOCK_PATH)) {
|
|
432
|
+
const content = fs.readFileSync(LOCK_PATH, 'utf-8').trim();
|
|
433
|
+
const [pidStr, tsStr] = content.split('|');
|
|
434
|
+
const lockTs = parseInt(tsStr, 10);
|
|
435
|
+
const age = Date.now() - lockTs;
|
|
436
|
+
if (age < 15000) {
|
|
437
|
+
console.error(`[plugkit-wasm] another watcher active (pid=${pidStr}, age=${age}ms); refusing to start`);
|
|
438
|
+
process.exit(1);
|
|
439
|
+
}
|
|
440
|
+
console.error(`[plugkit-wasm] stale lock (age=${age}ms); taking over`);
|
|
441
|
+
}
|
|
442
|
+
fs.writeFileSync(LOCK_PATH, `${process.pid}|${Date.now()}`);
|
|
443
|
+
} catch (e) {
|
|
444
|
+
console.error(`[plugkit-wasm] lock acquire failed: ${e.message}`);
|
|
445
|
+
process.exit(1);
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
function refreshLock() {
|
|
449
|
+
try { fs.writeFileSync(LOCK_PATH, `${process.pid}|${Date.now()}`); } catch (_) {}
|
|
450
|
+
}
|
|
451
|
+
function releaseLock() {
|
|
452
|
+
try {
|
|
453
|
+
const content = fs.readFileSync(LOCK_PATH, 'utf-8').trim();
|
|
454
|
+
const [pidStr] = content.split('|');
|
|
455
|
+
if (pidStr === String(process.pid)) fs.unlinkSync(LOCK_PATH);
|
|
456
|
+
} catch (_) {}
|
|
457
|
+
}
|
|
458
|
+
acquireLock();
|
|
459
|
+
setInterval(refreshLock, 5000);
|
|
460
|
+
process.on('SIGINT', () => { releaseLock(); process.exit(0); });
|
|
461
|
+
process.on('SIGTERM', () => { releaseLock(); process.exit(0); });
|
|
462
|
+
process.on('exit', releaseLock);
|
|
463
|
+
|
|
377
464
|
console.log(`[plugkit-wasm] plugkit v${resolveVersion(instance)} (wasm)`);
|
|
378
465
|
console.log(`[plugkit-wasm] watching ${inDir}`);
|
|
379
466
|
|
|
@@ -549,7 +636,7 @@ async function runSpoolWatcher(instance, spoolDir) {
|
|
|
549
636
|
|
|
550
637
|
const importObject = {
|
|
551
638
|
env: hostFunctions,
|
|
552
|
-
wasi_snapshot_preview1: createWasiShim(),
|
|
639
|
+
wasi_snapshot_preview1: createWasiShim(instanceRef),
|
|
553
640
|
};
|
|
554
641
|
|
|
555
642
|
const instance = new WebAssembly.Instance(wasmModule, importObject);
|