"""Prepare fixed, self-hosted browser assets from verified upstream releases.""" import base64 import hashlib from pathlib import Path import shutil import sys import tarfile PACKAGES = { 'mupdf.tgz': '5e9b53af1da46bee94fd903d22576c93a52b7bc0cec888ca0e39dbb34c836800', 'fflate.tgz': '61fd5061e2fc8e5e3e3129f7f2fec7bd78a313e1bf4becbf1cc1cc9998d141dc', 'xlsx.tgz': '8dc73fc3b00203e72d176e85b50938627c7b086e607c682e8d3c22c02bb99fe8', } FILES = { 'mupdf.tgz': { 'dist/mupdf.js': 'mupdf.js', 'dist/mupdf-wasm.js': 'mupdf-wasm.js', 'dist/mupdf-wasm.wasm': 'wasm-bytes.js', 'LICENSE': 'mupdf-license.txt', }, 'fflate.tgz': {'esm/browser.js':'fflate.mjs','LICENSE':'fflate-license.txt'}, 'xlsx.tgz': {'xlsx.mjs':'xlsx.mjs','LICENSE':'sheetjs-license.txt'}, } def vendoring(source, output): output.mkdir(parents=True, exist_ok=True) for package, files in FILES.items(): path = source / package if hashlib.sha256(path.read_bytes()).hexdigest() != PACKAGES[package]: raise RuntimeError('Dependency hash changed: ' + package) with tarfile.open(path,'r:gz') as archive: for name, target in files.items(): item = archive.getmember('package/' + name) if not item.isfile() or item.size > 13*1024*1024: raise RuntimeError('Unexpected dependency member: ' + name) with archive.extractfile(item) as content, (output/target).open('wb') as copy: if name.endswith('.wasm'): copy.write(b'export const wasmBinary=Uint8Array.from(atob("') base64.encode(content, copy) copy.write(b'"),character=>character.charCodeAt(0));\n') else: shutil.copyfileobj(content,copy) if name.endswith('.wasm'): binary_script=output/target binary_script.write_bytes(binary_script.read_bytes().replace(b'\n',b'')+b'\n') if __name__=='__main__': vendoring(Path(sys.argv[1]),Path(sys.argv[2]))