Created
September 30, 2025 14:49
-
-
Save weimzh/01e8f35110c26dfa9c2f91e479ff4198 to your computer and use it in GitHub Desktop.
Check if a package is in pnpm store/cache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| import semantic_version | |
| import re | |
| import json | |
| import base64 | |
| def _version_satisfies_any(version_range, versions): | |
| try: | |
| return semantic_version.NpmSpec(version_range).filter([semantic_version.Version(v) for v in versions]) | |
| except Exception as e: | |
| return [] | |
| def _check_package_in_store(package_name, package_ver, integrity): | |
| # See: pnpm/store/cafs/src/getFilePathInCafs.ts | |
| b64_part = integrity.split("sha512-")[1] | |
| binary_hash = base64.b64decode(b64_part) | |
| hex_hash = binary_hash.hex()[0:64] | |
| json_path = os.path.join(STORE_DIR, f"v10/index/{hex_hash[0:2]}/{hex_hash[2:]}-") | |
| json_path += re.sub(r'[\\/:*?"<>|]', '+', package_name + "@" + package_ver) + ".json" | |
| return os.path.exists(json_path) | |
| def _check_package_in_cache(package_name, package_ver): | |
| json_path = os.path.join(CACHE_DIR, package_name) + ".json" | |
| try: | |
| with open(json_path, "r", encoding="utf-8") as f: | |
| package_json = json.loads(f.read()) | |
| versions = package_json.get("versions", {}) | |
| versions = [key for key in versions] | |
| versions = _version_satisfies_any(package_ver, versions) | |
| versions = [v for v in versions] | |
| versions_valid = [] | |
| for v in versions: | |
| if _check_package_in_store(package_name, str(v), | |
| package_json.get("versions", {}).get(str(v))["dist"]["integrity"]): | |
| versions_valid.append(str(v)) | |
| return versions_valid | |
| except Exception as e: | |
| #print(f"cannot load json file {json_path} {e}") | |
| return [] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment