@hewel/zgui-build 0.1.2 → 0.1.3
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 +41 -2
- package/package.json +1 -1
- package/sdk/Cargo.lock +4 -4
- package/sdk/bun.lock +41 -7
- package/sdk/package.json +4 -3
- package/src/build.ts +64 -633
- package/src/css.ts +558 -25
- package/src/dev.ts +4 -3
- package/src/host.ts +253 -75
- package/src/index.ts +8 -2
- package/src/manifest-policy.ts +648 -0
- package/src/package-app.ts +21 -23
- package/src/preview-check.ts +14 -5
- package/src/preview.ts +16 -16
- package/src/public-api.d.ts +11 -0
- package/src/sdk-packages.ts +73 -14
- package/src/typecheck.ts +6 -2
- package/src/validate.ts +8 -4
- package/src/watch.ts +50 -3
- package/src/native-process.ts +0 -178
package/src/build.ts
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
import type {
|
|
2
2
|
BuildApplicationOptions,
|
|
3
3
|
BuildInfo,
|
|
4
|
-
Capabilities,
|
|
5
|
-
FontDeclaration,
|
|
6
4
|
Manifest,
|
|
7
5
|
PublishedApplication,
|
|
8
|
-
|
|
6
|
+
ResolveNativeHostOptions,
|
|
7
|
+
ResolvedNativeHost,
|
|
9
8
|
WorkerDeclaration,
|
|
10
9
|
} from "./index.ts";
|
|
11
10
|
import { readFileSync, realpathSync, type Dirent } from "node:fs";
|
|
@@ -18,6 +17,7 @@ import { typecheckProject } from "./typecheck.ts";
|
|
|
18
17
|
import { canonicalJson, sha256, moduleIdentity } from "./identity.ts";
|
|
19
18
|
import { compileModuleBytecode, resolveNativeHost } from "./host.ts";
|
|
20
19
|
import { publishGeneration, resourcePath } from "./publication.ts";
|
|
20
|
+
import { readManifestPolicy } from "./manifest-policy.ts";
|
|
21
21
|
import { syncCssModuleTypes, validateStylesheet } from "./css.ts";
|
|
22
22
|
import { workspaceRoot } from "./sdk.ts";
|
|
23
23
|
import { ensureSdkPackagesBuilt } from "./sdk-packages.ts";
|
|
@@ -280,13 +280,22 @@ export async function buildApplication(
|
|
|
280
280
|
hostSearchFrom,
|
|
281
281
|
extraCapabilities,
|
|
282
282
|
capabilitiesFile,
|
|
283
|
+
cssModuleInclude,
|
|
283
284
|
}: BuildApplicationOptions = {},
|
|
285
|
+
{
|
|
286
|
+
signal,
|
|
287
|
+
nativeHost,
|
|
288
|
+
}: { signal?: AbortSignal | undefined; nativeHost?: ResolvedNativeHost | undefined } = {},
|
|
284
289
|
): Promise<PublishedApplication> {
|
|
290
|
+
signal?.throwIfAborted();
|
|
285
291
|
if (!/^[a-z][a-z0-9-]*$/.test(app)) throw new Error(`Invalid application name: ${app}`);
|
|
286
292
|
root = path.resolve(root);
|
|
287
293
|
appDirectory = path.resolve(appDirectory);
|
|
288
294
|
await ensureSdkPackagesBuilt(root);
|
|
289
|
-
await syncCssModuleTypes(
|
|
295
|
+
await syncCssModuleTypes({
|
|
296
|
+
appDirectory,
|
|
297
|
+
include: cssModuleInclude,
|
|
298
|
+
});
|
|
290
299
|
typecheckProject(path.join(appDirectory, "tsconfig.json"));
|
|
291
300
|
const result = await bundle(path.join(appDirectory, "src/main.tsx"), {
|
|
292
301
|
root,
|
|
@@ -301,593 +310,38 @@ export async function buildApplication(
|
|
|
301
310
|
hostRoot,
|
|
302
311
|
hostExecutable,
|
|
303
312
|
hostSearchFrom,
|
|
313
|
+
nativeHost,
|
|
314
|
+
signal,
|
|
304
315
|
extraCapabilities,
|
|
305
316
|
capabilitiesFile,
|
|
306
317
|
});
|
|
307
318
|
}
|
|
308
319
|
|
|
309
|
-
function
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
typeof font === "object" &&
|
|
313
|
-
"family" in font &&
|
|
314
|
-
typeof font.family === "string" &&
|
|
315
|
-
!!font.family.trim() &&
|
|
316
|
-
font.family === font.family.trim() &&
|
|
317
|
-
"path" in font &&
|
|
318
|
-
typeof font.path === "string" &&
|
|
319
|
-
Object.keys(font).every(key => key === "family" || key === "path")
|
|
320
|
-
);
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
function capabilityString(value: unknown): value is string {
|
|
324
|
-
if (typeof value !== "string") return false;
|
|
325
|
-
let nonspace = false;
|
|
326
|
-
for (const char of value) {
|
|
327
|
-
const code = char.charCodeAt(0);
|
|
328
|
-
if (
|
|
329
|
-
code < 32 ||
|
|
330
|
-
(code >= 127 && code <= 159) ||
|
|
331
|
-
(char.length === 1 && code >= 0xd800 && code <= 0xdfff)
|
|
332
|
-
)
|
|
333
|
-
return false;
|
|
334
|
-
nonspace ||= code !== 32;
|
|
335
|
-
}
|
|
336
|
-
return nonspace;
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
function capabilityPath(value: string): boolean {
|
|
340
|
-
if (value === "/") return true;
|
|
341
|
-
if (value.includes("\\")) return false;
|
|
342
|
-
const parts = value.split("/");
|
|
343
|
-
const root = parts.shift();
|
|
344
|
-
const template = root === "$XDG_RUNTIME_DIR" || root === "$HOME" || root === "$XDG_DATA_HOME";
|
|
345
|
-
return (
|
|
346
|
-
(root === "" || template) &&
|
|
347
|
-
parts.every(
|
|
348
|
-
part =>
|
|
349
|
-
part !== "" &&
|
|
350
|
-
part !== "." &&
|
|
351
|
-
part !== ".." &&
|
|
352
|
-
(!template ||
|
|
353
|
-
part === "$WAYLAND_DISPLAY" ||
|
|
354
|
-
!part.replaceAll("${WAYLAND_DISPLAY}", "").includes("$")),
|
|
355
|
-
)
|
|
356
|
-
);
|
|
357
|
-
}
|
|
358
|
-
|
|
359
|
-
function fileDirectoryPath(value: string): boolean {
|
|
360
|
-
if (value === "/") return true;
|
|
361
|
-
if (value.includes("\\")) return false;
|
|
362
|
-
const parts = value.split("/");
|
|
363
|
-
const root = parts.shift();
|
|
364
|
-
const template = root === "$HOME" || root === "$XDG_DATA_HOME" || root === "$XDG_DOCUMENTS_DIR";
|
|
365
|
-
return (
|
|
366
|
-
(root === "" || template) &&
|
|
367
|
-
parts.every(part => part !== "" && part !== "." && part !== ".." && !part.includes("$"))
|
|
368
|
-
);
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
function isPrivateIpv4(host: string): boolean {
|
|
372
|
-
const parts = host.split(".");
|
|
373
|
-
if (parts.length !== 4 || !parts.every(p => /^\d{1,3}$/.test(p))) return false;
|
|
374
|
-
const nums = parts.map(Number);
|
|
375
|
-
if (nums.some(n => n > 255)) return false;
|
|
376
|
-
const [a, b] = nums as [number, number, number, number];
|
|
377
|
-
return (
|
|
378
|
-
a === 0 ||
|
|
379
|
-
a === 10 ||
|
|
380
|
-
a === 127 ||
|
|
381
|
-
(a === 100 && b >= 64 && b <= 127) ||
|
|
382
|
-
(a === 169 && b === 254) ||
|
|
383
|
-
(a === 172 && b >= 16 && b <= 31) ||
|
|
384
|
-
(a === 192 && b === 168) ||
|
|
385
|
-
a >= 224
|
|
386
|
-
);
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
function isPrivateHost(hostname: string): boolean {
|
|
390
|
-
const bare =
|
|
391
|
-
hostname.startsWith("[") && hostname.endsWith("]")
|
|
392
|
-
? hostname.slice(1, -1).toLowerCase()
|
|
393
|
-
: hostname.toLowerCase();
|
|
394
|
-
if (bare === "localhost" || bare.endsWith(".localhost")) return true;
|
|
395
|
-
if (isPrivateIpv4(bare)) return true;
|
|
396
|
-
if (bare.includes(":")) {
|
|
397
|
-
// Reject loopback, unspecified, ULA (fc00::/7), link-local (fe80::/10), multicast (ff00::/8),
|
|
398
|
-
// and IPv4-mapped IPv6 addresses unless allowPrivateNetworks is explicitly opted in.
|
|
399
|
-
if (bare === "::1" || bare === "::") return true;
|
|
400
|
-
const first = bare.split(":")[0] ?? "";
|
|
401
|
-
const head = Number.parseInt(first || "0", 16);
|
|
402
|
-
if (!Number.isNaN(head)) {
|
|
403
|
-
if ((head & 0xfe00) === 0xfc00) return true;
|
|
404
|
-
if ((head & 0xffc0) === 0xfe80) return true;
|
|
405
|
-
if ((head & 0xff00) === 0xff00) return true;
|
|
406
|
-
}
|
|
407
|
-
if (bare.startsWith("::ffff:")) {
|
|
408
|
-
const mapped = bare.slice("::ffff:".length);
|
|
409
|
-
if (isPrivateIpv4(mapped)) return true;
|
|
410
|
-
// Also handle hex form e.g. ::ffff:7f00:1 produced by WHATWG URL parser
|
|
411
|
-
const hexParts = mapped.split(":");
|
|
412
|
-
if (hexParts.length === 2) {
|
|
413
|
-
const hi = Number.parseInt(hexParts[0]!, 16);
|
|
414
|
-
const lo = Number.parseInt(hexParts[1]!, 16);
|
|
415
|
-
if (!Number.isNaN(hi) && !Number.isNaN(lo)) {
|
|
416
|
-
const ipv4 = `${(hi >> 8) & 255}.${hi & 255}.${(lo >> 8) & 255}.${lo & 255}`;
|
|
417
|
-
if (isPrivateIpv4(ipv4)) return true;
|
|
418
|
-
}
|
|
419
|
-
}
|
|
420
|
-
}
|
|
421
|
-
}
|
|
422
|
-
return false;
|
|
423
|
-
}
|
|
424
|
-
|
|
425
|
-
function parseNetworkOrigin(
|
|
426
|
-
entry: unknown,
|
|
427
|
-
field: string,
|
|
428
|
-
): {
|
|
429
|
-
canonical: string;
|
|
430
|
-
insecure: boolean;
|
|
431
|
-
host: string;
|
|
432
|
-
} {
|
|
433
|
-
if (!capabilityString(entry) || entry !== entry.trim() || entry.includes("*")) {
|
|
434
|
-
throw new Error(`${field} contains invalid origin ${JSON.stringify(entry)}`);
|
|
435
|
-
}
|
|
436
|
-
let parsed: URL;
|
|
437
|
-
try {
|
|
438
|
-
parsed = new URL(entry);
|
|
439
|
-
} catch {
|
|
440
|
-
throw new Error(`${field} contains invalid origin ${JSON.stringify(entry)}`);
|
|
441
|
-
}
|
|
442
|
-
const scheme = parsed.protocol.slice(0, -1).toLowerCase();
|
|
443
|
-
if (scheme !== "https" && scheme !== "http" && scheme !== "wss" && scheme !== "ws") {
|
|
444
|
-
throw new Error(`${field} origin scheme must be https, http, wss, or ws: ${entry}`);
|
|
445
|
-
}
|
|
446
|
-
if (parsed.username || parsed.password) {
|
|
447
|
-
throw new Error(`${field} origin must not include userinfo: ${entry}`);
|
|
448
|
-
}
|
|
449
|
-
if (parsed.search || parsed.hash) {
|
|
450
|
-
throw new Error(`${field} origin must not include query or fragment: ${entry}`);
|
|
451
|
-
}
|
|
452
|
-
const afterScheme = entry.slice(entry.indexOf("://") + 3);
|
|
453
|
-
if (afterScheme.includes("/") || afterScheme.includes("?") || afterScheme.includes("#")) {
|
|
454
|
-
throw new Error(`${field} origin must not include a path, query, or fragment: ${entry}`);
|
|
455
|
-
}
|
|
456
|
-
const host = parsed.hostname.toLowerCase();
|
|
457
|
-
if (!host) {
|
|
458
|
-
throw new Error(`${field} origin must include a host: ${entry}`);
|
|
459
|
-
}
|
|
460
|
-
const defaultPort = scheme === "https" || scheme === "wss" ? 443 : 80;
|
|
461
|
-
const port = parsed.port ? Number(parsed.port) : defaultPort;
|
|
462
|
-
if (!Number.isInteger(port) || port < 1 || port > 65535) {
|
|
463
|
-
throw new Error(`${field} origin has invalid port: ${entry}`);
|
|
464
|
-
}
|
|
465
|
-
const canonical = port === defaultPort ? `${scheme}://${host}` : `${scheme}://${host}:${port}`;
|
|
466
|
-
return { canonical, insecure: scheme === "http" || scheme === "ws", host };
|
|
467
|
-
}
|
|
468
|
-
|
|
469
|
-
function parseCapabilities(input: unknown, source: string): Capabilities {
|
|
470
|
-
if (input !== undefined && (input === null || typeof input !== "object" || Array.isArray(input)))
|
|
471
|
-
throw new Error(`${source} must be an object`);
|
|
472
|
-
|
|
473
|
-
const capabilities = Object.create(null) as Capabilities;
|
|
474
|
-
for (const [key, value] of Object.entries(input ?? {}) as [string, unknown][]) {
|
|
475
|
-
const field = `${source}.${key}`;
|
|
476
|
-
switch (key) {
|
|
477
|
-
case "shellSurfaces":
|
|
478
|
-
case "sessionLock":
|
|
479
|
-
case "controlChannel":
|
|
480
|
-
if (typeof value !== "boolean") throw new Error(`${field} must be a boolean`);
|
|
481
|
-
if (value) capabilities[key] = true;
|
|
482
|
-
break;
|
|
483
|
-
case "clipboard": {
|
|
484
|
-
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
485
|
-
throw new Error(`${field} must be an object`);
|
|
486
|
-
for (const [subKey, subVal] of Object.entries(value as Record<string, unknown>)) {
|
|
487
|
-
if (subKey !== "read") throw new Error(`Unknown capability: ${field}.${subKey}`);
|
|
488
|
-
if (typeof subVal !== "boolean") throw new Error(`${field}.read must be a boolean`);
|
|
489
|
-
if (subVal) capabilities.clipboard = { read: true };
|
|
490
|
-
}
|
|
491
|
-
break;
|
|
492
|
-
}
|
|
493
|
-
case "sqlite": {
|
|
494
|
-
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
495
|
-
throw new Error(`${field} must be an object`);
|
|
496
|
-
for (const [subKey, subVal] of Object.entries(value as Record<string, unknown>)) {
|
|
497
|
-
if (subKey !== "databases") throw new Error(`Unknown capability: ${field}.${subKey}`);
|
|
498
|
-
if (!Array.isArray(subVal)) throw new Error(`${field}.databases must be an array`);
|
|
499
|
-
const names = new Set<string>();
|
|
500
|
-
for (const item of subVal as unknown[]) {
|
|
501
|
-
if (typeof item !== "string" || !/^[A-Za-z0-9][A-Za-z0-9_-]{0,63}$/.test(item))
|
|
502
|
-
throw new Error(`${field}.databases contains invalid entries`);
|
|
503
|
-
if (names.has(item)) throw new Error(`${field}.databases contains duplicates`);
|
|
504
|
-
names.add(item);
|
|
505
|
-
}
|
|
506
|
-
if (names.size) capabilities.sqlite = { databases: [...names] };
|
|
507
|
-
}
|
|
508
|
-
break;
|
|
509
|
-
}
|
|
510
|
-
case "files": {
|
|
511
|
-
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
512
|
-
throw new Error(`${field} must be an object`);
|
|
513
|
-
for (const [subKey, subVal] of Object.entries(value as Record<string, unknown>)) {
|
|
514
|
-
if (subKey !== "directories") throw new Error(`Unknown capability: ${field}.${subKey}`);
|
|
515
|
-
if (!Array.isArray(subVal)) throw new Error(`${field}.directories must be an array`);
|
|
516
|
-
const seen = new Set<string>();
|
|
517
|
-
const dirs: { path: string; mode: "read" | "readwrite" }[] = [];
|
|
518
|
-
for (const item of subVal as unknown[]) {
|
|
519
|
-
if (!item || typeof item !== "object" || Array.isArray(item))
|
|
520
|
-
throw new Error(`${field}.directories entries must be objects`);
|
|
521
|
-
const record = item as Record<string, unknown>;
|
|
522
|
-
const keys = Object.keys(record);
|
|
523
|
-
if (!keys.every(k => k === "path" || k === "mode"))
|
|
524
|
-
throw new Error(`${field}.directories entry contains unknown keys`);
|
|
525
|
-
if (
|
|
526
|
-
!capabilityString(record.path) ||
|
|
527
|
-
!fileDirectoryPath(record.path) ||
|
|
528
|
-
(record.mode !== "read" && record.mode !== "readwrite")
|
|
529
|
-
)
|
|
530
|
-
throw new Error(`${field}.directories contains invalid entries`);
|
|
531
|
-
if (seen.has(record.path))
|
|
532
|
-
throw new Error(`${field}.directories contains duplicate path ${record.path}`);
|
|
533
|
-
seen.add(record.path);
|
|
534
|
-
dirs.push({ path: record.path, mode: record.mode });
|
|
535
|
-
}
|
|
536
|
-
if (dirs.length) capabilities.files = { directories: dirs };
|
|
537
|
-
}
|
|
538
|
-
break;
|
|
539
|
-
}
|
|
540
|
-
case "network": {
|
|
541
|
-
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
542
|
-
throw new Error(`${field} must be an object`);
|
|
543
|
-
const network: {
|
|
544
|
-
origins?: string[];
|
|
545
|
-
originRules?: {
|
|
546
|
-
origin: string;
|
|
547
|
-
allowInsecureHttp?: boolean;
|
|
548
|
-
allowPrivateNetworks?: boolean;
|
|
549
|
-
}[];
|
|
550
|
-
allowInsecureHttp?: boolean;
|
|
551
|
-
allowPrivateNetworks?: boolean;
|
|
552
|
-
} = {};
|
|
553
|
-
const originSet = new Set<string>();
|
|
554
|
-
const ruleSet = new Set<string>();
|
|
555
|
-
for (const [subKey, subVal] of Object.entries(value as Record<string, unknown>)) {
|
|
556
|
-
switch (subKey) {
|
|
557
|
-
case "origins": {
|
|
558
|
-
if (!Array.isArray(subVal)) throw new Error(`${field}.origins must be an array`);
|
|
559
|
-
for (const item of subVal as unknown[]) {
|
|
560
|
-
const parsed = parseNetworkOrigin(item, `${field}.origins`);
|
|
561
|
-
if (originSet.has(parsed.canonical))
|
|
562
|
-
throw new Error(`${field}.origins contains duplicate origin ${parsed.canonical}`);
|
|
563
|
-
originSet.add(parsed.canonical);
|
|
564
|
-
}
|
|
565
|
-
if (originSet.size) network.origins = [...originSet];
|
|
566
|
-
break;
|
|
567
|
-
}
|
|
568
|
-
case "originRules": {
|
|
569
|
-
if (!Array.isArray(subVal)) throw new Error(`${field}.originRules must be an array`);
|
|
570
|
-
const rules: {
|
|
571
|
-
origin: string;
|
|
572
|
-
allowInsecureHttp?: boolean;
|
|
573
|
-
allowPrivateNetworks?: boolean;
|
|
574
|
-
}[] = [];
|
|
575
|
-
for (const item of subVal as unknown[]) {
|
|
576
|
-
if (!item || typeof item !== "object" || Array.isArray(item))
|
|
577
|
-
throw new Error(`${field}.originRules entries must be objects`);
|
|
578
|
-
const record = item as Record<string, unknown>;
|
|
579
|
-
for (const ruleKey of Object.keys(record)) {
|
|
580
|
-
if (
|
|
581
|
-
ruleKey !== "origin" &&
|
|
582
|
-
ruleKey !== "allowInsecureHttp" &&
|
|
583
|
-
ruleKey !== "allowPrivateNetworks"
|
|
584
|
-
) {
|
|
585
|
-
throw new Error(`${field}.originRules entry contains unknown key: ${ruleKey}`);
|
|
586
|
-
}
|
|
587
|
-
}
|
|
588
|
-
const parsed = parseNetworkOrigin(record.origin, `${field}.originRules`);
|
|
589
|
-
if (
|
|
590
|
-
record.allowInsecureHttp !== undefined &&
|
|
591
|
-
typeof record.allowInsecureHttp !== "boolean"
|
|
592
|
-
) {
|
|
593
|
-
throw new Error(`${field}.originRules allowInsecureHttp must be a boolean`);
|
|
594
|
-
}
|
|
595
|
-
if (
|
|
596
|
-
record.allowPrivateNetworks !== undefined &&
|
|
597
|
-
typeof record.allowPrivateNetworks !== "boolean"
|
|
598
|
-
) {
|
|
599
|
-
throw new Error(`${field}.originRules allowPrivateNetworks must be a boolean`);
|
|
600
|
-
}
|
|
601
|
-
const allowInsecureHttp = record.allowInsecureHttp === true;
|
|
602
|
-
const allowPrivateNetworks = record.allowPrivateNetworks === true;
|
|
603
|
-
if (parsed.insecure && !allowInsecureHttp) {
|
|
604
|
-
throw new Error(
|
|
605
|
-
`${field}.originRules contains insecure origin ${parsed.canonical} without allowInsecureHttp: true`,
|
|
606
|
-
);
|
|
607
|
-
}
|
|
608
|
-
if (isPrivateHost(parsed.host) && !allowPrivateNetworks) {
|
|
609
|
-
throw new Error(
|
|
610
|
-
`${field}.originRules contains private/loopback origin ${parsed.canonical} without allowPrivateNetworks: true`,
|
|
611
|
-
);
|
|
612
|
-
}
|
|
613
|
-
if (ruleSet.has(parsed.canonical)) {
|
|
614
|
-
throw new Error(
|
|
615
|
-
`${field}.originRules contains duplicate origin ${parsed.canonical}`,
|
|
616
|
-
);
|
|
617
|
-
}
|
|
618
|
-
ruleSet.add(parsed.canonical);
|
|
619
|
-
rules.push({
|
|
620
|
-
origin: parsed.canonical,
|
|
621
|
-
...(allowInsecureHttp ? { allowInsecureHttp: true } : {}),
|
|
622
|
-
...(allowPrivateNetworks ? { allowPrivateNetworks: true } : {}),
|
|
623
|
-
});
|
|
624
|
-
}
|
|
625
|
-
if (rules.length) network.originRules = rules;
|
|
626
|
-
break;
|
|
627
|
-
}
|
|
628
|
-
case "allowInsecureHttp":
|
|
629
|
-
case "allowPrivateNetworks": {
|
|
630
|
-
if (typeof subVal !== "boolean")
|
|
631
|
-
throw new Error(`${field}.${subKey} must be a boolean`);
|
|
632
|
-
if (subVal) network[subKey] = true;
|
|
633
|
-
break;
|
|
634
|
-
}
|
|
635
|
-
default:
|
|
636
|
-
throw new Error(`Unknown capability: ${field}.${subKey}`);
|
|
637
|
-
}
|
|
638
|
-
}
|
|
639
|
-
for (const origin of ruleSet) {
|
|
640
|
-
if (originSet.has(origin)) {
|
|
641
|
-
throw new Error(
|
|
642
|
-
`${field} declares duplicate origin in origins and originRules: ${origin}`,
|
|
643
|
-
);
|
|
644
|
-
}
|
|
645
|
-
}
|
|
646
|
-
if (Object.keys(network).length) capabilities.network = network;
|
|
647
|
-
break;
|
|
648
|
-
}
|
|
649
|
-
case "imagePrefixes":
|
|
650
|
-
case "unixSockets":
|
|
651
|
-
case "commands":
|
|
652
|
-
case "namedSockets": {
|
|
653
|
-
if (!Array.isArray(value)) throw new Error(`${field} must be an array`);
|
|
654
|
-
const entries = new Set<string>();
|
|
655
|
-
for (const item of value as unknown[]) {
|
|
656
|
-
if (
|
|
657
|
-
!capabilityString(item) ||
|
|
658
|
-
(key === "namedSockets" && !/^[A-Za-z][A-Za-z0-9_-]{0,63}$/.test(item)) ||
|
|
659
|
-
((key === "imagePrefixes" || key === "unixSockets") && !capabilityPath(item))
|
|
660
|
-
)
|
|
661
|
-
throw new Error(`${field} contains invalid entries`);
|
|
662
|
-
if (entries.has(item)) throw new Error(`${field} contains duplicates`);
|
|
663
|
-
entries.add(item);
|
|
664
|
-
}
|
|
665
|
-
if (entries.size) capabilities[key] = [...entries];
|
|
666
|
-
break;
|
|
667
|
-
}
|
|
668
|
-
default:
|
|
669
|
-
throw new Error(`Unknown capability: ${field}`);
|
|
670
|
-
}
|
|
671
|
-
}
|
|
672
|
-
return capabilities;
|
|
673
|
-
}
|
|
674
|
-
|
|
675
|
-
function capabilityOverlay(input: unknown, source: string): Capabilities {
|
|
676
|
-
if (
|
|
677
|
-
input !== null &&
|
|
678
|
-
typeof input === "object" &&
|
|
679
|
-
"sessionLock" in input &&
|
|
680
|
-
input.sessionLock === true
|
|
681
|
-
)
|
|
682
|
-
throw new Error(`${source}.sessionLock: true may only be declared in base package.json`);
|
|
683
|
-
return parseCapabilities(input, source);
|
|
684
|
-
}
|
|
685
|
-
|
|
686
|
-
async function mergeCapabilities(
|
|
687
|
-
input: unknown,
|
|
320
|
+
export async function buildWithResolvedHost(
|
|
321
|
+
app: string,
|
|
322
|
+
options: BuildApplicationOptions & { root: string; appDirectory: string },
|
|
688
323
|
{
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
if (base.sessionLock) merged.sessionLock = true;
|
|
711
|
-
for (const key of ["imagePrefixes", "unixSockets", "namedSockets", "commands"] as const) {
|
|
712
|
-
const union = new Set(sources.flatMap(source => source[key] ?? []));
|
|
713
|
-
if (union.size) merged[key] = [...union].sort();
|
|
714
|
-
}
|
|
715
|
-
if (sources.some(source => source.clipboard?.read)) merged.clipboard = { read: true };
|
|
716
|
-
const sqliteDatabases = new Set(sources.flatMap(source => source.sqlite?.databases ?? []));
|
|
717
|
-
if (sqliteDatabases.size) merged.sqlite = { databases: [...sqliteDatabases].sort() };
|
|
718
|
-
const fileDirs = new Map<string, "read" | "readwrite">();
|
|
719
|
-
for (const grant of sources.flatMap(source => source.files?.directories ?? [])) {
|
|
720
|
-
const existing = fileDirs.get(grant.path);
|
|
721
|
-
if (!existing || grant.mode === "readwrite") fileDirs.set(grant.path, grant.mode);
|
|
722
|
-
}
|
|
723
|
-
if (fileDirs.size) {
|
|
724
|
-
merged.files = {
|
|
725
|
-
directories: [...fileDirs.entries()]
|
|
726
|
-
.sort(([a], [b]) => a.localeCompare(b))
|
|
727
|
-
.map(([dirPath, mode]) => ({ path: dirPath, mode })),
|
|
728
|
-
};
|
|
729
|
-
}
|
|
730
|
-
const networkSources = sources
|
|
731
|
-
.map(source => source.network)
|
|
732
|
-
.filter((net): net is NonNullable<Capabilities["network"]> => net !== undefined);
|
|
733
|
-
const usesLegacySingleSource =
|
|
734
|
-
networkSources.length === 1 &&
|
|
735
|
-
(networkSources[0]!.originRules === undefined || networkSources[0]!.originRules.length === 0);
|
|
736
|
-
if (usesLegacySingleSource) {
|
|
737
|
-
const only = networkSources[0]!;
|
|
738
|
-
const allowInsecureHttp = Boolean(only.allowInsecureHttp);
|
|
739
|
-
const allowPrivateNetworks = Boolean(only.allowPrivateNetworks);
|
|
740
|
-
const networkOrigins = [...new Set(only.origins ?? [])].sort();
|
|
741
|
-
for (const origin of networkOrigins) {
|
|
742
|
-
const parsed = parseNetworkOrigin(origin, "capabilities.network.origins");
|
|
743
|
-
if (parsed.insecure && !allowInsecureHttp) {
|
|
744
|
-
throw new Error(
|
|
745
|
-
`capabilities.network.origins contains insecure origin ${origin} without allowInsecureHttp: true`,
|
|
746
|
-
);
|
|
747
|
-
}
|
|
748
|
-
if (isPrivateHost(parsed.host) && !allowPrivateNetworks) {
|
|
749
|
-
throw new Error(
|
|
750
|
-
`capabilities.network.origins contains private/loopback origin ${origin} without allowPrivateNetworks: true`,
|
|
751
|
-
);
|
|
752
|
-
}
|
|
753
|
-
}
|
|
754
|
-
if (networkOrigins.length || allowInsecureHttp || allowPrivateNetworks) {
|
|
755
|
-
merged.network = {
|
|
756
|
-
...(networkOrigins.length ? { origins: networkOrigins } : {}),
|
|
757
|
-
...(allowInsecureHttp ? { allowInsecureHttp: true } : {}),
|
|
758
|
-
...(allowPrivateNetworks ? { allowPrivateNetworks: true } : {}),
|
|
759
|
-
};
|
|
760
|
-
}
|
|
761
|
-
} else if (networkSources.length > 0) {
|
|
762
|
-
const byOrigin = new Map<
|
|
763
|
-
string,
|
|
764
|
-
{ allowInsecureHttp: boolean; allowPrivateNetworks: boolean }
|
|
765
|
-
>();
|
|
766
|
-
for (const net of networkSources) {
|
|
767
|
-
const sourceInsecure = Boolean(net.allowInsecureHttp);
|
|
768
|
-
const sourcePrivate = Boolean(net.allowPrivateNetworks);
|
|
769
|
-
for (const origin of net.origins ?? []) {
|
|
770
|
-
const parsed = parseNetworkOrigin(origin, "capabilities.network.origins");
|
|
771
|
-
if (parsed.insecure && !sourceInsecure) {
|
|
772
|
-
throw new Error(
|
|
773
|
-
`capabilities.network.origins contains insecure origin ${origin} without allowInsecureHttp: true`,
|
|
774
|
-
);
|
|
775
|
-
}
|
|
776
|
-
if (isPrivateHost(parsed.host) && !sourcePrivate) {
|
|
777
|
-
throw new Error(
|
|
778
|
-
`capabilities.network.origins contains private/loopback origin ${origin} without allowPrivateNetworks: true`,
|
|
779
|
-
);
|
|
780
|
-
}
|
|
781
|
-
const prev = byOrigin.get(parsed.canonical) ?? {
|
|
782
|
-
allowInsecureHttp: false,
|
|
783
|
-
allowPrivateNetworks: false,
|
|
784
|
-
};
|
|
785
|
-
byOrigin.set(parsed.canonical, {
|
|
786
|
-
allowInsecureHttp: prev.allowInsecureHttp || sourceInsecure,
|
|
787
|
-
allowPrivateNetworks: prev.allowPrivateNetworks || sourcePrivate,
|
|
788
|
-
});
|
|
789
|
-
}
|
|
790
|
-
for (const rule of net.originRules ?? []) {
|
|
791
|
-
const parsed = parseNetworkOrigin(rule.origin, "capabilities.network.originRules");
|
|
792
|
-
const prev = byOrigin.get(parsed.canonical) ?? {
|
|
793
|
-
allowInsecureHttp: false,
|
|
794
|
-
allowPrivateNetworks: false,
|
|
795
|
-
};
|
|
796
|
-
byOrigin.set(parsed.canonical, {
|
|
797
|
-
allowInsecureHttp: prev.allowInsecureHttp || Boolean(rule.allowInsecureHttp),
|
|
798
|
-
allowPrivateNetworks: prev.allowPrivateNetworks || Boolean(rule.allowPrivateNetworks),
|
|
799
|
-
});
|
|
800
|
-
}
|
|
801
|
-
}
|
|
802
|
-
if (byOrigin.size > 0) {
|
|
803
|
-
merged.network = {
|
|
804
|
-
originRules: [...byOrigin.entries()]
|
|
805
|
-
.sort(([a], [b]) => a.localeCompare(b))
|
|
806
|
-
.map(([origin, flags]) => ({
|
|
807
|
-
origin,
|
|
808
|
-
...(flags.allowInsecureHttp ? { allowInsecureHttp: true } : {}),
|
|
809
|
-
...(flags.allowPrivateNetworks ? { allowPrivateNetworks: true } : {}),
|
|
810
|
-
})),
|
|
811
|
-
};
|
|
812
|
-
}
|
|
813
|
-
}
|
|
814
|
-
return Object.keys(merged).length ? merged : undefined;
|
|
815
|
-
}
|
|
816
|
-
|
|
817
|
-
const WINDOW_KEYS = new Set(["title", "width", "height", "minWidth", "minHeight", "resizable"]);
|
|
818
|
-
|
|
819
|
-
function parseWindowDeclaration(input: unknown): WindowDeclaration | undefined {
|
|
820
|
-
if (input === undefined) return undefined;
|
|
821
|
-
if (!input || typeof input !== "object" || Array.isArray(input)) {
|
|
822
|
-
throw new Error("package.json zgui.window must be an object");
|
|
823
|
-
}
|
|
824
|
-
const record = input as Record<string, unknown>;
|
|
825
|
-
for (const key of Object.keys(record)) {
|
|
826
|
-
if (!WINDOW_KEYS.has(key)) throw new Error(`Unknown package.json zgui.window key: ${key}`);
|
|
827
|
-
}
|
|
828
|
-
const result: WindowDeclaration = {};
|
|
829
|
-
if (record.title !== undefined) {
|
|
830
|
-
if (!capabilityString(record.title) || record.title !== record.title.trim()) {
|
|
831
|
-
throw new Error(
|
|
832
|
-
"package.json zgui.window.title must be a non-empty, trimmed string without control characters",
|
|
833
|
-
);
|
|
834
|
-
}
|
|
835
|
-
result.title = record.title;
|
|
836
|
-
}
|
|
837
|
-
for (const key of ["width", "height", "minWidth", "minHeight"] as const) {
|
|
838
|
-
const value = record[key];
|
|
839
|
-
if (value !== undefined) {
|
|
840
|
-
if (typeof value !== "number" || !Number.isInteger(value) || value < 1 || value > 16384) {
|
|
841
|
-
throw new Error(`package.json zgui.window.${key} must be an integer between 1 and 16384`);
|
|
842
|
-
}
|
|
843
|
-
result[key] = value;
|
|
844
|
-
}
|
|
845
|
-
}
|
|
846
|
-
if (record.resizable !== undefined) {
|
|
847
|
-
if (typeof record.resizable !== "boolean") {
|
|
848
|
-
throw new Error("package.json zgui.window.resizable must be a boolean");
|
|
849
|
-
}
|
|
850
|
-
result.resizable = record.resizable;
|
|
851
|
-
}
|
|
852
|
-
return Object.keys(result).length ? result : undefined;
|
|
853
|
-
}
|
|
854
|
-
|
|
855
|
-
const WORKER_NAME_RE = /^[a-zA-Z][a-zA-Z0-9_-]{0,63}$/;
|
|
856
|
-
const MAX_WORKERS = 32;
|
|
857
|
-
|
|
858
|
-
function parseWorkerDeclarations(
|
|
859
|
-
input: unknown,
|
|
860
|
-
appDirectory: string,
|
|
861
|
-
): { name: string; entry: string }[] {
|
|
862
|
-
if (input === undefined) return [];
|
|
863
|
-
if (!input || typeof input !== "object" || Array.isArray(input)) {
|
|
864
|
-
throw new Error(
|
|
865
|
-
"package.json zgui.workers must be an object mapping worker names to relative entry paths",
|
|
866
|
-
);
|
|
867
|
-
}
|
|
868
|
-
const entries = Object.entries(input as Record<string, unknown>);
|
|
869
|
-
if (entries.length > MAX_WORKERS) {
|
|
870
|
-
throw new Error(`package.json zgui.workers exceeds maximum of ${MAX_WORKERS} workers`);
|
|
871
|
-
}
|
|
872
|
-
const resolvedAppDir = path.resolve(appDirectory);
|
|
873
|
-
const parsed: { name: string; entry: string }[] = [];
|
|
874
|
-
for (const [name, relPath] of entries.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))) {
|
|
875
|
-
if (!WORKER_NAME_RE.test(name)) {
|
|
876
|
-
throw new Error(`Invalid compute worker name: ${name}`);
|
|
877
|
-
}
|
|
878
|
-
if (typeof relPath !== "string" || !relPath.trim()) {
|
|
879
|
-
throw new Error(`package.json zgui.workers.${name} must be a non-empty relative path`);
|
|
880
|
-
}
|
|
881
|
-
resourcePath(relPath);
|
|
882
|
-
const fullPath = path.resolve(resolvedAppDir, relPath);
|
|
883
|
-
if (!fullPath.startsWith(`${resolvedAppDir}${path.sep}`)) {
|
|
884
|
-
throw new Error(
|
|
885
|
-
`package.json zgui.workers.${name} escapes application directory: ${relPath}`,
|
|
886
|
-
);
|
|
887
|
-
}
|
|
888
|
-
parsed.push({ name, entry: fullPath });
|
|
889
|
-
}
|
|
890
|
-
return parsed;
|
|
324
|
+
hostRoot,
|
|
325
|
+
hostExecutable,
|
|
326
|
+
signal,
|
|
327
|
+
}: Pick<ResolveNativeHostOptions, "hostRoot" | "hostExecutable" | "signal">,
|
|
328
|
+
) {
|
|
329
|
+
let host: Promise<ResolvedNativeHost> | undefined;
|
|
330
|
+
const resolveHost = () =>
|
|
331
|
+
(host ??= resolveNativeHost({
|
|
332
|
+
hostExecutable,
|
|
333
|
+
hostRoot,
|
|
334
|
+
root: options.root,
|
|
335
|
+
cwd: options.appDirectory,
|
|
336
|
+
searchFrom: options.hostSearchFrom,
|
|
337
|
+
signal,
|
|
338
|
+
}));
|
|
339
|
+
// Bytecode needs the compiler early; packaging validates the manifest before resolving a runtime-only host.
|
|
340
|
+
const application = await buildApplication(app, options, {
|
|
341
|
+
signal,
|
|
342
|
+
nativeHost: options.bytecode ? await resolveHost() : undefined,
|
|
343
|
+
});
|
|
344
|
+
return { application, resolveHost };
|
|
891
345
|
}
|
|
892
346
|
|
|
893
347
|
export async function publishApplication(
|
|
@@ -902,9 +356,15 @@ export async function publishApplication(
|
|
|
902
356
|
hostRoot,
|
|
903
357
|
hostExecutable,
|
|
904
358
|
hostSearchFrom,
|
|
359
|
+
nativeHost,
|
|
905
360
|
extraCapabilities,
|
|
906
361
|
capabilitiesFile,
|
|
907
|
-
}: BuildApplicationOptions & {
|
|
362
|
+
}: BuildApplicationOptions & {
|
|
363
|
+
root: string;
|
|
364
|
+
appDirectory: string;
|
|
365
|
+
signal?: AbortSignal | undefined;
|
|
366
|
+
nativeHost?: ResolvedNativeHost | undefined;
|
|
367
|
+
},
|
|
908
368
|
): Promise<PublishedApplication> {
|
|
909
369
|
signal?.throwIfAborted();
|
|
910
370
|
const files = new Map(result.files);
|
|
@@ -926,29 +386,13 @@ export async function publishApplication(
|
|
|
926
386
|
files.set("app.css", Buffer.from(mergedAppCss, "utf8"));
|
|
927
387
|
const assets = await collectAssets(path.join(appDirectory, "assets"));
|
|
928
388
|
for (const item of assets) files.set(...item);
|
|
929
|
-
const
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
window?: unknown;
|
|
937
|
-
workers?: unknown;
|
|
938
|
-
};
|
|
939
|
-
}
|
|
940
|
-
| undefined = pkg && JSON.parse(pkg.toString("utf8"));
|
|
941
|
-
const id = packageJson?.zgui?.id === undefined ? `dev.example.${app}` : packageJson.zgui.id;
|
|
942
|
-
if (!capabilityString(id) || id !== id.trim())
|
|
943
|
-
throw new Error(
|
|
944
|
-
"package.json zgui.id must be a non-empty, trimmed string without control characters",
|
|
945
|
-
);
|
|
946
|
-
const windowConfig = parseWindowDeclaration(packageJson?.zgui?.window);
|
|
947
|
-
const capabilities = await mergeCapabilities(packageJson?.zgui?.capabilities, {
|
|
948
|
-
extraCapabilities,
|
|
949
|
-
capabilitiesFile,
|
|
950
|
-
});
|
|
951
|
-
const workerEntries = parseWorkerDeclarations(packageJson?.zgui?.workers, appDirectory);
|
|
389
|
+
const {
|
|
390
|
+
id,
|
|
391
|
+
window: windowConfig,
|
|
392
|
+
capabilities,
|
|
393
|
+
workerEntries,
|
|
394
|
+
fonts,
|
|
395
|
+
} = await readManifestPolicy(app, appDirectory, assets, { extraCapabilities, capabilitiesFile });
|
|
952
396
|
const workers: WorkerDeclaration[] = [];
|
|
953
397
|
for (const worker of workerEntries) {
|
|
954
398
|
signal?.throwIfAborted();
|
|
@@ -963,21 +407,6 @@ export async function publishApplication(
|
|
|
963
407
|
}
|
|
964
408
|
workers.push({ name: compiled.name, path: compiled.path, sourceMap: compiled.sourceMap });
|
|
965
409
|
}
|
|
966
|
-
const declarations = packageJson?.zgui?.fonts ?? [];
|
|
967
|
-
if (!Array.isArray(declarations))
|
|
968
|
-
throw new Error("package.json zgui.fonts must be an array of {family,path}");
|
|
969
|
-
const fonts: FontDeclaration[] = [];
|
|
970
|
-
const families = new Set<string>();
|
|
971
|
-
for (const font of declarations as unknown[]) {
|
|
972
|
-
if (!isFontDeclaration(font))
|
|
973
|
-
throw new Error("Invalid application font mapping; expected {family,path}");
|
|
974
|
-
resourcePath(font.path);
|
|
975
|
-
if (!assets.has(font.path)) throw new Error(`Font path must be a declared asset: ${font.path}`);
|
|
976
|
-
const family = font.family.toLowerCase();
|
|
977
|
-
if (families.has(family)) throw new Error(`Duplicate application font family: ${font.family}`);
|
|
978
|
-
families.add(family);
|
|
979
|
-
fonts.push(font);
|
|
980
|
-
}
|
|
981
410
|
files.set(
|
|
982
411
|
"build-info.json",
|
|
983
412
|
Buffer.from(
|
|
@@ -994,14 +423,16 @@ export async function publishApplication(
|
|
|
994
423
|
let finalChunks = chunks;
|
|
995
424
|
let finalWorkers = workers;
|
|
996
425
|
if (bytecode) {
|
|
997
|
-
const host =
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
426
|
+
const host =
|
|
427
|
+
nativeHost ??
|
|
428
|
+
(await resolveNativeHost({
|
|
429
|
+
hostExecutable,
|
|
430
|
+
hostRoot,
|
|
431
|
+
root,
|
|
432
|
+
cwd: appDirectory,
|
|
433
|
+
searchFrom: hostSearchFrom,
|
|
434
|
+
signal,
|
|
435
|
+
}));
|
|
1005
436
|
const bytecodeFormat = host.abi?.bytecodeFormat;
|
|
1006
437
|
const quickjsAbi = host.abi?.quickjsAbi;
|
|
1007
438
|
if (bytecodeFormat !== "qjs-bytecode-v1" || !quickjsAbi) {
|
|
@@ -1035,7 +466,7 @@ export async function publishApplication(
|
|
|
1035
466
|
const bcPath = toBytecodePath(jsPath);
|
|
1036
467
|
const inPath = path.join(sourceDir, jsPath);
|
|
1037
468
|
const outPath = path.join(outDir, bcPath);
|
|
1038
|
-
const bcBytes = await compileModuleBytecode(host
|
|
469
|
+
const bcBytes = await compileModuleBytecode(host, {
|
|
1039
470
|
input: inPath,
|
|
1040
471
|
output: outPath,
|
|
1041
472
|
moduleName: jsPath,
|