From b1c0e5199edd6df03e7f805f17b703a437a031bc Mon Sep 17 00:00:00 2001 From: Ralph Kuepper Date: Sun, 22 Nov 2026 15:30:09 +0100 Subject: [PATCH] Windows: paths, fetch of the app's files, storage, navigator - webRoot() found assets/ next to the executable only with "/" paths; on Windows execPath uses "\", so a built app fell back to the configured root (the source tree) or failed when moved. Both separators work now, and a root given as new URL(..., import.meta.url).pathname ("/C:/...") too. - The page URL was file://C:/..., and fetch read the URL's pathname ("/C:/...") as the file: every model 404'd. The URL is file:///C:/... and the pathname maps back to a drive path (urlPathToFile). - localStorage is saved to %APPDATA%\; HOME is set only under a Unix-style shell, so it went to .\.config\ from Explorer. - The `shot` command and the storage file made their folder by cutting at the last "/", which with a Windows path cut the file name instead (parentDir). - navigator is defined, not assigned: Perry builds that follow Node 21+ have a getter-only navigator, and the assignment threw at startup. --- src/browser.ts | 5 ++++- src/config.ts | 16 +++++++++++++--- src/fetch.ts | 4 ++-- src/script.ts | 4 ++-- src/storage.ts | 14 ++++++++++++-- 5 files changed, 33 insertions(+), 10 deletions(-) diff --git a/src/browser.ts b/src/browser.ts index 39f3619..c435872 100644 --- a/src/browser.ts +++ b/src/browser.ts @@ -249,7 +249,9 @@ const LANGUAGES: string[] = (() => { return langs.length > 0 ? langs : ["en-US"]; })(); -g["navigator"] = { +// defined, not assigned: where the host already has a getter-only navigator (Node 21+, and +// Perry builds that follow it) an assignment throws in a module +const NAVIGATOR = { userAgent: "Mozilla/5.0 (Bloom; Perry) AppleWebKit/537.36 (KHTML, like Gecko)", appName: "Netscape", appVersion: "5.0", vendor: "Bloom", platform: process.platform === "darwin" ? (MOBILE ? "iPhone" : "MacIntel") : process.platform, language: LANGUAGES[0], @@ -263,6 +265,7 @@ g["navigator"] = { storage: { persist: (): Promise => Promise.resolve(true), estimate: (): Promise<{ usage: number; quota: number }> => Promise.resolve({ usage: 0, quota: 1e9 }) }, mediaDevices: undefined, serviceWorker: undefined, xr: undefined, gpu: undefined, }; +Object.defineProperty(g, "navigator", { value: NAVIGATOR, writable: true, configurable: true, enumerable: true }); define("devicePixelRatio", () => pixelRatio()); define("innerWidth", () => cssSize().width); define("innerHeight", () => cssSize().height); diff --git a/src/config.ts b/src/config.ts index 97b9613..6c52270 100644 --- a/src/config.ts +++ b/src/config.ts @@ -126,14 +126,17 @@ let root: string | null = null; */ export function webRoot(): string { if (root !== null) return root; - const exe = process.execPath ?? process.argv[0] ?? ""; + // Windows paths use "\\": the rest of this module (and node:fs) take "/" there as well + const exe = (process.execPath ?? process.argv[0] ?? "").replace(/\\/g, "/"); const dir = exe.slice(0, exe.lastIndexOf("/") + 1); const bundle = settings.bundle; for (const bundled of [dir + "assets/" + bundle + "/", dir + "../Resources/assets/" + bundle + "/"]) { if (existsSync(bundled + "index.html")) { root = bundled; return root; } } let r = settings.root; - if (r === "") r = (process.cwd?.() ?? ".") + "/"; + if (r === "") r = (process.cwd?.() ?? ".").replace(/\\/g, "/") + "/"; + // new URL(..., import.meta.url).pathname is "/C:/..." on Windows + r = urlPathToFile(r); if (!r.endsWith("/")) r += "/"; root = r; return root; @@ -141,5 +144,12 @@ export function webRoot(): string { /** The URL the page is at: index.html in the served folder, as a file:// URL. */ export function pageUrl(): string { - return "file://" + webRoot() + settings.served + "index.html"; + const path = webRoot() + settings.served + "index.html"; + // a Windows path ("C:/...") needs the third slash: file:///C:/... + return "file://" + (path.startsWith("/") ? "" : "/") + path; +} + +/** A file:// URL's pathname as a path on disk: "/C:/x" is "C:/x" on Windows. */ +export function urlPathToFile(pathname: string): string { + return /^\/[A-Za-z]:\//.test(pathname) ? pathname.slice(1) : pathname; } diff --git a/src/fetch.ts b/src/fetch.ts index a5237b6..38b5bfc 100644 --- a/src/fetch.ts +++ b/src/fetch.ts @@ -10,7 +10,7 @@ * bytes. */ import { existsSync, readFileSync, statSync } from "node:fs"; -import { config, pageUrl, webRoot } from "./config.ts"; +import { config, pageUrl, urlPathToFile, webRoot } from "./config.ts"; import { blobOf } from "./blob.ts"; const files = new WeakMap(); @@ -24,7 +24,7 @@ export function localPath(input: string): string | null { if (/^[a-z][a-z0-9+.-]*:/i.test(href) && !href.startsWith("file:")) return null; const site = webRoot() + config().served; if (href.startsWith("/") && !href.startsWith("//")) return site + href.slice(1); - let path = decodeURIComponent(new URL(href, pageUrl()).pathname); + let path = urlPathToFile(decodeURIComponent(new URL(href, pageUrl()).pathname)); // "/x" resolved against the page's file:// URL lands at the disk's root: it meant the site's if (!existsSync(path) && !path.startsWith(site) && existsSync(site + path.slice(1))) path = site + path.slice(1); return path; diff --git a/src/script.ts b/src/script.ts index 82b4807..45353ca 100644 --- a/src/script.ts +++ b/src/script.ts @@ -24,7 +24,7 @@ import { clickAt, pressKey, scriptTouch } from "./input.ts"; import { KeyboardEvent, InputEvent, HTMLInputElement, HTMLTextAreaElement, type Element } from "./dom/index.ts"; import { frameCount, onAfterFrame, quit, timing } from "./frame.ts"; import { contextReport, mediaElements } from "./audio/index.ts"; -import { expandHome } from "./storage.ts"; +import { expandHome, parentDir } from "./storage.ts"; /** Milliseconds per frame of named parts a renderer measures (printed by `fps`), summed since the last read. */ export const extraTimings: Record = {}; @@ -42,7 +42,7 @@ function run(command: string, arg: string): void { switch (command) { case "shot": { const path = expandHome(arg); - mkdirSync(path.slice(0, path.lastIndexOf("/")), { recursive: true }); + mkdirSync(parentDir(path), { recursive: true }); takeScreenshot(path); return; } diff --git a/src/storage.ts b/src/storage.ts index 9fa76d7..507383c 100644 --- a/src/storage.ts +++ b/src/storage.ts @@ -8,12 +8,17 @@ * The file is $BLOOM_STORAGE, else config's storageFile, else * `` in the app's folder: ~/.config// (or * $XDG_CONFIG_HOME//) on a desktop and Apple TV, Library/Application - * Support// in an iOS app's container. + * Support// in an iOS app's container, %APPDATA%\\ on Windows. */ import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs"; import { config } from "./config.ts"; import { MOBILE, frameCount, onAfterFrame, onQuit } from "./frame.ts"; +/** The folder a file is in, for "/" and Windows' "\" alike. */ +export function parentDir(path: string): string { + return path.slice(0, Math.max(path.lastIndexOf("/"), path.lastIndexOf("\\"))); +} + /** `~/...` -> $HOME/... (on iOS HOME is the app's container). */ export function expandHome(path: string): string { return path.startsWith("~/") ? (process.env["HOME"] ?? ".") + path.slice(1) : path; @@ -26,6 +31,11 @@ export function storagePath(): string { const c = config(); if (c.storageFile !== "") return expandHome(c.storageFile); if (MOBILE) return (env["HOME"] ?? ".") + "/Library/Application Support/" + c.app + "/" + c.storageName; + // Windows: %APPDATA%\ (HOME is only set under a Unix-style shell there) + if (process.platform === "win32") { + const appData = env["APPDATA"] ?? env["USERPROFILE"]; + if (appData !== undefined && appData !== "") return appData.replace(/\\/g, "/") + "/" + c.app + "/" + c.storageName; + } const xdg = env["XDG_CONFIG_HOME"]; return (xdg !== undefined && xdg !== "" ? xdg : (env["HOME"] ?? ".") + "/.config") + "/" + c.app + "/" + c.storageName; } @@ -59,7 +69,7 @@ export function flushStorage(): void { const now = snapshot(); if (now === saved) return; try { - mkdirSync(STORAGE.slice(0, STORAGE.lastIndexOf("/")), { recursive: true }); + mkdirSync(parentDir(STORAGE), { recursive: true }); writeFileSync(STORAGE, now); saved = now; } catch (err) {