@tarve/core 0.3.0 → 0.5.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 +101 -1
- package/README.md +68 -11
- package/dist/npm/build.js +830 -84
- package/dist/npm/cli.js +1094 -117
- package/dist/npm/embedded-assets.js +35 -4
- package/dist/npm/index.js +2515 -207
- package/dist/npm/native-runtime.json +3 -3
- package/dist/npm/node-build.js +865 -0
- package/dist/npm/protocol.js +1 -1
- package/dist/npm/runtime.js +5 -5
- package/dist/types/core/app-metadata.d.ts +49 -0
- package/dist/types/core/build.d.ts +21 -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/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/runtime-select.d.ts +8 -0
- package/dist/types/core/src/app-metadata.d.ts +16 -0
- package/dist/types/core/src/app.d.ts +87 -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/canvas.d.ts +161 -0
- package/dist/types/core/src/components/image.d.ts +7 -3
- package/dist/types/core/src/components/layout.d.ts +4 -3
- package/dist/types/core/src/components/pressable.d.ts +2 -2
- package/dist/types/core/src/embedded-assets.d.ts +12 -2
- package/dist/types/core/src/extra-controls.d.ts +1 -1
- package/dist/types/core/src/html-elements.d.ts +26 -0
- package/dist/types/core/src/index.d.ts +5 -2
- package/dist/types/core/src/inline-text.d.ts +21 -0
- package/dist/types/core/src/jsx-runtime.d.ts +175 -4
- package/dist/types/core/src/media-element.d.ts +71 -0
- package/dist/types/core/src/memo.d.ts +18 -0
- package/dist/types/core/src/png.d.ts +29 -0
- package/dist/types/core/src/popups.d.ts +3 -2
- package/dist/types/core/src/reconciler.d.ts +27 -3
- package/dist/types/core/src/test-renderer.d.ts +2 -0
- package/dist/types/core/src/theme.d.ts +2 -1
- package/dist/types/core/src/virtual-list.d.ts +5 -1
- package/dist/types/core/windows-executable.d.ts +16 -0
- package/dist/types/protocol/src/index.d.ts +206 -6
- 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 +11 -2
package/dist/npm/build.js
CHANGED
|
@@ -1,31 +1,404 @@
|
|
|
1
1
|
// @bun
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __returnValue = (v) => v;
|
|
4
|
+
function __exportSetter(name, newValue) {
|
|
5
|
+
this[name] = __returnValue.bind(null, newValue);
|
|
6
|
+
}
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, {
|
|
10
|
+
get: all[name],
|
|
11
|
+
enumerable: true,
|
|
12
|
+
configurable: true,
|
|
13
|
+
set: __exportSetter.bind(all, name)
|
|
14
|
+
});
|
|
15
|
+
};
|
|
16
|
+
var __esm = (fn, res, err) => () => {
|
|
17
|
+
if (fn)
|
|
18
|
+
try {
|
|
19
|
+
res = fn(fn = 0);
|
|
20
|
+
} catch (e) {
|
|
21
|
+
err = [e];
|
|
22
|
+
}
|
|
23
|
+
if (err)
|
|
24
|
+
throw err[0];
|
|
25
|
+
return res;
|
|
26
|
+
};
|
|
8
27
|
|
|
9
|
-
// packages/core/
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
28
|
+
// packages/core/src/png.ts
|
|
29
|
+
import { deflateSync, inflateSync } from "zlib";
|
|
30
|
+
function isPng(bytes) {
|
|
31
|
+
return SIGNATURE.every((value, index) => bytes[index] === value);
|
|
32
|
+
}
|
|
33
|
+
function decodePng(bytes) {
|
|
34
|
+
if (!isPng(bytes))
|
|
35
|
+
throw new Error("not a PNG file");
|
|
36
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
37
|
+
let offset = 8;
|
|
38
|
+
let width = 0, height = 0, depth = 0, colour = 0, interlace = 0;
|
|
39
|
+
let palette;
|
|
40
|
+
let transparency;
|
|
41
|
+
const idat = [];
|
|
42
|
+
while (offset + 12 <= bytes.length) {
|
|
43
|
+
const length = view.getUint32(offset);
|
|
44
|
+
const type = String.fromCharCode(...bytes.subarray(offset + 4, offset + 8));
|
|
45
|
+
const data = bytes.subarray(offset + 8, offset + 8 + length);
|
|
46
|
+
if (offset + 12 + length > bytes.length)
|
|
47
|
+
throw new Error("PNG chunk runs past the end of the file");
|
|
48
|
+
if (type === "IHDR") {
|
|
49
|
+
width = view.getUint32(offset + 8);
|
|
50
|
+
height = view.getUint32(offset + 12);
|
|
51
|
+
depth = data[8];
|
|
52
|
+
colour = data[9];
|
|
53
|
+
interlace = data[12];
|
|
54
|
+
} else if (type === "PLTE")
|
|
55
|
+
palette = data;
|
|
56
|
+
else if (type === "tRNS")
|
|
57
|
+
transparency = data;
|
|
58
|
+
else if (type === "IDAT")
|
|
59
|
+
idat.push(data);
|
|
60
|
+
else if (type === "IEND")
|
|
61
|
+
break;
|
|
62
|
+
offset += 12 + length;
|
|
27
63
|
}
|
|
28
|
-
|
|
64
|
+
if (width <= 0 || height <= 0 || idat.length === 0)
|
|
65
|
+
throw new Error("PNG has no image data");
|
|
66
|
+
if (width * height > 16384 * 16384)
|
|
67
|
+
throw new Error("PNG is too large");
|
|
68
|
+
if (interlace !== 0)
|
|
69
|
+
throw new Error("interlaced PNGs are not supported; save it without interlacing");
|
|
70
|
+
const channels = { 0: 1, 2: 3, 3: 1, 4: 2, 6: 4 }[colour];
|
|
71
|
+
if (channels === undefined || ![1, 2, 4, 8, 16].includes(depth) || depth < 8 && colour !== 0 && colour !== 3 || depth === 16 && colour === 3) {
|
|
72
|
+
throw new Error(`unsupported PNG format (colour type ${colour}, bit depth ${depth})`);
|
|
73
|
+
}
|
|
74
|
+
const bitsPerPixel = channels * depth;
|
|
75
|
+
const bytesPerPixel = Math.max(1, bitsPerPixel >> 3);
|
|
76
|
+
const stride = Math.ceil(width * bitsPerPixel / 8);
|
|
77
|
+
const raw = inflateSync(Buffer.concat(idat.map((chunk) => Buffer.from(chunk))));
|
|
78
|
+
if (raw.length < height * (stride + 1))
|
|
79
|
+
throw new Error("PNG image data is truncated");
|
|
80
|
+
const lines = new Uint8Array(height * stride);
|
|
81
|
+
for (let y = 0;y < height; y++) {
|
|
82
|
+
const filter = raw[y * (stride + 1)];
|
|
83
|
+
const source = y * (stride + 1) + 1;
|
|
84
|
+
const target = y * stride;
|
|
85
|
+
for (let x = 0;x < stride; x++) {
|
|
86
|
+
const encoded = raw[source + x];
|
|
87
|
+
const left = x >= bytesPerPixel ? lines[target + x - bytesPerPixel] : 0;
|
|
88
|
+
const up = y > 0 ? lines[target - stride + x] : 0;
|
|
89
|
+
const upLeft = y > 0 && x >= bytesPerPixel ? lines[target - stride + x - bytesPerPixel] : 0;
|
|
90
|
+
let predictor = 0;
|
|
91
|
+
if (filter === 1)
|
|
92
|
+
predictor = left;
|
|
93
|
+
else if (filter === 2)
|
|
94
|
+
predictor = up;
|
|
95
|
+
else if (filter === 3)
|
|
96
|
+
predictor = left + up >> 1;
|
|
97
|
+
else if (filter === 4) {
|
|
98
|
+
const p = left + up - upLeft;
|
|
99
|
+
const pa = Math.abs(p - left), pb = Math.abs(p - up), pc = Math.abs(p - upLeft);
|
|
100
|
+
predictor = pa <= pb && pa <= pc ? left : pb <= pc ? up : upLeft;
|
|
101
|
+
} else if (filter !== 0)
|
|
102
|
+
throw new Error(`unsupported PNG filter ${filter}`);
|
|
103
|
+
lines[target + x] = encoded + predictor & 255;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
const sample = (y, index) => {
|
|
107
|
+
const row = y * stride;
|
|
108
|
+
if (depth === 8)
|
|
109
|
+
return lines[row + index];
|
|
110
|
+
if (depth === 16)
|
|
111
|
+
return lines[row + index * 2];
|
|
112
|
+
const bit = index * depth;
|
|
113
|
+
return lines[row + (bit >> 3)] >> 8 - depth - (bit & 7) & (1 << depth) - 1;
|
|
114
|
+
};
|
|
115
|
+
const scale = depth < 8 && colour === 0 ? 255 / ((1 << depth) - 1) : 1;
|
|
116
|
+
const rgba = new Uint8Array(width * height * 4);
|
|
117
|
+
for (let y = 0;y < height; y++) {
|
|
118
|
+
for (let x = 0;x < width; x++) {
|
|
119
|
+
const out = (y * width + x) * 4;
|
|
120
|
+
const base = x * channels;
|
|
121
|
+
if (colour === 3) {
|
|
122
|
+
const index = sample(y, base);
|
|
123
|
+
rgba[out] = palette?.[index * 3] ?? 0;
|
|
124
|
+
rgba[out + 1] = palette?.[index * 3 + 1] ?? 0;
|
|
125
|
+
rgba[out + 2] = palette?.[index * 3 + 2] ?? 0;
|
|
126
|
+
rgba[out + 3] = transparency?.[index] ?? 255;
|
|
127
|
+
} else if (colour === 0 || colour === 4) {
|
|
128
|
+
const grey = Math.round(sample(y, base) * scale);
|
|
129
|
+
rgba[out] = rgba[out + 1] = rgba[out + 2] = grey;
|
|
130
|
+
rgba[out + 3] = colour === 4 ? sample(y, base + 1) : 255;
|
|
131
|
+
} else {
|
|
132
|
+
rgba[out] = sample(y, base);
|
|
133
|
+
rgba[out + 1] = sample(y, base + 1);
|
|
134
|
+
rgba[out + 2] = sample(y, base + 2);
|
|
135
|
+
rgba[out + 3] = colour === 6 ? sample(y, base + 3) : 255;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return { width, height, rgba };
|
|
140
|
+
}
|
|
141
|
+
function crc32(bytes) {
|
|
142
|
+
let crc = 4294967295;
|
|
143
|
+
for (const byte of bytes)
|
|
144
|
+
crc = CRC_TABLE[(crc ^ byte) & 255] ^ crc >>> 8;
|
|
145
|
+
return (crc ^ 4294967295) >>> 0;
|
|
146
|
+
}
|
|
147
|
+
function chunk(type, data) {
|
|
148
|
+
const out = Buffer.alloc(12 + data.length);
|
|
149
|
+
out.writeUInt32BE(data.length, 0);
|
|
150
|
+
out.write(type, 4, "ascii");
|
|
151
|
+
out.set(data, 8);
|
|
152
|
+
out.writeUInt32BE(crc32(out.subarray(4, 8 + data.length)), 8 + data.length);
|
|
153
|
+
return out;
|
|
154
|
+
}
|
|
155
|
+
function encodePng(rgba, width, height) {
|
|
156
|
+
const stride = width * 4;
|
|
157
|
+
const raw = Buffer.alloc((stride + 1) * height);
|
|
158
|
+
for (let y = 0;y < height; y++)
|
|
159
|
+
raw.set(rgba.subarray(y * stride, (y + 1) * stride), y * (stride + 1) + 1);
|
|
160
|
+
const header = Buffer.alloc(13);
|
|
161
|
+
header.writeUInt32BE(width, 0);
|
|
162
|
+
header.writeUInt32BE(height, 4);
|
|
163
|
+
header[8] = 8;
|
|
164
|
+
header[9] = 6;
|
|
165
|
+
return new Uint8Array(Buffer.concat([
|
|
166
|
+
Buffer.from(SIGNATURE),
|
|
167
|
+
chunk("IHDR", header),
|
|
168
|
+
chunk("IDAT", deflateSync(raw, { level: 9 })),
|
|
169
|
+
chunk("IEND", new Uint8Array)
|
|
170
|
+
]));
|
|
171
|
+
}
|
|
172
|
+
function resizeSquare(image, size) {
|
|
173
|
+
const side = Math.max(image.width, image.height);
|
|
174
|
+
const offsetX = (side - image.width) / 2, offsetY = (side - image.height) / 2;
|
|
175
|
+
const out = new Uint8Array(size * size * 4);
|
|
176
|
+
const ratio = side / size;
|
|
177
|
+
for (let y = 0;y < size; y++) {
|
|
178
|
+
const y0 = y * ratio - offsetY, y1 = (y + 1) * ratio - offsetY;
|
|
179
|
+
for (let x = 0;x < size; x++) {
|
|
180
|
+
const x0 = x * ratio - offsetX, x1 = (x + 1) * ratio - offsetX;
|
|
181
|
+
let r = 0, g = 0, b = 0, a = 0;
|
|
182
|
+
for (let sy = Math.max(0, Math.floor(y0));sy < Math.min(image.height, Math.ceil(y1)); sy++) {
|
|
183
|
+
const wy = Math.min(y1, sy + 1) - Math.max(y0, sy);
|
|
184
|
+
for (let sx = Math.max(0, Math.floor(x0));sx < Math.min(image.width, Math.ceil(x1)); sx++) {
|
|
185
|
+
const w = wy * (Math.min(x1, sx + 1) - Math.max(x0, sx));
|
|
186
|
+
const i = (sy * image.width + sx) * 4;
|
|
187
|
+
const alpha = image.rgba[i + 3] * w;
|
|
188
|
+
r += image.rgba[i] * alpha;
|
|
189
|
+
g += image.rgba[i + 1] * alpha;
|
|
190
|
+
b += image.rgba[i + 2] * alpha;
|
|
191
|
+
a += alpha;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
const area = ratio * ratio;
|
|
195
|
+
const o = (y * size + x) * 4;
|
|
196
|
+
if (a > 0) {
|
|
197
|
+
out[o] = Math.round(r / a);
|
|
198
|
+
out[o + 1] = Math.round(g / a);
|
|
199
|
+
out[o + 2] = Math.round(b / a);
|
|
200
|
+
}
|
|
201
|
+
out[o + 3] = Math.round(a / area);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
return out;
|
|
205
|
+
}
|
|
206
|
+
function encodeIco(entries) {
|
|
207
|
+
const header = Buffer.alloc(6 + entries.length * 16);
|
|
208
|
+
header.writeUInt16LE(0, 0);
|
|
209
|
+
header.writeUInt16LE(1, 2);
|
|
210
|
+
header.writeUInt16LE(entries.length, 4);
|
|
211
|
+
let offset = header.length;
|
|
212
|
+
entries.forEach(({ size, png }, index) => {
|
|
213
|
+
const at = 6 + index * 16;
|
|
214
|
+
header[at] = size >= 256 ? 0 : size;
|
|
215
|
+
header[at + 1] = size >= 256 ? 0 : size;
|
|
216
|
+
header.writeUInt16LE(1, at + 4);
|
|
217
|
+
header.writeUInt16LE(32, at + 6);
|
|
218
|
+
header.writeUInt32LE(png.length, at + 8);
|
|
219
|
+
header.writeUInt32LE(offset, at + 12);
|
|
220
|
+
offset += png.length;
|
|
221
|
+
});
|
|
222
|
+
return new Uint8Array(Buffer.concat([header, ...entries.map((entry) => Buffer.from(entry.png))]));
|
|
223
|
+
}
|
|
224
|
+
function isIco(bytes) {
|
|
225
|
+
return bytes.length >= 6 && bytes[0] === 0 && bytes[1] === 0 && bytes[2] === 1 && bytes[3] === 0;
|
|
226
|
+
}
|
|
227
|
+
function largestIcoPng(bytes) {
|
|
228
|
+
if (!isIco(bytes))
|
|
229
|
+
return;
|
|
230
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
231
|
+
let best;
|
|
232
|
+
let bestSize = 0;
|
|
233
|
+
for (let index = 0;index < view.getUint16(4, true); index++) {
|
|
234
|
+
const at = 6 + index * 16;
|
|
235
|
+
if (at + 16 > bytes.length)
|
|
236
|
+
break;
|
|
237
|
+
const size = bytes[at] || 256;
|
|
238
|
+
const length = view.getUint32(at + 8, true), start = view.getUint32(at + 12, true);
|
|
239
|
+
const data = bytes.subarray(start, start + length);
|
|
240
|
+
if (isPng(data) && size > bestSize) {
|
|
241
|
+
best = data;
|
|
242
|
+
bestSize = size;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
return best;
|
|
246
|
+
}
|
|
247
|
+
var SIGNATURE, CRC_TABLE;
|
|
248
|
+
var init_png = __esm(() => {
|
|
249
|
+
SIGNATURE = [137, 80, 78, 71, 13, 10, 26, 10];
|
|
250
|
+
CRC_TABLE = new Uint32Array(256).map((_, n) => {
|
|
251
|
+
let c = n;
|
|
252
|
+
for (let k = 0;k < 8; k++)
|
|
253
|
+
c = c & 1 ? 3988292384 ^ c >>> 1 : c >>> 1;
|
|
254
|
+
return c >>> 0;
|
|
255
|
+
});
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
// packages/core/app-metadata.ts
|
|
259
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
|
|
260
|
+
import { basename, dirname, extname, join, relative, resolve } from "path";
|
|
261
|
+
function fields(value, where) {
|
|
262
|
+
if (value === undefined)
|
|
263
|
+
return {};
|
|
264
|
+
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
265
|
+
throw new TypeError(`${where} must be an object`);
|
|
266
|
+
const out = {};
|
|
267
|
+
for (const key of FIELDS) {
|
|
268
|
+
const field = value[key];
|
|
269
|
+
if (field === undefined)
|
|
270
|
+
continue;
|
|
271
|
+
if (typeof field !== "string" || !field.trim())
|
|
272
|
+
throw new TypeError(`${where}.${key} must be a non-empty string`);
|
|
273
|
+
out[key] = field.trim();
|
|
274
|
+
}
|
|
275
|
+
return out;
|
|
276
|
+
}
|
|
277
|
+
function author(value) {
|
|
278
|
+
if (typeof value === "string")
|
|
279
|
+
return value.replace(/\s*[<(].*$/, "").trim() || undefined;
|
|
280
|
+
if (value && typeof value === "object" && typeof value.name === "string")
|
|
281
|
+
return value.name;
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
function validAppId(id) {
|
|
285
|
+
return id.length > 0 && id.length <= 128 && !/[\s\\]/.test(id);
|
|
286
|
+
}
|
|
287
|
+
function resolveAppMetadata(entrypoint, overrides = {}) {
|
|
288
|
+
const entry = resolve(entrypoint);
|
|
289
|
+
let directory = dirname(entry);
|
|
290
|
+
let manifest;
|
|
291
|
+
let root = directory;
|
|
292
|
+
for (;; ) {
|
|
293
|
+
const path = join(directory, "package.json");
|
|
294
|
+
if (existsSync(path)) {
|
|
295
|
+
manifest = JSON.parse(readFileSync(path, "utf8"));
|
|
296
|
+
root = directory;
|
|
297
|
+
break;
|
|
298
|
+
}
|
|
299
|
+
const parent = dirname(directory);
|
|
300
|
+
if (parent === directory)
|
|
301
|
+
break;
|
|
302
|
+
directory = parent;
|
|
303
|
+
}
|
|
304
|
+
const tarve = manifest?.tarve;
|
|
305
|
+
const shared = fields(tarve === undefined ? undefined : Object.fromEntries(Object.entries(tarve).filter(([key]) => key !== "apps")), "package.json tarve");
|
|
306
|
+
const apps = tarve?.apps;
|
|
307
|
+
const key = relative(root, entry).replaceAll("\\", "/");
|
|
308
|
+
const own = fields(apps?.[key] ?? apps?.["./" + key], `package.json tarve.apps["${key}"]`);
|
|
309
|
+
const fromPackage = tarve === undefined ? {} : {
|
|
310
|
+
...typeof manifest?.version === "string" ? { version: manifest.version } : {},
|
|
311
|
+
...typeof manifest?.description === "string" ? { description: manifest.description } : {},
|
|
312
|
+
...author(manifest?.author) ? { publisher: author(manifest?.author) } : {}
|
|
313
|
+
};
|
|
314
|
+
const configured = { ...fromPackage, ...shared, ...own };
|
|
315
|
+
if (configured.icon)
|
|
316
|
+
configured.icon = resolve(root, configured.icon);
|
|
317
|
+
const flags = fields(Object.fromEntries(Object.entries(overrides).filter(([, value]) => value !== undefined)), "option");
|
|
318
|
+
if (flags.icon)
|
|
319
|
+
flags.icon = resolve(flags.icon);
|
|
320
|
+
const merged = { ...configured, ...flags };
|
|
321
|
+
const metadata = { name: merged.name ?? basename(entry).replace(/\.[^.]+$/, "") };
|
|
322
|
+
for (const field of FIELDS)
|
|
323
|
+
if (field !== "name" && merged[field] !== undefined)
|
|
324
|
+
metadata[field] = merged[field];
|
|
325
|
+
if (metadata.appId !== undefined && !validAppId(metadata.appId)) {
|
|
326
|
+
throw new TypeError(`appId must be 1-128 characters without spaces or backslashes: ${metadata.appId}`);
|
|
327
|
+
}
|
|
328
|
+
if (metadata.icon !== undefined) {
|
|
329
|
+
if (!existsSync(metadata.icon))
|
|
330
|
+
throw new Error(`App icon not found: ${metadata.icon}`);
|
|
331
|
+
if (![".png", ".ico"].includes(extname(metadata.icon).toLowerCase()))
|
|
332
|
+
throw new TypeError(`App icon must be a .png or .ico file: ${metadata.icon}`);
|
|
333
|
+
}
|
|
334
|
+
return metadata;
|
|
335
|
+
}
|
|
336
|
+
function appIconImages(path) {
|
|
337
|
+
const bytes = new Uint8Array(readFileSync(path));
|
|
338
|
+
if (isIco(bytes)) {
|
|
339
|
+
const png = largestIcoPng(bytes);
|
|
340
|
+
if (!png)
|
|
341
|
+
throw new Error(`${path} has no PNG-compressed image; Linux builds and the window icon need one. Use a .png icon instead.`);
|
|
342
|
+
return { ico: bytes, png };
|
|
343
|
+
}
|
|
344
|
+
if (!isPng(bytes))
|
|
345
|
+
throw new TypeError(`${path} is not a PNG or ICO file`);
|
|
346
|
+
let image;
|
|
347
|
+
try {
|
|
348
|
+
image = decodePng(bytes);
|
|
349
|
+
} catch (error) {
|
|
350
|
+
throw new Error(`App icon ${path}: ${error instanceof Error ? error.message : String(error)}`);
|
|
351
|
+
}
|
|
352
|
+
const side = Math.max(image.width, image.height);
|
|
353
|
+
const sizes = ICO_SIZES.filter((size) => size <= side);
|
|
354
|
+
if (!sizes.length)
|
|
355
|
+
sizes.push(16);
|
|
356
|
+
const entries = sizes.map((size) => ({ size, png: encodePng(resizeSquare(image, size), size, size) }));
|
|
357
|
+
return { ico: encodeIco(entries), png: entries.at(-1).png };
|
|
358
|
+
}
|
|
359
|
+
function buildMetadata(entrypoint, overrides) {
|
|
360
|
+
const metadata = resolveAppMetadata(entrypoint, overrides);
|
|
361
|
+
const images = metadata.icon ? appIconImages(metadata.icon) : undefined;
|
|
362
|
+
const icon = images ? `data:image/png;base64,${Buffer.from(images.png).toString("base64")}` : undefined;
|
|
363
|
+
return { metadata, images, define: { "process.env.TARVE_APP_METADATA": JSON.stringify(runtimeMetadata(metadata, icon)) } };
|
|
364
|
+
}
|
|
365
|
+
function runtimeMetadata(metadata, icon) {
|
|
366
|
+
return JSON.stringify({
|
|
367
|
+
name: metadata.name,
|
|
368
|
+
...metadata.appId ? { appId: metadata.appId } : {},
|
|
369
|
+
...icon ? { icon } : {}
|
|
370
|
+
});
|
|
371
|
+
}
|
|
372
|
+
function writeLinuxDesktopEntry(outfile, metadata, png) {
|
|
373
|
+
const id = metadata.appId ?? basename(outfile);
|
|
374
|
+
const escape = (value) => value.replace(/[\\\n\r\t]/g, (match) => ({ "\\": "\\\\", "\n": "\\n", "\r": "\\r", "\t": "\\t" })[match]);
|
|
375
|
+
const lines = [
|
|
376
|
+
"[Desktop Entry]",
|
|
377
|
+
"Type=Application",
|
|
378
|
+
`Name=${escape(metadata.name)}`,
|
|
379
|
+
...metadata.description ? [`Comment=${escape(metadata.description)}`] : [],
|
|
380
|
+
`Exec="${outfile.replaceAll('"', "\\\"")}" %U`,
|
|
381
|
+
...png ? [`Icon=${id}`] : [],
|
|
382
|
+
`StartupWMClass=${id}`,
|
|
383
|
+
"Terminal=false",
|
|
384
|
+
""
|
|
385
|
+
];
|
|
386
|
+
mkdirSync(dirname(outfile), { recursive: true });
|
|
387
|
+
const desktop = join(dirname(outfile), `${id}.desktop`);
|
|
388
|
+
writeFileSync(desktop, lines.join(`
|
|
389
|
+
`));
|
|
390
|
+
if (png)
|
|
391
|
+
writeFileSync(join(dirname(outfile), `${id}.png`), png);
|
|
392
|
+
return desktop;
|
|
393
|
+
}
|
|
394
|
+
var FIELDS, ICO_SIZES;
|
|
395
|
+
var init_app_metadata = __esm(() => {
|
|
396
|
+
init_png();
|
|
397
|
+
FIELDS = ["name", "version", "description", "publisher", "copyright", "icon", "appId"];
|
|
398
|
+
ICO_SIZES = [16, 20, 24, 32, 40, 48, 64, 96, 128, 256];
|
|
399
|
+
});
|
|
400
|
+
|
|
401
|
+
// packages/core/targets.ts
|
|
29
402
|
function isBuildTarget(value) {
|
|
30
403
|
return BUILD_TARGETS.includes(value);
|
|
31
404
|
}
|
|
@@ -45,34 +418,50 @@ function nativeRelativePath(target) {
|
|
|
45
418
|
const config = TARGET_CONFIGS[target];
|
|
46
419
|
return `native/${config.nativeDirectory}/${config.nativeName}`;
|
|
47
420
|
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
421
|
+
var BUILD_TARGETS, TARGET_CONFIGS;
|
|
422
|
+
var init_targets = __esm(() => {
|
|
423
|
+
BUILD_TARGETS = ["windows-x64", "linux-x64"];
|
|
424
|
+
TARGET_CONFIGS = {
|
|
425
|
+
"windows-x64": {
|
|
426
|
+
platform: "win32",
|
|
427
|
+
arch: "x64",
|
|
428
|
+
bunTarget: "bun-windows-x64",
|
|
429
|
+
nativeDirectory: "win32-x64",
|
|
430
|
+
nativeName: "tarve_native.dll",
|
|
431
|
+
executableSuffix: ".exe"
|
|
432
|
+
},
|
|
433
|
+
"linux-x64": {
|
|
434
|
+
platform: "linux",
|
|
435
|
+
arch: "x64",
|
|
436
|
+
bunTarget: "bun-linux-x64",
|
|
437
|
+
nativeDirectory: "linux-x64",
|
|
438
|
+
nativeName: "libtarve_native.so",
|
|
439
|
+
executableSuffix: ""
|
|
440
|
+
}
|
|
441
|
+
};
|
|
442
|
+
});
|
|
54
443
|
|
|
55
444
|
// packages/protocol/src/index.ts
|
|
56
|
-
var NATIVE_ABI_VERSION = 5;
|
|
57
|
-
var PROTOCOL_VERSION = 47;
|
|
445
|
+
var NATIVE_ABI_VERSION = 5, PROTOCOL_VERSION = 58;
|
|
58
446
|
|
|
59
447
|
// packages/core/native-runtime.ts
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
448
|
+
import { createHash } from "crypto";
|
|
449
|
+
import { existsSync as existsSync2 } from "fs";
|
|
450
|
+
import { readFile, writeFile, glob } from "fs/promises";
|
|
451
|
+
import { basename as basename2, dirname as dirname2, join as join2 } from "path";
|
|
63
452
|
async function sha256File(path) {
|
|
64
453
|
return createHash("sha256").update(await readFile(path)).digest("hex");
|
|
65
454
|
}
|
|
66
455
|
async function addGlobFiles(files, root, prefix, pattern) {
|
|
67
|
-
const directory =
|
|
68
|
-
if (!
|
|
456
|
+
const directory = join2(root, "native", prefix);
|
|
457
|
+
if (!existsSync2(directory))
|
|
69
458
|
return;
|
|
70
|
-
for await (const relative of
|
|
71
|
-
files.push(
|
|
459
|
+
for await (const relative of glob(pattern, { cwd: directory })) {
|
|
460
|
+
files.push(join2(prefix, relative).replaceAll("\\", "/"));
|
|
72
461
|
}
|
|
73
462
|
}
|
|
74
463
|
async function nativeSourceFingerprint(root) {
|
|
75
|
-
const native =
|
|
464
|
+
const native = join2(root, "native");
|
|
76
465
|
const files = ["Cargo.toml", "Cargo.lock"];
|
|
77
466
|
await addGlobFiles(files, root, "src", "**/*.rs");
|
|
78
467
|
await addGlobFiles(files, root, "vendor", "**/*.rs");
|
|
@@ -80,8 +469,8 @@ async function nativeSourceFingerprint(root) {
|
|
|
80
469
|
files.sort();
|
|
81
470
|
const hash = createHash("sha256");
|
|
82
471
|
for (const relative of files) {
|
|
83
|
-
const path =
|
|
84
|
-
if (!
|
|
472
|
+
const path = join2(native, ...relative.split("/"));
|
|
473
|
+
if (!existsSync2(path))
|
|
85
474
|
throw new Error(`Native source input is missing: ${path}`);
|
|
86
475
|
hash.update(relative);
|
|
87
476
|
hash.update("\x00");
|
|
@@ -93,8 +482,8 @@ async function nativeSourceFingerprint(root) {
|
|
|
93
482
|
}
|
|
94
483
|
async function createNativeCompatibilityManifest(root, packageVersion) {
|
|
95
484
|
const [rustProtocol, rustBridge] = await Promise.all([
|
|
96
|
-
readFile(
|
|
97
|
-
readFile(
|
|
485
|
+
readFile(join2(root, "native", "src", "protocol.rs"), "utf8"),
|
|
486
|
+
readFile(join2(root, "native", "src", "bridge.rs"), "utf8")
|
|
98
487
|
]);
|
|
99
488
|
const rustProtocolVersion = Number(rustProtocol.match(/pub const VERSION: u32 = (\d+);/)?.[1]);
|
|
100
489
|
const rustAbiVersion = Number(rustBridge.match(/pub const ABI_VERSION: u32 = (\d+);/)?.[1]);
|
|
@@ -121,7 +510,7 @@ function validateCompatibilityShape(value, path) {
|
|
|
121
510
|
}
|
|
122
511
|
}
|
|
123
512
|
async function readNativeCompatibilityManifest(path) {
|
|
124
|
-
if (!
|
|
513
|
+
if (!existsSync2(path))
|
|
125
514
|
throw new Error(`Tarve native compatibility metadata is missing: ${path}`);
|
|
126
515
|
let value;
|
|
127
516
|
try {
|
|
@@ -133,8 +522,8 @@ async function readNativeCompatibilityManifest(path) {
|
|
|
133
522
|
return value;
|
|
134
523
|
}
|
|
135
524
|
async function assertNativeRuntimeCompatible(library, target, expected) {
|
|
136
|
-
const path =
|
|
137
|
-
if (!
|
|
525
|
+
const path = join2(dirname2(library), NATIVE_RUNTIME_MANIFEST_NAME);
|
|
526
|
+
if (!existsSync2(path)) {
|
|
138
527
|
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
528
|
}
|
|
140
529
|
let value;
|
|
@@ -159,12 +548,327 @@ async function assertNativeRuntimeCompatible(library, target, expected) {
|
|
|
159
548
|
}
|
|
160
549
|
return manifest;
|
|
161
550
|
}
|
|
551
|
+
var NATIVE_RUNTIME_MANIFEST_NAME = "runtime.json", NATIVE_COMPATIBILITY_MANIFEST_NAME = "native-runtime.json", NATIVE_RUNTIME_MANIFEST_SCHEMA = 1;
|
|
552
|
+
var init_native_runtime = __esm(() => {
|
|
553
|
+
init_targets();
|
|
554
|
+
});
|
|
555
|
+
|
|
556
|
+
// packages/core/esbuild-file-imports.ts
|
|
557
|
+
import { readFileSync as readFileSync2 } from "fs";
|
|
558
|
+
import { dirname as dirname3, resolve as resolve2 } from "path";
|
|
559
|
+
var ASSET, fileImportAttributes;
|
|
560
|
+
var init_esbuild_file_imports = __esm(() => {
|
|
561
|
+
ASSET = /\.(?:png|jpe?g|webp|gif|svg)$/i;
|
|
562
|
+
fileImportAttributes = {
|
|
563
|
+
name: "tarve-file-import-attributes",
|
|
564
|
+
setup(build) {
|
|
565
|
+
build.onResolve({ filter: /.*/ }, (args) => {
|
|
566
|
+
if (args.namespace === "tarve-file") {
|
|
567
|
+
return args.path.startsWith("node:") ? { path: args.path, external: true } : { path: args.path, namespace: "tarve-file-copy" };
|
|
568
|
+
}
|
|
569
|
+
const type = args.with?.type ?? (ASSET.test(args.path) && args.kind === "import-statement" ? "file" : undefined);
|
|
570
|
+
if (type !== "file" && type !== "text")
|
|
571
|
+
return;
|
|
572
|
+
return { path: resolve2(dirname3(args.importer), args.path), namespace: `tarve-${type}` };
|
|
573
|
+
});
|
|
574
|
+
build.onLoad({ filter: /.*/, namespace: "tarve-text" }, (args) => ({ contents: readFileSync2(args.path, "utf8"), loader: "text" }));
|
|
575
|
+
build.onLoad({ filter: /.*/, namespace: "tarve-file" }, (args) => ({
|
|
576
|
+
contents: [
|
|
577
|
+
`import relative from ${JSON.stringify(args.path)};`,
|
|
578
|
+
`import { join } from "node:path";`,
|
|
579
|
+
`export default join(import.meta.dirname, relative);`
|
|
580
|
+
].join(`
|
|
581
|
+
`),
|
|
582
|
+
loader: "js",
|
|
583
|
+
resolveDir: dirname3(args.path)
|
|
584
|
+
}));
|
|
585
|
+
build.onLoad({ filter: /.*/, namespace: "tarve-file-copy" }, (args) => ({
|
|
586
|
+
contents: readFileSync2(args.path),
|
|
587
|
+
loader: "file"
|
|
588
|
+
}));
|
|
589
|
+
}
|
|
590
|
+
};
|
|
591
|
+
});
|
|
592
|
+
|
|
593
|
+
// packages/core/node-target.ts
|
|
594
|
+
import { createHash as createHash2, randomUUID } from "crypto";
|
|
595
|
+
import { chmodSync, existsSync as existsSync3, mkdirSync as mkdirSync2, readFileSync as readFileSync3, renameSync, rmSync, writeFileSync as writeFileSync2 } from "fs";
|
|
596
|
+
import { homedir } from "os";
|
|
597
|
+
import { join as join3 } from "path";
|
|
598
|
+
import { gunzipSync } from "zlib";
|
|
599
|
+
function distribution(target, version) {
|
|
600
|
+
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" };
|
|
601
|
+
}
|
|
602
|
+
function cacheRoot() {
|
|
603
|
+
if (process.env.TARVE_CACHE_DIR)
|
|
604
|
+
return process.env.TARVE_CACHE_DIR;
|
|
605
|
+
if (process.platform === "win32")
|
|
606
|
+
return join3(process.env.LOCALAPPDATA ?? join3(homedir(), "AppData", "Local"), "tarve", "cache");
|
|
607
|
+
return join3(process.env.XDG_CACHE_HOME ?? join3(homedir(), ".cache"), "tarve");
|
|
608
|
+
}
|
|
609
|
+
function mirror() {
|
|
610
|
+
return (process.env.NODEJS_ORG_MIRROR ?? "https://nodejs.org/dist").replace(/\/+$/, "");
|
|
611
|
+
}
|
|
612
|
+
async function download(url) {
|
|
613
|
+
const response = await fetch(url);
|
|
614
|
+
if (!response.ok)
|
|
615
|
+
throw new Error(`Could not download ${url}: HTTP ${response.status}`);
|
|
616
|
+
return Buffer.from(await response.arrayBuffer());
|
|
617
|
+
}
|
|
618
|
+
function extractTarMember(archive, member) {
|
|
619
|
+
let offset = 0;
|
|
620
|
+
let longName;
|
|
621
|
+
while (offset + 512 <= archive.length) {
|
|
622
|
+
const header = archive.subarray(offset, offset + 512);
|
|
623
|
+
if (header.every((byte) => byte === 0))
|
|
624
|
+
break;
|
|
625
|
+
const field = (start, length) => header.toString("utf8", start, start + length).replace(/\0.*$/s, "");
|
|
626
|
+
const size = Number.parseInt(field(124, 12).trim() || "0", 8);
|
|
627
|
+
const type = field(156, 1) || "0";
|
|
628
|
+
const prefix = field(345, 155);
|
|
629
|
+
const name = longName ?? (prefix ? `${prefix}/${field(0, 100)}` : field(0, 100));
|
|
630
|
+
const body = archive.subarray(offset + 512, offset + 512 + size);
|
|
631
|
+
longName = undefined;
|
|
632
|
+
if (type === "L")
|
|
633
|
+
longName = body.toString("utf8").replace(/\0.*$/s, "");
|
|
634
|
+
else if (type === "x")
|
|
635
|
+
longName = /\d+ path=([^\n]*)\n/.exec(body.toString("utf8"))?.[1];
|
|
636
|
+
else if ((type === "0" || type === "\x00") && name.replace(/^\.\//, "") === member)
|
|
637
|
+
return Buffer.from(body);
|
|
638
|
+
offset += 512 + Math.ceil(size / 512) * 512;
|
|
639
|
+
}
|
|
640
|
+
throw new Error(`${member} not found in the Node.js archive`);
|
|
641
|
+
}
|
|
642
|
+
function assertNodeVersion(executable, version) {
|
|
643
|
+
if (!readFileSync3(executable).includes(`node-v${version}`)) {
|
|
644
|
+
throw new Error(`${executable} is not Node.js v${version}; the target executable must match the Node.js that builds the app`);
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
async function targetNodeExecutable(target, version) {
|
|
648
|
+
const { file, member, executable } = distribution(target, version);
|
|
649
|
+
const directory = join3(cacheRoot(), "node", `v${version}`, target);
|
|
650
|
+
const path = join3(directory, executable);
|
|
651
|
+
if (existsSync3(path))
|
|
652
|
+
return path;
|
|
653
|
+
const base = `${mirror()}/v${version}`;
|
|
654
|
+
const sums = (await download(`${base}/SHASUMS256.txt`)).toString("utf8");
|
|
655
|
+
const expected = sums.split(/\r?\n/).find((line) => line.endsWith(` ${file}`))?.split(/\s+/)[0];
|
|
656
|
+
if (!expected)
|
|
657
|
+
throw new Error(`${file} is not listed in the Node.js v${version} SHASUMS256.txt`);
|
|
658
|
+
const payload = await download(`${base}/${file}`);
|
|
659
|
+
const actual = createHash2("sha256").update(payload).digest("hex");
|
|
660
|
+
if (actual !== expected)
|
|
661
|
+
throw new Error(`Checksum mismatch for ${file}: expected ${expected}, got ${actual}`);
|
|
662
|
+
const binary = member ? extractTarMember(gunzipSync(payload), member) : payload;
|
|
663
|
+
mkdirSync2(directory, { recursive: true });
|
|
664
|
+
const temporary = join3(directory, `${executable}.${randomUUID()}.tmp`);
|
|
665
|
+
try {
|
|
666
|
+
writeFileSync2(temporary, binary, { mode: 493 });
|
|
667
|
+
try {
|
|
668
|
+
renameSync(temporary, path);
|
|
669
|
+
} catch (error) {
|
|
670
|
+
if (!existsSync3(path))
|
|
671
|
+
throw error;
|
|
672
|
+
}
|
|
673
|
+
} finally {
|
|
674
|
+
rmSync(temporary, { force: true });
|
|
675
|
+
}
|
|
676
|
+
if (process.platform !== "win32")
|
|
677
|
+
chmodSync(path, 493);
|
|
678
|
+
return path;
|
|
679
|
+
}
|
|
680
|
+
var init_node_target = () => {};
|
|
681
|
+
|
|
682
|
+
// packages/core/windows-executable.ts
|
|
683
|
+
var exports_windows_executable = {};
|
|
684
|
+
__export(exports_windows_executable, {
|
|
685
|
+
brandWindowsExecutable: () => brandWindowsExecutable
|
|
686
|
+
});
|
|
687
|
+
import { readFileSync as readFileSync4, writeFileSync as writeFileSync3 } from "fs";
|
|
688
|
+
import { Data, NtExecutable, NtExecutableResource, Resource } from "resedit";
|
|
689
|
+
function numericVersion(version) {
|
|
690
|
+
const parts = version.split(/[.+-]/).slice(0, 4).map((part) => Number.parseInt(part, 10));
|
|
691
|
+
return [0, 1, 2, 3].map((index) => {
|
|
692
|
+
const value = parts[index];
|
|
693
|
+
return Number.isInteger(value) && value >= 0 && value <= 65535 ? value : 0;
|
|
694
|
+
});
|
|
695
|
+
}
|
|
696
|
+
function brandWindowsExecutable(path, info) {
|
|
697
|
+
const executable = NtExecutable.from(readFileSync4(path), { ignoreCert: true });
|
|
698
|
+
const resources = NtExecutableResource.from(executable);
|
|
699
|
+
const versionInfo = Resource.VersionInfo.fromEntries(resources.entries)[0] ?? Resource.VersionInfo.createEmpty();
|
|
700
|
+
const languages = versionInfo.getAllLanguagesForStringValues();
|
|
701
|
+
if (languages.length === 0)
|
|
702
|
+
languages.push({ lang: 1033, codepage: 1200 });
|
|
703
|
+
const [major, minor, patch, revision] = numericVersion(info.version);
|
|
704
|
+
const text = `${major}.${minor}.${patch}.${revision}`;
|
|
705
|
+
for (const language of languages) {
|
|
706
|
+
for (const key of ["CompanyName", "LegalCopyright", "LegalTrademarks", "Comments", "PrivateBuild", "SpecialBuild"]) {
|
|
707
|
+
versionInfo.removeStringValue(language, key);
|
|
708
|
+
}
|
|
709
|
+
versionInfo.setStringValues(language, {
|
|
710
|
+
FileDescription: info.description ?? `${info.name} \u2014 native Tarve application`,
|
|
711
|
+
...info.publisher ? { CompanyName: info.publisher } : {},
|
|
712
|
+
...info.copyright ? { LegalCopyright: info.copyright } : {},
|
|
713
|
+
ProductName: info.name,
|
|
714
|
+
InternalName: info.name,
|
|
715
|
+
OriginalFilename: info.filename,
|
|
716
|
+
FileVersion: text,
|
|
717
|
+
ProductVersion: text
|
|
718
|
+
});
|
|
719
|
+
}
|
|
720
|
+
versionInfo.setFileVersion(major, minor, patch, revision);
|
|
721
|
+
versionInfo.setProductVersion(major, minor, patch, revision);
|
|
722
|
+
versionInfo.outputToResourceEntries(resources.entries);
|
|
723
|
+
if (info.ico) {
|
|
724
|
+
const groups = Resource.IconGroupEntry.fromEntries(resources.entries);
|
|
725
|
+
const group = groups[0];
|
|
726
|
+
const icons = Data.IconFile.from(info.ico).icons.map((icon) => icon.data);
|
|
727
|
+
Resource.IconGroupEntry.replaceIconsForResource(resources.entries, group?.id ?? 1, group?.lang ?? 1033, icons);
|
|
728
|
+
}
|
|
729
|
+
resources.outputResource(executable);
|
|
730
|
+
const output = Buffer.from(executable.generate());
|
|
731
|
+
output.writeUInt16LE(IMAGE_SUBSYSTEM_WINDOWS_GUI, output.readUInt32LE(60) + 24 + 68);
|
|
732
|
+
writeFileSync3(path, output);
|
|
733
|
+
}
|
|
734
|
+
var IMAGE_SUBSYSTEM_WINDOWS_GUI = 2;
|
|
735
|
+
var init_windows_executable = () => {};
|
|
736
|
+
|
|
737
|
+
// packages/core/node-build.ts
|
|
738
|
+
import { execFileSync } from "child_process";
|
|
739
|
+
import { existsSync as existsSync4, mkdtempSync, writeFileSync as writeFileSync4, readFileSync as readFileSync5 } from "fs";
|
|
740
|
+
import { mkdir, rm } from "fs/promises";
|
|
741
|
+
import { tmpdir } from "os";
|
|
742
|
+
import { basename as basename3, dirname as dirname4, join as join4, relative as relative2, resolve as resolve3 } from "path";
|
|
743
|
+
import { nativePath } from "#tarve/runtime";
|
|
744
|
+
async function buildNode(options) {
|
|
745
|
+
const target = options.target ?? hostBuildTarget();
|
|
746
|
+
if (!target)
|
|
747
|
+
throw new Error(`Unsupported Node.js build host: ${process.platform}-${process.arch}`);
|
|
748
|
+
const config = targetConfig(target);
|
|
749
|
+
const node = options.nodeExecutable ?? (process.versions.bun ? Bun.which("node") ?? "node" : process.execPath);
|
|
750
|
+
const version = execFileSync(node, ["--version"], { encoding: "utf8" }).trim();
|
|
751
|
+
const match = /^v(\d+)\.(\d+)\./.exec(version);
|
|
752
|
+
if (!match || Number(match[1]) < 26 || Number(match[1]) === 26 && Number(match[2]) < 10) {
|
|
753
|
+
throw new Error(`Node.js 26.10+ is required for node:ffi inside a SEA (found ${version})`);
|
|
754
|
+
}
|
|
755
|
+
const { metadata, images, define } = buildMetadata(options.entrypoint, metadataOverrides(options));
|
|
756
|
+
const outfile = resolve3(options.outfile ?? `dist/${metadata.name}${config.executableSuffix}`);
|
|
757
|
+
let executable;
|
|
758
|
+
if (options.targetExecutable) {
|
|
759
|
+
executable = resolve3(options.targetExecutable);
|
|
760
|
+
if (!existsSync4(executable))
|
|
761
|
+
throw new Error(`Target Node.js executable is missing: ${executable}`);
|
|
762
|
+
assertNodeVersion(executable, version.slice(1));
|
|
763
|
+
}
|
|
764
|
+
const library = resolve3(options.nativeLibrary ?? (target === hostBuildTarget() ? nativePath() : join4(import.meta.dirname, "../../native", config.nativeDirectory, config.nativeName)));
|
|
765
|
+
if (!existsSync4(library))
|
|
766
|
+
throw new Error(`Tarve native library missing: ${library}`);
|
|
767
|
+
const temporary = mkdtempSync(join4(tmpdir(), "tarve-node-build-"));
|
|
768
|
+
try {
|
|
769
|
+
const root = import.meta.dirname;
|
|
770
|
+
const source = existsSync4(join4(root, "src/index.ts"));
|
|
771
|
+
if (!options.nativeLibrary && (!source || target !== hostBuildTarget())) {
|
|
772
|
+
const expected = source ? await createNativeCompatibilityManifest(resolve3(root, "../.."), JSON.parse(readFileSync5(resolve3(root, "../../package.json"), "utf8")).version) : await readNativeCompatibilityManifest(join4(root, NATIVE_COMPATIBILITY_MANIFEST_NAME));
|
|
773
|
+
await assertNativeRuntimeCompatible(library, target, expected);
|
|
774
|
+
}
|
|
775
|
+
if (!executable && target !== hostBuildTarget()) {
|
|
776
|
+
executable = await targetNodeExecutable(target, version.slice(1));
|
|
777
|
+
}
|
|
778
|
+
const { build: bundle } = await import("esbuild");
|
|
779
|
+
const result = await bundle({
|
|
780
|
+
entryPoints: [resolve3(options.entrypoint)],
|
|
781
|
+
outfile: join4(temporary, "app.mjs"),
|
|
782
|
+
bundle: true,
|
|
783
|
+
platform: "node",
|
|
784
|
+
format: "esm",
|
|
785
|
+
target: "node26",
|
|
786
|
+
jsx: "automatic",
|
|
787
|
+
jsxImportSource: "@tarve/core",
|
|
788
|
+
loader: { ".png": "file", ".jpg": "file", ".jpeg": "file", ".webp": "file", ".gif": "file", ".svg": "file" },
|
|
789
|
+
metafile: true,
|
|
790
|
+
define,
|
|
791
|
+
external: ["esbuild"],
|
|
792
|
+
plugins: [fileImportAttributes, {
|
|
793
|
+
name: "tarve-node-runtime",
|
|
794
|
+
setup(build) {
|
|
795
|
+
if (source) {
|
|
796
|
+
build.onResolve({ filter: /^@tarve\/core(?:\/(?:jsx-runtime|jsx-dev-runtime))?$/ }, (args) => {
|
|
797
|
+
const entry = args.path.slice("@tarve/core".length) || "/index";
|
|
798
|
+
return { path: join4(root, "src", `${entry.slice(1)}.ts`) };
|
|
799
|
+
});
|
|
800
|
+
}
|
|
801
|
+
build.onResolve({ filter: /^#tarve\/runtime$/ }, () => ({ path: "native-runtime", namespace: "tarve-node" }));
|
|
802
|
+
build.onLoad({ filter: /.*/, namespace: "tarve-node" }, () => ({
|
|
803
|
+
contents: `import { join } from "node:path"; export function nativePath() { return join(import.meta.dirname, ${JSON.stringify(config.nativeName)}); }`,
|
|
804
|
+
loader: "js"
|
|
805
|
+
}));
|
|
806
|
+
}
|
|
807
|
+
}]
|
|
808
|
+
});
|
|
809
|
+
const assets = { [config.nativeName]: seaPath(library) };
|
|
810
|
+
for (const [path, output] of Object.entries(result.metafile.outputs)) {
|
|
811
|
+
if (!output.entryPoint)
|
|
812
|
+
assets[relative2(temporary, resolve3(path)).replaceAll("\\", "/")] = seaPath(resolve3(path));
|
|
813
|
+
}
|
|
814
|
+
const sea = join4(temporary, "sea.json");
|
|
815
|
+
writeFileSync4(sea, JSON.stringify({
|
|
816
|
+
main: seaPath(join4(temporary, "app.mjs")),
|
|
817
|
+
mainFormat: "module",
|
|
818
|
+
output: seaPath(outfile),
|
|
819
|
+
...executable ? { executable: seaPath(executable) } : {},
|
|
820
|
+
useVfs: true,
|
|
821
|
+
useCodeCache: false,
|
|
822
|
+
useSnapshot: false,
|
|
823
|
+
assets,
|
|
824
|
+
execArgv: ["--disable-warning=ExperimentalWarning"],
|
|
825
|
+
execArgvExtension: "env"
|
|
826
|
+
}));
|
|
827
|
+
await mkdir(dirname4(outfile), { recursive: true });
|
|
828
|
+
await rm(outfile, { force: true });
|
|
829
|
+
execFileSync(node, ["--build-sea", sea], { stdio: "pipe" });
|
|
830
|
+
if (config.platform === "win32") {
|
|
831
|
+
const tarveVersion = JSON.parse(readFileSync5(resolve3(root, "../../package.json"), "utf8")).version;
|
|
832
|
+
(await Promise.resolve().then(() => (init_windows_executable(), exports_windows_executable))).brandWindowsExecutable(outfile, {
|
|
833
|
+
name: metadata.name,
|
|
834
|
+
version: metadata.version ?? tarveVersion,
|
|
835
|
+
filename: basename3(outfile),
|
|
836
|
+
description: metadata.description,
|
|
837
|
+
publisher: metadata.publisher,
|
|
838
|
+
copyright: metadata.copyright,
|
|
839
|
+
ico: images?.ico
|
|
840
|
+
});
|
|
841
|
+
}
|
|
842
|
+
if (config.platform === "linux" && (metadata.icon || metadata.appId))
|
|
843
|
+
writeLinuxDesktopEntry(outfile, metadata, images?.png);
|
|
844
|
+
return outfile;
|
|
845
|
+
} finally {
|
|
846
|
+
await rm(temporary, { recursive: true, force: true });
|
|
847
|
+
}
|
|
848
|
+
}
|
|
849
|
+
function seaPath(path) {
|
|
850
|
+
return path.replaceAll("\\", "/");
|
|
851
|
+
}
|
|
852
|
+
var init_node_build = __esm(() => {
|
|
853
|
+
init_esbuild_file_imports();
|
|
854
|
+
init_targets();
|
|
855
|
+
init_build();
|
|
856
|
+
init_app_metadata();
|
|
857
|
+
init_native_runtime();
|
|
858
|
+
init_node_target();
|
|
859
|
+
});
|
|
162
860
|
|
|
163
861
|
// packages/core/build.ts
|
|
862
|
+
import { existsSync as existsSync5, mkdtempSync as mkdtempSync2, writeFileSync as writeFileSync5 } from "fs";
|
|
863
|
+
import { mkdir as mkdir2, rm as rm2 } from "fs/promises";
|
|
864
|
+
import { tmpdir as tmpdir2 } from "os";
|
|
865
|
+
import { dirname as dirname5, join as join5, resolve as resolve4 } from "path";
|
|
866
|
+
import { fileURLToPath } from "url";
|
|
867
|
+
import { nativePath as nativePath2 } from "#tarve/runtime";
|
|
164
868
|
function resolveNativeLibrary(target, override) {
|
|
165
869
|
if (override) {
|
|
166
|
-
const path =
|
|
167
|
-
if (!
|
|
870
|
+
const path = resolve4(override);
|
|
871
|
+
if (!existsSync5(path))
|
|
168
872
|
throw new Error(`Tarve native library does not exist: ${path}`);
|
|
169
873
|
return path;
|
|
170
874
|
}
|
|
@@ -172,48 +876,58 @@ function resolveNativeLibrary(target, override) {
|
|
|
172
876
|
const hostTarget = hostBuildTarget();
|
|
173
877
|
if (hostTarget === target) {
|
|
174
878
|
try {
|
|
175
|
-
return
|
|
879
|
+
return nativePath2();
|
|
176
880
|
} catch {}
|
|
177
881
|
}
|
|
178
882
|
const candidates = [
|
|
179
|
-
|
|
180
|
-
|
|
883
|
+
join5(import.meta.dir, config.nativeName),
|
|
884
|
+
resolve4(import.meta.dir, "../../native", config.nativeDirectory, config.nativeName)
|
|
181
885
|
];
|
|
182
|
-
const found = candidates.find(
|
|
886
|
+
const found = candidates.find(existsSync5);
|
|
183
887
|
if (found)
|
|
184
888
|
return found;
|
|
185
889
|
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
890
|
}
|
|
187
891
|
async function build(options) {
|
|
892
|
+
if (options.runtime === "node") {
|
|
893
|
+
await Promise.resolve().then(() => init_node_build());
|
|
894
|
+
return buildNode(options);
|
|
895
|
+
}
|
|
188
896
|
const target = options.target ?? hostBuildTarget();
|
|
189
897
|
if (!target)
|
|
190
898
|
throw new Error(`Tarve executable distribution is not supported for ${process.platform}-${process.arch}. Pass an explicit supported target when cross-compiling.`);
|
|
191
899
|
if (!isBuildTarget(target))
|
|
192
900
|
throw new TypeError(`Unsupported Tarve build target: ${String(target)}. Expected ${BUILD_TARGETS.join(" or ")}.`);
|
|
193
901
|
const config = targetConfig(target);
|
|
194
|
-
const entrypoint =
|
|
195
|
-
const packageMetadata = await Bun.file(
|
|
902
|
+
const entrypoint = resolve4(options.entrypoint);
|
|
903
|
+
const packageMetadata = await Bun.file(resolve4(import.meta.dir, "../../package.json")).json();
|
|
196
904
|
if (typeof packageMetadata.version !== "string" || packageMetadata.version.length === 0) {
|
|
197
905
|
throw new Error("Tarve package version is missing.");
|
|
198
906
|
}
|
|
199
|
-
const
|
|
200
|
-
const
|
|
907
|
+
const { metadata, images, define } = buildMetadata(entrypoint, metadataOverrides(options));
|
|
908
|
+
const name = metadata.name;
|
|
909
|
+
const outfile = resolve4(options.outfile ?? `dist/${name}${config.executableSuffix}`);
|
|
201
910
|
const library = resolveNativeLibrary(target, options.nativeLibrary);
|
|
202
911
|
const assetsModule = fileURLToPath(import.meta.resolve("#tarve/assets"));
|
|
203
|
-
const sourceDirectory =
|
|
204
|
-
const repositorySource =
|
|
205
|
-
const expectedCompatibility = repositorySource ? await createNativeCompatibilityManifest(
|
|
912
|
+
const sourceDirectory = join5(import.meta.dir, "src");
|
|
913
|
+
const repositorySource = existsSync5(join5(sourceDirectory, "index.ts"));
|
|
914
|
+
const expectedCompatibility = repositorySource ? await createNativeCompatibilityManifest(resolve4(import.meta.dir, "../.."), packageMetadata.version) : await readNativeCompatibilityManifest(join5(import.meta.dir, NATIVE_COMPATIBILITY_MANIFEST_NAME));
|
|
206
915
|
const requireRuntimeManifest = target !== hostBuildTarget() || !options.nativeLibrary && !repositorySource;
|
|
207
916
|
if (requireRuntimeManifest) {
|
|
208
917
|
await assertNativeRuntimeCompatible(library, target, expectedCompatibility);
|
|
209
918
|
}
|
|
919
|
+
const libraryBytes = await Bun.file(library).bytes();
|
|
920
|
+
const libraryDigest = {
|
|
921
|
+
sha256: new Bun.CryptoHasher("sha256").update(libraryBytes).digest("hex"),
|
|
922
|
+
size: libraryBytes.byteLength
|
|
923
|
+
};
|
|
210
924
|
const runtime = {
|
|
211
925
|
name: "tarve-native-runtime",
|
|
212
926
|
setup(builder) {
|
|
213
927
|
if (repositorySource) {
|
|
214
|
-
builder.onResolve({ filter: /^tarve$/ }, () => ({ path:
|
|
215
|
-
builder.onResolve({ filter: /^tarve\/jsx-runtime$/ }, () => ({ path:
|
|
216
|
-
builder.onResolve({ filter: /^tarve\/jsx-dev-runtime$/ }, () => ({ path:
|
|
928
|
+
builder.onResolve({ filter: /^tarve$/ }, () => ({ path: join5(sourceDirectory, "index.ts") }));
|
|
929
|
+
builder.onResolve({ filter: /^tarve\/jsx-runtime$/ }, () => ({ path: join5(sourceDirectory, "jsx-runtime.ts") }));
|
|
930
|
+
builder.onResolve({ filter: /^tarve\/jsx-dev-runtime$/ }, () => ({ path: join5(sourceDirectory, "jsx-dev-runtime.ts") }));
|
|
217
931
|
}
|
|
218
932
|
builder.onResolve({ filter: /^#tarve\/runtime$/ }, () => ({ path: "runtime", namespace: "tarve" }));
|
|
219
933
|
builder.onLoad({ filter: /^runtime$/, namespace: "tarve" }, () => ({
|
|
@@ -221,38 +935,70 @@ async function build(options) {
|
|
|
221
935
|
contents: [
|
|
222
936
|
`import library from ${JSON.stringify(library)} with { type: "file" };`,
|
|
223
937
|
`import { materializeAsset } from ${JSON.stringify(assetsModule)};`,
|
|
224
|
-
`
|
|
938
|
+
`const digest = ${JSON.stringify(libraryDigest)};`,
|
|
939
|
+
`export function nativePath() { return materializeAsset(library, ${JSON.stringify(config.nativeName)}, digest); }`
|
|
225
940
|
].join(`
|
|
226
941
|
`)
|
|
227
942
|
}));
|
|
228
943
|
}
|
|
229
944
|
};
|
|
230
|
-
await
|
|
231
|
-
const
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
target:
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
945
|
+
await mkdir2(dirname5(outfile), { recursive: true });
|
|
946
|
+
const iconDirectory = images && config.platform === "win32" ? mkdtempSync2(join5(tmpdir2(), "tarve-icon-")) : undefined;
|
|
947
|
+
const icoPath = iconDirectory ? join5(iconDirectory, "app.ico") : undefined;
|
|
948
|
+
if (icoPath)
|
|
949
|
+
writeFileSync5(icoPath, images.ico);
|
|
950
|
+
try {
|
|
951
|
+
const result = await Bun.build({
|
|
952
|
+
entrypoints: [entrypoint],
|
|
953
|
+
target: "bun",
|
|
954
|
+
plugins: [runtime],
|
|
955
|
+
define: { "process.env.NODE_ENV": JSON.stringify("production"), ...define },
|
|
956
|
+
minify: true,
|
|
957
|
+
compile: {
|
|
958
|
+
target: config.bunTarget,
|
|
959
|
+
outfile,
|
|
960
|
+
autoloadDotenv: false,
|
|
961
|
+
autoloadBunfig: false,
|
|
962
|
+
...config.platform === "win32" ? {
|
|
963
|
+
windows: {
|
|
964
|
+
hideConsole: true,
|
|
965
|
+
title: name,
|
|
966
|
+
version: metadata.version ?? packageMetadata.version,
|
|
967
|
+
description: metadata.description ?? `${name} \u2014 native Tarve application`,
|
|
968
|
+
...metadata.publisher ? { publisher: metadata.publisher } : {},
|
|
969
|
+
...metadata.copyright ? { copyright: metadata.copyright } : {},
|
|
970
|
+
...icoPath ? { icon: icoPath } : {}
|
|
971
|
+
}
|
|
972
|
+
} : {}
|
|
973
|
+
}
|
|
974
|
+
});
|
|
975
|
+
if (!result.success)
|
|
976
|
+
throw new AggregateError(result.logs, "Could not compile the Tarve application");
|
|
977
|
+
} finally {
|
|
978
|
+
if (iconDirectory)
|
|
979
|
+
await rm2(iconDirectory, { recursive: true, force: true });
|
|
980
|
+
}
|
|
981
|
+
if (config.platform === "linux" && (metadata.icon || metadata.appId))
|
|
982
|
+
writeLinuxDesktopEntry(outfile, metadata, images?.png);
|
|
249
983
|
return outfile;
|
|
250
984
|
}
|
|
985
|
+
function metadataOverrides(options) {
|
|
986
|
+
return { name: options.name, version: options.version, description: options.description, publisher: options.publisher, copyright: options.copyright, icon: options.icon, appId: options.appId };
|
|
987
|
+
}
|
|
988
|
+
var init_build = __esm(() => {
|
|
989
|
+
init_app_metadata();
|
|
990
|
+
init_targets();
|
|
991
|
+
init_native_runtime();
|
|
992
|
+
init_targets();
|
|
993
|
+
});
|
|
994
|
+
init_build();
|
|
995
|
+
|
|
251
996
|
export {
|
|
252
997
|
BUILD_TARGETS,
|
|
253
998
|
build,
|
|
254
999
|
hostBuildTarget,
|
|
255
1000
|
isBuildTarget,
|
|
1001
|
+
metadataOverrides,
|
|
256
1002
|
nativeDirectoryForBuildTarget,
|
|
257
1003
|
nativeRelativePath,
|
|
258
1004
|
targetConfig
|