@procedures/npm 0.55.1 → 0.55.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +19 -0
- package/README.md +33 -0
- package/dist/measures/deps-dev.d.ts +35 -0
- package/dist/measures/deps-dev.d.ts.map +1 -0
- package/dist/measures/deps-dev.js +78 -0
- package/dist/measures/deps-dev.js.map +1 -0
- package/dist/measures/downloads.d.ts +23 -0
- package/dist/measures/downloads.d.ts.map +1 -0
- package/dist/measures/downloads.js +68 -0
- package/dist/measures/downloads.js.map +1 -0
- package/dist/measures/index.d.ts +10 -0
- package/dist/measures/index.d.ts.map +1 -0
- package/dist/measures/index.js +10 -0
- package/dist/measures/index.js.map +1 -0
- package/dist/measures/jsdelivr.d.ts +26 -0
- package/dist/measures/jsdelivr.d.ts.map +1 -0
- package/dist/measures/jsdelivr.js +50 -0
- package/dist/measures/jsdelivr.js.map +1 -0
- package/dist/measures/registry.d.ts +40 -0
- package/dist/measures/registry.d.ts.map +1 -0
- package/dist/measures/registry.js +89 -0
- package/dist/measures/registry.js.map +1 -0
- package/dist/measures/shared.d.ts +53 -0
- package/dist/measures/shared.d.ts.map +1 -0
- package/dist/measures/shared.js +61 -0
- package/dist/measures/shared.js.map +1 -0
- package/package.json +12 -4
- package/src/measures/deps-dev.ts +98 -0
- package/src/measures/downloads.ts +76 -0
- package/src/measures/index.ts +9 -0
- package/src/measures/jsdelivr.ts +69 -0
- package/src/measures/registry.ts +113 -0
- package/src/measures/shared.ts +97 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,24 @@
|
|
|
1
1
|
# @procedures/npm
|
|
2
2
|
|
|
3
|
+
## 0.55.2
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 12ececd: `@procedures/npm` has the new entry `./measures`. It reads how much a package is used, from free public sources. The sources are the npm downloads, the dependents of deps.dev, the hits of jsDelivr and the npm registry.
|
|
8
|
+
|
|
9
|
+
- Each measure is a pure pair. A request builder gives the URL and the `Accept` header. An extractor reads the body text into typed rows.
|
|
10
|
+
- `downloadsRequests` puts up to 128 packages without a scope into one bulk request.
|
|
11
|
+
- The request builders accept the names of old packages with capital letters, for example `JSONStream`.
|
|
12
|
+
- The extractors read the bodies with `parseJsonOrUndefined` and `isPlainObject` of `@procedures/json`.
|
|
13
|
+
- An extractor puts each package that the source does not know into `missing`. A body of another shape throws an error that names the URL.
|
|
14
|
+
- The caller gives the fetch. `measure(fetch, request, extract)` accepts `globalThis.fetch`, and a crawler can use the pairs in its own recipes.
|
|
15
|
+
- The entry has no Node built-in, so a browser can also use it.
|
|
16
|
+
- The tests read responses that were recorded from each source.
|
|
17
|
+
- Updated dependencies [12ececd]
|
|
18
|
+
- @procedures/json@0.55.2
|
|
19
|
+
- @procedures/client@0.55.2
|
|
20
|
+
- @procedures/shell@0.55.2
|
|
21
|
+
|
|
3
22
|
## 0.55.1
|
|
4
23
|
|
|
5
24
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -57,6 +57,39 @@ await invokePath(
|
|
|
57
57
|
mark npm pack --cwd packages/app
|
|
58
58
|
```
|
|
59
59
|
|
|
60
|
+
## The use measures
|
|
61
|
+
|
|
62
|
+
The entry `@procedures/npm/measures` reads how much a package is used, from free public sources. It has no Node built-in, so a browser can also use it.
|
|
63
|
+
|
|
64
|
+
Each measure is a pair of pure functions. The request builder gives the URL and the `Accept` header. The extractor reads the body text of the response into typed rows. Neither function sends a request.
|
|
65
|
+
|
|
66
|
+
| Measure | Request builder | Extractor | Row |
|
|
67
|
+
|---|---|---|---|
|
|
68
|
+
| npm downloads | `downloadsRequests(packages, period)` | `extractDownloads` | `DownloadsRow` |
|
|
69
|
+
| deps.dev versions | `depsDevPackageRequest(name)` | `extractDepsDevPackage` | `DepsDevPackage` |
|
|
70
|
+
| deps.dev dependents | `depsDevDependentsRequest(name, version)` | `extractDepsDevDependents` | `DepsDevDependents` |
|
|
71
|
+
| jsDelivr hits | `jsdelivrHitsRequest(name, period)` | `extractJsdelivrHits` | `JsdelivrHits` |
|
|
72
|
+
| The version of a tag | `registryVersionRequest(name, tag)` | `extractRegistryVersion` | `RegistryVersion` |
|
|
73
|
+
| The abbreviated document | `registryDocumentRequest(name)` | `extractRegistryDocument` | `RegistryDocument` |
|
|
74
|
+
|
|
75
|
+
- The npm downloads include the installs through other packages. The deps.dev dependents include the indirect dependents. Thus both measures include transitive use.
|
|
76
|
+
- `downloadsRequests` puts up to 128 packages without a scope into one bulk request. Each package with a scope gets its own request.
|
|
77
|
+
- The request builders accept the name of each existing package. Some old packages have names that a new package cannot have, for example `JSONStream`. `assertExistingPackageName` applies the rules of npm for these names.
|
|
78
|
+
- An extractor puts each package that the source does not know into `missing`. A body of another shape, for example a rate limit, throws an error that names the URL.
|
|
79
|
+
- The dependents need a version. `extractDepsDevPackage` gives the default version.
|
|
80
|
+
|
|
81
|
+
The caller gives the fetch. `measure(fetch, request, extract)` sends one request and reads the response. `globalThis.fetch` fits the type `MeasureFetch`. A crawler can use the request builder and the extractor in its own recipe.
|
|
82
|
+
|
|
83
|
+
```typescript
|
|
84
|
+
import { downloadsRequests, extractDownloads, measure } from "@procedures/npm/measures";
|
|
85
|
+
|
|
86
|
+
for (const request of downloadsRequests(["sockette", "@stomp/stompjs"], "last-week")) {
|
|
87
|
+
const { rows, missing } = await measure(globalThis.fetch, request, extractDownloads);
|
|
88
|
+
}
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
`measure` does not wait between two requests. Keep the pace of each source: the npm downloads API refuses fast series of requests with HTTP 429.
|
|
92
|
+
|
|
60
93
|
## How the package finds npm
|
|
61
94
|
|
|
62
95
|
On Windows, `npm` is a `.cmd` file, and Node starts a `.cmd` file only through a shell. So `resolveNpm()` finds the `npm-cli.js` script of the npm of this Node, and starts it with Node. When the npm CLI starts the process, the script comes from `npm_execpath`.
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { type Extracted, type MeasureRequest } from "./shared.js";
|
|
2
|
+
/** One version of a package, as deps.dev knows it. */
|
|
3
|
+
export interface DepsDevVersion {
|
|
4
|
+
readonly version: string;
|
|
5
|
+
readonly publishedAt: string | null;
|
|
6
|
+
readonly isDefault: boolean;
|
|
7
|
+
/** The reason of the deprecation of the version, or `null` when the version is not deprecated. */
|
|
8
|
+
readonly deprecated: string | null;
|
|
9
|
+
}
|
|
10
|
+
/** A package as deps.dev knows it. The default version is the version of the tag `latest`. */
|
|
11
|
+
export interface DepsDevPackage {
|
|
12
|
+
readonly package: string;
|
|
13
|
+
readonly defaultVersion: string | null;
|
|
14
|
+
readonly versions: readonly DepsDevVersion[];
|
|
15
|
+
}
|
|
16
|
+
/** The dependents of one version of a package. `dependents` is the sum of the direct and the indirect dependents. */
|
|
17
|
+
export interface DepsDevDependents {
|
|
18
|
+
readonly package: string;
|
|
19
|
+
readonly version: string;
|
|
20
|
+
readonly dependents: number;
|
|
21
|
+
readonly direct: number;
|
|
22
|
+
readonly indirect: number;
|
|
23
|
+
}
|
|
24
|
+
/** This function gives the request for the versions of a package. */
|
|
25
|
+
export declare function depsDevPackageRequest(name: string): MeasureRequest;
|
|
26
|
+
/** This function gives the request for the dependents of one version of a package. */
|
|
27
|
+
export declare function depsDevDependentsRequest(name: string, version: string): MeasureRequest;
|
|
28
|
+
/** This function reads the response of `depsDevPackageRequest`. A package that deps.dev does not know is in `missing`. */
|
|
29
|
+
export declare function extractDepsDevPackage(text: string, url: string): Extracted<DepsDevPackage>;
|
|
30
|
+
/**
|
|
31
|
+
* This function reads the response of `depsDevDependentsRequest`. A version that deps.dev does not know is in
|
|
32
|
+
* `missing`, as `name@version`.
|
|
33
|
+
*/
|
|
34
|
+
export declare function extractDepsDevDependents(text: string, url: string): Extracted<DepsDevDependents>;
|
|
35
|
+
//# sourceMappingURL=deps-dev.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"deps-dev.d.ts","sourceRoot":"","sources":["../../src/measures/deps-dev.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iB,CAAC,CAiBhG"}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The versions and the dependents of a package from the deps.dev API (`api.deps.dev`).
|
|
3
|
+
*
|
|
4
|
+
* The dependents of a version are the packages that depend on it. deps.dev counts the direct and the indirect
|
|
5
|
+
* dependents. Thus the count includes transitive use. The dependents need a version, and the package request
|
|
6
|
+
* gives the default version.
|
|
7
|
+
*/
|
|
8
|
+
import { isPlainObject, parseJsonOrUndefined } from "@procedures/json";
|
|
9
|
+
import { ACCEPT_JSON, assertExistingPackageName, pathAfter, shapeError } from "./shared.js";
|
|
10
|
+
const PACKAGE_PREFIX = "/v3/systems/npm/packages/";
|
|
11
|
+
const DEPENDENTS_PREFIX = "/v3alpha/systems/npm/packages/";
|
|
12
|
+
/** This function gives the request for the versions of a package. */
|
|
13
|
+
export function depsDevPackageRequest(name) {
|
|
14
|
+
assertExistingPackageName(name);
|
|
15
|
+
return { url: `https://api.deps.dev${PACKAGE_PREFIX}${encodeURIComponent(name)}`, accept: ACCEPT_JSON };
|
|
16
|
+
}
|
|
17
|
+
/** This function gives the request for the dependents of one version of a package. */
|
|
18
|
+
export function depsDevDependentsRequest(name, version) {
|
|
19
|
+
assertExistingPackageName(name);
|
|
20
|
+
if (version.trim() === "")
|
|
21
|
+
throw new Error("The version of a dependents request is empty.");
|
|
22
|
+
return { url: `https://api.deps.dev${DEPENDENTS_PREFIX}${encodeURIComponent(name)}/versions/${encodeURIComponent(version)}:dependents`, accept: ACCEPT_JSON };
|
|
23
|
+
}
|
|
24
|
+
/** This function reads the response of `depsDevPackageRequest`. A package that deps.dev does not know is in `missing`. */
|
|
25
|
+
export function extractDepsDevPackage(text, url) {
|
|
26
|
+
const [name] = pathAfter(url, PACKAGE_PREFIX);
|
|
27
|
+
if (name === undefined)
|
|
28
|
+
throw new Error(`${url} names no package.`);
|
|
29
|
+
if (text.trim() === "package not found")
|
|
30
|
+
return { rows: [], missing: [name] };
|
|
31
|
+
const body = parseJsonOrUndefined(text);
|
|
32
|
+
const versions = isPlainObject(body) ? body["versions"] : undefined;
|
|
33
|
+
if (!Array.isArray(versions))
|
|
34
|
+
throw shapeError(url, "a deps.dev package", text);
|
|
35
|
+
const rows = versions.map((entry) => {
|
|
36
|
+
const key = isPlainObject(entry) ? entry["versionKey"] : undefined;
|
|
37
|
+
const version = isPlainObject(key) ? key["version"] : undefined;
|
|
38
|
+
if (!isPlainObject(entry) || typeof version !== "string")
|
|
39
|
+
throw shapeError(url, "a deps.dev package", text);
|
|
40
|
+
const published = entry["publishedAt"];
|
|
41
|
+
const reason = entry["deprecatedReason"];
|
|
42
|
+
return {
|
|
43
|
+
version,
|
|
44
|
+
publishedAt: typeof published === "string" && published !== "" ? published : null,
|
|
45
|
+
isDefault: entry["isDefault"] === true,
|
|
46
|
+
deprecated: entry["isDeprecated"] === true ? (typeof reason === "string" ? reason : "") : null,
|
|
47
|
+
};
|
|
48
|
+
});
|
|
49
|
+
const found = rows.find((v) => v.isDefault);
|
|
50
|
+
return { rows: [{ package: name, defaultVersion: found === undefined ? null : found.version, versions: rows }], missing: [] };
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* This function reads the response of `depsDevDependentsRequest`. A version that deps.dev does not know is in
|
|
54
|
+
* `missing`, as `name@version`.
|
|
55
|
+
*/
|
|
56
|
+
export function extractDepsDevDependents(text, url) {
|
|
57
|
+
const [name, , last] = pathAfter(url, DEPENDENTS_PREFIX);
|
|
58
|
+
const version = last?.replace(/:dependents$/, "");
|
|
59
|
+
if (name === undefined || version === undefined)
|
|
60
|
+
throw new Error(`${url} names no package version.`);
|
|
61
|
+
if (text.trim() === "dependents not found")
|
|
62
|
+
return { rows: [], missing: [`${name}@${version}`] };
|
|
63
|
+
const body = parseJsonOrUndefined(text);
|
|
64
|
+
if (!isPlainObject(body))
|
|
65
|
+
throw shapeError(url, "a deps.dev count of dependents", text);
|
|
66
|
+
const count = (field) => {
|
|
67
|
+
// The proto3 JSON of deps.dev leaves out each field with the value 0. Thus a missing count is 0, not an error.
|
|
68
|
+
const value = body[field] ?? 0;
|
|
69
|
+
if (!Number.isInteger(value))
|
|
70
|
+
throw shapeError(url, "a deps.dev count of dependents", text);
|
|
71
|
+
return value;
|
|
72
|
+
};
|
|
73
|
+
return {
|
|
74
|
+
rows: [{ package: name, version, dependents: count("dependentCount"), direct: count("directDependentCount"), indirect: count("indirectDependentCount") }],
|
|
75
|
+
missing: [],
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
//# sourceMappingURL=deps-dev.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"deps-dev.js","sourceRoot":"","sources":["../../src/measures/deps-dev.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,EAAE,aAAa,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAC;AACvE,OAAO,EAAE,WAAW,EAAuC,yBAAyB,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AA2BjI,MAAM,cAAc,GAAG,2BAA2B,CAAC;AACnD,MAAM,iBAAiB,GAAG,gCAAgC,CAAC;AAE3D,qEAAqE;AACrE,MAAM,UAAU,qBAAqB,CAAC,IAAY;IAChD,yBAAyB,CAAC,IAAI,CAAC,CAAC;IAChC,OAAO,EAAE,GAAG,EAAE,uBAAuB,cAAc,GAAG,kBAAkB,CAAC,IAAI,CAAC,EAAE,EAAE,MAAM,EAAE,WAAW,EAAE,CAAC;AAC1G,CAAC;AAED,sFAAsF;AACtF,MAAM,UAAU,wBAAwB,CAAC,IAAY,EAAE,OAAe;IACpE,yBAAyB,CAAC,IAAI,CAAC,CAAC;IAChC,IAAI,OAAO,CAAC,IAAI,EAAE,KAAK,EAAE;QAAE,MAAM,IAAI,KAAK,CAAC,+CAA+C,CAAC,CAAC;IAC5F,OAAO,EAAE,GAAG,EAAE,uBAAuB,iBAAiB,GAAG,kBAAkB,CAAC,IAAI,CAAC,aAAa,kBAAkB,CAAC,O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EAAE,KAAK,sBAAsB;QAAE,OAAO,EAAE,IAAI,EAAE,EAAE,EAAE,OAAO,EAAE,CAAC,GAAG,IAAI,IAAI,OAAO,EAAE,CAAC,EAAE,CAAC;IACjG,MAAM,IAAI,GAAG,oBAAoB,CAAC,IAAI,CAAC,CAAC;IACxC,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC;QAAE,MAAM,UAAU,CAAC,GAAG,EAAE,gCAAgC,EAAE,IAAI,CAAC,CAAC;IACxF,MAAM,KAAK,GAAG,CAAC,KAAa,EAAU,EAAE;QACtC,+GAA+G;QAC/G,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QAC/B,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC;YAAE,MAAM,UAAU,CAAC,GAAG,EAAE,gCAAgC,EAAE,IAAI,CAAC,CAAC;QAC5F,OAAO,KAAe,CAAC;IACzB,CAAC,CAAC;IACF,OAAO;QACL,IAAI,EAAE,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,UAAU,EAAE,KAAK,CAAC,gBAAgB,CAAC,EAAE,MAAM,EAAE,KAAK,CAAC,sBAAsB,CAAC,EAAE,QAAQ,EAAE,KAAK,CAAC,wBAAwB,CAAC,EAAE,CAAC;QACzJ,OAAO,EAAE,EAAE;KACZ,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type Extracted, type MeasureRequest } from "./shared.js";
|
|
2
|
+
/** The most packages of one bulk request of the npm downloads API. */
|
|
3
|
+
export declare const DOWNLOADS_BULK_LIMIT = 128;
|
|
4
|
+
/** A period of the npm downloads API: a named period, or two days `YYYY-MM-DD:YYYY-MM-DD`. */
|
|
5
|
+
export type DownloadsPeriod = "last-day" | "last-week" | "last-month" | `${string}:${string}`;
|
|
6
|
+
/** The downloads of one package in one period. The days are the first and the last day of the period. */
|
|
7
|
+
export interface DownloadsRow {
|
|
8
|
+
readonly package: string;
|
|
9
|
+
readonly start: string;
|
|
10
|
+
readonly end: string;
|
|
11
|
+
readonly downloads: number;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* This function gives the requests for the downloads of the packages in one period. Each package goes into one
|
|
15
|
+
* request: the packages without a scope go into bulk requests, and each package with a scope gets its own request.
|
|
16
|
+
*/
|
|
17
|
+
export declare function downloadsRequests(packages: readonly string[], period?: DownloadsPeriod): MeasureRequest[];
|
|
18
|
+
/**
|
|
19
|
+
* This function reads the response of a request of `downloadsRequests`. A package that the registry does not know is
|
|
20
|
+
* in `missing`. An error of another kind, for example a rate limit, throws an error.
|
|
21
|
+
*/
|
|
22
|
+
export declare function extractDownloads(text: string, url: string): Extracted<DownloadsRow>;
|
|
23
|
+
//# sourceMappingURL=downloads.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"downloads.d.ts","sourceRoot":"","sources":["../../src/measures/downloads.ts"],"names":[],"mappings":"AAOA,OAAO,EAAe,KAAK,SAAS,EAAE,KAAK,cAAc,EAAoD,MAAM,aAAa,CAAC;AAEjI,sEAAsE;AACtE,eAAO,MAAM,oBAAoB,MAAM,CAAC;AAExC,8FAA8F;AAC9F,MAAM,MAAM,eAAe,GAAG,UAAU,GAAG,WAAW,GAAG,YAAY,GAAG,GAAG,MAAM,IAAI,MAAM,EAAE,CAAC;AAE9F,yGAAyG;AACzG,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;CAC5B;AAKD;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,SAAS,MAAM,EAAE,EAAE,MAAM,GAAE,eAA6B,GAAG,cAAc,EAAE,CAYtH;AASD;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,SAAS,CAAC,YAAY,CAAC,CAoBnF"}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The downloads of a package from the npm downloads API (`api.npmjs.org`).
|
|
3
|
+
*
|
|
4
|
+
* The count includes each install that comes through another package, so it includes transitive use. A bulk request
|
|
5
|
+
* gives the counts of up to 128 packages without a scope. A package with a scope needs a request of its own.
|
|
6
|
+
*/
|
|
7
|
+
import { isPlainObject, parseJsonOrUndefined } from "@procedures/json";
|
|
8
|
+
import { ACCEPT_JSON, assertExistingPackageName, pathAfter, shapeError } from "./shared.js";
|
|
9
|
+
/** The most packages of one bulk request of the npm downloads API. */
|
|
10
|
+
export const DOWNLOADS_BULK_LIMIT = 128;
|
|
11
|
+
const PREFIX = "/downloads/point/";
|
|
12
|
+
const PERIOD = /^(?:last-day|last-week|last-month|\d{4}-\d{2}-\d{2}:\d{4}-\d{2}-\d{2})$/;
|
|
13
|
+
/**
|
|
14
|
+
* This function gives the requests for the downloads of the packages in one period. Each package goes into one
|
|
15
|
+
* request: the packages without a scope go into bulk requests, and each package with a scope gets its own request.
|
|
16
|
+
*/
|
|
17
|
+
export function downloadsRequests(packages, period = "last-week") {
|
|
18
|
+
if (!PERIOD.test(period))
|
|
19
|
+
throw new Error(`"${period}" is not a period of the npm downloads API.`);
|
|
20
|
+
const unique = [...new Set(packages)];
|
|
21
|
+
unique.forEach(assertExistingPackageName);
|
|
22
|
+
const base = `https://api.npmjs.org${PREFIX}${period}/`;
|
|
23
|
+
const plain = unique.filter((name) => !name.startsWith("@"));
|
|
24
|
+
const requests = [];
|
|
25
|
+
for (let i = 0; i < plain.length; i += DOWNLOADS_BULK_LIMIT) {
|
|
26
|
+
requests.push({ url: base + plain.slice(i, i + DOWNLOADS_BULK_LIMIT).join(","), accept: ACCEPT_JSON });
|
|
27
|
+
}
|
|
28
|
+
for (const name of unique.filter((n) => n.startsWith("@")))
|
|
29
|
+
requests.push({ url: base + name, accept: ACCEPT_JSON });
|
|
30
|
+
return requests;
|
|
31
|
+
}
|
|
32
|
+
function row(url, value, text) {
|
|
33
|
+
if (!isPlainObject(value) || typeof value["package"] !== "string" || typeof value["start"] !== "string" || typeof value["end"] !== "string" || !Number.isInteger(value["downloads"])) {
|
|
34
|
+
throw shapeError(url, "a count of downloads", text);
|
|
35
|
+
}
|
|
36
|
+
return { package: value["package"], start: value["start"], end: value["end"], downloads: value["downloads"] };
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* This function reads the response of a request of `downloadsRequests`. A package that the registry does not know is
|
|
40
|
+
* in `missing`. An error of another kind, for example a rate limit, throws an error.
|
|
41
|
+
*/
|
|
42
|
+
export function extractDownloads(text, url) {
|
|
43
|
+
const [, ...rest] = pathAfter(url, PREFIX);
|
|
44
|
+
const joined = rest.join("/");
|
|
45
|
+
const names = joined.startsWith("@") ? [joined] : joined.split(",");
|
|
46
|
+
const body = parseJsonOrUndefined(text);
|
|
47
|
+
if (!isPlainObject(body))
|
|
48
|
+
throw shapeError(url, "a JSON object", text);
|
|
49
|
+
const error = body["error"];
|
|
50
|
+
if (typeof error === "string") {
|
|
51
|
+
if (names.length === 1 && /not found/i.test(error))
|
|
52
|
+
return { rows: [], missing: names };
|
|
53
|
+
throw new Error(`${url} answered the error "${error}".`);
|
|
54
|
+
}
|
|
55
|
+
if ("downloads" in body)
|
|
56
|
+
return { rows: [row(url, body, text)], missing: [] };
|
|
57
|
+
const rows = [];
|
|
58
|
+
const missing = [];
|
|
59
|
+
for (const name of names) {
|
|
60
|
+
const value = body[name];
|
|
61
|
+
if (value === null || value === undefined)
|
|
62
|
+
missing.push(name);
|
|
63
|
+
else
|
|
64
|
+
rows.push(row(url, value, text));
|
|
65
|
+
}
|
|
66
|
+
return { rows, missing };
|
|
67
|
+
}
|
|
68
|
+
//# sourceMappingURL=downloads.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"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pairs of a request builder and an extractor. This entry has no Node
|
|
3
|
+
* built-in and registers nothing, so a browser can also use it.
|
|
4
|
+
*/
|
|
5
|
+
export { type Extracted, type Extractor, type MeasureFetch, type MeasureRequest, assertExistingPackageName, measure } from "./shared.js";
|
|
6
|
+
export * from "./downloads.js";
|
|
7
|
+
export * from "./deps-dev.js";
|
|
8
|
+
export * from "./jsdelivr.js";
|
|
9
|
+
export * from "./registry.js";
|
|
10
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/measures/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAE,KAAK,SAAS,EAAE,KAAK,SAAS,EAAE,KAAK,YAAY,EAAE,KAAK,cAAc,EAAE,yBAAyB,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AACzI,cAAc,gBAAgB,CAAC;AAC/B,cAAc,eAAe,CAAC;AAC9B,cAAc,eAAe,CAAC;AAC9B,cAAc,eAAe,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The use measures of npm packages, as pure pairs of a request builder and an extractor. This entry has no Node
|
|
3
|
+
* built-in and registers nothing, so a browser can also use it.
|
|
4
|
+
*/
|
|
5
|
+
export { assertExistingPackageName, measure } from "./shared.js";
|
|
6
|
+
export * from "./downloads.js";
|
|
7
|
+
export * from "./deps-dev.js";
|
|
8
|
+
export * from "./jsdelivr.js";
|
|
9
|
+
export * from "./registry.js";
|
|
10
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/measures/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAA0E,yBAAyB,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AACzI,cAAc,gBAAgB,CAAC;AAC/B,cAAc,eAAe,CAAC;AAC9B,cAAc,eAAe,CAAC;AAC9B,cAAc,eAAe,CAAC"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { type Extracted, type MeasureRequest } from "./shared.js";
|
|
2
|
+
/** A period of the jsDelivr data API. Each period ends on the last full day. */
|
|
3
|
+
export type JsdelivrPeriod = "day" | "week" | "month" | "quarter" | "year";
|
|
4
|
+
/**
|
|
5
|
+
* The hits of one package in one period. `rank` is the place of the package among all packages of jsDelivr, and
|
|
6
|
+
* `typeRank` is its place among the npm packages. Each rank is `null` when the package has no hits.
|
|
7
|
+
*/
|
|
8
|
+
export interface JsdelivrHits {
|
|
9
|
+
readonly package: string;
|
|
10
|
+
readonly period: JsdelivrPeriod;
|
|
11
|
+
readonly start: string | null;
|
|
12
|
+
readonly end: string | null;
|
|
13
|
+
readonly hits: number;
|
|
14
|
+
readonly rank: number | null;
|
|
15
|
+
readonly typeRank: number | null;
|
|
16
|
+
/** The hits of the period before this period. */
|
|
17
|
+
readonly previousHits: number | null;
|
|
18
|
+
}
|
|
19
|
+
/** This function gives the request for the hits of a package in one period. */
|
|
20
|
+
export declare function jsdelivrHitsRequest(name: string, period?: JsdelivrPeriod): MeasureRequest;
|
|
21
|
+
/**
|
|
22
|
+
* This function reads the response of `jsdelivrHitsRequest`. jsDelivr gives 0 hits and no rank for a package that it
|
|
23
|
+
* does not know. Thus `missing` is always empty.
|
|
24
|
+
*/
|
|
25
|
+
export declare function extractJsdelivrHits(text: string, url: string): Extracted<JsdelivrHits>;
|
|
26
|
+
//# sourceMappingURL=jsdelivr.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jsdelivr.d.ts","sourceRoot":"","sources":["../../src/measures/jsdelivr.ts"],"names":[],"mappings":"AAOA,OAAO,EAAe,KAAK,SAAS,EAAE,KAAK,cAAc,EAAoD,MAAM,aAAa,CAAC;AAEjI,gFAAgF;AAChF,MAAM,MAAM,cAAc,GAAG,KAAK,GAAG,MAAM,GAAG,OAAO,GAAG,SAAS,GAAG,MAAM,CAAC;AAE3E;;;GAGG;AACH,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,MAAM,EAAE,cAAc,CAAC;IAChC,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IAC9B,QAAQ,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,CAAC;IAC5B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IAC7B,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IACjC,iDAAiD;IACjD,QAAQ,CAAC,YAAY,EAAE,MAAM,GAAG,IAAI,CAAC;CACtC;AAKD,+EAA+E;AAC/E,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,GAAE,cAAwB,GAAG,cAAc,CAIlG;AAID;;;GAGG;AACH,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,SAAS,CAAC,YAAY,CAAC,CAwBtF"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The hits of a package on the jsDelivr CDN, from the jsDelivr data API (`data.jsdelivr.com`).
|
|
3
|
+
*
|
|
4
|
+
* A hit is one request of a file of the package to the CDN. Many pages load a browser client from a CDN, not from a
|
|
5
|
+
* bundle. The hits measure this use, and the npm downloads do not count it.
|
|
6
|
+
*/
|
|
7
|
+
import { isPlainObject, parseJsonOrUndefined } from "@procedures/json";
|
|
8
|
+
import { ACCEPT_JSON, assertExistingPackageName, pathAfter, shapeError } from "./shared.js";
|
|
9
|
+
const PREFIX = "/v1/stats/packages/npm/";
|
|
10
|
+
const PERIODS = ["day", "week", "month", "quarter", "year"];
|
|
11
|
+
/** This function gives the request for the hits of a package in one period. */
|
|
12
|
+
export function jsdelivrHitsRequest(name, period = "month") {
|
|
13
|
+
assertExistingPackageName(name);
|
|
14
|
+
if (!PERIODS.includes(period))
|
|
15
|
+
throw new Error(`"${period}" is not a period of the jsDelivr data API.`);
|
|
16
|
+
return { url: `https://data.jsdelivr.com${PREFIX}${name}?period=${period}`, accept: ACCEPT_JSON };
|
|
17
|
+
}
|
|
18
|
+
const rankOf = (value) => (Number.isInteger(value) ? value : null);
|
|
19
|
+
/**
|
|
20
|
+
* This function reads the response of `jsdelivrHitsRequest`. jsDelivr gives 0 hits and no rank for a package that it
|
|
21
|
+
* does not know. Thus `missing` is always empty.
|
|
22
|
+
*/
|
|
23
|
+
export function extractJsdelivrHits(text, url) {
|
|
24
|
+
const name = pathAfter(url, PREFIX).join("/");
|
|
25
|
+
const period = new URL(url).searchParams.get("period");
|
|
26
|
+
if (period === null || !PERIODS.includes(period))
|
|
27
|
+
throw new Error(`${url} names no period of the jsDelivr data API.`);
|
|
28
|
+
const body = parseJsonOrUndefined(text);
|
|
29
|
+
const hits = isPlainObject(body) ? body["hits"] : undefined;
|
|
30
|
+
if (!isPlainObject(hits) || !Number.isInteger(hits["total"]))
|
|
31
|
+
throw shapeError(url, "jsDelivr hits", text);
|
|
32
|
+
const days = isPlainObject(hits["dates"]) ? Object.keys(hits["dates"]).sort() : [];
|
|
33
|
+
const previous = hits["prev"];
|
|
34
|
+
return {
|
|
35
|
+
rows: [
|
|
36
|
+
{
|
|
37
|
+
package: name,
|
|
38
|
+
period,
|
|
39
|
+
start: days[0] ?? null,
|
|
40
|
+
end: days.at(-1) ?? null,
|
|
41
|
+
hits: hits["total"],
|
|
42
|
+
rank: rankOf(hits["rank"]),
|
|
43
|
+
typeRank: rankOf(hits["typeRank"]),
|
|
44
|
+
previousHits: isPlainObject(previous) && Number.isInteger(previous["total"]) ? previous["total"] : null,
|
|
45
|
+
},
|
|
46
|
+
],
|
|
47
|
+
missing: [],
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
//# sourceMappingURL=jsdelivr.js.map
|
|
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,CAAC,IAAI;aACpH;SACF;QACD,OAAO,EAAE,EAAE;KACZ,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { type Extracted, type MeasureRequest } from "./shared.js";
|
|
2
|
+
/** The `Accept` header of the abbreviated document of a package. */
|
|
3
|
+
export declare const ACCEPT_ABBREVIATED = "application/vnd.npm.install-v1+json";
|
|
4
|
+
/** The version that a tag of a package names, with the facts of that version. */
|
|
5
|
+
export interface RegistryVersion {
|
|
6
|
+
readonly package: string;
|
|
7
|
+
readonly version: string;
|
|
8
|
+
/** The message of the deprecation, or `null` when the version is not deprecated. */
|
|
9
|
+
readonly deprecated: string | null;
|
|
10
|
+
/** The URL of the repository as the package states it, or `null`. The function does not change its form. */
|
|
11
|
+
readonly repository: string | null;
|
|
12
|
+
/** The folder of the package in its repository, for a package of a monorepo. */
|
|
13
|
+
readonly repositoryDirectory: string | null;
|
|
14
|
+
readonly license: string | null;
|
|
15
|
+
}
|
|
16
|
+
/** The facts of the abbreviated document of a package. */
|
|
17
|
+
export interface RegistryDocument {
|
|
18
|
+
readonly package: string;
|
|
19
|
+
/**
|
|
20
|
+
* The time of the last change of the document. A release changes it. A deprecation or a change of a tag changes it
|
|
21
|
+
* too, so it is not the time of the last release.
|
|
22
|
+
*/
|
|
23
|
+
readonly modified: string | null;
|
|
24
|
+
readonly distTags: Readonly<Record<string, string>>;
|
|
25
|
+
readonly versions: number;
|
|
26
|
+
/** The message of the deprecation of the version of the tag `latest`, or `null`. */
|
|
27
|
+
readonly latestDeprecated: string | null;
|
|
28
|
+
}
|
|
29
|
+
/** This function gives the request for the version document of a tag of a package. */
|
|
30
|
+
export declare function registryVersionRequest(name: string, tag?: string): MeasureRequest;
|
|
31
|
+
/** This function gives the request for the abbreviated document of a package. */
|
|
32
|
+
export declare function registryDocumentRequest(name: string): MeasureRequest;
|
|
33
|
+
/**
|
|
34
|
+
* This function reads the response of `registryVersionRequest`. A package or a tag that the registry does not know is
|
|
35
|
+
* in `missing`.
|
|
36
|
+
*/
|
|
37
|
+
export declare function extractRegistryVersion(text: string, url: string): Extracted<RegistryVersion>;
|
|
38
|
+
/** This function reads the response of `registryDocumentRequest`. A package that the registry does not know is in `missing`. */
|
|
39
|
+
export declare function extractRegistryDocument(text: string, url: string): Extracted<RegistryDocument>;
|
|
40
|
+
//# sourceMappingURL=registry.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"registry.d.ts","sourceRoot":"","sources":["../../src/measures/registry.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,KAAK,SAAS,EAAE,KAAK,cAAc,EAAsC,MAAM,aAAa,CAAC;AAEtG,oEAAoE;AACpE,eAAO,MAAM,kBAAkB,wCAAwC,CAAC;AAExE,iFAAiF;AACjF,MAAM,WAAW,eAAe;IAC9B,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,oFAAoF;IACpF,QAAQ,CAAC,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;IACnC,4GAA4G;IAC5G,QAAQ,CAAC,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;IACnC,gFAAgF;IAChF,QAAQ,CAAC,mBAAmB,EAAE,MAAM,GAAG,IAAI,CAAC;IAC5C,QAAQ,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;CACjC;AAED,0DAA0D;AAC1D,MAAM,WAAW,gBAAgB;IAC/B,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB;;;OAGG;IACH,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IACjC,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;IACpD,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,oFAAoF;IACpF,QAAQ,CAAC,gBAAgB,EAAE,MAAM,GAAG,IAAI,CAAC;CAC1C;AAED,sFAAsF;AACtF,wBAAgB,sBAAsB,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,SAAW,GAAG,cAAc,CAGnF;AAED,iFAAiF;AACjF,wBAAgB,uBAAuB,CAAC,IAAI,EAAE,MAAM,GAAG,cAAc,CAEpE;AAgBD;;;GAGG;AACH,wBAAgB,sBAAsB,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,SAAS,CAAC,eAAe,CAAC,CAoB5F;AAED,gIAAgI;AAChI,wBAAgB,uBAAuB,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,SAAS,CAAC,gBAAgB,CAAC,CAoB9F"}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The facts of a package from the npm registry (`registry.npmjs.org`), as plain HTTP reads. The procedures
|
|
3
|
+
* `npm.view` and `npm.distTags.list` read the same data through the npm CLI.
|
|
4
|
+
*
|
|
5
|
+
* - The version document of a tag gives the version, its deprecation, its repository and its license.
|
|
6
|
+
* - The abbreviated document gives the tags, the count of versions and the time of the last change.
|
|
7
|
+
*/
|
|
8
|
+
import { isPlainObject, parseJsonOrUndefined } from "@procedures/json";
|
|
9
|
+
import { nameSegment, pathAfter, shapeError } from "./shared.js";
|
|
10
|
+
/** The `Accept` header of the abbreviated document of a package. */
|
|
11
|
+
export const ACCEPT_ABBREVIATED = "application/vnd.npm.install-v1+json";
|
|
12
|
+
/** This function gives the request for the version document of a tag of a package. */
|
|
13
|
+
export function registryVersionRequest(name, tag = "latest") {
|
|
14
|
+
if (tag.trim() === "")
|
|
15
|
+
throw new Error("The tag of a version request is empty.");
|
|
16
|
+
return { url: `https://registry.npmjs.org/${nameSegment(name)}/${encodeURIComponent(tag)}`, accept: "application/json" };
|
|
17
|
+
}
|
|
18
|
+
/** This function gives the request for the abbreviated document of a package. */
|
|
19
|
+
export function registryDocumentRequest(name) {
|
|
20
|
+
return { url: `https://registry.npmjs.org/${nameSegment(name)}`, accept: ACCEPT_ABBREVIATED };
|
|
21
|
+
}
|
|
22
|
+
/** This function gives the name of a package from the path segments of a registry URL. */
|
|
23
|
+
function nameOf(segments) {
|
|
24
|
+
const [first, ...rest] = segments.flatMap((segment) => segment.split("/"));
|
|
25
|
+
if (first === undefined)
|
|
26
|
+
throw new Error("The URL names no package.");
|
|
27
|
+
if (first.startsWith("@")) {
|
|
28
|
+
const [second, ...more] = rest;
|
|
29
|
+
if (second === undefined)
|
|
30
|
+
throw new Error(`The URL names the scope ${first} and no package.`);
|
|
31
|
+
return { name: `${first}/${second}`, rest: more };
|
|
32
|
+
}
|
|
33
|
+
return { name: first, rest };
|
|
34
|
+
}
|
|
35
|
+
const textOf = (value) => (typeof value === "string" && value !== "" ? value : null);
|
|
36
|
+
/**
|
|
37
|
+
* This function reads the response of `registryVersionRequest`. A package or a tag that the registry does not know is
|
|
38
|
+
* in `missing`.
|
|
39
|
+
*/
|
|
40
|
+
export function extractRegistryVersion(text, url) {
|
|
41
|
+
const { name } = nameOf(pathAfter(url, "/"));
|
|
42
|
+
const json = parseJsonOrUndefined(text);
|
|
43
|
+
if (typeof json === "string" && /not found/i.test(json))
|
|
44
|
+
return { rows: [], missing: [name] };
|
|
45
|
+
if (!isPlainObject(json) || typeof json["version"] !== "string")
|
|
46
|
+
throw shapeError(url, "an npm version document", text);
|
|
47
|
+
const repo = json["repository"];
|
|
48
|
+
const license = json["license"];
|
|
49
|
+
return {
|
|
50
|
+
rows: [
|
|
51
|
+
{
|
|
52
|
+
package: name,
|
|
53
|
+
version: json["version"],
|
|
54
|
+
deprecated: typeof json["deprecated"] === "string" ? json["deprecated"] : null,
|
|
55
|
+
repository: isPlainObject(repo) ? textOf(repo["url"]) : textOf(repo),
|
|
56
|
+
repositoryDirectory: isPlainObject(repo) ? textOf(repo["directory"]) : null,
|
|
57
|
+
license: isPlainObject(license) ? textOf(license["type"]) : textOf(license),
|
|
58
|
+
},
|
|
59
|
+
],
|
|
60
|
+
missing: [],
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
/** This function reads the response of `registryDocumentRequest`. A package that the registry does not know is in `missing`. */
|
|
64
|
+
export function extractRegistryDocument(text, url) {
|
|
65
|
+
const { name } = nameOf(pathAfter(url, "/"));
|
|
66
|
+
const json = parseJsonOrUndefined(text);
|
|
67
|
+
if (isPlainObject(json) && typeof json["error"] === "string" && /not found/i.test(json["error"]))
|
|
68
|
+
return { rows: [], missing: [name] };
|
|
69
|
+
if (!isPlainObject(json) || !isPlainObject(json["versions"]) || !isPlainObject(json["dist-tags"]))
|
|
70
|
+
throw shapeError(url, "an abbreviated npm document", text);
|
|
71
|
+
const tags = {};
|
|
72
|
+
for (const [tag, version] of Object.entries(json["dist-tags"]))
|
|
73
|
+
if (typeof version === "string")
|
|
74
|
+
tags[tag] = version;
|
|
75
|
+
const latest = tags["latest"] === undefined ? undefined : json["versions"][tags["latest"]];
|
|
76
|
+
return {
|
|
77
|
+
rows: [
|
|
78
|
+
{
|
|
79
|
+
package: name,
|
|
80
|
+
modified: textOf(json["modified"]),
|
|
81
|
+
distTags: tags,
|
|
82
|
+
versions: Object.keys(json["versions"]).length,
|
|
83
|
+
latestDeprecated: isPlainObject(latest) && typeof latest["deprecated"] === "string" ? latest["deprecated"] : null,
|
|
84
|
+
},
|
|
85
|
+
],
|
|
86
|
+
missing: [],
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
//# sourceMappingURL=registry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"registry.js","sourceRoot":"","sources":["../../src/measures/registry.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,EAAE,aAAa,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAC;AACvE,OAAO,EAAuC,WAAW,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEtG,oEAAoE;AACpE,MAAM,CAAC,MAAM,kBAAkB,GAAG,qCA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AAS,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC;IAC3F,OAAO;QACL,IAAI,EAAE;YACJ;gBACE,OAAO,EAAE,IAAI;gBACb,QAAQ,EAAE,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;gBAClC,QAAQ,EAAE,IAAI;gBACd,QAAQ,EAAE,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,MAAM;gBAC9C,gBAAgB,EAAE,aAAa,CAAC,MAAM,CAAC,IAAI,OAAO,MAAM,CAAC,YAAY,CAAC,KAAK,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,IAAI;aAClH;SACF;QACD,OAAO,EAAE,EAAE;KACZ,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The parts that each measure shares: the request, the result of an extractor, the fetch and the runner.
|
|
3
|
+
*
|
|
4
|
+
* A measure is a pure pair. Its request builder gives the URL and the `Accept` header, and its extractor reads the body
|
|
5
|
+
* text of the response into typed rows. Neither function sends a request. The caller gives the fetch, so the same pair
|
|
6
|
+
* works with `globalThis.fetch` and with the recipes of a crawler.
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* One request of a measure. Its fields fit the `RecipeRequest` of `polite-crawler` and the `init` of `fetch`.
|
|
10
|
+
*/
|
|
11
|
+
export interface MeasureRequest {
|
|
12
|
+
readonly url: string;
|
|
13
|
+
readonly accept: string;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* The result of an extractor. `rows` holds the values that the response gives. `missing` holds each package that the
|
|
17
|
+
* source does not know.
|
|
18
|
+
*/
|
|
19
|
+
export interface Extracted<Row> {
|
|
20
|
+
readonly rows: readonly Row[];
|
|
21
|
+
readonly missing: readonly string[];
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* An extractor reads the body text of one response. The URL of the request tells which packages the body answers for.
|
|
25
|
+
* An extractor throws an error that names the URL when the body has a shape that it does not know.
|
|
26
|
+
*/
|
|
27
|
+
export type Extractor<Row> = (text: string, url: string) => Extracted<Row>;
|
|
28
|
+
/** A fetch that gives the body text of a response. `globalThis.fetch` fits this type. */
|
|
29
|
+
export type MeasureFetch = (url: string, init: {
|
|
30
|
+
readonly headers: Readonly<Record<string, string>>;
|
|
31
|
+
}) => Promise<{
|
|
32
|
+
readonly text: () => Promise<string>;
|
|
33
|
+
}>;
|
|
34
|
+
/**
|
|
35
|
+
* This function sends one request of a measure through the given fetch and reads the response with the extractor. It
|
|
36
|
+
* does not wait between requests. The caller keeps the pace of each source.
|
|
37
|
+
*/
|
|
38
|
+
export declare function measure<Row>(fetch: MeasureFetch, request: MeasureRequest, extract: Extractor<Row>): Promise<Extracted<Row>>;
|
|
39
|
+
/** The `Accept` header of a JSON API. */
|
|
40
|
+
export declare const ACCEPT_JSON = "application/json";
|
|
41
|
+
/**
|
|
42
|
+
* This function throws an error when the text cannot be the name of a package on npm. It applies the rules of npm for
|
|
43
|
+
* existing packages, because old packages keep names that a new package cannot have. Thus capital letters and the
|
|
44
|
+
* characters `~'!()*` are valid, for example `JSONStream`. Each part of the name is safe in a URL.
|
|
45
|
+
*/
|
|
46
|
+
export declare function assertExistingPackageName(name: string): void;
|
|
47
|
+
/** This function gives the name of a package as one segment of a URL path: `@scope/name` becomes `@scope%2Fname`. */
|
|
48
|
+
export declare function nameSegment(name: string): string;
|
|
49
|
+
/** This function gives the error of a body with a shape that the extractor does not know. */
|
|
50
|
+
export declare function shapeError(url: string, what: string, text: string): Error;
|
|
51
|
+
/** This function gives the path segments of a URL after a prefix, with each segment decoded. */
|
|
52
|
+
export declare function pathAfter(url: string, prefix: string): string[];
|
|
53
|
+
//# sourceMappingURL=shared.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version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AAgB,UAAU,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,KAAK,CAGzE;AAED,gGAAgG;AAChG,wBAAgB,SAAS,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,CAQ/D"}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The parts that each measure shares: the request, the result of an extractor, the fetch and the runner.
|
|
3
|
+
*
|
|
4
|
+
* A measure is a pure pair. Its request builder gives the URL and the `Accept` header, and its extractor reads the body
|
|
5
|
+
* text of the response into typed rows. Neither function sends a request. The caller gives the fetch, so the same pair
|
|
6
|
+
* works with `globalThis.fetch` and with the recipes of a crawler.
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* This function sends one request of a measure through the given fetch and reads the response with the extractor. It
|
|
10
|
+
* does not wait between requests. The caller keeps the pace of each source.
|
|
11
|
+
*/
|
|
12
|
+
export async function measure(fetch, request, extract) {
|
|
13
|
+
const response = await fetch(request.url, { headers: { accept: request.accept } });
|
|
14
|
+
return extract(await response.text(), request.url);
|
|
15
|
+
}
|
|
16
|
+
/** The `Accept` header of a JSON API. */
|
|
17
|
+
export const ACCEPT_JSON = "application/json";
|
|
18
|
+
/** The names that npm refuses for all packages, also for old packages. */
|
|
19
|
+
const EXCLUDED = new Set(["node_modules", "favicon.ico"]);
|
|
20
|
+
const SCOPED = /^@([^/]+)\/([^/]+)$/;
|
|
21
|
+
const urlSafe = (part) => part !== "" && encodeURIComponent(part) === part;
|
|
22
|
+
/**
|
|
23
|
+
* This function throws an error when the text cannot be the name of a package on npm. It applies the rules of npm for
|
|
24
|
+
* existing packages, because old packages keep names that a new package cannot have. Thus capital letters and the
|
|
25
|
+
* characters `~'!()*` are valid, for example `JSONStream`. Each part of the name is safe in a URL.
|
|
26
|
+
*/
|
|
27
|
+
export function assertExistingPackageName(name) {
|
|
28
|
+
const scoped = SCOPED.exec(name);
|
|
29
|
+
const parts = scoped === null ? [name] : scoped.slice(1);
|
|
30
|
+
const valid = name.length <= 214 &&
|
|
31
|
+
!name.startsWith(".") &&
|
|
32
|
+
!name.startsWith("_") &&
|
|
33
|
+
!EXCLUDED.has(name.toLowerCase()) &&
|
|
34
|
+
parts.every(urlSafe) &&
|
|
35
|
+
// The name of a scoped package does not start with a period. Only a scoped name has a valid slash.
|
|
36
|
+
!name.includes("/.");
|
|
37
|
+
if (!valid)
|
|
38
|
+
throw new Error(`"${name}" is not a valid npm package name.`);
|
|
39
|
+
}
|
|
40
|
+
/** This function gives the name of a package as one segment of a URL path: `@scope/name` becomes `@scope%2Fname`. */
|
|
41
|
+
export function nameSegment(name) {
|
|
42
|
+
assertExistingPackageName(name);
|
|
43
|
+
return name.replaceAll("/", "%2F");
|
|
44
|
+
}
|
|
45
|
+
/** This function gives the error of a body with a shape that the extractor does not know. */
|
|
46
|
+
export function shapeError(url, what, text) {
|
|
47
|
+
const start = text.length > 200 ? `${text.slice(0, 200)}...` : text;
|
|
48
|
+
return new Error(`${url} answered a body that is not ${what}: ${start}`);
|
|
49
|
+
}
|
|
50
|
+
/** This function gives the path segments of a URL after a prefix, with each segment decoded. */
|
|
51
|
+
export function pathAfter(url, prefix) {
|
|
52
|
+
const path = new URL(url).pathname;
|
|
53
|
+
if (!path.startsWith(prefix))
|
|
54
|
+
throw new Error(`${url} is not a URL of the form ${prefix}...`);
|
|
55
|
+
return path
|
|
56
|
+
.slice(prefix.length)
|
|
57
|
+
.split("/")
|
|
58
|
+
.filter((segment) => segment !== "")
|
|
59
|
+
.map((segment) => decodeURIComponent(segment));
|
|
60
|
+
}
|
|
61
|
+
//# sourceMappingURL=shared.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shared.js","sourceRoot":"","sources":["../../src/measures/shared.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AA+BH;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,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
|
-
"version": "0.55.
|
|
5
|
-
"description": "The npm registry as typed procedures: view, dist-tags, whoami, pack (a dry run by default) and deprecate, run with argv, and a confirmation for each write to the registry.",
|
|
4
|
+
"version": "0.55.2",
|
|
5
|
+
"description": "The npm registry as typed procedures: view, dist-tags, whoami, pack (a dry run by default) and deprecate, run with argv, and a confirmation for each write to the registry. The entry ./measures reads the use of a package: npm downloads, deps.dev dependents and jsDelivr hits.",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"npm",
|
|
8
8
|
"registry",
|
|
@@ -30,6 +30,10 @@
|
|
|
30
30
|
"types": "./dist/index.d.ts",
|
|
31
31
|
"import": "./dist/index.js"
|
|
32
32
|
},
|
|
33
|
+
"./measures": {
|
|
34
|
+
"types": "./dist/measures/index.d.ts",
|
|
35
|
+
"import": "./dist/measures/index.js"
|
|
36
|
+
},
|
|
33
37
|
"./module": {
|
|
34
38
|
"types": "./dist/module.d.ts",
|
|
35
39
|
"import": "./dist/module.js"
|
|
@@ -65,11 +69,15 @@
|
|
|
65
69
|
"provenance": true
|
|
66
70
|
},
|
|
67
71
|
"client": {
|
|
72
|
+
"browser": [
|
|
73
|
+
"./measures"
|
|
74
|
+
],
|
|
68
75
|
"procedures": "./dist/register.js"
|
|
69
76
|
},
|
|
70
77
|
"dependencies": {
|
|
71
|
-
"@procedures/client": "0.55.
|
|
72
|
-
"@procedures/
|
|
78
|
+
"@procedures/client": "0.55.2",
|
|
79
|
+
"@procedures/json": "0.55.2",
|
|
80
|
+
"@procedures/shell": "0.55.2",
|
|
73
81
|
"zod": "^4.6.5"
|
|
74
82
|
},
|
|
75
83
|
"scripts": {
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The versions and the dependents of a package from the deps.dev API (`api.deps.dev`).
|
|
3
|
+
*
|
|
4
|
+
* The dependents of a version are the packages that depend on it. deps.dev counts the direct and the indirect
|
|
5
|
+
* dependents. Thus the count includes transitive use. The dependents need a version, and the package request
|
|
6
|
+
* gives the default version.
|
|
7
|
+
*/
|
|
8
|
+
import { isPlainObject, parseJsonOrUndefined } from "@procedures/json";
|
|
9
|
+
import { ACCEPT_JSON, type Extracted, type MeasureRequest, assertExistingPackageName, pathAfter, shapeError } from "./shared.js";
|
|
10
|
+
|
|
11
|
+
/** One version of a package, as deps.dev knows it. */
|
|
12
|
+
export interface DepsDevVersion {
|
|
13
|
+
readonly version: string;
|
|
14
|
+
readonly publishedAt: string | null;
|
|
15
|
+
readonly isDefault: boolean;
|
|
16
|
+
/** The reason of the deprecation of the version, or `null` when the version is not deprecated. */
|
|
17
|
+
readonly deprecated: string | null;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** A package as deps.dev knows it. The default version is the version of the tag `latest`. */
|
|
21
|
+
export interface DepsDevPackage {
|
|
22
|
+
readonly package: string;
|
|
23
|
+
readonly defaultVersion: string | null;
|
|
24
|
+
readonly versions: readonly DepsDevVersion[];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** The dependents of one version of a package. `dependents` is the sum of the direct and the indirect dependents. */
|
|
28
|
+
export interface DepsDevDependents {
|
|
29
|
+
readonly package: string;
|
|
30
|
+
readonly version: string;
|
|
31
|
+
readonly dependents: number;
|
|
32
|
+
readonly direct: number;
|
|
33
|
+
readonly indirect: number;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const PACKAGE_PREFIX = "/v3/systems/npm/packages/";
|
|
37
|
+
const DEPENDENTS_PREFIX = "/v3alpha/systems/npm/packages/";
|
|
38
|
+
|
|
39
|
+
/** This function gives the request for the versions of a package. */
|
|
40
|
+
export function depsDevPackageRequest(name: string): MeasureRequest {
|
|
41
|
+
assertExistingPackageName(name);
|
|
42
|
+
return { url: `https://api.deps.dev${PACKAGE_PREFIX}${encodeURIComponent(name)}`, accept: ACCEPT_JSON };
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** This function gives the request for the dependents of one version of a package. */
|
|
46
|
+
export function depsDevDependentsRequest(name: string, version: string): MeasureRequest {
|
|
47
|
+
assertExistingPackageName(name);
|
|
48
|
+
if (version.trim() === "") throw new Error("The version of a dependents request is empty.");
|
|
49
|
+
return { url: `https://api.deps.dev${DEPENDENTS_PREFIX}${encodeURIComponent(name)}/versions/${encodeURIComponent(version)}:dependents`, accept: ACCEPT_JSON };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** This function reads the response of `depsDevPackageRequest`. A package that deps.dev does not know is in `missing`. */
|
|
53
|
+
export function extractDepsDevPackage(text: string, url: string): Extracted<DepsDevPackage> {
|
|
54
|
+
const [name] = pathAfter(url, PACKAGE_PREFIX);
|
|
55
|
+
if (name === undefined) throw new Error(`${url} names no package.`);
|
|
56
|
+
if (text.trim() === "package not found") return { rows: [], missing: [name] };
|
|
57
|
+
const body = parseJsonOrUndefined(text);
|
|
58
|
+
const versions = isPlainObject(body) ? body["versions"] : undefined;
|
|
59
|
+
if (!Array.isArray(versions)) throw shapeError(url, "a deps.dev package", text);
|
|
60
|
+
const rows: DepsDevVersion[] = versions.map((entry: unknown) => {
|
|
61
|
+
const key = isPlainObject(entry) ? entry["versionKey"] : undefined;
|
|
62
|
+
const version = isPlainObject(key) ? key["version"] : undefined;
|
|
63
|
+
if (!isPlainObject(entry) || typeof version !== "string") throw shapeError(url, "a deps.dev package", text);
|
|
64
|
+
const published = entry["publishedAt"];
|
|
65
|
+
const reason = entry["deprecatedReason"];
|
|
66
|
+
return {
|
|
67
|
+
version,
|
|
68
|
+
publishedAt: typeof published === "string" && published !== "" ? published : null,
|
|
69
|
+
isDefault: entry["isDefault"] === true,
|
|
70
|
+
deprecated: entry["isDeprecated"] === true ? (typeof reason === "string" ? reason : "") : null,
|
|
71
|
+
};
|
|
72
|
+
});
|
|
73
|
+
const found = rows.find((v) => v.isDefault);
|
|
74
|
+
return { rows: [{ package: name, defaultVersion: found === undefined ? null : found.version, versions: rows }], missing: [] };
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* This function reads the response of `depsDevDependentsRequest`. A version that deps.dev does not know is in
|
|
79
|
+
* `missing`, as `name@version`.
|
|
80
|
+
*/
|
|
81
|
+
export function extractDepsDevDependents(text: string, url: string): Extracted<DepsDevDependents> {
|
|
82
|
+
const [name, , last] = pathAfter(url, DEPENDENTS_PREFIX);
|
|
83
|
+
const version = last?.replace(/:dependents$/, "");
|
|
84
|
+
if (name === undefined || version === undefined) throw new Error(`${url} names no package version.`);
|
|
85
|
+
if (text.trim() === "dependents not found") return { rows: [], missing: [`${name}@${version}`] };
|
|
86
|
+
const body = parseJsonOrUndefined(text);
|
|
87
|
+
if (!isPlainObject(body)) throw shapeError(url, "a deps.dev count of dependents", text);
|
|
88
|
+
const count = (field: string): number => {
|
|
89
|
+
// The proto3 JSON of deps.dev leaves out each field with the value 0. Thus a missing count is 0, not an error.
|
|
90
|
+
const value = body[field] ?? 0;
|
|
91
|
+
if (!Number.isInteger(value)) throw shapeError(url, "a deps.dev count of dependents", text);
|
|
92
|
+
return value as number;
|
|
93
|
+
};
|
|
94
|
+
return {
|
|
95
|
+
rows: [{ package: name, version, dependents: count("dependentCount"), direct: count("directDependentCount"), indirect: count("indirectDependentCount") }],
|
|
96
|
+
missing: [],
|
|
97
|
+
};
|
|
98
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The downloads of a package from the npm downloads API (`api.npmjs.org`).
|
|
3
|
+
*
|
|
4
|
+
* The count includes each install that comes through another package, so it includes transitive use. A bulk request
|
|
5
|
+
* gives the counts of up to 128 packages without a scope. A package with a scope needs a request of its own.
|
|
6
|
+
*/
|
|
7
|
+
import { isPlainObject, parseJsonOrUndefined } from "@procedures/json";
|
|
8
|
+
import { ACCEPT_JSON, type Extracted, type MeasureRequest, assertExistingPackageName, pathAfter, shapeError } from "./shared.js";
|
|
9
|
+
|
|
10
|
+
/** The most packages of one bulk request of the npm downloads API. */
|
|
11
|
+
export const DOWNLOADS_BULK_LIMIT = 128;
|
|
12
|
+
|
|
13
|
+
/** A period of the npm downloads API: a named period, or two days `YYYY-MM-DD:YYYY-MM-DD`. */
|
|
14
|
+
export type DownloadsPeriod = "last-day" | "last-week" | "last-month" | `${string}:${string}`;
|
|
15
|
+
|
|
16
|
+
/** The downloads of one package in one period. The days are the first and the last day of the period. */
|
|
17
|
+
export interface DownloadsRow {
|
|
18
|
+
readonly package: string;
|
|
19
|
+
readonly start: string;
|
|
20
|
+
readonly end: string;
|
|
21
|
+
readonly downloads: number;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const PREFIX = "/downloads/point/";
|
|
25
|
+
const PERIOD = /^(?:last-day|last-week|last-month|\d{4}-\d{2}-\d{2}:\d{4}-\d{2}-\d{2})$/;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* This function gives the requests for the downloads of the packages in one period. Each package goes into one
|
|
29
|
+
* request: the packages without a scope go into bulk requests, and each package with a scope gets its own request.
|
|
30
|
+
*/
|
|
31
|
+
export function downloadsRequests(packages: readonly string[], period: DownloadsPeriod = "last-week"): MeasureRequest[] {
|
|
32
|
+
if (!PERIOD.test(period)) throw new Error(`"${period}" is not a period of the npm downloads API.`);
|
|
33
|
+
const unique = [...new Set(packages)];
|
|
34
|
+
unique.forEach(assertExistingPackageName);
|
|
35
|
+
const base = `https://api.npmjs.org${PREFIX}${period}/`;
|
|
36
|
+
const plain = unique.filter((name) => !name.startsWith("@"));
|
|
37
|
+
const requests: MeasureRequest[] = [];
|
|
38
|
+
for (let i = 0; i < plain.length; i += DOWNLOADS_BULK_LIMIT) {
|
|
39
|
+
requests.push({ url: base + plain.slice(i, i + DOWNLOADS_BULK_LIMIT).join(","), accept: ACCEPT_JSON });
|
|
40
|
+
}
|
|
41
|
+
for (const name of unique.filter((n) => n.startsWith("@"))) requests.push({ url: base + name, accept: ACCEPT_JSON });
|
|
42
|
+
return requests;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function row(url: string, value: unknown, text: string): DownloadsRow {
|
|
46
|
+
if (!isPlainObject(value) || typeof value["package"] !== "string" || typeof value["start"] !== "string" || typeof value["end"] !== "string" || !Number.isInteger(value["downloads"])) {
|
|
47
|
+
throw shapeError(url, "a count of downloads", text);
|
|
48
|
+
}
|
|
49
|
+
return { package: value["package"], start: value["start"], end: value["end"], downloads: value["downloads"] as number };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* This function reads the response of a request of `downloadsRequests`. A package that the registry does not know is
|
|
54
|
+
* in `missing`. An error of another kind, for example a rate limit, throws an error.
|
|
55
|
+
*/
|
|
56
|
+
export function extractDownloads(text: string, url: string): Extracted<DownloadsRow> {
|
|
57
|
+
const [, ...rest] = pathAfter(url, PREFIX);
|
|
58
|
+
const joined = rest.join("/");
|
|
59
|
+
const names = joined.startsWith("@") ? [joined] : joined.split(",");
|
|
60
|
+
const body = parseJsonOrUndefined(text);
|
|
61
|
+
if (!isPlainObject(body)) throw shapeError(url, "a JSON object", text);
|
|
62
|
+
const error = body["error"];
|
|
63
|
+
if (typeof error === "string") {
|
|
64
|
+
if (names.length === 1 && /not found/i.test(error)) return { rows: [], missing: names };
|
|
65
|
+
throw new Error(`${url} answered the error "${error}".`);
|
|
66
|
+
}
|
|
67
|
+
if ("downloads" in body) return { rows: [row(url, body, text)], missing: [] };
|
|
68
|
+
const rows: DownloadsRow[] = [];
|
|
69
|
+
const missing: string[] = [];
|
|
70
|
+
for (const name of names) {
|
|
71
|
+
const value = body[name];
|
|
72
|
+
if (value === null || value === undefined) missing.push(name);
|
|
73
|
+
else rows.push(row(url, value, text));
|
|
74
|
+
}
|
|
75
|
+
return { rows, missing };
|
|
76
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The use measures of npm packages, as pure pairs of a request builder and an extractor. This entry has no Node
|
|
3
|
+
* built-in and registers nothing, so a browser can also use it.
|
|
4
|
+
*/
|
|
5
|
+
export { type Extracted, type Extractor, type MeasureFetch, type MeasureRequest, assertExistingPackageName, measure } from "./shared.js";
|
|
6
|
+
export * from "./downloads.js";
|
|
7
|
+
export * from "./deps-dev.js";
|
|
8
|
+
export * from "./jsdelivr.js";
|
|
9
|
+
export * from "./registry.js";
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The hits of a package on the jsDelivr CDN, from the jsDelivr data API (`data.jsdelivr.com`).
|
|
3
|
+
*
|
|
4
|
+
* A hit is one request of a file of the package to the CDN. Many pages load a browser client from a CDN, not from a
|
|
5
|
+
* bundle. The hits measure this use, and the npm downloads do not count it.
|
|
6
|
+
*/
|
|
7
|
+
import { isPlainObject, parseJsonOrUndefined } from "@procedures/json";
|
|
8
|
+
import { ACCEPT_JSON, type Extracted, type MeasureRequest, assertExistingPackageName, pathAfter, shapeError } from "./shared.js";
|
|
9
|
+
|
|
10
|
+
/** A period of the jsDelivr data API. Each period ends on the last full day. */
|
|
11
|
+
export type JsdelivrPeriod = "day" | "week" | "month" | "quarter" | "year";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The hits of one package in one period. `rank` is the place of the package among all packages of jsDelivr, and
|
|
15
|
+
* `typeRank` is its place among the npm packages. Each rank is `null` when the package has no hits.
|
|
16
|
+
*/
|
|
17
|
+
export interface JsdelivrHits {
|
|
18
|
+
readonly package: string;
|
|
19
|
+
readonly period: JsdelivrPeriod;
|
|
20
|
+
readonly start: string | null;
|
|
21
|
+
readonly end: string | null;
|
|
22
|
+
readonly hits: number;
|
|
23
|
+
readonly rank: number | null;
|
|
24
|
+
readonly typeRank: number | null;
|
|
25
|
+
/** The hits of the period before this period. */
|
|
26
|
+
readonly previousHits: number | null;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const PREFIX = "/v1/stats/packages/npm/";
|
|
30
|
+
const PERIODS: readonly JsdelivrPeriod[] = ["day", "week", "month", "quarter", "year"];
|
|
31
|
+
|
|
32
|
+
/** This function gives the request for the hits of a package in one period. */
|
|
33
|
+
export function jsdelivrHitsRequest(name: string, period: JsdelivrPeriod = "month"): MeasureRequest {
|
|
34
|
+
assertExistingPackageName(name);
|
|
35
|
+
if (!PERIODS.includes(period)) throw new Error(`"${period}" is not a period of the jsDelivr data API.`);
|
|
36
|
+
return { url: `https://data.jsdelivr.com${PREFIX}${name}?period=${period}`, accept: ACCEPT_JSON };
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const rankOf = (value: unknown): number | null => (Number.isInteger(value) ? (value as number) : null);
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* This function reads the response of `jsdelivrHitsRequest`. jsDelivr gives 0 hits and no rank for a package that it
|
|
43
|
+
* does not know. Thus `missing` is always empty.
|
|
44
|
+
*/
|
|
45
|
+
export function extractJsdelivrHits(text: string, url: string): Extracted<JsdelivrHits> {
|
|
46
|
+
const name = pathAfter(url, PREFIX).join("/");
|
|
47
|
+
const period = new URL(url).searchParams.get("period") as JsdelivrPeriod | null;
|
|
48
|
+
if (period === null || !PERIODS.includes(period)) throw new Error(`${url} names no period of the jsDelivr data API.`);
|
|
49
|
+
const body = parseJsonOrUndefined(text);
|
|
50
|
+
const hits = isPlainObject(body) ? body["hits"] : undefined;
|
|
51
|
+
if (!isPlainObject(hits) || !Number.isInteger(hits["total"])) throw shapeError(url, "jsDelivr hits", text);
|
|
52
|
+
const days = isPlainObject(hits["dates"]) ? Object.keys(hits["dates"]).sort() : [];
|
|
53
|
+
const previous = hits["prev"];
|
|
54
|
+
return {
|
|
55
|
+
rows: [
|
|
56
|
+
{
|
|
57
|
+
package: name,
|
|
58
|
+
period,
|
|
59
|
+
start: days[0] ?? null,
|
|
60
|
+
end: days.at(-1) ?? null,
|
|
61
|
+
hits: hits["total"] as number,
|
|
62
|
+
rank: rankOf(hits["rank"]),
|
|
63
|
+
typeRank: rankOf(hits["typeRank"]),
|
|
64
|
+
previousHits: isPlainObject(previous) && Number.isInteger(previous["total"]) ? (previous["total"] as number) : null,
|
|
65
|
+
},
|
|
66
|
+
],
|
|
67
|
+
missing: [],
|
|
68
|
+
};
|
|
69
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The facts of a package from the npm registry (`registry.npmjs.org`), as plain HTTP reads. The procedures
|
|
3
|
+
* `npm.view` and `npm.distTags.list` read the same data through the npm CLI.
|
|
4
|
+
*
|
|
5
|
+
* - The version document of a tag gives the version, its deprecation, its repository and its license.
|
|
6
|
+
* - The abbreviated document gives the tags, the count of versions and the time of the last change.
|
|
7
|
+
*/
|
|
8
|
+
import { isPlainObject, parseJsonOrUndefined } from "@procedures/json";
|
|
9
|
+
import { type Extracted, type MeasureRequest, nameSegment, pathAfter, shapeError } from "./shared.js";
|
|
10
|
+
|
|
11
|
+
/** The `Accept` header of the abbreviated document of a package. */
|
|
12
|
+
export const ACCEPT_ABBREVIATED = "application/vnd.npm.install-v1+json";
|
|
13
|
+
|
|
14
|
+
/** The version that a tag of a package names, with the facts of that version. */
|
|
15
|
+
export interface RegistryVersion {
|
|
16
|
+
readonly package: string;
|
|
17
|
+
readonly version: string;
|
|
18
|
+
/** The message of the deprecation, or `null` when the version is not deprecated. */
|
|
19
|
+
readonly deprecated: string | null;
|
|
20
|
+
/** The URL of the repository as the package states it, or `null`. The function does not change its form. */
|
|
21
|
+
readonly repository: string | null;
|
|
22
|
+
/** The folder of the package in its repository, for a package of a monorepo. */
|
|
23
|
+
readonly repositoryDirectory: string | null;
|
|
24
|
+
readonly license: string | null;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** The facts of the abbreviated document of a package. */
|
|
28
|
+
export interface RegistryDocument {
|
|
29
|
+
readonly package: string;
|
|
30
|
+
/**
|
|
31
|
+
* The time of the last change of the document. A release changes it. A deprecation or a change of a tag changes it
|
|
32
|
+
* too, so it is not the time of the last release.
|
|
33
|
+
*/
|
|
34
|
+
readonly modified: string | null;
|
|
35
|
+
readonly distTags: Readonly<Record<string, string>>;
|
|
36
|
+
readonly versions: number;
|
|
37
|
+
/** The message of the deprecation of the version of the tag `latest`, or `null`. */
|
|
38
|
+
readonly latestDeprecated: string | null;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** This function gives the request for the version document of a tag of a package. */
|
|
42
|
+
export function registryVersionRequest(name: string, tag = "latest"): MeasureRequest {
|
|
43
|
+
if (tag.trim() === "") throw new Error("The tag of a version request is empty.");
|
|
44
|
+
return { url: `https://registry.npmjs.org/${nameSegment(name)}/${encodeURIComponent(tag)}`, accept: "application/json" };
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** This function gives the request for the abbreviated document of a package. */
|
|
48
|
+
export function registryDocumentRequest(name: string): MeasureRequest {
|
|
49
|
+
return { url: `https://registry.npmjs.org/${nameSegment(name)}`, accept: ACCEPT_ABBREVIATED };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** This function gives the name of a package from the path segments of a registry URL. */
|
|
53
|
+
function nameOf(segments: readonly string[]): { name: string; rest: readonly string[] } {
|
|
54
|
+
const [first, ...rest] = segments.flatMap((segment) => segment.split("/"));
|
|
55
|
+
if (first === undefined) throw new Error("The URL names no package.");
|
|
56
|
+
if (first.startsWith("@")) {
|
|
57
|
+
const [second, ...more] = rest;
|
|
58
|
+
if (second === undefined) throw new Error(`The URL names the scope ${first} and no package.`);
|
|
59
|
+
return { name: `${first}/${second}`, rest: more };
|
|
60
|
+
}
|
|
61
|
+
return { name: first, rest };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const textOf = (value: unknown): string | null => (typeof value === "string" && value !== "" ? value : null);
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* This function reads the response of `registryVersionRequest`. A package or a tag that the registry does not know is
|
|
68
|
+
* in `missing`.
|
|
69
|
+
*/
|
|
70
|
+
export function extractRegistryVersion(text: string, url: string): Extracted<RegistryVersion> {
|
|
71
|
+
const { name } = nameOf(pathAfter(url, "/"));
|
|
72
|
+
const json = parseJsonOrUndefined(text);
|
|
73
|
+
if (typeof json === "string" && /not found/i.test(json)) return { rows: [], missing: [name] };
|
|
74
|
+
if (!isPlainObject(json) || typeof json["version"] !== "string") throw shapeError(url, "an npm version document", text);
|
|
75
|
+
const repo = json["repository"];
|
|
76
|
+
const license = json["license"];
|
|
77
|
+
return {
|
|
78
|
+
rows: [
|
|
79
|
+
{
|
|
80
|
+
package: name,
|
|
81
|
+
version: json["version"],
|
|
82
|
+
deprecated: typeof json["deprecated"] === "string" ? json["deprecated"] : null,
|
|
83
|
+
repository: isPlainObject(repo) ? textOf(repo["url"]) : textOf(repo),
|
|
84
|
+
repositoryDirectory: isPlainObject(repo) ? textOf(repo["directory"]) : null,
|
|
85
|
+
license: isPlainObject(license) ? textOf(license["type"]) : textOf(license),
|
|
86
|
+
},
|
|
87
|
+
],
|
|
88
|
+
missing: [],
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** This function reads the response of `registryDocumentRequest`. A package that the registry does not know is in `missing`. */
|
|
93
|
+
export function extractRegistryDocument(text: string, url: string): Extracted<RegistryDocument> {
|
|
94
|
+
const { name } = nameOf(pathAfter(url, "/"));
|
|
95
|
+
const json = parseJsonOrUndefined(text);
|
|
96
|
+
if (isPlainObject(json) && typeof json["error"] === "string" && /not found/i.test(json["error"])) return { rows: [], missing: [name] };
|
|
97
|
+
if (!isPlainObject(json) || !isPlainObject(json["versions"]) || !isPlainObject(json["dist-tags"])) throw shapeError(url, "an abbreviated npm document", text);
|
|
98
|
+
const tags: Record<string, string> = {};
|
|
99
|
+
for (const [tag, version] of Object.entries(json["dist-tags"])) if (typeof version === "string") tags[tag] = version;
|
|
100
|
+
const latest = tags["latest"] === undefined ? undefined : json["versions"][tags["latest"]];
|
|
101
|
+
return {
|
|
102
|
+
rows: [
|
|
103
|
+
{
|
|
104
|
+
package: name,
|
|
105
|
+
modified: textOf(json["modified"]),
|
|
106
|
+
distTags: tags,
|
|
107
|
+
versions: Object.keys(json["versions"]).length,
|
|
108
|
+
latestDeprecated: isPlainObject(latest) && typeof latest["deprecated"] === "string" ? latest["deprecated"] : null,
|
|
109
|
+
},
|
|
110
|
+
],
|
|
111
|
+
missing: [],
|
|
112
|
+
};
|
|
113
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The parts that each measure shares: the request, the result of an extractor, the fetch and the runner.
|
|
3
|
+
*
|
|
4
|
+
* A measure is a pure pair. Its request builder gives the URL and the `Accept` header, and its extractor reads the body
|
|
5
|
+
* text of the response into typed rows. Neither function sends a request. The caller gives the fetch, so the same pair
|
|
6
|
+
* works with `globalThis.fetch` and with the recipes of a crawler.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* One request of a measure. Its fields fit the `RecipeRequest` of `polite-crawler` and the `init` of `fetch`.
|
|
11
|
+
*/
|
|
12
|
+
export interface MeasureRequest {
|
|
13
|
+
readonly url: string;
|
|
14
|
+
readonly accept: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The result of an extractor. `rows` holds the values that the response gives. `missing` holds each package that the
|
|
19
|
+
* source does not know.
|
|
20
|
+
*/
|
|
21
|
+
export interface Extracted<Row> {
|
|
22
|
+
readonly rows: readonly Row[];
|
|
23
|
+
readonly missing: readonly string[];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* An extractor reads the body text of one response. The URL of the request tells which packages the body answers for.
|
|
28
|
+
* An extractor throws an error that names the URL when the body has a shape that it does not know.
|
|
29
|
+
*/
|
|
30
|
+
export type Extractor<Row> = (text: string, url: string) => Extracted<Row>;
|
|
31
|
+
|
|
32
|
+
/** A fetch that gives the body text of a response. `globalThis.fetch` fits this type. */
|
|
33
|
+
export type MeasureFetch = (
|
|
34
|
+
url: string,
|
|
35
|
+
init: { readonly headers: Readonly<Record<string, string>> },
|
|
36
|
+
) => Promise<{ readonly text: () => Promise<string> }>;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* This function sends one request of a measure through the given fetch and reads the response with the extractor. It
|
|
40
|
+
* does not wait between requests. The caller keeps the pace of each source.
|
|
41
|
+
*/
|
|
42
|
+
export async function measure<Row>(fetch: MeasureFetch, request: MeasureRequest, extract: Extractor<Row>): Promise<Extracted<Row>> {
|
|
43
|
+
const response = await fetch(request.url, { headers: { accept: request.accept } });
|
|
44
|
+
return extract(await response.text(), request.url);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** The `Accept` header of a JSON API. */
|
|
48
|
+
export const ACCEPT_JSON = "application/json";
|
|
49
|
+
|
|
50
|
+
/** The names that npm refuses for all packages, also for old packages. */
|
|
51
|
+
const EXCLUDED = new Set(["node_modules", "favicon.ico"]);
|
|
52
|
+
|
|
53
|
+
const SCOPED = /^@([^/]+)\/([^/]+)$/;
|
|
54
|
+
|
|
55
|
+
const urlSafe = (part: string): boolean => part !== "" && encodeURIComponent(part) === part;
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* This function throws an error when the text cannot be the name of a package on npm. It applies the rules of npm for
|
|
59
|
+
* existing packages, because old packages keep names that a new package cannot have. Thus capital letters and the
|
|
60
|
+
* characters `~'!()*` are valid, for example `JSONStream`. Each part of the name is safe in a URL.
|
|
61
|
+
*/
|
|
62
|
+
export function assertExistingPackageName(name: string): void {
|
|
63
|
+
const scoped = SCOPED.exec(name);
|
|
64
|
+
const parts = scoped === null ? [name] : scoped.slice(1);
|
|
65
|
+
const valid =
|
|
66
|
+
name.length <= 214 &&
|
|
67
|
+
!name.startsWith(".") &&
|
|
68
|
+
!name.startsWith("_") &&
|
|
69
|
+
!EXCLUDED.has(name.toLowerCase()) &&
|
|
70
|
+
parts.every(urlSafe) &&
|
|
71
|
+
// The name of a scoped package does not start with a period. Only a scoped name has a valid slash.
|
|
72
|
+
!name.includes("/.");
|
|
73
|
+
if (!valid) throw new Error(`"${name}" is not a valid npm package name.`);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** This function gives the name of a package as one segment of a URL path: `@scope/name` becomes `@scope%2Fname`. */
|
|
77
|
+
export function nameSegment(name: string): string {
|
|
78
|
+
assertExistingPackageName(name);
|
|
79
|
+
return name.replaceAll("/", "%2F");
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** This function gives the error of a body with a shape that the extractor does not know. */
|
|
83
|
+
export function shapeError(url: string, what: string, text: string): Error {
|
|
84
|
+
const start = text.length > 200 ? `${text.slice(0, 200)}...` : text;
|
|
85
|
+
return new Error(`${url} answered a body that is not ${what}: ${start}`);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** This function gives the path segments of a URL after a prefix, with each segment decoded. */
|
|
89
|
+
export function pathAfter(url: string, prefix: string): string[] {
|
|
90
|
+
const path = new URL(url).pathname;
|
|
91
|
+
if (!path.startsWith(prefix)) throw new Error(`${url} is not a URL of the form ${prefix}...`);
|
|
92
|
+
return path
|
|
93
|
+
.slice(prefix.length)
|
|
94
|
+
.split("/")
|
|
95
|
+
.filter((segment) => segment !== "")
|
|
96
|
+
.map((segment) => decodeURIComponent(segment));
|
|
97
|
+
}
|