UNPKG

query-registry

Version:

Query the npm registry for packuments, manifests, packages and download counts

160 lines (150 loc) • 6.12 kB
import urlJoin from "url-join"; import * as z from "zod"; import { PackageJson } from "zod-package-json"; import { assertValidPackageName } from "./assert-valid-package-name.js"; import { fetchData } from "./fetch-data.js"; import { npmRegistryUrl } from "./npm-registry.js"; import { Repository } from "./repository.js"; /** `Dist` describes the distribution metadata generated by the registry. @see {@link https://github.com/npm/registry/blob/master/docs/responses/package-metadata.md#dist} */ const Dist = z.object({ /** Tarball URL. */ tarball: z.string(), /** SHA1 sum of the tarball. */ shasum: z.string(), /** String in the format `<hashAlgorithm>-<base64-hash>`. */ integrity: z.string().optional(), /** Number of files in the tarball. */ fileCount: z.number().optional(), /** Total unpacked size in bytes of the files in the tarball. */ unpackedSize: z.number().optional(), /** PGP signature in the format `<package>@<version>:<integrity>`. @deprecated {@link https://docs.npmjs.com/about-registry-signatures#migrating-from-pgp-to-ecdsa-signatures} */ "npm-signature": z.string().optional(), /** ECDSA registry signatures. @see {@link https://docs.npmjs.com/about-registry-signatures} */ signatures: z .array(z.object({ keyid: z.string(), sig: z.string(), })) .optional(), }); export const PackageManifest = z.object({ ...PackageJson.shape, // // Known properties. // /** Package version ID in the format `<name>@<version>` (e.g., `foo@1.0.0`). */ _id: z.string(), /** Distribution metadata generated by the registry. */ dist: Dist, /** Text extracted from the README file. */ readme: z.string().optional(), /** Name of the README file. */ readmeFilename: z.string().optional(), /** Commit corresponding to the published package version. */ gitHead: z.string().optional(), /** True if the package contains a shrinkwrap file. */ _hasShrinkwrap: z.boolean().optional(), /** Node.js version used to publish the package. */ _nodeVersion: z.string().optional(), /** npm CLI version used to publish the package. */ _npmVersion: z.string().optional(), /** npm user who published the specific version of the package. */ _npmUser: PackageJson.shape.author.optional(), /** Internal npm registry data. */ _npmOperationalInternal: z .object({ host: z.string().optional(), tmp: z.string().optional(), }) .optional(), // // Quirks. // /** Runtime systems supported by the package. @remarks In some old packages (like `lodash@0.1.0`) the `engines` property is an array of strings instead of an object. In this case the array is discarded and `engines` becomes `undefined`. */ engines: PackageJson.shape.engines.catch(undefined), /** SPDX license expression or a custom license. @remarks In some old packages (like `eslint@0.0.6`) the `license` property is an object. In this case the object is discarded and `license` becomes `undefined`. */ license: PackageJson.shape.license.catch(undefined), /** URL of the package's homepage. @remarks In some old packages (like `fs-extra@0.0.1`) the `homepage` property is an array of strings. In this case the array is discarded and `homepage` becomes `undefined`. */ homepage: PackageJson.shape.homepage.catch(undefined), /** Deprecation status/message. @remarks In some packages (like `react@16.14.0`) the `deprecated` property is a boolean instead of a deprecation message. In this case the boolean is discarded and `deprecated` becomes `undefined`. */ deprecated: PackageJson.shape.deprecated.catch(undefined), /** Type for all the `.js` files in the package, usually `module`. @remarks In some old packages (like `mongoose@0.0.1`) the `type` property value is a string different from `module` or `commonjs`. In this case the string is discarded and `type` becomes `undefined`. */ type: PackageJson.shape.type.catch(undefined), /** Repository for the package's source code. @remarks In some packages (like `tinacms@3.15.2`) the `type` property of the repository object is missing. */ repository: Repository, /** File patterns for locating local workspaces. @remarks In some old packages (like `bower@1.8.9`) the `workspaces` property is defined as an object which was supported by old Yarn versions. In this case the object is discarded and `workspaces` becomes `undefined`. */ workspaces: PackageJson.shape.workspaces.catch(undefined), /** Configuration read by package managers when publishing a package. @see {@link https://docs.npmjs.com/cli/v12/using-npm/config#config-settings} @see {@link https://pnpm.io/package_json#publishconfig} @see {@link https://bun.com/docs/pm/cli/publish} @see {@link https://yarnpkg.com/configuration/manifest#publishConfig} */ publishConfig: z.optional(z.looseObject({ ...PackageJson.shape.publishConfig.unwrap().shape, // Since the `JsonStrip` test utility removes strings, // the literal check always fails (see package "inquire" in get-packument.test.ts). // Catch unknown literals like `""` and return `undefined`. access: PackageJson.shape.publishConfig.unwrap().shape.access.catch(undefined), })), }); /** `getPackageManifest` returns the manifest describing a specific version of a package (e.g., `foo@1.0.0`). @param name - package name @param versionOrTag - semver version number (e.g., `1.0.0`) or distribution tag (e.g., `latest`) (default: `latest`) @param registry - URL of the registry (default: npm registry) @see {@link PackageManifest} */ export async function getPackageManifest(name, versionOrTag = "latest", registry = npmRegistryUrl) { assertValidPackageName(name); return await fetchData(PackageManifest, urlJoin(registry, name, versionOrTag)); }