Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion scripts/build-web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)!]));
Expand Down
24 changes: 17 additions & 7 deletions src/browser/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
// <image>.jar, one per module; a named pack is <name>.pack next to it and is
// fetched whole the first time one of its files is read. Images are .jar
Expand All @@ -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(" ");
Comment thread
project516-review-bot[bot] marked this conversation as resolved.
Resolved
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") {
Expand Down Expand Up @@ -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.
Expand Down
32 changes: 32 additions & 0 deletions test/http-files.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"]);
});
10 changes: 10 additions & 0 deletions test/image.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, "..");
Expand Down Expand Up @@ -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");
Expand Down
Loading
⌂ Proxy