@tarve/core 0.4.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 +126 -1
- package/README.md +121 -12
- package/dist/npm/build.js +1401 -85
- package/dist/npm/cli.js +1940 -117
- package/dist/npm/embedded-assets.js +22 -3
- package/dist/npm/index.js +5274 -975
- 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 +1159 -0
- package/dist/npm/protocol.js +1 -1
- package/dist/npm/runtime.js +5 -5
- 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 +24 -0
- package/dist/types/core/cli.d.ts +1 -1
- package/dist/types/core/esbuild-file-imports.d.ts +8 -0
- package/dist/types/core/jsx-compile.d.ts +7 -0
- package/dist/types/core/node-build.d.ts +7 -0
- package/dist/types/core/node-dev.d.ts +4 -0
- package/dist/types/core/node-target.d.ts +10 -0
- package/dist/types/core/package-metadata.d.ts +49 -0
- package/dist/types/core/runtime-select.d.ts +8 -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 +18 -0
- package/dist/types/core/src/app.d.ts +89 -2
- package/dist/types/core/src/assets.d.ts +4 -0
- package/dist/types/core/src/bridge/binding.d.ts +10 -34
- package/dist/types/core/src/bridge/index.d.ts +1 -1
- package/dist/types/core/src/canvas.d.ts +161 -0
- 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 +8 -4
- package/dist/types/core/src/components/layout.d.ts +5 -4
- package/dist/types/core/src/components/pressable.d.ts +3 -3
- 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 +2 -2
- package/dist/types/core/src/form-controls.d.ts +1 -1
- package/dist/types/core/src/html-elements.d.ts +26 -0
- package/dist/types/core/src/index.d.ts +23 -11
- package/dist/types/core/src/inline-text.d.ts +25 -0
- package/dist/types/core/src/intrinsic-style.d.ts +1 -1
- package/dist/types/core/src/jsx-runtime.d.ts +182 -5
- package/dist/types/core/src/media-element.d.ts +70 -0
- package/dist/types/core/src/memo.d.ts +28 -0
- package/dist/types/core/src/motion.d.ts +1 -1
- package/dist/types/core/src/png.d.ts +29 -0
- package/dist/types/core/src/popups.d.ts +4 -3
- 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 +33 -10
- 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 +3 -1
- package/dist/types/core/src/text-search.d.ts +1 -1
- package/dist/types/core/src/theme.d.ts +10 -2
- package/dist/types/core/src/updates.d.ts +87 -0
- package/dist/types/core/src/virtual-list.d.ts +8 -2
- 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/core/windows-executable.d.ts +16 -0
- package/dist/types/protocol/src/index.d.ts +344 -11
- 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 +13 -3
|
@@ -0,0 +1,1159 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __returnValue = (v) => v;
|
|
3
|
+
function __exportSetter(name, newValue) {
|
|
4
|
+
this[name] = __returnValue.bind(null, newValue);
|
|
5
|
+
}
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, {
|
|
9
|
+
get: all[name],
|
|
10
|
+
enumerable: true,
|
|
11
|
+
configurable: true,
|
|
12
|
+
set: __exportSetter.bind(all, name)
|
|
13
|
+
});
|
|
14
|
+
};
|
|
15
|
+
var __esm = (fn, res, err) => () => {
|
|
16
|
+
if (fn)
|
|
17
|
+
try {
|
|
18
|
+
res = fn(fn = 0);
|
|
19
|
+
} catch (e) {
|
|
20
|
+
err = [e];
|
|
21
|
+
}
|
|
22
|
+
if (err)
|
|
23
|
+
throw err[0];
|
|
24
|
+
return res;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
// packages/core/esbuild-file-imports.ts
|
|
28
|
+
import { readFileSync } from "node:fs";
|
|
29
|
+
import { dirname, resolve } from "node:path";
|
|
30
|
+
var ASSET, fileImportAttributes;
|
|
31
|
+
var init_esbuild_file_imports = __esm(() => {
|
|
32
|
+
ASSET = /\.(?:png|jpe?g|webp|gif|svg)$/i;
|
|
33
|
+
fileImportAttributes = {
|
|
34
|
+
name: "tarve-file-import-attributes",
|
|
35
|
+
setup(build) {
|
|
36
|
+
build.onResolve({ filter: /.*/ }, (args) => {
|
|
37
|
+
if (args.namespace === "tarve-file") {
|
|
38
|
+
return args.path.startsWith("node:") ? { path: args.path, external: true } : { path: args.path, namespace: "tarve-file-copy" };
|
|
39
|
+
}
|
|
40
|
+
const type = args.with?.type ?? (ASSET.test(args.path) && args.kind === "import-statement" ? "file" : undefined);
|
|
41
|
+
if (type !== "file" && type !== "text")
|
|
42
|
+
return;
|
|
43
|
+
return { path: resolve(dirname(args.importer), args.path), namespace: `tarve-${type}` };
|
|
44
|
+
});
|
|
45
|
+
build.onLoad({ filter: /.*/, namespace: "tarve-text" }, (args) => ({ contents: readFileSync(args.path, "utf8"), loader: "text" }));
|
|
46
|
+
build.onLoad({ filter: /.*/, namespace: "tarve-file" }, (args) => ({
|
|
47
|
+
contents: [
|
|
48
|
+
`import relative from ${JSON.stringify(args.path)};`,
|
|
49
|
+
`import { join } from "node:path";`,
|
|
50
|
+
`export default join(import.meta.dirname, relative);`
|
|
51
|
+
].join(`
|
|
52
|
+
`),
|
|
53
|
+
loader: "js",
|
|
54
|
+
resolveDir: dirname(args.path)
|
|
55
|
+
}));
|
|
56
|
+
build.onLoad({ filter: /.*/, namespace: "tarve-file-copy" }, (args) => ({
|
|
57
|
+
contents: readFileSync(args.path),
|
|
58
|
+
loader: "file"
|
|
59
|
+
}));
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
// packages/core/targets.ts
|
|
65
|
+
function targetConfig(target) {
|
|
66
|
+
return TARGET_CONFIGS[target];
|
|
67
|
+
}
|
|
68
|
+
function hostBuildTarget(platform = process.platform, arch = process.arch) {
|
|
69
|
+
return BUILD_TARGETS.find((target) => {
|
|
70
|
+
const config = TARGET_CONFIGS[target];
|
|
71
|
+
return config.platform === platform && config.arch === arch;
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
var BUILD_TARGETS, TARGET_CONFIGS;
|
|
75
|
+
var init_targets = __esm(() => {
|
|
76
|
+
BUILD_TARGETS = ["windows-x64", "linux-x64"];
|
|
77
|
+
TARGET_CONFIGS = {
|
|
78
|
+
"windows-x64": {
|
|
79
|
+
platform: "win32",
|
|
80
|
+
arch: "x64",
|
|
81
|
+
bunTarget: "bun-windows-x64",
|
|
82
|
+
nativeDirectory: "win32-x64",
|
|
83
|
+
nativeName: "tarve_native.dll",
|
|
84
|
+
executableSuffix: ".exe"
|
|
85
|
+
},
|
|
86
|
+
"linux-x64": {
|
|
87
|
+
platform: "linux",
|
|
88
|
+
arch: "x64",
|
|
89
|
+
bunTarget: "bun-linux-x64",
|
|
90
|
+
nativeDirectory: "linux-x64",
|
|
91
|
+
nativeName: "libtarve_native.so",
|
|
92
|
+
executableSuffix: ""
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
// packages/core/src/png.ts
|
|
98
|
+
import { deflateSync, inflateSync } from "node:zlib";
|
|
99
|
+
function isPng(bytes) {
|
|
100
|
+
return SIGNATURE.every((value, index) => bytes[index] === value);
|
|
101
|
+
}
|
|
102
|
+
function decodePng(bytes) {
|
|
103
|
+
if (!isPng(bytes))
|
|
104
|
+
throw new Error("not a PNG file");
|
|
105
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
106
|
+
let offset = 8;
|
|
107
|
+
let width = 0, height = 0, depth = 0, colour = 0, interlace = 0;
|
|
108
|
+
let palette;
|
|
109
|
+
let transparency;
|
|
110
|
+
const idat = [];
|
|
111
|
+
while (offset + 12 <= bytes.length) {
|
|
112
|
+
const length = view.getUint32(offset);
|
|
113
|
+
const type = String.fromCharCode(...bytes.subarray(offset + 4, offset + 8));
|
|
114
|
+
const data = bytes.subarray(offset + 8, offset + 8 + length);
|
|
115
|
+
if (offset + 12 + length > bytes.length)
|
|
116
|
+
throw new Error("PNG chunk runs past the end of the file");
|
|
117
|
+
if (type === "IHDR") {
|
|
118
|
+
width = view.getUint32(offset + 8);
|
|
119
|
+
height = view.getUint32(offset + 12);
|
|
120
|
+
depth = data[8];
|
|
121
|
+
colour = data[9];
|
|
122
|
+
interlace = data[12];
|
|
123
|
+
} else if (type === "PLTE")
|
|
124
|
+
palette = data;
|
|
125
|
+
else if (type === "tRNS")
|
|
126
|
+
transparency = data;
|
|
127
|
+
else if (type === "IDAT")
|
|
128
|
+
idat.push(data);
|
|
129
|
+
else if (type === "IEND")
|
|
130
|
+
break;
|
|
131
|
+
offset += 12 + length;
|
|
132
|
+
}
|
|
133
|
+
if (width <= 0 || height <= 0 || idat.length === 0)
|
|
134
|
+
throw new Error("PNG has no image data");
|
|
135
|
+
if (width * height > 16384 * 16384)
|
|
136
|
+
throw new Error("PNG is too large");
|
|
137
|
+
if (interlace !== 0)
|
|
138
|
+
throw new Error("interlaced PNGs are not supported; save it without interlacing");
|
|
139
|
+
const channels = { 0: 1, 2: 3, 3: 1, 4: 2, 6: 4 }[colour];
|
|
140
|
+
if (channels === undefined || ![1, 2, 4, 8, 16].includes(depth) || depth < 8 && colour !== 0 && colour !== 3 || depth === 16 && colour === 3) {
|
|
141
|
+
throw new Error(`unsupported PNG format (colour type ${colour}, bit depth ${depth})`);
|
|
142
|
+
}
|
|
143
|
+
const bitsPerPixel = channels * depth;
|
|
144
|
+
const bytesPerPixel = Math.max(1, bitsPerPixel >> 3);
|
|
145
|
+
const stride = Math.ceil(width * bitsPerPixel / 8);
|
|
146
|
+
const raw = inflateSync(Buffer.concat(idat.map((chunk) => Buffer.from(chunk))));
|
|
147
|
+
if (raw.length < height * (stride + 1))
|
|
148
|
+
throw new Error("PNG image data is truncated");
|
|
149
|
+
const lines = new Uint8Array(height * stride);
|
|
150
|
+
for (let y = 0;y < height; y++) {
|
|
151
|
+
const filter = raw[y * (stride + 1)];
|
|
152
|
+
const source = y * (stride + 1) + 1;
|
|
153
|
+
const target = y * stride;
|
|
154
|
+
for (let x = 0;x < stride; x++) {
|
|
155
|
+
const encoded = raw[source + x];
|
|
156
|
+
const left = x >= bytesPerPixel ? lines[target + x - bytesPerPixel] : 0;
|
|
157
|
+
const up = y > 0 ? lines[target - stride + x] : 0;
|
|
158
|
+
const upLeft = y > 0 && x >= bytesPerPixel ? lines[target - stride + x - bytesPerPixel] : 0;
|
|
159
|
+
let predictor = 0;
|
|
160
|
+
if (filter === 1)
|
|
161
|
+
predictor = left;
|
|
162
|
+
else if (filter === 2)
|
|
163
|
+
predictor = up;
|
|
164
|
+
else if (filter === 3)
|
|
165
|
+
predictor = left + up >> 1;
|
|
166
|
+
else if (filter === 4) {
|
|
167
|
+
const p = left + up - upLeft;
|
|
168
|
+
const pa = Math.abs(p - left), pb = Math.abs(p - up), pc = Math.abs(p - upLeft);
|
|
169
|
+
predictor = pa <= pb && pa <= pc ? left : pb <= pc ? up : upLeft;
|
|
170
|
+
} else if (filter !== 0)
|
|
171
|
+
throw new Error(`unsupported PNG filter ${filter}`);
|
|
172
|
+
lines[target + x] = encoded + predictor & 255;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
const sample = (y, index) => {
|
|
176
|
+
const row = y * stride;
|
|
177
|
+
if (depth === 8)
|
|
178
|
+
return lines[row + index];
|
|
179
|
+
if (depth === 16)
|
|
180
|
+
return lines[row + index * 2];
|
|
181
|
+
const bit = index * depth;
|
|
182
|
+
return lines[row + (bit >> 3)] >> 8 - depth - (bit & 7) & (1 << depth) - 1;
|
|
183
|
+
};
|
|
184
|
+
const scale = depth < 8 && colour === 0 ? 255 / ((1 << depth) - 1) : 1;
|
|
185
|
+
const rgba = new Uint8Array(width * height * 4);
|
|
186
|
+
for (let y = 0;y < height; y++) {
|
|
187
|
+
for (let x = 0;x < width; x++) {
|
|
188
|
+
const out = (y * width + x) * 4;
|
|
189
|
+
const base = x * channels;
|
|
190
|
+
if (colour === 3) {
|
|
191
|
+
const index = sample(y, base);
|
|
192
|
+
rgba[out] = palette?.[index * 3] ?? 0;
|
|
193
|
+
rgba[out + 1] = palette?.[index * 3 + 1] ?? 0;
|
|
194
|
+
rgba[out + 2] = palette?.[index * 3 + 2] ?? 0;
|
|
195
|
+
rgba[out + 3] = transparency?.[index] ?? 255;
|
|
196
|
+
} else if (colour === 0 || colour === 4) {
|
|
197
|
+
const grey = Math.round(sample(y, base) * scale);
|
|
198
|
+
rgba[out] = rgba[out + 1] = rgba[out + 2] = grey;
|
|
199
|
+
rgba[out + 3] = colour === 4 ? sample(y, base + 1) : 255;
|
|
200
|
+
} else {
|
|
201
|
+
rgba[out] = sample(y, base);
|
|
202
|
+
rgba[out + 1] = sample(y, base + 1);
|
|
203
|
+
rgba[out + 2] = sample(y, base + 2);
|
|
204
|
+
rgba[out + 3] = colour === 6 ? sample(y, base + 3) : 255;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
return { width, height, rgba };
|
|
209
|
+
}
|
|
210
|
+
function crc32(bytes) {
|
|
211
|
+
let crc = 4294967295;
|
|
212
|
+
for (const byte of bytes)
|
|
213
|
+
crc = CRC_TABLE[(crc ^ byte) & 255] ^ crc >>> 8;
|
|
214
|
+
return (crc ^ 4294967295) >>> 0;
|
|
215
|
+
}
|
|
216
|
+
function chunk(type, data) {
|
|
217
|
+
const out = Buffer.alloc(12 + data.length);
|
|
218
|
+
out.writeUInt32BE(data.length, 0);
|
|
219
|
+
out.write(type, 4, "ascii");
|
|
220
|
+
out.set(data, 8);
|
|
221
|
+
out.writeUInt32BE(crc32(out.subarray(4, 8 + data.length)), 8 + data.length);
|
|
222
|
+
return out;
|
|
223
|
+
}
|
|
224
|
+
function encodePng(rgba, width, height) {
|
|
225
|
+
const stride = width * 4;
|
|
226
|
+
const raw = Buffer.alloc((stride + 1) * height);
|
|
227
|
+
for (let y = 0;y < height; y++)
|
|
228
|
+
raw.set(rgba.subarray(y * stride, (y + 1) * stride), y * (stride + 1) + 1);
|
|
229
|
+
const header = Buffer.alloc(13);
|
|
230
|
+
header.writeUInt32BE(width, 0);
|
|
231
|
+
header.writeUInt32BE(height, 4);
|
|
232
|
+
header[8] = 8;
|
|
233
|
+
header[9] = 6;
|
|
234
|
+
return new Uint8Array(Buffer.concat([
|
|
235
|
+
Buffer.from(SIGNATURE),
|
|
236
|
+
chunk("IHDR", header),
|
|
237
|
+
chunk("IDAT", deflateSync(raw, { level: 9 })),
|
|
238
|
+
chunk("IEND", new Uint8Array)
|
|
239
|
+
]));
|
|
240
|
+
}
|
|
241
|
+
function resizeSquare(image, size) {
|
|
242
|
+
const side = Math.max(image.width, image.height);
|
|
243
|
+
const offsetX = (side - image.width) / 2, offsetY = (side - image.height) / 2;
|
|
244
|
+
const out = new Uint8Array(size * size * 4);
|
|
245
|
+
const ratio = side / size;
|
|
246
|
+
for (let y = 0;y < size; y++) {
|
|
247
|
+
const y0 = y * ratio - offsetY, y1 = (y + 1) * ratio - offsetY;
|
|
248
|
+
for (let x = 0;x < size; x++) {
|
|
249
|
+
const x0 = x * ratio - offsetX, x1 = (x + 1) * ratio - offsetX;
|
|
250
|
+
let r = 0, g = 0, b = 0, a = 0;
|
|
251
|
+
for (let sy = Math.max(0, Math.floor(y0));sy < Math.min(image.height, Math.ceil(y1)); sy++) {
|
|
252
|
+
const wy = Math.min(y1, sy + 1) - Math.max(y0, sy);
|
|
253
|
+
for (let sx = Math.max(0, Math.floor(x0));sx < Math.min(image.width, Math.ceil(x1)); sx++) {
|
|
254
|
+
const w = wy * (Math.min(x1, sx + 1) - Math.max(x0, sx));
|
|
255
|
+
const i = (sy * image.width + sx) * 4;
|
|
256
|
+
const alpha = image.rgba[i + 3] * w;
|
|
257
|
+
r += image.rgba[i] * alpha;
|
|
258
|
+
g += image.rgba[i + 1] * alpha;
|
|
259
|
+
b += image.rgba[i + 2] * alpha;
|
|
260
|
+
a += alpha;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
const area = ratio * ratio;
|
|
264
|
+
const o = (y * size + x) * 4;
|
|
265
|
+
if (a > 0) {
|
|
266
|
+
out[o] = Math.round(r / a);
|
|
267
|
+
out[o + 1] = Math.round(g / a);
|
|
268
|
+
out[o + 2] = Math.round(b / a);
|
|
269
|
+
}
|
|
270
|
+
out[o + 3] = Math.round(a / area);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
return out;
|
|
274
|
+
}
|
|
275
|
+
function encodeIco(entries) {
|
|
276
|
+
const header = Buffer.alloc(6 + entries.length * 16);
|
|
277
|
+
header.writeUInt16LE(0, 0);
|
|
278
|
+
header.writeUInt16LE(1, 2);
|
|
279
|
+
header.writeUInt16LE(entries.length, 4);
|
|
280
|
+
let offset = header.length;
|
|
281
|
+
entries.forEach(({ size, png }, index) => {
|
|
282
|
+
const at = 6 + index * 16;
|
|
283
|
+
header[at] = size >= 256 ? 0 : size;
|
|
284
|
+
header[at + 1] = size >= 256 ? 0 : size;
|
|
285
|
+
header.writeUInt16LE(1, at + 4);
|
|
286
|
+
header.writeUInt16LE(32, at + 6);
|
|
287
|
+
header.writeUInt32LE(png.length, at + 8);
|
|
288
|
+
header.writeUInt32LE(offset, at + 12);
|
|
289
|
+
offset += png.length;
|
|
290
|
+
});
|
|
291
|
+
return new Uint8Array(Buffer.concat([header, ...entries.map((entry) => Buffer.from(entry.png))]));
|
|
292
|
+
}
|
|
293
|
+
function isIco(bytes) {
|
|
294
|
+
return bytes.length >= 6 && bytes[0] === 0 && bytes[1] === 0 && bytes[2] === 1 && bytes[3] === 0;
|
|
295
|
+
}
|
|
296
|
+
function largestIcoPng(bytes) {
|
|
297
|
+
if (!isIco(bytes))
|
|
298
|
+
return;
|
|
299
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
300
|
+
let best;
|
|
301
|
+
let bestSize = 0;
|
|
302
|
+
for (let index = 0;index < view.getUint16(4, true); index++) {
|
|
303
|
+
const at = 6 + index * 16;
|
|
304
|
+
if (at + 16 > bytes.length)
|
|
305
|
+
break;
|
|
306
|
+
const size = bytes[at] || 256;
|
|
307
|
+
const length = view.getUint32(at + 8, true), start = view.getUint32(at + 12, true);
|
|
308
|
+
const data = bytes.subarray(start, start + length);
|
|
309
|
+
if (isPng(data) && size > bestSize) {
|
|
310
|
+
best = data;
|
|
311
|
+
bestSize = size;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
return best;
|
|
315
|
+
}
|
|
316
|
+
var SIGNATURE, CRC_TABLE;
|
|
317
|
+
var init_png = __esm(() => {
|
|
318
|
+
SIGNATURE = [137, 80, 78, 71, 13, 10, 26, 10];
|
|
319
|
+
CRC_TABLE = new Uint32Array(256).map((_, n) => {
|
|
320
|
+
let c = n;
|
|
321
|
+
for (let k = 0;k < 8; k++)
|
|
322
|
+
c = c & 1 ? 3988292384 ^ c >>> 1 : c >>> 1;
|
|
323
|
+
return c >>> 0;
|
|
324
|
+
});
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
// packages/core/package-metadata.ts
|
|
328
|
+
import { existsSync, mkdirSync, readFileSync as readFileSync2, writeFileSync } from "node:fs";
|
|
329
|
+
import { basename, dirname as dirname2, extname, join, relative, resolve as resolve2 } from "node:path";
|
|
330
|
+
function fields(value, where) {
|
|
331
|
+
if (value === undefined)
|
|
332
|
+
return {};
|
|
333
|
+
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
334
|
+
throw new TypeError(`${where} must be an object`);
|
|
335
|
+
const out = {};
|
|
336
|
+
for (const key of FIELDS) {
|
|
337
|
+
const field = value[key];
|
|
338
|
+
if (field === undefined)
|
|
339
|
+
continue;
|
|
340
|
+
if (typeof field !== "string" || !field.trim())
|
|
341
|
+
throw new TypeError(`${where}.${key} must be a non-empty string`);
|
|
342
|
+
out[key] = field.trim();
|
|
343
|
+
}
|
|
344
|
+
return out;
|
|
345
|
+
}
|
|
346
|
+
function author(value) {
|
|
347
|
+
if (typeof value === "string")
|
|
348
|
+
return value.replace(/\s*[<(].*$/, "").trim() || undefined;
|
|
349
|
+
if (value && typeof value === "object" && typeof value.name === "string")
|
|
350
|
+
return value.name;
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
353
|
+
function validAppId(id) {
|
|
354
|
+
return id.length > 0 && id.length <= 128 && !/[\s\\]/.test(id);
|
|
355
|
+
}
|
|
356
|
+
function resolveAppMetadata(entrypoint, overrides = {}) {
|
|
357
|
+
const entry = resolve2(entrypoint);
|
|
358
|
+
let directory = dirname2(entry);
|
|
359
|
+
let manifest;
|
|
360
|
+
let root = directory;
|
|
361
|
+
for (;; ) {
|
|
362
|
+
const path = join(directory, "package.json");
|
|
363
|
+
if (existsSync(path)) {
|
|
364
|
+
manifest = JSON.parse(readFileSync2(path, "utf8"));
|
|
365
|
+
root = directory;
|
|
366
|
+
break;
|
|
367
|
+
}
|
|
368
|
+
const parent = dirname2(directory);
|
|
369
|
+
if (parent === directory)
|
|
370
|
+
break;
|
|
371
|
+
directory = parent;
|
|
372
|
+
}
|
|
373
|
+
const tarve = manifest?.tarve;
|
|
374
|
+
const shared = fields(tarve === undefined ? undefined : Object.fromEntries(Object.entries(tarve).filter(([key]) => key !== "apps")), "package.json tarve");
|
|
375
|
+
const apps = tarve?.apps;
|
|
376
|
+
const key = relative(root, entry).replaceAll("\\", "/");
|
|
377
|
+
const own = fields(apps?.[key] ?? apps?.["./" + key], `package.json tarve.apps["${key}"]`);
|
|
378
|
+
const fromPackage = tarve === undefined ? {} : {
|
|
379
|
+
...typeof manifest?.version === "string" ? { version: manifest.version } : {},
|
|
380
|
+
...typeof manifest?.description === "string" ? { description: manifest.description } : {},
|
|
381
|
+
...author(manifest?.author) ? { publisher: author(manifest?.author) } : {}
|
|
382
|
+
};
|
|
383
|
+
const configured = { ...fromPackage, ...shared, ...own };
|
|
384
|
+
if (configured.icon)
|
|
385
|
+
configured.icon = resolve2(root, configured.icon);
|
|
386
|
+
const flags = fields(Object.fromEntries(Object.entries(overrides).filter(([, value]) => value !== undefined)), "option");
|
|
387
|
+
if (flags.icon)
|
|
388
|
+
flags.icon = resolve2(flags.icon);
|
|
389
|
+
const merged = { ...configured, ...flags };
|
|
390
|
+
const metadata = { name: merged.name ?? basename(entry).replace(/\.[^.]+$/, "") };
|
|
391
|
+
for (const field of FIELDS)
|
|
392
|
+
if (field !== "name" && merged[field] !== undefined)
|
|
393
|
+
metadata[field] = merged[field];
|
|
394
|
+
if (metadata.appId !== undefined && !validAppId(metadata.appId)) {
|
|
395
|
+
throw new TypeError(`appId must be 1-128 characters without spaces or backslashes: ${metadata.appId}`);
|
|
396
|
+
}
|
|
397
|
+
if (metadata.icon !== undefined) {
|
|
398
|
+
if (!existsSync(metadata.icon))
|
|
399
|
+
throw new Error(`App icon not found: ${metadata.icon}`);
|
|
400
|
+
if (![".png", ".ico"].includes(extname(metadata.icon).toLowerCase()))
|
|
401
|
+
throw new TypeError(`App icon must be a .png or .ico file: ${metadata.icon}`);
|
|
402
|
+
}
|
|
403
|
+
return metadata;
|
|
404
|
+
}
|
|
405
|
+
function appIconImages(path) {
|
|
406
|
+
const bytes = new Uint8Array(readFileSync2(path));
|
|
407
|
+
if (isIco(bytes)) {
|
|
408
|
+
const png = largestIcoPng(bytes);
|
|
409
|
+
if (!png)
|
|
410
|
+
throw new Error(`${path} has no PNG-compressed image; Linux builds and the window icon need one. Use a .png icon instead.`);
|
|
411
|
+
return { ico: bytes, png };
|
|
412
|
+
}
|
|
413
|
+
if (!isPng(bytes))
|
|
414
|
+
throw new TypeError(`${path} is not a PNG or ICO file`);
|
|
415
|
+
let image;
|
|
416
|
+
try {
|
|
417
|
+
image = decodePng(bytes);
|
|
418
|
+
} catch (error) {
|
|
419
|
+
throw new Error(`App icon ${path}: ${error instanceof Error ? error.message : String(error)}`);
|
|
420
|
+
}
|
|
421
|
+
const side = Math.max(image.width, image.height);
|
|
422
|
+
const sizes = ICO_SIZES.filter((size) => size <= side);
|
|
423
|
+
if (!sizes.length)
|
|
424
|
+
sizes.push(16);
|
|
425
|
+
const entries = sizes.map((size) => ({ size, png: encodePng(resizeSquare(image, size), size, size) }));
|
|
426
|
+
return { ico: encodeIco(entries), png: entries.at(-1).png };
|
|
427
|
+
}
|
|
428
|
+
function buildMetadata(entrypoint, overrides) {
|
|
429
|
+
const metadata = resolveAppMetadata(entrypoint, overrides);
|
|
430
|
+
const images = metadata.icon ? appIconImages(metadata.icon) : undefined;
|
|
431
|
+
const icon = images ? `data:image/png;base64,${Buffer.from(images.png).toString("base64")}` : undefined;
|
|
432
|
+
return { metadata, images, define: { "process.env.TARVE_APP_METADATA": JSON.stringify(runtimeMetadata(metadata, icon)) } };
|
|
433
|
+
}
|
|
434
|
+
function runtimeMetadata(metadata, icon) {
|
|
435
|
+
return JSON.stringify({
|
|
436
|
+
name: metadata.name,
|
|
437
|
+
...metadata.version ? { version: metadata.version } : {},
|
|
438
|
+
...metadata.appId ? { appId: metadata.appId } : {},
|
|
439
|
+
...icon ? { icon } : {}
|
|
440
|
+
});
|
|
441
|
+
}
|
|
442
|
+
function writeLinuxDesktopEntry(outfile, metadata, png) {
|
|
443
|
+
const id = metadata.appId ?? basename(outfile);
|
|
444
|
+
const escape = (value) => value.replace(/[\\\n\r\t]/g, (match) => ({ "\\": "\\\\", "\n": "\\n", "\r": "\\r", "\t": "\\t" })[match]);
|
|
445
|
+
const lines = [
|
|
446
|
+
"[Desktop Entry]",
|
|
447
|
+
"Type=Application",
|
|
448
|
+
`Name=${escape(metadata.name)}`,
|
|
449
|
+
...metadata.description ? [`Comment=${escape(metadata.description)}`] : [],
|
|
450
|
+
`Exec="${outfile.replaceAll('"', "\\\"")}" %U`,
|
|
451
|
+
...png ? [`Icon=${id}`] : [],
|
|
452
|
+
`StartupWMClass=${id}`,
|
|
453
|
+
"Terminal=false",
|
|
454
|
+
""
|
|
455
|
+
];
|
|
456
|
+
mkdirSync(dirname2(outfile), { recursive: true });
|
|
457
|
+
const desktop = join(dirname2(outfile), `${id}.desktop`);
|
|
458
|
+
writeFileSync(desktop, lines.join(`
|
|
459
|
+
`));
|
|
460
|
+
if (png)
|
|
461
|
+
writeFileSync(join(dirname2(outfile), `${id}.png`), png);
|
|
462
|
+
return desktop;
|
|
463
|
+
}
|
|
464
|
+
var FIELDS, ICO_SIZES;
|
|
465
|
+
var init_package_metadata = __esm(() => {
|
|
466
|
+
init_png();
|
|
467
|
+
FIELDS = ["name", "version", "description", "publisher", "copyright", "icon", "appId"];
|
|
468
|
+
ICO_SIZES = [16, 20, 24, 32, 40, 48, 64, 96, 128, 256];
|
|
469
|
+
});
|
|
470
|
+
|
|
471
|
+
// packages/protocol/src/index.ts
|
|
472
|
+
var NATIVE_ABI_VERSION = 5, PROTOCOL_VERSION = 59;
|
|
473
|
+
|
|
474
|
+
// packages/core/native-runtime.ts
|
|
475
|
+
import { createHash } from "node:crypto";
|
|
476
|
+
import { existsSync as existsSync2 } from "node:fs";
|
|
477
|
+
import { readFile, writeFile, glob } from "node:fs/promises";
|
|
478
|
+
import { basename as basename2, dirname as dirname3, join as join2 } from "node:path";
|
|
479
|
+
async function sha256File(path) {
|
|
480
|
+
return createHash("sha256").update(await readFile(path)).digest("hex");
|
|
481
|
+
}
|
|
482
|
+
async function addGlobFiles(files, root, prefix, pattern) {
|
|
483
|
+
const directory = join2(root, "native", prefix);
|
|
484
|
+
if (!existsSync2(directory))
|
|
485
|
+
return;
|
|
486
|
+
for await (const relative of glob(pattern, { cwd: directory })) {
|
|
487
|
+
files.push(join2(prefix, relative).replaceAll("\\", "/"));
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
async function nativeSourceFingerprint(root) {
|
|
491
|
+
const native = join2(root, "native");
|
|
492
|
+
const files = ["Cargo.toml", "Cargo.lock"];
|
|
493
|
+
await addGlobFiles(files, root, "src", "**/*.rs");
|
|
494
|
+
await addGlobFiles(files, root, "vendor", "**/*.rs");
|
|
495
|
+
await addGlobFiles(files, root, "vendor", "**/Cargo.toml");
|
|
496
|
+
files.sort();
|
|
497
|
+
const hash = createHash("sha256");
|
|
498
|
+
for (const relative of files) {
|
|
499
|
+
const path = join2(native, ...relative.split("/"));
|
|
500
|
+
if (!existsSync2(path))
|
|
501
|
+
throw new Error(`Native source input is missing: ${path}`);
|
|
502
|
+
hash.update(relative);
|
|
503
|
+
hash.update("\x00");
|
|
504
|
+
hash.update((await readFile(path, "utf8")).replace(/\r\n?/g, `
|
|
505
|
+
`));
|
|
506
|
+
hash.update("\x00");
|
|
507
|
+
}
|
|
508
|
+
return hash.digest("hex");
|
|
509
|
+
}
|
|
510
|
+
async function createNativeCompatibilityManifest(root, packageVersion) {
|
|
511
|
+
const [rustProtocol, rustBridge] = await Promise.all([
|
|
512
|
+
readFile(join2(root, "native", "src", "protocol.rs"), "utf8"),
|
|
513
|
+
readFile(join2(root, "native", "src", "bridge.rs"), "utf8")
|
|
514
|
+
]);
|
|
515
|
+
const rustProtocolVersion = Number(rustProtocol.match(/pub const VERSION: u32 = (\d+);/)?.[1]);
|
|
516
|
+
const rustAbiVersion = Number(rustBridge.match(/pub const ABI_VERSION: u32 = (\d+);/)?.[1]);
|
|
517
|
+
if (rustProtocolVersion !== PROTOCOL_VERSION) {
|
|
518
|
+
throw new Error(`Tarve protocol source mismatch: TypeScript=${PROTOCOL_VERSION}, Rust=${Number.isFinite(rustProtocolVersion) ? rustProtocolVersion : "invalid"}`);
|
|
519
|
+
}
|
|
520
|
+
if (rustAbiVersion !== NATIVE_ABI_VERSION) {
|
|
521
|
+
throw new Error(`Tarve native ABI source mismatch: TypeScript=${NATIVE_ABI_VERSION}, Rust=${Number.isFinite(rustAbiVersion) ? rustAbiVersion : "invalid"}`);
|
|
522
|
+
}
|
|
523
|
+
return {
|
|
524
|
+
schemaVersion: NATIVE_RUNTIME_MANIFEST_SCHEMA,
|
|
525
|
+
packageVersion,
|
|
526
|
+
protocolVersion: PROTOCOL_VERSION,
|
|
527
|
+
nativeAbiVersion: NATIVE_ABI_VERSION,
|
|
528
|
+
sourceFingerprint: await nativeSourceFingerprint(root)
|
|
529
|
+
};
|
|
530
|
+
}
|
|
531
|
+
function validateCompatibilityShape(value, path) {
|
|
532
|
+
if (!value || typeof value !== "object")
|
|
533
|
+
throw new Error(`Invalid Tarve native compatibility metadata: ${path}`);
|
|
534
|
+
const manifest = value;
|
|
535
|
+
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)) {
|
|
536
|
+
throw new Error(`Invalid Tarve native compatibility metadata: ${path}`);
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
async function readNativeCompatibilityManifest(path) {
|
|
540
|
+
if (!existsSync2(path))
|
|
541
|
+
throw new Error(`Tarve native compatibility metadata is missing: ${path}`);
|
|
542
|
+
let value;
|
|
543
|
+
try {
|
|
544
|
+
value = JSON.parse(await readFile(path, "utf8"));
|
|
545
|
+
} catch (error) {
|
|
546
|
+
throw new Error(`Could not read Tarve native compatibility metadata: ${path}: ${error instanceof Error ? error.message : String(error)}`);
|
|
547
|
+
}
|
|
548
|
+
validateCompatibilityShape(value, path);
|
|
549
|
+
return value;
|
|
550
|
+
}
|
|
551
|
+
async function assertNativeRuntimeCompatible(library, target, expected) {
|
|
552
|
+
const path = join2(dirname3(library), NATIVE_RUNTIME_MANIFEST_NAME);
|
|
553
|
+
if (!existsSync2(path)) {
|
|
554
|
+
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.");
|
|
555
|
+
}
|
|
556
|
+
let value;
|
|
557
|
+
try {
|
|
558
|
+
value = JSON.parse(await readFile(path, "utf8"));
|
|
559
|
+
} catch (error) {
|
|
560
|
+
throw new Error(`Could not read Tarve native runtime metadata for ${target}: ${error instanceof Error ? error.message : String(error)}`);
|
|
561
|
+
}
|
|
562
|
+
validateCompatibilityShape(value, path);
|
|
563
|
+
const manifest = value;
|
|
564
|
+
const config = targetConfig(target);
|
|
565
|
+
if (manifest.target !== target || manifest.nativeName !== config.nativeName || typeof manifest.sha256 !== "string") {
|
|
566
|
+
throw new Error(`Invalid Tarve native runtime metadata for ${target}: ${path}`);
|
|
567
|
+
}
|
|
568
|
+
const stale = manifest.packageVersion !== expected.packageVersion || manifest.protocolVersion !== expected.protocolVersion || manifest.nativeAbiVersion !== expected.nativeAbiVersion || manifest.sourceFingerprint !== expected.sourceFingerprint;
|
|
569
|
+
if (stale) {
|
|
570
|
+
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.");
|
|
571
|
+
}
|
|
572
|
+
const hash = await sha256File(library);
|
|
573
|
+
if (manifest.sha256 !== hash) {
|
|
574
|
+
throw new Error(`Tarve native runtime hash mismatch for ${target}: ${library}. ` + "Rebuild/stage the runtime with `bun run package:native`.");
|
|
575
|
+
}
|
|
576
|
+
return manifest;
|
|
577
|
+
}
|
|
578
|
+
var NATIVE_RUNTIME_MANIFEST_NAME = "runtime.json", NATIVE_COMPATIBILITY_MANIFEST_NAME = "native-runtime.json", NATIVE_RUNTIME_MANIFEST_SCHEMA = 1;
|
|
579
|
+
var init_native_runtime = __esm(() => {
|
|
580
|
+
init_targets();
|
|
581
|
+
});
|
|
582
|
+
|
|
583
|
+
// packages/core/build.ts
|
|
584
|
+
import { nativePath } from "#tarve/runtime";
|
|
585
|
+
function metadataOverrides(options) {
|
|
586
|
+
return { name: options.name, version: options.version, description: options.description, publisher: options.publisher, copyright: options.copyright, icon: options.icon, appId: options.appId };
|
|
587
|
+
}
|
|
588
|
+
var init_build = __esm(() => {
|
|
589
|
+
init_package_metadata();
|
|
590
|
+
init_targets();
|
|
591
|
+
init_native_runtime();
|
|
592
|
+
init_targets();
|
|
593
|
+
});
|
|
594
|
+
|
|
595
|
+
// packages/core/node-target.ts
|
|
596
|
+
import { createHash as createHash2, randomUUID } from "node:crypto";
|
|
597
|
+
import { chmodSync, existsSync as existsSync3, mkdirSync as mkdirSync2, readFileSync as readFileSync3, renameSync, rmSync, writeFileSync as writeFileSync2 } from "node:fs";
|
|
598
|
+
import { homedir } from "node:os";
|
|
599
|
+
import { join as join3 } from "node:path";
|
|
600
|
+
import { gunzipSync } from "node:zlib";
|
|
601
|
+
function distribution(target, version) {
|
|
602
|
+
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" };
|
|
603
|
+
}
|
|
604
|
+
function cacheRoot() {
|
|
605
|
+
if (process.env.TARVE_CACHE_DIR)
|
|
606
|
+
return process.env.TARVE_CACHE_DIR;
|
|
607
|
+
if (process.platform === "win32")
|
|
608
|
+
return join3(process.env.LOCALAPPDATA ?? join3(homedir(), "AppData", "Local"), "tarve", "cache");
|
|
609
|
+
return join3(process.env.XDG_CACHE_HOME ?? join3(homedir(), ".cache"), "tarve");
|
|
610
|
+
}
|
|
611
|
+
function mirror() {
|
|
612
|
+
return (process.env.NODEJS_ORG_MIRROR ?? "https://nodejs.org/dist").replace(/\/+$/, "");
|
|
613
|
+
}
|
|
614
|
+
async function download(url) {
|
|
615
|
+
const response = await fetch(url);
|
|
616
|
+
if (!response.ok)
|
|
617
|
+
throw new Error(`Could not download ${url}: HTTP ${response.status}`);
|
|
618
|
+
return Buffer.from(await response.arrayBuffer());
|
|
619
|
+
}
|
|
620
|
+
function extractTarMember(archive, member) {
|
|
621
|
+
let offset = 0;
|
|
622
|
+
let longName;
|
|
623
|
+
while (offset + 512 <= archive.length) {
|
|
624
|
+
const header = archive.subarray(offset, offset + 512);
|
|
625
|
+
if (header.every((byte) => byte === 0))
|
|
626
|
+
break;
|
|
627
|
+
const field = (start, length) => header.toString("utf8", start, start + length).replace(/\0.*$/s, "");
|
|
628
|
+
const size = Number.parseInt(field(124, 12).trim() || "0", 8);
|
|
629
|
+
const type = field(156, 1) || "0";
|
|
630
|
+
const prefix = field(345, 155);
|
|
631
|
+
const name = longName ?? (prefix ? `${prefix}/${field(0, 100)}` : field(0, 100));
|
|
632
|
+
const body = archive.subarray(offset + 512, offset + 512 + size);
|
|
633
|
+
longName = undefined;
|
|
634
|
+
if (type === "L")
|
|
635
|
+
longName = body.toString("utf8").replace(/\0.*$/s, "");
|
|
636
|
+
else if (type === "x")
|
|
637
|
+
longName = /\d+ path=([^\n]*)\n/.exec(body.toString("utf8"))?.[1];
|
|
638
|
+
else if ((type === "0" || type === "\x00") && name.replace(/^\.\//, "") === member)
|
|
639
|
+
return Buffer.from(body);
|
|
640
|
+
offset += 512 + Math.ceil(size / 512) * 512;
|
|
641
|
+
}
|
|
642
|
+
throw new Error(`${member} not found in the Node.js archive`);
|
|
643
|
+
}
|
|
644
|
+
function assertNodeVersion(executable, version) {
|
|
645
|
+
if (!readFileSync3(executable).includes(`node-v${version}`)) {
|
|
646
|
+
throw new Error(`${executable} is not Node.js v${version}; the target executable must match the Node.js that builds the app`);
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
async function targetNodeExecutable(target, version) {
|
|
650
|
+
const { file, member, executable } = distribution(target, version);
|
|
651
|
+
const directory = join3(cacheRoot(), "node", `v${version}`, target);
|
|
652
|
+
const path = join3(directory, executable);
|
|
653
|
+
if (existsSync3(path))
|
|
654
|
+
return path;
|
|
655
|
+
const base = `${mirror()}/v${version}`;
|
|
656
|
+
const sums = (await download(`${base}/SHASUMS256.txt`)).toString("utf8");
|
|
657
|
+
const expected = sums.split(/\r?\n/).find((line) => line.endsWith(` ${file}`))?.split(/\s+/)[0];
|
|
658
|
+
if (!expected)
|
|
659
|
+
throw new Error(`${file} is not listed in the Node.js v${version} SHASUMS256.txt`);
|
|
660
|
+
const payload = await download(`${base}/${file}`);
|
|
661
|
+
const actual = createHash2("sha256").update(payload).digest("hex");
|
|
662
|
+
if (actual !== expected)
|
|
663
|
+
throw new Error(`Checksum mismatch for ${file}: expected ${expected}, got ${actual}`);
|
|
664
|
+
const binary = member ? extractTarMember(gunzipSync(payload), member) : payload;
|
|
665
|
+
mkdirSync2(directory, { recursive: true });
|
|
666
|
+
const temporary = join3(directory, `${executable}.${randomUUID()}.tmp`);
|
|
667
|
+
try {
|
|
668
|
+
writeFileSync2(temporary, binary, { mode: 493 });
|
|
669
|
+
try {
|
|
670
|
+
renameSync(temporary, path);
|
|
671
|
+
} catch (error) {
|
|
672
|
+
if (!existsSync3(path))
|
|
673
|
+
throw error;
|
|
674
|
+
}
|
|
675
|
+
} finally {
|
|
676
|
+
rmSync(temporary, { force: true });
|
|
677
|
+
}
|
|
678
|
+
if (process.platform !== "win32")
|
|
679
|
+
chmodSync(path, 493);
|
|
680
|
+
return path;
|
|
681
|
+
}
|
|
682
|
+
var init_node_target = () => {};
|
|
683
|
+
|
|
684
|
+
// packages/core/jsx-compile.ts
|
|
685
|
+
var exports_jsx_compile = {};
|
|
686
|
+
__export(exports_jsx_compile, {
|
|
687
|
+
closeJsxCompiler: () => closeJsxCompiler,
|
|
688
|
+
compileJsx: () => compileJsx
|
|
689
|
+
});
|
|
690
|
+
async function loadParser() {
|
|
691
|
+
let modules;
|
|
692
|
+
try {
|
|
693
|
+
modules = await Promise.all([import("typescript/unstable/ast"), import("typescript/unstable/ast/is")]);
|
|
694
|
+
} catch (error) {
|
|
695
|
+
throw new Error(`compileJsx needs TypeScript 7 (npm install -D typescript@7): ${error instanceof Error ? error.message : String(error)}`);
|
|
696
|
+
}
|
|
697
|
+
const [ast, is] = modules;
|
|
698
|
+
const files = new Map([[CONFIG, JSON.stringify({ compilerOptions: { jsx: "preserve", noLib: true, noResolve: true, types: [] }, files: ["app.tsx"] })]]);
|
|
699
|
+
const normalize = (path) => path.replace(/\\/g, "/").replace(/^[A-Za-z]:/, "");
|
|
700
|
+
const options = {
|
|
701
|
+
cwd: ROOT,
|
|
702
|
+
fs: {
|
|
703
|
+
readFile: (path) => files.get(normalize(path)),
|
|
704
|
+
fileExists: (path) => files.has(normalize(path)),
|
|
705
|
+
directoryExists: (path) => {
|
|
706
|
+
const p = normalize(path).replace(/\/$/, "");
|
|
707
|
+
return p === "" || ROOT.startsWith(p);
|
|
708
|
+
},
|
|
709
|
+
getAccessibleEntries: (path) => normalize(path).replace(/\/$/, "") === ROOT ? { files: ["app.tsx", "tsconfig.json"], directories: [] } : { files: [], directories: [] },
|
|
710
|
+
realpath: (path) => path
|
|
711
|
+
}
|
|
712
|
+
};
|
|
713
|
+
const request = { openProject: CONFIG, fileChanges: { invalidateAll: true } };
|
|
714
|
+
if ((process.platform === "win32" || typeof Bun === "undefined") && process.env.TARVE_TSGO_ASYNC !== "1") {
|
|
715
|
+
const sync = await import("typescript/unstable/sync");
|
|
716
|
+
const api = new sync.API(options);
|
|
717
|
+
return {
|
|
718
|
+
ast,
|
|
719
|
+
is,
|
|
720
|
+
files,
|
|
721
|
+
parse: async () => {
|
|
722
|
+
api.clearSourceFileCache();
|
|
723
|
+
const snapshot = api.updateSnapshot(request);
|
|
724
|
+
try {
|
|
725
|
+
return snapshot.getProjects()[0].program.getSourceFile(FILE);
|
|
726
|
+
} finally {
|
|
727
|
+
snapshot.dispose();
|
|
728
|
+
}
|
|
729
|
+
},
|
|
730
|
+
close: async () => api.close()
|
|
731
|
+
};
|
|
732
|
+
}
|
|
733
|
+
const asyncApi = await import("typescript/unstable/async");
|
|
734
|
+
const api = new asyncApi.API(options);
|
|
735
|
+
return {
|
|
736
|
+
ast,
|
|
737
|
+
is,
|
|
738
|
+
files,
|
|
739
|
+
parse: async () => {
|
|
740
|
+
api.clearSourceFileCache();
|
|
741
|
+
const snapshot = await api.updateSnapshot(request);
|
|
742
|
+
try {
|
|
743
|
+
return await snapshot.getProjects()[0].program.getSourceFile(FILE);
|
|
744
|
+
} finally {
|
|
745
|
+
await snapshot.dispose();
|
|
746
|
+
}
|
|
747
|
+
},
|
|
748
|
+
close: () => api.close()
|
|
749
|
+
};
|
|
750
|
+
}
|
|
751
|
+
async function closeJsxCompiler() {
|
|
752
|
+
const current = parser;
|
|
753
|
+
parser = undefined;
|
|
754
|
+
if (current)
|
|
755
|
+
await (await current).close();
|
|
756
|
+
}
|
|
757
|
+
function constant(node, ast, ts) {
|
|
758
|
+
const isConstant = (child) => constant(child, ast, ts);
|
|
759
|
+
if (ts.isParenthesizedExpression(node))
|
|
760
|
+
return isConstant(node.expression);
|
|
761
|
+
if (ts.isNumericLiteral(node) || ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node))
|
|
762
|
+
return true;
|
|
763
|
+
if (node.kind === ast.SyntaxKind.TrueKeyword || node.kind === ast.SyntaxKind.FalseKeyword || node.kind === ast.SyntaxKind.NullKeyword)
|
|
764
|
+
return true;
|
|
765
|
+
if (ts.isIdentifier(node) && node.text === "undefined")
|
|
766
|
+
return true;
|
|
767
|
+
if (ts.isPrefixUnaryExpression(node) && (node.operator === ast.SyntaxKind.MinusToken || node.operator === ast.SyntaxKind.PlusToken))
|
|
768
|
+
return ts.isNumericLiteral(node.operand);
|
|
769
|
+
if (ts.isArrayLiteralExpression(node))
|
|
770
|
+
return node.elements.every((element) => !ts.isSpreadElement(element) && isConstant(element));
|
|
771
|
+
if (ts.isObjectLiteralExpression(node)) {
|
|
772
|
+
return node.properties.every((property) => ts.isPropertyAssignment(property) && (ts.isIdentifier(property.name) || ts.isStringLiteral(property.name) || ts.isNumericLiteral(property.name)) && isConstant(property.initializer));
|
|
773
|
+
}
|
|
774
|
+
return false;
|
|
775
|
+
}
|
|
776
|
+
async function compileJsx(source) {
|
|
777
|
+
if (!/<[A-Za-z>]/.test(source))
|
|
778
|
+
return { code: source, templates: 0 };
|
|
779
|
+
const loaded = await (parser ??= loadParser());
|
|
780
|
+
const turn = queue.then(async () => {
|
|
781
|
+
loaded.files.set(FILE, source);
|
|
782
|
+
const file = await loaded.parse();
|
|
783
|
+
if (!file || file.text !== source)
|
|
784
|
+
throw new Error("compileJsx: TypeScript returned a different parse of the file");
|
|
785
|
+
return compileFile(source, file, loaded.ast, loaded.is);
|
|
786
|
+
});
|
|
787
|
+
queue = turn.catch(() => {
|
|
788
|
+
return;
|
|
789
|
+
});
|
|
790
|
+
return turn;
|
|
791
|
+
}
|
|
792
|
+
function compileFile(source, file, ast, ts) {
|
|
793
|
+
const isConstant = (node) => constant(node, ast, ts);
|
|
794
|
+
const isJsx = (node) => ts.isJsxElement(node) || ts.isJsxSelfClosingElement(node) || ts.isJsxFragment(node);
|
|
795
|
+
const core = new Set;
|
|
796
|
+
let lastImport = 0;
|
|
797
|
+
for (const statement of file.statements) {
|
|
798
|
+
if (!ts.isImportDeclaration(statement))
|
|
799
|
+
continue;
|
|
800
|
+
lastImport = statement.end;
|
|
801
|
+
if (!ts.isStringLiteral(statement.moduleSpecifier) || !CORE.test(statement.moduleSpecifier.text))
|
|
802
|
+
continue;
|
|
803
|
+
const bindings = statement.importClause?.namedBindings;
|
|
804
|
+
if (bindings && ts.isNamedImports(bindings) && statement.importClause.phaseModifier !== ast.SyntaxKind.TypeKeyword) {
|
|
805
|
+
for (const element of bindings.elements)
|
|
806
|
+
if (!element.isTypeOnly)
|
|
807
|
+
core.add(element.name.text);
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
const shadowed = new Set;
|
|
811
|
+
const scan = (node) => {
|
|
812
|
+
const name = node.kind === ast.SyntaxKind.Parameter || ts.isVariableDeclaration(node) || ts.isFunctionDeclaration(node) || ts.isClassDeclaration(node) ? node.name : undefined;
|
|
813
|
+
if (name && ts.isIdentifier(name) && core.has(name.text))
|
|
814
|
+
shadowed.add(name.text);
|
|
815
|
+
node.forEachChild(scan);
|
|
816
|
+
};
|
|
817
|
+
file.forEachChild(scan);
|
|
818
|
+
for (const name of shadowed)
|
|
819
|
+
core.delete(name);
|
|
820
|
+
const pure = (node) => {
|
|
821
|
+
if (ts.isJsxFragment(node))
|
|
822
|
+
return true;
|
|
823
|
+
const tag = ts.isJsxElement(node) ? node.openingElement.tagName : node.tagName;
|
|
824
|
+
if (ts.isIdentifier(tag))
|
|
825
|
+
return /^[a-z]/.test(tag.text) || core.has(tag.text);
|
|
826
|
+
return false;
|
|
827
|
+
};
|
|
828
|
+
const text = (start, end) => source.slice(start, end);
|
|
829
|
+
const declarations = [];
|
|
830
|
+
function rewrite(start, end, root) {
|
|
831
|
+
const roots = [];
|
|
832
|
+
const find = (node) => {
|
|
833
|
+
if (node.end <= start || node.pos >= end)
|
|
834
|
+
return;
|
|
835
|
+
if (isJsx(node) && node.getStart() >= start && node.end <= end) {
|
|
836
|
+
roots.push(node);
|
|
837
|
+
return;
|
|
838
|
+
}
|
|
839
|
+
node.forEachChild(find);
|
|
840
|
+
};
|
|
841
|
+
find(root);
|
|
842
|
+
let out = "";
|
|
843
|
+
let at = start;
|
|
844
|
+
for (const node of roots) {
|
|
845
|
+
out += text(at, node.getStart()) + compileRoot(node);
|
|
846
|
+
at = node.end;
|
|
847
|
+
}
|
|
848
|
+
return out + text(at, end);
|
|
849
|
+
}
|
|
850
|
+
function compileRoot(node) {
|
|
851
|
+
if (!pure(node))
|
|
852
|
+
return impure(node);
|
|
853
|
+
const holes = [];
|
|
854
|
+
const events = [];
|
|
855
|
+
let key;
|
|
856
|
+
const hole = (expression, event = false) => {
|
|
857
|
+
const name = "h" + holes.length;
|
|
858
|
+
holes.push(expression);
|
|
859
|
+
if (event)
|
|
860
|
+
events.push(name);
|
|
861
|
+
return "p." + name;
|
|
862
|
+
};
|
|
863
|
+
let elements = 0;
|
|
864
|
+
function body(element, isRoot) {
|
|
865
|
+
if (!pure(element))
|
|
866
|
+
return "{" + hole(impure(element)) + "}";
|
|
867
|
+
elements++;
|
|
868
|
+
let out = "";
|
|
869
|
+
let at = element.getStart();
|
|
870
|
+
const end = element.end;
|
|
871
|
+
const attributes = ts.isJsxElement(element) ? element.openingElement.attributes : ts.isJsxSelfClosingElement(element) ? element.attributes : undefined;
|
|
872
|
+
const children = ts.isJsxElement(element) || ts.isJsxFragment(element) ? element.children : undefined;
|
|
873
|
+
const parts = [];
|
|
874
|
+
for (const attribute of attributes?.properties ?? []) {
|
|
875
|
+
if (ts.isJsxSpreadAttribute(attribute)) {
|
|
876
|
+
parts.push({ start: attribute.expression.getStart(), end: attribute.expression.end, replacement: hole(rewrite(attribute.expression.getStart(), attribute.expression.end, attribute.expression)) });
|
|
877
|
+
continue;
|
|
878
|
+
}
|
|
879
|
+
const name = attribute.name.getText();
|
|
880
|
+
const initializer = attribute.initializer;
|
|
881
|
+
if (isRoot && name === "key") {
|
|
882
|
+
key = initializer ? ts.isJsxExpression(initializer) && initializer.expression ? rewrite(initializer.expression.getStart(), initializer.expression.end, initializer.expression) : initializer.getText() : "true";
|
|
883
|
+
parts.push({ start: attribute.getStart(), end: attribute.end, replacement: "" });
|
|
884
|
+
continue;
|
|
885
|
+
}
|
|
886
|
+
if (!initializer)
|
|
887
|
+
continue;
|
|
888
|
+
if (ts.isJsxExpression(initializer) && initializer.expression && !isConstant(initializer.expression)) {
|
|
889
|
+
const expression = initializer.expression;
|
|
890
|
+
parts.push({ start: expression.getStart(), end: expression.end, replacement: hole(rewrite(expression.getStart(), expression.end, expression), EVENT.test(name)) });
|
|
891
|
+
} else if (isJsx(initializer)) {
|
|
892
|
+
const value = initializer;
|
|
893
|
+
parts.push({ start: value.getStart(), end: value.end, replacement: "{" + hole(compileRoot(value)) + "}" });
|
|
894
|
+
}
|
|
895
|
+
}
|
|
896
|
+
for (const child of children ?? []) {
|
|
897
|
+
if (ts.isJsxExpression(child)) {
|
|
898
|
+
if (!child.expression)
|
|
899
|
+
continue;
|
|
900
|
+
if (isConstant(child.expression) && !ts.isObjectLiteralExpression(child.expression) && !ts.isArrayLiteralExpression(child.expression))
|
|
901
|
+
continue;
|
|
902
|
+
parts.push({ start: child.expression.getStart(), end: child.expression.end, replacement: hole(rewrite(child.expression.getStart(), child.expression.end, child.expression)) });
|
|
903
|
+
} else if (isJsx(child)) {
|
|
904
|
+
parts.push({ start: child.getStart(), end: child.end, replacement: body(child, false) });
|
|
905
|
+
}
|
|
906
|
+
}
|
|
907
|
+
parts.sort((a, b) => a.start - b.start);
|
|
908
|
+
for (const part of parts) {
|
|
909
|
+
out += text(at, part.start) + part.replacement;
|
|
910
|
+
at = part.end;
|
|
911
|
+
}
|
|
912
|
+
return out + text(at, end);
|
|
913
|
+
}
|
|
914
|
+
const template = body(node, true);
|
|
915
|
+
if (elements < 2)
|
|
916
|
+
return impure(node);
|
|
917
|
+
const name = "__tarveTemplate" + declarations.length;
|
|
918
|
+
declarations.push(`const ${name} = __tarveCompileTemplate((p: any) => (${template}), ${JSON.stringify(events)});`);
|
|
919
|
+
const props = holes.map((expression, index) => `h${index}={${expression}}`);
|
|
920
|
+
if (key !== undefined)
|
|
921
|
+
props.unshift(`key={${key}}`);
|
|
922
|
+
return `<${name} ${props.join(" ")} />`;
|
|
923
|
+
}
|
|
924
|
+
function impure(node) {
|
|
925
|
+
let out = "";
|
|
926
|
+
let at = node.getStart();
|
|
927
|
+
const visit = (child) => {
|
|
928
|
+
if (isJsx(child) && child !== node) {
|
|
929
|
+
out += text(at, child.getStart()) + compileRoot(child);
|
|
930
|
+
at = child.end;
|
|
931
|
+
return;
|
|
932
|
+
}
|
|
933
|
+
if (ts.isJsxExpression(child) && child.expression) {
|
|
934
|
+
out += text(at, child.expression.getStart()) + rewrite(child.expression.getStart(), child.expression.end, child.expression);
|
|
935
|
+
at = child.expression.end;
|
|
936
|
+
return;
|
|
937
|
+
}
|
|
938
|
+
if (ts.isJsxSpreadAttribute(child)) {
|
|
939
|
+
out += text(at, child.expression.getStart()) + rewrite(child.expression.getStart(), child.expression.end, child.expression);
|
|
940
|
+
at = child.expression.end;
|
|
941
|
+
return;
|
|
942
|
+
}
|
|
943
|
+
child.forEachChild(visit);
|
|
944
|
+
};
|
|
945
|
+
node.forEachChild(visit);
|
|
946
|
+
return out + text(at, node.end);
|
|
947
|
+
}
|
|
948
|
+
const body = rewrite(lastImport, source.length, file);
|
|
949
|
+
if (!declarations.length)
|
|
950
|
+
return { code: source, templates: 0 };
|
|
951
|
+
const code = text(0, lastImport) + `
|
|
952
|
+
import { template as __tarveCompileTemplate } from "@tarve/core";
|
|
953
|
+
` + declarations.join(`
|
|
954
|
+
`) + `
|
|
955
|
+
` + body;
|
|
956
|
+
return { code, templates: declarations.length };
|
|
957
|
+
}
|
|
958
|
+
var ROOT = "/tarve-jsx", FILE, CONFIG, parser, queue, CORE, EVENT;
|
|
959
|
+
var init_jsx_compile = __esm(() => {
|
|
960
|
+
FILE = ROOT + "/app.tsx";
|
|
961
|
+
CONFIG = ROOT + "/tsconfig.json";
|
|
962
|
+
queue = Promise.resolve();
|
|
963
|
+
CORE = /^(?:@tarve\/core|tarve)(?:\/.*)?$/;
|
|
964
|
+
EVENT = /^on[A-Z]/;
|
|
965
|
+
});
|
|
966
|
+
|
|
967
|
+
// packages/core/windows-executable.ts
|
|
968
|
+
var exports_windows_executable = {};
|
|
969
|
+
__export(exports_windows_executable, {
|
|
970
|
+
brandWindowsExecutable: () => brandWindowsExecutable
|
|
971
|
+
});
|
|
972
|
+
import { readFileSync as readFileSync4, writeFileSync as writeFileSync3 } from "node:fs";
|
|
973
|
+
import { Data, NtExecutable, NtExecutableResource, Resource } from "resedit";
|
|
974
|
+
function numericVersion(version) {
|
|
975
|
+
const parts = version.split(/[.+-]/).slice(0, 4).map((part) => Number.parseInt(part, 10));
|
|
976
|
+
return [0, 1, 2, 3].map((index) => {
|
|
977
|
+
const value = parts[index];
|
|
978
|
+
return Number.isInteger(value) && value >= 0 && value <= 65535 ? value : 0;
|
|
979
|
+
});
|
|
980
|
+
}
|
|
981
|
+
function brandWindowsExecutable(path, info) {
|
|
982
|
+
const executable = NtExecutable.from(readFileSync4(path), { ignoreCert: true });
|
|
983
|
+
const resources = NtExecutableResource.from(executable);
|
|
984
|
+
const versionInfo = Resource.VersionInfo.fromEntries(resources.entries)[0] ?? Resource.VersionInfo.createEmpty();
|
|
985
|
+
const languages = versionInfo.getAllLanguagesForStringValues();
|
|
986
|
+
if (languages.length === 0)
|
|
987
|
+
languages.push({ lang: 1033, codepage: 1200 });
|
|
988
|
+
const [major, minor, patch, revision] = numericVersion(info.version);
|
|
989
|
+
const text = `${major}.${minor}.${patch}.${revision}`;
|
|
990
|
+
for (const language of languages) {
|
|
991
|
+
for (const key of ["CompanyName", "LegalCopyright", "LegalTrademarks", "Comments", "PrivateBuild", "SpecialBuild"]) {
|
|
992
|
+
versionInfo.removeStringValue(language, key);
|
|
993
|
+
}
|
|
994
|
+
versionInfo.setStringValues(language, {
|
|
995
|
+
FileDescription: info.description ?? `${info.name} — native Tarve application`,
|
|
996
|
+
...info.publisher ? { CompanyName: info.publisher } : {},
|
|
997
|
+
...info.copyright ? { LegalCopyright: info.copyright } : {},
|
|
998
|
+
ProductName: info.name,
|
|
999
|
+
InternalName: info.name,
|
|
1000
|
+
OriginalFilename: info.filename,
|
|
1001
|
+
FileVersion: text,
|
|
1002
|
+
ProductVersion: text
|
|
1003
|
+
});
|
|
1004
|
+
}
|
|
1005
|
+
versionInfo.setFileVersion(major, minor, patch, revision);
|
|
1006
|
+
versionInfo.setProductVersion(major, minor, patch, revision);
|
|
1007
|
+
versionInfo.outputToResourceEntries(resources.entries);
|
|
1008
|
+
if (info.ico) {
|
|
1009
|
+
const groups = Resource.IconGroupEntry.fromEntries(resources.entries);
|
|
1010
|
+
const group = groups[0];
|
|
1011
|
+
const icons = Data.IconFile.from(info.ico).icons.map((icon) => icon.data);
|
|
1012
|
+
Resource.IconGroupEntry.replaceIconsForResource(resources.entries, group?.id ?? 1, group?.lang ?? 1033, icons);
|
|
1013
|
+
}
|
|
1014
|
+
resources.outputResource(executable);
|
|
1015
|
+
const output = Buffer.from(executable.generate());
|
|
1016
|
+
output.writeUInt16LE(IMAGE_SUBSYSTEM_WINDOWS_GUI, output.readUInt32LE(60) + 24 + 68);
|
|
1017
|
+
writeFileSync3(path, output);
|
|
1018
|
+
}
|
|
1019
|
+
var IMAGE_SUBSYSTEM_WINDOWS_GUI = 2;
|
|
1020
|
+
var init_windows_executable = () => {};
|
|
1021
|
+
|
|
1022
|
+
// packages/core/node-build.ts
|
|
1023
|
+
import { execFileSync } from "node:child_process";
|
|
1024
|
+
import { existsSync as existsSync4, mkdtempSync, writeFileSync as writeFileSync4, readFileSync as readFileSync5 } from "node:fs";
|
|
1025
|
+
import { mkdir, rm } from "node:fs/promises";
|
|
1026
|
+
import { tmpdir } from "node:os";
|
|
1027
|
+
import { basename as basename3, dirname as dirname4, join as join4, relative as relative2, resolve as resolve3 } from "node:path";
|
|
1028
|
+
import { nativePath as nativePath2 } from "#tarve/runtime";
|
|
1029
|
+
async function buildNode(options) {
|
|
1030
|
+
const target = options.target ?? hostBuildTarget();
|
|
1031
|
+
if (!target)
|
|
1032
|
+
throw new Error(`Unsupported Node.js build host: ${process.platform}-${process.arch}`);
|
|
1033
|
+
const config = targetConfig(target);
|
|
1034
|
+
const node = options.nodeExecutable ?? (process.versions.bun ? Bun.which("node") ?? "node" : process.execPath);
|
|
1035
|
+
const version = execFileSync(node, ["--version"], { encoding: "utf8" }).trim();
|
|
1036
|
+
const match = /^v(\d+)\.(\d+)\./.exec(version);
|
|
1037
|
+
if (!match || Number(match[1]) < 26 || Number(match[1]) === 26 && Number(match[2]) < 10) {
|
|
1038
|
+
throw new Error(`Node.js 26.10+ is required for node:ffi inside a SEA (found ${version})`);
|
|
1039
|
+
}
|
|
1040
|
+
const { metadata, images, define } = buildMetadata(options.entrypoint, metadataOverrides(options));
|
|
1041
|
+
const outfile = resolve3(options.outfile ?? `dist/${metadata.name}${config.executableSuffix}`);
|
|
1042
|
+
let executable;
|
|
1043
|
+
if (options.targetExecutable) {
|
|
1044
|
+
executable = resolve3(options.targetExecutable);
|
|
1045
|
+
if (!existsSync4(executable))
|
|
1046
|
+
throw new Error(`Target Node.js executable is missing: ${executable}`);
|
|
1047
|
+
assertNodeVersion(executable, version.slice(1));
|
|
1048
|
+
}
|
|
1049
|
+
const library = resolve3(options.nativeLibrary ?? (target === hostBuildTarget() ? nativePath2() : join4(import.meta.dirname, "../../native", config.nativeDirectory, config.nativeName)));
|
|
1050
|
+
if (!existsSync4(library))
|
|
1051
|
+
throw new Error(`Tarve native library missing: ${library}`);
|
|
1052
|
+
const temporary = mkdtempSync(join4(tmpdir(), "tarve-node-build-"));
|
|
1053
|
+
try {
|
|
1054
|
+
const root = import.meta.dirname;
|
|
1055
|
+
const source = existsSync4(join4(root, "src/index.ts"));
|
|
1056
|
+
if (!options.nativeLibrary && (!source || target !== hostBuildTarget())) {
|
|
1057
|
+
const expected = source ? await createNativeCompatibilityManifest(resolve3(root, "../.."), JSON.parse(readFileSync5(resolve3(root, "../../package.json"), "utf8")).version) : await readNativeCompatibilityManifest(join4(root, NATIVE_COMPATIBILITY_MANIFEST_NAME));
|
|
1058
|
+
await assertNativeRuntimeCompatible(library, target, expected);
|
|
1059
|
+
}
|
|
1060
|
+
if (!executable && target !== hostBuildTarget()) {
|
|
1061
|
+
executable = await targetNodeExecutable(target, version.slice(1));
|
|
1062
|
+
}
|
|
1063
|
+
const { build: bundle } = await import("esbuild");
|
|
1064
|
+
const result = await bundle({
|
|
1065
|
+
entryPoints: [resolve3(options.entrypoint)],
|
|
1066
|
+
outfile: join4(temporary, "app.mjs"),
|
|
1067
|
+
bundle: true,
|
|
1068
|
+
platform: "node",
|
|
1069
|
+
format: "esm",
|
|
1070
|
+
target: "node26",
|
|
1071
|
+
jsx: "automatic",
|
|
1072
|
+
jsxImportSource: "@tarve/core",
|
|
1073
|
+
loader: { ".png": "file", ".jpg": "file", ".jpeg": "file", ".webp": "file", ".gif": "file", ".svg": "file" },
|
|
1074
|
+
metafile: true,
|
|
1075
|
+
define,
|
|
1076
|
+
external: ["esbuild"],
|
|
1077
|
+
plugins: [fileImportAttributes, {
|
|
1078
|
+
name: "tarve-node-runtime",
|
|
1079
|
+
setup(build) {
|
|
1080
|
+
if (options.compileJsx) {
|
|
1081
|
+
build.onLoad({ filter: /\.[jt]sx$/ }, async (args) => {
|
|
1082
|
+
if (/[\\/]node_modules[\\/]/.test(args.path) || args.path.startsWith(join4(root, "src")))
|
|
1083
|
+
return;
|
|
1084
|
+
await Promise.resolve().then(() => init_jsx_compile());
|
|
1085
|
+
return { contents: (await compileJsx(readFileSync5(args.path, "utf8"))).code, loader: args.path.endsWith(".jsx") ? "jsx" : "tsx", resolveDir: dirname4(args.path) };
|
|
1086
|
+
});
|
|
1087
|
+
}
|
|
1088
|
+
if (source) {
|
|
1089
|
+
build.onResolve({ filter: /^@tarve\/core(?:\/(?:jsx-runtime|jsx-dev-runtime))?$/ }, (args) => {
|
|
1090
|
+
const entry = args.path.slice("@tarve/core".length) || "/index";
|
|
1091
|
+
return { path: join4(root, "src", `${entry.slice(1)}.ts`) };
|
|
1092
|
+
});
|
|
1093
|
+
}
|
|
1094
|
+
build.onResolve({ filter: /^#tarve\/runtime$/ }, () => ({ path: "native-runtime", namespace: "tarve-node" }));
|
|
1095
|
+
build.onLoad({ filter: /.*/, namespace: "tarve-node" }, () => ({
|
|
1096
|
+
contents: `import { join } from "node:path"; export function nativePath() { return join(import.meta.dirname, ${JSON.stringify(config.nativeName)}); }`,
|
|
1097
|
+
loader: "js"
|
|
1098
|
+
}));
|
|
1099
|
+
}
|
|
1100
|
+
}]
|
|
1101
|
+
});
|
|
1102
|
+
const assets = { [config.nativeName]: seaPath(library) };
|
|
1103
|
+
for (const [path, output] of Object.entries(result.metafile.outputs)) {
|
|
1104
|
+
if (!output.entryPoint)
|
|
1105
|
+
assets[relative2(temporary, resolve3(path)).replaceAll("\\", "/")] = seaPath(resolve3(path));
|
|
1106
|
+
}
|
|
1107
|
+
const sea = join4(temporary, "sea.json");
|
|
1108
|
+
writeFileSync4(sea, JSON.stringify({
|
|
1109
|
+
main: seaPath(join4(temporary, "app.mjs")),
|
|
1110
|
+
mainFormat: "module",
|
|
1111
|
+
output: seaPath(outfile),
|
|
1112
|
+
...executable ? { executable: seaPath(executable) } : {},
|
|
1113
|
+
useVfs: true,
|
|
1114
|
+
useCodeCache: false,
|
|
1115
|
+
useSnapshot: false,
|
|
1116
|
+
assets,
|
|
1117
|
+
execArgv: ["--disable-warning=ExperimentalWarning"],
|
|
1118
|
+
execArgvExtension: "env"
|
|
1119
|
+
}));
|
|
1120
|
+
await mkdir(dirname4(outfile), { recursive: true });
|
|
1121
|
+
await rm(outfile, { force: true });
|
|
1122
|
+
execFileSync(node, ["--build-sea", sea], { stdio: "pipe" });
|
|
1123
|
+
if (config.platform === "win32") {
|
|
1124
|
+
const tarveVersion = JSON.parse(readFileSync5(resolve3(root, "../../package.json"), "utf8")).version;
|
|
1125
|
+
(await Promise.resolve().then(() => (init_windows_executable(), exports_windows_executable))).brandWindowsExecutable(outfile, {
|
|
1126
|
+
name: metadata.name,
|
|
1127
|
+
version: metadata.version ?? tarveVersion,
|
|
1128
|
+
filename: basename3(outfile),
|
|
1129
|
+
description: metadata.description,
|
|
1130
|
+
publisher: metadata.publisher,
|
|
1131
|
+
copyright: metadata.copyright,
|
|
1132
|
+
ico: images?.ico
|
|
1133
|
+
});
|
|
1134
|
+
}
|
|
1135
|
+
if (config.platform === "linux" && (metadata.icon || metadata.appId))
|
|
1136
|
+
writeLinuxDesktopEntry(outfile, metadata, images?.png);
|
|
1137
|
+
return outfile;
|
|
1138
|
+
} finally {
|
|
1139
|
+
if (options.compileJsx)
|
|
1140
|
+
await (await Promise.resolve().then(() => (init_jsx_compile(), exports_jsx_compile))).closeJsxCompiler();
|
|
1141
|
+
await rm(temporary, { recursive: true, force: true });
|
|
1142
|
+
}
|
|
1143
|
+
}
|
|
1144
|
+
function seaPath(path) {
|
|
1145
|
+
return path.replaceAll("\\", "/");
|
|
1146
|
+
}
|
|
1147
|
+
var init_node_build = __esm(() => {
|
|
1148
|
+
init_esbuild_file_imports();
|
|
1149
|
+
init_targets();
|
|
1150
|
+
init_build();
|
|
1151
|
+
init_package_metadata();
|
|
1152
|
+
init_native_runtime();
|
|
1153
|
+
init_node_target();
|
|
1154
|
+
});
|
|
1155
|
+
init_node_build();
|
|
1156
|
+
|
|
1157
|
+
export {
|
|
1158
|
+
buildNode
|
|
1159
|
+
};
|