jsgotty 1.1.7 → 1.1.9
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/gotty.js +60 -76
- package/package.json +1 -1
- package/single-exe/README.md +47 -9
- package/single-exe/assetsHelper.js +59 -7
- package/single-exe/compiled.js +81 -12
- package/single-exe/entry.mjs +7 -0
- package/zmodem-node.js +14 -12
package/gotty.js
CHANGED
|
@@ -97,25 +97,30 @@ const DEFAULTS = {
|
|
|
97
97
|
const compiledHelperPromise = import("./single-exe/compiled.js").catch(() => null);
|
|
98
98
|
const assetsHelperPromise = import("./single-exe/assetsHelper.js").catch(() => null);
|
|
99
99
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
let
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
100
|
+
// Assets are addressed by the package-relative keys package.json declares
|
|
101
|
+
// ("static/index.html", "README.md"). assetsHelper turns a key into bytes,
|
|
102
|
+
// wherever they live: the embedded store, the compiled binary's virtual
|
|
103
|
+
// filesystem, or a file on disk. Nothing here builds an absolute path.
|
|
104
|
+
let assets = null;
|
|
105
|
+
|
|
106
|
+
// Node runs this file without the ESM-only helper. Then there is nothing
|
|
107
|
+
// embedded either, so a key is just a path under the source checkout.
|
|
108
|
+
function assetText(key) {
|
|
109
|
+
return assets
|
|
110
|
+
? assets.readAssetTextSync(key)
|
|
111
|
+
: fs.readFileSync(path.join(__dirname, key), "utf8");
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function assetFilePath(key) {
|
|
115
|
+
return assets ? assets.assetDiskPath(key) : path.join(__dirname, key);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// URL-relative path -> asset key, or null when it escapes `static/`.
|
|
119
|
+
// Normalizing against "/" is what makes "../../etc/passwd" unreachable.
|
|
120
|
+
function staticAssetKey(relativePath) {
|
|
121
|
+
const rel = path.posix.normalize(`/${String(relativePath ?? "")}`).slice(1);
|
|
122
|
+
if (!rel || rel === ".." || rel.startsWith("../")) return null;
|
|
123
|
+
return `static/${rel}`;
|
|
119
124
|
}
|
|
120
125
|
|
|
121
126
|
let cliBootstrapPromise = null;
|
|
@@ -247,8 +252,8 @@ Experimental:
|
|
|
247
252
|
--build-for <target> Build a Bun single-file executable for target`);
|
|
248
253
|
}
|
|
249
254
|
|
|
250
|
-
function printMarkdownFile(
|
|
251
|
-
const text =
|
|
255
|
+
function printMarkdownFile(key) {
|
|
256
|
+
const text = assetText(key);
|
|
252
257
|
const markdownAnsi =
|
|
253
258
|
typeof Bun !== "undefined" &&
|
|
254
259
|
Bun &&
|
|
@@ -259,10 +264,6 @@ function printMarkdownFile(filePath) {
|
|
|
259
264
|
console.log(markdownAnsi ? markdownAnsi(text, { hyperlinks: true }) : text);
|
|
260
265
|
}
|
|
261
266
|
|
|
262
|
-
function printWebHelp() {
|
|
263
|
-
printMarkdownFile(DEFAULT_HELP);
|
|
264
|
-
}
|
|
265
|
-
|
|
266
267
|
function fatal(message, code = 1) {
|
|
267
268
|
console.error(message);
|
|
268
269
|
process.exit(code);
|
|
@@ -510,15 +511,15 @@ function parseArgs(argv) {
|
|
|
510
511
|
process.exit(0);
|
|
511
512
|
break;
|
|
512
513
|
case "--help-web":
|
|
513
|
-
|
|
514
|
+
printMarkdownFile("static/help.md");
|
|
514
515
|
process.exit(0);
|
|
515
516
|
break;
|
|
516
517
|
case "--readme":
|
|
517
|
-
printMarkdownFile(
|
|
518
|
+
printMarkdownFile("README.md");
|
|
518
519
|
process.exit(0);
|
|
519
520
|
break;
|
|
520
521
|
case "--readme-en":
|
|
521
|
-
printMarkdownFile(
|
|
522
|
+
printMarkdownFile("README.en.md");
|
|
522
523
|
process.exit(0);
|
|
523
524
|
break;
|
|
524
525
|
case "--address":
|
|
@@ -664,36 +665,6 @@ function previewBuffer(buffer, limit = 96) {
|
|
|
664
665
|
});
|
|
665
666
|
}
|
|
666
667
|
|
|
667
|
-
function readTextFile(filePath) {
|
|
668
|
-
const bundledText = helperReadInternalAssetText?.(internalAssetPathFor(filePath));
|
|
669
|
-
if (bundledText == null) {
|
|
670
|
-
return fs.readFileSync(filePath, "utf8");
|
|
671
|
-
}
|
|
672
|
-
return bundledText;
|
|
673
|
-
}
|
|
674
|
-
|
|
675
|
-
function internalAssetPathFor(filePath) {
|
|
676
|
-
if (!helperAssetPath) {
|
|
677
|
-
return filePath;
|
|
678
|
-
}
|
|
679
|
-
|
|
680
|
-
const normalizedPath = path.normalize(filePath);
|
|
681
|
-
const staticRelativePath = path.relative(STATIC_ROOT, normalizedPath);
|
|
682
|
-
if (staticRelativePath && !staticRelativePath.startsWith("..") && !path.isAbsolute(staticRelativePath)) {
|
|
683
|
-
return helperAssetPath("static", staticRelativePath);
|
|
684
|
-
}
|
|
685
|
-
|
|
686
|
-
if (normalizedPath === DEFAULT_README) {
|
|
687
|
-
return helperAssetPath("README.md");
|
|
688
|
-
}
|
|
689
|
-
|
|
690
|
-
if (normalizedPath === DEFAULT_README_EN) {
|
|
691
|
-
return helperAssetPath("README.en.md");
|
|
692
|
-
}
|
|
693
|
-
|
|
694
|
-
return filePath;
|
|
695
|
-
}
|
|
696
|
-
|
|
697
668
|
function contentType(filePath) {
|
|
698
669
|
const ext = path.extname(filePath).toLowerCase();
|
|
699
670
|
switch (ext) {
|
|
@@ -1357,10 +1328,26 @@ class NodePtyBackend {
|
|
|
1357
1328
|
constructor(options) {
|
|
1358
1329
|
let nodePty;
|
|
1359
1330
|
try {
|
|
1331
|
+
// Split so the bundler cannot resolve it. This branch is unreachable
|
|
1332
|
+
// in a compiled binary (Bun >= 1.3.14 always takes BunPtyBackend), so
|
|
1333
|
+
// a static require would only bake ~54 KB of dead JS into the exe —
|
|
1334
|
+
// and node-pty's own `.node` loading is dynamic anyway, so it would
|
|
1335
|
+
// still need the files on disk.
|
|
1360
1336
|
let preventSingleExe="./lib/"
|
|
1361
1337
|
nodePty = require(preventSingleExe+"node-pty");
|
|
1362
1338
|
} catch (error) {
|
|
1363
|
-
|
|
1339
|
+
const detail = error && error.message ? `: ${error.message}` : "";
|
|
1340
|
+
throw new Error(
|
|
1341
|
+
globalThis.Bun
|
|
1342
|
+
? "This build falls back to node-pty because Bun " + globalThis.Bun.version
|
|
1343
|
+
+ " is older than 1.3.14, which is where the built-in ConPTY backend landed. "
|
|
1344
|
+
+ "A compiled binary cannot load node-pty — it is deliberately left out of the "
|
|
1345
|
+
+ "bundle — so rebuild with Bun 1.3.14 or newer, or run from a source checkout "
|
|
1346
|
+
+ "with node-pty installed" + detail
|
|
1347
|
+
: "node-pty is required to run under Node on Windows, and it could not be loaded "
|
|
1348
|
+
+ "from ./lib/node-pty. Run with Bun 1.3.14 or newer to use the built-in ConPTY "
|
|
1349
|
+
+ "backend instead" + detail,
|
|
1350
|
+
);
|
|
1364
1351
|
}
|
|
1365
1352
|
|
|
1366
1353
|
const system32 = path.join(process.env.windir, 'System32');
|
|
@@ -2659,8 +2646,10 @@ function createServerRuntime(command, argv, options) {
|
|
|
2659
2646
|
);
|
|
2660
2647
|
const wsPath = `${basePath}ws`;
|
|
2661
2648
|
const originMatcher = options.wsOrigin ? new RegExp(options.wsOrigin) : null;
|
|
2662
|
-
const indexTemplate =
|
|
2663
|
-
|
|
2649
|
+
const indexTemplate = options.index
|
|
2650
|
+
? fs.readFileSync(expandHome(options.index), "utf8")
|
|
2651
|
+
: assetText("static/index.html");
|
|
2652
|
+
const manifestTemplate = assetText("static/manifest.json");
|
|
2664
2653
|
const activeSessions = new Set();
|
|
2665
2654
|
const reconnectRegistry = new Map();
|
|
2666
2655
|
let acceptedOnce = false;
|
|
@@ -2784,7 +2773,7 @@ function createServerRuntime(command, argv, options) {
|
|
|
2784
2773
|
}
|
|
2785
2774
|
|
|
2786
2775
|
if (relativePath === "help.md" || relativePath === "help") {
|
|
2787
|
-
const markdown =
|
|
2776
|
+
const markdown = assetText("static/help.md");
|
|
2788
2777
|
const markdownToHtml = global.Bun?.markdown?.html;
|
|
2789
2778
|
const renderHtml = relativePath === "help" && typeof markdownToHtml === "function";
|
|
2790
2779
|
const body = renderHtml
|
|
@@ -2865,18 +2854,18 @@ function createServerRuntime(command, argv, options) {
|
|
|
2865
2854
|
return;
|
|
2866
2855
|
}
|
|
2867
2856
|
|
|
2868
|
-
const
|
|
2869
|
-
if (!
|
|
2857
|
+
const assetKey = staticAssetKey(relativePath);
|
|
2858
|
+
if (!assetKey) {
|
|
2870
2859
|
res.writeHead(403);
|
|
2871
2860
|
res.end("Forbidden");
|
|
2872
2861
|
return;
|
|
2873
2862
|
}
|
|
2874
2863
|
|
|
2875
|
-
const bundledAsset =
|
|
2864
|
+
const bundledAsset = assets?.readInternalAssetBytes(assetKey);
|
|
2876
2865
|
if (bundledAsset != null) {
|
|
2877
2866
|
const body = bundledAsset instanceof Uint8Array ? bundledAsset : new Uint8Array(bundledAsset);
|
|
2878
2867
|
res.writeHead(200, {
|
|
2879
|
-
"Content-Type": contentType(
|
|
2868
|
+
"Content-Type": contentType(assetKey),
|
|
2880
2869
|
"Content-Length": body.length,
|
|
2881
2870
|
"Server": "GoTTY"
|
|
2882
2871
|
});
|
|
@@ -2884,7 +2873,8 @@ function createServerRuntime(command, argv, options) {
|
|
|
2884
2873
|
return;
|
|
2885
2874
|
}
|
|
2886
2875
|
|
|
2887
|
-
|
|
2876
|
+
const externalPath = assetFilePath(assetKey);
|
|
2877
|
+
fs.stat(externalPath, (error, stats) => {
|
|
2888
2878
|
if (error || !stats.isFile()) {
|
|
2889
2879
|
res.writeHead(404, { "Server": "GoTTY" });
|
|
2890
2880
|
res.end("Not Found");
|
|
@@ -2892,11 +2882,11 @@ function createServerRuntime(command, argv, options) {
|
|
|
2892
2882
|
}
|
|
2893
2883
|
|
|
2894
2884
|
res.writeHead(200, {
|
|
2895
|
-
"Content-Type": contentType(
|
|
2885
|
+
"Content-Type": contentType(assetKey),
|
|
2896
2886
|
"Content-Length": stats.size,
|
|
2897
2887
|
"Server": "GoTTY"
|
|
2898
2888
|
});
|
|
2899
|
-
fs.createReadStream(
|
|
2889
|
+
fs.createReadStream(externalPath).pipe(res);
|
|
2900
2890
|
});
|
|
2901
2891
|
};
|
|
2902
2892
|
|
|
@@ -3290,15 +3280,9 @@ function main() {
|
|
|
3290
3280
|
async function bootstrap() {
|
|
3291
3281
|
try {
|
|
3292
3282
|
const compiledHelper = await compiledHelperPromise;
|
|
3293
|
-
configureResourceRoot(compiledHelper);
|
|
3294
3283
|
await compiledHelper?.buildEarlyExit?.(process.argv,'jsgt');
|
|
3295
3284
|
|
|
3296
|
-
|
|
3297
|
-
if (assetsHelper) {
|
|
3298
|
-
helperAssetPath = assetsHelper.assetPath;
|
|
3299
|
-
helperReadInternalAssetText = assetsHelper.readInternalAssetText;
|
|
3300
|
-
helperReadInternalAssetBytes = assetsHelper.readInternalAssetBytes;
|
|
3301
|
-
}
|
|
3285
|
+
assets = await assetsHelperPromise;
|
|
3302
3286
|
await globalThis.assetsLoaderPromise;
|
|
3303
3287
|
} catch (error) {
|
|
3304
3288
|
console.error(String(error && error.stack ? error.stack : error));
|
package/package.json
CHANGED
package/single-exe/README.md
CHANGED
|
@@ -55,14 +55,21 @@ project around it.
|
|
|
55
55
|
export const ASSETS_ROOT = "..";
|
|
56
56
|
```
|
|
57
57
|
|
|
58
|
-
2. **`entry.mjs`, the
|
|
59
|
-
|
|
60
|
-
it as the final import in the file:
|
|
58
|
+
2. **`entry.mjs`, the import of the program.** It names what to start, and
|
|
59
|
+
entry.mjs is the compiled entry point in both back ends:
|
|
61
60
|
|
|
62
61
|
```js
|
|
63
62
|
await import("../src/index.js");
|
|
64
63
|
```
|
|
65
64
|
|
|
65
|
+
**Keep the `await`.** It reads like a formality — nothing here needs the
|
|
66
|
+
module object — but together with the `await globalThis.assetsLoaderPromise`
|
|
67
|
+
line above it, it is the only top-level `await` the compiled program writes
|
|
68
|
+
anywhere, and `--bytecode` reads exactly that to decide the program starts
|
|
69
|
+
asynchronously. Turn both into `.then(…)` and the build produces an
|
|
70
|
+
executable that exits 0 without running a line.
|
|
71
|
+
[Why](#the-await-in-entrymjs-is-load-bearing) explains the trap.
|
|
72
|
+
|
|
66
73
|
3. **`package.json`.** Add the `assets` array shown above.
|
|
67
74
|
|
|
68
75
|
Everything else — `compiled.js`, `assetsHelper.js`, `assetsLoader.mjs` — is
|
|
@@ -87,6 +94,8 @@ appear in `package.json`.
|
|
|
87
94
|
| `listInternalAssetPaths(prefix?)` | every embedded path under `prefix` |
|
|
88
95
|
| `listInternalAssetDirs(prefix?)` | the immediate child names under `prefix` |
|
|
89
96
|
| `assetPath(...parts)` | joins parts into a key |
|
|
97
|
+
| `getAssetKey(path)` | the key as stored, namespace included |
|
|
98
|
+
| `assetDiskPath(path)` | where the disk fallback reads that key from |
|
|
90
99
|
| `SELF` | this package's namespace, `assets/<name>@<version>` |
|
|
91
100
|
|
|
92
101
|
The read functions are synchronous for embedded assets on both back ends.
|
|
@@ -178,8 +187,35 @@ so Node 20.11+ runs the same file directly against on-disk assets while Bun
|
|
|
178
187
|
runs it through the bootstrap. The Promise is that boundary, not a workaround
|
|
179
188
|
for missing top-level await.
|
|
180
189
|
|
|
181
|
-
`ASSETS_BUNFS=1` sidesteps the question:
|
|
182
|
-
|
|
190
|
+
`ASSETS_BUNFS=1` sidesteps the question: there is no tar to load, so the build
|
|
191
|
+
writes `.bunfs-entry.mjs` next to `entry.mjs` — the same file with the loader
|
|
192
|
+
import removed — compiles that, and deletes it afterwards. A copy rather than a
|
|
193
|
+
stub, because entry.mjs may do more than import the program: a CommonJS main
|
|
194
|
+
that starts itself from `require.main === module` needs the exported starter
|
|
195
|
+
called instead, and that call has to survive. jsgotty is exactly that case; see
|
|
196
|
+
[below](#the-entry-import-calls-an-exported-starter).
|
|
197
|
+
|
|
198
|
+
## The `await` in entry.mjs is load-bearing
|
|
199
|
+
|
|
200
|
+
`--bytecode` decides whether a program starts asynchronously by looking for a
|
|
201
|
+
top-level `await` **written in some file's own source**. The bundler moves a
|
|
202
|
+
module into a lazy init function whenever something dynamically imports it, and
|
|
203
|
+
a module moved that way takes its top-level `await` with it — what stays behind
|
|
204
|
+
is an `await init_x()` the bundler wrote itself, which does not count. If no
|
|
205
|
+
file wrote one, the executable is evaluated on the synchronous path, its
|
|
206
|
+
suspended startup is dropped, and it exits 0 having run nothing. No error, at
|
|
207
|
+
any `--minify` setting, however small the program.
|
|
208
|
+
|
|
209
|
+
`entry.mjs` is immune because it writes its own, and one is enough. Every other
|
|
210
|
+
`await` in the graph — all of `assetsLoader.mjs`'s, for one — sits inside a
|
|
211
|
+
function and cannot help. It is also why bunfs builds compile a copy of that
|
|
212
|
+
file rather than handing the main program to `bun build` directly: a main
|
|
213
|
+
program that reaches top-level await only through its imports writes none of
|
|
214
|
+
its own.
|
|
215
|
+
|
|
216
|
+
Upstream: `src/bundler/linker_context/postProcessJSChunk.rs` skips exactly the
|
|
217
|
+
modules the bundler moved, still true on oven-sh/bun `main` at 8eb5b6e2b5
|
|
218
|
+
(2026-08-22). Until that changes, treat entry.mjs's `await`s as API.
|
|
183
219
|
|
|
184
220
|
## Environment variables
|
|
185
221
|
|
|
@@ -326,10 +362,12 @@ const { bootstrap } = await import("../gotty.js");
|
|
|
326
362
|
await bootstrap();
|
|
327
363
|
```
|
|
328
364
|
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
365
|
+
Both back ends go through it: `ASSETS_BUNFS=1` compiles a copy of `entry.mjs`,
|
|
366
|
+
so `gotty.js` is never the entry point and the guard stays `false` in either
|
|
367
|
+
mode. Handing `gotty.js` to `bun build` directly instead would compile it as
|
|
368
|
+
the entry, Bun would rewrite the guard to `import.meta.main` — true — and it
|
|
369
|
+
would start itself; that path is not taken, and `bootstrap` is what starts the
|
|
370
|
+
program in both builds.
|
|
333
371
|
|
|
334
372
|
### `packAssets.sh`
|
|
335
373
|
|
|
@@ -35,11 +35,23 @@ function detectPrefix() {
|
|
|
35
35
|
for (const key of keys) if (key.startsWith(SELF + "/")) return SELF;
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
|
|
38
|
+
const base = bunfsBase();
|
|
39
|
+
if (base && existsSync(join(base, SELF))) return SELF;
|
|
39
40
|
|
|
40
41
|
return "";
|
|
41
42
|
}
|
|
42
43
|
|
|
44
|
+
// `--assets-external` means "ignore what is embedded". The tar loader
|
|
45
|
+
// signals it by setting the store to null; with no loader in the graph
|
|
46
|
+
// (the --asset back end) it stays undefined, so only null counts.
|
|
47
|
+
//
|
|
48
|
+
// Without this the bunfs branch below stayed live in tar builds too, and
|
|
49
|
+
// since it is rooted at the binary's own /$bunfs/root, an external run
|
|
50
|
+
// enumerated the executable and its embedded tar as if they were assets.
|
|
51
|
+
function externalAssets() {
|
|
52
|
+
return globalThis.internalAssets === null;
|
|
53
|
+
}
|
|
54
|
+
|
|
43
55
|
// Whether THIS package has anything embedded. A bare store check was
|
|
44
56
|
// true even for an empty one, which sent every caller down the internal
|
|
45
57
|
// branch to find nothing and never try the disk fallback.
|
|
@@ -140,7 +152,7 @@ export async function buildHtmlBundleImageMap(
|
|
|
140
152
|
// binary: in the source tree this file sits in single-exe/, which is
|
|
141
153
|
// not the asset root.
|
|
142
154
|
function bunfsBase() {
|
|
143
|
-
return IS_COMPILED ? import.meta.dir : null;
|
|
155
|
+
return IS_COMPILED && !externalAssets() ? import.meta.dir : null;
|
|
144
156
|
}
|
|
145
157
|
|
|
146
158
|
// Store-space keys for every regular file under <base>/<sub>.
|
|
@@ -178,15 +190,37 @@ function walkBunfs(base, sub) {
|
|
|
178
190
|
|
|
179
191
|
// Callers always speak package-relative keys. This is the one place
|
|
180
192
|
// those become store keys, so the namespace stays invisible to them.
|
|
181
|
-
|
|
193
|
+
// Exported because a caller rolling its own fallback needs the same
|
|
194
|
+
// answer — `--assets-extract` writes the archive under these keys.
|
|
195
|
+
export function getAssetKey(path) {
|
|
182
196
|
const key = assetPath(path);
|
|
183
197
|
const at = assetPrefix();
|
|
184
198
|
return at ? (key ? `${at}/${key}` : at) : key;
|
|
185
199
|
}
|
|
186
200
|
|
|
201
|
+
// Where the disk fallback looks for a package-relative key.
|
|
202
|
+
//
|
|
203
|
+
// A source checkout has the plain path. A compiled binary does not:
|
|
204
|
+
// `--assets-extract` writes the archive exactly as packed, namespace
|
|
205
|
+
// included, so the extracted `README.md` lands at
|
|
206
|
+
// `<exe dir>/assets/<name>@<version>/README.md` and the plain join
|
|
207
|
+
// misses it. `assetPrefix()` cannot answer this — with
|
|
208
|
+
// `--assets-external` there is no store to detect it from, which is
|
|
209
|
+
// precisely when the fallback runs — so try SELF and keep the plain
|
|
210
|
+
// path for archives that were packed flat.
|
|
211
|
+
export function assetDiskPath(path) {
|
|
212
|
+
const key = assetPath(path);
|
|
213
|
+
const plain = join(REPO_ROOT, key);
|
|
214
|
+
|
|
215
|
+
if (!IS_COMPILED) return plain;
|
|
216
|
+
|
|
217
|
+
const namespaced = join(REPO_ROOT, SELF, key);
|
|
218
|
+
return existsSync(namespaced) ? namespaced : plain;
|
|
219
|
+
}
|
|
220
|
+
|
|
187
221
|
export function listInternalAssetPaths(prefix = "") {
|
|
188
222
|
const at = assetPrefix();
|
|
189
|
-
const wanted =
|
|
223
|
+
const wanted = getAssetKey(prefix);
|
|
190
224
|
const store = getAssetStore();
|
|
191
225
|
const found = new Set();
|
|
192
226
|
|
|
@@ -227,7 +261,7 @@ export function listInternalAssetDirs(prefix = "") {
|
|
|
227
261
|
}
|
|
228
262
|
|
|
229
263
|
export function getInternalAsset(path) {
|
|
230
|
-
const key =
|
|
264
|
+
const key = getAssetKey(path);
|
|
231
265
|
|
|
232
266
|
const store = getAssetStore();
|
|
233
267
|
if (store) {
|
|
@@ -275,20 +309,38 @@ export async function readAssetText(path) {
|
|
|
275
309
|
// TextDecoder drops a leading BOM, so the embedded path never returns
|
|
276
310
|
// one; readFile keeps it. Strip it here or the same file reads
|
|
277
311
|
// differently depending on whether it was embedded.
|
|
278
|
-
return (await readFile(
|
|
312
|
+
return (await readFile(assetDiskPath(path), "utf8")).replace(/^\uFEFF/, "");
|
|
279
313
|
}
|
|
280
314
|
|
|
281
315
|
export async function readAssetBytes(path) {
|
|
282
316
|
const internal = readInternalAssetBytes(path);
|
|
283
317
|
if (internal) return internal;
|
|
284
318
|
|
|
285
|
-
const buf = await readFile(
|
|
319
|
+
const buf = await readFile(assetDiskPath(path));
|
|
286
320
|
|
|
287
321
|
// Buffer is a Uint8Array, but hand back a plain one so callers cannot
|
|
288
322
|
// come to depend on the Buffer-only methods.
|
|
289
323
|
return new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
|
|
290
324
|
}
|
|
291
325
|
|
|
326
|
+
// Sync twins of readAssetText / readAssetBytes, for callers that cannot
|
|
327
|
+
// await — argument parsing, module init, CommonJS programs. Same lookup
|
|
328
|
+
// and same fallback; only the disk read differs (node:fs sync).
|
|
329
|
+
export function readAssetTextSync(path) {
|
|
330
|
+
const internal = readInternalAssetText(path);
|
|
331
|
+
if (internal != null) return internal;
|
|
332
|
+
|
|
333
|
+
return readFileSync(assetDiskPath(path), "utf8").replace(/^\uFEFF/, "");
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
export function readAssetBytesSync(path) {
|
|
337
|
+
const internal = readInternalAssetBytes(path);
|
|
338
|
+
if (internal) return internal;
|
|
339
|
+
|
|
340
|
+
const buf = readFileSync(assetDiskPath(path));
|
|
341
|
+
return new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
|
|
342
|
+
}
|
|
343
|
+
|
|
292
344
|
export function internalAssetSource(path) {
|
|
293
345
|
return {
|
|
294
346
|
name: path.split("/").pop() ?? path,
|
package/single-exe/compiled.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { basename, dirname, resolve } from "node:path";
|
|
2
|
-
import { existsSync, mkdirSync, readFileSync, rmSync } from "node:fs";
|
|
2
|
+
import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
3
3
|
import { fileURLToPath } from "node:url";
|
|
4
4
|
import child_process from "node:child_process";
|
|
5
5
|
import { ASSETS_ROOT } from "./assetsPacker.js";
|
|
@@ -247,18 +247,29 @@ export async function buildExeAssets(target = "", build_outfile = "single.exe",
|
|
|
247
247
|
const bunfs = Boolean(process.env.ASSETS_BUNFS);
|
|
248
248
|
const staging = resolve(REPO_ROOT, "build", "assets");
|
|
249
249
|
|
|
250
|
-
// Nothing loads a tar in bunfs mode, so
|
|
251
|
-
//
|
|
252
|
-
//
|
|
253
|
-
//
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
250
|
+
// Nothing loads a tar in bunfs mode, so the asset loader — and the
|
|
251
|
+
// `assets.tar` it imports — cannot be in the graph. Everything else in
|
|
252
|
+
// entry.mjs still has to run, so the compile entry is a generated copy
|
|
253
|
+
// of it with that one import removed.
|
|
254
|
+
//
|
|
255
|
+
// Handing the main program to `bun build` directly would be simpler and
|
|
256
|
+
// breaks two ways. Whatever entry.mjs does after the import is lost, and
|
|
257
|
+
// a CommonJS program relying on a `require.main` guard the bundler
|
|
258
|
+
// inlines to false would never start. Worse, `--bytecode` only marks the
|
|
259
|
+
// chunk as an async module when a top-level await appears in an
|
|
260
|
+
// unwrapped part's own source; a main program that reaches top-level
|
|
261
|
+
// await only through `__esm`-wrapped dependencies is then evaluated on
|
|
262
|
+
// the sync path, its suspended generator is dropped, and the binary
|
|
263
|
+
// exits 0 having run nothing. entry.mjs awaits in its own source, so the
|
|
264
|
+
// copy carries that flag the way the tar build always has.
|
|
265
|
+
const bunfsEntry = resolve(SINGLE_EXE_DIR, ".bunfs-entry.mjs");
|
|
266
|
+
const dropBunfsEntry = () => bunfs && rmSync(bunfsEntry, { force: true });
|
|
267
|
+
|
|
268
|
+
if (bunfs && !writeBunfsEntry(resolve(SINGLE_EXE_DIR, "entry.mjs"), bunfsEntry)) {
|
|
258
269
|
return 1;
|
|
259
270
|
}
|
|
260
271
|
|
|
261
|
-
const compileEntry = bunfs ?
|
|
272
|
+
const compileEntry = bunfs ? bunfsEntry : "./entry.mjs";
|
|
262
273
|
|
|
263
274
|
const step = (label, args, cwd) => {
|
|
264
275
|
console.log("");
|
|
@@ -296,6 +307,7 @@ export async function buildExeAssets(target = "", build_outfile = "single.exe",
|
|
|
296
307
|
// and --bytecode; the module graph is the same and it is much faster.
|
|
297
308
|
if (step("Scan module graph", ["build", compileEntry, "--target=bun", `--metafile=${metafile}`, `--outdir=${scanDir}`], SINGLE_EXE_DIR) !== 0) {
|
|
298
309
|
console.error("Scan build failed");
|
|
310
|
+
dropBunfsEntry();
|
|
299
311
|
return 1;
|
|
300
312
|
}
|
|
301
313
|
|
|
@@ -314,6 +326,7 @@ export async function buildExeAssets(target = "", build_outfile = "single.exe",
|
|
|
314
326
|
|
|
315
327
|
if (step(`Pack ${name}`, args, dirname(packer)) !== 0) {
|
|
316
328
|
console.error(`Failed to pack assets from ${packer}`);
|
|
329
|
+
dropBunfsEntry();
|
|
317
330
|
return 1;
|
|
318
331
|
}
|
|
319
332
|
}
|
|
@@ -361,6 +374,8 @@ export async function buildExeAssets(target = "", build_outfile = "single.exe",
|
|
|
361
374
|
console.error("Compile failed");
|
|
362
375
|
}
|
|
363
376
|
|
|
377
|
+
dropBunfsEntry();
|
|
378
|
+
|
|
364
379
|
const isWindows = normalizedTarget ? normalizedTarget.toLowerCase().includes("windows") : process.platform === "win32";
|
|
365
380
|
if (isWindows && !outfile.toLowerCase().endsWith(".exe")) {
|
|
366
381
|
outfile += ".exe";
|
|
@@ -376,9 +391,63 @@ export async function buildExeAssets(target = "", build_outfile = "single.exe",
|
|
|
376
391
|
}
|
|
377
392
|
|
|
378
393
|
|
|
394
|
+
// entry.mjs, minus the import of the asset loader, written to `outFile`
|
|
395
|
+
// as the compile entry for bunfs builds. Returns whether it was written.
|
|
396
|
+
//
|
|
397
|
+
// A textual copy, not a generated stub: entry.mjs may do more than import
|
|
398
|
+
// the main program, and all of it has to survive.
|
|
399
|
+
export function writeBunfsEntry(entryFile, outFile) {
|
|
400
|
+
let src;
|
|
401
|
+
|
|
402
|
+
try {
|
|
403
|
+
src = readFileSync(entryFile, "utf8");
|
|
404
|
+
} catch (e) {
|
|
405
|
+
console.error(`Could not read ${entryFile}`);
|
|
406
|
+
console.error(e);
|
|
407
|
+
return false;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
// Whole-line side-effect import of the loader, in any of the shapes a
|
|
411
|
+
// project might have written it. Nothing else may reference it: the
|
|
412
|
+
// loader is what pulls in `assets.tar`, which no bunfs build produces.
|
|
413
|
+
const loaderImport = /^[ \t]*import\s+(?:[^"'\n]*\bfrom\s+)?["'][^"'\n]*assetsLoader\.mjs["'][ \t]*;?[ \t]*$/gm; // "
|
|
414
|
+
const stripped = src.replace(loaderImport, "");
|
|
415
|
+
|
|
416
|
+
if (stripped === src) {
|
|
417
|
+
console.log("");
|
|
418
|
+
console.log(`No assetsLoader import found in ${entryFile}; compiling it as is`);
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
const header =
|
|
422
|
+
"// Generated by single-exe/compiled.js for ASSETS_BUNFS=1 builds.\n"
|
|
423
|
+
+ "// A copy of entry.mjs without the asset loader; edit entry.mjs.\n";
|
|
424
|
+
|
|
425
|
+
// A hashbang is only a hashbang on the first line; anywhere else it is
|
|
426
|
+
// a syntax error. Keep it there and put the header underneath, unless
|
|
427
|
+
// the file is nothing but a hashbang and there is no underneath.
|
|
428
|
+
const firstBreak = stripped.indexOf("\n");
|
|
429
|
+
const hasHashbang = stripped.startsWith("#!");
|
|
430
|
+
const hashbang = hasHashbang && firstBreak !== -1 ? stripped.slice(0, firstBreak + 1) : "";
|
|
431
|
+
|
|
432
|
+
try {
|
|
433
|
+
writeFileSync(
|
|
434
|
+
outFile,
|
|
435
|
+
hasHashbang && !hashbang
|
|
436
|
+
? stripped
|
|
437
|
+
: hashbang + header + stripped.slice(hashbang.length),
|
|
438
|
+
);
|
|
439
|
+
} catch (e) {
|
|
440
|
+
console.error(`Could not write ${outFile}`);
|
|
441
|
+
console.error(e);
|
|
442
|
+
return false;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
return true;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
|
|
379
449
|
// The main program, as declared by the last import of entry.mjs. That
|
|
380
|
-
// file is already the one place an adopting project edits
|
|
381
|
-
// builds read it rather than adding a second declaration.
|
|
450
|
+
// file is already the one place an adopting project edits.
|
|
382
451
|
export function lastEntryImport(entryFile) {
|
|
383
452
|
let src;
|
|
384
453
|
|
package/single-exe/entry.mjs
CHANGED
|
@@ -6,6 +6,13 @@
|
|
|
6
6
|
// 3. Starts the main program only after the assets are ready
|
|
7
7
|
import "./assetsLoader.mjs";
|
|
8
8
|
|
|
9
|
+
// Keep these as `await`, not `.then()`. Besides the sequencing, they are
|
|
10
|
+
// the only top-level await this program writes anywhere, and `--bytecode`
|
|
11
|
+
// reads exactly that to decide the program starts asynchronously. With
|
|
12
|
+
// none written, the executable is evaluated synchronously, its suspended
|
|
13
|
+
// startup is dropped, and it exits 0 having run nothing — no error, at any
|
|
14
|
+
// size. Awaits the bundler generates for us do not count; see the README's
|
|
15
|
+
// "The await in entry.mjs is load-bearing".
|
|
9
16
|
await globalThis.assetsLoaderPromise;
|
|
10
17
|
|
|
11
18
|
// gotty.js starts itself from `require.main === module`, which the
|
package/zmodem-node.js
CHANGED
|
@@ -10,12 +10,12 @@ let ttyModeDepth = 0;
|
|
|
10
10
|
let rawOutputMode = false;
|
|
11
11
|
const defaultDebugLogPath = path.join(os.tmpdir(), "zmodem", "zmodem.log");
|
|
12
12
|
const debugLogPath = resolveDebugLogPath();
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
: Promise.resolve(
|
|
13
|
+
const ZMODEM_ASSET = "static/js/zmodem.js";
|
|
14
|
+
// ESM, and Bun-only in practice: under Node there is nothing embedded, so
|
|
15
|
+
// the source checkout's own copy is the only answer anyway.
|
|
16
|
+
const assetsHelperPromise = globalThis.Bun
|
|
17
|
+
? import("./single-exe/assetsHelper.js").catch(() => null)
|
|
18
|
+
: Promise.resolve(null);
|
|
19
19
|
|
|
20
20
|
async function loadZmodem() {
|
|
21
21
|
if (cachedZmodem) {
|
|
@@ -23,12 +23,14 @@ async function loadZmodem() {
|
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
await globalThis.assetsLoaderPromise;
|
|
26
|
-
const
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
const bundlePath =
|
|
26
|
+
const assets = await assetsHelperPromise;
|
|
27
|
+
const source = assets
|
|
28
|
+
? assets.readAssetTextSync(ZMODEM_ASSET)
|
|
29
|
+
: fs.readFileSync(path.join(__dirname, ZMODEM_ASSET), "utf8");
|
|
30
|
+
// Only a label for stack traces out of the vm context.
|
|
31
|
+
const bundlePath = assets
|
|
32
|
+
? assets.assetDiskPath(ZMODEM_ASSET)
|
|
33
|
+
: path.join(__dirname, ZMODEM_ASSET);
|
|
32
34
|
const quietConsole = {
|
|
33
35
|
debug() {},
|
|
34
36
|
log() {},
|