From c8093983a3a5bfc0c2b026f76ad625c917da25a6 Mon Sep 17 00:00:00 2001 From: Project516 <138796702+Project516@users.noreply.github.com> Date: Mon, 2 Nov 2026 19:24:35 -0600 Subject: [PATCH] Preserve special characters in browser file paths --- src/browser/http.ts | 8 ++++++-- test/http-files.test.ts | 45 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 test/http-files.test.ts diff --git a/src/browser/http.ts b/src/browser/http.ts index f3dccb3..2bd0302 100644 --- a/src/browser/http.ts +++ b/src/browser/http.ts @@ -63,6 +63,10 @@ export function addIndex(pack: Pack, text: string) { const BLOCK = 64 << 10; +function encodePath(path: string): string { + return path.split("/").map(encodeURIComponent).join("/"); +} + function compressed(xhr: XMLHttpRequest): boolean { const enc = xhr.getResponseHeader("Content-Encoding"); return !!enc && enc !== "identity"; @@ -96,7 +100,7 @@ export class HttpBackend implements Backend { private request(method: string, path: string): XMLHttpRequest | null { const xhr = new XMLHttpRequest(); - xhr.open(method, this.base + encodeURI(path), false); + xhr.open(method, this.base + encodePath(path), false); if (method === "GET") xhr.responseType = "arraybuffer"; try { xhr.send(); @@ -170,7 +174,7 @@ export class HttpBackend implements Backend { let end = b; while (end < last && !blocks.has(end + 1)) end++; const xhr = new XMLHttpRequest(); - xhr.open("GET", this.base + encodeURI(path), false); + xhr.open("GET", this.base + encodePath(path), false); xhr.responseType = "arraybuffer"; xhr.setRequestHeader("Range", `bytes=${b * BLOCK}-${(end + 1) * BLOCK - 1}`); try { diff --git a/test/http-files.test.ts b/test/http-files.test.ts new file mode 100644 index 0000000..683cfdd --- /dev/null +++ b/test/http-files.test.ts @@ -0,0 +1,45 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { HttpBackend } from "../src/browser/http.ts"; + +test("HTTP file reads preserve reserved characters in filenames", (t) => { + const files = new Map([ + ["/app/a#b.txt", new Uint8Array([1, 2, 3])], + ["/app/a?b.txt", new Uint8Array([4, 5, 6])], + ["/app/a%20b.txt", new Uint8Array([7, 8, 9])], + ["/app/a\\b-é.txt", new Uint8Array([10, 11, 12])], + ]); + class XHR { + status = 404; + response = new ArrayBuffer(0); + private url!: URL; + private range = false; + open(_method: string, url: string) { this.url = new URL(url); } + setRequestHeader() { this.range = true; } + getResponseHeader(name: string) { + if (name === "Content-Length") return "3"; + if (name === "Content-Range" && this.range) return "bytes 0-2/3"; + return null; + } + send() { + const data = files.get(decodeURIComponent(this.url.pathname)); + if (!data || this.url.search || this.url.hash) return; + this.status = this.range ? 206 : 200; + this.response = data.slice().buffer; + } + } + const original = Object.getOwnPropertyDescriptor(globalThis, "XMLHttpRequest"); + Object.defineProperty(globalThis, "XMLHttpRequest", { configurable: true, value: XHR }); + t.after(() => { + if (original) Object.defineProperty(globalThis, "XMLHttpRequest", original); + else Reflect.deleteProperty(globalThis, "XMLHttpRequest"); + }); + + for (const [path, data] of files) { + const name = path.slice("/app".length); + const backend = new HttpBackend("https://example.test/app"); + assert.equal(backend.stat(name)?.size, data.length, path); + assert.deepEqual(backend.read(name), data, path); + assert.deepEqual(new HttpBackend("https://example.test/app").readRange(name, 1, 1), data.subarray(1, 2), path); + } +});