@tsplc/compiler 0.5.0 → 0.5.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/dist/bun-runtime.js +1345 -0
- package/dist/bun-runtime.js.map +25 -0
- package/dist/cli.js +251 -46
- package/dist/cli.js.map +8 -6
- package/dist/codesys.js +11 -6
- package/dist/codesys.js.map +3 -3
- package/dist/elaborate-capture.js +5 -5
- package/dist/elaborate-capture.js.map +4 -4
- package/dist/index.js +3 -3
- package/dist/index.js.map +4 -4
- package/dist/project.js +45 -20
- package/dist/project.js.map +5 -4
- package/package.json +13 -9
- package/src/codesys/manifest.ts +8 -1
- package/src/frontend/elaborate/capture-protocol.ts +18 -0
- package/src/frontend/elaborate/capture.ts +3 -11
- package/src/frontend/elaborate/graph.ts +3 -2
- package/src/project/cli.ts +5 -0
- package/src/project/project-commands.ts +60 -6
- package/src/project/project.ts +4 -0
- package/src/runtime/async.ts +200 -0
- package/src/runtime/launch.ts +49 -0
- package/src/runtime/main.ts +54 -0
- package/src/runtime/process.ts +172 -0
- package/src/runtime/program.ts +142 -0
- package/src/runtime/retain.ts +100 -0
- package/src/runtime/runtime.ts +329 -0
- package/src/runtime/storage.ts +481 -0
- package/src/runtime/symbols.ts +153 -0
- package/src/runtime/telemetry.ts +123 -0
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { plcBufferBytes } from "@tsplc/server/wire";
|
|
2
|
+
|
|
3
|
+
/** A record as the program's telemetry calls make it; see `telemetryRecord` in `@tsplc/server`. */
|
|
4
|
+
export type TelemetryRecord = Readonly<Record<string, unknown>>;
|
|
5
|
+
|
|
6
|
+
/** How long a sender waits for a full batch before it sends what it has, and before it retries. */
|
|
7
|
+
const batchWaitMs = 200;
|
|
8
|
+
|
|
9
|
+
interface Queued {
|
|
10
|
+
sequence: number;
|
|
11
|
+
text: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Each task's records and the sender that posts them to `<server>/tsplc/telemetry`, as the
|
|
16
|
+
* compiled program's do: a ring of `capacity` records whose oldest unsent records are
|
|
17
|
+
* overwritten and counted as dropped when it fills, and a sender that runs at the start of the
|
|
18
|
+
* task's scans and posts one batch at a time, at most the PLC's HTTP buffer.
|
|
19
|
+
*/
|
|
20
|
+
export const createTelemetry = (options: {
|
|
21
|
+
/** The build id, as the debug manifest names it. */
|
|
22
|
+
build: string;
|
|
23
|
+
capacity: number;
|
|
24
|
+
/** Posts a batch's JSON; null when the program has no server to send to, which drops records. */
|
|
25
|
+
post: ((body: string) => Promise<unknown>) | null;
|
|
26
|
+
clock: () => number;
|
|
27
|
+
}) => {
|
|
28
|
+
const tasks = new Map<
|
|
29
|
+
string,
|
|
30
|
+
{
|
|
31
|
+
queued: Queued[];
|
|
32
|
+
next: number;
|
|
33
|
+
dropped: number;
|
|
34
|
+
sending: boolean;
|
|
35
|
+
/** When the oldest unsent record was queued, or the last failed send. */
|
|
36
|
+
waitingSince: number;
|
|
37
|
+
}
|
|
38
|
+
>();
|
|
39
|
+
const stateOf = (task: string) => {
|
|
40
|
+
const known = tasks.get(task);
|
|
41
|
+
if (known) {
|
|
42
|
+
return known;
|
|
43
|
+
}
|
|
44
|
+
const created = {
|
|
45
|
+
queued: [],
|
|
46
|
+
next: 0,
|
|
47
|
+
dropped: 0,
|
|
48
|
+
sending: false,
|
|
49
|
+
waitingSince: 0,
|
|
50
|
+
};
|
|
51
|
+
tasks.set(task, created);
|
|
52
|
+
return created;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const record = (task: string, value: TelemetryRecord) => {
|
|
56
|
+
const state = stateOf(task);
|
|
57
|
+
const text = JSON.stringify(value);
|
|
58
|
+
if (!state.queued.length) {
|
|
59
|
+
state.waitingSince = options.clock();
|
|
60
|
+
}
|
|
61
|
+
state.queued.push({ sequence: state.next, text });
|
|
62
|
+
state.next += 1;
|
|
63
|
+
if (state.queued.length > options.capacity) {
|
|
64
|
+
state.queued.shift();
|
|
65
|
+
state.dropped += 1;
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
/** The records from the oldest unsent on that fit one batch beside its other fields. */
|
|
70
|
+
const batchOf = (task: string, scan: number) => {
|
|
71
|
+
const state = stateOf(task);
|
|
72
|
+
const envelope = (records: readonly string[], first: number) =>
|
|
73
|
+
`{"build":${JSON.stringify(options.build)},"task":${JSON.stringify(task)},"now":${Math.max(0, options.clock())},"scan":${scan},"first":${first},"dropped":${state.dropped},"records":[${records.join(",")}]}`;
|
|
74
|
+
const first = state.queued[0]?.sequence ?? state.next;
|
|
75
|
+
const texts: string[] = [];
|
|
76
|
+
for (const queued of state.queued) {
|
|
77
|
+
const candidate = envelope([...texts, queued.text], first);
|
|
78
|
+
if (Buffer.byteLength(candidate, "utf8") > plcBufferBytes) {
|
|
79
|
+
break;
|
|
80
|
+
}
|
|
81
|
+
texts.push(queued.text);
|
|
82
|
+
}
|
|
83
|
+
return { body: envelope(texts, first), count: texts.length };
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
/** The sender's turn at the start of a scan of `task`. */
|
|
87
|
+
const send = (task: string, scan: number) => {
|
|
88
|
+
const state = stateOf(task);
|
|
89
|
+
const { post } = options;
|
|
90
|
+
if (!post || state.sending || !state.queued.length) {
|
|
91
|
+
if (!post) {
|
|
92
|
+
state.queued = [];
|
|
93
|
+
}
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
const batch = batchOf(task, scan);
|
|
97
|
+
const full = batch.count < state.queued.length;
|
|
98
|
+
if (!full && options.clock() - state.waitingSince < batchWaitMs) {
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
if (!batch.count) {
|
|
102
|
+
// One record larger than a batch can never be sent.
|
|
103
|
+
state.queued.shift();
|
|
104
|
+
state.dropped += 1;
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
const sent = state.queued.slice(0, batch.count);
|
|
108
|
+
state.sending = true;
|
|
109
|
+
post(batch.body)
|
|
110
|
+
.then(() => {
|
|
111
|
+
state.queued = state.queued.filter((queued) => !sent.includes(queued));
|
|
112
|
+
state.waitingSince = options.clock();
|
|
113
|
+
})
|
|
114
|
+
.catch(() => {
|
|
115
|
+
state.waitingSince = options.clock();
|
|
116
|
+
})
|
|
117
|
+
.finally(() => {
|
|
118
|
+
state.sending = false;
|
|
119
|
+
});
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
return { record, send };
|
|
123
|
+
};
|