@tsplc/tmp-debug 0.0.0-stage → 0.6.2

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.
Files changed (78) hide show
  1. package/README.md +38 -2
  2. package/dist/api.js +744 -0
  3. package/dist/api.js.map +12 -0
  4. package/dist/constant.js +33 -0
  5. package/dist/constant.js.map +10 -0
  6. package/dist/explain-file.js +468 -0
  7. package/dist/explain-file.js.map +11 -0
  8. package/dist/explain.js +1310 -0
  9. package/dist/explain.js.map +19 -0
  10. package/dist/format.js +214 -0
  11. package/dist/format.js.map +12 -0
  12. package/dist/index.js +4066 -0
  13. package/dist/index.js.map +44 -0
  14. package/dist/manifest.js +240 -0
  15. package/dist/manifest.js.map +10 -0
  16. package/dist/paths.js +242 -0
  17. package/dist/paths.js.map +10 -0
  18. package/package.json +72 -4
  19. package/src/adapter.ts +77 -0
  20. package/src/api.ts +446 -0
  21. package/src/app-server.ts +151 -0
  22. package/src/build-files.ts +46 -0
  23. package/src/constant.ts +41 -0
  24. package/src/debugger.ts +237 -0
  25. package/src/errors.ts +13 -0
  26. package/src/explain-expressions.ts +484 -0
  27. package/src/explain-file.ts +325 -0
  28. package/src/explain-frames.ts +155 -0
  29. package/src/explain-loops.ts +151 -0
  30. package/src/explain-operations.ts +232 -0
  31. package/src/explain-tables.ts +345 -0
  32. package/src/explain-value.ts +203 -0
  33. package/src/explain-verdicts.ts +548 -0
  34. package/src/explain.ts +82 -0
  35. package/src/hardware.ts +159 -0
  36. package/src/http.ts +218 -0
  37. package/src/index.ts +44 -0
  38. package/src/jobs.ts +139 -0
  39. package/src/login-value.ts +165 -0
  40. package/src/login.ts +315 -0
  41. package/src/manifest.ts +328 -0
  42. package/src/opcua.ts +80 -0
  43. package/src/paths.ts +363 -0
  44. package/src/requests.ts +99 -0
  45. package/src/server-cursor.ts +22 -0
  46. package/src/shown.ts +229 -0
  47. package/src/telemetry.ts +125 -0
  48. package/src/timeline.ts +148 -0
  49. package/src/trace.ts +62 -0
  50. package/src/ui/bits.ts +48 -0
  51. package/src/ui/client.ts +87 -0
  52. package/src/ui/dom.ts +61 -0
  53. package/src/ui/explain-read.ts +105 -0
  54. package/src/ui/format.ts +261 -0
  55. package/src/ui/hardware.ts +425 -0
  56. package/src/ui/header.ts +207 -0
  57. package/src/ui/health.ts +64 -0
  58. package/src/ui/index.ts +27 -0
  59. package/src/ui/jobs.ts +263 -0
  60. package/src/ui/links.ts +59 -0
  61. package/src/ui/live-value.ts +85 -0
  62. package/src/ui/main.ts +76 -0
  63. package/src/ui/overview.ts +178 -0
  64. package/src/ui/page.ts +286 -0
  65. package/src/ui/pages.ts +64 -0
  66. package/src/ui/recent-window.ts +49 -0
  67. package/src/ui/recent.ts +355 -0
  68. package/src/ui/request-trace.ts +38 -0
  69. package/src/ui/requests.ts +80 -0
  70. package/src/ui/routes.ts +98 -0
  71. package/src/ui/store.ts +295 -0
  72. package/src/ui/success-cache.ts +11 -0
  73. package/src/ui/tree.ts +162 -0
  74. package/src/ui/variables.ts +597 -0
  75. package/src/ui/why-watch.ts +11 -0
  76. package/src/ui/why.ts +694 -0
  77. package/src/values.ts +170 -0
  78. package/src/watch.ts +32 -0
