|
| 1 | +# |
| 2 | +# Copyright (c) nexB Inc. and others. All rights reserved. |
| 3 | +# purldb is a trademark of nexB Inc. |
| 4 | +# SPDX-License-Identifier: Apache-2.0 |
| 5 | +# See http://www.apache.org/licenses/LICENSE-2.0 for the license text. |
| 6 | +# See https://github.com/aboutcode-org/purldb for support or download. |
| 7 | +# See https://aboutcode.org for more information about nexB OSS projects. |
| 8 | +# |
| 9 | + |
| 10 | + |
| 11 | +import json |
| 12 | +import requests |
| 13 | + |
| 14 | +from packageurl import PackageURL |
| 15 | + |
| 16 | + |
| 17 | +""" |
| 18 | +Visitors for Npmjs and npmjs-like javascript package repositories. |
| 19 | +
|
| 20 | +We have this hierarchy in npm replicate and registry index: |
| 21 | + npm projects replicate.npmjs.com (paginated JSON) -> versions at registry.npmjs.org (JSON) -> download urls |
| 22 | +
|
| 23 | +See https://github.com/orgs/community/discussions/152515 for information on |
| 24 | +the latest replicate.npmjs.com API. |
| 25 | +
|
| 26 | +https://replicate.npmjs.com/_all_docs |
| 27 | +This NPMJS replicate API serves as an index to get all npm packages and their revision IDs |
| 28 | +in paginated queries. |
| 29 | +
|
| 30 | +https://replicate.npmjs.com/_changes |
| 31 | +This NPMJS replicate API serves as a CHANGELOG of npm packages with update sequneces which |
| 32 | +can be fetched in paginated queries. |
| 33 | +
|
| 34 | +https://registry.npmjs.org/{namespace/name} |
| 35 | +For each npm package, a JSON containing details including the list of all releases |
| 36 | +and archives, their URLs, and some metadata for each release. |
| 37 | +
|
| 38 | +https://registry.npmjs.org/{namespace/name}/{version} |
| 39 | +For each release, a JSON contains details for the released version and all the |
| 40 | +downloads available for this release. |
| 41 | +""" |
| 42 | + |
| 43 | + |
| 44 | +NPM_REPLICATE_REPO = "https://replicate.npmjs.com/" |
| 45 | +NPM_REGISTRY_REPO = "https://registry.npmjs.org/" |
| 46 | +NPM_TYPE = "NPM" |
| 47 | +NPM_REPLICATE_BATCH_SIZE = 10000 |
| 48 | + |
| 49 | + |
| 50 | +def get_package_names_last_key(package_data): |
| 51 | + names = [package.get("id") for package in package_data.get("rows")] |
| 52 | + last_key = package_data.get("rows")[-1].get("key") |
| 53 | + return names, last_key |
| 54 | + |
| 55 | + |
| 56 | +def get_package_names_last_seq(package_data): |
| 57 | + names = [package.get("id") for package in package_data.get("results")] |
| 58 | + last_seq = package_data.get("last_seq") |
| 59 | + return names, last_seq |
| 60 | + |
| 61 | + |
| 62 | +def get_current_last_seq(replicate_url=NPM_REPLICATE_REPO): |
| 63 | + npm_replicate_latest_changes = replicate_url + "_changes?descending=True" |
| 64 | + response = requests.get(npm_replicate_latest_changes) |
| 65 | + if not response.ok: |
| 66 | + return |
| 67 | + |
| 68 | + package_data = response.json() |
| 69 | + _package_names, last_seq = get_package_names_last_seq(package_data) |
| 70 | + return last_seq |
| 71 | + |
| 72 | + |
| 73 | +def get_updated_npm_packages(last_seq, replicate_url=NPM_REPLICATE_REPO): |
| 74 | + all_package_names = [] |
| 75 | + i = 0 |
| 76 | + |
| 77 | + while True: |
| 78 | + print(f"Processing iteration: {i}: changes after seq: {last_seq}") |
| 79 | + npm_replicate_changes = ( |
| 80 | + replicate_url + "_changes?" + f"limit={NPM_REPLICATE_BATCH_SIZE}" + f"&since={last_seq}" |
| 81 | + ) |
| 82 | + response = requests.get(npm_replicate_changes) |
| 83 | + if not response.ok: |
| 84 | + return all_package_names |
| 85 | + |
| 86 | + package_data = response.json() |
| 87 | + package_names, last_seq = get_package_names_last_seq(package_data) |
| 88 | + all_package_names.extend(package_names) |
| 89 | + |
| 90 | + # We have fetched the last set of changes if True |
| 91 | + if len(package_names) < NPM_REPLICATE_BATCH_SIZE: |
| 92 | + break |
| 93 | + |
| 94 | + i += 1 |
| 95 | + |
| 96 | + return {"packages": all_package_names}, last_seq |
| 97 | + |
| 98 | + |
| 99 | +def get_npm_packages(replicate_url=NPM_REPLICATE_REPO): |
| 100 | + all_package_names = [] |
| 101 | + |
| 102 | + npm_replicate_all = replicate_url + "_all_docs?" + f"limit={NPM_REPLICATE_BATCH_SIZE}" |
| 103 | + response = requests.get(npm_replicate_all) |
| 104 | + if not response.ok: |
| 105 | + return all_package_names |
| 106 | + |
| 107 | + package_data = response.json() |
| 108 | + package_names, last_key = get_package_names_last_key(package_data) |
| 109 | + all_package_names.append(package_names) |
| 110 | + |
| 111 | + total_rows = package_data.get("total_rows") |
| 112 | + iterations = int(total_rows / NPM_REPLICATE_BATCH_SIZE) + 1 |
| 113 | + |
| 114 | + for i in range(iterations): |
| 115 | + npm_replicate_from_id = npm_replicate_all + f'&start_key="{last_key}"' |
| 116 | + print(f"Processing iteration: {i}: {npm_replicate_from_id}") |
| 117 | + |
| 118 | + response = requests.get(npm_replicate_from_id) |
| 119 | + if not response.ok: |
| 120 | + raise Exception(npm_replicate_from_id, response.text) |
| 121 | + |
| 122 | + package_data = response.json() |
| 123 | + package_names, last_key = get_package_names_last_key(package_data) |
| 124 | + all_package_names.append(package_names) |
| 125 | + |
| 126 | + return {"packages": all_package_names} |
| 127 | + |
| 128 | + |
| 129 | +def get_npm_packageurls(name, npm_repo=NPM_REGISTRY_REPO): |
| 130 | + packageurls = [] |
| 131 | + |
| 132 | + project_index_api_url = npm_repo + name |
| 133 | + response = requests.get(project_index_api_url) |
| 134 | + if not response.ok: |
| 135 | + return packageurls |
| 136 | + |
| 137 | + project_data = response.json() |
| 138 | + for version in project_data.get("versions"): |
| 139 | + purl = PackageURL( |
| 140 | + type=NPM_TYPE, |
| 141 | + name=name, |
| 142 | + version=version, |
| 143 | + ) |
| 144 | + packageurls.append(purl.to_string()) |
| 145 | + |
| 146 | + return packageurls |
| 147 | + |
| 148 | + |
| 149 | +def load_npm_packages(packages_file): |
| 150 | + with open(packages_file) as f: |
| 151 | + packages_data = json.load(f) |
| 152 | + |
| 153 | + return packages_data.get("packages", []) |
0 commit comments