jsgotty 1.1.8 → 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 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
- let RESOURCE_ROOT = __dirname;
101
- let STATIC_ROOT = path.resolve(RESOURCE_ROOT, "./static");
102
- let DEFAULT_INDEX = path.join(STATIC_ROOT, "index.html");
103
- let DEFAULT_MANIFEST = path.join(STATIC_ROOT, "manifest.json");
104
- let DEFAULT_HELP = path.join(STATIC_ROOT, "help.md");
105
- let DEFAULT_README = path.resolve(RESOURCE_ROOT, "./README.md");
106
- let DEFAULT_README_EN = path.resolve(RESOURCE_ROOT, "./README.en.md");
107
- let helperAssetPath = null;
108
- let helperReadInternalAssetText = null;
109
- let helperReadInternalAssetBytes = null;
110
-
111
- function configureResourceRoot(compiledHelper) {
112
- RESOURCE_ROOT = compiledHelper?.REPO_ROOT
113
- STATIC_ROOT = path.resolve(RESOURCE_ROOT, "./static");
114
- DEFAULT_INDEX = path.join(STATIC_ROOT, "index.html");
115
- DEFAULT_MANIFEST = path.join(STATIC_ROOT, "manifest.json");
116
- DEFAULT_HELP = path.join(STATIC_ROOT, "help.md");
117
- DEFAULT_README = path.resolve(RESOURCE_ROOT, "./README.md");
118
- DEFAULT_README_EN = path.resolve(RESOURCE_ROOT, "./README.en.md");
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(filePath) {
251
- const text = readTextFile(filePath);
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
- printWebHelp();
514
+ printMarkdownFile("static/help.md");
514
515
  process.exit(0);
515
516
  break;
516
517
  case "--readme":
517
- printMarkdownFile(DEFAULT_README);
518
+ printMarkdownFile("README.md");
518
519
  process.exit(0);
519
520
  break;
520
521
  case "--readme-en":
521
- printMarkdownFile(DEFAULT_README_EN);
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
- throw new Error("node-pty is required on Windows but is not installed");
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 = readTextFile(options.index ? expandHome(options.index) : DEFAULT_INDEX);
2663
- const manifestTemplate = readTextFile(DEFAULT_MANIFEST);
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 = readTextFile(DEFAULT_HELP);
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 assetPath = path.normalize(path.join(STATIC_ROOT, relativePath));
2869
- if (!assetPath.startsWith(STATIC_ROOT)) {
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 = helperReadInternalAssetBytes?.(internalAssetPathFor(assetPath));
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(assetPath),
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
- fs.stat(assetPath, (error, stats) => {
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(assetPath),
2885
+ "Content-Type": contentType(assetKey),
2896
2886
  "Content-Length": stats.size,
2897
2887
  "Server": "GoTTY"
2898
2888
  });
2899
- fs.createReadStream(assetPath).pipe(res);
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
- const assetsHelper = await assetsHelperPromise;
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jsgotty",
3
- "version": "1.1.8",
3
+ "version": "1.1.9",
4
4
  "description": "Share your terminal as a web application: A JavaScript rewrite of GoTTY",
5
5
  "bin": {
6
6
  "jsgotty": "./gotty.js"
@@ -94,6 +94,8 @@ appear in `package.json`.
94
94
  | `listInternalAssetPaths(prefix?)` | every embedded path under `prefix` |
95
95
  | `listInternalAssetDirs(prefix?)` | the immediate child names under `prefix` |
96
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 |
97
99
  | `SELF` | this package's namespace, `assets/<name>@<version>` |
98
100
 
99
101
  The read functions are synchronous for embedded assets on both back ends.
@@ -35,11 +35,23 @@ function detectPrefix() {
35
35
  for (const key of keys) if (key.startsWith(SELF + "/")) return SELF;
36
36
  }
37
37
 
38
- if (IS_COMPILED && existsSync(join(import.meta.dir, SELF))) return SELF;
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
- function storeKey(path) {
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 = storeKey(prefix);
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 = storeKey(path);
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(join(REPO_ROOT, path), "utf8")).replace(/^\uFEFF/, "");
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(join(REPO_ROOT, path));
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/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 singleExeHelpersPromise = globalThis.Bun
14
- ? Promise.all([
15
- import("./single-exe/compiled.js").catch(() => null),
16
- import("./single-exe/assetsHelper.js").catch(() => null),
17
- ])
18
- : Promise.resolve([null, null]);
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 [compiledHelper, assetsHelper] = await singleExeHelpersPromise;
27
- const bundleAssetPath = assetsHelper?.assetPath?.("static", "js", "zmodem.js") ?? "static/js/zmodem.js";
28
- const source =
29
- assetsHelper?.readInternalAssetText?.(bundleAssetPath) ??
30
- fs.readFileSync(path.join(compiledHelper?.REPO_ROOT ?? __dirname, "static", "js", "zmodem.js"), "utf8");
31
- const bundlePath = path.join(compiledHelper?.REPO_ROOT ?? __dirname, "static", "js", "zmodem.js");
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() {},