Docs Packages
@procedures/npm
npm install @procedures/npm
The npm registry as procedures. The package reads the fields and the dist-tags of a package, and gives the user name of the login. It also lists the files of a tarball, and deprecates versions.
Each procedure starts the npm CLI through shell.run of @procedures/shell. It gives an argument list to npm, and no shell reads it. The options come first, then --, then the values. After --, the CLI reads no option, so a value that starts with - stays a value.
The procedures#
| Procedure | Input | Output | Effects |
|---|---|---|---|
npm.view | package, fields, registry | data: the JSON of npm view --json | reads fs and network |
npm.distTags.list | package, registry | tags: each tag and its version | reads fs and network |
npm.distTags.add | package, version, tag, confirm, otp, registry | the result of npm | writes network, destructive, idempotent |
npm.distTags.remove | package, tag, confirm, otp, registry | the result of npm | writes network, destructive, idempotent |
npm.whoami | registry | username | reads fs and network |
npm.pack | dryRun (default true), destination | data: the JSON of npm pack --json | writes fs and process |
npm.deprecate | package, message, confirm, otp, registry | the result of npm | writes network, not destructive, idempotent |
- Each input also takes
cwdandtimeout. - The result of npm has
exitCode,stdout,stderr,successandduration. A failed command does not throw:successisfalse. - The npm CLI reads its configuration on the disk (
.npmrc), so the reads of the registry also readfs.
The writes to the registry#
npm.distTags.add, npm.distTags.remove and npm.deprecate change the registry for each user of the package. Their input must have confirm: true, or the call fails before npm starts.
npm.distTags.addmoves a tag from its earlier version. Each install of<package>@<tag>then gets the new version.npm.deprecatekeeps the versions and their files. Only the message changes, so it is not destructive. An empty message removes the deprecation.- An account with two-factor authentication needs
otp, the one-time password.
import { createRegistry, invokePath } from "@procedures/client";
import { npmModule } from "@procedures/npm";
const registry = createRegistry(npmModule);
const view = await invokePath(["npm", "view"], { package: "zod", fields: ["version"] }, { registry });
await invokePath(
["npm", "deprecate"],
{ package: "my-package@<2.0.0", message: "Use version 2", confirm: true },
{ registry },
);
The tarball#
npm.pack is a dry run by default: the CLI lists the files of the tarball and writes no tarball. With dryRun: false, the CLI writes the tarball into destination, or into the folder of the package. The CLI also starts the prepack, prepare and postpack scripts of the package, in a dry run too.
mark npm pack --cwd packages/app
The use measures#
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.
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.
| Measure | Request builder | Extractor | Row |
|---|---|---|---|
| npm downloads | downloadsRequests(packages, period) | extractDownloads | DownloadsRow |
| deps.dev versions | depsDevPackageRequest(name) | extractDepsDevPackage | DepsDevPackage |
| deps.dev dependents | depsDevDependentsRequest(name, version) | extractDepsDevDependents | DepsDevDependents |
| jsDelivr hits | jsdelivrHitsRequest(name, period) | extractJsdelivrHits | JsdelivrHits |
| The version of a tag | registryVersionRequest(name, tag) | extractRegistryVersion | RegistryVersion |
| The abbreviated document | registryDocumentRequest(name) | extractRegistryDocument | RegistryDocument |
- The npm downloads include the installs through other packages. The deps.dev dependents include the indirect dependents. Thus both measures include transitive use.
downloadsRequestsputs up to 128 packages without a scope into one bulk request. Each package with a scope gets its own request.- The request builders accept the name of each existing package. Some old packages have names that a new package cannot have, for example
JSONStream.assertExistingPackageNameapplies the rules of npm for these names. - 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. - The dependents need a version.
extractDepsDevPackagegives the default version.
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.
import { downloadsRequests, extractDownloads, measure } from "@procedures/npm/measures";
for (const request of downloadsRequests(["sockette", "@stomp/stompjs"], "last-week")) {
const { rows, missing } = await measure(globalThis.fetch, request, extractDownloads);
}
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.
How the package finds npm#
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.
Related packages#
@procedures/shell: the shell module that runs npm.@procedures/pnpm: pnpm as procedures.@procedures/client: the core.