tracedye 0.0.0-stage → 0.1.1
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 +21 -0
- package/README.md +71 -2
- package/dist/capture/filter.js +24 -0
- package/dist/capture/forward.js +63 -0
- package/dist/capture/loader.js +39 -0
- package/dist/capture/otel.js +33 -0
- package/dist/capture/redact.js +140 -0
- package/dist/capture/register.js +12 -0
- package/dist/capture/root.js +16 -0
- package/dist/capture/runtime.js +360 -0
- package/dist/capture/transform.js +145 -0
- package/dist/cli.js +882 -0
- package/dist/dashboard/assets/geist-cyrillic-ext-wght-normal-DjL33-gN.woff2 +0 -0
- package/dist/dashboard/assets/geist-cyrillic-wght-normal-BEAKL7Jp.woff2 +0 -0
- package/dist/dashboard/assets/geist-latin-ext-wght-normal-DC-KSUi6.woff2 +0 -0
- package/dist/dashboard/assets/geist-latin-wght-normal-BgDaEnEv.woff2 +0 -0
- package/dist/dashboard/assets/geist-mono-cyrillic-ext-wght-normal-X_5orZeX.woff2 +0 -0
- package/dist/dashboard/assets/geist-mono-cyrillic-wght-normal-DiZS0aHC.woff2 +0 -0
- package/dist/dashboard/assets/geist-mono-latin-ext-wght-normal-Bwz-egvJ.woff2 +0 -0
- package/dist/dashboard/assets/geist-mono-latin-wght-normal-XN7g48iV.woff2 +0 -0
- package/dist/dashboard/assets/geist-mono-symbols2-wght-normal-CO5SzqOn.woff2 +0 -0
- package/dist/dashboard/assets/geist-mono-vietnamese-wght-normal-DadHysG0.woff2 +0 -0
- package/dist/dashboard/assets/geist-vietnamese-wght-normal-6IgcOCM7.woff2 +0 -0
- package/dist/dashboard/assets/index-BEUM-wup.js +15 -0
- package/dist/dashboard/assets/index-BMF_iKU2.css +1 -0
- package/dist/dashboard/assets/instrument-serif-latin-400-normal-BVbkICAY.woff +0 -0
- package/dist/dashboard/assets/instrument-serif-latin-400-normal-DnYpCC2O.woff2 +0 -0
- package/dist/dashboard/assets/instrument-serif-latin-ext-400-normal-C2je3j2s.woff2 +0 -0
- package/dist/dashboard/assets/instrument-serif-latin-ext-400-normal-CFCUzsTy.woff +0 -0
- package/dist/dashboard/index.html +15 -0
- package/dist/indexer.js +155 -0
- package/dist/preload/otel.mjs +25 -0
- package/dist/preload/start.mjs +11 -0
- package/package.json +52 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Sanjay
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,72 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Tracedye
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
See one real request move through your TypeScript app, with the payload visible at every hop and every function call, so you don't have to scatter `console.log`.
|
|
4
|
+
|
|
5
|
+
Arm a trigger, use your app, and watch that exact request travel: packets along the edges of a map of your code, the data going into and out of each hop, the function call stack with timings, and a replay of every request you captured.
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npx tracedye dev -- tsx src/server.ts
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
That one command starts a local collector and dashboard, builds a map of your code, and runs your app with capture loaded. Open http://localhost:5174, name a trigger, press **Arm**, and use your app.
|
|
12
|
+
|
|
13
|
+
## What it works with
|
|
14
|
+
|
|
15
|
+
Tracedye supports **TypeScript on Node with Express**. It is not a general tool for any codebase.
|
|
16
|
+
|
|
17
|
+
- Node 22.13 or newer. (Tracedye keeps its data in Node's built-in SQLite, so nothing is compiled when you install it.)
|
|
18
|
+
- Your app run from source with [`tsx`](https://tsx.is). A built `dist/` folder is not traced.
|
|
19
|
+
- ES modules (`"type": "module"`). Capture compiles your TypeScript as ES modules, so a CommonJS project may fail to load.
|
|
20
|
+
- Postgres (`pg`) and Redis (`ioredis`) calls are traced automatically. Other databases show up only as the functions that call them.
|
|
21
|
+
|
|
22
|
+
## Install
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
npm install --save-dev tracedye
|
|
26
|
+
npx tracedye init # adds a "dev:trace" script and ignores .tracedye/
|
|
27
|
+
npm run dev:trace
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
`tracedye init` only adds a `dev:trace` script to your `package.json` (it runs your `dev` script through Tracedye) and a `.tracedye/` line to `.gitignore`. `tracedye dev` changes nothing in your project; its data lives in `.tracedye/`.
|
|
31
|
+
|
|
32
|
+
## Commands
|
|
33
|
+
|
|
34
|
+
```
|
|
35
|
+
tracedye dev [options] -- <command that starts your app>
|
|
36
|
+
tracedye init [--dir <folder>] [--script <name>]
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
| Option | Meaning |
|
|
40
|
+
| --- | --- |
|
|
41
|
+
| `--dir <folder>` | project folder (default: the current one) |
|
|
42
|
+
| `--tsconfig <file>` | tsconfig used to build the map (default `tsconfig.json`) |
|
|
43
|
+
| `--arm <name>` | arm a trigger at start-up |
|
|
44
|
+
| `--port <n>` | collector port (default 4318) |
|
|
45
|
+
| `--dashboard-port <n>` | dashboard port (default 5174) |
|
|
46
|
+
| `--data-dir <folder>` | where the collector keeps its database |
|
|
47
|
+
| `--no-dashboard` | do not start the dashboard |
|
|
48
|
+
| `--no-otel` | do not trace Postgres and Redis |
|
|
49
|
+
| `--no-index` | do not build the static map |
|
|
50
|
+
| `--open` | open the dashboard in your browser |
|
|
51
|
+
|
|
52
|
+
Everything after `--` is your own command, including its own flags.
|
|
53
|
+
|
|
54
|
+
## How it stays safe
|
|
55
|
+
|
|
56
|
+
- **Dev only.** Tracedye refuses to start when `NODE_ENV=production`.
|
|
57
|
+
- **Armed triggers only.** Until you arm a trigger, your functions run exactly as they would without Tracedye.
|
|
58
|
+
- **Redacted in your process.** Arguments and return values are redacted (`password`, `token`, `authorization`, `cookie`, `secret` and anything containing them) and size-capped before anything leaves your app. Nothing unredacted is stored or logged.
|
|
59
|
+
- **Edges say where they came from.** An edge is *observed* only if a runtime event showed that call. Calls found only by reading your code are drawn as *static*, never as observed.
|
|
60
|
+
|
|
61
|
+
## Limits
|
|
62
|
+
|
|
63
|
+
- **Armed capture can be slow.** On a large app with big payloads, an armed request was about 37 times slower (11 ms to 409 ms on one Directus request). Arm it only while you test.
|
|
64
|
+
- **Classes are not covered.** Only top-level named functions and `const` arrow functions are mapped and traced, not class methods or anonymous callbacks.
|
|
65
|
+
- **Slow to index big repos.** The map of a 1,250-file app took about 2.5 minutes. Your app starts straight away and the map arrives when it is ready.
|
|
66
|
+
- **Start-up is slower** (about 12 s on that app) because files are compiled as they load.
|
|
67
|
+
|
|
68
|
+
The measurements are in [`docs/BENCHMARKS.md`](https://github.com/san-jay-14/Tracedye/blob/main/docs/BENCHMARKS.md).
|
|
69
|
+
|
|
70
|
+
## License
|
|
71
|
+
|
|
72
|
+
MIT
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// Plain JavaScript (types in JSDoc, checked by tsc): the loader hook runs on a
|
|
2
|
+
// thread without tsx, so this file and the other loader files must run as-is on Node 20.
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
|
|
5
|
+
const EXCLUDED_SEGMENTS = ["node_modules", "__tests__", "generated"];
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* True for TypeScript files under projectDir that are real project code:
|
|
9
|
+
* not dependencies, tests, declaration files or generated files.
|
|
10
|
+
* @param {string} filePath
|
|
11
|
+
* @param {string} projectDir
|
|
12
|
+
* @returns {boolean}
|
|
13
|
+
*/
|
|
14
|
+
export function isProjectSource(filePath, projectDir) {
|
|
15
|
+
const normalized = filePath.replaceAll("\\", "/");
|
|
16
|
+
const base = path.posix.basename(normalized);
|
|
17
|
+
|
|
18
|
+
const relative = path.posix.relative(projectDir.replaceAll("\\", "/"), normalized);
|
|
19
|
+
if (relative.startsWith("..") || path.posix.isAbsolute(relative)) return false;
|
|
20
|
+
|
|
21
|
+
if (!/\.m?ts$/.test(base) || base.endsWith(".d.ts")) return false;
|
|
22
|
+
if (/\.(test|spec|generated)\./.test(base)) return false;
|
|
23
|
+
return !relative.split("/").some((segment) => EXCLUDED_SEGMENTS.includes(segment));
|
|
24
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// Plain JavaScript (types in JSDoc, checked by tsc), see runtime.js.
|
|
2
|
+
// A preload that sends capture's events to the collector and follows its armed state:
|
|
3
|
+
// events are batched to POST /events, and GET /state is polled to arm and disarm.
|
|
4
|
+
// Collector address: TRACEDYE_COLLECTOR_URL, default http://localhost:4318.
|
|
5
|
+
import { arm, disarm, onEvent } from "./runtime.js";
|
|
6
|
+
|
|
7
|
+
const COLLECTOR = process.env.TRACEDYE_COLLECTOR_URL ?? "http://localhost:4318";
|
|
8
|
+
const FLUSH_MS = 100;
|
|
9
|
+
const POLL_MS = 500;
|
|
10
|
+
|
|
11
|
+
/** @type {import("./runtime.js").CaptureEvent[]} */
|
|
12
|
+
let buffer = [];
|
|
13
|
+
/** @type {ReturnType<typeof setTimeout> | undefined} */
|
|
14
|
+
let flushTimer;
|
|
15
|
+
let warned = false;
|
|
16
|
+
|
|
17
|
+
/** @param {string} message */
|
|
18
|
+
function warnOnce(message) {
|
|
19
|
+
if (warned) return;
|
|
20
|
+
warned = true;
|
|
21
|
+
console.warn(`[capture] ${message}`);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async function flush() {
|
|
25
|
+
flushTimer = undefined;
|
|
26
|
+
const batch = buffer;
|
|
27
|
+
buffer = [];
|
|
28
|
+
try {
|
|
29
|
+
const response = await fetch(`${COLLECTOR}/events`, {
|
|
30
|
+
method: "POST",
|
|
31
|
+
headers: { "content-type": "application/json" },
|
|
32
|
+
body: JSON.stringify(batch),
|
|
33
|
+
});
|
|
34
|
+
if (!response.ok) warnOnce(`collector rejected events: ${response.status}`);
|
|
35
|
+
} catch {
|
|
36
|
+
warnOnce(`collector not reachable at ${COLLECTOR}; dropping events`);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
onEvent((event) => {
|
|
41
|
+
buffer.push(event);
|
|
42
|
+
flushTimer ??= setTimeout(flush, FLUSH_MS);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
/** @type {string | null} */
|
|
46
|
+
let armedName = null;
|
|
47
|
+
|
|
48
|
+
async function syncArmedState() {
|
|
49
|
+
/** @type {string | null} */
|
|
50
|
+
let next = null;
|
|
51
|
+
try {
|
|
52
|
+
const response = await fetch(`${COLLECTOR}/state`);
|
|
53
|
+
next = /** @type {{ armed: string | null }} */ (await response.json()).armed;
|
|
54
|
+
} catch {
|
|
55
|
+
// Collector unreachable: stay disarmed rather than pay for events nobody receives.
|
|
56
|
+
}
|
|
57
|
+
if (next === armedName) return;
|
|
58
|
+
armedName = next;
|
|
59
|
+
if (next === null) disarm();
|
|
60
|
+
else arm(next);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
setInterval(syncArmedState, POLL_MS).unref();
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// Plain JavaScript (types in JSDoc, checked by tsc), see filter.js.
|
|
2
|
+
import { readFile } from "node:fs/promises";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
import { isProjectSource } from "./filter.js";
|
|
6
|
+
import { wrapSource } from "./transform.js";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @typedef {object} LoaderConfig
|
|
10
|
+
* @property {string} projectDir only code under here is wrapped
|
|
11
|
+
* @property {string} rootDir node ids are relative to here, to match the indexer
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/** @type {LoaderConfig} */
|
|
15
|
+
let config;
|
|
16
|
+
|
|
17
|
+
/** @param {LoaderConfig} data */
|
|
18
|
+
export function initialize(data) {
|
|
19
|
+
config = data;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Registered after tsx, so this runs first for project files. It compiles them itself
|
|
24
|
+
* (ESM only) and short-circuits; every other module falls through to tsx.
|
|
25
|
+
* @type {import("node:module").LoadHook}
|
|
26
|
+
*/
|
|
27
|
+
export async function load(url, context, nextLoad) {
|
|
28
|
+
if (url.startsWith("file:")) {
|
|
29
|
+
const fileUrl = new URL(url);
|
|
30
|
+
fileUrl.search = ""; // tsx may add a query to resolved urls
|
|
31
|
+
const filePath = fileURLToPath(fileUrl);
|
|
32
|
+
if (isProjectSource(filePath, config.projectDir)) {
|
|
33
|
+
const relative = path.relative(config.rootDir, filePath).replaceAll("\\", "/");
|
|
34
|
+
const source = wrapSource(await readFile(filePath, "utf8"), filePath, relative);
|
|
35
|
+
return { format: "module", source, shortCircuit: true };
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return nextLoad(url, context);
|
|
39
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// Plain JavaScript (types in JSDoc, checked by tsc), see runtime.js.
|
|
2
|
+
import { performance } from "node:perf_hooks";
|
|
3
|
+
import { currentCall } from "./runtime.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* An OpenTelemetry span processor that tags each span started inside a wrapped function
|
|
7
|
+
* call with the trigger and the calling function, so the collector can turn Postgres and
|
|
8
|
+
* Redis spans into hops. Spans started outside a trigger are left alone.
|
|
9
|
+
*/
|
|
10
|
+
export class CaptureSpanProcessor {
|
|
11
|
+
/** @param {{ setAttributes(attributes: Record<string, string | number>): unknown }} span */
|
|
12
|
+
onStart(span) {
|
|
13
|
+
const call = currentCall();
|
|
14
|
+
if (!call) return;
|
|
15
|
+
span.setAttributes({
|
|
16
|
+
"capture.trigger_id": call.triggerId,
|
|
17
|
+
"capture.trigger_name": call.triggerName,
|
|
18
|
+
"capture.caller_node": call.nodeId,
|
|
19
|
+
"capture.caller_span": call.spanId,
|
|
20
|
+
"capture.at_ms": performance.now(), // same clock as function events, so hops sort among them
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
onEnd() {}
|
|
25
|
+
|
|
26
|
+
shutdown() {
|
|
27
|
+
return Promise.resolve();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
forceFlush() {
|
|
31
|
+
return Promise.resolve();
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
// Plain JavaScript (types in JSDoc, checked by tsc), see runtime.js.
|
|
2
|
+
// Everything leaving the process goes through redact(): it deny-lists keys, walks only
|
|
3
|
+
// plain objects and arrays, and caps the size. It never mutates its input.
|
|
4
|
+
|
|
5
|
+
export const REDACTED = "[REDACTED]";
|
|
6
|
+
export const DEFAULT_DENY_KEYS = ["password", "token", "authorization", "cookie", "secret"];
|
|
7
|
+
export const DEFAULT_MAX_BYTES = 4096;
|
|
8
|
+
|
|
9
|
+
const MAX_DEPTH = 6;
|
|
10
|
+
const MAX_ENTRIES = 100;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @typedef {object} RedactOptions
|
|
14
|
+
* @property {string[]} denyKeys lower-case; a key is denied if it contains any of these
|
|
15
|
+
* @property {number} maxBytes cap on one redacted value
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/** @type {RedactOptions} */
|
|
19
|
+
export const DEFAULT_OPTIONS = { denyKeys: DEFAULT_DENY_KEYS, maxBytes: DEFAULT_MAX_BYTES };
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* @param {string} key
|
|
23
|
+
* @param {string[]} denyKeys
|
|
24
|
+
*/
|
|
25
|
+
export function isDeniedKey(key, denyKeys) {
|
|
26
|
+
const lower = key.toLowerCase();
|
|
27
|
+
return denyKeys.some((denied) => lower.includes(denied));
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Returns a JSON-safe copy of value with denied keys replaced and the size capped.
|
|
32
|
+
* @param {unknown} value
|
|
33
|
+
* @param {RedactOptions} [options]
|
|
34
|
+
* @returns {unknown}
|
|
35
|
+
*/
|
|
36
|
+
export function redact(value, options = DEFAULT_OPTIONS) {
|
|
37
|
+
const result = walk(value, options, 0, new WeakSet());
|
|
38
|
+
const json = JSON.stringify(result);
|
|
39
|
+
if (json === undefined) return result;
|
|
40
|
+
const bytes = Buffer.byteLength(json);
|
|
41
|
+
if (bytes <= options.maxBytes) return result;
|
|
42
|
+
const preview = Buffer.from(json).subarray(0, options.maxBytes).toString("utf8");
|
|
43
|
+
return `${preview}…[truncated, ${bytes} bytes]`;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* @param {unknown} value
|
|
48
|
+
* @param {RedactOptions} options
|
|
49
|
+
* @param {number} depth
|
|
50
|
+
* @param {WeakSet<object>} seen
|
|
51
|
+
* @returns {unknown}
|
|
52
|
+
*/
|
|
53
|
+
function walk(value, options, depth, seen) {
|
|
54
|
+
if (value === null || value === undefined) return value;
|
|
55
|
+
switch (typeof value) {
|
|
56
|
+
case "string":
|
|
57
|
+
return value.length > options.maxBytes ? `${value.slice(0, options.maxBytes)}…` : value;
|
|
58
|
+
case "number":
|
|
59
|
+
case "boolean":
|
|
60
|
+
return value;
|
|
61
|
+
case "bigint":
|
|
62
|
+
return `${value}n`;
|
|
63
|
+
case "symbol":
|
|
64
|
+
return value.toString();
|
|
65
|
+
case "function":
|
|
66
|
+
return "[Function]";
|
|
67
|
+
}
|
|
68
|
+
if (depth >= MAX_DEPTH) return "[MaxDepth]";
|
|
69
|
+
if (seen.has(value)) return "[Circular]";
|
|
70
|
+
if (value instanceof Date) return Number.isNaN(value.getTime()) ? "[Invalid Date]" : value.toISOString();
|
|
71
|
+
|
|
72
|
+
if (Array.isArray(value)) {
|
|
73
|
+
seen.add(value);
|
|
74
|
+
const items = value.slice(0, MAX_ENTRIES).map((item) => walk(item, options, depth + 1, seen));
|
|
75
|
+
if (value.length > MAX_ENTRIES) items.push(`[${value.length - MAX_ENTRIES} more]`);
|
|
76
|
+
seen.delete(value);
|
|
77
|
+
return items;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Class instances (Express res, sockets, ORM models...) can hold secrets under no key at
|
|
81
|
+
// all, such as rawHeaders, so they are never opened. A request is the one exception: it
|
|
82
|
+
// is summarised from a fixed list of fields, each going through the same key deny list.
|
|
83
|
+
if (!isPlainObject(value)) {
|
|
84
|
+
if (constructorName(value) === "IncomingMessage") return walk(summarizeRequest(value), options, depth, seen);
|
|
85
|
+
return `[${constructorName(value)}]`;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
seen.add(value);
|
|
89
|
+
/** @type {Record<string, unknown>} */
|
|
90
|
+
const out = {};
|
|
91
|
+
const record = /** @type {Record<string, unknown>} */ (value);
|
|
92
|
+
const keys = Object.keys(record);
|
|
93
|
+
for (const key of keys.slice(0, MAX_ENTRIES)) {
|
|
94
|
+
let copy;
|
|
95
|
+
if (isDeniedKey(key, options.denyKeys)) copy = REDACTED;
|
|
96
|
+
else {
|
|
97
|
+
try {
|
|
98
|
+
copy = walk(record[key], options, depth + 1, seen);
|
|
99
|
+
} catch {
|
|
100
|
+
copy = "[Unreadable]";
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
// defineProperty, so a key named "__proto__" stays a plain key
|
|
104
|
+
Object.defineProperty(out, key, { value: copy, enumerable: true, writable: true, configurable: true });
|
|
105
|
+
}
|
|
106
|
+
if (keys.length > MAX_ENTRIES) out["…"] = `${keys.length - MAX_ENTRIES} more`;
|
|
107
|
+
seen.delete(value);
|
|
108
|
+
return out;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Method, path, query, headers, parsed body and client address. The query string is split
|
|
113
|
+
* off the url and parsed, so `?token=abc` is caught by key like any other field.
|
|
114
|
+
* @param {object} value
|
|
115
|
+
*/
|
|
116
|
+
function summarizeRequest(value) {
|
|
117
|
+
const request = /** @type {{ method?: string, url?: string, headers?: object, body?: unknown, socket?: { remoteAddress?: string } }} */ (
|
|
118
|
+
value
|
|
119
|
+
);
|
|
120
|
+
const [path, queryString = ""] = (request.url ?? "").split("?");
|
|
121
|
+
return {
|
|
122
|
+
method: request.method,
|
|
123
|
+
path,
|
|
124
|
+
query: Object.fromEntries(new URLSearchParams(queryString)),
|
|
125
|
+
headers: { ...request.headers },
|
|
126
|
+
body: request.body,
|
|
127
|
+
ip: request.socket?.remoteAddress,
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** @param {object} value */
|
|
132
|
+
function isPlainObject(value) {
|
|
133
|
+
const prototype = Object.getPrototypeOf(value);
|
|
134
|
+
return prototype === Object.prototype || prototype === null;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** @param {object} value */
|
|
138
|
+
function constructorName(value) {
|
|
139
|
+
return value instanceof Error ? `Error: ${value.name}` : (value.constructor?.name ?? "Object");
|
|
140
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// Use as `node --import capture/register`. Wraps code under the working directory.
|
|
2
|
+
// Plain JavaScript on purpose: on Node 20 a preload can't be TypeScript.
|
|
3
|
+
import { register } from "node:module";
|
|
4
|
+
import { installHttpTrigger } from "./runtime.js"; // also defines globalThis.__captureWrap
|
|
5
|
+
import { findWorkspaceRoot } from "./root.js";
|
|
6
|
+
|
|
7
|
+
const projectDir = process.cwd();
|
|
8
|
+
register("./loader.js", import.meta.url, {
|
|
9
|
+
data: { projectDir, rootDir: findWorkspaceRoot(projectDir) },
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
installHttpTrigger();
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// Plain JavaScript (types in JSDoc, checked by tsc), see runtime.js.
|
|
2
|
+
import { existsSync } from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The folder node ids are relative to: the nearest parent with a pnpm-workspace.yaml,
|
|
7
|
+
* otherwise `from` itself. The indexer must use the same folder for its ids to match.
|
|
8
|
+
* @param {string} from
|
|
9
|
+
* @returns {string}
|
|
10
|
+
*/
|
|
11
|
+
export function findWorkspaceRoot(from) {
|
|
12
|
+
for (let dir = from; ; dir = path.dirname(dir)) {
|
|
13
|
+
if (existsSync(path.join(dir, "pnpm-workspace.yaml"))) return dir;
|
|
14
|
+
if (path.dirname(dir) === dir) return from;
|
|
15
|
+
}
|
|
16
|
+
}
|