// Source of the only modification to Chromium's generated Liblouis loader/data. // Run alongside the original liblouis_wasm.js/.data; no WebAssembly binary changes. import {readFile,writeFile} from 'node:fs/promises'; const jsPath=new URL('liblouis_wasm.js',import.meta.url),dataPath=new URL('liblouis_wasm.data',import.meta.url); let js=await readFile(jsPath,'utf8');const data=await readFile(dataPath); const match=js.match(/loadPackage\((\{"files":[^\n]+)\);/);if(!match)throw Error('Package metadata missing'); const metadata=JSON.parse(match[1]),byName=new Map(metadata.files.map(f=>[f.filename.slice(1),f])),needed=new Set(); function include(name){if(needed.has(name))return;const file=byName.get(name);if(!file)throw Error('Missing table '+name);needed.add(name);const text=data.subarray(file.start,file.end).toString('utf8');for(const line of text.split(/\r?\n/)){const m=line.match(/^\s*include\s+(\S+)/);if(m)for(const child of m[1].split(','))include(child);}} for(const name of ['unicode.dis','en-ueb-g1.ctb','en-ueb-g2.ctb'])include(name); let offset=0;const chunks=[];metadata.files=metadata.files.filter(f=>needed.has(f.filename.slice(1))).map(f=>{const bytes=data.subarray(f.start,f.end);chunks.push(bytes);const entry={...f,start:offset,end:offset+bytes.length};offset+=bytes.length;return entry;});metadata.remote_package_size=offset;metadata.package_uuid='nextbraille-en-ueb-20261017'; js=js.replace(match[0],`loadPackage(${JSON.stringify(metadata)});`); await writeFile(jsPath,js);await writeFile(dataPath,Buffer.concat(chunks)); console.log(`English UEB data: ${metadata.files.length} tables, ${offset} bytes.`);