jsgotty 1.1.5 → 1.1.7

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/README.en.md CHANGED
@@ -3,6 +3,10 @@
3
3
  [中文 README](README.md)
4
4
 
5
5
  ## News
6
+ ### 2026/08/16
7
+ - Fixed the disconnected-screen `Reconnect` button not being clickable in WebGL mode.
8
+ - The `Reconnect` UI now stays above the WebGL canvas and receives mouse and touch events correctly; Kitty image handling no longer intercepts right-click / long-press for entering a reconnect token or PID.
9
+
6
10
  ### 2026/07/03 - [1.1.0]
7
11
  - Unicode 11 from upstream
8
12
  - WebGL support by `--webgl` and query args
package/README.md CHANGED
@@ -3,6 +3,10 @@
3
3
  [English README](README.en.md)
4
4
 
5
5
  ## News
6
+ ### 2026/08/16
7
+ - 修正 WebGL 模式下斷線畫面的 `Reconnect` 按鈕無法點擊。
8
+ - `Reconnect` 介面現在會正確位於 WebGL canvas 上方並接收滑鼠與觸控事件;右鍵/長按輸入 reconnect token 或 PID 也不會被 Kitty 圖片事件攔截。
9
+
6
10
  ### 2026/07/03 - [1.1.0]
7
11
  - Unicode 11 from upstream
8
12
  - WebGL support by `--webgl` & query args
package/gotty.js CHANGED
@@ -3323,3 +3323,8 @@ if (require.main === module) {
3323
3323
  process.exitCode = 1;
3324
3324
  });
3325
3325
  }
