reg-cli 0.19.0-rc0 → 0.19.0-rc2

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.
@@ -0,0 +1,235 @@
1
+ import { c as computeWasiSandbox, d as normalizeWasiArgv, f as readWasm, l as filterWasiImports } from "./tracing-Bo38b9aR.mjs";
2
+ import { parentPort, workerData } from "node:worker_threads";
3
+ import { env } from "node:process";
4
+ import fs from "node:fs";
5
+ import { WASI } from "@tybys/wasm-util";
6
+ //#region src/proxy.ts
7
+ const kIsProxy = Symbol("kIsProxy");
8
+ const createInstanceProxy = (instance, memory) => {
9
+ if (instance[kIsProxy]) return instance;
10
+ const originalExports = instance.exports;
11
+ const createHandler = function(target) {
12
+ const handlers = [
13
+ "apply",
14
+ "construct",
15
+ "defineProperty",
16
+ "deleteProperty",
17
+ "get",
18
+ "getOwnPropertyDescriptor",
19
+ "getPrototypeOf",
20
+ "has",
21
+ "isExtensible",
22
+ "ownKeys",
23
+ "preventExtensions",
24
+ "set",
25
+ "setPrototypeOf"
26
+ ];
27
+ const handler = {};
28
+ for (let i = 0; i < handlers.length; i++) {
29
+ const name = handlers[i];
30
+ handler[name] = function() {
31
+ const args = Array.prototype.slice.call(arguments, 1);
32
+ args.unshift(target);
33
+ return Reflect[name].apply(Reflect, args);
34
+ };
35
+ }
36
+ return handler;
37
+ };
38
+ const handler = createHandler(originalExports);
39
+ const _initialize = () => {};
40
+ const _start = () => 0;
41
+ handler.get = function(_target, p, receiver) {
42
+ if (p === "memory") return (typeof memory === "function" ? memory() : memory) ?? Reflect.get(originalExports, p, receiver);
43
+ if (p === "_initialize") return p in originalExports ? _initialize : void 0;
44
+ if (p === "_start") return p in originalExports ? _start : void 0;
45
+ return Reflect.get(originalExports, p, receiver);
46
+ };
47
+ handler.has = function(_target, p) {
48
+ if (p === "memory") return true;
49
+ return Reflect.has(originalExports, p);
50
+ };
51
+ const exportsProxy = new Proxy(Object.create(null), handler);
52
+ return new Proxy(instance, { get(target, p, receiver) {
53
+ if (p === "exports") return exportsProxy;
54
+ if (p === kIsProxy) return true;
55
+ return Reflect.get(target, p, receiver);
56
+ } });
57
+ };
58
+ //#endregion
59
+ //#region src/progress.ts
60
+ const MARKER = "__REG_CLI_EVT__ ";
61
+ /**
62
+ * Build a WASI `printErr` hook that forwards progress events to `onEvent`
63
+ * and everything else to `fallback` (defaults to `console.error`).
64
+ *
65
+ * `@tybys/wasm-util`'s `StandardOutput.write` already buffers until a
66
+ * newline and invokes the callback with one stripped line per call
67
+ * (see the `StandardOutput` class in
68
+ * `node_modules/@tybys/wasm-util/dist/wasm-util.esm-bundler.js`). So
69
+ * we can treat each invocation as a single complete line.
70
+ */
71
+ const createPrintErrHook = (onEvent, fallback = (s) => console.error(s)) => {
72
+ return (line) => {
73
+ if (line.startsWith(MARKER)) {
74
+ const json = line.slice(16);
75
+ try {
76
+ const ev = JSON.parse(json);
77
+ if (ev && typeof ev.type === "string" && typeof ev.path === "string") {
78
+ onEvent(ev);
79
+ return;
80
+ }
81
+ } catch {}
82
+ }
83
+ fallback(line);
84
+ };
85
+ };
86
+ //#endregion
87
+ //#region src/runner.ts
88
+ const mode = workerData.mode;
89
+ const isTracingEnabled = () => env.OTEL_ENABLED === "true" || env.JAEGER_ENABLED === "true";
90
+ const argv = normalizeWasiArgv(workerData.argv);
91
+ const sandbox = computeWasiSandbox(argv);
92
+ const printErr = createPrintErrHook((ev) => {
93
+ parentPort?.postMessage({
94
+ cmd: "compare-event",
95
+ event: ev
96
+ });
97
+ });
98
+ const wasi = new WASI({
99
+ version: "preview1",
100
+ args: argv,
101
+ env: sandbox.env,
102
+ returnOnExit: true,
103
+ preopens: sandbox.preopens,
104
+ fs,
105
+ printErr
106
+ });
107
+ const wasmFile = mode === "entry" ? readWasm() : null;
108
+ const readWasmString = (exports, memory, outputPtr) => {
109
+ const view = new DataView(memory.buffer, outputPtr);
110
+ const len = view.getUint32(0, true);
111
+ const bufPtr = view.getUint32(4, true);
112
+ const bytes = new Uint8Array(memory.buffer, bufPtr, len);
113
+ const s = new TextDecoder("utf-8").decode(bytes);
114
+ exports.free_wasm_output(outputPtr);
115
+ return s;
116
+ };
117
+ const makeSpanRecorder = (workerLabel) => {
118
+ const workerSpans = [];
119
+ const tSpan = async (name, fn, attributes) => {
120
+ if (!isTracingEnabled()) return fn();
121
+ const start_ms = Date.now();
122
+ const r = await fn();
123
+ workerSpans.push({
124
+ name,
125
+ start_ms,
126
+ end_ms: Date.now(),
127
+ attributes,
128
+ worker_label: workerLabel
129
+ });
130
+ return r;
131
+ };
132
+ return {
133
+ workerSpans,
134
+ tSpan
135
+ };
136
+ };
137
+ const makeThreadSpawnImport = (memory, wasmModule) => (startArg) => {
138
+ const buf = new SharedArrayBuffer(4);
139
+ const id = new Int32Array(buf);
140
+ Atomics.store(id, 0, -1);
141
+ parentPort?.postMessage({
142
+ cmd: "thread-spawn",
143
+ startArg,
144
+ threadId: id,
145
+ memory,
146
+ wasmModule
147
+ });
148
+ Atomics.wait(id, 0, -1);
149
+ return Atomics.load(id, 0);
150
+ };
151
+ const compileAndInstantiate = async (memory, tSpan, prefix, compiledModule) => {
152
+ const wasm = compiledModule ?? await tSpan(`${prefix}.wasm_compile`, async () => {
153
+ if (!wasmFile) throw new Error("Wasm bytes are unavailable in a thread worker");
154
+ return WebAssembly.compile(await wasmFile);
155
+ });
156
+ const wasi_snapshot_preview1 = filterWasiImports(wasm, wasi.getImportObject().wasi_snapshot_preview1);
157
+ return {
158
+ instance: await tSpan(`${prefix}.wasm_instantiate`, async () => WebAssembly.instantiate(wasm, {
159
+ wasi_snapshot_preview1,
160
+ wasi: { "thread-spawn": makeThreadSpawnImport(memory, wasm) },
161
+ env: { memory }
162
+ })),
163
+ module: wasm
164
+ };
165
+ };
166
+ if (mode === "entry") (async () => {
167
+ const entry_start_ms = Date.now();
168
+ const { workerSpans, tSpan } = makeSpanRecorder("entry");
169
+ const memory = new WebAssembly.Memory({
170
+ initial: 256,
171
+ maximum: 16384,
172
+ shared: true
173
+ });
174
+ const { instance } = await compileAndInstantiate(memory, tSpan, "entry");
175
+ const exports = instance.exports;
176
+ await tSpan("entry.wasi_start", async () => {
177
+ wasi.start(instance);
178
+ });
179
+ if (isTracingEnabled() && typeof exports.init_tracing === "function") await tSpan("entry.init_tracing_rust", async () => {
180
+ exports.init_tracing();
181
+ });
182
+ const m = await tSpan("entry.wasm_main", async () => exports.wasm_main());
183
+ const reportString = await tSpan("entry.read_report_string", async () => readWasmString(exports, memory, m));
184
+ const report = JSON.parse(reportString);
185
+ let traceData = null;
186
+ if (isTracingEnabled() && typeof exports.get_trace_data === "function") await tSpan("entry.collect_rust_traces", async () => {
187
+ const traceJson = readWasmString(exports, memory, exports.get_trace_data());
188
+ try {
189
+ traceData = JSON.parse(traceJson);
190
+ } catch (e) {
191
+ console.error("[Tracing] Failed to parse trace data:", e);
192
+ }
193
+ if (typeof exports.clear_trace_data === "function") exports.clear_trace_data();
194
+ });
195
+ if (isTracingEnabled()) workerSpans.push({
196
+ name: "entry.worker_total",
197
+ start_ms: entry_start_ms,
198
+ end_ms: Date.now(),
199
+ worker_label: "entry"
200
+ });
201
+ parentPort?.postMessage({
202
+ cmd: "complete",
203
+ data: report,
204
+ traceData,
205
+ workerSpans
206
+ });
207
+ })();
208
+ else parentPort?.addListener("message", async ({ startArg, tid, memory, wasmModule }) => {
209
+ const workerLabel = `thread-${tid}`;
210
+ const handler_start_ms = Date.now();
211
+ const { workerSpans, tSpan } = makeSpanRecorder(workerLabel);
212
+ let { instance } = await compileAndInstantiate(memory, tSpan, "worker", wasmModule);
213
+ instance = createInstanceProxy(instance, memory);
214
+ await tSpan("worker.wasi_start", async () => {
215
+ wasi.start(instance);
216
+ });
217
+ await tSpan("worker.wasi_thread_start", async () => {
218
+ instance.exports.wasi_thread_start(tid, startArg);
219
+ }, { tid });
220
+ if (isTracingEnabled()) {
221
+ workerSpans.push({
222
+ name: "worker.thread_total",
223
+ start_ms: handler_start_ms,
224
+ end_ms: Date.now(),
225
+ worker_label: workerLabel,
226
+ attributes: { tid }
227
+ });
228
+ parentPort?.postMessage({
229
+ cmd: "worker-spans",
230
+ workerSpans
231
+ });
232
+ }
233
+ });
234
+ //#endregion
235
+ export {};