|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Vendor a pinned build of @mermaid-js/tiny into docs/assets/javascripts/. |
| 3 | +
|
| 4 | +Why this exists: |
| 5 | + docs.aws.amazon.com applies a Content Security Policy that disallows |
| 6 | + third-party script origins. Zensical's default Mermaid integration loads |
| 7 | + mermaid from unpkg.com, which the CSP blocks. We therefore self-host |
| 8 | + Mermaid from docs/assets/ (served same-origin). |
| 9 | +
|
| 10 | + We use the "tiny" build (@mermaid-js/tiny), which ships as a single UMD |
| 11 | + file with no lazy-loaded chunks. All currently-used diagram types |
| 12 | + (flowchart, sequence, state, class, ER) are supported. Mindmap, |
| 13 | + architecture, and KaTeX math are not. |
| 14 | +
|
| 15 | +Configuration: |
| 16 | + Version and expected SHA-256 are read from scripts/vendor_mermaid.toml. |
| 17 | +
|
| 18 | +Usage: |
| 19 | + python3 scripts/vendor_mermaid.py # download and verify |
| 20 | + python3 scripts/vendor_mermaid.py --check # verify only, do not download |
| 21 | + python3 scripts/vendor_mermaid.py --latest # print pinned + latest on npm |
| 22 | +
|
| 23 | +Upgrading: |
| 24 | + 1. Bump `version` in scripts/vendor_mermaid.toml. |
| 25 | + 2. Run the script. It will fail with the new SHA-256 printed. Paste that |
| 26 | + value into `sha256` in the TOML. |
| 27 | + 3. Run the script again. It should succeed. |
| 28 | + 4. Preview with `zensical serve`, then commit the TOML and the vendored |
| 29 | + file together. |
| 30 | +""" |
| 31 | + |
| 32 | +from __future__ import annotations |
| 33 | + |
| 34 | +import argparse |
| 35 | +import hashlib |
| 36 | +import json |
| 37 | +import sys |
| 38 | +import tomllib |
| 39 | +import urllib.request |
| 40 | +from pathlib import Path |
| 41 | + |
| 42 | +REPO_ROOT = Path(__file__).resolve().parent.parent |
| 43 | +PIN_FILE = Path(__file__).resolve().parent / "vendor_mermaid.toml" |
| 44 | +DEST_FILE = REPO_ROOT / "docs" / "assets" / "javascripts" / "mermaid.tiny.js" |
| 45 | +SOURCE_URL_TEMPLATE = ( |
| 46 | + "https://cdn.jsdelivr.net/npm/@mermaid-js/tiny@{version}/dist/mermaid.tiny.js" |
| 47 | +) |
| 48 | +NPM_REGISTRY_LATEST = "https://registry.npmjs.org/@mermaid-js/tiny/latest" |
| 49 | + |
| 50 | + |
| 51 | +def load_pin() -> tuple[str, str]: |
| 52 | + """Read the pinned version and expected SHA-256 from the TOML file.""" |
| 53 | + if not PIN_FILE.exists(): |
| 54 | + print(f"ERROR: pin file missing: {PIN_FILE}", file=sys.stderr) |
| 55 | + sys.exit(1) |
| 56 | + |
| 57 | + with PIN_FILE.open("rb") as handle: |
| 58 | + data = tomllib.load(handle) |
| 59 | + |
| 60 | + missing = [key for key in ("version", "sha256") if key not in data] |
| 61 | + if missing: |
| 62 | + print( |
| 63 | + f"ERROR: {PIN_FILE.name} is missing required keys: " |
| 64 | + f"{', '.join(missing)}", |
| 65 | + file=sys.stderr, |
| 66 | + ) |
| 67 | + sys.exit(1) |
| 68 | + return data["version"], data["sha256"] |
| 69 | + |
| 70 | + |
| 71 | +def sha256_of(path: Path) -> str: |
| 72 | + hasher = hashlib.sha256() |
| 73 | + with path.open("rb") as handle: |
| 74 | + for chunk in iter(lambda: handle.read(65536), b""): |
| 75 | + hasher.update(chunk) |
| 76 | + return hasher.hexdigest() |
| 77 | + |
| 78 | + |
| 79 | +def check_only(version: str, expected_sha256: str) -> int: |
| 80 | + if not DEST_FILE.exists(): |
| 81 | + print( |
| 82 | + f"ERROR: {DEST_FILE.relative_to(REPO_ROOT)} is missing. " |
| 83 | + f"Run without --check to download.", |
| 84 | + file=sys.stderr, |
| 85 | + ) |
| 86 | + return 1 |
| 87 | + |
| 88 | + actual = sha256_of(DEST_FILE) |
| 89 | + if actual != expected_sha256: |
| 90 | + print( |
| 91 | + f"ERROR: SHA-256 mismatch for {DEST_FILE.relative_to(REPO_ROOT)}", |
| 92 | + file=sys.stderr, |
| 93 | + ) |
| 94 | + print(f" expected (from {PIN_FILE.name}): {expected_sha256}", file=sys.stderr) |
| 95 | + print(f" actual: {actual}", file=sys.stderr) |
| 96 | + return 1 |
| 97 | + |
| 98 | + print( |
| 99 | + f"OK: {DEST_FILE.relative_to(REPO_ROOT)} matches " |
| 100 | + f"@mermaid-js/tiny@{version} (sha256={expected_sha256})" |
| 101 | + ) |
| 102 | + return 0 |
| 103 | + |
| 104 | + |
| 105 | +def print_latest(version: str) -> int: |
| 106 | + with urllib.request.urlopen(NPM_REGISTRY_LATEST, timeout=10) as response: |
| 107 | + payload = json.load(response) |
| 108 | + print(f"Pinned: {version}") |
| 109 | + print(f"Latest: {payload['version']}") |
| 110 | + return 0 |
| 111 | + |
| 112 | + |
| 113 | +def download_and_verify(version: str, expected_sha256: str) -> int: |
| 114 | + # Idempotent: skip download if the committed file already matches. |
| 115 | + if DEST_FILE.exists() and sha256_of(DEST_FILE) == expected_sha256: |
| 116 | + print(f"Already up to date: @mermaid-js/tiny@{version}") |
| 117 | + return 0 |
| 118 | + |
| 119 | + source_url = SOURCE_URL_TEMPLATE.format(version=version) |
| 120 | + print(f"Downloading @mermaid-js/tiny@{version} from {source_url}") |
| 121 | + |
| 122 | + DEST_FILE.parent.mkdir(parents=True, exist_ok=True) |
| 123 | + tmp_file = DEST_FILE.with_suffix(DEST_FILE.suffix + ".tmp") |
| 124 | + |
| 125 | + try: |
| 126 | + with urllib.request.urlopen(source_url, timeout=30) as response: |
| 127 | + tmp_file.write_bytes(response.read()) |
| 128 | + |
| 129 | + actual = sha256_of(tmp_file) |
| 130 | + if actual != expected_sha256: |
| 131 | + print("ERROR: SHA-256 mismatch after download", file=sys.stderr) |
| 132 | + print( |
| 133 | + f" expected (from {PIN_FILE.name}): {expected_sha256}", |
| 134 | + file=sys.stderr, |
| 135 | + ) |
| 136 | + print(f" actual: {actual}", file=sys.stderr) |
| 137 | + print( |
| 138 | + f"\nIf you intentionally bumped `version` in {PIN_FILE.name}, " |
| 139 | + f"update `sha256` to the 'actual' value above and re-run.", |
| 140 | + file=sys.stderr, |
| 141 | + ) |
| 142 | + tmp_file.unlink(missing_ok=True) |
| 143 | + return 1 |
| 144 | + |
| 145 | + tmp_file.replace(DEST_FILE) |
| 146 | + except Exception: |
| 147 | + tmp_file.unlink(missing_ok=True) |
| 148 | + raise |
| 149 | + |
| 150 | + print(f"Vendored @mermaid-js/tiny@{version}") |
| 151 | + print(f" Path: {DEST_FILE.relative_to(REPO_ROOT)}") |
| 152 | + print(f" SHA-256: {expected_sha256}") |
| 153 | + return 0 |
| 154 | + |
| 155 | + |
| 156 | +def main() -> int: |
| 157 | + parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) |
| 158 | + group = parser.add_mutually_exclusive_group() |
| 159 | + group.add_argument( |
| 160 | + "--check", |
| 161 | + action="store_true", |
| 162 | + help="Verify the committed vendored file matches the pinned SHA-256 " |
| 163 | + "without downloading.", |
| 164 | + ) |
| 165 | + group.add_argument( |
| 166 | + "--latest", |
| 167 | + action="store_true", |
| 168 | + help="Print the currently-pinned version and the latest version on npm, " |
| 169 | + "then exit.", |
| 170 | + ) |
| 171 | + args = parser.parse_args() |
| 172 | + |
| 173 | + version, expected_sha256 = load_pin() |
| 174 | + |
| 175 | + if args.check: |
| 176 | + return check_only(version, expected_sha256) |
| 177 | + if args.latest: |
| 178 | + return print_latest(version) |
| 179 | + return download_and_verify(version, expected_sha256) |
| 180 | + |
| 181 | + |
| 182 | +if __name__ == "__main__": |
| 183 | + sys.exit(main()) |
0 commit comments