@tsplc/tmp-debug 0.0.0-stage → 0.6.3
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/README.md +38 -2
- package/dist/api.js +744 -0
- package/dist/api.js.map +12 -0
- package/dist/constant.js +33 -0
- package/dist/constant.js.map +10 -0
- package/dist/explain-file.js +468 -0
- package/dist/explain-file.js.map +11 -0
- package/dist/explain.js +1310 -0
- package/dist/explain.js.map +19 -0
- package/dist/format.js +214 -0
- package/dist/format.js.map +12 -0
- package/dist/index.js +4066 -0
- package/dist/index.js.map +44 -0
- package/dist/manifest.js +240 -0
- package/dist/manifest.js.map +10 -0
- package/dist/paths.js +242 -0
- package/dist/paths.js.map +10 -0
- package/package.json +72 -4
- package/src/adapter.ts +77 -0
- package/src/api.ts +446 -0
- package/src/app-server.ts +151 -0
- package/src/build-files.ts +46 -0
- package/src/constant.ts +41 -0
- package/src/debugger.ts +237 -0
- package/src/errors.ts +13 -0
- package/src/explain-expressions.ts +484 -0
- package/src/explain-file.ts +325 -0
- package/src/explain-frames.ts +155 -0
- package/src/explain-loops.ts +151 -0
- package/src/explain-operations.ts +232 -0
- package/src/explain-tables.ts +345 -0
- package/src/explain-value.ts +203 -0
- package/src/explain-verdicts.ts +548 -0
- package/src/explain.ts +82 -0
- package/src/hardware.ts +159 -0
- package/src/http.ts +218 -0
- package/src/index.ts +44 -0
- package/src/jobs.ts +139 -0
- package/src/login-value.ts +165 -0
- package/src/login.ts +315 -0
- package/src/manifest.ts +328 -0
- package/src/opcua.ts +80 -0
- package/src/paths.ts +363 -0
- package/src/requests.ts +99 -0
- package/src/server-cursor.ts +22 -0
- package/src/shown.ts +229 -0
- package/src/telemetry.ts +125 -0
- package/src/timeline.ts +148 -0
- package/src/trace.ts +62 -0
- package/src/ui/bits.ts +48 -0
- package/src/ui/client.ts +87 -0
- package/src/ui/dom.ts +61 -0
- package/src/ui/explain-read.ts +105 -0
- package/src/ui/format.ts +261 -0
- package/src/ui/hardware.ts +425 -0
- package/src/ui/header.ts +207 -0
- package/src/ui/health.ts +64 -0
- package/src/ui/index.ts +27 -0
- package/src/ui/jobs.ts +263 -0
- package/src/ui/links.ts +59 -0
- package/src/ui/live-value.ts +85 -0
- package/src/ui/main.ts +76 -0
- package/src/ui/overview.ts +178 -0
- package/src/ui/page.ts +286 -0
- package/src/ui/pages.ts +64 -0
- package/src/ui/recent-window.ts +49 -0
- package/src/ui/recent.ts +355 -0
- package/src/ui/request-trace.ts +38 -0
- package/src/ui/requests.ts +80 -0
- package/src/ui/routes.ts +98 -0
- package/src/ui/store.ts +295 -0
- package/src/ui/success-cache.ts +11 -0
- package/src/ui/tree.ts +162 -0
- package/src/ui/variables.ts +597 -0
- package/src/ui/why-watch.ts +11 -0
- package/src/ui/why.ts +694 -0
- package/src/values.ts +170 -0
- package/src/watch.ts +32 -0
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { readFile } from "node:fs/promises";
|
|
3
|
+
import { dirname, join } from "node:path";
|
|
4
|
+
import { type ExplainFile, explainFile, explainFileName } from "./explain-file";
|
|
5
|
+
import { type DebugManifest, debugManifest } from "./manifest";
|
|
6
|
+
|
|
7
|
+
/** An `explain.json` that matched its manifest's digest, and its exact text. */
|
|
8
|
+
export interface LoadedExplanations {
|
|
9
|
+
text: string;
|
|
10
|
+
sha256: string;
|
|
11
|
+
file: ExplainFile;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export const loadManifest = async (file: string) =>
|
|
15
|
+
debugManifest.parse(JSON.parse(await readFile(file, "utf8")));
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The build's `explain.json` beside its manifest: null when the manifest records none, refused
|
|
19
|
+
* when the file's digest isn't the one the manifest records.
|
|
20
|
+
*/
|
|
21
|
+
export const loadExplanations = async (
|
|
22
|
+
manifestFile: string,
|
|
23
|
+
manifest: DebugManifest,
|
|
24
|
+
): Promise<LoadedExplanations | null> => {
|
|
25
|
+
if (!manifest.explain) {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
const text = await readFile(
|
|
29
|
+
join(dirname(manifestFile), explainFileName),
|
|
30
|
+
"utf8",
|
|
31
|
+
);
|
|
32
|
+
const sha256 = createHash("sha256").update(text).digest("hex");
|
|
33
|
+
if (sha256 !== manifest.explain.sha256) {
|
|
34
|
+
throw new Error(`${explainFileName} doesn't belong to this build`);
|
|
35
|
+
}
|
|
36
|
+
return { text, sha256, file: explainFile.parse(JSON.parse(text)) };
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/** The debug manifest at `manifestFile` and its build's explanations. */
|
|
40
|
+
export const loadDebugBuild = async (manifestFile: string) => {
|
|
41
|
+
const manifest = await loadManifest(manifestFile);
|
|
42
|
+
return {
|
|
43
|
+
manifest,
|
|
44
|
+
explanations: await loadExplanations(manifestFile, manifest),
|
|
45
|
+
};
|
|
46
|
+
};
|
package/src/constant.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { nanosecondsPerMillisecond } from "@tsplc/iec";
|
|
2
|
+
|
|
3
|
+
/** A constant's scalar that JSON can't hold as itself: a whole number, or a duration. */
|
|
4
|
+
export type ConstantScalar =
|
|
5
|
+
| { kind: "integer"; value: bigint }
|
|
6
|
+
| { kind: "duration"; nanoseconds: bigint };
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* How `debug.json` and `explain.json` write a constant's scalar: an integer as a number while
|
|
10
|
+
* doubles hold it exactly, else as its decimal text; a duration as whole milliseconds, `250ms`.
|
|
11
|
+
*/
|
|
12
|
+
export const encodeConstant = (scalar: ConstantScalar): number | string => {
|
|
13
|
+
switch (scalar.kind) {
|
|
14
|
+
case "integer":
|
|
15
|
+
return Number.isSafeInteger(Number(scalar.value))
|
|
16
|
+
? Number(scalar.value)
|
|
17
|
+
: String(scalar.value);
|
|
18
|
+
case "duration":
|
|
19
|
+
return `${scalar.nanoseconds / nanosecondsPerMillisecond}ms`;
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/** What `encodeConstant` wrote, or undefined for any other JSON value. */
|
|
24
|
+
export const decodeConstant = (value: unknown): ConstantScalar | undefined => {
|
|
25
|
+
if (typeof value === "number" && Number.isInteger(value)) {
|
|
26
|
+
return { kind: "integer", value: BigInt(value) };
|
|
27
|
+
}
|
|
28
|
+
if (typeof value !== "string") {
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
31
|
+
if (/^-?\d+$/.test(value)) {
|
|
32
|
+
return { kind: "integer", value: BigInt(value) };
|
|
33
|
+
}
|
|
34
|
+
const milliseconds = /^(-?\d+)ms$/.exec(value)?.[1];
|
|
35
|
+
return milliseconds === undefined
|
|
36
|
+
? undefined
|
|
37
|
+
: {
|
|
38
|
+
kind: "duration",
|
|
39
|
+
nanoseconds: BigInt(milliseconds) * nanosecondsPerMillisecond,
|
|
40
|
+
};
|
|
41
|
+
};
|
package/src/debugger.ts
ADDED
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
import { join } from "node:path";
|
|
2
|
+
import { scalarRead, type TargetAdapter } from "./adapter";
|
|
3
|
+
import type {
|
|
4
|
+
ExplanationResponse,
|
|
5
|
+
Forced,
|
|
6
|
+
Session,
|
|
7
|
+
StreamEvent,
|
|
8
|
+
TimelineEntry,
|
|
9
|
+
WatchGroups,
|
|
10
|
+
} from "./api";
|
|
11
|
+
import { followAppServer } from "./app-server";
|
|
12
|
+
import type { LoadedExplanations } from "./build-files";
|
|
13
|
+
import { DebugError } from "./errors";
|
|
14
|
+
import { createExplainer } from "./explain";
|
|
15
|
+
import type { DebugManifest } from "./manifest";
|
|
16
|
+
import { createPaths } from "./paths";
|
|
17
|
+
import { createRequestFollower } from "./requests";
|
|
18
|
+
import { createShownReads } from "./shown";
|
|
19
|
+
import { createTelemetryFollower } from "./telemetry";
|
|
20
|
+
import { createTimeline, timelineEntries } from "./timeline";
|
|
21
|
+
import { createTraceDecoder } from "./trace";
|
|
22
|
+
import { createValueReader } from "./values";
|
|
23
|
+
import { loadWatchGroups } from "./watch";
|
|
24
|
+
|
|
25
|
+
export interface DebuggerOptions {
|
|
26
|
+
manifest: DebugManifest;
|
|
27
|
+
/** Owned by the debugger from here on: closing the debugger closes it. */
|
|
28
|
+
adapter: TargetAdapter;
|
|
29
|
+
/** Saved watch groups live here. */
|
|
30
|
+
stateDirectory: string;
|
|
31
|
+
/** `debug.watch` from the config. */
|
|
32
|
+
watch: Record<string, string[]>;
|
|
33
|
+
/** The app server, whose telemetry and request trace are pulled every second. */
|
|
34
|
+
server?: { url: string; token: string };
|
|
35
|
+
release?: Session["release"];
|
|
36
|
+
/** Entries from before the debugger started, such as the agent's deployments. */
|
|
37
|
+
timeline?: readonly TimelineEntry[];
|
|
38
|
+
projectRoot?: string;
|
|
39
|
+
/** The build's `explain.json`, or null when it has none. */
|
|
40
|
+
explanations: LoadedExplanations | null;
|
|
41
|
+
/** The IEC expressions the controller forces, or null when they can't be read; null when nothing reports them. */
|
|
42
|
+
forced: (() => Promise<readonly string[] | null>) | null;
|
|
43
|
+
log: (message: string) => void;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** How often the build id is read through the controller. */
|
|
47
|
+
const buildCheckMs = 10_000;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The debugger for one build on one controller (contributing/architecture.md). It reads what the
|
|
51
|
+
* pages show through the adapter, follows the app server's telemetry and requests, and keeps the
|
|
52
|
+
* recent window of both. Everything it offers is read-only.
|
|
53
|
+
*/
|
|
54
|
+
export const createDebugger = async (options: DebuggerOptions) => {
|
|
55
|
+
const { manifest, adapter } = options;
|
|
56
|
+
const paths = createPaths(manifest);
|
|
57
|
+
const telemetry = createTelemetryFollower(manifest);
|
|
58
|
+
const requests = createRequestFollower();
|
|
59
|
+
const timeline = createTimeline();
|
|
60
|
+
const watchGroups = await loadWatchGroups(
|
|
61
|
+
join(options.stateDirectory, "watch.json"),
|
|
62
|
+
options.watch,
|
|
63
|
+
);
|
|
64
|
+
const listeners = new Set<(event: StreamEvent) => void>();
|
|
65
|
+
const controller = new AbortController();
|
|
66
|
+
let controllerBuild: string | null = null;
|
|
67
|
+
|
|
68
|
+
const emit = (event: StreamEvent) => {
|
|
69
|
+
for (const listener of listeners) {
|
|
70
|
+
listener(event);
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
const record = (entry: TimelineEntry) => {
|
|
74
|
+
emit({ name: "timeline", data: timeline.add(entry) });
|
|
75
|
+
};
|
|
76
|
+
for (const entry of options.timeline ?? []) {
|
|
77
|
+
timeline.add(entry);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const session = (): Session => ({
|
|
81
|
+
build: {
|
|
82
|
+
manifest: manifest.build.id,
|
|
83
|
+
controller: controllerBuild,
|
|
84
|
+
matches: controllerBuild === manifest.build.id,
|
|
85
|
+
},
|
|
86
|
+
connection: { ...adapter.connection(), endpoint: adapter.endpoint },
|
|
87
|
+
reading: { method: adapter.method, intervalMs: adapter.samplingMs },
|
|
88
|
+
tasks: telemetry.tasks(),
|
|
89
|
+
clock: telemetry.clock(),
|
|
90
|
+
...(options.release && { release: options.release }),
|
|
91
|
+
...(options.projectRoot && { projectRoot: options.projectRoot }),
|
|
92
|
+
});
|
|
93
|
+
const sessionChanged = () => emit({ name: "session", data: session() });
|
|
94
|
+
|
|
95
|
+
const setBuild = (build: string) => {
|
|
96
|
+
if (build === controllerBuild) {
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
controllerBuild = build;
|
|
100
|
+
sessionChanged();
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
const values = createValueReader({ manifest, paths, adapter });
|
|
104
|
+
const shown = createShownReads({
|
|
105
|
+
manifest,
|
|
106
|
+
paths,
|
|
107
|
+
adapter,
|
|
108
|
+
emit,
|
|
109
|
+
clock: telemetry.clock,
|
|
110
|
+
request: requests.call,
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
const stopBuild = adapter.subscribe(
|
|
114
|
+
[scalarRead(manifest.build.target, "STRING")],
|
|
115
|
+
(_, sample) => {
|
|
116
|
+
if (sample.quality === "good" && typeof sample.value === "string") {
|
|
117
|
+
setBuild(sample.value);
|
|
118
|
+
}
|
|
119
|
+
},
|
|
120
|
+
buildCheckMs,
|
|
121
|
+
);
|
|
122
|
+
|
|
123
|
+
const stopConnection = adapter.onConnection((connection) => {
|
|
124
|
+
record(
|
|
125
|
+
timelineEntries.connection(
|
|
126
|
+
connection.since,
|
|
127
|
+
connection.state,
|
|
128
|
+
connection.error,
|
|
129
|
+
),
|
|
130
|
+
);
|
|
131
|
+
sessionChanged();
|
|
132
|
+
if (connection.state !== "connected") {
|
|
133
|
+
shown.disconnected(connection.error ?? "The controller is not connected");
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
const running = options.server
|
|
138
|
+
? followAppServer({
|
|
139
|
+
server: options.server,
|
|
140
|
+
manifest,
|
|
141
|
+
telemetry,
|
|
142
|
+
requests,
|
|
143
|
+
trace: createTraceDecoder(manifest, paths),
|
|
144
|
+
shown,
|
|
145
|
+
emit,
|
|
146
|
+
record,
|
|
147
|
+
recordChange: (entry) =>
|
|
148
|
+
emit({ name: "timeline", data: timeline.changed(entry) }),
|
|
149
|
+
setBuild,
|
|
150
|
+
sessionChanged,
|
|
151
|
+
signal: controller.signal,
|
|
152
|
+
log: options.log,
|
|
153
|
+
})
|
|
154
|
+
: undefined;
|
|
155
|
+
|
|
156
|
+
const explained = options.explanations && {
|
|
157
|
+
file: options.explanations,
|
|
158
|
+
explainer: createExplainer(manifest, options.explanations.file),
|
|
159
|
+
};
|
|
160
|
+
const explanations = () => {
|
|
161
|
+
if (!explained) {
|
|
162
|
+
throw new DebugError("This build has no explanations", 404);
|
|
163
|
+
}
|
|
164
|
+
return explained;
|
|
165
|
+
};
|
|
166
|
+
/** The statements that can write `path`; refused when the build has no explanations or the path is unknown. */
|
|
167
|
+
const explain = (path: string): ExplanationResponse => {
|
|
168
|
+
const { explainer } = explanations();
|
|
169
|
+
const resolved = paths.resolve(path);
|
|
170
|
+
if (resolved?.kind !== "target") {
|
|
171
|
+
throw new DebugError(`Unknown path ${path}`, 400);
|
|
172
|
+
}
|
|
173
|
+
const explanation = explainer.explain(path);
|
|
174
|
+
return {
|
|
175
|
+
...explanation,
|
|
176
|
+
type: resolved.type,
|
|
177
|
+
writers: explanation.writers.map((writer) => ({
|
|
178
|
+
...writer,
|
|
179
|
+
indexes: writer.indexes.map(Number),
|
|
180
|
+
chains: writer.chains.map((chain) => ({
|
|
181
|
+
...chain,
|
|
182
|
+
calls: [...chain.calls],
|
|
183
|
+
})),
|
|
184
|
+
})),
|
|
185
|
+
};
|
|
186
|
+
};
|
|
187
|
+
/** The paths the forced IEC expressions name: storage roots and their fields, and I/O bindings. */
|
|
188
|
+
const forced = async (): Promise<Forced> => {
|
|
189
|
+
const at = new Date().toISOString();
|
|
190
|
+
const expressions = options.forced ? await options.forced() : null;
|
|
191
|
+
return {
|
|
192
|
+
forced: expressions && [
|
|
193
|
+
...new Set(
|
|
194
|
+
expressions.flatMap(
|
|
195
|
+
(expression) => paths.fromTarget(expression) ?? [],
|
|
196
|
+
),
|
|
197
|
+
),
|
|
198
|
+
],
|
|
199
|
+
at,
|
|
200
|
+
};
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
return {
|
|
204
|
+
manifest,
|
|
205
|
+
paths,
|
|
206
|
+
session,
|
|
207
|
+
explain,
|
|
208
|
+
forced,
|
|
209
|
+
/** `explain.json` as the build wrote it; refused when the build has no explanations. */
|
|
210
|
+
explanationFile: () => explanations().file,
|
|
211
|
+
values: values.read,
|
|
212
|
+
jobs: shown.jobs,
|
|
213
|
+
hardware: shown.hardware,
|
|
214
|
+
timeline: timeline.since,
|
|
215
|
+
watchGroups: () => watchGroups.groups(),
|
|
216
|
+
saveWatchGroups: (groups: WatchGroups) => watchGroups.save(groups),
|
|
217
|
+
/** Adds an entry from another source, such as the agent's audit log. */
|
|
218
|
+
record,
|
|
219
|
+
watch: values.watch,
|
|
220
|
+
show: shown.show,
|
|
221
|
+
/** Calls `listener` with every other stream event until the returned function is called. */
|
|
222
|
+
subscribe: (listener: (event: StreamEvent) => void) => {
|
|
223
|
+
listeners.add(listener);
|
|
224
|
+
return () => listeners.delete(listener);
|
|
225
|
+
},
|
|
226
|
+
close: async () => {
|
|
227
|
+
controller.abort();
|
|
228
|
+
stopBuild();
|
|
229
|
+
shown.close();
|
|
230
|
+
stopConnection();
|
|
231
|
+
await running;
|
|
232
|
+
await adapter.close();
|
|
233
|
+
},
|
|
234
|
+
};
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
export type Debugger = Awaited<ReturnType<typeof createDebugger>>;
|
package/src/errors.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** What to show for a caught value: an error's message, or anything else as text. */
|
|
2
|
+
export const errorMessage = (error: unknown) =>
|
|
3
|
+
error instanceof Error ? error.message : String(error);
|
|
4
|
+
|
|
5
|
+
/** A request the API refuses, with its HTTP status. */
|
|
6
|
+
export class DebugError extends Error {
|
|
7
|
+
constructor(
|
|
8
|
+
message: string,
|
|
9
|
+
readonly status: number,
|
|
10
|
+
) {
|
|
11
|
+
super(message);
|
|
12
|
+
}
|
|
13
|
+
}
|