artifact_mirror.py 1.1 KB

1234567891011121314151617181920212223242526272829303132
  1. """Public, content-addressed copies of reviewed binary inputs."""
  2. from __future__ import annotations
  3. import json
  4. import os
  5. from pathlib import Path
  6. import re
  7. from pm.downloader import Source
  8. from pm.index_config import npm_registry_url
  9. _LAYOUT = json.loads(Path(__file__).with_name("artifact-mirror.json").read_text(encoding="utf-8-sig"))
  10. KEY_PREFIX = _LAYOUT["prefix"]
  11. PUBLIC_PREFIX = _LAYOUT["origin"] + "/" + KEY_PREFIX
  12. def object_key(sha256: str) -> str:
  13. if not isinstance(sha256, str) or not re.fullmatch(r"[a-f0-9]{64}", sha256):
  14. raise ValueError("A pinned input requires a full lowercase SHA256")
  15. return KEY_PREFIX + sha256
  16. def mirror_url(sha256: str) -> str:
  17. object_key(sha256)
  18. return PUBLIC_PREFIX + sha256
  19. def pinned_source(url: str, dest: Path, sha256: str) -> Source:
  20. archive = mirror_url(sha256)
  21. # The lock records registry.npmjs.org; a user's npm mirror serves the same pinned bytes (#123132).
  22. url = npm_registry_url(url, os.environ)
  23. return Source(url, dest, sha256, fallbacks=() if url == archive else (archive,))