Docs Packages

@procedures/npm

packages/npm/README.md on GitHub · Package map · Procedures

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#

ProcedureInputOutputEffects
npm.viewpackage, fields, registrydata: the JSON of npm view --jsonreads fs and network
npm.distTags.listpackage, registrytags: each tag and its versionreads fs and network
npm.distTags.addpackage, version, tag, confirm, otp, registrythe result of npmwrites network, destructive, idempotent
npm.distTags.removepackage, tag, confirm, otp, registrythe result of npmwrites network, destructive, idempotent
npm.whoamiregistryusernamereads fs and network
npm.packdryRun (default true), destinationdata: the JSON of npm pack --jsonwrites fs and process
npm.deprecatepackage, message, confirm, otp, registrythe result of npmwrites network, not destructive, idempotent
  • Each input also takes cwd and timeout.
  • The result of npm has exitCode, stdout, stderr, success and duration. A failed command does not throw: success is false.
  • The npm CLI reads its configuration on the disk (.npmrc), so the reads of the registry also read fs.

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.add moves a tag from its earlier version. Each install of <package>@<tag> then gets the new version.
  • npm.deprecate keeps 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.

MeasureRequest builderExtractorRow
npm downloadsdownloadsRequests(packages, period)extractDownloadsDownloadsRow
deps.dev versionsdepsDevPackageRequest(name)extractDepsDevPackageDepsDevPackage
deps.dev dependentsdepsDevDependentsRequest(name, version)extractDepsDevDependentsDepsDevDependents
jsDelivr hitsjsdelivrHitsRequest(name, period)extractJsdelivrHitsJsdelivrHits
The version of a tagregistryVersionRequest(name, tag)extractRegistryVersionRegistryVersion
The abbreviated documentregistryDocumentRequest(name)extractRegistryDocumentRegistryDocument
  • The npm downloads include the installs through other packages. The deps.dev dependents include the indirect dependents. Thus both measures include transitive use.
  • downloadsRequests puts 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. assertExistingPackageName applies 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. extractDepsDevPackage gives 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.