package/src/values.ts ADDED
@@ -0,0 +1,170 @@
1
+ import type { TargetAdapter, TargetRead, TargetSample } from "./adapter";
2
+ import { clockSample, type Sample, slotSample } from "./api";
3
+ import { DebugError } from "./errors";
4
+ import { type DebugManifest, scanClockType, writerSlotType } from "./manifest";
5
+ import type { Paths } from "./paths";
6
+
7
+ /** A stored value to read: its sample's path, its target and its manifest type. */
8
+ interface Located {
9
+ path: string;
10
+ target: string;
11
+ type: string;
12
+ }
13
+
14
+ /** Values by TypeScript path: read now, or watched each sampling interval. */
15
+ export const createValueReader = ({
16
+ manifest,
17
+ paths,
18
+ adapter,
19
+ }: {
20
+ manifest: DebugManifest;
21
+ paths: Paths;
22
+ adapter: TargetAdapter;
23
+ }) => {
24
+ const unavailable = (path: string, status: string): Sample => ({
25
+ path,
26
+ value: null,
27
+ quality: "unavailable",
28
+ status,
29
+ receivedAt: new Date().toISOString(),
30
+ });
31
+
32
+ /** A path's target, or the sample it has without reading the controller. */
33
+ const locate = (path: string): Located | Sample => {
34
+ const resolved = paths.resolve(path);
35
+ if (!resolved) {
36
+ return unavailable(path, "Unknown path");
37
+ }
38
+ if (resolved.kind === "constant") {
39
+ return {
40
+ path,
41
+ value: resolved.value,
42
+ quality: "good",
43
+ status: "Constant",
44
+ receivedAt: new Date().toISOString(),
45
+ };
46
+ }
47
+ if (!resolved.readable) {
48
+ return unavailable(path, "Not readable");
49
+ }
50
+ return resolved;
51
+ };
52
+
53
+ const targetRead = ({ target, type }: Located): TargetRead => ({
54
+ target,
55
+ type: paths.typeOf(type),
56
+ });
57
+
58
+ const sampleOf = ({ path, type }: Located, sample: TargetSample): Sample => ({
59
+ path,
60
+ ...sample,
61
+ value: paths.decode(type, sample.value),
62
+ });
63
+
64
+ /** The clocks and last-writer slots asked for, as samples of their own paths. */
65
+ const rawTargets = (raw: {
66
+ clocks: readonly string[];
67
+ slots: readonly number[];
68
+ }): Located[] => [
69
+ ...raw.clocks.flatMap((name) => {
70
+ const target = manifest.tasks.find((task) => task.name === name)?.clock;
71
+ return target
72
+ ? [{ path: clockSample(name), target, type: scanClockType }]
73
+ : [];
74
+ }),
75
+ ...raw.slots.flatMap((index) => {
76
+ const target = manifest.writers[index]?.target;
77
+ return target
78
+ ? [{ path: slotSample(index), target, type: writerSlotType }]
79
+ : [];
80
+ }),
81
+ ];
82
+
83
+ /**
84
+ * Reads the leaves `patterns` match each sampling interval, and the clocks and last-writer slots
85
+ * asked for in the same read, until the returned function is called.
86
+ */
87
+ const watch = (
88
+ patterns: readonly string[],
89
+ listener: (sample: Sample) => void,
90
+ raw: { clocks: readonly string[]; slots: readonly number[] },
91
+ ) => {
92
+ const watched = new Map<string, Located[]>();
93
+ const reads: TargetRead[] = [];
94
+ const add = (located: Located) => {
95
+ if (!watched.has(located.target)) {
96
+ reads.push(targetRead(located));
97
+ }
98
+ watched.set(located.target, [
99
+ ...(watched.get(located.target) ?? []),
100
+ located,
101
+ ]);
102
+ };
103
+ for (const item of rawTargets(raw)) {
104
+ add(item);
105
+ }
106
+ const unread: Sample[] = [];
107
+ for (const path of paths.expand(patterns)) {
108
+ const located = locate(path);
109
+ if ("target" in located) {
110
+ add(located);
111
+ } else {
112
+ unread.push(located);
113
+ listener(located);
114
+ }
115
+ }
116
+ // A sample that needs no read, such as a constant's, is as current as those read with it.
117
+ const repeat = unread.length
118
+ ? setInterval(() => {
119
+ if (adapter.connection().state !== "connected") {
120
+ return;
121
+ }
122
+ const receivedAt = new Date().toISOString();
123
+ for (const sample of unread) {
124
+ listener({ ...sample, receivedAt });
125
+ }
126
+ }, adapter.samplingMs)
127
+ : undefined;
128
+ const stop = reads.length
129
+ ? adapter.subscribe(reads, (target, received) => {
130
+ for (const located of watched.get(target) ?? []) {
131
+ listener(sampleOf(located, received));
132
+ }
133
+ })
134
+ : () => {};
135
+ return () => {
136
+ clearInterval(repeat);
137
+ stop();
138
+ };
139
+ };
140
+
141
+ /** Reads the leaves `patterns` match now; refused while the controller isn't connected. */
142
+ const read = async (patterns: readonly string[]) => {
143
+ const connection = adapter.connection();
144
+ if (connection.state !== "connected") {
145
+ throw new DebugError(
146
+ connection.error ?? "The controller is not connected",
147
+ 503,
148
+ );
149
+ }
150
+ const located = paths
151
+ .expand(patterns)
152
+ .map((path) => ({ path, located: locate(path) }));
153
+ const samples = await adapter.read(
154
+ located.flatMap(({ located }) =>
155
+ "target" in located ? [targetRead(located)] : [],
156
+ ),
157
+ );
158
+ return located.map(({ path, located }) => {
159
+ if (!("target" in located)) {
160
+ return located;
161
+ }
162
+ const sample = samples.get(located.target);
163
+ return sample
164
+ ? sampleOf({ ...located, path }, sample)
165
+ : unavailable(path, "Not read");
166
+ });
167
+ };
168
+
169
+ return { watch, read };
170
+ };
package/src/watch.ts ADDED
@@ -0,0 +1,32 @@
1
+ import { existsSync } from "node:fs";
2
+ import { mkdir, readFile, writeFile } from "node:fs/promises";
3
+ import { dirname } from "node:path";
4
+ import { isDeepStrictEqual } from "node:util";
5
+ import { type WatchGroups, watchGroupsSchema } from "./api";
6
+
7
+ /**
8
+ * Watch groups: the config's `debug.watch`, then groups saved in `file`. Saving keeps only the
9
+ * groups that differ from the config, so the config stays their source.
10
+ */
11
+ export const loadWatchGroups = async (
12
+ file: string,
13
+ configured: Record<string, string[]>,
14
+ ) => {
15
+ let saved = existsSync(file)
16
+ ? watchGroupsSchema.parse(JSON.parse(await readFile(file, "utf8"))).groups
17
+ : {};
18
+ const groups = (): WatchGroups => ({ groups: { ...configured, ...saved } });
19
+ return {
20
+ groups,
21
+ save: async ({ groups: next }: WatchGroups) => {
22
+ saved = Object.fromEntries(
23
+ Object.entries(next).filter(
24
+ ([name, paths]) => !isDeepStrictEqual(configured[name], paths),
25
+ ),
26
+ );
27
+ await mkdir(dirname(file), { recursive: true });
28
+ await writeFile(file, JSON.stringify({ groups: saved }, null, 2));
29
+ return groups();
30
+ },
31
+ };
32
+ };