@mica-js/sdk 0.3.3 → 0.4.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/README.md +162 -24
- package/dist/build/cli.js +90 -26
- package/dist/build/config.d.ts +11 -0
- package/dist/build/config.js +15 -1
- package/dist/build/content.d.ts +2 -0
- package/dist/build/content.js +2 -1
- package/dist/build/prepare.d.ts +2 -0
- package/dist/build/prepare.js +2 -0
- package/dist/build/project.d.ts +6 -0
- package/dist/build/project.js +25 -8
- package/dist/build/unified-build.d.ts +38 -0
- package/dist/build/unified-build.js +65 -0
- package/dist/build/unified-defaults.d.ts +21 -0
- package/dist/build/unified-defaults.js +267 -0
- package/dist/build/unified-manifest.d.ts +34 -0
- package/dist/build/unified-manifest.js +113 -0
- package/dist/build/unified-package.d.ts +64 -0
- package/dist/build/unified-package.js +354 -0
- package/dist/build/unified-paths.d.ts +30 -0
- package/dist/build/unified-paths.js +94 -0
- package/dist/build/unified-prepare.d.ts +58 -0
- package/dist/build/unified-prepare.js +209 -0
- package/dist/build/unified-types.d.ts +20 -0
- package/dist/build/unified-types.js +253 -0
- package/dist/device.d.ts +13 -1
- package/dist/fused/agreement.d.ts +15 -0
- package/dist/fused/agreement.js +104 -0
- package/dist/fused/diagnostics.d.ts +51 -0
- package/dist/fused/diagnostics.js +97 -0
- package/dist/fused/firmware.d.ts +27 -0
- package/dist/fused/firmware.js +105 -0
- package/dist/fused/index.d.ts +29 -0
- package/dist/fused/index.js +11 -0
- package/dist/fused/model.d.ts +101 -0
- package/dist/fused/model.js +47 -0
- package/dist/fused/models.d.ts +113 -0
- package/dist/fused/models.js +445 -0
- package/dist/fused/node.d.ts +5 -0
- package/dist/fused/node.js +139 -0
- package/dist/fused/package.d.ts +40 -0
- package/dist/fused/package.js +270 -0
- package/dist/fused/prepare.d.ts +50 -0
- package/dist/fused/prepare.js +214 -0
- package/dist/fused/program-memory.d.ts +2 -0
- package/dist/fused/program-memory.js +174 -0
- package/dist/fused/program.d.ts +47 -0
- package/dist/fused/program.js +99 -0
- package/dist/fused/properties.d.ts +48 -0
- package/dist/fused/properties.js +465 -0
- package/dist/fused/runtime.d.ts +60 -0
- package/dist/fused/runtime.js +568 -0
- package/dist/fused/values.d.ts +27 -0
- package/dist/fused/values.js +98 -0
- package/package.json +20 -3
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { join, resolve } from 'node:path';
|
|
2
|
+
import { assertSafeOutput, collectDevicePackage, createRelease, writeDevicePackage, } from './unified-package.js';
|
|
3
|
+
import { prepareDevice } from './unified-prepare.js';
|
|
4
|
+
/**
|
|
5
|
+
* Prepares the type environment, builds every package file and writes the release.
|
|
6
|
+
*
|
|
7
|
+
* Preparation runs first, so a manifest or DDL error prevents the build instead of shipping an
|
|
8
|
+
* artifact that contradicts the sources. Nothing authored by the device is executed: modules are
|
|
9
|
+
* bundled, not run.
|
|
10
|
+
*/
|
|
11
|
+
export async function buildDevice(options) {
|
|
12
|
+
const sourceDir = options.sourceDir ?? 'src';
|
|
13
|
+
const root = resolve(options.root);
|
|
14
|
+
const guarded = [
|
|
15
|
+
root,
|
|
16
|
+
resolve(root, sourceDir),
|
|
17
|
+
resolve(root, options.manifest),
|
|
18
|
+
...(options.model === undefined ? [] : [resolve(root, options.model)]),
|
|
19
|
+
join(root, '.mica'),
|
|
20
|
+
];
|
|
21
|
+
// Validate before preparation writes anything, and again before the output reset.
|
|
22
|
+
const outDir = assertSafeOutput(options.outDir, guarded);
|
|
23
|
+
const prepared = await prepareDevice({
|
|
24
|
+
root,
|
|
25
|
+
sourceDir,
|
|
26
|
+
manifest: options.manifest,
|
|
27
|
+
...(options.model === undefined ? {} : { model: options.model }),
|
|
28
|
+
...(options.configFile === undefined ? {} : { configFile: options.configFile }),
|
|
29
|
+
});
|
|
30
|
+
const aliases = resolveAliases(options.root, options.alias ?? {});
|
|
31
|
+
const files = await collectDevicePackage({
|
|
32
|
+
root,
|
|
33
|
+
sourceDir,
|
|
34
|
+
device: prepared.device,
|
|
35
|
+
...(aliases === undefined ? {} : { aliases }),
|
|
36
|
+
...(options.minify === undefined ? {} : { minify: options.minify }),
|
|
37
|
+
});
|
|
38
|
+
const release = createRelease(prepared.device.manifest, files);
|
|
39
|
+
await writeDevicePackage({ outDir, files, release, protect: guarded });
|
|
40
|
+
return {
|
|
41
|
+
outDir,
|
|
42
|
+
files: files.map((file) => resolve(outDir, ...file.path.split('/'))),
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
/** Builds the unified device package that the project config declares. */
|
|
46
|
+
export async function buildDeviceProject(project, outDir) {
|
|
47
|
+
const device = project.config.device;
|
|
48
|
+
if (device === undefined)
|
|
49
|
+
throw new Error('The project does not configure a unified device build.');
|
|
50
|
+
return buildDevice({
|
|
51
|
+
root: project.root,
|
|
52
|
+
manifest: device.manifest,
|
|
53
|
+
outDir,
|
|
54
|
+
sourceDir: project.config.sourceDir ?? 'src',
|
|
55
|
+
...(device.model === undefined ? {} : { model: device.model }),
|
|
56
|
+
configFile: project.configFile,
|
|
57
|
+
...(project.config.alias === undefined ? {} : { alias: project.config.alias }),
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
function resolveAliases(root, alias) {
|
|
61
|
+
const entries = Object.entries(alias);
|
|
62
|
+
if (entries.length === 0)
|
|
63
|
+
return undefined;
|
|
64
|
+
return Object.fromEntries(entries.map(([key, path]) => [key, resolve(root, path)]));
|
|
65
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { ComputationIR } from '@mica-js/device-model';
|
|
2
|
+
/** Result of a pure, side-effect-free evaluation of a DDL default computation. */
|
|
3
|
+
export type ConstantEvaluation = {
|
|
4
|
+
readonly known: true;
|
|
5
|
+
readonly value: unknown;
|
|
6
|
+
} | {
|
|
7
|
+
readonly known: false;
|
|
8
|
+
readonly reason: string;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Evaluates a DDL computation that must be pure, such as a property default.
|
|
12
|
+
*
|
|
13
|
+
* Only operations whose result depends solely on literals and `const` declarations are evaluated:
|
|
14
|
+
* literals, unary and binary operators, pure numeric builtins, record/bytes construction, field and
|
|
15
|
+
* index access, and `length`. Anything else reports `known: false` with the reason, so a caller can
|
|
16
|
+
* reject the input instead of silently accepting an unverifiable value.
|
|
17
|
+
*
|
|
18
|
+
* `constants` maps DDL `const` declarations to their value computations; the evaluation is
|
|
19
|
+
* recursive and detects constant cycles.
|
|
20
|
+
*/
|
|
21
|
+
export declare function evaluateConstant(computation: ComputationIR, constants?: ReadonlyMap<string, ComputationIR>): ConstantEvaluation;
|
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
const RUNTIME_REASON = 'it depends on a value that is only known at run time';
|
|
2
|
+
/**
|
|
3
|
+
* Evaluates a DDL computation that must be pure, such as a property default.
|
|
4
|
+
*
|
|
5
|
+
* Only operations whose result depends solely on literals and `const` declarations are evaluated:
|
|
6
|
+
* literals, unary and binary operators, pure numeric builtins, record/bytes construction, field and
|
|
7
|
+
* index access, and `length`. Anything else reports `known: false` with the reason, so a caller can
|
|
8
|
+
* reject the input instead of silently accepting an unverifiable value.
|
|
9
|
+
*
|
|
10
|
+
* `constants` maps DDL `const` declarations to their value computations; the evaluation is
|
|
11
|
+
* recursive and detects constant cycles.
|
|
12
|
+
*/
|
|
13
|
+
export function evaluateConstant(computation, constants = new Map()) {
|
|
14
|
+
try {
|
|
15
|
+
const value = evaluate(computation, constants, new Set());
|
|
16
|
+
if (value === UNVERIFIED)
|
|
17
|
+
return { known: false, reason: RUNTIME_REASON };
|
|
18
|
+
return { known: true, value };
|
|
19
|
+
}
|
|
20
|
+
catch (error) {
|
|
21
|
+
if (error instanceof Unverifiable)
|
|
22
|
+
return { known: false, reason: error.reason };
|
|
23
|
+
throw error;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
class Unverifiable extends Error {
|
|
27
|
+
reason;
|
|
28
|
+
constructor(reason) {
|
|
29
|
+
super(reason);
|
|
30
|
+
this.reason = reason;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
const UNVERIFIED = Symbol('unverified');
|
|
34
|
+
function fail(reason) {
|
|
35
|
+
throw new Unverifiable(reason);
|
|
36
|
+
}
|
|
37
|
+
function evaluate(computation, constants, visiting) {
|
|
38
|
+
const results = new Map();
|
|
39
|
+
let returned = UNVERIFIED;
|
|
40
|
+
const operations = computation.body.operations;
|
|
41
|
+
for (const operation of operations) {
|
|
42
|
+
const kind = operation['kind'];
|
|
43
|
+
if (kind === 'returnValue') {
|
|
44
|
+
const value = operation['value'];
|
|
45
|
+
returned = results.has(value) ? results.get(value) : UNVERIFIED;
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
if (kind === 'local' || kind === 'localWrite' || kind === 'write')
|
|
49
|
+
continue;
|
|
50
|
+
const result = operation['result'];
|
|
51
|
+
if (typeof result !== 'number')
|
|
52
|
+
continue;
|
|
53
|
+
if (kind === 'localRead') {
|
|
54
|
+
results.set(result, UNVERIFIED);
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
results.set(result, apply(operation, results, constants, visiting));
|
|
58
|
+
}
|
|
59
|
+
return returned;
|
|
60
|
+
}
|
|
61
|
+
function apply(operation, results, constants, visiting) {
|
|
62
|
+
const read = (id) => {
|
|
63
|
+
if (typeof id !== 'number')
|
|
64
|
+
return UNVERIFIED;
|
|
65
|
+
return results.has(id) ? results.get(id) : UNVERIFIED;
|
|
66
|
+
};
|
|
67
|
+
switch (operation['kind']) {
|
|
68
|
+
case 'constant':
|
|
69
|
+
return constantValue(operation['constant']);
|
|
70
|
+
case 'read': {
|
|
71
|
+
if (operation['resource'] !== 'const')
|
|
72
|
+
return UNVERIFIED;
|
|
73
|
+
const name = operation['name'];
|
|
74
|
+
if (typeof name !== 'string')
|
|
75
|
+
return UNVERIFIED;
|
|
76
|
+
const declaration = constants.get(name);
|
|
77
|
+
if (declaration === undefined)
|
|
78
|
+
return fail(`constant "${name}" is not declared in the model`);
|
|
79
|
+
if (visiting.has(name))
|
|
80
|
+
return fail(`constant "${name}" is defined in terms of itself`);
|
|
81
|
+
visiting.add(name);
|
|
82
|
+
try {
|
|
83
|
+
return evaluate(declaration, constants, visiting);
|
|
84
|
+
}
|
|
85
|
+
finally {
|
|
86
|
+
visiting.delete(name);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
case 'unary': {
|
|
90
|
+
const value = read(operation['value']);
|
|
91
|
+
if (value === UNVERIFIED)
|
|
92
|
+
return UNVERIFIED;
|
|
93
|
+
switch (operation['operator']) {
|
|
94
|
+
case 'positive':
|
|
95
|
+
return typeof value === 'number' ? value : UNVERIFIED;
|
|
96
|
+
case 'negate':
|
|
97
|
+
return typeof value === 'number' ? -value : UNVERIFIED;
|
|
98
|
+
case 'not':
|
|
99
|
+
return typeof value === 'boolean' ? !value : UNVERIFIED;
|
|
100
|
+
case 'bitNot':
|
|
101
|
+
return typeof value === 'number' ? ~value : UNVERIFIED;
|
|
102
|
+
default:
|
|
103
|
+
return UNVERIFIED;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
case 'binary':
|
|
107
|
+
return binaryValue(operation, read(operation['left']), read(operation['right']));
|
|
108
|
+
case 'numeric':
|
|
109
|
+
return numericValue(operation['builtin'], operation['args'].map(read));
|
|
110
|
+
case 'record': {
|
|
111
|
+
const fields = operation['fields'];
|
|
112
|
+
const record = {};
|
|
113
|
+
for (const field of fields) {
|
|
114
|
+
const value = read(field.value);
|
|
115
|
+
if (value === UNVERIFIED)
|
|
116
|
+
return UNVERIFIED;
|
|
117
|
+
record[field.name] = value;
|
|
118
|
+
}
|
|
119
|
+
return record;
|
|
120
|
+
}
|
|
121
|
+
case 'field': {
|
|
122
|
+
const value = read(operation['value']);
|
|
123
|
+
const field = operation['field'];
|
|
124
|
+
if (value === UNVERIFIED || typeof value !== 'object' || value === null)
|
|
125
|
+
return UNVERIFIED;
|
|
126
|
+
return value[String(field)];
|
|
127
|
+
}
|
|
128
|
+
case 'index': {
|
|
129
|
+
const value = read(operation['value']);
|
|
130
|
+
const index = read(operation['index']);
|
|
131
|
+
if (value === UNVERIFIED || typeof index !== 'number')
|
|
132
|
+
return UNVERIFIED;
|
|
133
|
+
if (typeof value === 'string')
|
|
134
|
+
return value[index];
|
|
135
|
+
if (Array.isArray(value))
|
|
136
|
+
return value[index];
|
|
137
|
+
return UNVERIFIED;
|
|
138
|
+
}
|
|
139
|
+
case 'bytes': {
|
|
140
|
+
const values = [];
|
|
141
|
+
for (const element of operation['elements']) {
|
|
142
|
+
const value = read(element);
|
|
143
|
+
if (typeof value !== 'number')
|
|
144
|
+
return UNVERIFIED;
|
|
145
|
+
values.push(value);
|
|
146
|
+
}
|
|
147
|
+
return values;
|
|
148
|
+
}
|
|
149
|
+
case 'length': {
|
|
150
|
+
const value = read(operation['value']);
|
|
151
|
+
if (typeof value === 'string' || Array.isArray(value))
|
|
152
|
+
return value.length;
|
|
153
|
+
return UNVERIFIED;
|
|
154
|
+
}
|
|
155
|
+
case 'optional':
|
|
156
|
+
case 'nonNull':
|
|
157
|
+
return read(operation['value']);
|
|
158
|
+
case 'isNone':
|
|
159
|
+
return read(operation['value']) === null;
|
|
160
|
+
default:
|
|
161
|
+
return UNVERIFIED;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
function constantValue(constant) {
|
|
165
|
+
if (constant === undefined)
|
|
166
|
+
return UNVERIFIED;
|
|
167
|
+
switch (constant['kind']) {
|
|
168
|
+
case 'number':
|
|
169
|
+
case 'bool':
|
|
170
|
+
case 'string':
|
|
171
|
+
return constant['value'];
|
|
172
|
+
case 'none':
|
|
173
|
+
return null;
|
|
174
|
+
case 'enum':
|
|
175
|
+
return constant['member'];
|
|
176
|
+
case 'record': {
|
|
177
|
+
const fields = constant['fields'];
|
|
178
|
+
return Object.fromEntries(fields.map((field) => [field.name, field.value]));
|
|
179
|
+
}
|
|
180
|
+
case 'bytes':
|
|
181
|
+
return Array.from(constant['value']);
|
|
182
|
+
default:
|
|
183
|
+
return UNVERIFIED;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
function binaryValue(operation, left, right) {
|
|
187
|
+
if (left === UNVERIFIED || right === UNVERIFIED)
|
|
188
|
+
return UNVERIFIED;
|
|
189
|
+
const numeric = typeof left === 'number' && typeof right === 'number';
|
|
190
|
+
switch (operation['operator']) {
|
|
191
|
+
case 'add':
|
|
192
|
+
if (numeric)
|
|
193
|
+
return left + right;
|
|
194
|
+
return typeof left === 'string' && typeof right === 'string' ? left + right : UNVERIFIED;
|
|
195
|
+
case 'subtract':
|
|
196
|
+
return numeric ? left - right : UNVERIFIED;
|
|
197
|
+
case 'multiply':
|
|
198
|
+
return numeric ? left * right : UNVERIFIED;
|
|
199
|
+
case 'divide':
|
|
200
|
+
return numeric ? left / right : UNVERIFIED;
|
|
201
|
+
case 'remainder':
|
|
202
|
+
return numeric ? left % right : UNVERIFIED;
|
|
203
|
+
case 'power':
|
|
204
|
+
return numeric ? left ** right : UNVERIFIED;
|
|
205
|
+
case 'lessThan':
|
|
206
|
+
return numeric ? left < right : UNVERIFIED;
|
|
207
|
+
case 'lessThanOrEqual':
|
|
208
|
+
return numeric ? left <= right : UNVERIFIED;
|
|
209
|
+
case 'greaterThan':
|
|
210
|
+
return numeric ? left > right : UNVERIFIED;
|
|
211
|
+
case 'greaterThanOrEqual':
|
|
212
|
+
return numeric ? left >= right : UNVERIFIED;
|
|
213
|
+
case 'equal':
|
|
214
|
+
return left === right;
|
|
215
|
+
case 'notEqual':
|
|
216
|
+
return left !== right;
|
|
217
|
+
case 'concat':
|
|
218
|
+
if (typeof left === 'string' && typeof right === 'string')
|
|
219
|
+
return left + right;
|
|
220
|
+
if (Array.isArray(left) && Array.isArray(right))
|
|
221
|
+
return [...left, ...right];
|
|
222
|
+
return UNVERIFIED;
|
|
223
|
+
case 'bitAnd':
|
|
224
|
+
return numeric ? left & right : UNVERIFIED;
|
|
225
|
+
case 'bitOr':
|
|
226
|
+
return numeric ? left | right : UNVERIFIED;
|
|
227
|
+
case 'bitXor':
|
|
228
|
+
return numeric ? left ^ right : UNVERIFIED;
|
|
229
|
+
default:
|
|
230
|
+
return UNVERIFIED;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
function numericValue(builtin, args) {
|
|
234
|
+
const numbers = args;
|
|
235
|
+
if (numbers.some((value) => typeof value !== 'number'))
|
|
236
|
+
return UNVERIFIED;
|
|
237
|
+
switch (builtin) {
|
|
238
|
+
case 'isNaN':
|
|
239
|
+
return Number.isNaN(numbers[0]);
|
|
240
|
+
case 'isFinite':
|
|
241
|
+
return Number.isFinite(numbers[0]);
|
|
242
|
+
case 'isInteger':
|
|
243
|
+
return Number.isInteger(numbers[0]);
|
|
244
|
+
case 'isSafeInteger':
|
|
245
|
+
return Number.isSafeInteger(numbers[0]);
|
|
246
|
+
case 'abs':
|
|
247
|
+
return Math.abs(numbers[0]);
|
|
248
|
+
case 'floor':
|
|
249
|
+
return Math.floor(numbers[0]);
|
|
250
|
+
case 'ceil':
|
|
251
|
+
return Math.ceil(numbers[0]);
|
|
252
|
+
case 'trunc':
|
|
253
|
+
return Math.trunc(numbers[0]);
|
|
254
|
+
case 'round':
|
|
255
|
+
return Math.round(numbers[0]);
|
|
256
|
+
case 'sqrt':
|
|
257
|
+
return Math.sqrt(numbers[0]);
|
|
258
|
+
case 'min':
|
|
259
|
+
return Math.min(...numbers);
|
|
260
|
+
case 'max':
|
|
261
|
+
return Math.max(...numbers);
|
|
262
|
+
case 'pow':
|
|
263
|
+
return numbers[0] ** numbers[1];
|
|
264
|
+
default:
|
|
265
|
+
return UNVERIFIED;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { type DeviceIR } from '@mica-js/ddl';
|
|
2
|
+
import type { Geometry2D, UnifiedDeviceManifest } from '@mica-js/document/fused';
|
|
3
|
+
export interface LoadedDevicePackage {
|
|
4
|
+
/** Absolute path of the manifest file. */
|
|
5
|
+
readonly manifestFile: string;
|
|
6
|
+
/** Authored manifest value, emitted unchanged as the package manifest. */
|
|
7
|
+
readonly authored: unknown;
|
|
8
|
+
/** Validated manifest contract shared with hosts and the runtime. */
|
|
9
|
+
readonly manifest: UnifiedDeviceManifest;
|
|
10
|
+
/** Compiled model IR of the DDL source, when the project declares one. */
|
|
11
|
+
readonly model?: DeviceIR;
|
|
12
|
+
/** Absolute path of the DDL source. */
|
|
13
|
+
readonly modelFile?: string;
|
|
14
|
+
/** Resolved 2D geometry, which the build needs for the body image. */
|
|
15
|
+
readonly geometry?: Geometry2D;
|
|
16
|
+
/** Package path of the geometry JSON when the manifest references a file. */
|
|
17
|
+
readonly geometryFile?: string;
|
|
18
|
+
}
|
|
19
|
+
export interface LoadDeviceOptions {
|
|
20
|
+
readonly root: string;
|
|
21
|
+
readonly sourceDir: string;
|
|
22
|
+
/** Manifest path relative to the project root. */
|
|
23
|
+
readonly manifest: string;
|
|
24
|
+
/** DDL source path relative to the project root. */
|
|
25
|
+
readonly model?: string;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Reads, validates and compiles one unified device project.
|
|
29
|
+
*
|
|
30
|
+
* The manifest is parsed by the shared device contract, so a package the build accepts is the same
|
|
31
|
+
* package a host loads. The DDL model is compiled but never executed, and it is cross-checked
|
|
32
|
+
* against the manifest: pin sets, property types, nullability and literal defaults must agree.
|
|
33
|
+
*/
|
|
34
|
+
export declare function loadDevicePackage(options: LoadDeviceOptions): Promise<LoadedDevicePackage>;
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { readFile } from 'node:fs/promises';
|
|
2
|
+
import { resolve } from 'node:path';
|
|
3
|
+
import { pathToFileURL } from 'node:url';
|
|
4
|
+
import { compileDDL } from '@mica-js/ddl';
|
|
5
|
+
import { parseGeometry2D, parseUnifiedManifest } from '@mica-js/document/fused';
|
|
6
|
+
import { checkModelAgreement } from '../fused/agreement.js';
|
|
7
|
+
import { isContained, isFile, packagePath } from './unified-paths.js';
|
|
8
|
+
/**
|
|
9
|
+
* Reads, validates and compiles one unified device project.
|
|
10
|
+
*
|
|
11
|
+
* The manifest is parsed by the shared device contract, so a package the build accepts is the same
|
|
12
|
+
* package a host loads. The DDL model is compiled but never executed, and it is cross-checked
|
|
13
|
+
* against the manifest: pin sets, property types, nullability and literal defaults must agree.
|
|
14
|
+
*/
|
|
15
|
+
export async function loadDevicePackage(options) {
|
|
16
|
+
const root = resolve(options.root);
|
|
17
|
+
const sourceRoot = resolve(root, options.sourceDir);
|
|
18
|
+
const manifestFile = resolve(root, options.manifest);
|
|
19
|
+
const text = await readText(manifestFile, 'device manifest');
|
|
20
|
+
let manifest;
|
|
21
|
+
try {
|
|
22
|
+
manifest = parseUnifiedManifest(text);
|
|
23
|
+
}
|
|
24
|
+
catch (error) {
|
|
25
|
+
throw new Error(`${manifestFile}: ${messageOf(error)}`, { cause: error });
|
|
26
|
+
}
|
|
27
|
+
// The text already passed the strict decoder inside parseUnifiedManifest (duplicate keys are
|
|
28
|
+
// rejected there), so this second decode only captures the authored value for package emission.
|
|
29
|
+
const authored = JSON.parse(text);
|
|
30
|
+
let model;
|
|
31
|
+
let modelFile;
|
|
32
|
+
if (options.model !== undefined) {
|
|
33
|
+
modelFile = resolve(root, options.model);
|
|
34
|
+
if (!(await isFile(modelFile)))
|
|
35
|
+
throw new Error(`Cannot find the DDL model source ${modelFile}`);
|
|
36
|
+
const source = await readFile(modelFile, 'utf8');
|
|
37
|
+
try {
|
|
38
|
+
model = await compileDDL(source, { uri: pathToFileURL(modelFile).href });
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
throw new Error(`${modelFile}: ${messageOf(error)}`, { cause: error });
|
|
42
|
+
}
|
|
43
|
+
assertModelAgreement(manifest, model, manifestFile);
|
|
44
|
+
}
|
|
45
|
+
const geometry = await loadGeometry(manifest, sourceRoot);
|
|
46
|
+
return {
|
|
47
|
+
manifestFile,
|
|
48
|
+
authored,
|
|
49
|
+
manifest,
|
|
50
|
+
...(model === undefined ? {} : { model }),
|
|
51
|
+
...(modelFile === undefined ? {} : { modelFile }),
|
|
52
|
+
...(geometry.geometry === undefined ? {} : { geometry: geometry.geometry }),
|
|
53
|
+
...(geometry.file === undefined ? {} : { geometryFile: geometry.file }),
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
async function loadGeometry(manifest, sourceRoot) {
|
|
57
|
+
const view2d = manifest.view2d;
|
|
58
|
+
if (view2d === undefined)
|
|
59
|
+
return {};
|
|
60
|
+
const pins = Object.keys(manifest.pins ?? {});
|
|
61
|
+
if (typeof view2d.geometry !== 'string')
|
|
62
|
+
return { geometry: parseGeometry(view2d.geometry, pins, 'view2d.geometry') };
|
|
63
|
+
const path = packagePath(view2d.geometry, 'view2d.geometry');
|
|
64
|
+
const source = resolve(sourceRoot, ...path.split('/'));
|
|
65
|
+
if (!isContained(sourceRoot, source))
|
|
66
|
+
throw new Error(`${path}: the geometry source escapes ${sourceRoot}`);
|
|
67
|
+
const text = await readText(source, `geometry '${path}'`);
|
|
68
|
+
return { geometry: parseGeometry(text, pins, path), file: path };
|
|
69
|
+
}
|
|
70
|
+
function parseGeometry(value, pins, label) {
|
|
71
|
+
try {
|
|
72
|
+
return parseGeometry2D(value, pins);
|
|
73
|
+
}
|
|
74
|
+
catch (error) {
|
|
75
|
+
throw new Error(`${label}: ${messageOf(error)}`, { cause: error });
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Fails the build when the compiled model and the manifest disagree.
|
|
80
|
+
*
|
|
81
|
+
* The rules are the runtime's pure checker (RFC 0020 / RFC 0021), so a package the build accepts is
|
|
82
|
+
* the same package a runtime accepts. Every problem is reported with the manifest path and the
|
|
83
|
+
* manifest field it belongs to.
|
|
84
|
+
*/
|
|
85
|
+
function assertModelAgreement(manifest, model, manifestFile) {
|
|
86
|
+
const problems = checkModelAgreement(manifest, model);
|
|
87
|
+
if (problems.length === 0)
|
|
88
|
+
return;
|
|
89
|
+
const details = problems.map((entry) => {
|
|
90
|
+
const field = manifestField(entry.pointer);
|
|
91
|
+
return field === '' ? `- ${entry.message}` : `- ${field}: ${entry.message}`;
|
|
92
|
+
});
|
|
93
|
+
throw new Error(`${manifestFile}: the manifest and the compiled model disagree:\n${details.join('\n')}`);
|
|
94
|
+
}
|
|
95
|
+
/** `/properties/debounce/default` → `properties.debounce.default`. */
|
|
96
|
+
function manifestField(pointer) {
|
|
97
|
+
return pointer
|
|
98
|
+
.split('/')
|
|
99
|
+
.filter((segment) => segment.length > 0)
|
|
100
|
+
.map((segment) => segment.replaceAll('~1', '/').replaceAll('~0', '~'))
|
|
101
|
+
.join('.');
|
|
102
|
+
}
|
|
103
|
+
async function readText(file, label) {
|
|
104
|
+
try {
|
|
105
|
+
return (await readFile(file, 'utf8')).replace(/^\uFEFF/, '');
|
|
106
|
+
}
|
|
107
|
+
catch (error) {
|
|
108
|
+
throw new Error(`Cannot read the ${label} ${file}: ${messageOf(error)}`, { cause: error });
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
function messageOf(error) {
|
|
112
|
+
return error instanceof Error ? error.message : String(error);
|
|
113
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { type AliasOptions } from 'vite';
|
|
2
|
+
import type { UnifiedDeviceManifest } from '@mica-js/document/fused';
|
|
3
|
+
import type { LoadedDevicePackage } from './unified-manifest.js';
|
|
4
|
+
/** One emitted package file; `path` is the package-root-relative POSIX path. */
|
|
5
|
+
export interface DevicePackageFile {
|
|
6
|
+
readonly path: string;
|
|
7
|
+
readonly data: Uint8Array;
|
|
8
|
+
}
|
|
9
|
+
export interface CollectDeviceOptions {
|
|
10
|
+
/** Project root; bundling runs with this directory as the Vite root. */
|
|
11
|
+
readonly root: string;
|
|
12
|
+
/** Source directory whose contents form the package root. */
|
|
13
|
+
readonly sourceDir: string;
|
|
14
|
+
readonly device: LoadedDevicePackage;
|
|
15
|
+
readonly aliases?: AliasOptions;
|
|
16
|
+
/** Minifies every emitted module; defaults to `true`. */
|
|
17
|
+
readonly minify?: boolean;
|
|
18
|
+
}
|
|
19
|
+
export interface ReleaseFile {
|
|
20
|
+
readonly size: number;
|
|
21
|
+
readonly integrity: string;
|
|
22
|
+
}
|
|
23
|
+
export interface ReleaseManifest {
|
|
24
|
+
readonly id: string;
|
|
25
|
+
readonly version: string;
|
|
26
|
+
readonly files: Readonly<Record<string, ReleaseFile>>;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Builds every package file of a unified device.
|
|
30
|
+
*
|
|
31
|
+
* Each declared JS entry becomes one self-contained ESM module; declared and referenced assets are
|
|
32
|
+
* copied at their package path; HTML is copied together with its relative resources, and a
|
|
33
|
+
* referenced script module is bundled like any other entry. The authored manifest is emitted as
|
|
34
|
+
* the package manifest, so the package never ships different content than the validated manifest.
|
|
35
|
+
*/
|
|
36
|
+
export declare function collectDevicePackage(options: CollectDeviceOptions): Promise<readonly DevicePackageFile[]>;
|
|
37
|
+
/** Builds the release file list with real byte sizes and SHA-256 digests. */
|
|
38
|
+
export declare function createRelease(manifest: UnifiedDeviceManifest, files: readonly DevicePackageFile[]): ReleaseManifest;
|
|
39
|
+
export declare function renderRelease(release: ReleaseManifest): string;
|
|
40
|
+
export interface WriteDeviceOptions {
|
|
41
|
+
/** Output directory that is reset before the package files are written. */
|
|
42
|
+
readonly outDir: string;
|
|
43
|
+
readonly files: readonly DevicePackageFile[];
|
|
44
|
+
readonly release: ReleaseManifest;
|
|
45
|
+
/** Absolute files and directories that the output reset must never touch. */
|
|
46
|
+
readonly protect?: readonly string[];
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Validates an output directory that is going to be deleted and recreated.
|
|
50
|
+
*
|
|
51
|
+
* The directory may not be a filesystem root, the home directory or one of its ancestors, and it
|
|
52
|
+
* may not contain any protected path such as the project root, the source directory or the
|
|
53
|
+
* manifest. Returns the resolved directory.
|
|
54
|
+
*
|
|
55
|
+
* @throws When resetting the directory could remove sources, tooling state or user data.
|
|
56
|
+
*/
|
|
57
|
+
export declare function assertSafeOutput(outDir: string, protect?: readonly string[]): string;
|
|
58
|
+
/**
|
|
59
|
+
* Replaces the output directory with the package files and writes `release.json` beside them.
|
|
60
|
+
*
|
|
61
|
+
* The release file itself is not part of `files`, so it never describes itself. The directory is
|
|
62
|
+
* validated by {@link assertSafeOutput} before anything is removed.
|
|
63
|
+
*/
|
|
64
|
+
export declare function writeDevicePackage(options: WriteDeviceOptions): Promise<void>;
|