vole
Site
Clone
Read only, no credentials:
https://rodent.software/vole.git
Write over SSH with a registered key:
git clone ssh://git@rodent.software/vole.git
Self-hosted forge that lives in your Git repo.
|
3b2bcc30
Keenan Nemetz
13d ago
|
1 | #!/usr/bin/env python3 |
| 2 | """Regenerate the vendored three.js bundle in server/web/static/models. | |
| 3 | ||
| 4 | The 3D preview (static/models/model-viewer.js) needs three.js and four of its | |
| 5 | example loaders, so the server embeds them as one minified ES module. app.js | |
| 6 | imports model-viewer.js on demand, which is why a page without a model downloads | |
| 7 | neither the viewer nor three.js. | |
| 8 | ||
| 9 | server/web/static/models/three.bundle.js this script's output | |
| 10 | server/web/static/models/three.LICENSE three.js's MIT license | |
| 11 | server/web/static/models/model-viewer.js written by hand, imports the bundle | |
| 12 | ||
| 13 | The bundle re-exports three's namespace and the loaders the viewer knows: | |
| 14 | ||
| 15 | THREE, OrbitControls, STLLoader, OBJLoader, GLTFLoader, ThreeMFLoader | |
| 16 | ||
| 17 | esbuild resolves the loaders' bare `three` imports and their own dependencies | |
| 18 | (BufferGeometryUtils, SkeletonUtils, and fflate for 3MF) into the one file, so the | |
| 19 | browser needs no import map and the CSP stays script-src 'self'. | |
| 20 | ||
| 21 | scripts/vendor-three.py write the files | |
| 22 | scripts/vendor-three.py --check compare, exit 1 if they differ | |
| 23 | ||
| 24 | Bumping THREE_VERSION is the whole upgrade: rerun, then run the tests, which hold | |
| 25 | the server's model extension table (server/web/media.go) to what the viewer can | |
| 26 | load. node and npm are needed to regenerate the bundle; running Vole is not. | |
| 27 | """ | |
| 28 | ||
| 29 | import argparse | |
| 30 | import os | |
| 31 | import subprocess | |
| 32 | import sys | |
| 33 | import tarfile | |
| 34 | import tempfile | |
| 35 | import urllib.request | |
| 36 | ||
| 37 | THREE_VERSION = "0.187.0" | |
| 38 | ESBUILD_VERSION = "0.28.2" | |
| 39 | ||
| 40 | PACKAGE_URL = "https://registry.npmjs.org/three/-/three-{version}.tgz" | |
| 41 | ||
| 42 | STATIC_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), os.pardir, | |
| 43 | "server", "web", "static", "models") | |
| 44 | BUNDLE = "three.bundle.js" | |
| 45 | LICENSE = "three.LICENSE" | |
| 46 | ||
| 47 | # Re-exported from the bundle. The viewer imports exactly these names, so | |
| 48 | # esbuild can drop the rest of three instead of shipping every export. Adding a | |
| 49 | # loader or a three class is a change here and in model-viewer.js. | |
| 50 | ENTRY = """export { | |
| 51 | WebGLRenderer, Scene, PerspectiveCamera, HemisphereLight, DirectionalLight, | |
|
32eefb58
Keenan Nemetz
9d ago
|
52 | Box3, Vector3, Matrix4, Quaternion, Group, Mesh, MeshStandardMaterial, |
| 53 | SRGBColorSpace, | |
|
3b2bcc30
Keenan Nemetz
13d ago
|
54 | } from 'three'; |
| 55 | export { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js'; | |
| 56 | export { STLLoader } from 'three/examples/jsm/loaders/STLLoader.js'; | |
| 57 | export { OBJLoader } from 'three/examples/jsm/loaders/OBJLoader.js'; | |
| 58 | export { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js'; | |
| 59 | export { ThreeMFLoader } from 'three/examples/jsm/loaders/3MFLoader.js'; | |
| 60 | """ | |
| 61 | ||
| 62 | BANNER = ( | |
| 63 | "/* three.js {version} for Vole's 3D preview: three.module.js with the\n" | |
| 64 | " OrbitControls, STL, OBJ, glTF and 3MF loaders. Generated by\n" | |
| 65 | " scripts/vendor-three.py; edit that, not this file. MIT license: three.LICENSE. */" | |
| 66 | ) | |
| 67 | ||
| 68 | ||
| 69 | def fetch_package(work): | |
| 70 | url = PACKAGE_URL.format(version=THREE_VERSION) | |
| 71 | archive = os.path.join(work, "three.tgz") | |
| 72 | urllib.request.urlretrieve(url, archive) | |
| 73 | with tarfile.open(archive, "r:gz") as tar: | |
| 74 | try: | |
| 75 | # Refuse links and absolute paths in the archive; the filter argument | |
| 76 | # is newer than the oldest Python this repository may be built with. | |
| 77 | tar.extractall(work, filter="data") | |
| 78 | except TypeError: | |
| 79 | tar.extractall(work) | |
| 80 | package = os.path.join(work, "package") | |
| 81 | if not os.path.isdir(package): | |
| 82 | raise SystemExit("vendor-three: the tarball has no package directory") | |
| 83 | return package | |
| 84 | ||
| 85 | ||
| 86 | def build(work, package): | |
| 87 | node_modules = os.path.join(work, "node_modules") | |
| 88 | os.makedirs(node_modules, exist_ok=True) | |
| 89 | os.symlink(package, os.path.join(node_modules, "three")) | |
| 90 | entry = os.path.join(work, "entry.js") | |
| 91 | with open(entry, "w", encoding="utf-8") as f: | |
| 92 | f.write(ENTRY) | |
| 93 | out = os.path.join(work, BUNDLE) | |
| 94 | subprocess.run( | |
| 95 | [ | |
| 96 | "npx", "--yes", f"esbuild@{ESBUILD_VERSION}", entry, | |
| 97 | "--bundle", "--minify", "--format=esm", "--target=es2020", | |
| 98 | "--legal-comments=eof", | |
| 99 | "--log-level=warning", | |
| 100 | f"--banner:js={BANNER.format(version='r' + THREE_VERSION.split('.')[1])}", | |
| 101 | f"--outfile={out}", | |
| 102 | ], | |
| 103 | check=True, | |
| 104 | ) | |
| 105 | with open(out, "rb") as f: | |
| 106 | bundle = f.read() | |
| 107 | with open(os.path.join(package, "LICENSE"), "rb") as f: | |
| 108 | license_text = f.read() | |
| 109 | return bundle, license_text | |
| 110 | ||
| 111 | ||
| 112 | def main(): | |
| 113 | parser = argparse.ArgumentParser(description=__doc__) | |
| 114 | parser.add_argument("--check", action="store_true", | |
| 115 | help="compare the committed files, do not write") | |
| 116 | args = parser.parse_args() | |
| 117 | ||
| 118 | if not os.path.isdir(STATIC_DIR): | |
| 119 | os.makedirs(STATIC_DIR, exist_ok=True) | |
| 120 | ||
| 121 | with tempfile.TemporaryDirectory() as work: | |
| 122 | try: | |
| 123 | package = fetch_package(work) | |
| 124 | bundle, license_text = build(work, package) | |
| 125 | except FileNotFoundError as err: | |
| 126 | raise SystemExit(f"vendor-three: {err}: node and npx are needed to build the bundle") | |
| 127 | except subprocess.CalledProcessError as err: | |
| 128 | raise SystemExit(f"vendor-three: esbuild failed: {err}") | |
| 129 | ||
| 130 | outputs = {BUNDLE: bundle, LICENSE: license_text} | |
| 131 | failed = False | |
| 132 | for name, want in outputs.items(): | |
| 133 | path = os.path.join(STATIC_DIR, name) | |
| 134 | current = None | |
| 135 | if os.path.exists(path): | |
| 136 | with open(path, "rb") as f: | |
| 137 | current = f.read() | |
| 138 | if args.check: | |
| 139 | if current != want: | |
| 140 | print(f"vendor-three: {name} is out of date", file=sys.stderr) | |
| 141 | failed = True | |
| 142 | else: | |
| 143 | with open(path, "wb") as f: | |
| 144 | f.write(want) | |
| 145 | print(f"wrote {os.path.relpath(path)}") | |
| 146 | if failed: | |
| 147 | return 1 | |
| 148 | if args.check: | |
| 149 | print("vendor-three: up to date") | |
| 150 | return 0 | |
| 151 | ||
| 152 | ||
| 153 | if __name__ == "__main__": | |
| 154 | sys.exit(main()) |