// Serves the Foundry module as a real installable package: Foundry fetches a manifest (module.json) // and then a zip of the module. The zip is built from `dragonroll-foundry/` at request time, so it // can never drift from the source folder. Stored (uncompressed) entries keep the writer small and // dependency-free; the module is a few dozen KB. import { existsSync, readFileSync, readdirSync } from 'node:fs' import { join, relative, resolve, sep } from 'node:path' const MODULE_DIR = 'dragonroll-foundry' function resolveModuleDir() { const candidates = [ resolve(process.cwd(), MODULE_DIR), // production image: /app/dragonroll-foundry resolve(process.cwd(), '..', MODULE_DIR), // dev server: frontend/ -> repository root ] const dir = candidates.find((candidate) => existsSync(join(candidate, 'module.json'))) if (!dir) throw new Error(`Cannot find ${MODULE_DIR}/module.json`) return dir } function listFiles(dir, base = dir) { return readdirSync(dir, { withFileTypes: true }) .flatMap((entry) => { const full = join(dir, entry.name) return entry.isDirectory() ? listFiles(full, base) : [relative(base, full).split(sep).join('/')] }) .sort() } const CRC_TABLE = new Uint32Array(256).map((_, index) => { let value = index for (let bit = 0; bit < 8; bit++) { value = value & 1 ? 0xedb88320 ^ (value >>> 1) : value >>> 1 } return value >>> 0 }) function crc32(data) { let crc = 0xffffffff for (const byte of data) crc = CRC_TABLE[(crc ^ byte) & 0xff] ^ (crc >>> 8) return (crc ^ 0xffffffff) >>> 0 } export function readModuleManifest() { return JSON.parse(readFileSync(join(resolveModuleDir(), 'module.json'), 'utf8')) } export function buildFoundryModuleZip() { const dir = resolveModuleDir() const files = listFiles(dir) const localParts = [] const centralParts = [] let offset = 0 for (const name of files) { const data = readFileSync(join(dir, name)) const nameBuffer = Buffer.from(name, 'utf8') const crc = crc32(data) const header = Buffer.alloc(30) header.writeUInt32LE(0x04034b50, 0) // local file header header.writeUInt16LE(20, 4) // version needed header.writeUInt16LE(0x0800, 6) // UTF-8 names header.writeUInt16LE(0, 8) // stored header.writeUInt16LE(0, 10) // time header.writeUInt16LE(0x21, 12) // 1980-01-01, fixed so the zip is reproducible header.writeUInt32LE(crc, 14) header.writeUInt32LE(data.length, 18) header.writeUInt32LE(data.length, 22) header.writeUInt16LE(nameBuffer.length, 26) header.writeUInt16LE(0, 28) localParts.push(header, nameBuffer, data) const entry = Buffer.alloc(46) entry.writeUInt32LE(0x02014b50, 0) // central directory header entry.writeUInt16LE(0x031e, 4) // made by UNIX, version 3.0 entry.writeUInt16LE(20, 6) // version needed entry.writeUInt16LE(0x0800, 8) entry.writeUInt16LE(0, 10) entry.writeUInt16LE(0, 12) entry.writeUInt16LE(0x21, 14) entry.writeUInt32LE(crc, 16) entry.writeUInt32LE(data.length, 20) entry.writeUInt32LE(data.length, 24) entry.writeUInt16LE(nameBuffer.length, 28) entry.writeUInt16LE(0, 30) // extra length entry.writeUInt16LE(0, 32) // comment length entry.writeUInt16LE(0, 34) // disk entry.writeUInt16LE(0, 36) // internal attributes entry.writeUInt32LE((0o100644 << 16) >>> 0, 38) // -rw-r--r-- entry.writeUInt32LE(offset, 42) centralParts.push(entry, nameBuffer) offset += header.length + nameBuffer.length + data.length } const central = Buffer.concat(centralParts) const end = Buffer.alloc(22) end.writeUInt32LE(0x06054b50, 0) // end of central directory end.writeUInt16LE(0, 4) // disk end.writeUInt16LE(0, 6) // disk with the central directory end.writeUInt16LE(files.length, 8) end.writeUInt16LE(files.length, 10) end.writeUInt32LE(central.length, 12) end.writeUInt32LE(offset, 16) end.writeUInt16LE(0, 20) // comment length return Buffer.concat([...localParts, central, end]) }