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
4 changes: 2 additions & 2 deletions bench/html-subset-render.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
const { createHash } = require("node:crypto");
const { readFileSync } = require("node:fs");
const { join } = require("node:path");
const { loadMarkengineInRealm } = require("../tests/support/loadMarkengine.cjs");
const { loadMarkengineInRealm, bundleToDisk } = require("../tests/support/loadMarkengine.cjs");

const E = loadMarkengineInRealm("bench/scanEntry.ts");

Expand All @@ -26,7 +26,7 @@ const CACHE_DIR = join(__dirname, "..", "node_modules", ".cache", "loadMarkengin
// tests/support/loadMarkengine.cjs writes it). The digest lets the test hold
// every worker to one bundle (D6): a stale or split bundle shows as two digests.
function bundleEvidence() {
const file = join(CACHE_DIR, `bench_scanEntry_ts.${process.pid}.cjs`);
const file = bundleToDisk("bench/scanEntry.ts");
return { file, sha256: createHash("sha256").update(readFileSync(file)).digest("hex") };
}

Expand Down
26 changes: 26 additions & 0 deletions tests/bundle-cache.test.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"use strict";

const test = require("node:test");
const assert = require("node:assert/strict");
const { basename } = require("node:path");
const { bundleToDisk, computeSourceTreeHash } = require("./support/loadMarkengine.cjs");

test("bundleToDisk keys the cached file by source tree hash rather than pid", () => {
const outfile = bundleToDisk("index.ts");
const fileName = basename(outfile);

// Must match index_ts.<16-char-hash>.cjs
const hash = computeSourceTreeHash();
assert.equal(hash.length, 16);
assert.match(fileName, /^index_ts\.[0-9a-f]{16}\.cjs$/);
assert.equal(fileName, `index_ts.${hash}.cjs`);

// Must not be keyed by process.pid
assert.notEqual(fileName, `index_ts.${process.pid}.cjs`);
});

test("bundleToDisk produces consistent output across invocations", () => {
const first = bundleToDisk("blocks.ts");
const second = bundleToDisk("blocks.ts");
assert.equal(first, second);
});
50 changes: 45 additions & 5 deletions tests/support/loadMarkengine.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
// Shared loader: bundles the TS engine entry with esbuild to disk cache
// so .cjs tests can exercise the public API safely in the current realm.
const { join, resolve } = require("node:path");
const { mkdirSync, writeFileSync, existsSync, renameSync } = require("node:fs");
const { mkdirSync, writeFileSync, existsSync, renameSync, readdirSync, readFileSync, unlinkSync } = require("node:fs");
const { createHash } = require("node:crypto");
const esbuild = require("esbuild");

const MARKENGINE_ROOT = resolve(__dirname, "..", "..");
Expand All @@ -23,6 +24,38 @@ module.exports = {
);
}

let cachedSourceTreeHash = null;

function computeSourceTreeHash() {
if (cachedSourceTreeHash) return cachedSourceTreeHash;
const hash = createHash("sha256");
function walk(dir) {
const entries = readdirSync(dir, { withFileTypes: true });
entries.sort((a, b) => a.name.localeCompare(b.name));
for (const ent of entries) {
if (
ent.name === "node_modules" ||
ent.name === ".git" ||
ent.name === "dist" ||
ent.name === ".cache" ||
ent.name === "coverage"
) {
continue;
}
const full = join(dir, ent.name);
if (ent.isDirectory()) {
walk(full);
} else if (/\.(tsx?|json)$/.test(ent.name)) {
hash.update(ent.name);
hash.update(readFileSync(full));
}
}
}
walk(MARKENGINE_ROOT);
cachedSourceTreeHash = hash.digest("hex").slice(0, 16);
return cachedSourceTreeHash;
}

const memoized = new Map();

function bundleToDisk(entry) {
Expand All @@ -31,10 +64,11 @@ function bundleToDisk(entry) {
throw new Error("Invalid entry point: path traversal detected");
}
const safeName = entry.replace(/[/\\.]/g, "_");
const outfile = join(CACHE_DIR, `${safeName}.${process.pid}.cjs`);
const treeHash = computeSourceTreeHash();
const outfile = join(CACHE_DIR, `${safeName}.${treeHash}.cjs`);

if (!existsSync(outfile)) {
const tmpFile = `${outfile}.${Date.now()}.${Math.random().toString(16).slice(2)}.tmp`;
const tmpFile = `${outfile}.${process.pid}.${Date.now()}.${Math.random().toString(16).slice(2)}.tmp`;
esbuild.buildSync({
entryPoints: [fullPath],
bundle: true,
Expand All @@ -44,7 +78,13 @@ function bundleToDisk(entry) {
outfile: tmpFile,
tsconfig: join(MARKENGINE_ROOT, "tsconfig.json"),
});
renameSync(tmpFile, outfile);
try {
renameSync(tmpFile, outfile);
} catch {
try {
unlinkSync(tmpFile);
} catch {}
}
}

return outfile;
Expand All @@ -70,4 +110,4 @@ function loadMarkengineAll() {
return Object.assign({}, ...entries.map((entry) => loadMarkengine(entry)));
}

module.exports = { loadMarkengine, loadMarkengineAll, loadMarkengineInRealm };
module.exports = { loadMarkengine, loadMarkengineAll, loadMarkengineInRealm, bundleToDisk, computeSourceTreeHash };
Loading