3326
+
3327
+ // The guard above is inlined to false once this file is bundled, because
3328
+ // the bundle entry point is single-exe/entry.mjs rather than this file.
3329
+ // Export the starter so that entry point can call it.
3330
+ module.exports.bootstrap = bootstrap;
package/package.json CHANGED
@@ -1,11 +1,16 @@
1
1
  {
2
2
  "name": "jsgotty",
3
- "version": "1.1.5",
3
+ "version": "1.1.7",
4
4
  "description": "Share your terminal as a web application: A JavaScript rewrite of GoTTY",
5
5
  "bin": {
6
6
  "jsgotty": "./gotty.js"
7
7
  },
8
8
  "type": "commonjs",
9
+ "assets": [
10
+ "static",
11
+ "README.md",
12
+ "README.en.md"
13
+ ],
9
14
  "dependencies": {
10
15
  "node-addon-api": "^7.1.1",
11
16
  "ws": "^8.18.3"
@@ -1,80 +1,346 @@
1
- # This is completely optional
2
- - I still recommend using the methods in the root README.md
3
- - Bun's Android build is currently not supported
1
+ # Single-file executable bootstrap
4
2
 
5
- # Usage
6
- - First run `bun ./packAssets.sh` to bundle the assets into `assets.tar`
7
- - Then run the build script like this
3
+ Bundles a project's runtime assets into one `bun build --compile` binary, so
4
+ the executable carries its own `static/`, templates, documentation and so on.
8
5
 
9
- ```shell
10
- bun build --compile --bytecode --minify ./entry.mjs --outfile=binname
11
- ```
6
+ Assets are declared once, in `package.json`:
12
7
 
13
- - You'll get a binname executable
8
+ ```json
9
+ {
10
+ "assets": ["static", "README.md", "README.en.md"]
11
+ }
12
+ ```
13
+
14
+ Paths are relative to the project root. Folders are packed recursively; only
15
+ regular files are stored, so symlinks and empty folders leave no trace.
16
+
17
+ ## The two back ends
18
+
19
+ The default packs everything into a gzipped `assets.tar` that is embedded as a
20
+ single file and unpacked into memory at startup. Setting `ASSETS_BUNFS=1` at
21
+ build time switches to `bun build --compile --asset`, which stores the files
22
+ individually inside the binary's virtual filesystem instead.
23
+
24
+ Reading code never sees the difference — `assetsHelper` answers the same keys
25
+ either way. The trade is binary size against startup cost:
26
+
27
+ | | tar (default) | `ASSETS_BUNFS=1` |
28
+ | --- | --- | --- |
29
+ | Stored as | one gzipped archive | individual files, uncompressed |
30
+ | Size, for jsgotty's 3.45 MB of assets | **0.70 MB** | 3.45 MB |
31
+ | Work before `main` runs | decode the archive, copy every file into memory | **none** |
32
+ | Startup cost, same assets | ~16 ms | **0 ms** |
33
+ | Resident memory | whole payload, always | only what is read |
34
+ | Maturity | in use | experimental |
14
35
 
15
- # Single Executable Intro
36
+ Text compresses well, so the tar usually wins on size by a wide margin — for
37
+ jsgotty most of the payload is `static/`, whose JavaScript and CSS shrink to a
38
+ fifth. What it costs is paid on every run, whether or not the program touches a
39
+ single asset. Reach for `ASSETS_BUNFS=1` when that startup work, or holding the
40
+ whole payload in memory, matters more than the extra megabytes on disk.
16
41
 
17
- This folder contains the Bun single-exe bootstrap used by the project.
42
+ ## Adopting it in another project
18
43
 
19
- ## Entry Flow
44
+ Copy `single-exe/` to the project root. Nothing inside it hard-codes its own
45
+ name or depth, so it can be renamed or moved — but the importing code has to
46
+ follow, so staying at the root is the easy path. Three edits point it at the
47
+ project around it.
20
48
 
21
- - `entry.mjs` imports `assetsLoader.mjs` first
22
- - `assetsLoader.mjs` loads `assets.tar` with `Bun.Archive` and mounts it as `globalThis.internalAssets`
23
- - `assetsLoaderPromise` is exposed on `globalThis`
24
- - The main program waits for `assetsLoaderPromise` if it exists
49
+ 1. **`assetsPacker.js`, the first two lines.** They are the only place the
50
+ asset root is written down, and a static import specifier cannot be
51
+ computed, so both must be literal:
25
52
 
26
- That keeps the main program bootable even if asset loading reports errors.
53
+ ```js
54
+ import pkg from "../package.json" with { type: "json" };
55
+ export const ASSETS_ROOT = "..";
56
+ ```
27
57
 
28
- ## Assets Loading
58
+ 2. **`entry.mjs`, the last import.** It names the program to start. Build
59
+ also reads this line to find the entry point for `ASSETS_BUNFS=1`, so keep
60
+ it as the final import in the file:
29
61
 
30
- - Bundled assets are loaded sequentially with `await file.bytes()`
31
- - Load failures are collected and printed to `stderr`
32
- - Asset loading never rejects the bootstrap promise
33
- - When loading succeeds, the archive is available through `globalThis.internalAssets`
62
+ ```js
63
+ await import("../src/index.js");
64
+ ```
34
65
 
35
- ## CLI Flags
66
+ 3. **`package.json`.** Add the `assets` array shown above.
36
67
 
37
- - `--assets-list`
38
- - Lists all entries inside bundled `assets.tar`
39
- - Exits early before the main program starts
68
+ Everything else — `compiled.js`, `assetsHelper.js`, `assetsLoader.mjs` — is
69
+ project independent, with one exception: `buildHtmlBundleImageMap` defaults its
70
+ attribute name to jsmdcui's `data-mdcui-src`, so pass your own.
40
71
 
41
- - `--assets-extract`
42
- - Extracts bundled assets to the same directory as the executable
43
- - Exits early before the main program starts
72
+ Node's own entry path must not import `assetsLoader.mjs` or `entry.mjs`; those
73
+ are Bun only. `assetsHelper.js` and `compiled.js` run under Node 20.11+.
44
74
 
45
- - `--assets-external`
46
- - Skips loading bundled assets into `globalThis.internalAssets`
47
- - Forces the main program and runtime helpers to use the external file tree
48
- - Keeps the bootstrap alive while leaving `internalAssets` falsy
75
+ ## Reading assets
49
76
 
50
- ## Adapting this folder
77
+ Import from `assetsHelper.js`. Keys are the same package-relative paths that
78
+ appear in `package.json`.
51
79
 
52
- This single-exe folder is intended for reuse by other projects. Usually you
53
- only need to edit these project-specific files first:
80
+ | Function | Returns |
81
+ | --- | --- |
82
+ | `readInternalAssetText(path)` | string, or `null` when not embedded |
83
+ | `readInternalAssetBytes(path)` | `Uint8Array`, or `null` |
84
+ | `readAssetText(path)` | string, falling back to the file on disk |
85
+ | `readAssetBytes(path)` | `Uint8Array`, same fallback |
86
+ | `hasInternalAssets()` | whether this package has anything embedded |
87
+ | `listInternalAssetPaths(prefix?)` | every embedded path under `prefix` |
88
+ | `listInternalAssetDirs(prefix?)` | the immediate child names under `prefix` |
89
+ | `assetPath(...parts)` | joins parts into a key |
90
+ | `SELF` | this package's namespace, `assets/<name>@<version>` |
54
91
 
55
- - `entry.mjs`
56
- - `packAssets.sh`
92
+ The read functions are synchronous for embedded assets on both back ends.
93
+ `readAssetText` / `readAssetBytes` are async only because of the disk
94
+ fallback, which is what makes the same code work from a source checkout.
57
95
 
58
- Then the main program can import `buildEarlyExit` from `compiled.js` and call
59
- it before normal CLI parsing. `--build-exe` and `--build-for <target>` will run
60
- `packAssets.sh` first, so `assets.tar` is generated as part of the build flow.
96
+ **Only the read functions fall back to disk.** The listing functions report
97
+ what is embedded and nothing else, so from a source checkout they return an
98
+ empty array. Branch on `hasInternalAssets()` and read the directory yourself:
61
99
 
62
100
  ```js
63
- const compiledHelper = await import("./single-exe/compiled.js").catch(() => null);
64
- await compiledHelper?.buildEarlyExit?.(process.argv, "my-bin");
101
+ import { readdirSync } from "node:fs";
102
+ import { join } from "node:path";
103
+ import { readAssetText, hasInternalAssets, listInternalAssetDirs } from "../single-exe/assetsHelper.js";
104
+ import { REPO_ROOT } from "../single-exe/compiled.js";
105
+
106
+ // Embedded or on disk, this one already works either way.
107
+ const index = await readAssetText("static/index.html");
108
+
109
+ // Listing does not, so pick the source.
110
+ const staticDirs = hasInternalAssets()
111
+ ? listInternalAssetDirs("static")
112
+ : readdirSync(join(REPO_ROOT, "static"));
65
113
  ```
66
114
 
67
- - `--build-exe` builds with no Bun target.
68
- - `--build-for <target>` passes `<target>` to `bun build --target=<target>`.
69
- - The second argument is the output filename. If omitted, it defaults to `single.exe`.
115
+ ## Building
70
116
 
71
- The main program should also switch its repo/resource root when running as a
72
- compiled binary. Use `IS_COMPILED` and `REPO_ROOT` from
73
- `compiled.js`; when compiled, treat the executable directory as the repo root
74
- used for external fallback files such as `static/`, `README.md`, or other files
75
- packed by `packAssets.sh`.
117
+ `compiled.js` exports the build entry points. Wire `buildEarlyExit` into the
118
+ CLI and it handles `--build-exe` and `--build-for <target>`:
76
119
 
77
120
  ```js
78
- const compiledHelper = await import("./single-exe/compiled.js").catch(() => null);
79
- const repoRoot = compiledHelper?.REPO_ROOT
121
+ import { buildEarlyExit, IS_COMPILED, REPO_ROOT } from "./single-exe/compiled.js";
122
+
123
+ await buildEarlyExit(process.argv, "myapp");
124
+ ```
125
+
126
+ `REPO_ROOT` is the project root in a checkout and the executable's own folder
127
+ once compiled, which is where `--assets-extract` writes. `IS_COMPILED` tells
128
+ the two apart.
129
+
130
+ ### Chaining file/folder assets across dependencies
131
+
132
+ A build first bundles once to write a metafile, then treats every
133
+ `assetsPacker.js` appearing in its `inputs` as a participant. Each package
134
+ vendors its own `single-exe/`, and its `assetsHelper` imports its
135
+ `assetsPacker`, so a packer lands in that graph exactly when the package is
136
+ reachable from the entry point. There is no registration step, and anything
137
+ tree-shaken away brings nothing with it.
138
+
139
+ The packers then run one after another, each adding its files under its own
140
+ `assets/<name>@<version>/` namespace — appending into the shared `assets.tar`
141
+ by default, or copying into the shared `build/assets` tree under
142
+ `ASSETS_BUNFS=1`. Only once every packer has finished does the real compile
143
+ run, so the archive is complete and, by default, compressed by then.
144
+
145
+ That namespace is why `ASSETS_BUNFS=1` stages every package into one fixed
146
+ `build/assets`. `--asset` keeps only the **basename** of the path it is given
147
+ and preserves everything below it, so the folder's own name becomes the root
148
+ inside the binary:
149
+
150
+ ```
151
+ --asset ./build/assets with build/assets/jsgotty@1.1.6/static/index.html
152
+ -> assets/jsgotty@1.1.6/static/index.html
153
+ ```
154
+
155
+ Pass `./build/assets/jsgotty@1.1.6` instead and `build/assets/` is gone: the
156
+ files land at `jsgotty@1.1.6/...` and every package gets its own root. One
157
+ folder named `assets`, everyone copying into it, is what makes the keys come
158
+ out the same as the ones the tar back end writes.
159
+
160
+ `assetsPacker.js` also runs standalone; `--help` documents its flags.
161
+
162
+ ## Why the loader hands over a Promise
163
+
164
+ `entry.mjs` imports `assetsLoader.mjs`, awaits `globalThis.assetsLoaderPromise`,
165
+ and only then imports the main program. The indirection is deliberate: the
166
+ loader carries a Bun-only import,
167
+
168
+ ```js
169
+ import assets from "./assets.tar" with { type: "file" };
170
+ ```
171
+
172
+ and if the main program imported the loader, that dependency would land in its
173
+ module graph. Node could then no longer load the program at all — not even to
174
+ fall back to reading the assets from disk.
175
+
176
+ Keeping the loader behind `entry.mjs` means the main program never mentions it,
177
+ so Node 20.11+ runs the same file directly against on-disk assets while Bun
178
+ runs it through the bootstrap. The Promise is that boundary, not a workaround
179
+ for missing top-level await.
180
+
181
+ `ASSETS_BUNFS=1` sidesteps the question: with no tar to load, the build skips
182
+ `entry.mjs` and compiles the main program as its own entry point.
183
+
184
+ ## Environment variables
185
+
186
+ | Variable | When | Effect |
187
+ | --- | --- | --- |
188
+ | `ASSETS_BUNFS=1` | build | use `--asset` instead of the tar |
189
+ | `ASSETS_NO_GZIP=1` | build | leave `assets.tar` uncompressed |
190
+ | `ASSETS_DEBUG=1` | run | print how long unpacking took |
191
+
192
+ ## Runtime flags
193
+
194
+ | Flag | Effect |
195
+ | --- | --- |
196
+ | `--assets-list` | print every embedded path and exit |
197
+ | `--assets-extract` | write the assets next to the executable and exit |
198
+ | `--assets-external` | ignore embedded assets and read from disk |
199
+
200
+ These are handled by `assetsLoader.mjs`, so they exist in tar builds only.
201
+
202
+ ## Advanced usage
203
+
204
+ ### Forwarding arguments to `bun build`
205
+
206
+ Anything after `--build-exe`, or after the target given to `--build-for`, is
207
+ passed straight through to `bun build`:
208
+
209
+ ```shell
210
+ bun ./gotty.js --build-exe --sourcemap
211
+ bun ./gotty.js --build-for bun-linux-x64 --sourcemap
212
+ ```
213
+
214
+ Order matters. The flags have to come after the build switch, or Bun consumes
215
+ them before the program ever starts:
216
+
217
+ ```shell
218
+ # Wrong: Bun parses this define before any of your code runs
219
+ bun --define MY_APP=../app.md ./gotty.js --build-exe
220
+
221
+ # Right
222
+ bun ./gotty.js --build-exe --define MY_APP=../app.md
223
+ ```
224
+
225
+ ### `--define` with a string value
226
+
227
+ **Bun expects `--define` values to be JSON literals, so a string has to arrive
228
+ already quoted.** A bare path is read as an identifier and the build fails or
229
+ inlines something unintended. Quoting it through a shell is awkward, so
230
+ `compiled.js` exports a helper that does it:
231
+
232
+ ```js
233
+ import { buildEarlyExit, stringifyNonPrimitiveDefineValues } from "./single-exe/compiled.js";
234
+
235
+ stringifyNonPrimitiveDefineValues(process.argv, "MY_APP");
236
+ await buildEarlyExit(process.argv, "my-bin");
237
+ ```
238
+
239
+ Call it before `buildEarlyExit`, once per define name you want treated as a
240
+ string. It rewrites the value in place:
241
+
242
+ ```js
243
+ ["--define", "MY_APP=../app.md"] // what the user typed
244
+ ["--define", 'MY_APP="../app.md"'] // what Bun receives
245
+ ```
246
+
247
+ `--define=MY_APP=../app.md` works the same way. Numbers, booleans, `null` and
248
+ `undefined` are left alone, so only genuine strings get quoted.
249
+
250
+ ### Images from an imported HTML bundle
251
+
252
+ A compiled binary exposes an imported HTML entry as a `homepage` object:
253
+ `homepage.index` is the compiled HTML and `homepage.files` lists the
254
+ content-hashed assets by their path inside the binary. `assetsHelper` can map
255
+ the *original* image references back to those paths, without copying any image
256
+ bytes into memory.
257
+
258
+ Bun rewrites `src` during the build, so keep the original reference in a custom
259
+ attribute of your own choosing:
260
+
261
+ ```html
262
+ <!-- source -->
263
+ <img src="./images/photo.jpg" data-original-image="./images/photo.jpg">
264
+
265
+ <!-- compiled -->
266
+ <img src="/photo-abcd1234.jpg" data-original-image="./images/photo.jpg">
267
+ ```
268
+
269
+ Then build the map once, in a compiled binary only:
270
+
271
+ ```js
272
+ import { buildHtmlBundleImageMap, canonicalHtmlBundleImageHref } from "../single-exe/assetsHelper.js";
273
+ import { IS_COMPILED } from "../single-exe/compiled.js";
274
+
275
+ const images = IS_COMPILED
276
+ ? await buildHtmlBundleImageMap(homepage, "data-original-image")
277
+ : null;
278
+
279
+ const path = images?.get(canonicalHtmlBundleImageHref(originalHref));
280
+ const bytes = path ? await Bun.file(path).bytes() : null;
281
+ ```
282
+
283
+ `buildHtmlBundleImageMap` returns a `Map` from canonical original reference to
284
+ the path inside the binary; it decodes HTML entities and percent encoding so a
285
+ reference written either way still matches. Reading stays lazy — the bytes are
286
+ only touched when you use the returned path.
287
+
288
+ Lower level, if you already hold a rewritten public path such as
289
+ `/photo-abcd1234.jpg`: `findHtmlBundleAsset(homepage, publicPath, options)`
290
+ finds its `homepage.files` entry, `findHtmlBundleImageAsset()` restricts that
291
+ to `image/*`, and `htmlBundleImageAssetPath()` returns just the path.
292
+
293
+ Never hard-code `/$bunfs/root/` or `B:/~BUN/`; always use the path that
294
+ `homepage.files` reports.
295
+
296
+ ## jsgotty specifics
297
+
298
+ The rest of this file is generic. These names belong to jsgotty itself and are
299
+ not reserved by the bootstrap — an adopting project picks its own.
300
+
301
+ Building is optional; running `bun gotty.js` from a checkout, as the root
302
+ `README.md` describes, remains the usual way to run jsgotty.
303
+
304
+ ```shell
305
+ bun ./gotty.js --build-exe
306
+ bun ./gotty.js --build-for bun-linux-x64
307
+ ```
308
+
309
+ Both produce `jsgt`: `gotty.js` calls `buildEarlyExit(process.argv, "jsgt")`
310
+ from `bootstrap()`, before any CLI parsing, so the build switches exit before
311
+ a server is ever started. Neither works from an already compiled binary.
312
+
313
+ The three packed entries — `static`, `README.md`, `README.en.md` — are what the
314
+ HTTP server hands out: `index.html`, `manifest.json`, `help.md`, the frontend
315
+ JavaScript and CSS, and the two READMEs behind the help pages.
316
+
317
+ ### The entry import calls an exported starter
318
+
319
+ `gotty.js` is CommonJS and starts itself from `require.main === module`. With
320
+ `entry.mjs` as the bundle's entry point that guard is inlined to `false`, so
321
+ the import cannot rely on it and calls the export instead:
322
+
323
+ ```js
324
+ const { bootstrap } = await import("../gotty.js");
325
+
326
+ await bootstrap();
327
+ ```
328
+
329
+ It is still the last import in the file, which is all `ASSETS_BUNFS=1` needs to
330
+ find the main program. In that mode `gotty.js` is compiled as the entry point
331
+ itself, Bun rewrites the guard to `import.meta.main` — true — and it starts
332
+ itself; the exported `bootstrap` then simply goes unused.
333
+
334
+ ### `packAssets.sh`
335
+
336
+ The original implementation, kept as a reference: a hand-written tar of the
337
+ three asset entries, from before `assetsPacker.js` and its `package.json`
338
+ declaration existed. `buildExecutable()` in `compiled.js` is the matching
339
+ build path and still runs it, but `buildExe` now points at `buildExeAssets`,
340
+ so nothing in the current flow calls either. It is the shortest way to see
341
+ what the packer produces, and it still packs an `assets.tar` by hand:
342
+
343
+ ```shell
344
+ bun ./single-exe/packAssets.sh
345
+ bun build --compile --bytecode --minify ./single-exe/entry.mjs --outfile=jsgt
80
346
  ```