tools/client/publish_wasm.py (3242B)
1 """Publish the browser client for Go-only checkouts, without intermediate files.""" 2 import brotli 3 import hashlib 4 import json 5 import pathlib 6 import shutil 7 import subprocess 8 9 10 root = pathlib.Path(__file__).resolve().parents[2] 11 source = root / 'client/build/wasm/web' 12 destination = root / 'server/assets/client' 13 destination.mkdir(parents=True, exist_ok=True) 14 staging = root / 'client/build/wasm/publish' 15 staging.mkdir(exist_ok=True) 16 17 # Publish one matching JS/WASM pair, compressed for transfer to browsers and laptops. 18 for name in ['game.js', 'index.html', 'profile-worker.js', 'audio-worklet.js']: 19 shutil.copyfile(source / name, staging / name) 20 21 # High-quality compression is expensive; reuse it when the executable is unchanged. 22 module_hash = hashlib.sha256((source / 'game.wasm').read_bytes()).hexdigest() 23 compressed = source / 'game.wasm.br' 24 stamp = source / 'game.wasm.br.sha256' 25 if not compressed.exists() or not stamp.exists() or stamp.read_text().strip() != module_hash: 26 compressor = brotli.Compressor() 27 temporary = source / 'game.wasm.br.tmp' 28 with (source / 'game.wasm').open('rb') as input_file, temporary.open('wb') as output_file: 29 while chunk := input_file.read(1024 * 1024): 30 output_file.write(compressor.process(chunk)) 31 output_file.write(compressor.finish()) 32 temporary.replace(compressed) 33 stamp.write_text(module_hash + '\n') 34 shutil.copyfile(compressed, staging / 'game.wasm.br') 35 36 # Fill the loading page from this bundle's exact compressed and decoded sizes. 37 page = (staging / 'index.html').read_text() 38 page = page.replace('__WASM_DOWNLOAD_MB__', f'{compressed.stat().st_size / 1000000:.2f}') 39 page = page.replace('__WASM_DECODED_BYTES__', str((source / 'game.wasm').stat().st_size)) 40 (staging / 'index.html').write_text(page) 41 42 # Record the exact inputs and published bytes, including uncommitted source edits. 43 inputs = sorted(subprocess.check_output( 44 ['git', 'ls-files', '--cached', '--others', '--exclude-standard', 'client', 'tools/client/build_wasm_dependencies.py', 45 'tools/client/resolve_guest_calls.py', 'tools/client/share_guest_bodies.py'], cwd=root 46 ).decode().splitlines()) 47 digest = hashlib.sha256() 48 for name in inputs: 49 digest.update(name.encode() + b'\0') 50 digest.update((root / name).read_bytes()) 51 metadata = { 52 'source_commit': subprocess.check_output(['git', 'rev-parse', 'HEAD'], cwd=root).decode().strip(), 53 'source_sha256': digest.hexdigest(), 54 'compiler': subprocess.check_output([ 55 str(root / 'toolchains/emsdk/upstream/emscripten/emcc'), '--version' 56 ]).decode().splitlines()[0], 57 'files': {}, 58 } 59 for name in ['game.js', 'game.wasm.br', 'index.html', 'profile-worker.js', 'audio-worklet.js']: 60 path = staging / name 61 metadata['files'][name] = { 62 'bytes': path.stat().st_size, 63 'sha256': hashlib.sha256(path.read_bytes()).hexdigest(), 64 } 65 (staging / 'build.json').write_text(json.dumps(metadata, indent=2) + '\n') 66 for name in metadata['files']: 67 (staging / name).replace(destination / name) 68 (staging / 'build.json').replace(destination / 'build.json') 69 for directory in [staging, destination]: 70 (directory / 'game.wasm.gz').unlink(missing_ok=True) 71 print('Published browser client to', destination)