@tarve/core 0.5.0 → 0.6.0
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/CHANGELOG.md +46 -0
- package/README.md +54 -2
- package/dist/npm/build.js +714 -138
- package/dist/npm/cli.js +1013 -161
- package/dist/npm/embedded-assets.js +7 -2
- package/dist/npm/index.js +3096 -1084
- package/dist/npm/jsx-dev-runtime.js +1268 -0
- package/dist/npm/jsx-runtime.js +1268 -0
- package/dist/npm/native-runtime.json +3 -3
- package/dist/npm/node-build.js +299 -5
- package/dist/npm/protocol.js +1 -1
- package/dist/npm/txiki/fetch.ts +228 -0
- package/dist/npm/txiki/intl-data.ts +7 -0
- package/dist/npm/txiki/intl.test.ts +53 -0
- package/dist/npm/txiki/intl.ts +249 -0
- package/dist/npm/txiki/prelude.ts +132 -0
- package/dist/npm/txiki/runtime.ts +6 -0
- package/dist/npm/txiki/shims/buffer.ts +112 -0
- package/dist/npm/txiki/shims/child-process.ts +151 -0
- package/dist/npm/txiki/shims/crypto.ts +30 -0
- package/dist/npm/txiki/shims/events.ts +75 -0
- package/dist/npm/txiki/shims/fs-promises.ts +77 -0
- package/dist/npm/txiki/shims/fs.ts +231 -0
- package/dist/npm/txiki/shims/module.ts +8 -0
- package/dist/npm/txiki/shims/net.ts +69 -0
- package/dist/npm/txiki/shims/os.ts +72 -0
- package/dist/npm/txiki/shims/path.ts +4 -0
- package/dist/npm/txiki/shims/timers-promises.ts +9 -0
- package/dist/npm/txiki/shims/zlib.ts +9 -0
- package/dist/npm/txiki/tjs.d.ts +58 -0
- package/dist/npm/txiki/unavailable.ts +10 -0
- package/dist/types/core/build.d.ts +5 -2
- package/dist/types/core/jsx-compile.d.ts +7 -0
- package/dist/types/core/src/advanced-controls.d.ts +1 -1
- package/dist/types/core/src/app/element-events.d.ts +18 -0
- package/dist/types/core/src/app/media.d.ts +15 -0
- package/dist/types/core/src/app/requests.d.ts +25 -0
- package/dist/types/core/src/app/shadow.d.ts +7 -0
- package/dist/types/core/src/app/stats.d.ts +12 -0
- package/dist/types/core/src/app/tray.d.ts +40 -0
- package/dist/types/core/src/app-metadata.d.ts +2 -0
- package/dist/types/core/src/app.d.ts +29 -27
- package/dist/types/core/src/bridge/index.d.ts +1 -1
- package/dist/types/core/src/chat-controls.d.ts +2 -1
- package/dist/types/core/src/components/button.d.ts +1 -1
- package/dist/types/core/src/components/glass.d.ts +52 -0
- package/dist/types/core/src/components/icon.d.ts +1 -1
- package/dist/types/core/src/components/image.d.ts +1 -1
- package/dist/types/core/src/components/layout.d.ts +1 -1
- package/dist/types/core/src/components/pressable.d.ts +1 -1
- package/dist/types/core/src/components/rich-content.d.ts +1 -1
- package/dist/types/core/src/components/scroll.d.ts +2 -2
- package/dist/types/core/src/components/svg.d.ts +1 -1
- package/dist/types/core/src/components/titlebar.d.ts +15 -1
- package/dist/types/core/src/components/window.d.ts +21 -1
- package/dist/types/core/src/components.d.ts +2 -0
- package/dist/types/core/src/css-shadow.d.ts +1 -1
- package/dist/types/core/src/desktop-controls.d.ts +2 -2
- package/dist/types/core/src/embedded-assets.d.ts +3 -2
- package/dist/types/core/src/extra-controls.d.ts +1 -1
- package/dist/types/core/src/form-controls.d.ts +1 -1
- package/dist/types/core/src/index.d.ts +20 -11
- package/dist/types/core/src/inline-text.d.ts +7 -3
- package/dist/types/core/src/intrinsic-style.d.ts +1 -1
- package/dist/types/core/src/jsx-runtime.d.ts +10 -4
- package/dist/types/core/src/media-element.d.ts +3 -4
- package/dist/types/core/src/memo.d.ts +10 -0
- package/dist/types/core/src/motion.d.ts +1 -1
- package/dist/types/core/src/popups.d.ts +1 -1
- package/dist/types/core/src/protocol.d.ts +6 -0
- package/dist/types/core/src/reconciler/diff.d.ts +11 -0
- package/dist/types/core/src/reconciler/intrinsics.d.ts +24 -0
- package/dist/types/core/src/reconciler.d.ts +10 -11
- package/dist/types/core/src/render-scope.d.ts +8 -0
- package/dist/types/core/src/style.d.ts +1 -1
- package/dist/types/core/src/tailwind-palette.d.ts +1 -0
- package/dist/types/core/src/tailwind.d.ts +8 -0
- package/dist/types/core/src/test-renderer.d.ts +1 -1
- package/dist/types/core/src/text-search.d.ts +1 -1
- package/dist/types/core/src/theme.d.ts +8 -1
- package/dist/types/core/src/updates.d.ts +87 -0
- package/dist/types/core/src/virtual-list.d.ts +3 -1
- package/dist/types/core/txiki/fetch.d.ts +1 -0
- package/dist/types/core/txiki/intl-data.d.ts +6 -0
- package/dist/types/core/txiki/intl.d.ts +92 -0
- package/dist/types/core/txiki/prelude.d.ts +1 -0
- package/dist/types/core/txiki/runtime.d.ts +2 -0
- package/dist/types/core/txiki/shims/buffer.d.ts +37 -0
- package/dist/types/core/txiki/shims/child-process.d.ts +46 -0
- package/dist/types/core/txiki/shims/crypto.d.ts +16 -0
- package/dist/types/core/txiki/shims/events.d.ts +21 -0
- package/dist/types/core/txiki/shims/fs-promises.d.ts +41 -0
- package/dist/types/core/txiki/shims/fs.d.ts +44 -0
- package/dist/types/core/txiki/shims/module.d.ts +5 -0
- package/dist/types/core/txiki/shims/net.d.ts +22 -0
- package/dist/types/core/txiki/shims/os.d.ts +52 -0
- package/dist/types/core/txiki/shims/path.d.ts +3 -0
- package/dist/types/core/txiki/shims/timers-promises.d.ts +7 -0
- package/dist/types/core/txiki/shims/zlib.d.ts +11 -0
- package/dist/types/core/txiki/unavailable.d.ts +5 -0
- package/dist/types/core/txiki-build.d.ts +23 -0
- package/dist/types/core/updates-build.d.ts +35 -0
- package/dist/types/protocol/src/index.d.ts +159 -8
- package/native/linux-x64/libtarve_native.so +0 -0
- package/native/linux-x64/runtime.json +4 -4
- package/native/win32-x64/runtime.json +4 -4
- package/native/win32-x64/tarve_native.dll +0 -0
- package/package.json +3 -2
- /package/dist/types/core/{app-metadata.d.ts → package-metadata.d.ts} +0 -0
package/dist/npm/cli.js
CHANGED
|
@@ -255,7 +255,7 @@ var init_png = __esm(() => {
|
|
|
255
255
|
});
|
|
256
256
|
});
|
|
257
257
|
|
|
258
|
-
// packages/core/
|
|
258
|
+
// packages/core/package-metadata.ts
|
|
259
259
|
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
260
260
|
import { basename, dirname, extname, join, relative, resolve } from "node:path";
|
|
261
261
|
function fields(value, where) {
|
|
@@ -365,6 +365,7 @@ function buildMetadata(entrypoint, overrides) {
|
|
|
365
365
|
function runtimeMetadata(metadata, icon) {
|
|
366
366
|
return JSON.stringify({
|
|
367
367
|
name: metadata.name,
|
|
368
|
+
...metadata.version ? { version: metadata.version } : {},
|
|
368
369
|
...metadata.appId ? { appId: metadata.appId } : {},
|
|
369
370
|
...icon ? { icon } : {}
|
|
370
371
|
});
|
|
@@ -392,7 +393,7 @@ function writeLinuxDesktopEntry(outfile, metadata, png) {
|
|
|
392
393
|
return desktop;
|
|
393
394
|
}
|
|
394
395
|
var FIELDS, ICO_SIZES;
|
|
395
|
-
var
|
|
396
|
+
var init_package_metadata = __esm(() => {
|
|
396
397
|
init_png();
|
|
397
398
|
FIELDS = ["name", "version", "description", "publisher", "copyright", "icon", "appId"];
|
|
398
399
|
ICO_SIZES = [16, 20, 24, 32, 40, 48, 64, 96, 128, 256];
|
|
@@ -435,7 +436,7 @@ var init_targets = __esm(() => {
|
|
|
435
436
|
});
|
|
436
437
|
|
|
437
438
|
// packages/protocol/src/index.ts
|
|
438
|
-
var NATIVE_ABI_VERSION = 5, PROTOCOL_VERSION =
|
|
439
|
+
var NATIVE_ABI_VERSION = 5, PROTOCOL_VERSION = 59;
|
|
439
440
|
|
|
440
441
|
// packages/core/native-runtime.ts
|
|
441
442
|
import { createHash } from "node:crypto";
|
|
@@ -546,12 +547,613 @@ var init_native_runtime = __esm(() => {
|
|
|
546
547
|
init_targets();
|
|
547
548
|
});
|
|
548
549
|
|
|
550
|
+
// packages/core/jsx-compile.ts
|
|
551
|
+
var exports_jsx_compile = {};
|
|
552
|
+
__export(exports_jsx_compile, {
|
|
553
|
+
closeJsxCompiler: () => closeJsxCompiler,
|
|
554
|
+
compileJsx: () => compileJsx
|
|
555
|
+
});
|
|
556
|
+
async function loadParser() {
|
|
557
|
+
let modules;
|
|
558
|
+
try {
|
|
559
|
+
modules = await Promise.all([import("typescript/unstable/ast"), import("typescript/unstable/ast/is")]);
|
|
560
|
+
} catch (error) {
|
|
561
|
+
throw new Error(`compileJsx needs TypeScript 7 (npm install -D typescript@7): ${error instanceof Error ? error.message : String(error)}`);
|
|
562
|
+
}
|
|
563
|
+
const [ast, is] = modules;
|
|
564
|
+
const files = new Map([[CONFIG, JSON.stringify({ compilerOptions: { jsx: "preserve", noLib: true, noResolve: true, types: [] }, files: ["app.tsx"] })]]);
|
|
565
|
+
const normalize = (path) => path.replace(/\\/g, "/").replace(/^[A-Za-z]:/, "");
|
|
566
|
+
const options = {
|
|
567
|
+
cwd: ROOT,
|
|
568
|
+
fs: {
|
|
569
|
+
readFile: (path) => files.get(normalize(path)),
|
|
570
|
+
fileExists: (path) => files.has(normalize(path)),
|
|
571
|
+
directoryExists: (path) => {
|
|
572
|
+
const p = normalize(path).replace(/\/$/, "");
|
|
573
|
+
return p === "" || ROOT.startsWith(p);
|
|
574
|
+
},
|
|
575
|
+
getAccessibleEntries: (path) => normalize(path).replace(/\/$/, "") === ROOT ? { files: ["app.tsx", "tsconfig.json"], directories: [] } : { files: [], directories: [] },
|
|
576
|
+
realpath: (path) => path
|
|
577
|
+
}
|
|
578
|
+
};
|
|
579
|
+
const request = { openProject: CONFIG, fileChanges: { invalidateAll: true } };
|
|
580
|
+
if ((process.platform === "win32" || typeof Bun === "undefined") && process.env.TARVE_TSGO_ASYNC !== "1") {
|
|
581
|
+
const sync = await import("typescript/unstable/sync");
|
|
582
|
+
const api = new sync.API(options);
|
|
583
|
+
return {
|
|
584
|
+
ast,
|
|
585
|
+
is,
|
|
586
|
+
files,
|
|
587
|
+
parse: async () => {
|
|
588
|
+
api.clearSourceFileCache();
|
|
589
|
+
const snapshot = api.updateSnapshot(request);
|
|
590
|
+
try {
|
|
591
|
+
return snapshot.getProjects()[0].program.getSourceFile(FILE);
|
|
592
|
+
} finally {
|
|
593
|
+
snapshot.dispose();
|
|
594
|
+
}
|
|
595
|
+
},
|
|
596
|
+
close: async () => api.close()
|
|
597
|
+
};
|
|
598
|
+
}
|
|
599
|
+
const asyncApi = await import("typescript/unstable/async");
|
|
600
|
+
const api = new asyncApi.API(options);
|
|
601
|
+
return {
|
|
602
|
+
ast,
|
|
603
|
+
is,
|
|
604
|
+
files,
|
|
605
|
+
parse: async () => {
|
|
606
|
+
api.clearSourceFileCache();
|
|
607
|
+
const snapshot = await api.updateSnapshot(request);
|
|
608
|
+
try {
|
|
609
|
+
return await snapshot.getProjects()[0].program.getSourceFile(FILE);
|
|
610
|
+
} finally {
|
|
611
|
+
await snapshot.dispose();
|
|
612
|
+
}
|
|
613
|
+
},
|
|
614
|
+
close: () => api.close()
|
|
615
|
+
};
|
|
616
|
+
}
|
|
617
|
+
async function closeJsxCompiler() {
|
|
618
|
+
const current = parser;
|
|
619
|
+
parser = undefined;
|
|
620
|
+
if (current)
|
|
621
|
+
await (await current).close();
|
|
622
|
+
}
|
|
623
|
+
function constant(node, ast, ts) {
|
|
624
|
+
const isConstant = (child) => constant(child, ast, ts);
|
|
625
|
+
if (ts.isParenthesizedExpression(node))
|
|
626
|
+
return isConstant(node.expression);
|
|
627
|
+
if (ts.isNumericLiteral(node) || ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node))
|
|
628
|
+
return true;
|
|
629
|
+
if (node.kind === ast.SyntaxKind.TrueKeyword || node.kind === ast.SyntaxKind.FalseKeyword || node.kind === ast.SyntaxKind.NullKeyword)
|
|
630
|
+
return true;
|
|
631
|
+
if (ts.isIdentifier(node) && node.text === "undefined")
|
|
632
|
+
return true;
|
|
633
|
+
if (ts.isPrefixUnaryExpression(node) && (node.operator === ast.SyntaxKind.MinusToken || node.operator === ast.SyntaxKind.PlusToken))
|
|
634
|
+
return ts.isNumericLiteral(node.operand);
|
|
635
|
+
if (ts.isArrayLiteralExpression(node))
|
|
636
|
+
return node.elements.every((element) => !ts.isSpreadElement(element) && isConstant(element));
|
|
637
|
+
if (ts.isObjectLiteralExpression(node)) {
|
|
638
|
+
return node.properties.every((property) => ts.isPropertyAssignment(property) && (ts.isIdentifier(property.name) || ts.isStringLiteral(property.name) || ts.isNumericLiteral(property.name)) && isConstant(property.initializer));
|
|
639
|
+
}
|
|
640
|
+
return false;
|
|
641
|
+
}
|
|
642
|
+
async function compileJsx(source) {
|
|
643
|
+
if (!/<[A-Za-z>]/.test(source))
|
|
644
|
+
return { code: source, templates: 0 };
|
|
645
|
+
const loaded = await (parser ??= loadParser());
|
|
646
|
+
const turn = queue.then(async () => {
|
|
647
|
+
loaded.files.set(FILE, source);
|
|
648
|
+
const file = await loaded.parse();
|
|
649
|
+
if (!file || file.text !== source)
|
|
650
|
+
throw new Error("compileJsx: TypeScript returned a different parse of the file");
|
|
651
|
+
return compileFile(source, file, loaded.ast, loaded.is);
|
|
652
|
+
});
|
|
653
|
+
queue = turn.catch(() => {
|
|
654
|
+
return;
|
|
655
|
+
});
|
|
656
|
+
return turn;
|
|
657
|
+
}
|
|
658
|
+
function compileFile(source, file, ast, ts) {
|
|
659
|
+
const isConstant = (node) => constant(node, ast, ts);
|
|
660
|
+
const isJsx = (node) => ts.isJsxElement(node) || ts.isJsxSelfClosingElement(node) || ts.isJsxFragment(node);
|
|
661
|
+
const core = new Set;
|
|
662
|
+
let lastImport = 0;
|
|
663
|
+
for (const statement of file.statements) {
|
|
664
|
+
if (!ts.isImportDeclaration(statement))
|
|
665
|
+
continue;
|
|
666
|
+
lastImport = statement.end;
|
|
667
|
+
if (!ts.isStringLiteral(statement.moduleSpecifier) || !CORE.test(statement.moduleSpecifier.text))
|
|
668
|
+
continue;
|
|
669
|
+
const bindings = statement.importClause?.namedBindings;
|
|
670
|
+
if (bindings && ts.isNamedImports(bindings) && statement.importClause.phaseModifier !== ast.SyntaxKind.TypeKeyword) {
|
|
671
|
+
for (const element of bindings.elements)
|
|
672
|
+
if (!element.isTypeOnly)
|
|
673
|
+
core.add(element.name.text);
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
const shadowed = new Set;
|
|
677
|
+
const scan = (node) => {
|
|
678
|
+
const name = node.kind === ast.SyntaxKind.Parameter || ts.isVariableDeclaration(node) || ts.isFunctionDeclaration(node) || ts.isClassDeclaration(node) ? node.name : undefined;
|
|
679
|
+
if (name && ts.isIdentifier(name) && core.has(name.text))
|
|
680
|
+
shadowed.add(name.text);
|
|
681
|
+
node.forEachChild(scan);
|
|
682
|
+
};
|
|
683
|
+
file.forEachChild(scan);
|
|
684
|
+
for (const name of shadowed)
|
|
685
|
+
core.delete(name);
|
|
686
|
+
const pure = (node) => {
|
|
687
|
+
if (ts.isJsxFragment(node))
|
|
688
|
+
return true;
|
|
689
|
+
const tag = ts.isJsxElement(node) ? node.openingElement.tagName : node.tagName;
|
|
690
|
+
if (ts.isIdentifier(tag))
|
|
691
|
+
return /^[a-z]/.test(tag.text) || core.has(tag.text);
|
|
692
|
+
return false;
|
|
693
|
+
};
|
|
694
|
+
const text = (start, end) => source.slice(start, end);
|
|
695
|
+
const declarations = [];
|
|
696
|
+
function rewrite(start, end, root) {
|
|
697
|
+
const roots = [];
|
|
698
|
+
const find = (node) => {
|
|
699
|
+
if (node.end <= start || node.pos >= end)
|
|
700
|
+
return;
|
|
701
|
+
if (isJsx(node) && node.getStart() >= start && node.end <= end) {
|
|
702
|
+
roots.push(node);
|
|
703
|
+
return;
|
|
704
|
+
}
|
|
705
|
+
node.forEachChild(find);
|
|
706
|
+
};
|
|
707
|
+
find(root);
|
|
708
|
+
let out = "";
|
|
709
|
+
let at = start;
|
|
710
|
+
for (const node of roots) {
|
|
711
|
+
out += text(at, node.getStart()) + compileRoot(node);
|
|
712
|
+
at = node.end;
|
|
713
|
+
}
|
|
714
|
+
return out + text(at, end);
|
|
715
|
+
}
|
|
716
|
+
function compileRoot(node) {
|
|
717
|
+
if (!pure(node))
|
|
718
|
+
return impure(node);
|
|
719
|
+
const holes = [];
|
|
720
|
+
const events = [];
|
|
721
|
+
let key;
|
|
722
|
+
const hole = (expression, event = false) => {
|
|
723
|
+
const name = "h" + holes.length;
|
|
724
|
+
holes.push(expression);
|
|
725
|
+
if (event)
|
|
726
|
+
events.push(name);
|
|
727
|
+
return "p." + name;
|
|
728
|
+
};
|
|
729
|
+
let elements = 0;
|
|
730
|
+
function body(element, isRoot) {
|
|
731
|
+
if (!pure(element))
|
|
732
|
+
return "{" + hole(impure(element)) + "}";
|
|
733
|
+
elements++;
|
|
734
|
+
let out = "";
|
|
735
|
+
let at = element.getStart();
|
|
736
|
+
const end = element.end;
|
|
737
|
+
const attributes = ts.isJsxElement(element) ? element.openingElement.attributes : ts.isJsxSelfClosingElement(element) ? element.attributes : undefined;
|
|
738
|
+
const children = ts.isJsxElement(element) || ts.isJsxFragment(element) ? element.children : undefined;
|
|
739
|
+
const parts = [];
|
|
740
|
+
for (const attribute of attributes?.properties ?? []) {
|
|
741
|
+
if (ts.isJsxSpreadAttribute(attribute)) {
|
|
742
|
+
parts.push({ start: attribute.expression.getStart(), end: attribute.expression.end, replacement: hole(rewrite(attribute.expression.getStart(), attribute.expression.end, attribute.expression)) });
|
|
743
|
+
continue;
|
|
744
|
+
}
|
|
745
|
+
const name = attribute.name.getText();
|
|
746
|
+
const initializer = attribute.initializer;
|
|
747
|
+
if (isRoot && name === "key") {
|
|
748
|
+
key = initializer ? ts.isJsxExpression(initializer) && initializer.expression ? rewrite(initializer.expression.getStart(), initializer.expression.end, initializer.expression) : initializer.getText() : "true";
|
|
749
|
+
parts.push({ start: attribute.getStart(), end: attribute.end, replacement: "" });
|
|
750
|
+
continue;
|
|
751
|
+
}
|
|
752
|
+
if (!initializer)
|
|
753
|
+
continue;
|
|
754
|
+
if (ts.isJsxExpression(initializer) && initializer.expression && !isConstant(initializer.expression)) {
|
|
755
|
+
const expression = initializer.expression;
|
|
756
|
+
parts.push({ start: expression.getStart(), end: expression.end, replacement: hole(rewrite(expression.getStart(), expression.end, expression), EVENT.test(name)) });
|
|
757
|
+
} else if (isJsx(initializer)) {
|
|
758
|
+
const value = initializer;
|
|
759
|
+
parts.push({ start: value.getStart(), end: value.end, replacement: "{" + hole(compileRoot(value)) + "}" });
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
for (const child of children ?? []) {
|
|
763
|
+
if (ts.isJsxExpression(child)) {
|
|
764
|
+
if (!child.expression)
|
|
765
|
+
continue;
|
|
766
|
+
if (isConstant(child.expression) && !ts.isObjectLiteralExpression(child.expression) && !ts.isArrayLiteralExpression(child.expression))
|
|
767
|
+
continue;
|
|
768
|
+
parts.push({ start: child.expression.getStart(), end: child.expression.end, replacement: hole(rewrite(child.expression.getStart(), child.expression.end, child.expression)) });
|
|
769
|
+
} else if (isJsx(child)) {
|
|
770
|
+
parts.push({ start: child.getStart(), end: child.end, replacement: body(child, false) });
|
|
771
|
+
}
|
|
772
|
+
}
|
|
773
|
+
parts.sort((a, b) => a.start - b.start);
|
|
774
|
+
for (const part of parts) {
|
|
775
|
+
out += text(at, part.start) + part.replacement;
|
|
776
|
+
at = part.end;
|
|
777
|
+
}
|
|
778
|
+
return out + text(at, end);
|
|
779
|
+
}
|
|
780
|
+
const template = body(node, true);
|
|
781
|
+
if (elements < 2)
|
|
782
|
+
return impure(node);
|
|
783
|
+
const name = "__tarveTemplate" + declarations.length;
|
|
784
|
+
declarations.push(`const ${name} = __tarveCompileTemplate((p: any) => (${template}), ${JSON.stringify(events)});`);
|
|
785
|
+
const props = holes.map((expression, index) => `h${index}={${expression}}`);
|
|
786
|
+
if (key !== undefined)
|
|
787
|
+
props.unshift(`key={${key}}`);
|
|
788
|
+
return `<${name} ${props.join(" ")} />`;
|
|
789
|
+
}
|
|
790
|
+
function impure(node) {
|
|
791
|
+
let out = "";
|
|
792
|
+
let at = node.getStart();
|
|
793
|
+
const visit = (child) => {
|
|
794
|
+
if (isJsx(child) && child !== node) {
|
|
795
|
+
out += text(at, child.getStart()) + compileRoot(child);
|
|
796
|
+
at = child.end;
|
|
797
|
+
return;
|
|
798
|
+
}
|
|
799
|
+
if (ts.isJsxExpression(child) && child.expression) {
|
|
800
|
+
out += text(at, child.expression.getStart()) + rewrite(child.expression.getStart(), child.expression.end, child.expression);
|
|
801
|
+
at = child.expression.end;
|
|
802
|
+
return;
|
|
803
|
+
}
|
|
804
|
+
if (ts.isJsxSpreadAttribute(child)) {
|
|
805
|
+
out += text(at, child.expression.getStart()) + rewrite(child.expression.getStart(), child.expression.end, child.expression);
|
|
806
|
+
at = child.expression.end;
|
|
807
|
+
return;
|
|
808
|
+
}
|
|
809
|
+
child.forEachChild(visit);
|
|
810
|
+
};
|
|
811
|
+
node.forEachChild(visit);
|
|
812
|
+
return out + text(at, node.end);
|
|
813
|
+
}
|
|
814
|
+
const body = rewrite(lastImport, source.length, file);
|
|
815
|
+
if (!declarations.length)
|
|
816
|
+
return { code: source, templates: 0 };
|
|
817
|
+
const code = text(0, lastImport) + `
|
|
818
|
+
import { template as __tarveCompileTemplate } from "@tarve/core";
|
|
819
|
+
` + declarations.join(`
|
|
820
|
+
`) + `
|
|
821
|
+
` + body;
|
|
822
|
+
return { code, templates: declarations.length };
|
|
823
|
+
}
|
|
824
|
+
var ROOT = "/tarve-jsx", FILE, CONFIG, parser, queue, CORE, EVENT;
|
|
825
|
+
var init_jsx_compile = __esm(() => {
|
|
826
|
+
FILE = ROOT + "/app.tsx";
|
|
827
|
+
CONFIG = ROOT + "/tsconfig.json";
|
|
828
|
+
queue = Promise.resolve();
|
|
829
|
+
CORE = /^(?:@tarve\/core|tarve)(?:\/.*)?$/;
|
|
830
|
+
EVENT = /^on[A-Z]/;
|
|
831
|
+
});
|
|
832
|
+
|
|
833
|
+
// packages/core/windows-executable.ts
|
|
834
|
+
var exports_windows_executable = {};
|
|
835
|
+
__export(exports_windows_executable, {
|
|
836
|
+
brandWindowsExecutable: () => brandWindowsExecutable
|
|
837
|
+
});
|
|
838
|
+
import { readFileSync as readFileSync2, writeFileSync as writeFileSync2 } from "node:fs";
|
|
839
|
+
import { Data, NtExecutable, NtExecutableResource, Resource } from "resedit";
|
|
840
|
+
function numericVersion(version) {
|
|
841
|
+
const parts = version.split(/[.+-]/).slice(0, 4).map((part) => Number.parseInt(part, 10));
|
|
842
|
+
return [0, 1, 2, 3].map((index) => {
|
|
843
|
+
const value = parts[index];
|
|
844
|
+
return Number.isInteger(value) && value >= 0 && value <= 65535 ? value : 0;
|
|
845
|
+
});
|
|
846
|
+
}
|
|
847
|
+
function brandWindowsExecutable(path, info) {
|
|
848
|
+
const executable = NtExecutable.from(readFileSync2(path), { ignoreCert: true });
|
|
849
|
+
const resources = NtExecutableResource.from(executable);
|
|
850
|
+
const versionInfo = Resource.VersionInfo.fromEntries(resources.entries)[0] ?? Resource.VersionInfo.createEmpty();
|
|
851
|
+
const languages = versionInfo.getAllLanguagesForStringValues();
|
|
852
|
+
if (languages.length === 0)
|
|
853
|
+
languages.push({ lang: 1033, codepage: 1200 });
|
|
854
|
+
const [major, minor, patch, revision] = numericVersion(info.version);
|
|
855
|
+
const text = `${major}.${minor}.${patch}.${revision}`;
|
|
856
|
+
for (const language of languages) {
|
|
857
|
+
for (const key of ["CompanyName", "LegalCopyright", "LegalTrademarks", "Comments", "PrivateBuild", "SpecialBuild"]) {
|
|
858
|
+
versionInfo.removeStringValue(language, key);
|
|
859
|
+
}
|
|
860
|
+
versionInfo.setStringValues(language, {
|
|
861
|
+
FileDescription: info.description ?? `${info.name} — native Tarve application`,
|
|
862
|
+
...info.publisher ? { CompanyName: info.publisher } : {},
|
|
863
|
+
...info.copyright ? { LegalCopyright: info.copyright } : {},
|
|
864
|
+
ProductName: info.name,
|
|
865
|
+
InternalName: info.name,
|
|
866
|
+
OriginalFilename: info.filename,
|
|
867
|
+
FileVersion: text,
|
|
868
|
+
ProductVersion: text
|
|
869
|
+
});
|
|
870
|
+
}
|
|
871
|
+
versionInfo.setFileVersion(major, minor, patch, revision);
|
|
872
|
+
versionInfo.setProductVersion(major, minor, patch, revision);
|
|
873
|
+
versionInfo.outputToResourceEntries(resources.entries);
|
|
874
|
+
if (info.ico) {
|
|
875
|
+
const groups = Resource.IconGroupEntry.fromEntries(resources.entries);
|
|
876
|
+
const group = groups[0];
|
|
877
|
+
const icons = Data.IconFile.from(info.ico).icons.map((icon) => icon.data);
|
|
878
|
+
Resource.IconGroupEntry.replaceIconsForResource(resources.entries, group?.id ?? 1, group?.lang ?? 1033, icons);
|
|
879
|
+
}
|
|
880
|
+
resources.outputResource(executable);
|
|
881
|
+
const output = Buffer.from(executable.generate());
|
|
882
|
+
output.writeUInt16LE(IMAGE_SUBSYSTEM_WINDOWS_GUI, output.readUInt32LE(60) + 24 + 68);
|
|
883
|
+
writeFileSync2(path, output);
|
|
884
|
+
}
|
|
885
|
+
var IMAGE_SUBSYSTEM_WINDOWS_GUI = 2;
|
|
886
|
+
var init_windows_executable = () => {};
|
|
887
|
+
|
|
888
|
+
// packages/core/txiki-build.ts
|
|
889
|
+
import { execFileSync } from "node:child_process";
|
|
890
|
+
import { createHash as createHash2 } from "node:crypto";
|
|
891
|
+
import { chmodSync, existsSync as existsSync3, mkdtempSync, readFileSync as readFileSync3, writeFileSync as writeFileSync3 } from "node:fs";
|
|
892
|
+
import { mkdir, rm } from "node:fs/promises";
|
|
893
|
+
import { builtinModules } from "node:module";
|
|
894
|
+
import { tmpdir } from "node:os";
|
|
895
|
+
import { basename as basename3, dirname as dirname3, join as join3, relative as relative2, resolve as resolve2 } from "node:path";
|
|
896
|
+
import { gzipSync } from "node:zlib";
|
|
897
|
+
import { nativePath } from "#tarve/runtime";
|
|
898
|
+
function findTxiki(explicit) {
|
|
899
|
+
const candidate = explicit ?? process.env.TARVE_TXIKI ?? (process.versions.bun ? Bun.which("tjs") : undefined) ?? "tjs";
|
|
900
|
+
try {
|
|
901
|
+
execFileSync(candidate, ["--version"], { stdio: "pipe" });
|
|
902
|
+
} catch {
|
|
903
|
+
throw new Error(`txiki.js was not found (${candidate}). Install it (winget install Saghul.TxikiJS) or set TARVE_TXIKI.`);
|
|
904
|
+
}
|
|
905
|
+
return candidate;
|
|
906
|
+
}
|
|
907
|
+
function txikiBinary(tjs) {
|
|
908
|
+
return execFileSync(tjs, ["eval", "console.log(tjs.exePath)"], { encoding: "utf8" }).trim();
|
|
909
|
+
}
|
|
910
|
+
async function buildTxiki(options) {
|
|
911
|
+
const target = options.target ?? hostBuildTarget();
|
|
912
|
+
if (!target)
|
|
913
|
+
throw new Error(`Unsupported txiki.js build host: ${process.platform}-${process.arch}`);
|
|
914
|
+
const config = targetConfig(target);
|
|
915
|
+
const tjs = findTxiki(options.txikiExecutable);
|
|
916
|
+
const host = txikiBinary(tjs);
|
|
917
|
+
const base = options.targetExecutable ? resolve2(options.targetExecutable) : host;
|
|
918
|
+
if (target !== hostBuildTarget() && !options.targetExecutable) {
|
|
919
|
+
throw new Error(`Cross-compiling for ${target} needs targetExecutable: the txiki.js ${execFileSync(tjs, ["--version"], { encoding: "utf8" }).trim()} binary for that platform.`);
|
|
920
|
+
}
|
|
921
|
+
if (!existsSync3(base))
|
|
922
|
+
throw new Error(`txiki.js executable is missing: ${base}`);
|
|
923
|
+
const { metadata, images, define } = buildMetadata(options.entrypoint, metadataOverrides(options));
|
|
924
|
+
const outfile = resolve2(options.outfile ?? `dist/${metadata.name}${config.executableSuffix}`);
|
|
925
|
+
const library = resolve2(options.nativeLibrary ?? (target === hostBuildTarget() ? nativePath() : join3(import.meta.dirname, "../../native", config.nativeDirectory, config.nativeName)));
|
|
926
|
+
if (!existsSync3(library))
|
|
927
|
+
throw new Error(`Tarve native library missing: ${library}`);
|
|
928
|
+
const temporary = mkdtempSync(join3(tmpdir(), "tarve-txiki-build-"));
|
|
929
|
+
try {
|
|
930
|
+
const root = import.meta.dirname;
|
|
931
|
+
const source = existsSync3(join3(root, "src/index.ts"));
|
|
932
|
+
const assets = [];
|
|
933
|
+
const missing = new Map;
|
|
934
|
+
const importers = new Map;
|
|
935
|
+
const runtime = {
|
|
936
|
+
name: "tarve-txiki-runtime",
|
|
937
|
+
setup(build) {
|
|
938
|
+
build.onResolve({ filter: /^tjs:/ }, (args) => ({ path: args.path, external: true }));
|
|
939
|
+
build.onResolve({ filter: /^(?:node:)?[a-z_]+(?:\/[a-z_]+)?$/ }, (args) => {
|
|
940
|
+
const name = args.path.replace(/^node:/, "");
|
|
941
|
+
if (!NODE_SHIMS[name] && !args.path.startsWith("node:") && !builtinModules.includes(name))
|
|
942
|
+
return;
|
|
943
|
+
if (!importers.has(name))
|
|
944
|
+
importers.set(name, new Set);
|
|
945
|
+
importers.get(name).add(args.importer);
|
|
946
|
+
return { path: name, namespace: "tarve-node", sideEffects: false };
|
|
947
|
+
});
|
|
948
|
+
build.onResolve({ filter: /^#tarve\/runtime$/ }, () => ({ path: join3(root, "txiki/runtime.ts") }));
|
|
949
|
+
if (source) {
|
|
950
|
+
build.onResolve({ filter: /^(?:@tarve\/core(?:-internal)?|tarve)(?:\/(?:jsx-runtime|jsx-dev-runtime))?$/ }, (args) => {
|
|
951
|
+
const entry = args.path.replace(/^(?:@tarve\/core(?:-internal)?|tarve)/, "") || "/index";
|
|
952
|
+
return { path: join3(root, "src", `${entry.slice(1)}.ts`) };
|
|
953
|
+
});
|
|
954
|
+
}
|
|
955
|
+
build.onResolve({ filter: /.*/ }, (args) => {
|
|
956
|
+
const type = args.with?.type ?? (ASSET.test(args.path) && args.kind === "import-statement" ? "file" : undefined);
|
|
957
|
+
if (type !== "file" && type !== "text")
|
|
958
|
+
return;
|
|
959
|
+
return { path: resolve2(dirname3(args.importer), args.path), namespace: `tarve-${type}` };
|
|
960
|
+
});
|
|
961
|
+
build.onLoad({ filter: /.*/, namespace: "tarve-node" }, (args) => {
|
|
962
|
+
const shim = NODE_SHIMS[args.path] && JSON.stringify(join3(root, "txiki/shims", `${NODE_SHIMS[args.path]}.ts`));
|
|
963
|
+
const placeholder = (api) => `/* @__PURE__ */ unavailable(${JSON.stringify(`${UNAVAILABLE}node:${api}`)})`;
|
|
964
|
+
return {
|
|
965
|
+
contents: [
|
|
966
|
+
`import { unavailable } from ${JSON.stringify(join3(root, "txiki/unavailable.ts"))};`,
|
|
967
|
+
...shim ? [`export * from ${shim};`, `export { default } from ${shim};`] : [`export default ${placeholder(args.path)};`],
|
|
968
|
+
...[...missing.get(args.path) ?? []].map((name) => `export const ${name} = ${placeholder(`${args.path}.${name}`)};`)
|
|
969
|
+
].join(`
|
|
970
|
+
`),
|
|
971
|
+
loader: "js",
|
|
972
|
+
resolveDir: root
|
|
973
|
+
};
|
|
974
|
+
});
|
|
975
|
+
if (options.compileJsx) {
|
|
976
|
+
build.onLoad({ filter: /\.[jt]sx$/ }, async (args) => {
|
|
977
|
+
if (/[\\/]node_modules[\\/]/.test(args.path) || args.path.startsWith(join3(root, "src")))
|
|
978
|
+
return;
|
|
979
|
+
await Promise.resolve().then(() => init_jsx_compile());
|
|
980
|
+
return { contents: (await compileJsx(readFileSync3(args.path, "utf8"))).code, loader: args.path.endsWith(".jsx") ? "jsx" : "tsx", resolveDir: dirname3(args.path) };
|
|
981
|
+
});
|
|
982
|
+
}
|
|
983
|
+
build.onLoad({ filter: /.*/, namespace: "tarve-text" }, (args) => ({ contents: readFileSync3(args.path, "utf8"), loader: "text" }));
|
|
984
|
+
build.onLoad({ filter: /.*/, namespace: "tarve-file" }, (args) => {
|
|
985
|
+
const data = readFileSync3(args.path);
|
|
986
|
+
const name = `${createHash2("sha256").update(data).digest("hex").slice(0, 12)}-${basename3(args.path)}`;
|
|
987
|
+
if (!assets.some((asset) => asset.name === name))
|
|
988
|
+
assets.push({ name, data, gzip: false });
|
|
989
|
+
return { contents: `export default globalThis.__tarveAssets[${JSON.stringify(name)}];`, loader: "js" };
|
|
990
|
+
});
|
|
991
|
+
}
|
|
992
|
+
};
|
|
993
|
+
const entry = join3(temporary, "entry.ts");
|
|
994
|
+
writeFileSync3(entry, [
|
|
995
|
+
`import ${JSON.stringify(join3(root, "txiki/prelude.ts"))};`,
|
|
996
|
+
`import ${JSON.stringify(resolve2(options.entrypoint))};`
|
|
997
|
+
].join(`
|
|
998
|
+
`));
|
|
999
|
+
const bundle = join3(temporary, "app.js");
|
|
1000
|
+
const { build: esbuild } = await import("esbuild");
|
|
1001
|
+
const bundleApp = () => esbuild({
|
|
1002
|
+
entryPoints: [entry],
|
|
1003
|
+
outfile: bundle,
|
|
1004
|
+
bundle: true,
|
|
1005
|
+
platform: "neutral",
|
|
1006
|
+
mainFields: ["module", "main"],
|
|
1007
|
+
format: "esm",
|
|
1008
|
+
target: "es2023",
|
|
1009
|
+
minify: !process.env.TARVE_TXIKI_DEBUG,
|
|
1010
|
+
jsx: "automatic",
|
|
1011
|
+
jsxImportSource: "@tarve/core",
|
|
1012
|
+
logLevel: "silent",
|
|
1013
|
+
define: {
|
|
1014
|
+
...define,
|
|
1015
|
+
"process.env.NODE_ENV": JSON.stringify("production"),
|
|
1016
|
+
"import.meta.dirname": "globalThis.__tarveDirname",
|
|
1017
|
+
TARVE_TXIKI_PLATFORM: JSON.stringify(config.platform),
|
|
1018
|
+
TARVE_TXIKI_NATIVE_NAME: JSON.stringify(config.nativeName)
|
|
1019
|
+
},
|
|
1020
|
+
plugins: [runtime],
|
|
1021
|
+
metafile: !!process.env.TARVE_TXIKI_DEBUG
|
|
1022
|
+
});
|
|
1023
|
+
for (let attempt = 0;; attempt++) {
|
|
1024
|
+
try {
|
|
1025
|
+
const result = await bundleApp();
|
|
1026
|
+
if (process.env.TARVE_TXIKI_DEBUG)
|
|
1027
|
+
writeFileSync3(`${process.env.TARVE_TXIKI_DEBUG}.meta.json`, JSON.stringify(result.metafile));
|
|
1028
|
+
break;
|
|
1029
|
+
} catch (error) {
|
|
1030
|
+
const errors = error.errors ?? [];
|
|
1031
|
+
const lacking = errors.map(({ text }) => /^No matching export in "tarve-node:([^"]+)" for import "([^"]+)"$/.exec(text));
|
|
1032
|
+
if (!lacking.length || lacking.some((match) => !match) || attempt > 3)
|
|
1033
|
+
throw error;
|
|
1034
|
+
for (const match of lacking) {
|
|
1035
|
+
if (!missing.has(match[1]))
|
|
1036
|
+
missing.set(match[1], new Set);
|
|
1037
|
+
missing.get(match[1]).add(match[2]);
|
|
1038
|
+
}
|
|
1039
|
+
}
|
|
1040
|
+
}
|
|
1041
|
+
assertAvailable(readFileSync3(bundle, "utf8"), importers);
|
|
1042
|
+
if (process.env.TARVE_TXIKI_DEBUG)
|
|
1043
|
+
writeFileSync3(process.env.TARVE_TXIKI_DEBUG, readFileSync3(bundle));
|
|
1044
|
+
const compiled = join3(temporary, `compiled${config.executableSuffix}`);
|
|
1045
|
+
execFileSync(tjs, ["compile", bundle, compiled], { stdio: "pipe" });
|
|
1046
|
+
const payload = txikiPayload(readFileSync3(compiled), readFileSync3(host).byteLength);
|
|
1047
|
+
const executable = join3(temporary, `base${config.executableSuffix}`);
|
|
1048
|
+
writeFileSync3(executable, readFileSync3(base));
|
|
1049
|
+
if (config.platform === "win32") {
|
|
1050
|
+
const tarveVersion = JSON.parse(readFileSync3(resolve2(root, "../../package.json"), "utf8")).version;
|
|
1051
|
+
(await Promise.resolve().then(() => (init_windows_executable(), exports_windows_executable))).brandWindowsExecutable(executable, {
|
|
1052
|
+
name: metadata.name,
|
|
1053
|
+
version: metadata.version ?? tarveVersion,
|
|
1054
|
+
filename: basename3(outfile),
|
|
1055
|
+
description: metadata.description,
|
|
1056
|
+
publisher: metadata.publisher,
|
|
1057
|
+
copyright: metadata.copyright,
|
|
1058
|
+
ico: images?.ico
|
|
1059
|
+
});
|
|
1060
|
+
}
|
|
1061
|
+
const files = [{ name: config.nativeName, data: readFileSync3(library), gzip: true }, ...assets];
|
|
1062
|
+
await mkdir(dirname3(outfile), { recursive: true });
|
|
1063
|
+
await rm(outfile, { force: true });
|
|
1064
|
+
writeFileSync3(outfile, fuse(readFileSync3(executable), files, payload));
|
|
1065
|
+
if (config.platform !== "win32")
|
|
1066
|
+
chmodSync(outfile, 493);
|
|
1067
|
+
if (config.platform === "linux" && (metadata.icon || metadata.appId))
|
|
1068
|
+
writeLinuxDesktopEntry(outfile, metadata, images?.png);
|
|
1069
|
+
return outfile;
|
|
1070
|
+
} finally {
|
|
1071
|
+
if (options.compileJsx)
|
|
1072
|
+
await (await Promise.resolve().then(() => (init_jsx_compile(), exports_jsx_compile))).closeJsxCompiler();
|
|
1073
|
+
await rm(temporary, { recursive: true, force: true });
|
|
1074
|
+
}
|
|
1075
|
+
}
|
|
1076
|
+
function assertAvailable(bundle, importers = new Map) {
|
|
1077
|
+
const apis = [...new Set([...bundle.matchAll(new RegExp(`${UNAVAILABLE}node:([\\w/.-]+)`, "g"))].map((match) => match[1]))].sort();
|
|
1078
|
+
const bun = [...new Set([...bundle.matchAll(/(?<![\w$.])Bun\.([A-Za-z_$][\w$]*)/g)].filter((match) => !bundle.slice(Math.max(0, match.index - 400), match.index).includes("typeof Bun")).map((match) => `Bun.${match[1]}`))].sort();
|
|
1079
|
+
if (!apis.length && !bun.length)
|
|
1080
|
+
return;
|
|
1081
|
+
const lines = apis.map((api) => {
|
|
1082
|
+
const [module, name] = api.split(".");
|
|
1083
|
+
const users = [...importers.get(module) ?? []].filter((file) => !name || !existsSync3(file) || new RegExp(`\\b${name}\\b`).test(readFileSync3(file, "utf8"))).map((file) => relative2(process.cwd(), file).replaceAll("\\", "/"));
|
|
1084
|
+
return ` node:${api}${users.length ? ` (imported by ${users.slice(0, 3).join(", ")}${users.length > 3 ? ", …" : ""})` : ""}`;
|
|
1085
|
+
});
|
|
1086
|
+
throw new Error([
|
|
1087
|
+
"This app reaches APIs the txiki.js runtime does not provide:",
|
|
1088
|
+
...lines,
|
|
1089
|
+
...bun.map((api) => ` ${api} (the Bun global; guard it with typeof Bun !== "undefined")`),
|
|
1090
|
+
"Remove those calls from code the app can run, or build with --runtime bun or --runtime node."
|
|
1091
|
+
].join(`
|
|
1092
|
+
`));
|
|
1093
|
+
}
|
|
1094
|
+
function txikiPayload(compiled, hostSize) {
|
|
1095
|
+
const trailer = compiled.subarray(compiled.length - 12);
|
|
1096
|
+
const offset = new DataView(trailer.buffer, trailer.byteOffset, 12).getUint32(8, true);
|
|
1097
|
+
if (new TextDecoder().decode(trailer.subarray(0, 8)) !== TXIKI_MAGIC || offset !== hostSize) {
|
|
1098
|
+
throw new Error("Unrecognized `tjs compile` output; this txiki.js version is not supported.");
|
|
1099
|
+
}
|
|
1100
|
+
return compiled.subarray(offset, compiled.length - 12);
|
|
1101
|
+
}
|
|
1102
|
+
function fuse(executable, files, payload) {
|
|
1103
|
+
const parts = [executable];
|
|
1104
|
+
const index = [];
|
|
1105
|
+
let offset = executable.length;
|
|
1106
|
+
for (const file of files) {
|
|
1107
|
+
const stored = file.gzip ? gzipSync(file.data, { level: 9 }) : file.data;
|
|
1108
|
+
index.push({ name: file.name, offset, length: stored.length, size: file.data.length, sha256: createHash2("sha256").update(file.data).digest("hex"), gzip: file.gzip });
|
|
1109
|
+
parts.push(stored);
|
|
1110
|
+
offset += stored.length;
|
|
1111
|
+
}
|
|
1112
|
+
const json = new TextEncoder().encode(JSON.stringify(index));
|
|
1113
|
+
const header = new Uint8Array(16);
|
|
1114
|
+
new DataView(header.buffer).setUint32(0, json.length, true);
|
|
1115
|
+
header.set(new TextEncoder().encode(BLOB_MAGIC), 8);
|
|
1116
|
+
parts.push(json, header);
|
|
1117
|
+
const payloadOffset = offset + json.length + header.length;
|
|
1118
|
+
const trailer = new Uint8Array(12);
|
|
1119
|
+
trailer.set(new TextEncoder().encode(TXIKI_MAGIC));
|
|
1120
|
+
new DataView(trailer.buffer).setUint32(8, payloadOffset, true);
|
|
1121
|
+
parts.push(payload, trailer);
|
|
1122
|
+
const out = new Uint8Array(parts.reduce((size, part) => size + part.length, 0));
|
|
1123
|
+
let position = 0;
|
|
1124
|
+
for (const part of parts) {
|
|
1125
|
+
out.set(part, position);
|
|
1126
|
+
position += part.length;
|
|
1127
|
+
}
|
|
1128
|
+
return out;
|
|
1129
|
+
}
|
|
1130
|
+
var TXIKI_MAGIC = "tx1k1.js", BLOB_MAGIC = "TARVEBLB", NODE_SHIMS, ASSET, UNAVAILABLE = "tarve-txiki-unavailable:";
|
|
1131
|
+
var init_txiki_build = __esm(() => {
|
|
1132
|
+
init_build();
|
|
1133
|
+
init_package_metadata();
|
|
1134
|
+
init_targets();
|
|
1135
|
+
NODE_SHIMS = {
|
|
1136
|
+
fs: "fs",
|
|
1137
|
+
"fs/promises": "fs-promises",
|
|
1138
|
+
path: "path",
|
|
1139
|
+
os: "os",
|
|
1140
|
+
crypto: "crypto",
|
|
1141
|
+
net: "net",
|
|
1142
|
+
module: "module",
|
|
1143
|
+
zlib: "zlib",
|
|
1144
|
+
events: "events",
|
|
1145
|
+
child_process: "child-process",
|
|
1146
|
+
"timers/promises": "timers-promises"
|
|
1147
|
+
};
|
|
1148
|
+
ASSET = /\.(?:png|jpe?g|webp|gif|svg|ico|ttf|otf|woff2?)$/i;
|
|
1149
|
+
});
|
|
1150
|
+
|
|
549
1151
|
// packages/core/esbuild-file-imports.ts
|
|
550
|
-
import { readFileSync as
|
|
551
|
-
import { dirname as
|
|
552
|
-
var
|
|
1152
|
+
import { readFileSync as readFileSync4 } from "node:fs";
|
|
1153
|
+
import { dirname as dirname4, resolve as resolve3 } from "node:path";
|
|
1154
|
+
var ASSET2, fileImportAttributes;
|
|
553
1155
|
var init_esbuild_file_imports = __esm(() => {
|
|
554
|
-
|
|
1156
|
+
ASSET2 = /\.(?:png|jpe?g|webp|gif|svg)$/i;
|
|
555
1157
|
fileImportAttributes = {
|
|
556
1158
|
name: "tarve-file-import-attributes",
|
|
557
1159
|
setup(build) {
|
|
@@ -559,12 +1161,12 @@ var init_esbuild_file_imports = __esm(() => {
|
|
|
559
1161
|
if (args.namespace === "tarve-file") {
|
|
560
1162
|
return args.path.startsWith("node:") ? { path: args.path, external: true } : { path: args.path, namespace: "tarve-file-copy" };
|
|
561
1163
|
}
|
|
562
|
-
const type = args.with?.type ?? (
|
|
1164
|
+
const type = args.with?.type ?? (ASSET2.test(args.path) && args.kind === "import-statement" ? "file" : undefined);
|
|
563
1165
|
if (type !== "file" && type !== "text")
|
|
564
1166
|
return;
|
|
565
|
-
return { path:
|
|
1167
|
+
return { path: resolve3(dirname4(args.importer), args.path), namespace: `tarve-${type}` };
|
|
566
1168
|
});
|
|
567
|
-
build.onLoad({ filter: /.*/, namespace: "tarve-text" }, (args) => ({ contents:
|
|
1169
|
+
build.onLoad({ filter: /.*/, namespace: "tarve-text" }, (args) => ({ contents: readFileSync4(args.path, "utf8"), loader: "text" }));
|
|
568
1170
|
build.onLoad({ filter: /.*/, namespace: "tarve-file" }, (args) => ({
|
|
569
1171
|
contents: [
|
|
570
1172
|
`import relative from ${JSON.stringify(args.path)};`,
|
|
@@ -573,10 +1175,10 @@ var init_esbuild_file_imports = __esm(() => {
|
|
|
573
1175
|
].join(`
|
|
574
1176
|
`),
|
|
575
1177
|
loader: "js",
|
|
576
|
-
resolveDir:
|
|
1178
|
+
resolveDir: dirname4(args.path)
|
|
577
1179
|
}));
|
|
578
1180
|
build.onLoad({ filter: /.*/, namespace: "tarve-file-copy" }, (args) => ({
|
|
579
|
-
contents:
|
|
1181
|
+
contents: readFileSync4(args.path),
|
|
580
1182
|
loader: "file"
|
|
581
1183
|
}));
|
|
582
1184
|
}
|
|
@@ -584,10 +1186,10 @@ var init_esbuild_file_imports = __esm(() => {
|
|
|
584
1186
|
});
|
|
585
1187
|
|
|
586
1188
|
// packages/core/node-target.ts
|
|
587
|
-
import { createHash as
|
|
588
|
-
import { chmodSync, existsSync as
|
|
1189
|
+
import { createHash as createHash3, randomUUID } from "node:crypto";
|
|
1190
|
+
import { chmodSync as chmodSync2, existsSync as existsSync4, mkdirSync as mkdirSync2, readFileSync as readFileSync5, renameSync, rmSync, writeFileSync as writeFileSync4 } from "node:fs";
|
|
589
1191
|
import { homedir } from "node:os";
|
|
590
|
-
import { join as
|
|
1192
|
+
import { join as join4 } from "node:path";
|
|
591
1193
|
import { gunzipSync } from "node:zlib";
|
|
592
1194
|
function distribution(target, version) {
|
|
593
1195
|
return target === "windows-x64" ? { file: "win-x64/node.exe", executable: "node.exe" } : { file: `node-v${version}-linux-x64.tar.gz`, member: `node-v${version}-linux-x64/bin/node`, executable: "node" };
|
|
@@ -596,8 +1198,8 @@ function cacheRoot() {
|
|
|
596
1198
|
if (process.env.TARVE_CACHE_DIR)
|
|
597
1199
|
return process.env.TARVE_CACHE_DIR;
|
|
598
1200
|
if (process.platform === "win32")
|
|
599
|
-
return
|
|
600
|
-
return
|
|
1201
|
+
return join4(process.env.LOCALAPPDATA ?? join4(homedir(), "AppData", "Local"), "tarve", "cache");
|
|
1202
|
+
return join4(process.env.XDG_CACHE_HOME ?? join4(homedir(), ".cache"), "tarve");
|
|
601
1203
|
}
|
|
602
1204
|
function mirror() {
|
|
603
1205
|
return (process.env.NODEJS_ORG_MIRROR ?? "https://nodejs.org/dist").replace(/\/+$/, "");
|
|
@@ -633,15 +1235,15 @@ function extractTarMember(archive, member) {
|
|
|
633
1235
|
throw new Error(`${member} not found in the Node.js archive`);
|
|
634
1236
|
}
|
|
635
1237
|
function assertNodeVersion(executable, version) {
|
|
636
|
-
if (!
|
|
1238
|
+
if (!readFileSync5(executable).includes(`node-v${version}`)) {
|
|
637
1239
|
throw new Error(`${executable} is not Node.js v${version}; the target executable must match the Node.js that builds the app`);
|
|
638
1240
|
}
|
|
639
1241
|
}
|
|
640
1242
|
async function targetNodeExecutable(target, version) {
|
|
641
1243
|
const { file, member, executable } = distribution(target, version);
|
|
642
|
-
const directory =
|
|
643
|
-
const path =
|
|
644
|
-
if (
|
|
1244
|
+
const directory = join4(cacheRoot(), "node", `v${version}`, target);
|
|
1245
|
+
const path = join4(directory, executable);
|
|
1246
|
+
if (existsSync4(path))
|
|
645
1247
|
return path;
|
|
646
1248
|
const base = `${mirror()}/v${version}`;
|
|
647
1249
|
const sums = (await download(`${base}/SHASUMS256.txt`)).toString("utf8");
|
|
@@ -649,120 +1251,65 @@ async function targetNodeExecutable(target, version) {
|
|
|
649
1251
|
if (!expected)
|
|
650
1252
|
throw new Error(`${file} is not listed in the Node.js v${version} SHASUMS256.txt`);
|
|
651
1253
|
const payload = await download(`${base}/${file}`);
|
|
652
|
-
const actual =
|
|
1254
|
+
const actual = createHash3("sha256").update(payload).digest("hex");
|
|
653
1255
|
if (actual !== expected)
|
|
654
1256
|
throw new Error(`Checksum mismatch for ${file}: expected ${expected}, got ${actual}`);
|
|
655
1257
|
const binary = member ? extractTarMember(gunzipSync(payload), member) : payload;
|
|
656
1258
|
mkdirSync2(directory, { recursive: true });
|
|
657
|
-
const temporary =
|
|
1259
|
+
const temporary = join4(directory, `${executable}.${randomUUID()}.tmp`);
|
|
658
1260
|
try {
|
|
659
|
-
|
|
1261
|
+
writeFileSync4(temporary, binary, { mode: 493 });
|
|
660
1262
|
try {
|
|
661
1263
|
renameSync(temporary, path);
|
|
662
1264
|
} catch (error) {
|
|
663
|
-
if (!
|
|
1265
|
+
if (!existsSync4(path))
|
|
664
1266
|
throw error;
|
|
665
1267
|
}
|
|
666
1268
|
} finally {
|
|
667
1269
|
rmSync(temporary, { force: true });
|
|
668
1270
|
}
|
|
669
1271
|
if (process.platform !== "win32")
|
|
670
|
-
|
|
1272
|
+
chmodSync2(path, 493);
|
|
671
1273
|
return path;
|
|
672
1274
|
}
|
|
673
1275
|
var init_node_target = () => {};
|
|
674
1276
|
|
|
675
|
-
// packages/core/windows-executable.ts
|
|
676
|
-
var exports_windows_executable = {};
|
|
677
|
-
__export(exports_windows_executable, {
|
|
678
|
-
brandWindowsExecutable: () => brandWindowsExecutable
|
|
679
|
-
});
|
|
680
|
-
import { readFileSync as readFileSync4, writeFileSync as writeFileSync3 } from "node:fs";
|
|
681
|
-
import { Data, NtExecutable, NtExecutableResource, Resource } from "resedit";
|
|
682
|
-
function numericVersion(version) {
|
|
683
|
-
const parts = version.split(/[.+-]/).slice(0, 4).map((part) => Number.parseInt(part, 10));
|
|
684
|
-
return [0, 1, 2, 3].map((index) => {
|
|
685
|
-
const value = parts[index];
|
|
686
|
-
return Number.isInteger(value) && value >= 0 && value <= 65535 ? value : 0;
|
|
687
|
-
});
|
|
688
|
-
}
|
|
689
|
-
function brandWindowsExecutable(path, info) {
|
|
690
|
-
const executable = NtExecutable.from(readFileSync4(path), { ignoreCert: true });
|
|
691
|
-
const resources = NtExecutableResource.from(executable);
|
|
692
|
-
const versionInfo = Resource.VersionInfo.fromEntries(resources.entries)[0] ?? Resource.VersionInfo.createEmpty();
|
|
693
|
-
const languages = versionInfo.getAllLanguagesForStringValues();
|
|
694
|
-
if (languages.length === 0)
|
|
695
|
-
languages.push({ lang: 1033, codepage: 1200 });
|
|
696
|
-
const [major, minor, patch, revision] = numericVersion(info.version);
|
|
697
|
-
const text = `${major}.${minor}.${patch}.${revision}`;
|
|
698
|
-
for (const language of languages) {
|
|
699
|
-
for (const key of ["CompanyName", "LegalCopyright", "LegalTrademarks", "Comments", "PrivateBuild", "SpecialBuild"]) {
|
|
700
|
-
versionInfo.removeStringValue(language, key);
|
|
701
|
-
}
|
|
702
|
-
versionInfo.setStringValues(language, {
|
|
703
|
-
FileDescription: info.description ?? `${info.name} — native Tarve application`,
|
|
704
|
-
...info.publisher ? { CompanyName: info.publisher } : {},
|
|
705
|
-
...info.copyright ? { LegalCopyright: info.copyright } : {},
|
|
706
|
-
ProductName: info.name,
|
|
707
|
-
InternalName: info.name,
|
|
708
|
-
OriginalFilename: info.filename,
|
|
709
|
-
FileVersion: text,
|
|
710
|
-
ProductVersion: text
|
|
711
|
-
});
|
|
712
|
-
}
|
|
713
|
-
versionInfo.setFileVersion(major, minor, patch, revision);
|
|
714
|
-
versionInfo.setProductVersion(major, minor, patch, revision);
|
|
715
|
-
versionInfo.outputToResourceEntries(resources.entries);
|
|
716
|
-
if (info.ico) {
|
|
717
|
-
const groups = Resource.IconGroupEntry.fromEntries(resources.entries);
|
|
718
|
-
const group = groups[0];
|
|
719
|
-
const icons = Data.IconFile.from(info.ico).icons.map((icon) => icon.data);
|
|
720
|
-
Resource.IconGroupEntry.replaceIconsForResource(resources.entries, group?.id ?? 1, group?.lang ?? 1033, icons);
|
|
721
|
-
}
|
|
722
|
-
resources.outputResource(executable);
|
|
723
|
-
const output = Buffer.from(executable.generate());
|
|
724
|
-
output.writeUInt16LE(IMAGE_SUBSYSTEM_WINDOWS_GUI, output.readUInt32LE(60) + 24 + 68);
|
|
725
|
-
writeFileSync3(path, output);
|
|
726
|
-
}
|
|
727
|
-
var IMAGE_SUBSYSTEM_WINDOWS_GUI = 2;
|
|
728
|
-
var init_windows_executable = () => {};
|
|
729
|
-
|
|
730
1277
|
// packages/core/node-build.ts
|
|
731
|
-
import { execFileSync } from "node:child_process";
|
|
732
|
-
import { existsSync as
|
|
733
|
-
import { mkdir, rm } from "node:fs/promises";
|
|
734
|
-
import { tmpdir } from "node:os";
|
|
735
|
-
import { basename as
|
|
736
|
-
import { nativePath } from "#tarve/runtime";
|
|
1278
|
+
import { execFileSync as execFileSync2 } from "node:child_process";
|
|
1279
|
+
import { existsSync as existsSync5, mkdtempSync as mkdtempSync2, writeFileSync as writeFileSync5, readFileSync as readFileSync6 } from "node:fs";
|
|
1280
|
+
import { mkdir as mkdir2, rm as rm2 } from "node:fs/promises";
|
|
1281
|
+
import { tmpdir as tmpdir2 } from "node:os";
|
|
1282
|
+
import { basename as basename4, dirname as dirname5, join as join5, relative as relative3, resolve as resolve4 } from "node:path";
|
|
1283
|
+
import { nativePath as nativePath2 } from "#tarve/runtime";
|
|
737
1284
|
async function buildNode(options) {
|
|
738
1285
|
const target = options.target ?? hostBuildTarget();
|
|
739
1286
|
if (!target)
|
|
740
1287
|
throw new Error(`Unsupported Node.js build host: ${process.platform}-${process.arch}`);
|
|
741
1288
|
const config = targetConfig(target);
|
|
742
1289
|
const node = options.nodeExecutable ?? (process.versions.bun ? Bun.which("node") ?? "node" : process.execPath);
|
|
743
|
-
const version =
|
|
1290
|
+
const version = execFileSync2(node, ["--version"], { encoding: "utf8" }).trim();
|
|
744
1291
|
const match = /^v(\d+)\.(\d+)\./.exec(version);
|
|
745
1292
|
if (!match || Number(match[1]) < 26 || Number(match[1]) === 26 && Number(match[2]) < 10) {
|
|
746
1293
|
throw new Error(`Node.js 26.10+ is required for node:ffi inside a SEA (found ${version})`);
|
|
747
1294
|
}
|
|
748
1295
|
const { metadata, images, define } = buildMetadata(options.entrypoint, metadataOverrides(options));
|
|
749
|
-
const outfile =
|
|
1296
|
+
const outfile = resolve4(options.outfile ?? `dist/${metadata.name}${config.executableSuffix}`);
|
|
750
1297
|
let executable;
|
|
751
1298
|
if (options.targetExecutable) {
|
|
752
|
-
executable =
|
|
753
|
-
if (!
|
|
1299
|
+
executable = resolve4(options.targetExecutable);
|
|
1300
|
+
if (!existsSync5(executable))
|
|
754
1301
|
throw new Error(`Target Node.js executable is missing: ${executable}`);
|
|
755
1302
|
assertNodeVersion(executable, version.slice(1));
|
|
756
1303
|
}
|
|
757
|
-
const library =
|
|
758
|
-
if (!
|
|
1304
|
+
const library = resolve4(options.nativeLibrary ?? (target === hostBuildTarget() ? nativePath2() : join5(import.meta.dirname, "../../native", config.nativeDirectory, config.nativeName)));
|
|
1305
|
+
if (!existsSync5(library))
|
|
759
1306
|
throw new Error(`Tarve native library missing: ${library}`);
|
|
760
|
-
const temporary =
|
|
1307
|
+
const temporary = mkdtempSync2(join5(tmpdir2(), "tarve-node-build-"));
|
|
761
1308
|
try {
|
|
762
1309
|
const root = import.meta.dirname;
|
|
763
|
-
const source =
|
|
1310
|
+
const source = existsSync5(join5(root, "src/index.ts"));
|
|
764
1311
|
if (!options.nativeLibrary && (!source || target !== hostBuildTarget())) {
|
|
765
|
-
const expected = source ? await createNativeCompatibilityManifest(
|
|
1312
|
+
const expected = source ? await createNativeCompatibilityManifest(resolve4(root, "../.."), JSON.parse(readFileSync6(resolve4(root, "../../package.json"), "utf8")).version) : await readNativeCompatibilityManifest(join5(root, NATIVE_COMPATIBILITY_MANIFEST_NAME));
|
|
766
1313
|
await assertNativeRuntimeCompatible(library, target, expected);
|
|
767
1314
|
}
|
|
768
1315
|
if (!executable && target !== hostBuildTarget()) {
|
|
@@ -770,8 +1317,8 @@ async function buildNode(options) {
|
|
|
770
1317
|
}
|
|
771
1318
|
const { build: bundle } = await import("esbuild");
|
|
772
1319
|
const result = await bundle({
|
|
773
|
-
entryPoints: [
|
|
774
|
-
outfile:
|
|
1320
|
+
entryPoints: [resolve4(options.entrypoint)],
|
|
1321
|
+
outfile: join5(temporary, "app.mjs"),
|
|
775
1322
|
bundle: true,
|
|
776
1323
|
platform: "node",
|
|
777
1324
|
format: "esm",
|
|
@@ -785,10 +1332,18 @@ async function buildNode(options) {
|
|
|
785
1332
|
plugins: [fileImportAttributes, {
|
|
786
1333
|
name: "tarve-node-runtime",
|
|
787
1334
|
setup(build) {
|
|
1335
|
+
if (options.compileJsx) {
|
|
1336
|
+
build.onLoad({ filter: /\.[jt]sx$/ }, async (args) => {
|
|
1337
|
+
if (/[\\/]node_modules[\\/]/.test(args.path) || args.path.startsWith(join5(root, "src")))
|
|
1338
|
+
return;
|
|
1339
|
+
await Promise.resolve().then(() => init_jsx_compile());
|
|
1340
|
+
return { contents: (await compileJsx(readFileSync6(args.path, "utf8"))).code, loader: args.path.endsWith(".jsx") ? "jsx" : "tsx", resolveDir: dirname5(args.path) };
|
|
1341
|
+
});
|
|
1342
|
+
}
|
|
788
1343
|
if (source) {
|
|
789
1344
|
build.onResolve({ filter: /^@tarve\/core(?:\/(?:jsx-runtime|jsx-dev-runtime))?$/ }, (args) => {
|
|
790
1345
|
const entry = args.path.slice("@tarve/core".length) || "/index";
|
|
791
|
-
return { path:
|
|
1346
|
+
return { path: join5(root, "src", `${entry.slice(1)}.ts`) };
|
|
792
1347
|
});
|
|
793
1348
|
}
|
|
794
1349
|
build.onResolve({ filter: /^#tarve\/runtime$/ }, () => ({ path: "native-runtime", namespace: "tarve-node" }));
|
|
@@ -802,11 +1357,11 @@ async function buildNode(options) {
|
|
|
802
1357
|
const assets = { [config.nativeName]: seaPath(library) };
|
|
803
1358
|
for (const [path, output] of Object.entries(result.metafile.outputs)) {
|
|
804
1359
|
if (!output.entryPoint)
|
|
805
|
-
assets[
|
|
1360
|
+
assets[relative3(temporary, resolve4(path)).replaceAll("\\", "/")] = seaPath(resolve4(path));
|
|
806
1361
|
}
|
|
807
|
-
const sea =
|
|
808
|
-
|
|
809
|
-
main: seaPath(
|
|
1362
|
+
const sea = join5(temporary, "sea.json");
|
|
1363
|
+
writeFileSync5(sea, JSON.stringify({
|
|
1364
|
+
main: seaPath(join5(temporary, "app.mjs")),
|
|
810
1365
|
mainFormat: "module",
|
|
811
1366
|
output: seaPath(outfile),
|
|
812
1367
|
...executable ? { executable: seaPath(executable) } : {},
|
|
@@ -817,15 +1372,15 @@ async function buildNode(options) {
|
|
|
817
1372
|
execArgv: ["--disable-warning=ExperimentalWarning"],
|
|
818
1373
|
execArgvExtension: "env"
|
|
819
1374
|
}));
|
|
820
|
-
await
|
|
821
|
-
await
|
|
822
|
-
|
|
1375
|
+
await mkdir2(dirname5(outfile), { recursive: true });
|
|
1376
|
+
await rm2(outfile, { force: true });
|
|
1377
|
+
execFileSync2(node, ["--build-sea", sea], { stdio: "pipe" });
|
|
823
1378
|
if (config.platform === "win32") {
|
|
824
|
-
const tarveVersion = JSON.parse(
|
|
1379
|
+
const tarveVersion = JSON.parse(readFileSync6(resolve4(root, "../../package.json"), "utf8")).version;
|
|
825
1380
|
(await Promise.resolve().then(() => (init_windows_executable(), exports_windows_executable))).brandWindowsExecutable(outfile, {
|
|
826
1381
|
name: metadata.name,
|
|
827
1382
|
version: metadata.version ?? tarveVersion,
|
|
828
|
-
filename:
|
|
1383
|
+
filename: basename4(outfile),
|
|
829
1384
|
description: metadata.description,
|
|
830
1385
|
publisher: metadata.publisher,
|
|
831
1386
|
copyright: metadata.copyright,
|
|
@@ -836,7 +1391,9 @@ async function buildNode(options) {
|
|
|
836
1391
|
writeLinuxDesktopEntry(outfile, metadata, images?.png);
|
|
837
1392
|
return outfile;
|
|
838
1393
|
} finally {
|
|
839
|
-
|
|
1394
|
+
if (options.compileJsx)
|
|
1395
|
+
await (await Promise.resolve().then(() => (init_jsx_compile(), exports_jsx_compile))).closeJsxCompiler();
|
|
1396
|
+
await rm2(temporary, { recursive: true, force: true });
|
|
840
1397
|
}
|
|
841
1398
|
}
|
|
842
1399
|
function seaPath(path) {
|
|
@@ -846,22 +1403,22 @@ var init_node_build = __esm(() => {
|
|
|
846
1403
|
init_esbuild_file_imports();
|
|
847
1404
|
init_targets();
|
|
848
1405
|
init_build();
|
|
849
|
-
|
|
1406
|
+
init_package_metadata();
|
|
850
1407
|
init_native_runtime();
|
|
851
1408
|
init_node_target();
|
|
852
1409
|
});
|
|
853
1410
|
|
|
854
1411
|
// packages/core/build.ts
|
|
855
|
-
import { existsSync as
|
|
856
|
-
import { mkdir as
|
|
857
|
-
import { tmpdir as
|
|
858
|
-
import { dirname as
|
|
1412
|
+
import { existsSync as existsSync6, mkdtempSync as mkdtempSync3, writeFileSync as writeFileSync6 } from "node:fs";
|
|
1413
|
+
import { mkdir as mkdir3, rm as rm3 } from "node:fs/promises";
|
|
1414
|
+
import { tmpdir as tmpdir3 } from "node:os";
|
|
1415
|
+
import { dirname as dirname6, join as join6, resolve as resolve5 } from "node:path";
|
|
859
1416
|
import { fileURLToPath } from "node:url";
|
|
860
|
-
import { nativePath as
|
|
1417
|
+
import { nativePath as nativePath3 } from "#tarve/runtime";
|
|
861
1418
|
function resolveNativeLibrary(target, override) {
|
|
862
1419
|
if (override) {
|
|
863
|
-
const path =
|
|
864
|
-
if (!
|
|
1420
|
+
const path = resolve5(override);
|
|
1421
|
+
if (!existsSync6(path))
|
|
865
1422
|
throw new Error(`Tarve native library does not exist: ${path}`);
|
|
866
1423
|
return path;
|
|
867
1424
|
}
|
|
@@ -869,19 +1426,23 @@ function resolveNativeLibrary(target, override) {
|
|
|
869
1426
|
const hostTarget = hostBuildTarget();
|
|
870
1427
|
if (hostTarget === target) {
|
|
871
1428
|
try {
|
|
872
|
-
return
|
|
1429
|
+
return nativePath3();
|
|
873
1430
|
} catch {}
|
|
874
1431
|
}
|
|
875
1432
|
const candidates = [
|
|
876
|
-
|
|
877
|
-
|
|
1433
|
+
join6(import.meta.dir, config.nativeName),
|
|
1434
|
+
resolve5(import.meta.dir, "../../native", config.nativeDirectory, config.nativeName)
|
|
878
1435
|
];
|
|
879
|
-
const found = candidates.find(
|
|
1436
|
+
const found = candidates.find(existsSync6);
|
|
880
1437
|
if (found)
|
|
881
1438
|
return found;
|
|
882
1439
|
throw new Error(`Tarve native library missing for ${target}. Expected ${config.nativeName} in native/${config.nativeDirectory}. ` + "Use a Tarve package containing that target runtime or pass nativeLibrary explicitly.");
|
|
883
1440
|
}
|
|
884
1441
|
async function build(options) {
|
|
1442
|
+
if (options.runtime === "txiki") {
|
|
1443
|
+
await Promise.resolve().then(() => init_txiki_build());
|
|
1444
|
+
return buildTxiki(options);
|
|
1445
|
+
}
|
|
885
1446
|
if (options.runtime === "node") {
|
|
886
1447
|
await Promise.resolve().then(() => init_node_build());
|
|
887
1448
|
return buildNode(options);
|
|
@@ -892,19 +1453,19 @@ async function build(options) {
|
|
|
892
1453
|
if (!isBuildTarget(target))
|
|
893
1454
|
throw new TypeError(`Unsupported Tarve build target: ${String(target)}. Expected ${BUILD_TARGETS.join(" or ")}.`);
|
|
894
1455
|
const config = targetConfig(target);
|
|
895
|
-
const entrypoint =
|
|
896
|
-
const packageMetadata = await Bun.file(
|
|
1456
|
+
const entrypoint = resolve5(options.entrypoint);
|
|
1457
|
+
const packageMetadata = await Bun.file(resolve5(import.meta.dir, "../../package.json")).json();
|
|
897
1458
|
if (typeof packageMetadata.version !== "string" || packageMetadata.version.length === 0) {
|
|
898
1459
|
throw new Error("Tarve package version is missing.");
|
|
899
1460
|
}
|
|
900
1461
|
const { metadata, images, define } = buildMetadata(entrypoint, metadataOverrides(options));
|
|
901
1462
|
const name = metadata.name;
|
|
902
|
-
const outfile =
|
|
1463
|
+
const outfile = resolve5(options.outfile ?? `dist/${name}${config.executableSuffix}`);
|
|
903
1464
|
const library = resolveNativeLibrary(target, options.nativeLibrary);
|
|
904
1465
|
const assetsModule = fileURLToPath(import.meta.resolve("#tarve/assets"));
|
|
905
|
-
const sourceDirectory =
|
|
906
|
-
const repositorySource =
|
|
907
|
-
const expectedCompatibility = repositorySource ? await createNativeCompatibilityManifest(
|
|
1466
|
+
const sourceDirectory = join6(import.meta.dir, "src");
|
|
1467
|
+
const repositorySource = existsSync6(join6(sourceDirectory, "index.ts"));
|
|
1468
|
+
const expectedCompatibility = repositorySource ? await createNativeCompatibilityManifest(resolve5(import.meta.dir, "../.."), packageMetadata.version) : await readNativeCompatibilityManifest(join6(import.meta.dir, NATIVE_COMPATIBILITY_MANIFEST_NAME));
|
|
908
1469
|
const requireRuntimeManifest = target !== hostBuildTarget() || !options.nativeLibrary && !repositorySource;
|
|
909
1470
|
if (requireRuntimeManifest) {
|
|
910
1471
|
await assertNativeRuntimeCompatible(library, target, expectedCompatibility);
|
|
@@ -914,32 +1475,43 @@ async function build(options) {
|
|
|
914
1475
|
sha256: new Bun.CryptoHasher("sha256").update(libraryBytes).digest("hex"),
|
|
915
1476
|
size: libraryBytes.byteLength
|
|
916
1477
|
};
|
|
1478
|
+
const packDirectory = mkdtempSync3(join6(tmpdir3(), "tarve-native-"));
|
|
1479
|
+
const packedLibrary = join6(packDirectory, `${config.nativeName}.zst`);
|
|
1480
|
+
writeFileSync6(packedLibrary, Bun.zstdCompressSync(libraryBytes, { level: NATIVE_ZSTD_LEVEL }));
|
|
917
1481
|
const runtime = {
|
|
918
1482
|
name: "tarve-native-runtime",
|
|
919
1483
|
setup(builder) {
|
|
920
1484
|
if (repositorySource) {
|
|
921
|
-
builder.onResolve({ filter: /^tarve$/ }, () => ({ path:
|
|
922
|
-
builder.onResolve({ filter: /^tarve\/jsx-runtime$/ }, () => ({ path:
|
|
923
|
-
builder.onResolve({ filter: /^tarve\/jsx-dev-runtime$/ }, () => ({ path:
|
|
1485
|
+
builder.onResolve({ filter: /^tarve$/ }, () => ({ path: join6(sourceDirectory, "index.ts") }));
|
|
1486
|
+
builder.onResolve({ filter: /^tarve\/jsx-runtime$/ }, () => ({ path: join6(sourceDirectory, "jsx-runtime.ts") }));
|
|
1487
|
+
builder.onResolve({ filter: /^tarve\/jsx-dev-runtime$/ }, () => ({ path: join6(sourceDirectory, "jsx-dev-runtime.ts") }));
|
|
1488
|
+
}
|
|
1489
|
+
if (options.compileJsx) {
|
|
1490
|
+
builder.onLoad({ filter: /\.[jt]sx$/ }, async (args) => {
|
|
1491
|
+
if (/[\\/]node_modules[\\/]/.test(args.path) || args.path.startsWith(join6(import.meta.dir, "src")))
|
|
1492
|
+
return;
|
|
1493
|
+
await Promise.resolve().then(() => init_jsx_compile());
|
|
1494
|
+
return { contents: (await compileJsx(await Bun.file(args.path).text())).code, loader: args.path.endsWith(".jsx") ? "jsx" : "tsx" };
|
|
1495
|
+
});
|
|
924
1496
|
}
|
|
925
1497
|
builder.onResolve({ filter: /^#tarve\/runtime$/ }, () => ({ path: "runtime", namespace: "tarve" }));
|
|
926
1498
|
builder.onLoad({ filter: /^runtime$/, namespace: "tarve" }, () => ({
|
|
927
1499
|
loader: "ts",
|
|
928
1500
|
contents: [
|
|
929
|
-
`import library from ${JSON.stringify(
|
|
1501
|
+
`import library from ${JSON.stringify(packedLibrary)} with { type: "file" };`,
|
|
930
1502
|
`import { materializeAsset } from ${JSON.stringify(assetsModule)};`,
|
|
931
1503
|
`const digest = ${JSON.stringify(libraryDigest)};`,
|
|
932
|
-
`export function nativePath() { return materializeAsset(library, ${JSON.stringify(config.nativeName)}, digest); }`
|
|
1504
|
+
`export function nativePath() { return materializeAsset(library, ${JSON.stringify(config.nativeName)}, digest, "zstd"); }`
|
|
933
1505
|
].join(`
|
|
934
1506
|
`)
|
|
935
1507
|
}));
|
|
936
1508
|
}
|
|
937
1509
|
};
|
|
938
|
-
await
|
|
939
|
-
const iconDirectory = images && config.platform === "win32" ?
|
|
940
|
-
const icoPath = iconDirectory ?
|
|
1510
|
+
await mkdir3(dirname6(outfile), { recursive: true });
|
|
1511
|
+
const iconDirectory = images && config.platform === "win32" ? mkdtempSync3(join6(tmpdir3(), "tarve-icon-")) : undefined;
|
|
1512
|
+
const icoPath = iconDirectory ? join6(iconDirectory, "app.ico") : undefined;
|
|
941
1513
|
if (icoPath)
|
|
942
|
-
|
|
1514
|
+
writeFileSync6(icoPath, images.ico);
|
|
943
1515
|
try {
|
|
944
1516
|
const result = await Bun.build({
|
|
945
1517
|
entrypoints: [entrypoint],
|
|
@@ -968,8 +1540,11 @@ async function build(options) {
|
|
|
968
1540
|
if (!result.success)
|
|
969
1541
|
throw new AggregateError(result.logs, "Could not compile the Tarve application");
|
|
970
1542
|
} finally {
|
|
1543
|
+
if (options.compileJsx)
|
|
1544
|
+
await (await Promise.resolve().then(() => (init_jsx_compile(), exports_jsx_compile))).closeJsxCompiler();
|
|
1545
|
+
await rm3(packDirectory, { recursive: true, force: true });
|
|
971
1546
|
if (iconDirectory)
|
|
972
|
-
await
|
|
1547
|
+
await rm3(iconDirectory, { recursive: true, force: true });
|
|
973
1548
|
}
|
|
974
1549
|
if (config.platform === "linux" && (metadata.icon || metadata.appId))
|
|
975
1550
|
writeLinuxDesktopEntry(outfile, metadata, images?.png);
|
|
@@ -978,28 +1553,29 @@ async function build(options) {
|
|
|
978
1553
|
function metadataOverrides(options) {
|
|
979
1554
|
return { name: options.name, version: options.version, description: options.description, publisher: options.publisher, copyright: options.copyright, icon: options.icon, appId: options.appId };
|
|
980
1555
|
}
|
|
1556
|
+
var NATIVE_ZSTD_LEVEL = 12;
|
|
981
1557
|
var init_build = __esm(() => {
|
|
982
|
-
|
|
1558
|
+
init_package_metadata();
|
|
983
1559
|
init_targets();
|
|
984
1560
|
init_native_runtime();
|
|
985
1561
|
init_targets();
|
|
986
1562
|
});
|
|
987
1563
|
|
|
988
1564
|
// packages/core/cli.ts
|
|
989
|
-
|
|
1565
|
+
init_package_metadata();
|
|
990
1566
|
init_build();
|
|
991
1567
|
import { spawn } from "node:child_process";
|
|
992
|
-
import { resolve as
|
|
1568
|
+
import { resolve as resolve8 } from "node:path";
|
|
993
1569
|
import { parseArgs } from "node:util";
|
|
994
1570
|
|
|
995
1571
|
// packages/core/node-dev.ts
|
|
996
1572
|
init_esbuild_file_imports();
|
|
997
1573
|
import { fork, spawnSync } from "node:child_process";
|
|
998
|
-
import { mkdtempSync as
|
|
999
|
-
import { tmpdir as
|
|
1000
|
-
import { join as
|
|
1574
|
+
import { mkdtempSync as mkdtempSync4, rmSync as rmSync2, writeFileSync as writeFileSync7 } from "node:fs";
|
|
1575
|
+
import { tmpdir as tmpdir4 } from "node:os";
|
|
1576
|
+
import { join as join7, resolve as resolve6 } from "node:path";
|
|
1001
1577
|
import { context, build as build2 } from "esbuild";
|
|
1002
|
-
import { nativePath as
|
|
1578
|
+
import { nativePath as nativePath4 } from "#tarve/runtime";
|
|
1003
1579
|
var installedBuildApi = {
|
|
1004
1580
|
name: "tarve-installed-build-api",
|
|
1005
1581
|
setup(build) {
|
|
@@ -1007,10 +1583,10 @@ var installedBuildApi = {
|
|
|
1007
1583
|
}
|
|
1008
1584
|
};
|
|
1009
1585
|
async function devNode(entrypoint) {
|
|
1010
|
-
const directory =
|
|
1011
|
-
const app =
|
|
1012
|
-
const bootstrap =
|
|
1013
|
-
|
|
1586
|
+
const directory = mkdtempSync4(join7(tmpdir4(), "tarve-node-dev-"));
|
|
1587
|
+
const app = join7(directory, "app.mjs");
|
|
1588
|
+
const bootstrap = join7(directory, "bootstrap.mjs");
|
|
1589
|
+
writeFileSync7(bootstrap, `
|
|
1014
1590
|
import { pathToFileURL } from "node:url";
|
|
1015
1591
|
const entry = pathToFileURL(process.argv[2]).href;
|
|
1016
1592
|
let version = 0;
|
|
@@ -1033,7 +1609,7 @@ process.on("disconnect", () => process.exit(0));
|
|
|
1033
1609
|
let readyToReload = false;
|
|
1034
1610
|
let builds = 0;
|
|
1035
1611
|
const compiler = await context({
|
|
1036
|
-
entryPoints: [
|
|
1612
|
+
entryPoints: [resolve6(entrypoint)],
|
|
1037
1613
|
outfile: app,
|
|
1038
1614
|
bundle: true,
|
|
1039
1615
|
platform: "node",
|
|
@@ -1065,7 +1641,7 @@ process.on("disconnect", () => process.exit(0));
|
|
|
1065
1641
|
execPath: nodeExecutable(),
|
|
1066
1642
|
stdio: ["inherit", "inherit", "inherit", "ipc"],
|
|
1067
1643
|
execArgv: ["--disable-warning=ExperimentalWarning"],
|
|
1068
|
-
env: { ...process.env, TARVE_DEV: "1", TARVE_HOT: "1", TARVE_NATIVE:
|
|
1644
|
+
env: { ...process.env, TARVE_DEV: "1", TARVE_HOT: "1", TARVE_NATIVE: nativePath4(), TARVE_ENTRY: resolve6(entrypoint) }
|
|
1069
1645
|
});
|
|
1070
1646
|
child.on("message", (message) => {
|
|
1071
1647
|
if (typeof message === "object" && message && "version" in message) {
|
|
@@ -1104,11 +1680,11 @@ function nodeExecutable() {
|
|
|
1104
1680
|
return process.versions.bun ? Bun.which("node") ?? "node" : process.execPath;
|
|
1105
1681
|
}
|
|
1106
1682
|
async function runNode(entrypoint) {
|
|
1107
|
-
const directory =
|
|
1683
|
+
const directory = mkdtempSync4(join7(tmpdir4(), "tarve-node-run-"));
|
|
1108
1684
|
try {
|
|
1109
|
-
const app =
|
|
1685
|
+
const app = join7(directory, "app.mjs");
|
|
1110
1686
|
await build2({
|
|
1111
|
-
entryPoints: [
|
|
1687
|
+
entryPoints: [resolve6(entrypoint)],
|
|
1112
1688
|
outfile: app,
|
|
1113
1689
|
bundle: true,
|
|
1114
1690
|
platform: "node",
|
|
@@ -1121,7 +1697,7 @@ async function runNode(entrypoint) {
|
|
|
1121
1697
|
});
|
|
1122
1698
|
const result = spawnSync(nodeExecutable(), ["--disable-warning=ExperimentalWarning", app], {
|
|
1123
1699
|
stdio: "inherit",
|
|
1124
|
-
env: { ...process.env, TARVE_NATIVE:
|
|
1700
|
+
env: { ...process.env, TARVE_NATIVE: nativePath4(), TARVE_ENTRY: resolve6(entrypoint) }
|
|
1125
1701
|
});
|
|
1126
1702
|
if (result.error)
|
|
1127
1703
|
throw result.error;
|
|
@@ -1150,28 +1726,260 @@ function supportsNodeRuntime(version) {
|
|
|
1150
1726
|
return major > 26 || major === 26 && minor >= 10;
|
|
1151
1727
|
}
|
|
1152
1728
|
|
|
1729
|
+
// packages/core/updates-build.ts
|
|
1730
|
+
import { spawnSync as spawnSync2 } from "node:child_process";
|
|
1731
|
+
import { createHash as createHash5, createPrivateKey, createPublicKey as createPublicKey2, generateKeyPairSync, sign } from "node:crypto";
|
|
1732
|
+
import { copyFileSync, existsSync as existsSync7, mkdirSync as mkdirSync3, readFileSync as readFileSync7, statSync, writeFileSync as writeFileSync8 } from "node:fs";
|
|
1733
|
+
import { basename as basename5, extname as extname2, join as join8, resolve as resolve7 } from "node:path";
|
|
1734
|
+
|
|
1735
|
+
// packages/core/src/updates.ts
|
|
1736
|
+
import { createHash as createHash4, createPublicKey, verify } from "node:crypto";
|
|
1737
|
+
|
|
1738
|
+
// packages/core/src/embedded-assets.ts
|
|
1739
|
+
var materialized = new Map;
|
|
1740
|
+
|
|
1741
|
+
// packages/core/src/app-metadata.ts
|
|
1742
|
+
init_png();
|
|
1743
|
+
|
|
1744
|
+
// packages/core/src/updates.ts
|
|
1745
|
+
var SPKI_ED25519 = "302a300506032b6570032100";
|
|
1746
|
+
var MAX_MANIFEST_BYTES = 1 << 20;
|
|
1747
|
+
var VERSION = /^v?\d+(\.\d+){0,3}(-[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*)?(\+[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*)?$/;
|
|
1748
|
+
var PLATFORM = /^[a-z0-9]+-[a-z0-9]+$/;
|
|
1749
|
+
function isValidVersion(version) {
|
|
1750
|
+
return typeof version === "string" && version.length <= 128 && VERSION.test(version);
|
|
1751
|
+
}
|
|
1752
|
+
function isValidPlatform(platform) {
|
|
1753
|
+
return PLATFORM.test(platform);
|
|
1754
|
+
}
|
|
1755
|
+
function isLoopback(host) {
|
|
1756
|
+
return host === "localhost" || host === "[::1]" || /^127\.\d+\.\d+\.\d+$/.test(host);
|
|
1757
|
+
}
|
|
1758
|
+
function assertUpdateUrl(url, allowInsecure = false) {
|
|
1759
|
+
let parsed;
|
|
1760
|
+
try {
|
|
1761
|
+
parsed = new URL(url);
|
|
1762
|
+
} catch {
|
|
1763
|
+
throw new TypeError(`Invalid update URL: ${url}`);
|
|
1764
|
+
}
|
|
1765
|
+
if (parsed.protocol === "https:")
|
|
1766
|
+
return parsed;
|
|
1767
|
+
if (parsed.protocol === "http:" && (allowInsecure || isLoopback(parsed.hostname)))
|
|
1768
|
+
return parsed;
|
|
1769
|
+
throw new Error(`Update URL must use HTTPS: ${url}`);
|
|
1770
|
+
}
|
|
1771
|
+
function invalid(reason) {
|
|
1772
|
+
throw new TypeError(`Invalid update manifest: ${reason}`);
|
|
1773
|
+
}
|
|
1774
|
+
function validateUpdateManifest(value) {
|
|
1775
|
+
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
1776
|
+
invalid("not an object");
|
|
1777
|
+
const m = value;
|
|
1778
|
+
if (typeof m.version !== "string" || !isValidVersion(m.version))
|
|
1779
|
+
invalid("version");
|
|
1780
|
+
if (typeof m.pubDate !== "string" || Number.isNaN(Date.parse(m.pubDate)))
|
|
1781
|
+
invalid("pubDate");
|
|
1782
|
+
if (m.name !== undefined && (typeof m.name !== "string" || !m.name))
|
|
1783
|
+
invalid("name");
|
|
1784
|
+
if (m.notes !== undefined && typeof m.notes !== "string")
|
|
1785
|
+
invalid("notes");
|
|
1786
|
+
if (!m.platforms || typeof m.platforms !== "object" || Array.isArray(m.platforms))
|
|
1787
|
+
invalid("platforms");
|
|
1788
|
+
for (const [platform, artifact] of Object.entries(m.platforms)) {
|
|
1789
|
+
if (!isValidPlatform(platform))
|
|
1790
|
+
invalid(`platform ${platform}`);
|
|
1791
|
+
const a = artifact;
|
|
1792
|
+
if (!a || typeof a !== "object")
|
|
1793
|
+
invalid(`platforms.${platform}`);
|
|
1794
|
+
if (typeof a.url !== "string" || !a.url)
|
|
1795
|
+
invalid(`platforms.${platform}.url`);
|
|
1796
|
+
if (typeof a.sha256 !== "string" || !/^[0-9a-f]{64}$/i.test(a.sha256))
|
|
1797
|
+
invalid(`platforms.${platform}.sha256`);
|
|
1798
|
+
if (typeof a.size !== "number" || !Number.isSafeInteger(a.size) || a.size <= 0)
|
|
1799
|
+
invalid(`platforms.${platform}.size`);
|
|
1800
|
+
}
|
|
1801
|
+
return value;
|
|
1802
|
+
}
|
|
1803
|
+
function verifyUpdateManifest(signed, publicKey) {
|
|
1804
|
+
if (typeof signed?.payload !== "string" || typeof signed.signature !== "string")
|
|
1805
|
+
invalid("expected { payload, signature }");
|
|
1806
|
+
if (Buffer.byteLength(signed.payload) > MAX_MANIFEST_BYTES)
|
|
1807
|
+
invalid("too large");
|
|
1808
|
+
const raw = Buffer.from(publicKey.trim(), "base64");
|
|
1809
|
+
if (raw.length !== 32)
|
|
1810
|
+
throw new TypeError("Update public key must be 32 bytes of base64 (tarve keygen)");
|
|
1811
|
+
const signature = Buffer.from(signed.signature, "base64");
|
|
1812
|
+
const key = createPublicKey({ key: Buffer.concat([Buffer.from(SPKI_ED25519, "hex"), raw]), format: "der", type: "spki" });
|
|
1813
|
+
if (signature.length !== 64 || !verify(null, Buffer.from(signed.payload, "utf8"), key, signature)) {
|
|
1814
|
+
throw new Error("Update manifest signature is invalid");
|
|
1815
|
+
}
|
|
1816
|
+
let parsed;
|
|
1817
|
+
try {
|
|
1818
|
+
parsed = JSON.parse(signed.payload);
|
|
1819
|
+
} catch {
|
|
1820
|
+
invalid("payload is not JSON");
|
|
1821
|
+
}
|
|
1822
|
+
return validateUpdateManifest(parsed);
|
|
1823
|
+
}
|
|
1824
|
+
function executableMagic(platform) {
|
|
1825
|
+
if (platform.startsWith("win32-"))
|
|
1826
|
+
return Uint8Array.of(77, 90);
|
|
1827
|
+
if (platform.startsWith("linux-"))
|
|
1828
|
+
return Uint8Array.of(127, 69, 76, 70);
|
|
1829
|
+
return;
|
|
1830
|
+
}
|
|
1831
|
+
function looksExecutable(bytes, platform) {
|
|
1832
|
+
const magic = executableMagic(platform);
|
|
1833
|
+
return !magic || bytes.length >= magic.length && magic.every((byte, i) => bytes[i] === byte);
|
|
1834
|
+
}
|
|
1835
|
+
|
|
1836
|
+
// packages/core/updates-build.ts
|
|
1837
|
+
function generateUpdateKeys() {
|
|
1838
|
+
const { publicKey, privateKey } = generateKeyPairSync("ed25519");
|
|
1839
|
+
const spki = publicKey.export({ format: "der", type: "spki" });
|
|
1840
|
+
return {
|
|
1841
|
+
publicKey: Buffer.from(spki.subarray(spki.length - 32)).toString("base64"),
|
|
1842
|
+
privateKey: Buffer.from(privateKey.export({ format: "der", type: "pkcs8" })).toString("base64")
|
|
1843
|
+
};
|
|
1844
|
+
}
|
|
1845
|
+
function privateKeyObject(privateKey) {
|
|
1846
|
+
try {
|
|
1847
|
+
return createPrivateKey({ key: Buffer.from(privateKey.trim(), "base64"), format: "der", type: "pkcs8" });
|
|
1848
|
+
} catch {
|
|
1849
|
+
throw new Error("Invalid update private key: expected the base64 PKCS#8 key written by tarve keygen");
|
|
1850
|
+
}
|
|
1851
|
+
}
|
|
1852
|
+
function updatePublicKey(privateKey) {
|
|
1853
|
+
const spki = createPublicKey2(privateKeyObject(privateKey)).export({ format: "der", type: "spki" });
|
|
1854
|
+
return Buffer.from(spki.subarray(spki.length - 32)).toString("base64");
|
|
1855
|
+
}
|
|
1856
|
+
function signUpdateManifest(manifest, privateKey) {
|
|
1857
|
+
const key = privateKeyObject(privateKey);
|
|
1858
|
+
if (key.asymmetricKeyType !== "ed25519")
|
|
1859
|
+
throw new Error("Update private key must be Ed25519 (tarve keygen)");
|
|
1860
|
+
const payload = JSON.stringify(manifest);
|
|
1861
|
+
return { payload, signature: sign(null, Buffer.from(payload, "utf8"), key).toString("base64") };
|
|
1862
|
+
}
|
|
1863
|
+
function signFile(command, file) {
|
|
1864
|
+
const quoted = `"${file.replaceAll('"', "\\\"")}"`;
|
|
1865
|
+
const line = command.includes("{file}") ? command.replaceAll("{file}", quoted) : `${command} ${quoted}`;
|
|
1866
|
+
const result = spawnSync2(line, { shell: true, stdio: "inherit" });
|
|
1867
|
+
if (result.error)
|
|
1868
|
+
throw result.error;
|
|
1869
|
+
if (result.status !== 0)
|
|
1870
|
+
throw new Error(`Signing ${basename5(file)} failed (exit ${result.status}): ${line}`);
|
|
1871
|
+
}
|
|
1872
|
+
function release(options) {
|
|
1873
|
+
if (!options.artifacts.length)
|
|
1874
|
+
throw new Error("tarve release needs at least one --artifact platform=path");
|
|
1875
|
+
if (!isValidVersion(options.version))
|
|
1876
|
+
throw new Error(`Invalid --version ${options.version}: expected e.g. 1.2.3 or 1.2.3-beta.1`);
|
|
1877
|
+
if (options.baseUrl)
|
|
1878
|
+
assertUpdateUrl(options.baseUrl);
|
|
1879
|
+
const key = existsSync7(options.privateKey) ? readFileSync7(options.privateKey, "utf8") : options.privateKey;
|
|
1880
|
+
const publicKey = updatePublicKey(key);
|
|
1881
|
+
if (options.publicKey && options.publicKey.trim() !== publicKey) {
|
|
1882
|
+
throw new Error(`The private key does not match --public-key: apps built with ${options.publicKey.trim()} would reject this release`);
|
|
1883
|
+
}
|
|
1884
|
+
const entries = options.artifacts.map((pair) => {
|
|
1885
|
+
const at = pair.indexOf("=");
|
|
1886
|
+
if (at <= 0)
|
|
1887
|
+
throw new Error(`Expected --artifact platform=path, got ${pair}`);
|
|
1888
|
+
const platform = pair.slice(0, at), source = resolve7(pair.slice(at + 1));
|
|
1889
|
+
if (!isValidPlatform(platform))
|
|
1890
|
+
throw new Error(`Invalid platform ${platform}, expected e.g. win32-x64`);
|
|
1891
|
+
if (!existsSync7(source) || !statSync(source).isFile())
|
|
1892
|
+
throw new Error(`Artifact not found: ${source}`);
|
|
1893
|
+
if (statSync(source).size === 0)
|
|
1894
|
+
throw new Error(`Artifact is empty: ${source}`);
|
|
1895
|
+
if (!looksExecutable(readFileSync7(source).subarray(0, 4), platform))
|
|
1896
|
+
throw new Error(`${source} is not a ${platform} executable`);
|
|
1897
|
+
return { platform, source };
|
|
1898
|
+
});
|
|
1899
|
+
const seen = new Set;
|
|
1900
|
+
for (const { platform } of entries) {
|
|
1901
|
+
if (seen.has(platform))
|
|
1902
|
+
throw new Error(`Platform ${platform} is listed twice`);
|
|
1903
|
+
seen.add(platform);
|
|
1904
|
+
}
|
|
1905
|
+
const outDir = resolve7(options.outDir ?? "dist/updates");
|
|
1906
|
+
mkdirSync3(outDir, { recursive: true });
|
|
1907
|
+
const platforms = {};
|
|
1908
|
+
for (const { platform, source } of entries) {
|
|
1909
|
+
const stem = basename5(source, extname2(source));
|
|
1910
|
+
const file = `${stem}-${options.version}-${platform}${extname2(source)}`;
|
|
1911
|
+
const target = join8(outDir, file);
|
|
1912
|
+
if (resolve7(target) === source)
|
|
1913
|
+
throw new Error(`Artifact ${source} is already in --out; build it elsewhere`);
|
|
1914
|
+
copyFileSync(source, target);
|
|
1915
|
+
if (options.sign && platform.startsWith("win32-")) {
|
|
1916
|
+
const before = createHash5("sha256").update(readFileSync7(target)).digest("hex");
|
|
1917
|
+
signFile(options.sign, target);
|
|
1918
|
+
if (createHash5("sha256").update(readFileSync7(target)).digest("hex") === before) {
|
|
1919
|
+
throw new Error(`The sign command left ${file} unchanged: it was not signed`);
|
|
1920
|
+
}
|
|
1921
|
+
}
|
|
1922
|
+
const bytes = readFileSync7(target);
|
|
1923
|
+
if (!looksExecutable(bytes.subarray(0, 4), platform))
|
|
1924
|
+
throw new Error(`The sign command broke ${file}: it is no longer an executable`);
|
|
1925
|
+
platforms[platform] = {
|
|
1926
|
+
url: options.baseUrl ? options.baseUrl.replace(/\/?$/, "/") + encodeURIComponent(file) : encodeURIComponent(file),
|
|
1927
|
+
sha256: createHash5("sha256").update(bytes).digest("hex"),
|
|
1928
|
+
size: bytes.length
|
|
1929
|
+
};
|
|
1930
|
+
}
|
|
1931
|
+
const manifest = {
|
|
1932
|
+
...options.name ? { name: options.name } : {},
|
|
1933
|
+
version: options.version,
|
|
1934
|
+
...options.notes ? { notes: options.notes } : {},
|
|
1935
|
+
pubDate: new Date().toISOString(),
|
|
1936
|
+
platforms
|
|
1937
|
+
};
|
|
1938
|
+
const signed = signUpdateManifest(manifest, key);
|
|
1939
|
+
verifyUpdateManifest(signed, publicKey);
|
|
1940
|
+
const path = join8(outDir, "latest.json");
|
|
1941
|
+
writeFileSync8(path, JSON.stringify(signed, null, 2) + `
|
|
1942
|
+
`);
|
|
1943
|
+
return path;
|
|
1944
|
+
}
|
|
1945
|
+
|
|
1153
1946
|
// packages/core/cli.ts
|
|
1154
1947
|
init_targets();
|
|
1948
|
+
import { existsSync as existsSync8, writeFileSync as writeFileSync9 } from "node:fs";
|
|
1155
1949
|
var COMMANDS = ["run", "dev", "build"];
|
|
1950
|
+
var KEY_FILE = "tarve-update.key";
|
|
1156
1951
|
function usage(target) {
|
|
1157
1952
|
const output = `dist/App${target ? targetConfig(target).executableSuffix : ""}`;
|
|
1158
1953
|
return [
|
|
1159
1954
|
"Usage: tarve <run|dev|build> <app.tsx> [options]",
|
|
1955
|
+
" tarve keygen",
|
|
1956
|
+
" tarve release --version 1.2.0 --artifact win32-x64=dist/App.exe [...]",
|
|
1160
1957
|
"",
|
|
1161
|
-
" run
|
|
1162
|
-
" dev
|
|
1163
|
-
" build
|
|
1958
|
+
" run run the app",
|
|
1959
|
+
" dev run with hot reload into the same window",
|
|
1960
|
+
" build compile a standalone executable",
|
|
1961
|
+
` keygen create an update signing key pair (writes ${KEY_FILE}, prints the public key)`,
|
|
1962
|
+
" release copy built executables to --out and write the signed latest.json",
|
|
1164
1963
|
"",
|
|
1165
1964
|
"Options:",
|
|
1166
|
-
" --runtime bun|node
|
|
1965
|
+
" --runtime bun|node|txiki defaults to the runtime that launched tarve; txiki: build only",
|
|
1167
1966
|
` --target ${BUILD_TARGETS.join("|")} build: target OS (cross-compiles)`,
|
|
1168
1967
|
` --outfile ${output} build: output path`,
|
|
1968
|
+
" --compile-jsx build: compile JSX into reusable templates (needs typescript 7)",
|
|
1169
1969
|
" --name App, --version 1.0.0 app metadata (executable, window, app identity)",
|
|
1170
1970
|
" --icon icon.png app icon: .png (square, 256 px+) or .ico",
|
|
1171
1971
|
" --app-id Acme.App Windows AppUserModelID / Linux app id",
|
|
1172
1972
|
" --description, --publisher, --copyright build: executable details",
|
|
1173
1973
|
' each defaults to the "tarve" field of package.json',
|
|
1174
|
-
" --target-executable path build (node): target OS Node binary for offline cross-builds"
|
|
1974
|
+
" --target-executable path build (node): target OS Node binary for offline cross-builds",
|
|
1975
|
+
" --artifact platform=path release: repeatable, platform is <os>-<arch> (win32-x64, linux-x64)",
|
|
1976
|
+
` --key ${KEY_FILE} release: private key file or base64 (default: TARVE_UPDATE_PRIVATE_KEY, then ./${KEY_FILE})`,
|
|
1977
|
+
" --public-key base64 release: verify the signed manifest before writing it",
|
|
1978
|
+
" --out dist/updates release: output directory; keygen: where the key goes",
|
|
1979
|
+
" --base-url https://host/path/ release: artifact URL prefix (default: relative to latest.json)",
|
|
1980
|
+
" --notes text release: release notes",
|
|
1981
|
+
' --sign "signtool sign … {file}" release: Authenticode command per Windows artifact, before hashing',
|
|
1982
|
+
" (default: TARVE_SIGN_COMMAND)"
|
|
1175
1983
|
].join(`
|
|
1176
1984
|
`);
|
|
1177
1985
|
}
|
|
@@ -1206,7 +2014,15 @@ async function main() {
|
|
|
1206
2014
|
copyright: { type: "string" },
|
|
1207
2015
|
target: { type: "string" },
|
|
1208
2016
|
runtime: { type: "string" },
|
|
2017
|
+
"compile-jsx": { type: "boolean" },
|
|
1209
2018
|
"target-executable": { type: "string" },
|
|
2019
|
+
artifact: { type: "string", multiple: true },
|
|
2020
|
+
key: { type: "string" },
|
|
2021
|
+
"public-key": { type: "string" },
|
|
2022
|
+
out: { type: "string" },
|
|
2023
|
+
"base-url": { type: "string" },
|
|
2024
|
+
notes: { type: "string" },
|
|
2025
|
+
sign: { type: "string" },
|
|
1210
2026
|
help: { type: "boolean", short: "h" }
|
|
1211
2027
|
}
|
|
1212
2028
|
});
|
|
@@ -1215,13 +2031,37 @@ async function main() {
|
|
|
1215
2031
|
console.log(usage(values.target && isBuildTarget(values.target) ? values.target : hostBuildTarget()));
|
|
1216
2032
|
return 0;
|
|
1217
2033
|
}
|
|
2034
|
+
if (command === "keygen")
|
|
2035
|
+
return keygen(values.out);
|
|
2036
|
+
if (command === "release") {
|
|
2037
|
+
if (!values.version)
|
|
2038
|
+
throw new Error("tarve release needs --version.");
|
|
2039
|
+
const privateKey = values.key ?? process.env.TARVE_UPDATE_PRIVATE_KEY ?? (existsSync8(KEY_FILE) ? KEY_FILE : undefined);
|
|
2040
|
+
if (!privateKey)
|
|
2041
|
+
throw new Error(`tarve release needs --key, TARVE_UPDATE_PRIVATE_KEY or ./${KEY_FILE} (tarve keygen).`);
|
|
2042
|
+
console.log(release({
|
|
2043
|
+
version: values.version,
|
|
2044
|
+
artifacts: values.artifact ?? [],
|
|
2045
|
+
privateKey,
|
|
2046
|
+
publicKey: values["public-key"],
|
|
2047
|
+
outDir: values.out,
|
|
2048
|
+
baseUrl: values["base-url"],
|
|
2049
|
+
name: values.name,
|
|
2050
|
+
notes: values.notes,
|
|
2051
|
+
sign: values.sign ?? process.env.TARVE_SIGN_COMMAND
|
|
2052
|
+
}));
|
|
2053
|
+
return 0;
|
|
2054
|
+
}
|
|
1218
2055
|
if (!COMMANDS.includes(command) || !entrypoint || positionals.length !== 2) {
|
|
1219
2056
|
throw new Error("Expected: tarve <run|dev|build> <app.tsx>. Use --help for options.");
|
|
1220
2057
|
}
|
|
1221
2058
|
if (values.target !== undefined && !isBuildTarget(values.target)) {
|
|
1222
2059
|
throw new Error(`Unsupported target: ${values.target}. Expected ${BUILD_TARGETS.join(" or ")}.`);
|
|
1223
2060
|
}
|
|
1224
|
-
const
|
|
2061
|
+
const txiki = values.runtime === "txiki";
|
|
2062
|
+
if (txiki && command !== "build")
|
|
2063
|
+
throw new Error("--runtime txiki is only supported by tarve build.");
|
|
2064
|
+
const runtime = txiki ? "txiki" : selectRuntime(values.runtime);
|
|
1225
2065
|
if (runtime === "bun" && !process.versions.bun) {
|
|
1226
2066
|
return forward("bun", [process.argv[1], ...process.argv.slice(2), "--runtime", "bun"]);
|
|
1227
2067
|
}
|
|
@@ -1238,6 +2078,7 @@ async function main() {
|
|
|
1238
2078
|
copyright: values.copyright,
|
|
1239
2079
|
target: values.target,
|
|
1240
2080
|
runtime,
|
|
2081
|
+
compileJsx: values["compile-jsx"],
|
|
1241
2082
|
targetExecutable: values["target-executable"]
|
|
1242
2083
|
}));
|
|
1243
2084
|
return 0;
|
|
@@ -1251,7 +2092,7 @@ async function main() {
|
|
|
1251
2092
|
}
|
|
1252
2093
|
return runNode(entrypoint);
|
|
1253
2094
|
}
|
|
1254
|
-
const app =
|
|
2095
|
+
const app = resolve8(entrypoint);
|
|
1255
2096
|
return command === "dev" ? forward(process.execPath, ["--hot", app], { ...process.env, TARVE_DEV: process.env.TARVE_DEV ?? "1" }) : forward(process.execPath, [app]);
|
|
1256
2097
|
}
|
|
1257
2098
|
try {
|
|
@@ -1260,3 +2101,14 @@ try {
|
|
|
1260
2101
|
console.error(`error: ${error instanceof Error ? error.message : String(error)}`);
|
|
1261
2102
|
process.exitCode = 1;
|
|
1262
2103
|
}
|
|
2104
|
+
function keygen(outDir = ".") {
|
|
2105
|
+
const path = resolve8(outDir, KEY_FILE);
|
|
2106
|
+
if (existsSync8(path))
|
|
2107
|
+
throw new Error(`${path} already exists; delete it to replace the key (installed apps reject updates signed with a new one).`);
|
|
2108
|
+
const keys = generateUpdateKeys();
|
|
2109
|
+
writeFileSync9(path, keys.privateKey + `
|
|
2110
|
+
`, { mode: 384 });
|
|
2111
|
+
console.log(`private key: ${path} (keep it secret and out of git)`);
|
|
2112
|
+
console.log(`public key: ${keys.publicKey}`);
|
|
2113
|
+
return 0;
|
|
2114
|
+
}
|