@tsplc/compiler 0.5.13 → 0.5.15
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 +33 -8
- package/dist/cli.js +794 -621
- package/dist/cli.js.map +19 -16
- package/dist/clock.js +5 -2
- package/dist/clock.js.map +5 -4
- package/dist/codesys.js +4162 -4157
- package/dist/codesys.js.map +43 -29
- package/dist/index.js +7852 -6549
- package/dist/index.js.map +53 -50
- package/dist/ir.js +2482 -2277
- package/dist/ir.js.map +28 -25
- package/dist/retain.js +3 -1
- package/dist/retain.js.map +3 -3
- package/dist/signals.js +115 -0
- package/dist/signals.js.map +10 -0
- package/dist/telemetry.js +25 -20
- package/dist/telemetry.js.map +6 -6
- package/package.json +19 -39
- package/src/codesys/config.ts +3 -3
- package/src/codesys/debug.ts +92 -108
- package/src/codesys/emit.ts +100 -88
- package/src/codesys/endpoints.ts +111 -0
- package/src/codesys/engineering.ts +30 -54
- package/src/codesys/explain/expressions.ts +445 -0
- package/src/codesys/explain/flags.ts +134 -0
- package/src/codesys/explain/parameters.ts +53 -0
- package/src/codesys/explain/rows.ts +182 -0
- package/src/codesys/explain/tables.ts +79 -0
- package/src/codesys/explain/tasks.ts +68 -0
- package/src/codesys/explain/writes.ts +497 -0
- package/src/codesys/explain.ts +85 -1401
- package/src/codesys/expressions.ts +41 -33
- package/src/codesys/global-lists.ts +114 -94
- package/src/codesys/global-names.ts +12 -14
- package/src/codesys/http.ts +428 -388
- package/src/codesys/instrumentation.ts +142 -148
- package/src/codesys/io-paths.ts +31 -23
- package/src/codesys/loops.ts +101 -0
- package/src/codesys/manifest.ts +269 -269
- package/src/codesys/names.ts +10 -33
- package/src/codesys/numeric.ts +89 -42
- package/src/codesys/plan.ts +202 -190
- package/src/codesys/programs.ts +23 -16
- package/src/codesys/publication.ts +13 -86
- package/src/codesys/required.ts +8 -0
- package/src/codesys/routines.ts +103 -489
- package/src/codesys/scheduling.ts +96 -0
- package/src/codesys/service-channels.ts +23 -0
- package/src/codesys/sources.ts +34 -28
- package/src/codesys/st-files.ts +112 -105
- package/src/codesys/statements.ts +409 -0
- package/src/codesys/storage.ts +41 -23
- package/src/codesys/symbols.ts +130 -119
- package/src/codesys/task-access.ts +65 -221
- package/src/codesys/telemetry-settings.ts +19 -0
- package/src/codesys/telemetry.ts +185 -148
- package/src/codesys/types.ts +69 -109
- package/src/codesys/vendor.ts +13 -50
- package/src/frontend/analyzer.ts +66 -111
- package/src/frontend/async.ts +316 -128
- package/src/frontend/blocks.ts +178 -154
- package/src/frontend/configuration.ts +49 -153
- package/src/frontend/const-functions.ts +50 -40
- package/src/frontend/control-flow.ts +164 -129
- package/src/frontend/cooperative.ts +17 -9
- package/src/frontend/coroutines.ts +539 -441
- package/src/frontend/dialect-names.ts +104 -0
- package/src/frontend/elaborate/array-searches.ts +77 -69
- package/src/frontend/elaborate/assignments.ts +68 -77
- package/src/frontend/elaborate/calls.ts +878 -512
- package/src/frontend/elaborate/capture-protocol.ts +118 -21
- package/src/frontend/elaborate/capture.ts +196 -109
- package/src/frontend/elaborate/declarations.ts +124 -0
- package/src/frontend/elaborate/dialect.ts +119 -46
- package/src/frontend/elaborate/expressions.ts +763 -442
- package/src/frontend/elaborate/filtered-arrays.ts +127 -91
- package/src/frontend/elaborate/frame.ts +50 -2
- package/src/frontend/elaborate/generics.ts +20 -34
- package/src/frontend/elaborate/graph.ts +13 -87
- package/src/frontend/elaborate/imports.ts +0 -5
- package/src/frontend/elaborate/index.ts +416 -169
- package/src/frontend/elaborate/initializers.ts +64 -42
- package/src/frontend/elaborate/iterators.ts +228 -364
- package/src/frontend/elaborate/layouts.ts +477 -116
- package/src/frontend/elaborate/model.ts +113 -55
- package/src/frontend/elaborate/references.ts +116 -138
- package/src/frontend/elaborate/retention.ts +137 -59
- package/src/frontend/elaborate/specialize.ts +617 -308
- package/src/frontend/elaborate/statements.ts +436 -247
- package/src/frontend/elaborate/storage.ts +280 -213
- package/src/frontend/elaborate/time-constructors.ts +34 -0
- package/src/frontend/elaborate/values.ts +77 -7
- package/src/frontend/elaborate/writes.ts +98 -84
- package/src/frontend/expressions.ts +149 -151
- package/src/frontend/generated-names.ts +3 -3
- package/src/frontend/intrinsics.ts +31 -17
- package/src/frontend/lowering-key.ts +23 -0
- package/src/frontend/module-graph.ts +105 -43
- package/src/frontend/positions.ts +43 -35
- package/src/frontend/promise-chains.ts +226 -266
- package/src/frontend/promises.ts +88 -67
- package/src/frontend/routines.ts +122 -65
- package/src/frontend/sdk.ts +104 -5
- package/src/frontend/services.ts +149 -102
- package/src/frontend/statements.ts +60 -64
- package/src/frontend/tasks.ts +151 -96
- package/src/frontend/telemetry.ts +1 -1
- package/src/frontend/types.ts +366 -181
- package/src/frontend/typescript.ts +239 -112
- package/src/index.ts +2 -1
- package/src/ir/aggregates.ts +90 -75
- package/src/ir/allocation.ts +29 -18
- package/src/ir/conditions.ts +38 -78
- package/src/ir/core-model.ts +128 -11
- package/src/ir/declarations.ts +18 -32
- package/src/ir/diagnostics.ts +25 -18
- package/src/ir/domains.ts +26 -23
- package/src/ir/fold.ts +55 -50
- package/src/ir/identity.ts +10 -0
- package/src/ir/index.ts +31 -7
- package/src/ir/job-errors.ts +30 -8
- package/src/ir/layouts.ts +63 -34
- package/src/ir/link.ts +11 -46
- package/src/ir/model.ts +25 -6
- package/src/ir/normalize.ts +3 -28
- package/src/ir/numeric.ts +45 -18
- package/src/ir/operands.ts +102 -0
- package/src/ir/ranges.ts +242 -67
- package/src/ir/routine.ts +126 -183
- package/src/ir/scalars.ts +29 -25
- package/src/ir/share.ts +323 -343
- package/src/ir/short-circuit.ts +140 -0
- package/src/ir/tables.ts +111 -0
- package/src/ir/temporaries.ts +15 -113
- package/src/ir/types.ts +32 -8
- package/src/project/artifacts.ts +143 -71
- package/src/project/cli.ts +104 -87
- package/src/project/debugger.ts +48 -34
- package/src/project/deployments.ts +12 -68
- package/src/project/flags.ts +14 -0
- package/src/project/plant.ts +293 -225
- package/src/project/project-commands.ts +254 -192
- package/src/project/project.ts +184 -141
- package/src/project/release.ts +22 -28
- package/src/project/server.ts +22 -26
- package/src/project/terminal.ts +13 -2
- package/src/runtime/async.ts +17 -13
- package/src/runtime/launch.ts +5 -2
- package/src/runtime/main.ts +8 -3
- package/src/runtime/missing-file.ts +3 -0
- package/src/runtime/process.ts +24 -19
- package/src/runtime/program.ts +20 -53
- package/src/runtime/retain.ts +18 -10
- package/src/runtime/runtime.ts +47 -60
- package/src/runtime/settings.ts +27 -0
- package/src/runtime/storage.ts +49 -74
- package/src/runtime/symbols.ts +19 -75
- package/src/runtime/telemetry.ts +37 -14
- package/src/runtime/transport.ts +26 -30
- package/stdlib/clock.ts +4 -2
- package/stdlib/plc-time.ts +2 -0
- package/stdlib/retain.ts +2 -0
- package/stdlib/signals.ts +142 -0
- package/stdlib/telemetry.ts +5 -12
- package/types/dialect.d.ts +49 -0
- package/types/globals.d.ts +2 -42
- package/types/index.d.ts +3 -1
- package/types/retain.d.ts +8 -2
- package/types/telemetry.d.ts +3 -0
- package/dist/bun-runtime.js +0 -1472
- package/dist/bun-runtime.js.map +0 -26
- package/dist/elaborate-capture.js +0 -304
- package/dist/elaborate-capture.js.map +0 -11
- package/dist/math.js +0 -24
- package/dist/math.js.map +0 -10
- package/dist/project.js +0 -626
- package/dist/project.js.map +0 -12
- package/dist/server-check.js +0 -59
- package/dist/server-check.js.map +0 -11
- package/src/frontend/exposure.ts +0 -181
- package/src/project/index.ts +0 -6
- package/src/runtime/.runtime-65O4JH/server/router.ts +0 -17
- package/src/runtime/.runtime-65O4JH/src/cell.ts +0 -45
- package/src/runtime/.runtime-65O4JH/src/controller.ts +0 -58
- package/src/runtime/.runtime-65O4JH/src/lane.ts +0 -6
- package/src/runtime/.runtime-65O4JH/src/main.ts +0 -11
- package/src/runtime/.runtime-65O4JH/src/services.ts +0 -8
- package/src/runtime/.runtime-65O4JH/tsconfig.json +0 -13
- package/src/runtime/.runtime-65O4JH/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-Brw2kO/server/router.ts +0 -17
- package/src/runtime/.runtime-Brw2kO/src/cell.ts +0 -45
- package/src/runtime/.runtime-Brw2kO/src/controller.ts +0 -58
- package/src/runtime/.runtime-Brw2kO/src/lane.ts +0 -6
- package/src/runtime/.runtime-Brw2kO/src/main.ts +0 -11
- package/src/runtime/.runtime-Brw2kO/src/services.ts +0 -8
- package/src/runtime/.runtime-Brw2kO/tsconfig.json +0 -13
- package/src/runtime/.runtime-Brw2kO/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-NHfu1D/server/router.ts +0 -17
- package/src/runtime/.runtime-NHfu1D/src/cell.ts +0 -45
- package/src/runtime/.runtime-NHfu1D/src/controller.ts +0 -58
- package/src/runtime/.runtime-NHfu1D/src/lane.ts +0 -6
- package/src/runtime/.runtime-NHfu1D/src/main.ts +0 -11
- package/src/runtime/.runtime-NHfu1D/src/services.ts +0 -8
- package/src/runtime/.runtime-NHfu1D/tsconfig.json +0 -13
- package/src/runtime/.runtime-NHfu1D/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-VaUSOu/server/router.ts +0 -17
- package/src/runtime/.runtime-VaUSOu/src/cell.ts +0 -45
- package/src/runtime/.runtime-VaUSOu/src/controller.ts +0 -58
- package/src/runtime/.runtime-VaUSOu/src/lane.ts +0 -6
- package/src/runtime/.runtime-VaUSOu/src/main.ts +0 -11
- package/src/runtime/.runtime-VaUSOu/src/services.ts +0 -8
- package/src/runtime/.runtime-VaUSOu/tsconfig.json +0 -13
- package/src/runtime/.runtime-VaUSOu/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-WbtNvR/server/router.ts +0 -17
- package/src/runtime/.runtime-WbtNvR/src/cell.ts +0 -45
- package/src/runtime/.runtime-WbtNvR/src/controller.ts +0 -58
- package/src/runtime/.runtime-WbtNvR/src/lane.ts +0 -6
- package/src/runtime/.runtime-WbtNvR/src/main.ts +0 -11
- package/src/runtime/.runtime-WbtNvR/src/services.ts +0 -8
- package/src/runtime/.runtime-WbtNvR/tsconfig.json +0 -13
- package/src/runtime/.runtime-WbtNvR/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-opcua-GMPUt8/server/router.ts +0 -17
- package/src/runtime/.runtime-opcua-GMPUt8/src/cell.ts +0 -45
- package/src/runtime/.runtime-opcua-GMPUt8/src/controller.ts +0 -58
- package/src/runtime/.runtime-opcua-GMPUt8/src/lane.ts +0 -6
- package/src/runtime/.runtime-opcua-GMPUt8/src/main.ts +0 -11
- package/src/runtime/.runtime-opcua-GMPUt8/src/services.ts +0 -8
- package/src/runtime/.runtime-opcua-GMPUt8/tsconfig.json +0 -13
- package/src/runtime/.runtime-opcua-GMPUt8/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-opcua-UwafxO/server/router.ts +0 -17
- package/src/runtime/.runtime-opcua-UwafxO/src/cell.ts +0 -45
- package/src/runtime/.runtime-opcua-UwafxO/src/controller.ts +0 -58
- package/src/runtime/.runtime-opcua-UwafxO/src/lane.ts +0 -6
- package/src/runtime/.runtime-opcua-UwafxO/src/main.ts +0 -11
- package/src/runtime/.runtime-opcua-UwafxO/src/services.ts +0 -8
- package/src/runtime/.runtime-opcua-UwafxO/tsconfig.json +0 -13
- package/src/runtime/.runtime-opcua-UwafxO/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-opcua-ecaSee/server/router.ts +0 -17
- package/src/runtime/.runtime-opcua-ecaSee/src/cell.ts +0 -45
- package/src/runtime/.runtime-opcua-ecaSee/src/controller.ts +0 -58
- package/src/runtime/.runtime-opcua-ecaSee/src/lane.ts +0 -6
- package/src/runtime/.runtime-opcua-ecaSee/src/main.ts +0 -11
- package/src/runtime/.runtime-opcua-ecaSee/src/services.ts +0 -8
- package/src/runtime/.runtime-opcua-ecaSee/tsconfig.json +0 -13
- package/src/runtime/.runtime-opcua-ecaSee/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-opcua-y91UCm/server/router.ts +0 -17
- package/src/runtime/.runtime-opcua-y91UCm/src/cell.ts +0 -45
- package/src/runtime/.runtime-opcua-y91UCm/src/controller.ts +0 -58
- package/src/runtime/.runtime-opcua-y91UCm/src/lane.ts +0 -6
- package/src/runtime/.runtime-opcua-y91UCm/src/main.ts +0 -11
- package/src/runtime/.runtime-opcua-y91UCm/src/services.ts +0 -8
- package/src/runtime/.runtime-opcua-y91UCm/tsconfig.json +0 -13
- package/src/runtime/.runtime-opcua-y91UCm/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-ybIxYh/server/router.ts +0 -17
- package/src/runtime/.runtime-ybIxYh/src/cell.ts +0 -45
- package/src/runtime/.runtime-ybIxYh/src/controller.ts +0 -58
- package/src/runtime/.runtime-ybIxYh/src/lane.ts +0 -6
- package/src/runtime/.runtime-ybIxYh/src/main.ts +0 -11
- package/src/runtime/.runtime-ybIxYh/src/services.ts +0 -8
- package/src/runtime/.runtime-ybIxYh/tsconfig.json +0 -13
- package/src/runtime/.runtime-ybIxYh/tsplc.config.ts +0 -31
- package/stdlib/math.ts +0 -23
- package/types/plc.d.ts +0 -4
package/src/project/server.ts
CHANGED
|
@@ -2,8 +2,8 @@ import { existsSync } from "node:fs";
|
|
|
2
2
|
import { mkdir, mkdtemp, rename, rm } from "node:fs/promises";
|
|
3
3
|
import { createRequire } from "node:module";
|
|
4
4
|
import { dirname, join, resolve } from "node:path";
|
|
5
|
-
import {
|
|
6
|
-
import
|
|
5
|
+
import { projectSlugs, type ResolvedTsplcConfig } from "@tsplc/config";
|
|
6
|
+
import { generatedDirectory } from "@tsplc/config/project-files";
|
|
7
7
|
import { z } from "zod";
|
|
8
8
|
|
|
9
9
|
const resolveServerModule = (root: string, name: string) =>
|
|
@@ -13,14 +13,10 @@ const resolveServerModule = (root: string, name: string) =>
|
|
|
13
13
|
? resolve(root, name)
|
|
14
14
|
: createRequire(join(root, "package.json")).resolve(name);
|
|
15
15
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
.replace(/^-|-$/g, "")
|
|
21
|
-
.toLowerCase();
|
|
22
|
-
|
|
23
|
-
/** `build/server/<name>-services<extension>`, where the server's script or executable goes. */
|
|
16
|
+
/**
|
|
17
|
+
* `.tsplc/server/<name>-services<extension>`, where the server's script or executable goes: beside
|
|
18
|
+
* the other generated outputs, never inside `build.outDir`, the CODESYS engineering cache.
|
|
19
|
+
*/
|
|
24
20
|
const serverOutput = (
|
|
25
21
|
config: ResolvedTsplcConfig,
|
|
26
22
|
root: string,
|
|
@@ -28,8 +24,9 @@ const serverOutput = (
|
|
|
28
24
|
) =>
|
|
29
25
|
resolve(
|
|
30
26
|
root,
|
|
31
|
-
|
|
32
|
-
|
|
27
|
+
generatedDirectory,
|
|
28
|
+
"server",
|
|
29
|
+
`${projectSlugs.kebab(config.name)}-services${extension}`,
|
|
33
30
|
);
|
|
34
31
|
|
|
35
32
|
/** What `checkServer` posts to its worker: the server entry and the project root diagnostics are relative to. */
|
|
@@ -43,17 +40,6 @@ export const serverCheckResultSchema = z.strictObject({
|
|
|
43
40
|
failure: z.string().nullable(),
|
|
44
41
|
});
|
|
45
42
|
|
|
46
|
-
/** The type-check script beside this module: its source, or the bundle a built package ships. */
|
|
47
|
-
const serverCheckScript = () => {
|
|
48
|
-
const script = ["./server-check.ts", "./server-check.js"]
|
|
49
|
-
.map((path) => fileURLToPath(new URL(path, import.meta.url)))
|
|
50
|
-
.find((path) => existsSync(path));
|
|
51
|
-
if (!script) {
|
|
52
|
-
throw new Error("The server type-check script is missing from this build.");
|
|
53
|
-
}
|
|
54
|
-
return script;
|
|
55
|
-
};
|
|
56
|
-
|
|
57
43
|
/** A server entry that passed its type check, and the runtime it is built for. */
|
|
58
44
|
export interface CheckedServer {
|
|
59
45
|
entry: string;
|
|
@@ -73,7 +59,7 @@ export const checkServer = async (
|
|
|
73
59
|
throw new Error("Configure server.runtime to build the server.");
|
|
74
60
|
}
|
|
75
61
|
const entry = resolveServerModule(root, runtime.entry);
|
|
76
|
-
const worker = new Worker(
|
|
62
|
+
const worker = new Worker(import.meta.resolve("#server-check"));
|
|
77
63
|
try {
|
|
78
64
|
const answer = await new Promise<unknown>((resolve, reject) => {
|
|
79
65
|
worker.onmessage = (event: MessageEvent) => resolve(event.data);
|
|
@@ -94,9 +80,19 @@ export const checkServer = async (
|
|
|
94
80
|
}
|
|
95
81
|
};
|
|
96
82
|
|
|
83
|
+
/**
|
|
84
|
+
* `checkServer` started now, for a caller that builds the PLC program meanwhile: a failed PLC
|
|
85
|
+
* build reports its own diagnostics, so the check's outcome is read only after a success.
|
|
86
|
+
*/
|
|
87
|
+
export const startServerCheck = (config: ResolvedTsplcConfig, root: string) => {
|
|
88
|
+
const check = checkServer(config, root);
|
|
89
|
+
check.catch(() => undefined);
|
|
90
|
+
return check;
|
|
91
|
+
};
|
|
92
|
+
|
|
97
93
|
/**
|
|
98
94
|
* Bundles the host entry, which starts its own listener, into one minified script,
|
|
99
|
-
*
|
|
95
|
+
* `.tsplc/server/<name>-services.js`, which the plant agent runs on its own Bun. It is a few MB
|
|
100
96
|
* where an executable, which embeds Bun, is over 100, and deployments upload it.
|
|
101
97
|
*/
|
|
102
98
|
export const bundleServer = async (
|
|
@@ -125,7 +121,7 @@ export const bundleServer = async (
|
|
|
125
121
|
return outFile;
|
|
126
122
|
};
|
|
127
123
|
|
|
128
|
-
/** Compiles the host entry, which starts its own listener, into
|
|
124
|
+
/** Compiles the host entry, which starts its own listener, into `.tsplc/server/<name>-services`. */
|
|
129
125
|
export const buildServer = async (
|
|
130
126
|
{ entry, runtime }: CheckedServer,
|
|
131
127
|
config: ResolvedTsplcConfig,
|
package/src/project/terminal.ts
CHANGED
|
@@ -17,8 +17,19 @@ export const choose = async <T>(
|
|
|
17
17
|
}
|
|
18
18
|
};
|
|
19
19
|
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
/** Asks a yes-or-no question; no unless the answer is yes. */
|
|
21
|
+
export const confirm = async (question: string) =>
|
|
22
|
+
/^y(es)?$/i.test(await ask(`${question} [y/N]: `));
|
|
23
|
+
|
|
24
|
+
/** Goes on when `--yes` is given or the answer is yes, else stops the command as cancelled. */
|
|
25
|
+
export const confirmOrCancel = async (
|
|
26
|
+
question: string,
|
|
27
|
+
flags: { yes?: boolean },
|
|
28
|
+
) => {
|
|
29
|
+
if (!flags.yes && !(await confirm(question))) {
|
|
30
|
+
throw new Error("Cancelled.");
|
|
31
|
+
}
|
|
32
|
+
};
|
|
22
33
|
|
|
23
34
|
export const progress = (message: string) => console.log(` ${message}`);
|
|
24
35
|
|
package/src/runtime/async.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import { httpErrors, jobError } from "@tsplc/compiler/ir";
|
|
1
|
+
import { failureCode, httpErrors, jobError } from "@tsplc/compiler/ir";
|
|
2
2
|
import { plcBufferBytes } from "@tsplc/server/wire";
|
|
3
|
+
import type { resolveHttpCredentials } from "../codesys/endpoints";
|
|
3
4
|
import type { Transport } from "./transport";
|
|
4
5
|
|
|
5
6
|
/** How the program's HTTP calls reach their servers, as the build compiled them. */
|
|
6
7
|
export interface HttpSettings {
|
|
7
|
-
/** Bearer tokens by the exact origin they are sent to. */
|
|
8
|
-
|
|
8
|
+
/** Bearer tokens by the exact origin they are sent to, or why the build refuses the credentials. */
|
|
9
|
+
credentials: ReturnType<typeof resolveHttpCredentials>;
|
|
9
10
|
timeoutMs: number;
|
|
10
11
|
}
|
|
11
12
|
|
|
@@ -43,18 +44,23 @@ const boundedBody = async (response: Response) => {
|
|
|
43
44
|
/**
|
|
44
45
|
* One HTTP exchange as a compiled call makes it: bounded JSON both ways, the bearer token of the
|
|
45
46
|
* URL's origin, the configured timeout and no retry. It rejects with the compiled call's error
|
|
46
|
-
* code, a number,
|
|
47
|
+
* code, a number; with credentials the build refuses, a program can't call at all, so it rejects
|
|
48
|
+
* with that refusal as an Error.
|
|
47
49
|
*/
|
|
48
50
|
export const httpCall = async (
|
|
49
|
-
settings:
|
|
51
|
+
settings: HttpSettings,
|
|
50
52
|
url: string,
|
|
51
53
|
request: { method: string; body?: string },
|
|
52
54
|
): Promise<unknown> => {
|
|
55
|
+
const { credentials } = settings;
|
|
56
|
+
if ("error" in credentials) {
|
|
57
|
+
throw new Error(credentials.error);
|
|
58
|
+
}
|
|
53
59
|
const { body } = request;
|
|
54
60
|
if (body !== undefined && Buffer.byteLength(body, "utf8") > plcBufferBytes) {
|
|
55
61
|
throw jobError.tooLarge;
|
|
56
62
|
}
|
|
57
|
-
const token =
|
|
63
|
+
const token = credentials.tokens.get(URL.parse(url)?.origin ?? "");
|
|
58
64
|
const response = await globalThis
|
|
59
65
|
.fetch(url, {
|
|
60
66
|
method: request.method,
|
|
@@ -126,7 +132,7 @@ export const createAsync = (
|
|
|
126
132
|
// A sleep resumes on a later scan, even `sleep(ms(0))`: scans never yield to timers.
|
|
127
133
|
sleep: (duration: number) =>
|
|
128
134
|
new Promise<void>((resolve) => setTimeout(resolve, duration)),
|
|
129
|
-
errorCode:
|
|
135
|
+
errorCode: failureCode,
|
|
130
136
|
asyncDiagnostics: () => ({ valid: true, ...diagnostics }),
|
|
131
137
|
cancelAsync: unsupported("cancelAsync"),
|
|
132
138
|
opcuaCall: unsupported("opcuaCall"),
|
|
@@ -136,13 +142,11 @@ export const createAsync = (
|
|
|
136
142
|
};
|
|
137
143
|
};
|
|
138
144
|
|
|
139
|
-
/** What a launch dropped because its call site was busy reports, as a compiled launch does. */
|
|
140
|
-
const capacityError = jobError.noRequestSlot;
|
|
141
|
-
|
|
142
145
|
/**
|
|
143
146
|
* A compiled program stores one invocation of each async method per instance: launching it
|
|
144
|
-
* from scan code while that invocation runs is dropped, counted with
|
|
145
|
-
* so neither its `then` nor its `catch` runs. The program's classes get the
|
|
147
|
+
* from scan code while that invocation runs is dropped, counted with `jobError.noRequestSlot`
|
|
148
|
+
* and never settles, so neither its `then` nor its `catch` runs. The program's classes get the
|
|
149
|
+
* same rule here.
|
|
146
150
|
* A launch from a continuation is an awaited child, which has storage of its own, so it runs.
|
|
147
151
|
* `failed` names a method's rejected promise for an unhandled-failure record.
|
|
148
152
|
*/
|
|
@@ -176,7 +180,7 @@ export const oneLaunchAtATime = (
|
|
|
176
180
|
running.set(this, methods);
|
|
177
181
|
if (methods.has(method)) {
|
|
178
182
|
options.diagnostics.count += 1;
|
|
179
|
-
options.diagnostics.lastError =
|
|
183
|
+
options.diagnostics.lastError = jobError.noRequestSlot;
|
|
180
184
|
return new Promise(() => {});
|
|
181
185
|
}
|
|
182
186
|
const started: unknown = original.apply(this, input);
|
package/src/runtime/launch.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { VerifiedCoreModule } from "@tsplc/compiler/ir";
|
|
2
2
|
import { generatedDirectory } from "@tsplc/config/project-files";
|
|
3
|
-
import { serviceOfPath } from "@tsplc/server";
|
|
3
|
+
import { serviceOfPath } from "@tsplc/server/wire";
|
|
4
4
|
import { z } from "zod";
|
|
5
5
|
|
|
6
6
|
/** The Bun runtime's files, relative to the directory that holds a project's config. */
|
|
@@ -23,7 +23,10 @@ export const launchSchema = z.strictObject({
|
|
|
23
23
|
config: z.string().nonempty(),
|
|
24
24
|
target: z.string().nonempty().nullable(),
|
|
25
25
|
/** Each service procedure's route, with its wire-contract version: `/tsplc/services/<version>/<path>` by `<path>`. */
|
|
26
|
-
services: z.record(
|
|
26
|
+
services: z.record(
|
|
27
|
+
z.string(),
|
|
28
|
+
z.string().refine((route) => serviceOfPath(route) !== undefined),
|
|
29
|
+
),
|
|
27
30
|
});
|
|
28
31
|
export type Launch = z.infer<typeof launchSchema>;
|
|
29
32
|
|
package/src/runtime/main.ts
CHANGED
|
@@ -6,8 +6,9 @@
|
|
|
6
6
|
* Usage: bun main.ts <launch.json> [port]
|
|
7
7
|
*/
|
|
8
8
|
import { readFileSync } from "node:fs";
|
|
9
|
-
import { rm, writeFile } from "node:fs/promises";
|
|
9
|
+
import { rename, rm, writeFile } from "node:fs/promises";
|
|
10
10
|
import { dirname, join } from "node:path";
|
|
11
|
+
import { tcpPort } from "@tsplc/config/build";
|
|
11
12
|
import { launchSchema, type RuntimeState, runtimeFiles } from "./launch";
|
|
12
13
|
import { runtimeEndpoint, startRuntime } from "./runtime";
|
|
13
14
|
|
|
@@ -21,17 +22,21 @@ try {
|
|
|
21
22
|
const runtime = await startRuntime({
|
|
22
23
|
launch: launchSchema.parse(JSON.parse(readFileSync(launchFile, "utf8"))),
|
|
23
24
|
environment: process.env,
|
|
24
|
-
port:
|
|
25
|
+
port:
|
|
26
|
+
port === undefined ? runtimeEndpoint.port : tcpPort.parse(Number(port)),
|
|
25
27
|
log,
|
|
26
28
|
});
|
|
29
|
+
// Written whole under another name first, so a reader never sees half of it.
|
|
30
|
+
const written = `${stateFile}.${process.pid}`;
|
|
27
31
|
await writeFile(
|
|
28
|
-
|
|
32
|
+
written,
|
|
29
33
|
`${JSON.stringify({
|
|
30
34
|
pid: process.pid,
|
|
31
35
|
endpoint: runtime.endpoint,
|
|
32
36
|
startedAt: new Date().toISOString(),
|
|
33
37
|
} satisfies RuntimeState)}\n`,
|
|
34
38
|
);
|
|
39
|
+
await rename(written, stateFile);
|
|
35
40
|
const stop = async (signal: string) => {
|
|
36
41
|
log(`Stopping on ${signal}.`);
|
|
37
42
|
try {
|
package/src/runtime/process.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { spawn, spawnSync } from "node:child_process";
|
|
2
|
-
import {
|
|
2
|
+
import { openSync } from "node:fs";
|
|
3
3
|
import { mkdir, readFile, rm, writeFile } from "node:fs/promises";
|
|
4
4
|
import { join, relative } from "node:path";
|
|
5
5
|
import { setTimeout as delay } from "node:timers/promises";
|
|
6
6
|
import { fileURLToPath } from "node:url";
|
|
7
7
|
import { type ProgressStream, progressLog } from "@tsplc/build";
|
|
8
|
+
import type { Environment } from "@tsplc/config";
|
|
8
9
|
import {
|
|
9
10
|
type Launch,
|
|
10
11
|
type RuntimeState,
|
|
@@ -12,28 +13,19 @@ import {
|
|
|
12
13
|
runtimeFiles,
|
|
13
14
|
runtimeStateSchema,
|
|
14
15
|
} from "./launch";
|
|
16
|
+
import { isMissingFile } from "./missing-file";
|
|
15
17
|
|
|
16
18
|
/** How long a runtime has to start serving, and to stop. */
|
|
17
19
|
const startTimeoutMs = 60_000;
|
|
18
20
|
const stopTimeoutMs = 15_000;
|
|
19
21
|
|
|
20
|
-
/**
|
|
21
|
-
const runtimeScript = () => {
|
|
22
|
-
const script = ["./main.ts", "./bun-runtime.js"]
|
|
23
|
-
.map((path) => fileURLToPath(new URL(path, import.meta.url)))
|
|
24
|
-
.find((path) => existsSync(path));
|
|
25
|
-
if (!script) {
|
|
26
|
-
throw new Error("The Bun runtime's script is missing from this build.");
|
|
27
|
-
}
|
|
28
|
-
return script;
|
|
29
|
-
};
|
|
30
|
-
|
|
22
|
+
/** Whether `pid` runs: a process this user may not signal runs too. */
|
|
31
23
|
const alive = (pid: number) => {
|
|
32
24
|
try {
|
|
33
25
|
process.kill(pid, 0);
|
|
34
26
|
return true;
|
|
35
|
-
} catch {
|
|
36
|
-
return
|
|
27
|
+
} catch (cause) {
|
|
28
|
+
return cause instanceof Error && "code" in cause && cause.code === "EPERM";
|
|
37
29
|
}
|
|
38
30
|
};
|
|
39
31
|
|
|
@@ -42,6 +34,7 @@ const isRuntime = (pid: number, launchFile: string) => {
|
|
|
42
34
|
if (!alive(pid)) {
|
|
43
35
|
return false;
|
|
44
36
|
}
|
|
37
|
+
// Windows has no `ps` to name the process's command, so any process with the number counts.
|
|
45
38
|
if (process.platform === "win32") {
|
|
46
39
|
return true;
|
|
47
40
|
}
|
|
@@ -51,15 +44,24 @@ const isRuntime = (pid: number, launchFile: string) => {
|
|
|
51
44
|
return command.includes(launchFile);
|
|
52
45
|
};
|
|
53
46
|
|
|
47
|
+
/** The running runtime's state; undefined when no runtime serves. */
|
|
54
48
|
const readState = async (file: string): Promise<RuntimeState | undefined> => {
|
|
55
|
-
const text = await readFile(file, "utf8").catch(() =>
|
|
49
|
+
const text = await readFile(file, "utf8").catch((cause: unknown) => {
|
|
50
|
+
if (isMissingFile(cause)) {
|
|
51
|
+
return undefined;
|
|
52
|
+
}
|
|
53
|
+
throw cause;
|
|
54
|
+
});
|
|
56
55
|
if (text === undefined) {
|
|
57
56
|
return undefined;
|
|
58
57
|
}
|
|
59
58
|
try {
|
|
60
59
|
return runtimeStateSchema.parse(JSON.parse(text));
|
|
61
|
-
} catch {
|
|
62
|
-
|
|
60
|
+
} catch (cause) {
|
|
61
|
+
throw new Error(
|
|
62
|
+
`${file} is not a Bun runtime's state: delete it once no runtime runs.`,
|
|
63
|
+
{ cause },
|
|
64
|
+
);
|
|
63
65
|
}
|
|
64
66
|
};
|
|
65
67
|
|
|
@@ -93,7 +95,7 @@ const stopRunning = async (directory: string, log: (line: string) => void) => {
|
|
|
93
95
|
export const runInBun = async (options: {
|
|
94
96
|
root: string;
|
|
95
97
|
launch: Launch;
|
|
96
|
-
environment:
|
|
98
|
+
environment: Environment;
|
|
97
99
|
foreground: boolean;
|
|
98
100
|
progress: ProgressStream;
|
|
99
101
|
}) => {
|
|
@@ -104,7 +106,10 @@ export const runInBun = async (options: {
|
|
|
104
106
|
const launchFile = join(directory, runtimeFiles.launch);
|
|
105
107
|
await writeFile(launchFile, `${JSON.stringify(options.launch, null, 2)}\n`);
|
|
106
108
|
const logFile = join(directory, runtimeFiles.log);
|
|
107
|
-
const command = [
|
|
109
|
+
const command = [
|
|
110
|
+
fileURLToPath(import.meta.resolve("#bun-runtime")),
|
|
111
|
+
launchFile,
|
|
112
|
+
];
|
|
108
113
|
if (options.foreground) {
|
|
109
114
|
log("Running the program in Bun; stop it with Ctrl-C");
|
|
110
115
|
const child = spawn(process.execPath, command, {
|
package/src/runtime/program.ts
CHANGED
|
@@ -1,30 +1,19 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { realpathSync } from "node:fs";
|
|
2
2
|
import { plugin } from "bun";
|
|
3
3
|
import ts from "typescript";
|
|
4
|
-
import {
|
|
5
|
-
|
|
4
|
+
import {
|
|
5
|
+
bindingNames,
|
|
6
|
+
bindingRegistry,
|
|
7
|
+
loadWithBindingRegistry,
|
|
8
|
+
} from "../frontend/elaborate/capture-protocol";
|
|
6
9
|
|
|
7
10
|
/** The SDK modules a program imports, as the runtime implements them, by export name. */
|
|
8
11
|
export type Sdk = Readonly<Record<string, Readonly<Record<string, unknown>>>>;
|
|
9
12
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
values: Record<string, () => unknown>;
|
|
15
|
-
classes: Record<string, unknown>;
|
|
16
|
-
functions: Record<string, unknown>;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
const isRegistry = (value: unknown): value is Registry =>
|
|
20
|
-
typeof value === "object" &&
|
|
21
|
-
value !== null &&
|
|
22
|
-
"values" in value &&
|
|
23
|
-
"classes" in value &&
|
|
24
|
-
"functions" in value;
|
|
25
|
-
|
|
26
|
-
const escapeRegExp = (text: string) =>
|
|
27
|
-
text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
13
|
+
/** An SDK module's implementation: every value its declaration exports, and no other. */
|
|
14
|
+
export type SdkModule<Declared> = {
|
|
15
|
+
readonly [Name in keyof Declared]: unknown;
|
|
16
|
+
};
|
|
28
17
|
|
|
29
18
|
/**
|
|
30
19
|
* Loads the program's modules in this process: each `@tsplc/compiler/*` SDK module it imports
|
|
@@ -38,40 +27,18 @@ export const loadProgram = async (options: {
|
|
|
38
27
|
/** Each scheduled program's module and export. */
|
|
39
28
|
entries: Readonly<Record<string, { module: string; export: string }>>;
|
|
40
29
|
}) => {
|
|
41
|
-
(globalThis as Record<symbol, unknown>)[sdkKey] = options.sdk;
|
|
42
|
-
const files = new Set(options.sources.map((file) => realpathSync(file)));
|
|
43
30
|
plugin({
|
|
44
31
|
name: "tsplc-runtime",
|
|
45
32
|
setup(build) {
|
|
46
33
|
for (const [specifier, exports] of Object.entries(options.sdk)) {
|
|
47
|
-
build.module(specifier, () => ({
|
|
48
|
-
contents: `const sdk = globalThis[Symbol.for("tsplc.runtime.sdk")][${JSON.stringify(specifier)}];\n${Object.keys(
|
|
49
|
-
exports,
|
|
50
|
-
)
|
|
51
|
-
.map((name) => `export const ${name} = sdk.${name};`)
|
|
52
|
-
.join("\n")}\n`,
|
|
53
|
-
loader: "js",
|
|
54
|
-
}));
|
|
55
|
-
}
|
|
56
|
-
if (!files.size) {
|
|
57
|
-
return;
|
|
34
|
+
build.module(specifier, () => ({ exports, loader: "object" }));
|
|
58
35
|
}
|
|
59
|
-
build.
|
|
60
|
-
|
|
61
|
-
(
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
text,
|
|
66
|
-
bindingNames(
|
|
67
|
-
ts.createSourceFile(path, text, ts.ScriptTarget.Latest, true),
|
|
68
|
-
),
|
|
69
|
-
registryExport,
|
|
70
|
-
),
|
|
71
|
-
loader: path.endsWith("x") ? "tsx" : "ts",
|
|
72
|
-
};
|
|
73
|
-
},
|
|
74
|
-
);
|
|
36
|
+
loadWithBindingRegistry(build, options.sources, ({ file, source }) => ({
|
|
37
|
+
source,
|
|
38
|
+
bindings: bindingNames(
|
|
39
|
+
ts.createSourceFile(file, source, ts.ScriptTarget.Latest, true),
|
|
40
|
+
),
|
|
41
|
+
}));
|
|
75
42
|
},
|
|
76
43
|
});
|
|
77
44
|
const modules = new Map<string, Record<string, unknown>>();
|
|
@@ -97,13 +64,13 @@ export const loadProgram = async (options: {
|
|
|
97
64
|
exported();
|
|
98
65
|
});
|
|
99
66
|
}
|
|
100
|
-
for (const file of
|
|
67
|
+
for (const file of options.sources) {
|
|
101
68
|
await load(file);
|
|
102
69
|
}
|
|
103
70
|
const registries = [...modules]
|
|
104
71
|
.flatMap(([file, loaded]) => {
|
|
105
|
-
const registry = loaded
|
|
106
|
-
return
|
|
72
|
+
const registry = bindingRegistry(loaded);
|
|
73
|
+
return registry ? [{ file, registry }] : [];
|
|
107
74
|
})
|
|
108
75
|
// The entries' modules first: their bindings win a name two modules share.
|
|
109
76
|
.sort(
|
package/src/runtime/retain.ts
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import { createHash } from "node:crypto";
|
|
2
2
|
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
3
3
|
import { dirname } from "node:path";
|
|
4
|
-
import
|
|
5
|
-
PublishedGlobal,
|
|
6
|
-
PublishedScalar,
|
|
7
|
-
PublishedValue,
|
|
4
|
+
import {
|
|
5
|
+
type PublishedGlobal,
|
|
6
|
+
type PublishedScalar,
|
|
7
|
+
type PublishedValue,
|
|
8
|
+
publishedScalars,
|
|
8
9
|
} from "@tsplc/opcua/server";
|
|
9
10
|
import { z } from "zod";
|
|
10
|
-
import {
|
|
11
|
+
import { isMissingFile } from "./missing-file";
|
|
11
12
|
|
|
12
13
|
const retainedValue = z.union([
|
|
13
14
|
z.boolean(),
|
|
@@ -47,10 +48,12 @@ export const createRetention = (
|
|
|
47
48
|
scalar: (path: string) => PublishedScalar | undefined,
|
|
48
49
|
file: string,
|
|
49
50
|
) => {
|
|
50
|
-
const scalars = globals
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
51
|
+
const scalars = globals
|
|
52
|
+
.flatMap(publishedScalars)
|
|
53
|
+
.flatMap(({ path, type }) => {
|
|
54
|
+
const bound = scalar(path);
|
|
55
|
+
return bound ? [{ path, type, bound }] : [];
|
|
56
|
+
});
|
|
54
57
|
const layout = createHash("sha256")
|
|
55
58
|
.update(JSON.stringify(scalars.map(({ path, type }) => [path, type])))
|
|
56
59
|
.digest("hex");
|
|
@@ -62,7 +65,12 @@ export const createRetention = (
|
|
|
62
65
|
if (!scalars.length) {
|
|
63
66
|
return { none: true };
|
|
64
67
|
}
|
|
65
|
-
const text = await readFile(file, "utf8").catch(() =>
|
|
68
|
+
const text = await readFile(file, "utf8").catch((cause: unknown) => {
|
|
69
|
+
if (isMissingFile(cause)) {
|
|
70
|
+
return undefined;
|
|
71
|
+
}
|
|
72
|
+
throw cause;
|
|
73
|
+
});
|
|
66
74
|
if (text === undefined) {
|
|
67
75
|
return { none: true };
|
|
68
76
|
}
|