@tsplc/compiler 0.5.6 → 0.5.7
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 +139 -30
- package/dist/bun-runtime.js.map +8 -7
- package/dist/cli.js +8 -5
- package/dist/cli.js.map +3 -3
- package/dist/codesys.js +216 -85
- package/dist/codesys.js.map +8 -8
- package/dist/project.js +8 -5
- package/dist/project.js.map +3 -3
- package/package.json +9 -9
- package/src/codesys/emit.ts +8 -2
- package/src/codesys/http.ts +114 -72
- package/src/codesys/instrumentation.ts +30 -6
- package/src/codesys/routines.ts +39 -9
- package/src/codesys/st-files.ts +73 -6
- package/src/codesys/telemetry.ts +70 -35
- package/src/runtime/async.ts +15 -12
- package/src/runtime/launch.ts +11 -4
- package/src/runtime/runtime.ts +26 -23
- package/src/runtime/telemetry.ts +14 -6
- package/src/runtime/transport.ts +172 -0
package/dist/bun-runtime.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
// packages/compiler/src/runtime/main.ts
|
|
5
5
|
import { readFileSync as readFileSync2 } from "fs";
|
|
6
6
|
import { rm, writeFile as writeFile2 } from "fs/promises";
|
|
7
|
-
import { dirname as dirname2, join as
|
|
7
|
+
import { dirname as dirname2, join as join3 } from "path";
|
|
8
8
|
|
|
9
9
|
// packages/compiler/src/runtime/launch.ts
|
|
10
10
|
import { generatedDirectory } from "@tsplc/config/project-files";
|
|
@@ -30,8 +30,8 @@ var runtimeStateSchema = z.strictObject({
|
|
|
30
30
|
});
|
|
31
31
|
|
|
32
32
|
// packages/compiler/src/runtime/runtime.ts
|
|
33
|
-
import { readFile as
|
|
34
|
-
import { isAbsolute, join, relative as relative2, resolve } from "path";
|
|
33
|
+
import { readFile as readFile3 } from "fs/promises";
|
|
34
|
+
import { isAbsolute, join as join2, relative as relative2, resolve } from "path";
|
|
35
35
|
import { errorName as errorName2 } from "@tsplc/compiler/ir";
|
|
36
36
|
import { controllerDeviceName, loadTsplcConfig } from "@tsplc/config";
|
|
37
37
|
import { emittedStDirectory } from "@tsplc/config/project-files";
|
|
@@ -222,14 +222,12 @@ var unsupported = (name) => () => {
|
|
|
222
222
|
};
|
|
223
223
|
var createAsync = (settings, diagnostics) => {
|
|
224
224
|
const call = (path, input) => {
|
|
225
|
-
const route = settings.routes[path];
|
|
226
|
-
|
|
225
|
+
const route = settings.services.routes[path];
|
|
226
|
+
const { transport } = settings.services;
|
|
227
|
+
if (!route || !transport) {
|
|
227
228
|
return Promise.reject(new Error(`The build compiled no call to the service ${path}.`));
|
|
228
229
|
}
|
|
229
|
-
return
|
|
230
|
-
method: "POST",
|
|
231
|
-
body: JSON.stringify(input)
|
|
232
|
-
});
|
|
230
|
+
return transport.call(route, JSON.stringify(input));
|
|
233
231
|
};
|
|
234
232
|
const client = (path) => new Proxy(() => {
|
|
235
233
|
return;
|
|
@@ -239,7 +237,7 @@ var createAsync = (settings, diagnostics) => {
|
|
|
239
237
|
});
|
|
240
238
|
return {
|
|
241
239
|
createServiceClient: () => client([]),
|
|
242
|
-
fetch: (url, options = {}) => httpCall(settings, url, {
|
|
240
|
+
fetch: (url, options = {}) => httpCall(settings.http, url, {
|
|
243
241
|
method: options.method ?? "GET",
|
|
244
242
|
...options.json !== undefined && {
|
|
245
243
|
body: JSON.stringify(options.json)
|
|
@@ -1013,6 +1011,7 @@ var createStorage = (manifest, binding) => {
|
|
|
1013
1011
|
};
|
|
1014
1012
|
|
|
1015
1013
|
// packages/compiler/src/runtime/telemetry.ts
|
|
1014
|
+
import { httpErrors as httpErrors3 } from "@tsplc/compiler/ir";
|
|
1016
1015
|
import { plcBufferBytes as plcBufferBytes2 } from "@tsplc/server/wire";
|
|
1017
1016
|
var batchWaitMs = 200;
|
|
1018
1017
|
var createTelemetry = (options) => {
|
|
@@ -1080,10 +1079,16 @@ var createTelemetry = (options) => {
|
|
|
1080
1079
|
}
|
|
1081
1080
|
const sent = state.queued.slice(0, batch.count);
|
|
1082
1081
|
state.sending = true;
|
|
1083
|
-
|
|
1082
|
+
const settle = () => {
|
|
1084
1083
|
state.queued = state.queued.filter((queued) => !sent.includes(queued));
|
|
1085
1084
|
state.waitingSince = options.clock();
|
|
1086
|
-
}
|
|
1085
|
+
};
|
|
1086
|
+
post(batch.body).then(settle).catch((reason) => {
|
|
1087
|
+
if (reason === httpErrors3.base + 400) {
|
|
1088
|
+
state.dropped += sent.length;
|
|
1089
|
+
settle();
|
|
1090
|
+
return;
|
|
1091
|
+
}
|
|
1087
1092
|
state.waitingSince = options.clock();
|
|
1088
1093
|
}).finally(() => {
|
|
1089
1094
|
state.sending = false;
|
|
@@ -1092,6 +1097,116 @@ var createTelemetry = (options) => {
|
|
|
1092
1097
|
return { record, send };
|
|
1093
1098
|
};
|
|
1094
1099
|
|
|
1100
|
+
// packages/compiler/src/runtime/transport.ts
|
|
1101
|
+
import { readFile as readFile2 } from "fs/promises";
|
|
1102
|
+
import { join } from "path";
|
|
1103
|
+
import { httpErrors as httpErrors4, jobError as jobError3 } from "@tsplc/compiler/ir";
|
|
1104
|
+
import { emittedFiles } from "@tsplc/config/project-files";
|
|
1105
|
+
import { plcBufferBytes as plcBufferBytes3 } from "@tsplc/server/wire";
|
|
1106
|
+
import { z as z5 } from "zod";
|
|
1107
|
+
var httpTransport = (settings, baseUrl) => ({
|
|
1108
|
+
call: (route, body) => httpCall(settings, `${baseUrl}${route}`, { method: "POST", body })
|
|
1109
|
+
});
|
|
1110
|
+
var serviceChannel = z5.strictObject({
|
|
1111
|
+
path: z5.string(),
|
|
1112
|
+
request: z5.string(),
|
|
1113
|
+
requestId: z5.string(),
|
|
1114
|
+
reply: z5.string(),
|
|
1115
|
+
replyId: z5.string(),
|
|
1116
|
+
epoch: z5.string()
|
|
1117
|
+
});
|
|
1118
|
+
var readServiceChannels = async (st) => {
|
|
1119
|
+
const text = await readFile2(join(st, emittedFiles.machines), "utf8");
|
|
1120
|
+
const listed = /export const serviceChannels=([\s\S]*?) as const;/.exec(text);
|
|
1121
|
+
if (!listed?.[1]) {
|
|
1122
|
+
throw new Error(`${emittedFiles.machines} lists no service channels.`);
|
|
1123
|
+
}
|
|
1124
|
+
return z5.array(serviceChannel).parse(JSON.parse(listed[1]));
|
|
1125
|
+
};
|
|
1126
|
+
var sequenceLimit = 2 ** 32;
|
|
1127
|
+
var replyPollMs = 5;
|
|
1128
|
+
var decodeReply = (rest) => {
|
|
1129
|
+
if (Buffer.byteLength(rest, "utf8") < 4) {
|
|
1130
|
+
throw jobError3.malformedResponse;
|
|
1131
|
+
}
|
|
1132
|
+
if (rest.startsWith("4|")) {
|
|
1133
|
+
throw httpErrors4.base + 400;
|
|
1134
|
+
}
|
|
1135
|
+
if (!rest.startsWith("0|")) {
|
|
1136
|
+
throw jobError3.network;
|
|
1137
|
+
}
|
|
1138
|
+
try {
|
|
1139
|
+
return JSON.parse(rest.slice(2));
|
|
1140
|
+
} catch {
|
|
1141
|
+
throw jobError3.malformedResponse;
|
|
1142
|
+
}
|
|
1143
|
+
};
|
|
1144
|
+
var opcuaSlots = (channels, timeoutMs) => {
|
|
1145
|
+
const values = new Map(channels.flatMap((channel) => [
|
|
1146
|
+
channel.request,
|
|
1147
|
+
channel.requestId,
|
|
1148
|
+
channel.reply,
|
|
1149
|
+
channel.replyId,
|
|
1150
|
+
channel.epoch
|
|
1151
|
+
].map((path) => [path, ""])));
|
|
1152
|
+
const read = (path) => values.get(path) ?? "";
|
|
1153
|
+
const slots = new Map(channels.map((channel) => [channel, { inUse: false, sequence: 0 }]));
|
|
1154
|
+
const awaitReply = async (channel, epoch, identity, deadline) => {
|
|
1155
|
+
const prefix = `${identity}|`;
|
|
1156
|
+
for (;; ) {
|
|
1157
|
+
if (performance.now() >= deadline) {
|
|
1158
|
+
throw jobError3.timeout;
|
|
1159
|
+
}
|
|
1160
|
+
if (read(channel.epoch) !== epoch) {
|
|
1161
|
+
throw jobError3.network;
|
|
1162
|
+
}
|
|
1163
|
+
const reply = read(channel.reply);
|
|
1164
|
+
if (read(channel.replyId) === identity && reply.startsWith(prefix)) {
|
|
1165
|
+
return decodeReply(reply.slice(prefix.length));
|
|
1166
|
+
}
|
|
1167
|
+
await Bun.sleep(replyPollMs);
|
|
1168
|
+
}
|
|
1169
|
+
};
|
|
1170
|
+
const call = async (route, body) => {
|
|
1171
|
+
const deadline = performance.now() + timeoutMs;
|
|
1172
|
+
const channel = channels.find((candidate) => candidate.path === route && read(candidate.epoch) !== "" && !slots.get(candidate)?.inUse);
|
|
1173
|
+
const slot = channel && slots.get(channel);
|
|
1174
|
+
if (!channel || !slot) {
|
|
1175
|
+
throw jobError3.noRequestSlot;
|
|
1176
|
+
}
|
|
1177
|
+
slot.inUse = true;
|
|
1178
|
+
try {
|
|
1179
|
+
slot.sequence = (slot.sequence + 1) % sequenceLimit;
|
|
1180
|
+
if (slot.sequence === 0) {
|
|
1181
|
+
values.set(channel.epoch, "");
|
|
1182
|
+
throw jobError3.network;
|
|
1183
|
+
}
|
|
1184
|
+
const epoch = read(channel.epoch);
|
|
1185
|
+
const identity = `${epoch}:${slot.sequence}`;
|
|
1186
|
+
const request = `${identity}|${body}`;
|
|
1187
|
+
if (Buffer.byteLength(request, "utf8") > plcBufferBytes3) {
|
|
1188
|
+
throw jobError3.tooLarge;
|
|
1189
|
+
}
|
|
1190
|
+
values.set(channel.request, request);
|
|
1191
|
+
values.set(channel.requestId, identity);
|
|
1192
|
+
return await awaitReply(channel, epoch, identity, deadline);
|
|
1193
|
+
} finally {
|
|
1194
|
+
values.set(channel.requestId, "");
|
|
1195
|
+
values.set(channel.request, "");
|
|
1196
|
+
slot.inUse = false;
|
|
1197
|
+
}
|
|
1198
|
+
};
|
|
1199
|
+
return {
|
|
1200
|
+
transport: { call },
|
|
1201
|
+
scalar: (path) => values.has(path) ? {
|
|
1202
|
+
read: () => read(path),
|
|
1203
|
+
write: (value) => {
|
|
1204
|
+
values.set(path, String(value));
|
|
1205
|
+
}
|
|
1206
|
+
} : undefined
|
|
1207
|
+
};
|
|
1208
|
+
};
|
|
1209
|
+
|
|
1095
1210
|
// packages/compiler/src/runtime/runtime.ts
|
|
1096
1211
|
var runtimeEndpoint = { host: "127.0.0.1", port: 4840 };
|
|
1097
1212
|
var failureName = (reason) => {
|
|
@@ -1120,17 +1235,10 @@ var startRuntime = async (options) => {
|
|
|
1120
1235
|
if (!compiler || !tasks) {
|
|
1121
1236
|
throw new Error("The Bun runtime runs a TypeScript program.");
|
|
1122
1237
|
}
|
|
1123
|
-
if (config.server?.transport === "opcua") {
|
|
1124
|
-
throw new Error("The Bun runtime calls services over HTTP; this program calls them through the OPC UA gateway.");
|
|
1125
|
-
}
|
|
1126
1238
|
const st = resolve(root, emittedStDirectory);
|
|
1127
|
-
const manifest = debugManifest.parse(JSON.parse(await
|
|
1239
|
+
const manifest = debugManifest.parse(JSON.parse(await readFile3(join2(st, debugManifestFile), "utf8")));
|
|
1128
1240
|
const globals = plcGlobals(await readDeclarations(st));
|
|
1129
|
-
const baseUrlEnv = config.server?.baseUrlEnv;
|
|
1130
|
-
const baseUrl = baseUrlEnv && env[baseUrlEnv]?.replace(/\/+$/, "") || null;
|
|
1131
1241
|
const http = {
|
|
1132
|
-
baseUrl,
|
|
1133
|
-
routes: launch.services,
|
|
1134
1242
|
tokens: new Map((config.codesys?.http?.credentials ?? []).flatMap(({ originEnv, bearerTokenEnv }) => {
|
|
1135
1243
|
const origin = URL.parse(env[originEnv] ?? "")?.origin;
|
|
1136
1244
|
const token = env[bearerTokenEnv];
|
|
@@ -1138,15 +1246,16 @@ var startRuntime = async (options) => {
|
|
|
1138
1246
|
})),
|
|
1139
1247
|
timeoutMs: config.codesys?.http?.timeoutMs ?? 2000
|
|
1140
1248
|
};
|
|
1249
|
+
const baseUrlEnv = config.server?.baseUrlEnv;
|
|
1250
|
+
const baseUrl = baseUrlEnv && env[baseUrlEnv]?.replace(/\/+$/, "") || null;
|
|
1251
|
+
const slots = config.server?.transport === "opcua" ? opcuaSlots(await readServiceChannels(st), http.timeoutMs) : null;
|
|
1252
|
+
const transport = slots?.transport ?? (baseUrl === null ? null : httpTransport(http, baseUrl));
|
|
1141
1253
|
const current = { task: tasks[0]?.name ?? "", scan: 0, scanning: false };
|
|
1142
1254
|
const diagnostics = { count: 0, lastError: 0 };
|
|
1143
1255
|
const records = createTelemetry({
|
|
1144
1256
|
build: manifest.build.id,
|
|
1145
1257
|
capacity: config.telemetry?.capacity ?? 64,
|
|
1146
|
-
post:
|
|
1147
|
-
method: "POST",
|
|
1148
|
-
body
|
|
1149
|
-
}),
|
|
1258
|
+
post: transport ? (body) => transport.call("/tsplc/telemetry", body) : null,
|
|
1150
1259
|
clock: now
|
|
1151
1260
|
});
|
|
1152
1261
|
const stopTelemetry = onTelemetry((record) => records.record(current.task, {
|
|
@@ -1191,7 +1300,7 @@ var startRuntime = async (options) => {
|
|
|
1191
1300
|
"@tsplc/compiler/retain": { retain },
|
|
1192
1301
|
"@tsplc/compiler/telemetry": { telemetry, onTelemetry },
|
|
1193
1302
|
"@tsplc/compiler/math": { min, max, clamp },
|
|
1194
|
-
"@tsplc/compiler/async": createAsync(http, diagnostics),
|
|
1303
|
+
"@tsplc/compiler/async": createAsync({ http, services: { routes: launch.services, transport } }, diagnostics),
|
|
1195
1304
|
"@tsplc/compiler/config": {
|
|
1196
1305
|
env: (name) => {
|
|
1197
1306
|
const value = env[name];
|
|
@@ -1215,7 +1324,7 @@ var startRuntime = async (options) => {
|
|
|
1215
1324
|
storage.keepStoredValues();
|
|
1216
1325
|
const scalars = new Map;
|
|
1217
1326
|
const bound = (global) => scalarPaths(global).every(({ path }) => {
|
|
1218
|
-
const scalar = storage.scalar(path);
|
|
1327
|
+
const scalar = storage.scalar(path) ?? slots?.scalar(path);
|
|
1219
1328
|
if (scalar) {
|
|
1220
1329
|
scalars.set(path, scalar);
|
|
1221
1330
|
}
|
|
@@ -1223,7 +1332,7 @@ var startRuntime = async (options) => {
|
|
|
1223
1332
|
});
|
|
1224
1333
|
const published = globals.published.filter(bound);
|
|
1225
1334
|
const unbound = globals.published.filter((global) => !published.includes(global));
|
|
1226
|
-
const retention = createRetention(globals.retained, storage.scalar,
|
|
1335
|
+
const retention = createRetention(globals.retained, storage.scalar, join2(root, runtimeDirectory, runtimeFiles.retain));
|
|
1227
1336
|
const restored = await retention.restore();
|
|
1228
1337
|
if ("restored" in restored) {
|
|
1229
1338
|
log(`Restored ${restored.restored} retained values.`);
|
|
@@ -1242,7 +1351,7 @@ var startRuntime = async (options) => {
|
|
|
1242
1351
|
}
|
|
1243
1352
|
return scalar;
|
|
1244
1353
|
},
|
|
1245
|
-
certificateDirectory:
|
|
1354
|
+
certificateDirectory: join2(root, runtimeDirectory, runtimeFiles.certificates)
|
|
1246
1355
|
}).catch((cause) => {
|
|
1247
1356
|
throw cause instanceof Error && "code" in cause && cause.code === "EADDRINUSE" ? new Error(`Port ${options.port} is taken: stop the other PLC first, such as the CODESYS runtime \`tsplc run\` starts.`) : cause;
|
|
1248
1357
|
});
|
|
@@ -1319,7 +1428,7 @@ var [launchFile, port] = process.argv.slice(2);
|
|
|
1319
1428
|
if (!launchFile) {
|
|
1320
1429
|
throw new Error("Usage: main.ts <launch.json> [port]");
|
|
1321
1430
|
}
|
|
1322
|
-
var stateFile =
|
|
1431
|
+
var stateFile = join3(dirname2(launchFile), runtimeFiles.state);
|
|
1323
1432
|
var log = (line) => console.log(`[tsplc runtime] ${line}`);
|
|
1324
1433
|
try {
|
|
1325
1434
|
const runtime = await startRuntime({
|
|
@@ -1353,5 +1462,5 @@ try {
|
|
|
1353
1462
|
process.exit(1);
|
|
1354
1463
|
}
|
|
1355
1464
|
|
|
1356
|
-
//# debugId=
|
|
1465
|
+
//# debugId=1A73F16FFD98272F64756E2164756E21
|
|
1357
1466
|
//# sourceMappingURL=bun-runtime.js.map
|