10 Companion server.js (2/13)
const here = path.dirname(fileURLToPath(import.meta.url));
const PORT = Number(process.env.PORT) || 8095;
const GAMES_DIR = path.resolve(process.env.GAMES_DIR || path.join(here, 'games'));
const DATA_DIR = path.resolve(process.env.DATA_DIR || path.join(here, 'data'));
const HISTORY = 30; // past turns kept for /out
const DETAILS_MAX = 20 * 1024; // the reader accepts 24 KB for a Details page
const EXCERPT = 160; // characters of output under the first row
const MAX_OBJECTS = 6; // "examine X" rows
const DIRECTIONS = ['north', 'south', 'east', 'west', 'northeast', 'northwest',
'southeast', 'southwest', 'up', 'down', 'in', 'out'];
// ------------------------------------------------------------------ games
function readStory(file) {
const data = fs.readFileSync(file);
if (!/\.(zblorb|zlb|blorb|blb)$/i.test(file)) return new Uint8Array(data);
// Blorb: IFF "FORM....IFRS", take the first ZCOD chunk.
if (data.toString('latin1', 0, 4) !== 'FORM' || data.toString('latin1', 8, 12) !== 'IFRS') {
throw new Error('not a Blorb file');
}
for (let at = 12; at + 8 <= data.length;) {
const type = data.toString('latin1', at, at + 4);
const len = data.readUInt32BE(at + 4);
if (type === 'ZCOD') return new Uint8Array(data.subarray(at + 8, at + 8 + len));
at += 8 + len + (len & 1);
}
throw new Error('no Z-code inside this Blorb file');
}