reg-cli 0.18.16 → 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.
- package/LICENSE +0 -0
- package/README.md +155 -46
- package/dist/cli.cjs +220 -0
- package/dist/cli.d.cts +1 -0
- package/dist/cli.d.mts +1 -0
- package/dist/cli.mjs +221 -0
- package/dist/index.cjs +212 -0
- package/dist/index.d.cts +52 -0
- package/dist/index.d.mts +52 -0
- package/dist/index.mjs +207 -0
- package/dist/reg.wasm +0 -0
- package/dist/runner.cjs +228 -0
- package/dist/runner.d.cts +1 -0
- package/dist/runner.d.mts +1 -0
- package/dist/runner.mjs +228 -0
- package/dist/shared/report-worker.js +1766 -0
- package/{template → dist/shared}/worker_pre.js +0 -0
- package/dist/tracing-Bo38b9aR.mjs +422 -0
- package/dist/tracing-Cf9TNPXQ.cjs +543 -0
- package/dist/ximgdiff-B16oe7EL.cjs +41 -0
- package/dist/ximgdiff-D8GAJaxA.mjs +41 -0
- package/package.json +79 -113
- package/dist/.keep +0 -0
- package/dist/cli.js +0 -214
- package/dist/diff.js +0 -80
- package/dist/icon.js +0 -12
- package/dist/image-finder.js +0 -25
- package/dist/index.js +0 -200
- package/dist/log.js +0 -21
- package/dist/process-adaptor.js +0 -40
- package/dist/report.js +0 -170
- package/dist/tracing.js +0 -169
- package/report/assets/favicon_failure.png +0 -0
- package/report/assets/favicon_success.png +0 -0
- package/report/sample/actual/sample.png +0 -0
- package/report/sample/diff/sample.png +0 -0
- package/report/sample/expected/sample.png +0 -0
- package/report/ui/dist/report.js +0 -123
- package/report/ui/dist/style.css +0 -1
- package/report/ui/dist/worker.js +0 -1535
- package/template/template.html +0 -20
|
@@ -0,0 +1,543 @@
|
|
|
1
|
+
//#region \0rolldown/runtime.js
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __exportAll = (all, no_symbols) => {
|
|
9
|
+
let target = {};
|
|
10
|
+
for (var name in all) __defProp(target, name, {
|
|
11
|
+
get: all[name],
|
|
12
|
+
enumerable: true
|
|
13
|
+
});
|
|
14
|
+
if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
|
|
15
|
+
return target;
|
|
16
|
+
};
|
|
17
|
+
var __copyProps = (to, from, except, desc) => {
|
|
18
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
19
|
+
key = keys[i];
|
|
20
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
21
|
+
get: ((k) => from[k]).bind(null, key),
|
|
22
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
return to;
|
|
26
|
+
};
|
|
27
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
28
|
+
value: mod,
|
|
29
|
+
enumerable: true
|
|
30
|
+
}) : target, mod));
|
|
31
|
+
//#endregion
|
|
32
|
+
let path = require("path");
|
|
33
|
+
path = __toESM(path, 1);
|
|
34
|
+
let url = require("url");
|
|
35
|
+
let node_fs_promises = require("node:fs/promises");
|
|
36
|
+
let node_path = require("node:path");
|
|
37
|
+
let node_process = require("node:process");
|
|
38
|
+
let node_module = require("node:module");
|
|
39
|
+
//#region src/utils.ts
|
|
40
|
+
const isCJS = typeof __dirname !== "undefined";
|
|
41
|
+
const readWasm = () => {
|
|
42
|
+
return (0, node_fs_promises.readFile)((0, node_path.join)(isCJS ? __dirname : path.default.dirname((0, url.fileURLToPath)(require("url").pathToFileURL(__filename).href)), "./reg.wasm"));
|
|
43
|
+
};
|
|
44
|
+
const resolveExtention = () => {
|
|
45
|
+
return isCJS ? "cjs" : "mjs";
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* Restrict the WASI namespace passed to `WebAssembly.instantiate` to only the
|
|
49
|
+
* functions the wasm module actually imports. `@tybys/wasm-util` exposes the
|
|
50
|
+
* full WASI preview1 surface (~46 fns) but `reg.wasm` declares ~20. The unused
|
|
51
|
+
* 26 include capabilities reg-cli never needs (`path_unlink_file`,
|
|
52
|
+
* `path_remove_directory`, `sock_*`, `proc_raise`, ...). Withholding them
|
|
53
|
+
* shrinks the blast radius if the bundled wasm or one of its image-decoder
|
|
54
|
+
* dependencies (libpng/libwebp/libjpeg) is ever compromised.
|
|
55
|
+
*/
|
|
56
|
+
const filterWasiImports = (module, full) => {
|
|
57
|
+
const needed = new Set(WebAssembly.Module.imports(module).filter((i) => i.module === "wasi_snapshot_preview1").map((i) => i.name));
|
|
58
|
+
const filtered = {};
|
|
59
|
+
const src = full;
|
|
60
|
+
for (const name of needed) if (name in src) filtered[name] = src[name];
|
|
61
|
+
return filtered;
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* Host environment variables intentionally forwarded into the Wasm sandbox.
|
|
65
|
+
* Everything else (shell secrets, CI credentials, AWS_*, NPM_TOKEN, ...) is
|
|
66
|
+
* filtered out.
|
|
67
|
+
*/
|
|
68
|
+
const FORWARDED_ENV = [
|
|
69
|
+
"OTEL_ENABLED",
|
|
70
|
+
"JAEGER_ENABLED",
|
|
71
|
+
"OTEL_DEBUG",
|
|
72
|
+
"OTEL_EXPORTER_OTLP_ENDPOINT",
|
|
73
|
+
"OTEL_EXPORTER_OTLP_PROTOCOL",
|
|
74
|
+
"OTEL_SERVICE_NAME",
|
|
75
|
+
"OTEL_TRACES_EXPORTER",
|
|
76
|
+
"OTEL_RESOURCE_ATTRIBUTES"
|
|
77
|
+
];
|
|
78
|
+
/**
|
|
79
|
+
* Longest directory path that every input in `paths` lives under. Treats
|
|
80
|
+
* each path as a sequence of "/"-separated segments; absolute-vs-relative
|
|
81
|
+
* mixes fall back to '.'.
|
|
82
|
+
*/
|
|
83
|
+
const commonAncestor = (paths) => {
|
|
84
|
+
const defined = paths.filter(Boolean);
|
|
85
|
+
if (defined.length === 0) return ".";
|
|
86
|
+
if (defined.length === 1) return defined[0];
|
|
87
|
+
const isAbs = (p) => p.startsWith("/");
|
|
88
|
+
const anyAbs = defined.some(isAbs);
|
|
89
|
+
const allAbs = defined.every(isAbs);
|
|
90
|
+
if (anyAbs && !allAbs) return ".";
|
|
91
|
+
const split = defined.map((p) => p.split("/").filter(Boolean));
|
|
92
|
+
const shortest = Math.min(...split.map((s) => s.length));
|
|
93
|
+
const common = [];
|
|
94
|
+
for (let i = 0; i < shortest; i++) {
|
|
95
|
+
const seg = split[0][i];
|
|
96
|
+
if (split.every((s) => s[i] === seg)) common.push(seg);
|
|
97
|
+
else break;
|
|
98
|
+
}
|
|
99
|
+
if (common.length === 0) return allAbs ? "/" : ".";
|
|
100
|
+
return (allAbs ? "/" : "") + common.join("/");
|
|
101
|
+
};
|
|
102
|
+
const WASI_PATH_FLAGS = new Set([
|
|
103
|
+
"--report",
|
|
104
|
+
"-R",
|
|
105
|
+
"--json",
|
|
106
|
+
"-J",
|
|
107
|
+
"--junit",
|
|
108
|
+
"--from",
|
|
109
|
+
"-F"
|
|
110
|
+
]);
|
|
111
|
+
/**
|
|
112
|
+
* Normalize `./foo` to `foo` for paths passed into WASI. wasm-util does not
|
|
113
|
+
* match a guest path beginning with `./` to an otherwise equivalent relative
|
|
114
|
+
* preopen, so leaving the prefix intact makes valid paths unreachable.
|
|
115
|
+
*/
|
|
116
|
+
const normalizeWasiArgv = (argv) => {
|
|
117
|
+
const normalized = [...argv];
|
|
118
|
+
const stripDot = (value) => value.startsWith("./") ? value.slice(2) || "." : value;
|
|
119
|
+
const offset = normalized[0] === "--" ? 1 : 0;
|
|
120
|
+
for (let i = offset; i < normalized.length && i < offset + 3; i++) {
|
|
121
|
+
if (normalized[i].startsWith("-")) break;
|
|
122
|
+
normalized[i] = stripDot(normalized[i]);
|
|
123
|
+
}
|
|
124
|
+
for (let i = offset; i < normalized.length - 1; i++) if (WASI_PATH_FLAGS.has(normalized[i])) {
|
|
125
|
+
normalized[i + 1] = stripDot(normalized[i + 1]);
|
|
126
|
+
i++;
|
|
127
|
+
}
|
|
128
|
+
return normalized;
|
|
129
|
+
};
|
|
130
|
+
/**
|
|
131
|
+
* Compute the minimum-capability WASI sandbox for a given reg-cli invocation:
|
|
132
|
+
*
|
|
133
|
+
* - preopen only the smallest ancestor directory that covers every path
|
|
134
|
+
* this run touches (actualDir, expectedDir, diffDir, and the parents
|
|
135
|
+
* of --report / --json)
|
|
136
|
+
* - forward only an allowlisted subset of host env into Wasm
|
|
137
|
+
*
|
|
138
|
+
* Before this, entry.ts / worker.ts did `preopens: { './': './' }` which
|
|
139
|
+
* exposed the entire cwd (and therefore `.npmrc`, `.env`, `node_modules`,
|
|
140
|
+
* etc.) to whatever runs inside Wasm. Narrowing matters because reg-cli
|
|
141
|
+
* deliberately ingests untrusted images from CI and image decoders
|
|
142
|
+
* (libpng, libwebp, libjpeg) have a long history of RCE-class CVEs.
|
|
143
|
+
*
|
|
144
|
+
* Why a single common-ancestor and not one preopen per directory:
|
|
145
|
+
* On the `wasm32-wasip1-threads` target, Rust's libstd only enumerates
|
|
146
|
+
* the first preopen returned by WASI (`fd_prestat_get(3)` is queried but
|
|
147
|
+
* `fd_prestat_get(4)` is never issued). Until that's fixed upstream,
|
|
148
|
+
* registering one preopen per dir effectively hides every dir but the
|
|
149
|
+
* first. Picking the narrowest *single* ancestor that still contains
|
|
150
|
+
* every touched path gives us a real-world win without tripping the bug.
|
|
151
|
+
*
|
|
152
|
+
* Caveats (follow-ups, not in this PR):
|
|
153
|
+
* - Symlinks and FIFOs inside the preopened directory are still resolved
|
|
154
|
+
* by the host, so an attacker who can plant files under `./diff/` could
|
|
155
|
+
* still exfiltrate via an evil symlink pointing outside the sandbox.
|
|
156
|
+
* Addressing this needs host-side WASI changes.
|
|
157
|
+
* - `@tybys/wasm-util`'s WASI still runs on top of Node's `fs` with full
|
|
158
|
+
* privilege (`fs: fs as IFs`); we only constrain *what paths Wasm is
|
|
159
|
+
* allowed to name*, not what the host fs underneath could do.
|
|
160
|
+
* - One-preopen-per-dir limitation above.
|
|
161
|
+
*/
|
|
162
|
+
const computeWasiSandbox = (argv) => {
|
|
163
|
+
const args = argv[0] === "--" ? argv.slice(1) : argv;
|
|
164
|
+
const positional = [];
|
|
165
|
+
for (let i = 0; i < args.length && positional.length < 3; i++) {
|
|
166
|
+
if (args[i].startsWith("--")) break;
|
|
167
|
+
positional.push(args[i]);
|
|
168
|
+
}
|
|
169
|
+
const flagValue = (name) => {
|
|
170
|
+
const i = args.indexOf(name);
|
|
171
|
+
return i >= 0 && i + 1 < args.length ? args[i + 1] : void 0;
|
|
172
|
+
};
|
|
173
|
+
const dirs = [];
|
|
174
|
+
for (const p of positional) if (p) dirs.push(p);
|
|
175
|
+
const report = flagValue("--report");
|
|
176
|
+
const json = flagValue("--json");
|
|
177
|
+
const junit = flagValue("--junit");
|
|
178
|
+
const from = flagValue("--from") ?? flagValue("-F");
|
|
179
|
+
if (report) dirs.push((0, node_path.dirname)(report) || ".");
|
|
180
|
+
if (json) dirs.push((0, node_path.dirname)(json) || ".");
|
|
181
|
+
if (junit) dirs.push((0, node_path.dirname)(junit) || ".");
|
|
182
|
+
if (from) dirs.push((0, node_path.dirname)(from) || ".");
|
|
183
|
+
const preopenRoot = commonAncestor(dirs) || ".";
|
|
184
|
+
const mapWasm = (p) => p === "." || p === "./" || p.startsWith("/") || p.startsWith("./") ? p : `./${p}`;
|
|
185
|
+
const preopens = { [mapWasm(preopenRoot)]: preopenRoot };
|
|
186
|
+
if (!preopenRoot || preopenRoot === "." || preopenRoot === "./") preopens["./"] = "./";
|
|
187
|
+
const env = {};
|
|
188
|
+
for (const k of FORWARDED_ENV) {
|
|
189
|
+
const v = node_process.env[k];
|
|
190
|
+
if (v !== void 0) env[k] = v;
|
|
191
|
+
}
|
|
192
|
+
return {
|
|
193
|
+
preopens,
|
|
194
|
+
env
|
|
195
|
+
};
|
|
196
|
+
};
|
|
197
|
+
//#endregion
|
|
198
|
+
//#region src/tracing.ts
|
|
199
|
+
/**
|
|
200
|
+
* OpenTelemetry tracing utilities for reg-cli
|
|
201
|
+
*
|
|
202
|
+
* This module receives trace data from the Rust/WASM side and converts it
|
|
203
|
+
* to OpenTelemetry spans that can be exported to Jaeger or other backends.
|
|
204
|
+
*
|
|
205
|
+
* The @opentelemetry/* packages are declared as **optional peer
|
|
206
|
+
* dependencies** — they are loaded lazily inside `initTracing()` only when
|
|
207
|
+
* `OTEL_ENABLED=true` (or `JAEGER_ENABLED=true`). Default installs of this
|
|
208
|
+
* package therefore pay zero install or runtime cost for OTel.
|
|
209
|
+
*/
|
|
210
|
+
const PKG_VERSION = (() => {
|
|
211
|
+
try {
|
|
212
|
+
return (0, node_module.createRequire)(require("url").pathToFileURL(__filename).href)("../package.json").version ?? "unknown";
|
|
213
|
+
} catch {
|
|
214
|
+
return "unknown";
|
|
215
|
+
}
|
|
216
|
+
})();
|
|
217
|
+
let otel = null;
|
|
218
|
+
let provider = null;
|
|
219
|
+
let isInitialized = false;
|
|
220
|
+
let currentRootSpan = null;
|
|
221
|
+
let currentRootContext = null;
|
|
222
|
+
/**
|
|
223
|
+
* Check if tracing is enabled via environment variable
|
|
224
|
+
*/
|
|
225
|
+
const isTracingEnabled = () => {
|
|
226
|
+
return process.env.OTEL_ENABLED === "true" || process.env.JAEGER_ENABLED === "true";
|
|
227
|
+
};
|
|
228
|
+
/**
|
|
229
|
+
* Lazily import the @opentelemetry/* packages. Returns null and prints a
|
|
230
|
+
* single warning if any are missing — keeps the CLI working without OTel
|
|
231
|
+
* installed even when the env vars are set.
|
|
232
|
+
*/
|
|
233
|
+
const loadOtel = async () => {
|
|
234
|
+
if (otel) return otel;
|
|
235
|
+
try {
|
|
236
|
+
const [api, resources, sdkBase, sdkNode, exporter, semconv] = await Promise.all([
|
|
237
|
+
import("@opentelemetry/api"),
|
|
238
|
+
import("@opentelemetry/resources"),
|
|
239
|
+
import("@opentelemetry/sdk-trace-base"),
|
|
240
|
+
import("@opentelemetry/sdk-trace-node"),
|
|
241
|
+
import("@opentelemetry/exporter-trace-otlp-http"),
|
|
242
|
+
import("@opentelemetry/semantic-conventions")
|
|
243
|
+
]);
|
|
244
|
+
otel = {
|
|
245
|
+
api,
|
|
246
|
+
resources,
|
|
247
|
+
sdkBase,
|
|
248
|
+
sdkNode,
|
|
249
|
+
exporter,
|
|
250
|
+
semconv
|
|
251
|
+
};
|
|
252
|
+
return otel;
|
|
253
|
+
} catch (err) {
|
|
254
|
+
const pkgs = [
|
|
255
|
+
"@opentelemetry/api",
|
|
256
|
+
"@opentelemetry/exporter-trace-otlp-http",
|
|
257
|
+
"@opentelemetry/resources",
|
|
258
|
+
"@opentelemetry/sdk-trace-base",
|
|
259
|
+
"@opentelemetry/sdk-trace-node",
|
|
260
|
+
"@opentelemetry/semantic-conventions"
|
|
261
|
+
].join(" ");
|
|
262
|
+
console.warn(`[Tracing] OTEL_ENABLED is set but the @opentelemetry/* peer dependencies are not installed. Continuing without tracing. To enable, install the peers alongside reg-cli, e.g.:
|
|
263
|
+
npm i -g ${pkgs} # if reg-cli is installed globally\n npm i ${pkgs} # if reg-cli is a project dependency`);
|
|
264
|
+
if (process.env.OTEL_DEBUG === "true") console.warn("[Tracing] Import error:", err);
|
|
265
|
+
return null;
|
|
266
|
+
}
|
|
267
|
+
};
|
|
268
|
+
/**
|
|
269
|
+
* Initialize OpenTelemetry SDK
|
|
270
|
+
*/
|
|
271
|
+
const initTracing = async () => {
|
|
272
|
+
if (!isTracingEnabled() || isInitialized) return;
|
|
273
|
+
const mods = await loadOtel();
|
|
274
|
+
if (!mods) return;
|
|
275
|
+
const otlpEndpoint = process.env.OTEL_EXPORTER_OTLP_ENDPOINT || "http://localhost:4318/v1/traces";
|
|
276
|
+
const otlpExporter = new mods.exporter.OTLPTraceExporter({ url: otlpEndpoint });
|
|
277
|
+
provider = new mods.sdkNode.NodeTracerProvider({
|
|
278
|
+
resource: mods.resources.resourceFromAttributes({
|
|
279
|
+
[mods.semconv.ATTR_SERVICE_NAME]: "reg-cli",
|
|
280
|
+
[mods.semconv.ATTR_SERVICE_VERSION]: "0.18.10"
|
|
281
|
+
}),
|
|
282
|
+
spanProcessors: [new mods.sdkBase.BatchSpanProcessor(otlpExporter)]
|
|
283
|
+
});
|
|
284
|
+
provider.register();
|
|
285
|
+
isInitialized = true;
|
|
286
|
+
if (process.env.OTEL_DEBUG === "true") {
|
|
287
|
+
console.log("[Tracing] OpenTelemetry initialized");
|
|
288
|
+
console.log(`[Tracing] OTLP endpoint: ${otlpEndpoint}`);
|
|
289
|
+
}
|
|
290
|
+
};
|
|
291
|
+
/**
|
|
292
|
+
* Shutdown OpenTelemetry SDK gracefully
|
|
293
|
+
*/
|
|
294
|
+
const shutdownTracing = async () => {
|
|
295
|
+
if (!provider || !isInitialized) return;
|
|
296
|
+
if (process.env.OTEL_DEBUG === "true") console.log("[Tracing] Starting SDK shutdown...");
|
|
297
|
+
try {
|
|
298
|
+
await provider.shutdown();
|
|
299
|
+
} catch (err) {
|
|
300
|
+
console.error("[Tracing] Error during shutdown:", err);
|
|
301
|
+
}
|
|
302
|
+
isInitialized = false;
|
|
303
|
+
provider = null;
|
|
304
|
+
if (process.env.OTEL_DEBUG === "true") console.log("[Tracing] OpenTelemetry SDK shut down");
|
|
305
|
+
};
|
|
306
|
+
/**
|
|
307
|
+
* Get the tracer instance
|
|
308
|
+
*/
|
|
309
|
+
const getTracer = () => {
|
|
310
|
+
return otel.api.trace.getTracer("reg-cli", PKG_VERSION);
|
|
311
|
+
};
|
|
312
|
+
/**
|
|
313
|
+
* Start a root span and return context info to pass to Rust
|
|
314
|
+
*/
|
|
315
|
+
const startRootSpan = (name) => {
|
|
316
|
+
if (!isTracingEnabled() || !isInitialized || !otel) return null;
|
|
317
|
+
currentRootSpan = getTracer().startSpan(name);
|
|
318
|
+
currentRootContext = otel.api.trace.setSpan(otel.api.ROOT_CONTEXT, currentRootSpan);
|
|
319
|
+
const spanContext = currentRootSpan.spanContext();
|
|
320
|
+
if (process.env.OTEL_DEBUG === "true") {
|
|
321
|
+
console.log(`[Tracing] Started root span: ${name}`);
|
|
322
|
+
console.log(`[Tracing] Trace ID: ${spanContext.traceId}`);
|
|
323
|
+
console.log(`[Tracing] Span ID: ${spanContext.spanId}`);
|
|
324
|
+
}
|
|
325
|
+
return {
|
|
326
|
+
traceId: spanContext.traceId,
|
|
327
|
+
spanId: spanContext.spanId
|
|
328
|
+
};
|
|
329
|
+
};
|
|
330
|
+
/**
|
|
331
|
+
* End the current root span
|
|
332
|
+
*/
|
|
333
|
+
const endRootSpan = (success = true) => {
|
|
334
|
+
if (currentRootSpan && otel) {
|
|
335
|
+
if (success) currentRootSpan.setStatus({ code: otel.api.SpanStatusCode.OK });
|
|
336
|
+
else currentRootSpan.setStatus({ code: otel.api.SpanStatusCode.ERROR });
|
|
337
|
+
currentRootSpan.end();
|
|
338
|
+
if (process.env.OTEL_DEBUG === "true") console.log("[Tracing] Ended root span");
|
|
339
|
+
currentRootSpan = null;
|
|
340
|
+
currentRootContext = null;
|
|
341
|
+
}
|
|
342
|
+
};
|
|
343
|
+
/**
|
|
344
|
+
* Topologically sort spans so parents are always processed before children
|
|
345
|
+
*/
|
|
346
|
+
const topologicalSortSpans = (spans) => {
|
|
347
|
+
const spanMap = /* @__PURE__ */ new Map();
|
|
348
|
+
const childrenMap = /* @__PURE__ */ new Map();
|
|
349
|
+
const rootSpans = [];
|
|
350
|
+
for (const span of spans) {
|
|
351
|
+
spanMap.set(span.span_id, span);
|
|
352
|
+
if (!span.parent_span_id) rootSpans.push(span.span_id);
|
|
353
|
+
else {
|
|
354
|
+
const children = childrenMap.get(span.parent_span_id) || [];
|
|
355
|
+
children.push(span.span_id);
|
|
356
|
+
childrenMap.set(span.parent_span_id, children);
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
if (process.env.OTEL_DEBUG === "true") {
|
|
360
|
+
console.log(`[Tracing] Total spans: ${spans.length}, Unique IDs in spanMap: ${spanMap.size}`);
|
|
361
|
+
console.log(`[Tracing] Root spans: ${rootSpans.join(", ")}`);
|
|
362
|
+
console.log(`[Tracing] All spans:`);
|
|
363
|
+
for (const span of spans) console.log(` ${span.span_id}: ${span.name} (parent: ${span.parent_span_id || "none"})`);
|
|
364
|
+
console.log(`[Tracing] Parent->Children map:`);
|
|
365
|
+
for (const [parent, children] of childrenMap.entries()) console.log(` ${parent} -> ${children.join(", ")}`);
|
|
366
|
+
}
|
|
367
|
+
const result = [];
|
|
368
|
+
const queue = [...rootSpans];
|
|
369
|
+
const visited = /* @__PURE__ */ new Set();
|
|
370
|
+
while (queue.length > 0) {
|
|
371
|
+
const spanId = queue.shift();
|
|
372
|
+
if (visited.has(spanId)) continue;
|
|
373
|
+
visited.add(spanId);
|
|
374
|
+
const span = spanMap.get(spanId);
|
|
375
|
+
if (span) {
|
|
376
|
+
result.push(span);
|
|
377
|
+
const children = childrenMap.get(spanId) || [];
|
|
378
|
+
queue.push(...children);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
for (const span of spans) if (!visited.has(span.span_id)) {
|
|
382
|
+
if (process.env.OTEL_DEBUG === "true") console.log(`[Tracing] Orphan span: ${span.name} (id: ${span.span_id}, parent: ${span.parent_span_id})`);
|
|
383
|
+
result.push(span);
|
|
384
|
+
}
|
|
385
|
+
return result;
|
|
386
|
+
};
|
|
387
|
+
/**
|
|
388
|
+
* Convert Rust trace data to OpenTelemetry spans
|
|
389
|
+
*
|
|
390
|
+
* Since Rust spans have already ended by the time we receive them,
|
|
391
|
+
* we reconstruct them with their recorded timestamps.
|
|
392
|
+
*/
|
|
393
|
+
const processRustTraceData = (traceData) => {
|
|
394
|
+
if (!isTracingEnabled() || !isInitialized || !otel) return;
|
|
395
|
+
const tracer = getTracer();
|
|
396
|
+
if (process.env.OTEL_DEBUG === "true") {
|
|
397
|
+
console.log(`[Tracing] Processing ${traceData.spans.length} spans from Rust`);
|
|
398
|
+
console.log(`[Tracing] JS parent context available: ${currentRootContext !== null}`);
|
|
399
|
+
}
|
|
400
|
+
const sortedSpans = topologicalSortSpans(traceData.spans);
|
|
401
|
+
const spanContextMap = /* @__PURE__ */ new Map();
|
|
402
|
+
for (const rustSpan of sortedSpans) {
|
|
403
|
+
const startTimeNs = rustSpan.start_time_ms * 1e6;
|
|
404
|
+
const endTimeNs = rustSpan.end_time_ms * 1e6;
|
|
405
|
+
let parentContext;
|
|
406
|
+
let parentInfo;
|
|
407
|
+
if (rustSpan.parent_span_id && spanContextMap.has(rustSpan.parent_span_id)) {
|
|
408
|
+
parentContext = spanContextMap.get(rustSpan.parent_span_id);
|
|
409
|
+
parentInfo = `rust parent: ${rustSpan.parent_span_id}`;
|
|
410
|
+
} else if (!rustSpan.parent_span_id && currentRootContext) {
|
|
411
|
+
parentContext = currentRootContext;
|
|
412
|
+
parentInfo = `js parent: ${currentRootSpan?.spanContext().spanId}`;
|
|
413
|
+
} else {
|
|
414
|
+
parentContext = currentRootContext || otel.api.ROOT_CONTEXT;
|
|
415
|
+
parentInfo = rustSpan.parent_span_id ? `orphan (parent ${rustSpan.parent_span_id} not found)` : "root (no js context)";
|
|
416
|
+
}
|
|
417
|
+
const span = tracer.startSpan(rustSpan.name, { startTime: [Math.floor(startTimeNs / 1e9), startTimeNs % 1e9] }, parentContext);
|
|
418
|
+
span.setAttribute("rust.target", rustSpan.target);
|
|
419
|
+
span.setAttribute("rust.level", rustSpan.level);
|
|
420
|
+
span.setAttribute("rust.span_id", rustSpan.span_id);
|
|
421
|
+
span.setAttribute("duration_ms", rustSpan.duration_ms);
|
|
422
|
+
for (const [key, value] of Object.entries(rustSpan.attributes)) span.setAttribute(`rust.${key}`, value);
|
|
423
|
+
if (rustSpan.status === "error") span.setStatus({
|
|
424
|
+
code: otel.api.SpanStatusCode.ERROR,
|
|
425
|
+
message: rustSpan.error_message || "Unknown error"
|
|
426
|
+
});
|
|
427
|
+
else span.setStatus({ code: otel.api.SpanStatusCode.OK });
|
|
428
|
+
span.end([Math.floor(endTimeNs / 1e9), endTimeNs % 1e9]);
|
|
429
|
+
const ctx = otel.api.trace.setSpan(parentContext, span);
|
|
430
|
+
spanContextMap.set(rustSpan.span_id, ctx);
|
|
431
|
+
if (process.env.OTEL_DEBUG === "true") console.log(`[Tracing] Created span: ${rustSpan.name} (${rustSpan.duration_ms}ms) [${parentInfo}] start=${rustSpan.start_time_ms}, end=${rustSpan.end_time_ms}`);
|
|
432
|
+
}
|
|
433
|
+
};
|
|
434
|
+
/**
|
|
435
|
+
* Convert worker-side timing events to OpenTelemetry spans attached under the
|
|
436
|
+
* current root span. Used to surface JS-bridge costs (Worker creation, wasm
|
|
437
|
+
* compile / instantiate, message round-trips) that Rust-side tracing cannot see.
|
|
438
|
+
*/
|
|
439
|
+
const processWorkerSpans = (spans, workerLabel) => {
|
|
440
|
+
if (!isTracingEnabled() || !isInitialized || !otel || !spans?.length) return;
|
|
441
|
+
const tracer = getTracer();
|
|
442
|
+
const parentCtx = currentRootContext ?? otel.api.ROOT_CONTEXT;
|
|
443
|
+
for (const s of spans) {
|
|
444
|
+
const startNs = s.start_ms * 1e6;
|
|
445
|
+
const endNs = s.end_ms * 1e6;
|
|
446
|
+
const span = tracer.startSpan(s.name, { startTime: [Math.floor(startNs / 1e9), startNs % 1e9] }, parentCtx);
|
|
447
|
+
span.setAttribute("worker", s.worker_label ?? workerLabel);
|
|
448
|
+
span.setAttribute("duration_ms", s.end_ms - s.start_ms);
|
|
449
|
+
if (s.attributes) for (const [k, v] of Object.entries(s.attributes)) span.setAttribute(k, v);
|
|
450
|
+
span.end([Math.floor(endNs / 1e9), endNs % 1e9]);
|
|
451
|
+
}
|
|
452
|
+
};
|
|
453
|
+
//#endregion
|
|
454
|
+
Object.defineProperty(exports, "__exportAll", {
|
|
455
|
+
enumerable: true,
|
|
456
|
+
get: function() {
|
|
457
|
+
return __exportAll;
|
|
458
|
+
}
|
|
459
|
+
});
|
|
460
|
+
Object.defineProperty(exports, "__toESM", {
|
|
461
|
+
enumerable: true,
|
|
462
|
+
get: function() {
|
|
463
|
+
return __toESM;
|
|
464
|
+
}
|
|
465
|
+
});
|
|
466
|
+
Object.defineProperty(exports, "computeWasiSandbox", {
|
|
467
|
+
enumerable: true,
|
|
468
|
+
get: function() {
|
|
469
|
+
return computeWasiSandbox;
|
|
470
|
+
}
|
|
471
|
+
});
|
|
472
|
+
Object.defineProperty(exports, "endRootSpan", {
|
|
473
|
+
enumerable: true,
|
|
474
|
+
get: function() {
|
|
475
|
+
return endRootSpan;
|
|
476
|
+
}
|
|
477
|
+
});
|
|
478
|
+
Object.defineProperty(exports, "filterWasiImports", {
|
|
479
|
+
enumerable: true,
|
|
480
|
+
get: function() {
|
|
481
|
+
return filterWasiImports;
|
|
482
|
+
}
|
|
483
|
+
});
|
|
484
|
+
Object.defineProperty(exports, "initTracing", {
|
|
485
|
+
enumerable: true,
|
|
486
|
+
get: function() {
|
|
487
|
+
return initTracing;
|
|
488
|
+
}
|
|
489
|
+
});
|
|
490
|
+
Object.defineProperty(exports, "isCJS", {
|
|
491
|
+
enumerable: true,
|
|
492
|
+
get: function() {
|
|
493
|
+
return isCJS;
|
|
494
|
+
}
|
|
495
|
+
});
|
|
496
|
+
Object.defineProperty(exports, "isTracingEnabled", {
|
|
497
|
+
enumerable: true,
|
|
498
|
+
get: function() {
|
|
499
|
+
return isTracingEnabled;
|
|
500
|
+
}
|
|
501
|
+
});
|
|
502
|
+
Object.defineProperty(exports, "normalizeWasiArgv", {
|
|
503
|
+
enumerable: true,
|
|
504
|
+
get: function() {
|
|
505
|
+
return normalizeWasiArgv;
|
|
506
|
+
}
|
|
507
|
+
});
|
|
508
|
+
Object.defineProperty(exports, "processRustTraceData", {
|
|
509
|
+
enumerable: true,
|
|
510
|
+
get: function() {
|
|
511
|
+
return processRustTraceData;
|
|
512
|
+
}
|
|
513
|
+
});
|
|
514
|
+
Object.defineProperty(exports, "processWorkerSpans", {
|
|
515
|
+
enumerable: true,
|
|
516
|
+
get: function() {
|
|
517
|
+
return processWorkerSpans;
|
|
518
|
+
}
|
|
519
|
+
});
|
|
520
|
+
Object.defineProperty(exports, "readWasm", {
|
|
521
|
+
enumerable: true,
|
|
522
|
+
get: function() {
|
|
523
|
+
return readWasm;
|
|
524
|
+
}
|
|
525
|
+
});
|
|
526
|
+
Object.defineProperty(exports, "resolveExtention", {
|
|
527
|
+
enumerable: true,
|
|
528
|
+
get: function() {
|
|
529
|
+
return resolveExtention;
|
|
530
|
+
}
|
|
531
|
+
});
|
|
532
|
+
Object.defineProperty(exports, "shutdownTracing", {
|
|
533
|
+
enumerable: true,
|
|
534
|
+
get: function() {
|
|
535
|
+
return shutdownTracing;
|
|
536
|
+
}
|
|
537
|
+
});
|
|
538
|
+
Object.defineProperty(exports, "startRootSpan", {
|
|
539
|
+
enumerable: true,
|
|
540
|
+
get: function() {
|
|
541
|
+
return startRootSpan;
|
|
542
|
+
}
|
|
543
|
+
});
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
const require_tracing = require("./tracing-Cf9TNPXQ.cjs");
|
|
2
|
+
let node_fs_promises = require("node:fs/promises");
|
|
3
|
+
let node_path = require("node:path");
|
|
4
|
+
let node_module = require("node:module");
|
|
5
|
+
//#region src/ximgdiff.ts
|
|
6
|
+
var ximgdiff_exports = /* @__PURE__ */ require_tracing.__exportAll({ writeXimgdiffAssets: () => writeXimgdiffAssets });
|
|
7
|
+
const requireShim = (0, node_module.createRequire)(require("url").pathToFileURL(__filename).href);
|
|
8
|
+
const renderWorkerPre = (template, wasmUrl) => template.replace(/\{\{&\s*ximgdiffWasmUrl\s*\}\}/g, wasmUrl);
|
|
9
|
+
/**
|
|
10
|
+
* Concatenate worker_pre.js + report-ui worker + x-img-diff-js loader and
|
|
11
|
+
* write the result + the wasm binary next to the HTML report. Safe to call
|
|
12
|
+
* whether or not the report dir already exists.
|
|
13
|
+
*/
|
|
14
|
+
const writeXimgdiffAssets = async (opts) => {
|
|
15
|
+
const outDir = (0, node_path.dirname)(opts.reportPath);
|
|
16
|
+
const wasmUrl = `${opts.urlPrefix ?? ""}detector.wasm`;
|
|
17
|
+
const xImgDiff = requireShim("x-img-diff-js");
|
|
18
|
+
const sharedDir = (0, node_path.join)(opts.distDir, "shared");
|
|
19
|
+
const [workerPreTpl, reportWorkerJs, wasmLoaderJs, wasmBuf] = await Promise.all([
|
|
20
|
+
(0, node_fs_promises.readFile)((0, node_path.join)(sharedDir, "worker_pre.js"), "utf8"),
|
|
21
|
+
(0, node_fs_promises.readFile)((0, node_path.join)(sharedDir, "report-worker.js"), "utf8"),
|
|
22
|
+
(0, node_fs_promises.readFile)(xImgDiff.getBrowserJsPath(), "utf8"),
|
|
23
|
+
(0, node_fs_promises.readFile)(xImgDiff.getBrowserWasmPath())
|
|
24
|
+
]);
|
|
25
|
+
const workerJs = renderWorkerPre(workerPreTpl, wasmUrl) + "\n" + reportWorkerJs + "\n" + wasmLoaderJs;
|
|
26
|
+
await (0, node_fs_promises.mkdir)(outDir, { recursive: true });
|
|
27
|
+
await Promise.all([(0, node_fs_promises.writeFile)((0, node_path.join)(outDir, "worker.js"), workerJs, "utf8"), (0, node_fs_promises.writeFile)((0, node_path.join)(outDir, "detector.wasm"), wasmBuf)]);
|
|
28
|
+
};
|
|
29
|
+
//#endregion
|
|
30
|
+
Object.defineProperty(exports, "writeXimgdiffAssets", {
|
|
31
|
+
enumerable: true,
|
|
32
|
+
get: function() {
|
|
33
|
+
return writeXimgdiffAssets;
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
Object.defineProperty(exports, "ximgdiff_exports", {
|
|
37
|
+
enumerable: true,
|
|
38
|
+
get: function() {
|
|
39
|
+
return ximgdiff_exports;
|
|
40
|
+
}
|
|
41
|
+
});
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
3
|
+
import { dirname, join } from "node:path";
|
|
4
|
+
//#region \0rolldown/runtime.js
|
|
5
|
+
var __defProp = Object.defineProperty;
|
|
6
|
+
var __exportAll = (all, no_symbols) => {
|
|
7
|
+
let target = {};
|
|
8
|
+
for (var name in all) __defProp(target, name, {
|
|
9
|
+
get: all[name],
|
|
10
|
+
enumerable: true
|
|
11
|
+
});
|
|
12
|
+
if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
|
|
13
|
+
return target;
|
|
14
|
+
};
|
|
15
|
+
//#endregion
|
|
16
|
+
//#region src/ximgdiff.ts
|
|
17
|
+
var ximgdiff_exports = /* @__PURE__ */ __exportAll({ writeXimgdiffAssets: () => writeXimgdiffAssets });
|
|
18
|
+
const requireShim = createRequire(import.meta.url);
|
|
19
|
+
const renderWorkerPre = (template, wasmUrl) => template.replace(/\{\{&\s*ximgdiffWasmUrl\s*\}\}/g, wasmUrl);
|
|
20
|
+
/**
|
|
21
|
+
* Concatenate worker_pre.js + report-ui worker + x-img-diff-js loader and
|
|
22
|
+
* write the result + the wasm binary next to the HTML report. Safe to call
|
|
23
|
+
* whether or not the report dir already exists.
|
|
24
|
+
*/
|
|
25
|
+
const writeXimgdiffAssets = async (opts) => {
|
|
26
|
+
const outDir = dirname(opts.reportPath);
|
|
27
|
+
const wasmUrl = `${opts.urlPrefix ?? ""}detector.wasm`;
|
|
28
|
+
const xImgDiff = requireShim("x-img-diff-js");
|
|
29
|
+
const sharedDir = join(opts.distDir, "shared");
|
|
30
|
+
const [workerPreTpl, reportWorkerJs, wasmLoaderJs, wasmBuf] = await Promise.all([
|
|
31
|
+
readFile(join(sharedDir, "worker_pre.js"), "utf8"),
|
|
32
|
+
readFile(join(sharedDir, "report-worker.js"), "utf8"),
|
|
33
|
+
readFile(xImgDiff.getBrowserJsPath(), "utf8"),
|
|
34
|
+
readFile(xImgDiff.getBrowserWasmPath())
|
|
35
|
+
]);
|
|
36
|
+
const workerJs = renderWorkerPre(workerPreTpl, wasmUrl) + "\n" + reportWorkerJs + "\n" + wasmLoaderJs;
|
|
37
|
+
await mkdir(outDir, { recursive: true });
|
|
38
|
+
await Promise.all([writeFile(join(outDir, "worker.js"), workerJs, "utf8"), writeFile(join(outDir, "detector.wasm"), wasmBuf)]);
|
|
39
|
+
};
|
|
40
|
+
//#endregion
|
|
41
|
+
export { ximgdiff_exports as n, writeXimgdiffAssets as t };
|