reg-cli 0.19.0-rc0 → 0.19.0-rc1

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,228 @@
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 = readWasm();
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) => (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
+ });
147
+ Atomics.wait(id, 0, -1);
148
+ return Atomics.load(id, 0);
149
+ };
150
+ const compileAndInstantiate = async (memory, tSpan, prefix) => {
151
+ const wasm = await tSpan(`${prefix}.wasm_compile`, async () => WebAssembly.compile(await wasmFile));
152
+ const wasi_snapshot_preview1 = filterWasiImports(wasm, wasi.getImportObject().wasi_snapshot_preview1);
153
+ return tSpan(`${prefix}.wasm_instantiate`, async () => WebAssembly.instantiate(wasm, {
154
+ wasi_snapshot_preview1,
155
+ wasi: { "thread-spawn": makeThreadSpawnImport(memory) },
156
+ env: { memory }
157
+ }));
158
+ };
159
+ if (mode === "entry") (async () => {
160
+ const entry_start_ms = Date.now();
161
+ const { workerSpans, tSpan } = makeSpanRecorder("entry");
162
+ const memory = new WebAssembly.Memory({
163
+ initial: 256,
164
+ maximum: 16384,
165
+ shared: true
166
+ });
167
+ const instance = await compileAndInstantiate(memory, tSpan, "entry");
168
+ const exports = instance.exports;
169
+ await tSpan("entry.wasi_start", async () => {
170
+ wasi.start(instance);
171
+ });
172
+ if (isTracingEnabled() && typeof exports.init_tracing === "function") await tSpan("entry.init_tracing_rust", async () => {
173
+ exports.init_tracing();
174
+ });
175
+ const m = await tSpan("entry.wasm_main", async () => exports.wasm_main());
176
+ const reportString = await tSpan("entry.read_report_string", async () => readWasmString(exports, memory, m));
177
+ const report = JSON.parse(reportString);
178
+ let traceData = null;
179
+ if (isTracingEnabled() && typeof exports.get_trace_data === "function") await tSpan("entry.collect_rust_traces", async () => {
180
+ const traceJson = readWasmString(exports, memory, exports.get_trace_data());
181
+ try {
182
+ traceData = JSON.parse(traceJson);
183
+ } catch (e) {
184
+ console.error("[Tracing] Failed to parse trace data:", e);
185
+ }
186
+ if (typeof exports.clear_trace_data === "function") exports.clear_trace_data();
187
+ });
188
+ if (isTracingEnabled()) workerSpans.push({
189
+ name: "entry.worker_total",
190
+ start_ms: entry_start_ms,
191
+ end_ms: Date.now(),
192
+ worker_label: "entry"
193
+ });
194
+ parentPort?.postMessage({
195
+ cmd: "complete",
196
+ data: report,
197
+ traceData,
198
+ workerSpans
199
+ });
200
+ })();
201
+ else parentPort?.addListener("message", async ({ startArg, tid, memory }) => {
202
+ const workerLabel = `thread-${tid}`;
203
+ const handler_start_ms = Date.now();
204
+ const { workerSpans, tSpan } = makeSpanRecorder(workerLabel);
205
+ let instance = await compileAndInstantiate(memory, tSpan, "worker");
206
+ instance = createInstanceProxy(instance, memory);
207
+ await tSpan("worker.wasi_start", async () => {
208
+ wasi.start(instance);
209
+ });
210
+ await tSpan("worker.wasi_thread_start", async () => {
211
+ instance.exports.wasi_thread_start(tid, startArg);
212
+ }, { tid });
213
+ if (isTracingEnabled()) {
214
+ workerSpans.push({
215
+ name: "worker.thread_total",
216
+ start_ms: handler_start_ms,
217
+ end_ms: Date.now(),
218
+ worker_label: workerLabel,
219
+ attributes: { tid }
220
+ });
221
+ parentPort?.postMessage({
222
+ cmd: "worker-spans",
223
+ workerSpans
224
+ });
225
+ }
226
+ });
227
+ //#endregion
228
+ export {};