@tsplc/build 0.2.17 → 0.2.19
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/cli.js +628 -352
- package/dist/cli.js.map +30 -28
- package/dist/index.js +548 -387
- package/dist/index.js.map +27 -25
- package/dist/libraries.js +68 -27
- package/dist/libraries.js.map +3 -3
- package/docker/Dockerfile +2 -0
- package/docker/Dockerfile.dockerignore +4 -1
- package/docker/Dockerfile.source +3 -0
- package/docker/config.ts +10 -5
- package/docker/runner.ts +140 -127
- package/package.json +54 -53
- package/python/build.py +183 -124
- package/python/codesys_online.py +44 -45
- package/python/hardware.py +9 -9
- package/python/test_build.py +3 -3
- package/python/test_codesys_online.py +18 -12
- package/python/test_installer_packages.py +1 -0
- package/python/worker.py +11 -7
- package/src/artifacts.ts +82 -60
- package/src/build.ts +71 -62
- package/src/cli.ts +109 -100
- package/src/controller-online.ts +400 -425
- package/src/controller-protocol.ts +140 -0
- package/src/controller-session.ts +466 -397
- package/src/device-tree.ts +19 -15
- package/src/diagnostics.ts +59 -49
- package/src/docker-build.ts +217 -156
- package/src/docker-context.ts +49 -27
- package/src/docker-run.ts +137 -111
- package/src/engineering-settings.ts +42 -0
- package/src/host/codesys-command.ts +35 -34
- package/src/host/codesys-host.ts +157 -129
- package/src/host/codesys-worker.ts +318 -301
- package/src/host/windows-path.ts +63 -59
- package/src/import-request.ts +24 -0
- package/src/index.ts +37 -31
- package/src/io.ts +152 -116
- package/src/libraries.ts +214 -146
- package/src/package-path.ts +3 -1
- package/src/run.ts +132 -108
- package/src/runtime-status.ts +25 -19
- package/src/sources.ts +264 -225
- package/src/st/declarations.ts +181 -193
- package/src/st/lexer.ts +102 -118
- package/src/st/parse.ts +193 -171
- package/src/st/reader.ts +47 -42
- package/src/__tests__/fixtures/xml/fb-simple.xml +0 -40
- package/src/__tests__/fixtures/xml/fb-state-machine.xml +0 -86
- package/src/__tests__/fixtures/xml/prg-cyclic.xml +0 -44
- package/src/config.ts +0 -12
package/src/libraries.ts
CHANGED
|
@@ -2,165 +2,233 @@ import { createHash, randomUUID } from "node:crypto";
|
|
|
2
2
|
import { mkdir, readFile, rename, writeFile } from "node:fs/promises";
|
|
3
3
|
import { basename, relative, resolve } from "node:path";
|
|
4
4
|
import type { LibraryDependency } from "@tsplc/config";
|
|
5
|
+
import { sha256Digest } from "@tsplc/config/build";
|
|
6
|
+
import { z } from "zod";
|
|
5
7
|
|
|
6
8
|
export const libraryRegistry = "https://store-archive.codesys.com/CODESYSLibs";
|
|
7
9
|
const libraryFile = /\.(?:compiled-library(?:-v\d+)?|library)$/i;
|
|
8
|
-
type Fetcher = (
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
const
|
|
10
|
+
type Fetcher = (
|
|
11
|
+
input: string | URL | Request,
|
|
12
|
+
init?: RequestInit,
|
|
13
|
+
) => Promise<Response>;
|
|
14
|
+
const installedLibrarySchema = z.object({
|
|
15
|
+
title: z.string(),
|
|
16
|
+
company: z.string().optional(),
|
|
17
|
+
version: z.string().optional(),
|
|
18
|
+
namespace: z.string().optional(),
|
|
19
|
+
path: z.string(),
|
|
20
|
+
sha256: sha256Digest,
|
|
21
|
+
url: z.string().optional(),
|
|
22
|
+
});
|
|
23
|
+
export type InstalledLibrary = z.infer<typeof installedLibrarySchema>;
|
|
24
|
+
const libraryLockSchema = z.object({
|
|
25
|
+
version: z.literal(1),
|
|
26
|
+
dependencies: z.record(z.string(), installedLibrarySchema),
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
/** The project's lockfile; a project without one has locked nothing yet. */
|
|
30
|
+
const readLock = async (
|
|
31
|
+
file: string,
|
|
32
|
+
): Promise<z.infer<typeof libraryLockSchema>> => {
|
|
33
|
+
const text = await readFile(file, "utf8").catch((cause: unknown) => {
|
|
34
|
+
if (
|
|
35
|
+
cause &&
|
|
36
|
+
typeof cause === "object" &&
|
|
37
|
+
"code" in cause &&
|
|
38
|
+
cause.code === "ENOENT"
|
|
39
|
+
) {
|
|
40
|
+
return undefined;
|
|
41
|
+
}
|
|
42
|
+
throw cause;
|
|
43
|
+
});
|
|
44
|
+
if (text === undefined) {
|
|
45
|
+
return { version: 1, dependencies: {} };
|
|
46
|
+
}
|
|
47
|
+
const lock = libraryLockSchema.safeParse(JSON.parse(text));
|
|
48
|
+
if (!lock.success) {
|
|
49
|
+
throw new Error("Invalid tsplc.lock.json.", { cause: lock.error });
|
|
50
|
+
}
|
|
51
|
+
return lock.data;
|
|
52
|
+
};
|
|
53
|
+
const hash = (data: Uint8Array) =>
|
|
54
|
+
createHash("sha256").update(data).digest("hex");
|
|
23
55
|
const component = (name: string) => {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
56
|
+
if (
|
|
57
|
+
!name.trim() ||
|
|
58
|
+
/[/\\\0\r\n]/.test(name) ||
|
|
59
|
+
name === "." ||
|
|
60
|
+
name === ".."
|
|
61
|
+
) {
|
|
62
|
+
throw new Error(`Invalid library registry component: ${name}.`);
|
|
63
|
+
}
|
|
64
|
+
return encodeURIComponent(name);
|
|
27
65
|
};
|
|
28
66
|
async function response(url: string, fetcher: Fetcher) {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
67
|
+
const result = await fetcher(url, { signal: AbortSignal.timeout(30_000) });
|
|
68
|
+
if (!result.ok) {
|
|
69
|
+
throw new Error(`Library registry ${result.status}: ${url}`);
|
|
70
|
+
}
|
|
71
|
+
return result;
|
|
32
72
|
}
|
|
33
73
|
export async function libraryIndex(
|
|
34
|
-
|
|
35
|
-
|
|
74
|
+
path: string[] = ["System"],
|
|
75
|
+
fetcher: Fetcher = fetch,
|
|
36
76
|
): Promise<string[]> {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
77
|
+
const url = `${libraryRegistry}/${path.map(component).join("/")}/index`;
|
|
78
|
+
const result = await response(url, fetcher);
|
|
79
|
+
const text = await result.text();
|
|
80
|
+
if (text.length > 4_000_000 || /<html/i.test(text)) {
|
|
81
|
+
throw new Error(`Invalid library registry index: ${url}`);
|
|
82
|
+
}
|
|
83
|
+
return text
|
|
84
|
+
.split(/\r?\n/)
|
|
85
|
+
.map((s) => s.trim())
|
|
86
|
+
.filter(Boolean);
|
|
46
87
|
}
|
|
47
88
|
export async function validateLibrary(
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
89
|
+
title: string,
|
|
90
|
+
version: string,
|
|
91
|
+
company = "System",
|
|
92
|
+
fetcher: Fetcher = fetch,
|
|
52
93
|
) {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
94
|
+
if (!(await libraryIndex([company], fetcher)).includes(title)) {
|
|
95
|
+
throw new Error(
|
|
96
|
+
`Unknown CODESYS library ${company}/${title}. Names are case sensitive.`,
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
const versions = await libraryIndex([company, title], fetcher);
|
|
100
|
+
if (!versions.includes(version)) {
|
|
101
|
+
throw new Error(
|
|
102
|
+
`Unknown version ${version} for ${title}. Available: ${versions.join(", ")}`,
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
const filenames = (
|
|
106
|
+
await libraryIndex([company, title, version], fetcher)
|
|
107
|
+
).filter((name) => libraryFile.test(name));
|
|
108
|
+
const [filename, ...others] = filenames;
|
|
109
|
+
if (filename === undefined || others.length) {
|
|
110
|
+
throw new Error(
|
|
111
|
+
`Expected one library payload for ${title} ${version}, received ${filenames.length}.`,
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
return {
|
|
115
|
+
filename,
|
|
116
|
+
url: `${libraryRegistry}/${[company, title, version, filename].map(component).join("/")}`,
|
|
117
|
+
};
|
|
69
118
|
}
|
|
70
119
|
export async function installLibraries(
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
120
|
+
dependencies: Record<string, LibraryDependency>,
|
|
121
|
+
root: string,
|
|
122
|
+
options: { frozen?: boolean; fetcher?: Fetcher } = {},
|
|
74
123
|
) {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
124
|
+
const file = resolve(root, "tsplc.lock.json");
|
|
125
|
+
const store = resolve(root, ".tsplc/libraries");
|
|
126
|
+
const previous = await readLock(file);
|
|
127
|
+
const installed: Record<string, InstalledLibrary> = {};
|
|
128
|
+
const fetcher = options.fetcher ?? fetch;
|
|
129
|
+
await mkdir(store, { recursive: true });
|
|
130
|
+
for (const [title, dependency] of Object.entries(dependencies)) {
|
|
131
|
+
component(title);
|
|
132
|
+
const settings =
|
|
133
|
+
typeof dependency === "string" ? { version: dependency } : dependency;
|
|
134
|
+
const locked = previous.dependencies[title];
|
|
135
|
+
const prior = locked
|
|
136
|
+
? { ...locked, path: resolve(root, locked.path) }
|
|
137
|
+
: undefined;
|
|
138
|
+
const same =
|
|
139
|
+
prior !== undefined &&
|
|
140
|
+
prior.namespace === settings.namespace &&
|
|
141
|
+
("path" in settings
|
|
142
|
+
? prior.url === undefined && prior.path === resolve(root, settings.path)
|
|
143
|
+
: prior.version === settings.version &&
|
|
144
|
+
prior.company === (settings.company ?? "System"));
|
|
145
|
+
if (same) {
|
|
146
|
+
try {
|
|
147
|
+
if (hash(await readFile(prior.path)) === prior.sha256) {
|
|
148
|
+
installed[title] = prior;
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
} catch {
|
|
152
|
+
/* Missing cache can be restored from the exact URL. */
|
|
153
|
+
}
|
|
154
|
+
if (options.frozen) {
|
|
155
|
+
throw new Error(`Locked library ${title} is missing or changed.`);
|
|
156
|
+
}
|
|
157
|
+
} else if (options.frozen) {
|
|
158
|
+
throw new Error(`Dependency ${title} differs from tsplc.lock.json.`);
|
|
159
|
+
}
|
|
160
|
+
if ("path" in settings) {
|
|
161
|
+
const path = resolve(root, settings.path);
|
|
162
|
+
if (!libraryFile.test(path)) {
|
|
163
|
+
throw new Error(
|
|
164
|
+
`${title}: expected a .library or .compiled-library file.`,
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
installed[title] = {
|
|
168
|
+
title,
|
|
169
|
+
path,
|
|
170
|
+
sha256: hash(await readFile(path)),
|
|
171
|
+
...(settings.namespace ? { namespace: settings.namespace } : {}),
|
|
172
|
+
};
|
|
173
|
+
} else {
|
|
174
|
+
const company = settings.company ?? "System";
|
|
175
|
+
const payload = await validateLibrary(
|
|
176
|
+
title,
|
|
177
|
+
settings.version,
|
|
178
|
+
company,
|
|
179
|
+
fetcher,
|
|
180
|
+
);
|
|
181
|
+
const result = await response(payload.url, fetcher);
|
|
182
|
+
const data = new Uint8Array(await result.arrayBuffer());
|
|
183
|
+
if (!data.length || data.length > 256 * 1024 * 1024) {
|
|
184
|
+
throw new Error(`${title}: invalid library payload size.`);
|
|
185
|
+
}
|
|
186
|
+
const sha256 = hash(data);
|
|
187
|
+
// An existing lock pins the payload, even if the registry later changes it.
|
|
188
|
+
if (same && prior.sha256 !== sha256) {
|
|
189
|
+
throw new Error(
|
|
190
|
+
`Registry payload changed for locked library ${title} ${settings.version}.`,
|
|
191
|
+
);
|
|
192
|
+
}
|
|
193
|
+
const path = resolve(store, `${sha256}-${basename(payload.filename)}`);
|
|
194
|
+
const temp = `${path}.${randomUUID()}.tmp`;
|
|
195
|
+
await writeFile(temp, data, { flag: "wx" });
|
|
196
|
+
await rename(temp, path);
|
|
197
|
+
installed[title] = {
|
|
198
|
+
title,
|
|
199
|
+
company,
|
|
200
|
+
version: settings.version,
|
|
201
|
+
path,
|
|
202
|
+
sha256,
|
|
203
|
+
url: payload.url,
|
|
204
|
+
...(settings.namespace ? { namespace: settings.namespace } : {}),
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
if (
|
|
209
|
+
options.frozen &&
|
|
210
|
+
Object.keys(previous.dependencies).some(
|
|
211
|
+
(name) => !Object.hasOwn(dependencies, name),
|
|
212
|
+
)
|
|
213
|
+
) {
|
|
214
|
+
throw new Error("Removed dependencies differ from tsplc.lock.json.");
|
|
215
|
+
}
|
|
216
|
+
if (!options.frozen) {
|
|
217
|
+
const temp = `${file}.${randomUUID()}.tmp`;
|
|
218
|
+
const portable = Object.fromEntries(
|
|
219
|
+
Object.entries(installed).map(([title, library]) => [
|
|
220
|
+
title,
|
|
221
|
+
{
|
|
222
|
+
...library,
|
|
223
|
+
path: relative(root, library.path).replaceAll("\\", "/"),
|
|
224
|
+
},
|
|
225
|
+
]),
|
|
226
|
+
);
|
|
227
|
+
await writeFile(
|
|
228
|
+
temp,
|
|
229
|
+
JSON.stringify({ version: 1, dependencies: portable }, null, 2) + "\n",
|
|
230
|
+
);
|
|
231
|
+
await rename(temp, file);
|
|
232
|
+
}
|
|
233
|
+
return Object.values(installed);
|
|
166
234
|
}
|
package/src/package-path.ts
CHANGED
|
@@ -2,4 +2,6 @@ import { resolve } from "node:path";
|
|
|
2
2
|
import { fileURLToPath } from "node:url";
|
|
3
3
|
|
|
4
4
|
// Both source modules and published bundles live one directory below the package root.
|
|
5
|
-
export const packageDirectory = resolve(
|
|
5
|
+
export const packageDirectory = resolve(
|
|
6
|
+
fileURLToPath(new URL("..", import.meta.url)),
|
|
7
|
+
);
|