npm Registry Adapter
Relevant source files
The following files were used as context for generating this wiki page:
The npm Registry Adapter is a core component of the regxa library designed to interface with the npm registry (registry.npmjs.org). It implements the standard Registry contract, allowing the system to fetch package metadata, version histories, dependencies, and maintainer information in a normalized format.
As part of the Universal Registry Client, the adapter handles the complexities of the npm API—such as scoped package name encoding and varying metadata structures—and transforms them into shared core types used across the entire project.
Sources: src/registries/npm.ts:1-12, src/registries/AGENTS.md:1-10, AGENTS.md:5-10
Architecture and Integration
The npm adapter is implemented as the NpmRegistry class, which is registered into the global registry factory upon import. It relies on a centralized Client for HTTP communication, ensuring consistent retry logic and rate limiting across all registry interactions.
The diagram shows the relationship between the npm adapter, the core factory, and the shared HTTP client.
Sources: src/registries/npm.ts:257-261, src/core/registry.ts, AGENTS.md:49-55
Data Normalization
A primary responsibility of the adapter is mapping NpmPackageResponse and NpmVersion payloads into the core Package, Version, Dependency, and Maintainer types.
Package Metadata Extraction
When fetching package details via fetchPackage(name), the adapter extracts:
- Namespace: Parsed from scoped names (e.g.,
@babelfrom@babel/core). - Repository: Normalized using
normalizeRepositoryURL. - License: Extracted from either the top-level package data or the latest version's license field.
Sources: src/registries/npm.ts:88-120, test/unit/registries.test.ts:14-55
Version and Dependency Resolution
The adapter processes version history and dependencies through specialized endpoints. Since v0.1.7, the adapter fetches version-specific dependencies from a per-version endpoint to improve performance.
| Core Type | Implementation Logic | Source Fields |
|---|---|---|
| Version | Maps dist-tags and versions map. | version, dist.integrity, deprecated, time |
| Dependency | Categorizes into runtime, development, and optional. | dependencies, devDependencies, optionalDependencies, peerDependencies |
Sources: src/registries/npm.ts:133-156, src/registries/npm.ts:175-212, CHANGELOG.md:8-10
Maintainer Deduplication Logic
The adapter uses a specific strategy to identify maintainers, combining top-level registry maintainers with version-specific authors and contributors while avoiding duplicates.
The sequence diagram illustrates the supplemental data gathering used to build a complete maintainer list.
Sources: src/registries/npm.ts:221-267, test/unit/registries.test.ts:101-139
URL Generation
The urls() method provides a URLBuilder that generates canonical links for various resources.
// Example URL Building Logic
urls(): URLBuilder {
return {
registry: (name: string, version?: string) => {
const base = `https://www.npmjs.com/package/${name}`;
return version ? `${base}/v/${version}` : base;
},
download: (name: string, version: string) => {
const encodedName = this.encodeName(name);
const tarballName = name.includes("/") ? name.split("/")[1] : name;
return `https://registry.npmjs.org/${encodedName}/-/${tarballName}-${version}.tgz`;
},
purl: (name: string, version?: string) => {
const namespace = this.extractNamespace(name);
const bareName = namespace ? name.slice(namespace.length + 1) : name;
return buildPURL({ type: "npm", namespace, name: bareName, version });
},
// ... documentation and readme helpers
};
}
Sources: src/registries/npm.ts:281-306, test/unit/registries.test.ts:187-193
Technical Constraints
- Scoping: Scoped packages (starting with
@) require specific URI encoding where the/is replaced with%2Ffor API requests. - Integrity: The adapter prefers
dist.integrity(Subresource Integrity) but falls back todist.shasum(SHA-1) formatted assha1-{hash}if the former is missing. - Errors: HTTP 404 responses from the registry are caught and re-thrown as typed
NotFoundErrorobjects.
Sources: src/registries/npm.ts:117-119, src/registries/npm.ts:147-152, src/registries/npm.ts:320-325
The npm Registry Adapter serves as a reference implementation for other ecosystems in the regxa project due to its comprehensive handling of scoped names, complex dependency types, and maintainer metadata.