From 759ae491c3485e90ab998d493d9f20e318c40ac3 Mon Sep 17 00:00:00 2001 From: Project516 <138796702+Project516@users.noreply.github.com> Date: Tue, 3 Nov 2026 18:04:11 -0600 Subject: [PATCH] Fetch an app class in one request and prefix-code the boot index --- scripts/build-web.ts | 12 +++++++++++- src/browser/http.ts | 24 +++++++++++++++++------- test/http-files.test.ts | 32 ++++++++++++++++++++++++++++++++ test/image.test.ts | 10 ++++++++++ 4 files changed, 70 insertions(+), 8 deletions(-) diff --git a/scripts/build-web.ts b/scripts/build-web.ts index 399990e..413ba0e 100644 --- a/scripts/build-web.ts +++ b/scripts/build-web.ts @@ -170,8 +170,18 @@ function writePack(file: string, entries: [string, Uint8Array][]) { return `${(size / 1024).toFixed(0)} KB, ${entries.length} files`; } +let previous = ""; +const compactIndex = lines.map((line) => { + let n = 0; + while (n < line.length && line[n] === previous[n]) n++; + // Keep surrogate pairs together when encoding the suffix as UTF-8. + if (n > 0 && line.charCodeAt(n - 1) >= 0xd800 && line.charCodeAt(n - 1) <= 0xdbff) n--; + const result = `${n} ${line.slice(n)}`; + previous = line; + return result; +}).join("\n"); const boot = writePack(join(dist, "boot.pack"), [ - ["/index.txt", enc.encode(lines.join("\n"))], + ["/index.prefix", enc.encode(compactIndex)], ...[...recorder.seen].sort().map((p): [string, Uint8Array] => [p, recorder.read(p)!]), ]); const javacPack = writePack(join(dist, "jdk", "javac.pack"), javacFiles.map((p) => [p, javacRecorder.read(p)!])); diff --git a/src/browser/http.ts b/src/browser/http.ts index 2bd0302..1b7d355 100644 --- a/src/browser/http.ts +++ b/src/browser/http.ts @@ -15,7 +15,7 @@ export interface Pack { } // Pack layout: repeated [u16 path length][path][u32 size][bytes]. In the boot -// pack, a file named "/index.txt" holds "d path" and "f path size +// pack, "/index.prefix" holds prefix-coded "d path" and "f path size // [image:offset] [pack]" lines. A file with an offset is read in ranges from // .jar, one per module; a named pack is .pack next to it and is // fetched whole the first time one of its files is read. Images are .jar @@ -35,18 +35,25 @@ export function parsePack(buf: ArrayBuffer): Pack { p += 4 + size; } const pack: Pack = { files, index: null, lazy: new Map(), offsets: new Map() }; - const raw = files.get("/index.txt"); + const raw = files.get("/index.prefix") ?? files.get("/index.txt"); if (raw) { pack.index = new Map([["/", -1]]); - addIndex(pack, dec.decode(raw)); + addIndex(pack, dec.decode(raw), files.has("/index.prefix")); files.delete("/index.txt"); + files.delete("/index.prefix"); } return pack; } -// Adds index lines, as in /index.txt, to a pack that has an index. -export function addIndex(pack: Pack, text: string) { - for (const line of text.split("\n")) { +// Adds index lines to a pack that has an index. +export function addIndex(pack: Pack, text: string, prefix = false) { + let previous = ""; + for (let line of text.split("\n")) { + if (prefix) { + const space = line.indexOf(" "); + line = previous.slice(0, Number(line.slice(0, space))) + line.slice(space + 1); + previous = line; + } const [kind, path, size, ...rest] = line.split(" "); if (kind === "d") pack.index!.set(path, -1); else if (kind === "f") { @@ -121,7 +128,10 @@ export class HttpBackend implements Backend { const known = this.cache.get(path); let st: Stat | null = null; if (known) st = { dir: false, size: known.length, mtime: 0 }; - else { + else if (path.endsWith(".class")) { + const data = this.read(path); + if (data) st = { dir: false, size: data.length, mtime: 0 }; + } else { // HTTP cannot tell us about directories. An extensionless path that is // missing, or answered with an HTML page (a server's directory index), // is taken to be one, so class folders work on the classpath. diff --git a/test/http-files.test.ts b/test/http-files.test.ts index 683cfdd..4672100 100644 --- a/test/http-files.test.ts +++ b/test/http-files.test.ts @@ -43,3 +43,35 @@ test("HTTP file reads preserve reserved characters in filenames", (t) => { assert.deepEqual(new HttpBackend("https://example.test/app").readRange(name, 1, 1), data.subarray(1, 2), path); } }); + +test("class metadata and contents use one request without Content-Length", (t) => { + const calls: string[] = []; + class XHR { + status = 404; + response = new ArrayBuffer(0); + private method = ""; + private url = ""; + open(method: string, url: string) { this.method = method; this.url = url; } + getResponseHeader(name: string) { return name === "Content-Type" ? "text/html" : null; } + send() { + calls.push(`${this.method} ${this.url}`); + if (this.url.endsWith("/Found.class")) { + this.status = 200; + this.response = Uint8Array.of(1, 2, 3).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"); + }); + const backend = new HttpBackend("https://example.test/app"); + assert.deepEqual(backend.stat("/Found.class"), { dir: false, size: 3, mtime: 0 }); + assert.deepEqual(backend.read("/Found.class"), Uint8Array.of(1, 2, 3)); + assert.equal(backend.stat("/Missing.class"), null); + assert.equal(backend.read("/Missing.class"), null); + assert.deepEqual(backend.stat("/folder"), { dir: true, size: 0, mtime: 0 }); + assert.deepEqual(calls, ["GET https://example.test/app/Found.class", "GET https://example.test/app/Missing.class", "HEAD https://example.test/app/folder"]); +}); diff --git a/test/image.test.ts b/test/image.test.ts index 51472a9..18fea82 100644 --- a/test/image.test.ts +++ b/test/image.test.ts @@ -9,6 +9,7 @@ import { join } from "node:path"; import { test } from "node:test"; import assert from "node:assert/strict"; import { ImageBackend } from "../src/image.ts"; +import { parsePack, listIndex } from "../src/browser/http.ts"; import { hotspot } from "../scripts/hotspot.ts"; const root = join(import.meta.dirname, ".."); @@ -64,6 +65,15 @@ test("ImageBackend reads the index, packed files and module images", () => { } }); +test("compact index keeps paths, sizes, offsets, and directory order", () => { + const buf = pack([["/index.prefix", "0 d /lib\n0 f /lib/one 3 java.base:0\n7 two 4 java.base:3 javac\n0 d /modules"]]); + const image = parsePack(buf.buffer as ArrayBuffer); + assert.deepEqual([...image.index!], [["/", -1], ["/lib", -1], ["/lib/one", 3], ["/lib/two", 4], ["/modules", -1]]); + assert.deepEqual(listIndex(image.index!, "/lib"), ["one", "two"]); + assert.deepEqual(image.offsets.get("/lib/two"), { image: "/java.base.jar", at: 3 }); + assert.equal(image.lazy.get("/lib/two"), "javac"); +}); + const node = join(root, "dist", "node.js"); const skip = existsSync(node) ? false : "run `pnpm build` first"; const out = join(root, "build", "test-image");