@tarve/core 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/PERFORMANCE.md +82 -0
- package/README.md +549 -0
- package/RELEASE.md +72 -0
- package/dist/npm/build.js +259 -0
- package/dist/npm/cli.js +285 -0
- package/dist/npm/embedded-assets.js +52 -0
- package/dist/npm/index.js +7077 -0
- package/dist/npm/jsx-dev-runtime.js +17 -0
- package/dist/npm/jsx-runtime.js +27 -0
- package/dist/npm/native-runtime.json +7 -0
- package/dist/npm/protocol.js +8 -0
- package/dist/npm/runtime.js +98 -0
- package/dist/types/core/build.d.ts +13 -0
- package/dist/types/core/cli.d.ts +2 -0
- package/dist/types/core/native-runtime.d.ts +22 -0
- package/dist/types/core/src/advanced-controls.d.ts +120 -0
- package/dist/types/core/src/app.d.ts +59 -0
- package/dist/types/core/src/assets.d.ts +4 -0
- package/dist/types/core/src/bridge/binding.d.ts +34 -0
- package/dist/types/core/src/bridge/index.d.ts +41 -0
- package/dist/types/core/src/bridge/runtime.d.ts +4 -0
- package/dist/types/core/src/chat-controls.d.ts +40 -0
- package/dist/types/core/src/component-adapter.d.ts +11 -0
- package/dist/types/core/src/components/button.d.ts +12 -0
- package/dist/types/core/src/components/icon.d.ts +13 -0
- package/dist/types/core/src/components/image.d.ts +43 -0
- package/dist/types/core/src/components/input.d.ts +22 -0
- package/dist/types/core/src/components/layout.d.ts +22 -0
- package/dist/types/core/src/components/link.d.ts +10 -0
- package/dist/types/core/src/components/modal.d.ts +15 -0
- package/dist/types/core/src/components/pressable.d.ts +19 -0
- package/dist/types/core/src/components/rich-content.d.ts +45 -0
- package/dist/types/core/src/components/scroll.d.ts +12 -0
- package/dist/types/core/src/components/svg.d.ts +61 -0
- package/dist/types/core/src/components/text.d.ts +9 -0
- package/dist/types/core/src/components/titlebar.d.ts +10 -0
- package/dist/types/core/src/components/window.d.ts +20 -0
- package/dist/types/core/src/components.d.ts +34 -0
- package/dist/types/core/src/controls.d.ts +84 -0
- package/dist/types/core/src/desktop-controls.d.ts +62 -0
- package/dist/types/core/src/dev-overlay.d.ts +9 -0
- package/dist/types/core/src/embedded-assets.d.ts +4 -0
- package/dist/types/core/src/extra-controls.d.ts +182 -0
- package/dist/types/core/src/form-controls.d.ts +118 -0
- package/dist/types/core/src/hotkeys.d.ts +3 -0
- package/dist/types/core/src/index.d.ts +36 -0
- package/dist/types/core/src/intrinsic-style.d.ts +3 -0
- package/dist/types/core/src/jsx-dev-runtime.d.ts +2 -0
- package/dist/types/core/src/jsx-runtime.d.ts +279 -0
- package/dist/types/core/src/list.d.ts +10 -0
- package/dist/types/core/src/motion.d.ts +19 -0
- package/dist/types/core/src/popups.d.ts +115 -0
- package/dist/types/core/src/reconciler.d.ts +54 -0
- package/dist/types/core/src/render-scope.d.ts +3 -0
- package/dist/types/core/src/select.d.ts +17 -0
- package/dist/types/core/src/style.d.ts +7 -0
- package/dist/types/core/src/test-renderer.d.ts +84 -0
- package/dist/types/core/src/text-search.d.ts +37 -0
- package/dist/types/core/src/theme.d.ts +91 -0
- package/dist/types/core/src/virtual-list.d.ts +31 -0
- package/dist/types/core/src/visual-testing.d.ts +24 -0
- package/dist/types/core/targets.d.ts +16 -0
- package/dist/types/protocol/src/index.d.ts +534 -0
- package/native/linux-x64/libtarve_native.so +0 -0
- package/native/linux-x64/runtime.json +10 -0
- package/native/win32-x64/runtime.json +10 -0
- package/native/win32-x64/tarve_native.dll +0 -0
- package/package.json +131 -0
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// packages/core/build.ts
|
|
3
|
+
import { existsSync as existsSync2 } from "fs";
|
|
4
|
+
import { mkdir } from "fs/promises";
|
|
5
|
+
import { basename as basename2, dirname as dirname2, join as join2, resolve } from "path";
|
|
6
|
+
import { fileURLToPath } from "url";
|
|
7
|
+
import { nativePath } from "#tarve/runtime";
|
|
8
|
+
|
|
9
|
+
// packages/core/targets.ts
|
|
10
|
+
var BUILD_TARGETS = ["windows-x64", "linux-x64"];
|
|
11
|
+
var TARGET_CONFIGS = {
|
|
12
|
+
"windows-x64": {
|
|
13
|
+
platform: "win32",
|
|
14
|
+
arch: "x64",
|
|
15
|
+
bunTarget: "bun-windows-x64",
|
|
16
|
+
nativeDirectory: "win32-x64",
|
|
17
|
+
nativeName: "tarve_native.dll",
|
|
18
|
+
executableSuffix: ".exe"
|
|
19
|
+
},
|
|
20
|
+
"linux-x64": {
|
|
21
|
+
platform: "linux",
|
|
22
|
+
arch: "x64",
|
|
23
|
+
bunTarget: "bun-linux-x64",
|
|
24
|
+
nativeDirectory: "linux-x64",
|
|
25
|
+
nativeName: "libtarve_native.so",
|
|
26
|
+
executableSuffix: ""
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
function isBuildTarget(value) {
|
|
30
|
+
return BUILD_TARGETS.includes(value);
|
|
31
|
+
}
|
|
32
|
+
function targetConfig(target) {
|
|
33
|
+
return TARGET_CONFIGS[target];
|
|
34
|
+
}
|
|
35
|
+
function hostBuildTarget(platform = process.platform, arch = process.arch) {
|
|
36
|
+
return BUILD_TARGETS.find((target) => {
|
|
37
|
+
const config = TARGET_CONFIGS[target];
|
|
38
|
+
return config.platform === platform && config.arch === arch;
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
function nativeDirectoryForBuildTarget(target) {
|
|
42
|
+
return TARGET_CONFIGS[target].nativeDirectory;
|
|
43
|
+
}
|
|
44
|
+
function nativeRelativePath(target) {
|
|
45
|
+
const config = TARGET_CONFIGS[target];
|
|
46
|
+
return `native/${config.nativeDirectory}/${config.nativeName}`;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// packages/core/native-runtime.ts
|
|
50
|
+
import { createHash } from "crypto";
|
|
51
|
+
import { existsSync } from "fs";
|
|
52
|
+
import { readFile, writeFile } from "fs/promises";
|
|
53
|
+
import { basename, dirname, join } from "path";
|
|
54
|
+
|
|
55
|
+
// packages/protocol/src/index.ts
|
|
56
|
+
var NATIVE_ABI_VERSION = 5;
|
|
57
|
+
var PROTOCOL_VERSION = 45;
|
|
58
|
+
|
|
59
|
+
// packages/core/native-runtime.ts
|
|
60
|
+
var NATIVE_RUNTIME_MANIFEST_NAME = "runtime.json";
|
|
61
|
+
var NATIVE_COMPATIBILITY_MANIFEST_NAME = "native-runtime.json";
|
|
62
|
+
var NATIVE_RUNTIME_MANIFEST_SCHEMA = 1;
|
|
63
|
+
async function sha256File(path) {
|
|
64
|
+
return createHash("sha256").update(await readFile(path)).digest("hex");
|
|
65
|
+
}
|
|
66
|
+
async function addGlobFiles(files, root, prefix, pattern) {
|
|
67
|
+
const directory = join(root, "native", prefix);
|
|
68
|
+
if (!existsSync(directory))
|
|
69
|
+
return;
|
|
70
|
+
for await (const relative of new Bun.Glob(pattern).scan({ cwd: directory, onlyFiles: true })) {
|
|
71
|
+
files.push(join(prefix, relative).replaceAll("\\", "/"));
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
async function nativeSourceFingerprint(root) {
|
|
75
|
+
const native = join(root, "native");
|
|
76
|
+
const files = ["Cargo.toml", "Cargo.lock"];
|
|
77
|
+
await addGlobFiles(files, root, "src", "**/*.rs");
|
|
78
|
+
await addGlobFiles(files, root, "vendor", "**/*.rs");
|
|
79
|
+
await addGlobFiles(files, root, "vendor", "**/Cargo.toml");
|
|
80
|
+
files.sort();
|
|
81
|
+
const hash = createHash("sha256");
|
|
82
|
+
for (const relative of files) {
|
|
83
|
+
const path = join(native, ...relative.split("/"));
|
|
84
|
+
if (!existsSync(path))
|
|
85
|
+
throw new Error(`Native source input is missing: ${path}`);
|
|
86
|
+
hash.update(relative);
|
|
87
|
+
hash.update("\x00");
|
|
88
|
+
hash.update((await readFile(path, "utf8")).replace(/\r\n?/g, `
|
|
89
|
+
`));
|
|
90
|
+
hash.update("\x00");
|
|
91
|
+
}
|
|
92
|
+
return hash.digest("hex");
|
|
93
|
+
}
|
|
94
|
+
async function createNativeCompatibilityManifest(root, packageVersion) {
|
|
95
|
+
const [rustProtocol, rustBridge] = await Promise.all([
|
|
96
|
+
readFile(join(root, "native", "src", "protocol.rs"), "utf8"),
|
|
97
|
+
readFile(join(root, "native", "src", "bridge.rs"), "utf8")
|
|
98
|
+
]);
|
|
99
|
+
const rustProtocolVersion = Number(rustProtocol.match(/pub const VERSION: u32 = (\d+);/)?.[1]);
|
|
100
|
+
const rustAbiVersion = Number(rustBridge.match(/pub const ABI_VERSION: u32 = (\d+);/)?.[1]);
|
|
101
|
+
if (rustProtocolVersion !== PROTOCOL_VERSION) {
|
|
102
|
+
throw new Error(`Tarve protocol source mismatch: TypeScript=${PROTOCOL_VERSION}, Rust=${Number.isFinite(rustProtocolVersion) ? rustProtocolVersion : "invalid"}`);
|
|
103
|
+
}
|
|
104
|
+
if (rustAbiVersion !== NATIVE_ABI_VERSION) {
|
|
105
|
+
throw new Error(`Tarve native ABI source mismatch: TypeScript=${NATIVE_ABI_VERSION}, Rust=${Number.isFinite(rustAbiVersion) ? rustAbiVersion : "invalid"}`);
|
|
106
|
+
}
|
|
107
|
+
return {
|
|
108
|
+
schemaVersion: NATIVE_RUNTIME_MANIFEST_SCHEMA,
|
|
109
|
+
packageVersion,
|
|
110
|
+
protocolVersion: PROTOCOL_VERSION,
|
|
111
|
+
nativeAbiVersion: NATIVE_ABI_VERSION,
|
|
112
|
+
sourceFingerprint: await nativeSourceFingerprint(root)
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
function validateCompatibilityShape(value, path) {
|
|
116
|
+
if (!value || typeof value !== "object")
|
|
117
|
+
throw new Error(`Invalid Tarve native compatibility metadata: ${path}`);
|
|
118
|
+
const manifest = value;
|
|
119
|
+
if (manifest.schemaVersion !== NATIVE_RUNTIME_MANIFEST_SCHEMA || typeof manifest.packageVersion !== "string" || !Number.isSafeInteger(manifest.protocolVersion) || !Number.isSafeInteger(manifest.nativeAbiVersion) || typeof manifest.sourceFingerprint !== "string" || !/^[0-9a-f]{64}$/.test(manifest.sourceFingerprint)) {
|
|
120
|
+
throw new Error(`Invalid Tarve native compatibility metadata: ${path}`);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
async function readNativeCompatibilityManifest(path) {
|
|
124
|
+
if (!existsSync(path))
|
|
125
|
+
throw new Error(`Tarve native compatibility metadata is missing: ${path}`);
|
|
126
|
+
let value;
|
|
127
|
+
try {
|
|
128
|
+
value = JSON.parse(await readFile(path, "utf8"));
|
|
129
|
+
} catch (error) {
|
|
130
|
+
throw new Error(`Could not read Tarve native compatibility metadata: ${path}: ${error instanceof Error ? error.message : String(error)}`);
|
|
131
|
+
}
|
|
132
|
+
validateCompatibilityShape(value, path);
|
|
133
|
+
return value;
|
|
134
|
+
}
|
|
135
|
+
async function assertNativeRuntimeCompatible(library, target, expected) {
|
|
136
|
+
const path = join(dirname(library), NATIVE_RUNTIME_MANIFEST_NAME);
|
|
137
|
+
if (!existsSync(path)) {
|
|
138
|
+
throw new Error(`Tarve native runtime metadata missing for ${target}: ${path}. ` + "Rebuild/stage this target with `bun run package:native` on its native OS before cross-compiling.");
|
|
139
|
+
}
|
|
140
|
+
let value;
|
|
141
|
+
try {
|
|
142
|
+
value = JSON.parse(await readFile(path, "utf8"));
|
|
143
|
+
} catch (error) {
|
|
144
|
+
throw new Error(`Could not read Tarve native runtime metadata for ${target}: ${error instanceof Error ? error.message : String(error)}`);
|
|
145
|
+
}
|
|
146
|
+
validateCompatibilityShape(value, path);
|
|
147
|
+
const manifest = value;
|
|
148
|
+
const config = targetConfig(target);
|
|
149
|
+
if (manifest.target !== target || manifest.nativeName !== config.nativeName || typeof manifest.sha256 !== "string") {
|
|
150
|
+
throw new Error(`Invalid Tarve native runtime metadata for ${target}: ${path}`);
|
|
151
|
+
}
|
|
152
|
+
const stale = manifest.packageVersion !== expected.packageVersion || manifest.protocolVersion !== expected.protocolVersion || manifest.nativeAbiVersion !== expected.nativeAbiVersion || manifest.sourceFingerprint !== expected.sourceFingerprint;
|
|
153
|
+
if (stale) {
|
|
154
|
+
throw new Error(`Tarve native runtime for ${target} is stale or incompatible ` + `(runtime protocol ${manifest.protocolVersion}, expected ${expected.protocolVersion}). ` + "Rebuild it on its native OS with `bun run package:native` before cross-compiling.");
|
|
155
|
+
}
|
|
156
|
+
const hash = await sha256File(library);
|
|
157
|
+
if (manifest.sha256 !== hash) {
|
|
158
|
+
throw new Error(`Tarve native runtime hash mismatch for ${target}: ${library}. ` + "Rebuild/stage the runtime with `bun run package:native`.");
|
|
159
|
+
}
|
|
160
|
+
return manifest;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// packages/core/build.ts
|
|
164
|
+
function resolveNativeLibrary(target, override) {
|
|
165
|
+
if (override) {
|
|
166
|
+
const path = resolve(override);
|
|
167
|
+
if (!existsSync2(path))
|
|
168
|
+
throw new Error(`Tarve native library does not exist: ${path}`);
|
|
169
|
+
return path;
|
|
170
|
+
}
|
|
171
|
+
const config = targetConfig(target);
|
|
172
|
+
const hostTarget = hostBuildTarget();
|
|
173
|
+
if (hostTarget === target) {
|
|
174
|
+
try {
|
|
175
|
+
return nativePath();
|
|
176
|
+
} catch {}
|
|
177
|
+
}
|
|
178
|
+
const candidates = [
|
|
179
|
+
join2(import.meta.dir, config.nativeName),
|
|
180
|
+
resolve(import.meta.dir, "../../native", config.nativeDirectory, config.nativeName)
|
|
181
|
+
];
|
|
182
|
+
const found = candidates.find(existsSync2);
|
|
183
|
+
if (found)
|
|
184
|
+
return found;
|
|
185
|
+
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.");
|
|
186
|
+
}
|
|
187
|
+
async function build(options) {
|
|
188
|
+
const target = options.target ?? hostBuildTarget();
|
|
189
|
+
if (!target)
|
|
190
|
+
throw new Error(`Tarve executable distribution is not supported for ${process.platform}-${process.arch}. Pass an explicit supported target when cross-compiling.`);
|
|
191
|
+
if (!isBuildTarget(target))
|
|
192
|
+
throw new TypeError(`Unsupported Tarve build target: ${String(target)}. Expected ${BUILD_TARGETS.join(" or ")}.`);
|
|
193
|
+
const config = targetConfig(target);
|
|
194
|
+
const entrypoint = resolve(options.entrypoint);
|
|
195
|
+
const packageMetadata = await Bun.file(resolve(import.meta.dir, "../../package.json")).json();
|
|
196
|
+
if (typeof packageMetadata.version !== "string" || packageMetadata.version.length === 0) {
|
|
197
|
+
throw new Error("Tarve package version is missing.");
|
|
198
|
+
}
|
|
199
|
+
const name = options.name ?? basename2(entrypoint).replace(/\.[^.]+$/, "");
|
|
200
|
+
const outfile = resolve(options.outfile ?? `dist/${name}${config.executableSuffix}`);
|
|
201
|
+
const library = resolveNativeLibrary(target, options.nativeLibrary);
|
|
202
|
+
const assetsModule = fileURLToPath(import.meta.resolve("#tarve/assets"));
|
|
203
|
+
const sourceDirectory = join2(import.meta.dir, "src");
|
|
204
|
+
const repositorySource = existsSync2(join2(sourceDirectory, "index.ts"));
|
|
205
|
+
const expectedCompatibility = repositorySource ? await createNativeCompatibilityManifest(resolve(import.meta.dir, "../.."), packageMetadata.version) : await readNativeCompatibilityManifest(join2(import.meta.dir, NATIVE_COMPATIBILITY_MANIFEST_NAME));
|
|
206
|
+
const requireRuntimeManifest = target !== hostBuildTarget() || !options.nativeLibrary && !repositorySource;
|
|
207
|
+
if (requireRuntimeManifest) {
|
|
208
|
+
await assertNativeRuntimeCompatible(library, target, expectedCompatibility);
|
|
209
|
+
}
|
|
210
|
+
const runtime = {
|
|
211
|
+
name: "tarve-native-runtime",
|
|
212
|
+
setup(builder) {
|
|
213
|
+
if (repositorySource) {
|
|
214
|
+
builder.onResolve({ filter: /^tarve$/ }, () => ({ path: join2(sourceDirectory, "index.ts") }));
|
|
215
|
+
builder.onResolve({ filter: /^tarve\/jsx-runtime$/ }, () => ({ path: join2(sourceDirectory, "jsx-runtime.ts") }));
|
|
216
|
+
builder.onResolve({ filter: /^tarve\/jsx-dev-runtime$/ }, () => ({ path: join2(sourceDirectory, "jsx-dev-runtime.ts") }));
|
|
217
|
+
}
|
|
218
|
+
builder.onResolve({ filter: /^#tarve\/runtime$/ }, () => ({ path: "runtime", namespace: "tarve" }));
|
|
219
|
+
builder.onLoad({ filter: /^runtime$/, namespace: "tarve" }, () => ({
|
|
220
|
+
loader: "ts",
|
|
221
|
+
contents: [
|
|
222
|
+
`import library from ${JSON.stringify(library)} with { type: "file" };`,
|
|
223
|
+
`import { materializeAsset } from ${JSON.stringify(assetsModule)};`,
|
|
224
|
+
`export function nativePath() { return materializeAsset(library, ${JSON.stringify(config.nativeName)}); }`
|
|
225
|
+
].join(`
|
|
226
|
+
`)
|
|
227
|
+
}));
|
|
228
|
+
}
|
|
229
|
+
};
|
|
230
|
+
await mkdir(dirname2(outfile), { recursive: true });
|
|
231
|
+
const result = await Bun.build({
|
|
232
|
+
entrypoints: [entrypoint],
|
|
233
|
+
target: "bun",
|
|
234
|
+
plugins: [runtime],
|
|
235
|
+
define: { "process.env.NODE_ENV": JSON.stringify("production") },
|
|
236
|
+
minify: true,
|
|
237
|
+
compile: {
|
|
238
|
+
target: config.bunTarget,
|
|
239
|
+
outfile,
|
|
240
|
+
autoloadDotenv: false,
|
|
241
|
+
autoloadBunfig: false,
|
|
242
|
+
...config.platform === "win32" ? {
|
|
243
|
+
windows: { hideConsole: true, title: name, version: options.version ?? packageMetadata.version, description: `${name} \u2014 native Tarve application` }
|
|
244
|
+
} : {}
|
|
245
|
+
}
|
|
246
|
+
});
|
|
247
|
+
if (!result.success)
|
|
248
|
+
throw new AggregateError(result.logs, "Could not compile the Tarve application");
|
|
249
|
+
return outfile;
|
|
250
|
+
}
|
|
251
|
+
export {
|
|
252
|
+
BUILD_TARGETS,
|
|
253
|
+
build,
|
|
254
|
+
hostBuildTarget,
|
|
255
|
+
isBuildTarget,
|
|
256
|
+
nativeDirectoryForBuildTarget,
|
|
257
|
+
nativeRelativePath,
|
|
258
|
+
targetConfig
|
|
259
|
+
};
|
package/dist/npm/cli.js
ADDED
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
// @bun
|
|
3
|
+
|
|
4
|
+
// packages/core/cli.ts
|
|
5
|
+
import { parseArgs } from "util";
|
|
6
|
+
|
|
7
|
+
// packages/core/build.ts
|
|
8
|
+
import { existsSync as existsSync2 } from "fs";
|
|
9
|
+
import { mkdir } from "fs/promises";
|
|
10
|
+
import { basename as basename2, dirname as dirname2, join as join2, resolve } from "path";
|
|
11
|
+
import { fileURLToPath } from "url";
|
|
12
|
+
import { nativePath } from "#tarve/runtime";
|
|
13
|
+
|
|
14
|
+
// packages/core/targets.ts
|
|
15
|
+
var BUILD_TARGETS = ["windows-x64", "linux-x64"];
|
|
16
|
+
var TARGET_CONFIGS = {
|
|
17
|
+
"windows-x64": {
|
|
18
|
+
platform: "win32",
|
|
19
|
+
arch: "x64",
|
|
20
|
+
bunTarget: "bun-windows-x64",
|
|
21
|
+
nativeDirectory: "win32-x64",
|
|
22
|
+
nativeName: "tarve_native.dll",
|
|
23
|
+
executableSuffix: ".exe"
|
|
24
|
+
},
|
|
25
|
+
"linux-x64": {
|
|
26
|
+
platform: "linux",
|
|
27
|
+
arch: "x64",
|
|
28
|
+
bunTarget: "bun-linux-x64",
|
|
29
|
+
nativeDirectory: "linux-x64",
|
|
30
|
+
nativeName: "libtarve_native.so",
|
|
31
|
+
executableSuffix: ""
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
function isBuildTarget(value) {
|
|
35
|
+
return BUILD_TARGETS.includes(value);
|
|
36
|
+
}
|
|
37
|
+
function targetConfig(target) {
|
|
38
|
+
return TARGET_CONFIGS[target];
|
|
39
|
+
}
|
|
40
|
+
function hostBuildTarget(platform = process.platform, arch = process.arch) {
|
|
41
|
+
return BUILD_TARGETS.find((target) => {
|
|
42
|
+
const config = TARGET_CONFIGS[target];
|
|
43
|
+
return config.platform === platform && config.arch === arch;
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// packages/core/native-runtime.ts
|
|
48
|
+
import { createHash } from "crypto";
|
|
49
|
+
import { existsSync } from "fs";
|
|
50
|
+
import { readFile, writeFile } from "fs/promises";
|
|
51
|
+
import { basename, dirname, join } from "path";
|
|
52
|
+
|
|
53
|
+
// packages/protocol/src/index.ts
|
|
54
|
+
var NATIVE_ABI_VERSION = 5;
|
|
55
|
+
var PROTOCOL_VERSION = 45;
|
|
56
|
+
|
|
57
|
+
// packages/core/native-runtime.ts
|
|
58
|
+
var NATIVE_RUNTIME_MANIFEST_NAME = "runtime.json";
|
|
59
|
+
var NATIVE_COMPATIBILITY_MANIFEST_NAME = "native-runtime.json";
|
|
60
|
+
var NATIVE_RUNTIME_MANIFEST_SCHEMA = 1;
|
|
61
|
+
async function sha256File(path) {
|
|
62
|
+
return createHash("sha256").update(await readFile(path)).digest("hex");
|
|
63
|
+
}
|
|
64
|
+
async function addGlobFiles(files, root, prefix, pattern) {
|
|
65
|
+
const directory = join(root, "native", prefix);
|
|
66
|
+
if (!existsSync(directory))
|
|
67
|
+
return;
|
|
68
|
+
for await (const relative of new Bun.Glob(pattern).scan({ cwd: directory, onlyFiles: true })) {
|
|
69
|
+
files.push(join(prefix, relative).replaceAll("\\", "/"));
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
async function nativeSourceFingerprint(root) {
|
|
73
|
+
const native = join(root, "native");
|
|
74
|
+
const files = ["Cargo.toml", "Cargo.lock"];
|
|
75
|
+
await addGlobFiles(files, root, "src", "**/*.rs");
|
|
76
|
+
await addGlobFiles(files, root, "vendor", "**/*.rs");
|
|
77
|
+
await addGlobFiles(files, root, "vendor", "**/Cargo.toml");
|
|
78
|
+
files.sort();
|
|
79
|
+
const hash = createHash("sha256");
|
|
80
|
+
for (const relative of files) {
|
|
81
|
+
const path = join(native, ...relative.split("/"));
|
|
82
|
+
if (!existsSync(path))
|
|
83
|
+
throw new Error(`Native source input is missing: ${path}`);
|
|
84
|
+
hash.update(relative);
|
|
85
|
+
hash.update("\x00");
|
|
86
|
+
hash.update((await readFile(path, "utf8")).replace(/\r\n?/g, `
|
|
87
|
+
`));
|
|
88
|
+
hash.update("\x00");
|
|
89
|
+
}
|
|
90
|
+
return hash.digest("hex");
|
|
91
|
+
}
|
|
92
|
+
async function createNativeCompatibilityManifest(root, packageVersion) {
|
|
93
|
+
const [rustProtocol, rustBridge] = await Promise.all([
|
|
94
|
+
readFile(join(root, "native", "src", "protocol.rs"), "utf8"),
|
|
95
|
+
readFile(join(root, "native", "src", "bridge.rs"), "utf8")
|
|
96
|
+
]);
|
|
97
|
+
const rustProtocolVersion = Number(rustProtocol.match(/pub const VERSION: u32 = (\d+);/)?.[1]);
|
|
98
|
+
const rustAbiVersion = Number(rustBridge.match(/pub const ABI_VERSION: u32 = (\d+);/)?.[1]);
|
|
99
|
+
if (rustProtocolVersion !== PROTOCOL_VERSION) {
|
|
100
|
+
throw new Error(`Tarve protocol source mismatch: TypeScript=${PROTOCOL_VERSION}, Rust=${Number.isFinite(rustProtocolVersion) ? rustProtocolVersion : "invalid"}`);
|
|
101
|
+
}
|
|
102
|
+
if (rustAbiVersion !== NATIVE_ABI_VERSION) {
|
|
103
|
+
throw new Error(`Tarve native ABI source mismatch: TypeScript=${NATIVE_ABI_VERSION}, Rust=${Number.isFinite(rustAbiVersion) ? rustAbiVersion : "invalid"}`);
|
|
104
|
+
}
|
|
105
|
+
return {
|
|
106
|
+
schemaVersion: NATIVE_RUNTIME_MANIFEST_SCHEMA,
|
|
107
|
+
packageVersion,
|
|
108
|
+
protocolVersion: PROTOCOL_VERSION,
|
|
109
|
+
nativeAbiVersion: NATIVE_ABI_VERSION,
|
|
110
|
+
sourceFingerprint: await nativeSourceFingerprint(root)
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
function validateCompatibilityShape(value, path) {
|
|
114
|
+
if (!value || typeof value !== "object")
|
|
115
|
+
throw new Error(`Invalid Tarve native compatibility metadata: ${path}`);
|
|
116
|
+
const manifest = value;
|
|
117
|
+
if (manifest.schemaVersion !== NATIVE_RUNTIME_MANIFEST_SCHEMA || typeof manifest.packageVersion !== "string" || !Number.isSafeInteger(manifest.protocolVersion) || !Number.isSafeInteger(manifest.nativeAbiVersion) || typeof manifest.sourceFingerprint !== "string" || !/^[0-9a-f]{64}$/.test(manifest.sourceFingerprint)) {
|
|
118
|
+
throw new Error(`Invalid Tarve native compatibility metadata: ${path}`);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
async function readNativeCompatibilityManifest(path) {
|
|
122
|
+
if (!existsSync(path))
|
|
123
|
+
throw new Error(`Tarve native compatibility metadata is missing: ${path}`);
|
|
124
|
+
let value;
|
|
125
|
+
try {
|
|
126
|
+
value = JSON.parse(await readFile(path, "utf8"));
|
|
127
|
+
} catch (error) {
|
|
128
|
+
throw new Error(`Could not read Tarve native compatibility metadata: ${path}: ${error instanceof Error ? error.message : String(error)}`);
|
|
129
|
+
}
|
|
130
|
+
validateCompatibilityShape(value, path);
|
|
131
|
+
return value;
|
|
132
|
+
}
|
|
133
|
+
async function assertNativeRuntimeCompatible(library, target, expected) {
|
|
134
|
+
const path = join(dirname(library), NATIVE_RUNTIME_MANIFEST_NAME);
|
|
135
|
+
if (!existsSync(path)) {
|
|
136
|
+
throw new Error(`Tarve native runtime metadata missing for ${target}: ${path}. ` + "Rebuild/stage this target with `bun run package:native` on its native OS before cross-compiling.");
|
|
137
|
+
}
|
|
138
|
+
let value;
|
|
139
|
+
try {
|
|
140
|
+
value = JSON.parse(await readFile(path, "utf8"));
|
|
141
|
+
} catch (error) {
|
|
142
|
+
throw new Error(`Could not read Tarve native runtime metadata for ${target}: ${error instanceof Error ? error.message : String(error)}`);
|
|
143
|
+
}
|
|
144
|
+
validateCompatibilityShape(value, path);
|
|
145
|
+
const manifest = value;
|
|
146
|
+
const config = targetConfig(target);
|
|
147
|
+
if (manifest.target !== target || manifest.nativeName !== config.nativeName || typeof manifest.sha256 !== "string") {
|
|
148
|
+
throw new Error(`Invalid Tarve native runtime metadata for ${target}: ${path}`);
|
|
149
|
+
}
|
|
150
|
+
const stale = manifest.packageVersion !== expected.packageVersion || manifest.protocolVersion !== expected.protocolVersion || manifest.nativeAbiVersion !== expected.nativeAbiVersion || manifest.sourceFingerprint !== expected.sourceFingerprint;
|
|
151
|
+
if (stale) {
|
|
152
|
+
throw new Error(`Tarve native runtime for ${target} is stale or incompatible ` + `(runtime protocol ${manifest.protocolVersion}, expected ${expected.protocolVersion}). ` + "Rebuild it on its native OS with `bun run package:native` before cross-compiling.");
|
|
153
|
+
}
|
|
154
|
+
const hash = await sha256File(library);
|
|
155
|
+
if (manifest.sha256 !== hash) {
|
|
156
|
+
throw new Error(`Tarve native runtime hash mismatch for ${target}: ${library}. ` + "Rebuild/stage the runtime with `bun run package:native`.");
|
|
157
|
+
}
|
|
158
|
+
return manifest;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// packages/core/build.ts
|
|
162
|
+
function resolveNativeLibrary(target, override) {
|
|
163
|
+
if (override) {
|
|
164
|
+
const path = resolve(override);
|
|
165
|
+
if (!existsSync2(path))
|
|
166
|
+
throw new Error(`Tarve native library does not exist: ${path}`);
|
|
167
|
+
return path;
|
|
168
|
+
}
|
|
169
|
+
const config = targetConfig(target);
|
|
170
|
+
const hostTarget = hostBuildTarget();
|
|
171
|
+
if (hostTarget === target) {
|
|
172
|
+
try {
|
|
173
|
+
return nativePath();
|
|
174
|
+
} catch {}
|
|
175
|
+
}
|
|
176
|
+
const candidates = [
|
|
177
|
+
join2(import.meta.dir, config.nativeName),
|
|
178
|
+
resolve(import.meta.dir, "../../native", config.nativeDirectory, config.nativeName)
|
|
179
|
+
];
|
|
180
|
+
const found = candidates.find(existsSync2);
|
|
181
|
+
if (found)
|
|
182
|
+
return found;
|
|
183
|
+
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.");
|
|
184
|
+
}
|
|
185
|
+
async function build(options) {
|
|
186
|
+
const target = options.target ?? hostBuildTarget();
|
|
187
|
+
if (!target)
|
|
188
|
+
throw new Error(`Tarve executable distribution is not supported for ${process.platform}-${process.arch}. Pass an explicit supported target when cross-compiling.`);
|
|
189
|
+
if (!isBuildTarget(target))
|
|
190
|
+
throw new TypeError(`Unsupported Tarve build target: ${String(target)}. Expected ${BUILD_TARGETS.join(" or ")}.`);
|
|
191
|
+
const config = targetConfig(target);
|
|
192
|
+
const entrypoint = resolve(options.entrypoint);
|
|
193
|
+
const packageMetadata = await Bun.file(resolve(import.meta.dir, "../../package.json")).json();
|
|
194
|
+
if (typeof packageMetadata.version !== "string" || packageMetadata.version.length === 0) {
|
|
195
|
+
throw new Error("Tarve package version is missing.");
|
|
196
|
+
}
|
|
197
|
+
const name = options.name ?? basename2(entrypoint).replace(/\.[^.]+$/, "");
|
|
198
|
+
const outfile = resolve(options.outfile ?? `dist/${name}${config.executableSuffix}`);
|
|
199
|
+
const library = resolveNativeLibrary(target, options.nativeLibrary);
|
|
200
|
+
const assetsModule = fileURLToPath(import.meta.resolve("#tarve/assets"));
|
|
201
|
+
const sourceDirectory = join2(import.meta.dir, "src");
|
|
202
|
+
const repositorySource = existsSync2(join2(sourceDirectory, "index.ts"));
|
|
203
|
+
const expectedCompatibility = repositorySource ? await createNativeCompatibilityManifest(resolve(import.meta.dir, "../.."), packageMetadata.version) : await readNativeCompatibilityManifest(join2(import.meta.dir, NATIVE_COMPATIBILITY_MANIFEST_NAME));
|
|
204
|
+
const requireRuntimeManifest = target !== hostBuildTarget() || !options.nativeLibrary && !repositorySource;
|
|
205
|
+
if (requireRuntimeManifest) {
|
|
206
|
+
await assertNativeRuntimeCompatible(library, target, expectedCompatibility);
|
|
207
|
+
}
|
|
208
|
+
const runtime = {
|
|
209
|
+
name: "tarve-native-runtime",
|
|
210
|
+
setup(builder) {
|
|
211
|
+
if (repositorySource) {
|
|
212
|
+
builder.onResolve({ filter: /^tarve$/ }, () => ({ path: join2(sourceDirectory, "index.ts") }));
|
|
213
|
+
builder.onResolve({ filter: /^tarve\/jsx-runtime$/ }, () => ({ path: join2(sourceDirectory, "jsx-runtime.ts") }));
|
|
214
|
+
builder.onResolve({ filter: /^tarve\/jsx-dev-runtime$/ }, () => ({ path: join2(sourceDirectory, "jsx-dev-runtime.ts") }));
|
|
215
|
+
}
|
|
216
|
+
builder.onResolve({ filter: /^#tarve\/runtime$/ }, () => ({ path: "runtime", namespace: "tarve" }));
|
|
217
|
+
builder.onLoad({ filter: /^runtime$/, namespace: "tarve" }, () => ({
|
|
218
|
+
loader: "ts",
|
|
219
|
+
contents: [
|
|
220
|
+
`import library from ${JSON.stringify(library)} with { type: "file" };`,
|
|
221
|
+
`import { materializeAsset } from ${JSON.stringify(assetsModule)};`,
|
|
222
|
+
`export function nativePath() { return materializeAsset(library, ${JSON.stringify(config.nativeName)}); }`
|
|
223
|
+
].join(`
|
|
224
|
+
`)
|
|
225
|
+
}));
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
await mkdir(dirname2(outfile), { recursive: true });
|
|
229
|
+
const result = await Bun.build({
|
|
230
|
+
entrypoints: [entrypoint],
|
|
231
|
+
target: "bun",
|
|
232
|
+
plugins: [runtime],
|
|
233
|
+
define: { "process.env.NODE_ENV": JSON.stringify("production") },
|
|
234
|
+
minify: true,
|
|
235
|
+
compile: {
|
|
236
|
+
target: config.bunTarget,
|
|
237
|
+
outfile,
|
|
238
|
+
autoloadDotenv: false,
|
|
239
|
+
autoloadBunfig: false,
|
|
240
|
+
...config.platform === "win32" ? {
|
|
241
|
+
windows: { hideConsole: true, title: name, version: options.version ?? packageMetadata.version, description: `${name} \u2014 native Tarve application` }
|
|
242
|
+
} : {}
|
|
243
|
+
}
|
|
244
|
+
});
|
|
245
|
+
if (!result.success)
|
|
246
|
+
throw new AggregateError(result.logs, "Could not compile the Tarve application");
|
|
247
|
+
return outfile;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// packages/core/cli.ts
|
|
251
|
+
async function main() {
|
|
252
|
+
try {
|
|
253
|
+
const { values, positionals } = parseArgs({
|
|
254
|
+
args: process.argv.slice(2),
|
|
255
|
+
allowPositionals: true,
|
|
256
|
+
options: {
|
|
257
|
+
outfile: { type: "string" },
|
|
258
|
+
name: { type: "string" },
|
|
259
|
+
version: { type: "string" },
|
|
260
|
+
target: { type: "string" },
|
|
261
|
+
help: { type: "boolean", short: "h" }
|
|
262
|
+
}
|
|
263
|
+
});
|
|
264
|
+
if (values.help || positionals.length === 0) {
|
|
265
|
+
const defaultTarget = values.target && isBuildTarget(values.target) ? values.target : hostBuildTarget();
|
|
266
|
+
const defaultOutput = `dist/App${defaultTarget ? targetConfig(defaultTarget).executableSuffix : ""}`;
|
|
267
|
+
console.log(`Usage: tarve build <app.tsx> [--target ${BUILD_TARGETS.join("|")}] [--outfile ${defaultOutput}] [--name App] [--version 1.0.0]`);
|
|
268
|
+
} else if (positionals[0] === "build" && positionals.length === 2) {
|
|
269
|
+
if (values.target !== undefined && !isBuildTarget(values.target)) {
|
|
270
|
+
throw new Error(`Unsupported target: ${values.target}. Expected ${BUILD_TARGETS.join(" or ")}.`);
|
|
271
|
+
}
|
|
272
|
+
console.log(await build({
|
|
273
|
+
entrypoint: positionals[1],
|
|
274
|
+
...values,
|
|
275
|
+
target: values.target
|
|
276
|
+
}));
|
|
277
|
+
} else {
|
|
278
|
+
throw new Error("Expected: tarve build <app.tsx>. Use --help for options.");
|
|
279
|
+
}
|
|
280
|
+
} catch (error) {
|
|
281
|
+
console.error(`error: ${error instanceof Error ? error.message : String(error)}`);
|
|
282
|
+
process.exitCode = 1;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
await main();
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// packages/core/src/embedded-assets.ts
|
|
3
|
+
import { createHash, randomUUID } from "crypto";
|
|
4
|
+
import { existsSync, mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from "fs";
|
|
5
|
+
import { tmpdir } from "os";
|
|
6
|
+
import { basename, dirname, isAbsolute, join, resolve } from "path";
|
|
7
|
+
var materialized = new Map;
|
|
8
|
+
function nativeAssetPath(source) {
|
|
9
|
+
const path = isAbsolute(source) ? source : resolve(dirname(Bun.main), source);
|
|
10
|
+
const normalized = path.replaceAll("\\", "/");
|
|
11
|
+
if (normalized.startsWith("/$bunfs/") || /^[a-z]:\/~BUN\//i.test(normalized)) {
|
|
12
|
+
return materializeAsset(path, basename(path));
|
|
13
|
+
}
|
|
14
|
+
return path;
|
|
15
|
+
}
|
|
16
|
+
function materializeAsset(source, filename) {
|
|
17
|
+
if (!filename || filename === "." || filename === ".." || basename(filename) !== filename) {
|
|
18
|
+
throw new Error("An embedded asset filename must be a simple filename");
|
|
19
|
+
}
|
|
20
|
+
const key = `${source}\x00${filename}`;
|
|
21
|
+
const cached = materialized.get(key);
|
|
22
|
+
if (cached)
|
|
23
|
+
return cached;
|
|
24
|
+
const bytes = readFileSync(source);
|
|
25
|
+
const hash = createHash("sha256").update(bytes).digest("hex");
|
|
26
|
+
const directory = join(tmpdir(), "tarve-assets", hash);
|
|
27
|
+
const path = join(directory, filename);
|
|
28
|
+
mkdirSync(directory, { recursive: true });
|
|
29
|
+
if (!existsSync(path)) {
|
|
30
|
+
const temporary = join(directory, `${filename}.${randomUUID()}.tmp`);
|
|
31
|
+
try {
|
|
32
|
+
writeFileSync(temporary, bytes, { flag: "wx" });
|
|
33
|
+
try {
|
|
34
|
+
renameSync(temporary, path);
|
|
35
|
+
} catch (error) {
|
|
36
|
+
if (!existsSync(path))
|
|
37
|
+
throw error;
|
|
38
|
+
}
|
|
39
|
+
} finally {
|
|
40
|
+
rmSync(temporary, { force: true });
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
const actual = createHash("sha256").update(readFileSync(path)).digest("hex");
|
|
44
|
+
if (actual !== hash)
|
|
45
|
+
throw new Error(`Embedded asset cache is corrupt. Remove this file and retry: ${path}`);
|
|
46
|
+
materialized.set(key, path);
|
|
47
|
+
return path;
|
|
48
|
+
}
|
|
49
|
+
export {
|
|
50
|
+
materializeAsset,
|
|
51
|
+
nativeAssetPath
|
|
52
|
+
};
|