mirror of
https://github.com/rommapp/romm.git
synced 2026-11-30 02:22:44 +01:00
The nginx guard only read the http-level default.conf and the three raw location bodies, so two ways of reintroducing the ROM-corruption bug went undetected. Both were confirmed against the suite before this change, each leaving all seven tests green: - gzip_static added at server scope in the template, with the locations left intact: /library/ and /cache/ inherit it, and a request for Game.nes returns Game.nes.gz's bytes. - gzip_static removed from the fingerprinted-assets regex location, which the parametrised list never covered, silently dropping precompressed bundles. Parse the template's location blocks instead, honouring braces inside the quoted regex matcher, and assert against server scope, each build-output location including the regex one, and any location that gains the directive. A prefix match would have let /assets/romm/resources/ pass as expected, so matchers resolve exactly and only the regex is keyed by its opening. Also tie MIN_BYTES to nginx's gzip_min_length with a test rather than a comment, cover the "gzip made it bigger" branch that the extension filter kept unreachable, drop an em dash, and correct a comment that claimed every file without a .gz sibling is gzipped on the fly, which is untrue for types outside gzip_types. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HDXxq3vAxJVGhm1pQuK8V7
91 lines
2.9 KiB
TypeScript
91 lines
2.9 KiB
TypeScript
import { mkdtemp, mkdir, readFile, readdir, writeFile } from "node:fs/promises";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
import { gunzipSync } from "node:zlib";
|
|
import { describe, expect, it } from "vitest";
|
|
import { precompress } from "../scripts/precompress";
|
|
|
|
/** Run the plugin's build hooks against a throwaway output directory. */
|
|
async function runOn(outDir: string): Promise<void> {
|
|
const plugin = precompress();
|
|
const { configResolved, closeBundle } = plugin as unknown as {
|
|
configResolved: (config: unknown) => void;
|
|
closeBundle: (this: { info: (msg: string) => void }) => Promise<void>;
|
|
};
|
|
configResolved({ root: outDir, build: { outDir } });
|
|
await closeBundle.call({ info: () => {} });
|
|
}
|
|
|
|
async function fixture(
|
|
files: Record<string, string | Buffer>,
|
|
): Promise<string> {
|
|
const dir = await mkdtemp(join(tmpdir(), "precompress-"));
|
|
for (const [name, body] of Object.entries(files)) {
|
|
await mkdir(join(dir, name, ".."), { recursive: true });
|
|
await writeFile(join(dir, name), body);
|
|
}
|
|
return dir;
|
|
}
|
|
|
|
describe("precompress", () => {
|
|
it("only runs on builds, after the other plugins have emitted", () => {
|
|
const plugin = precompress();
|
|
expect(plugin.apply).toBe("build");
|
|
expect(plugin.enforce).toBe("post");
|
|
});
|
|
|
|
it("writes a .gz sibling whose contents round-trip", async () => {
|
|
const body = "const compressible = 1;\n".repeat(200);
|
|
const dir = await fixture({ "assets/app.js": body });
|
|
|
|
await runOn(dir);
|
|
|
|
const gz = await readFile(join(dir, "assets/app.js.gz"));
|
|
expect(gunzipSync(gz).toString()).toBe(body);
|
|
expect(gz.byteLength).toBeLessThan(Buffer.byteLength(body));
|
|
});
|
|
|
|
it("skips files below the gzip_min_length nginx uses", async () => {
|
|
const dir = await fixture({ "tiny.css": "a{color:red}" });
|
|
|
|
await runOn(dir);
|
|
|
|
expect(await readdir(dir)).not.toContain("tiny.css.gz");
|
|
});
|
|
|
|
it("leaves already-compressed formats alone", async () => {
|
|
const dir = await fixture({ "logo.png": Buffer.alloc(4096, 7) });
|
|
|
|
await runOn(dir);
|
|
|
|
expect(await readdir(dir)).not.toContain("logo.png.gz");
|
|
});
|
|
|
|
it("skips a file gzip would make bigger", async () => {
|
|
// High-entropy bytes under a compressible extension, so the extension
|
|
// filter lets it through and only the size guard can reject it.
|
|
const noise = Buffer.alloc(4096);
|
|
let seed = 1;
|
|
for (let i = 0; i < noise.length; i += 1) {
|
|
seed = (seed * 1103515245 + 12345) & 0x7fffffff;
|
|
noise[i] = seed >>> 16;
|
|
}
|
|
const dir = await fixture({ "noise.json": noise });
|
|
|
|
await runOn(dir);
|
|
|
|
expect(await readdir(dir)).not.toContain("noise.json.gz");
|
|
});
|
|
|
|
it("does not compress its own output on a second run", async () => {
|
|
const dir = await fixture({
|
|
"app.js": "const compressible = 1;\n".repeat(200),
|
|
});
|
|
|
|
await runOn(dir);
|
|
await runOn(dir);
|
|
|
|
expect(await readdir(dir)).not.toContain("app.js.gz.gz");
|
|
});
|
|
});
|