npm

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 16, 2026 License: MIT Imports: 31 Imported by: 0

Documentation

Overview

Package npm is the format of npm repositories (decision 0034). It reads each tarball's package.json, indexes a tarball at its canonical path as a version of its package (decision 0018) with the per-version Metadata that packuments and search are built from, and keeps the npm.* properties. Every write reaches it through the item service, so a tarball deployed by npm publish, by jf npm publish's REST deploy, or landed by a copy, a move or a restore is indexed the same way.

A .tgz anywhere else, or one without a readable package.json, is stored but not indexed: every repository has strict layout (DV-041).

Role

artifacts module, ecosystem layer: the format rules of the npm slice (package type npm). Its Handler plugs into the item service through the format hooks (decision 0032); the registry protocol that serves what it keeps is the slice's http package (artifacts/ecosystem/npm/http).

Inputs

  • Driving: the format registry (artifacts/ecosystem), which hands the item service's hooks for npm repositories to Handler; the persistent work queue, which runs the strict-layout conversion (KindConvert, which the server's npm operations queue) and every format's reindex through Handler (decision 0033); and the registry protocol, which builds packuments from the npm.* properties and each version's Metadata (DecodeMetadata) by the latest rules (Settings, Candidate), and checks a publish with ReadStored, ValidName and CanonicalPath.
  • Driven: the format hooks' environment (format.Env), through which Handler reads stored tarballs, keeps the package index (artifacts/core/pkgindex), finds tarballs by AQL (artifacts/core/aql) and moves them to their canonical paths; and the generic deploy policy (artifacts/ecosystem/generic), which Handler embeds.
  • Settings: the server's npm options (decision 0034), which reach Handler through ecosystem.Settings.

Outputs

  • Handler: the hooks that inspect and admit a write (Admit adds the release and snapshot handling and npm.enforce_path to the generic policy), index a tarball, follow its dist-tags and deprecation (Annotated), settle latest after a delete, and rebuild the index or convert a repository to strict layout as queued work.
  • The npm.* properties (PropName to PropDeprecated) and each version's Metadata in the package index.
  • The rules the registry protocol shares: Tarball, ReadTarball and ReadStored; CanonicalPath, CleanVersion, ValidName and Keywords; and Settings, Candidate and Outranks, which decide latest.

Invariants

  • A dist-tag names one version of its package, latest included, and latest moves only by decision 0034's rules, whichever route changed the tarballs.
  • Only a tarball at its canonical path is indexed; KindConvert moves the others there, as 7.x's background conversion does.
  • A read waits for no queued work (Owed is empty): the deploys keep the index current, as 7.x's reads assume.
  • Statuses and messages a client sees stay byte-identical.

References

  • Decision 0034: docs/research/decisions/0034-npm.md ("Storage and properties", "Dist-tags and latest", "Strict layout, conversion and reindex").
  • Decision 0033: docs/research/decisions/0033-persistent-work-queue.md (the conversion and reindex as queued work).
  • Decision 0032: docs/research/decisions/0032-package-format-handlers.md (the format hooks); decision 0018: docs/research/decisions/0018-package-index.md.
  • docs/operations/npm-remote.md (the remote cache this format indexes).
  • JFrog npm repositories, as decision 0034 cites it: https://docs.jfrog.com/artifactory/docs/npm-repositories.
  • Target layout: docs/research/architecture-options/5-modular-monolith.md, line 227 (ecosystem/npm), "npm ecosystem" (lines 413 to 445), line 664 (virtual.go) and line 997 (the mapping row).
  • Plan: docs/implementation/plan.md#p7-npm-go-maven-locals, #p9-remote-repositories and #p9d-modular-monolith-migration, epic ECO (https://github.com/weaveplatform/artifactweave/issues/943).
  • https://github.com/weaveplatform/artifactweave/issues/1031

Status

Implemented P7 (npm local repositories), with remote repositories in P9. Moved here in P9d. Roadmap: virtual.go (P10), the Merger that merges packuments for virtual repositories.

Testing

  • handler_test.go, latest_test.go and overwrite_test.go: indexing, properties, dist-tags, deprecation and latest across deploys, overwrites, transfers and reindexes.
  • tarball_test.go and enforce_test.go: reading a tarball, names, versions and canonical paths, and npm.enforce_path.
  • work_test.go and fault_test.go: the queued reindex and conversion, store faults and corrupt metadata.
  • contract_test.go: the format contract suite (artifacts/core/formattest).

Index

Constants

View Source
const (
	PropName        = "npm.name"
	PropVersion     = "npm.version"
	PropDescription = "npm.description"
	// PropKeywords is one value: the keywords as a JSON array of strings,
	// which Artifactory's keyword search matches with *"<keyword>"*.
	PropKeywords = "npm.keywords"
	// PropDistTag has one value per dist-tag. A tag names one version of
	// its package: a tarball that gains one takes it from the others.
	PropDistTag = "npm.disttag"
	// PropDeprecated is the version's deprecation message.
	PropDeprecated = "npm.deprecated"
	// PropLicenses is package.json's license, which 7.x writes beside the
	// npm.* properties when it indexes a tarball (RTFACT-30834). It is not
	// in npm's namespace: other tools, Xray among them, write it too.
	PropLicenses = "artifactory.licenses"
)

Properties the format keeps on an indexed tarball. Name, version, description, keywords and license follow from package.json and are set by the format; dist-tags and deprecation are the state npm changes after a publish, and the index follows them.

View Source
const KindConvert = "npm.convert"

KindConvert is the npm format's work from the persistent work queue (decisions 0033 and 0034), keyed by repository key: it converts a repository to strict layout, moving each tarball off its canonical path to it, as 7.x's conversion does in the background (npm-repositories, "Enforce strict layout"). Its payload is a format.WorkRequest. The reindex, which 7.x schedules too, is every format's format.KindReindex.

View Source
const MaxVersionLength = 256

MaxVersionLength is the longest version string npm's semver parses.

View Source
const TagLatest = "latest"

TagLatest is the dist-tag npm installs when no version is asked for. 7.x stores it as it stores any dist-tag, as npm.disttag=latest on one tarball (7.84.10, RTDEV-37593 and RTDEV-40221; RTFACT-31340), so it moves only when one of decision 0034's rules moves it:

  1. npm.disttag=latest set on a tarball by any route makes its version latest (Annotated, through moveTags);
  2. a new version written without a dist-tag takes latest when it outranks the version that has it (claimLatest);
  3. a new version written with a dist-tag leaves latest where it is, unless no version has it yet (claimLatest; RTFACT-24072);
  4. a package left without latest, its version removed or the tag taken off, gives it to the version that outranks the others (settleLatest).

Which version outranks another is Settings.Outranks.

Variables

View Source
var ErrNotTarball = errors.New("npm: not a usable package tarball")

ErrNotTarball is content the format cannot index: no tar or tar.gz, no readable package.json in its top-level folder, a name npm would not accept, a version that is not SemVer, or more than limits allow. Such a .tgz is stored but not indexed (decision 0034, strict layout).

View Source
var ErrUnconverted = errors.New("npm: tarballs left off their canonical paths")

ErrUnconverted reports a conversion that left tarballs off their canonical paths.

Functions

func CanonicalPath

func CanonicalPath(name, version string) string

CanonicalPath is where a version's tarball is stored: the path npm names in its publish body and the JFrog CLI deploys to, so a scoped package's file name keeps its scope (@scope/name/-/@scope/name-1.0.0.tgz). The scope appears in both the directory and the tarball name.

func CleanVersion

func CleanVersion(v string) (string, bool)

CleanVersion is npm's semver.clean: surrounding space and any leading "=" and "v" dropped, then a SemVer version without its build metadata, which npm ignores, so 1.0.0+build and 1.0.0 are one version. ok is false when v is no SemVer version.

func Keywords

func Keywords(raw json.RawMessage) []string

Keywords is a package.json keywords field as indexing reads it into npm.keywords: one string split at commas, and entries that are no non-empty string dropped. npm search matches against these, so a keyword found by the property search is found by npm search too.

func Outranks

func Outranks(a, b string) bool

Outranks reports whether version a outranks b for latest by default: a version that is no pre-release outranks every pre-release, and between two of the same kind the higher SemVer wins, as a 7.x local repository ranks them (RTFACT-30783, RTFACT-17732). Both are versions as the index cleans them, without build metadata, so a hyphen can only start a pre-release.

func Serves

func Serves(t repo.PackageType) bool

Serves reports whether t is a package type of this format.

func ValidName

func ValidName(name string) bool

ValidName reports whether npm accepts name for a package that exists, as validate-npm-package-name's validForOldPackages does: a name, or @scope/name, whose parts need no URL encoding, that starts with neither "." nor "_", and is not one npm reserves. Rules npm adds for new names, such as lower case, are left to the client, so older packages index.

Types

type Candidate

type Candidate struct {
	// Version is the index's cleaned version.
	Version string
	// Published is when its tarball was last deployed (Metadata.Published).
	Published time.Time
}

Candidate is a version as the latest rules weigh it.

type Dist

type Dist struct {
	// Shasum is the stored SHA-1, in hex.
	Shasum string `json:"shasum"`
	// Integrity is the stored SHA-512 in Subresource Integrity form.
	Integrity string `json:"integrity"`
	// Signatures are the repository's signatures of the version, made when
	// it was indexed (SignerFor). A repository that signs nothing stores
	// none, so its Metadata keeps the bytes it had before signing existed.
	Signatures []Signature `json:"signatures,omitempty"`
}

Dist is a version's dist object, less the tarball URL, which a packument builds from the base URL of each request. 7.x's dist has nothing else (RTFACT-30472) but the signatures of a signing repository: npm's own registry adds fileCount and unpackedSize, which no JFrog client reads.

type Handler

type Handler struct {
	// The generic format's hooks for what npm leaves alone. Admit adds the
	// release and snapshot handling to its deploy policy (policy.go). A
	// republish's 403 belongs to the npm publish protocol: a REST deploy
	// over a tarball is an overwrite, which needs Delete, as for any item
	// (decision 0034).
	generic.Handler
	// Settings are the server's npm options.
	Settings Settings
}

Handler is the npm format.

func (Handler) Admit

func (h Handler) Admit(
	_ context.Context,
	env format.Env,
	c repo.Config,
	w format.Write,
) (format.Write, error)

Admit applies the deploy policy of every package type with the repository's release and snapshot handling between its checks, in Artifactory's order (RealRepoBase.assertValidPath): a blacked-out repository, then handleReleases and handleSnapshots, then the include and exclude patterns. 7.x refuses an npm local deploy the handling forbids with 409 (KB 000006800). The policy applies to every write that lands a file, an npm publish's included, as it does for Maven; a checksum upload is exempt, as Artifactory exempts checksum paths. Last, npm.enforce_path checks a deploy's file name (checkFileName).

func (Handler) Annotated

func (h Handler) Annotated(
	ctx context.Context,
	env format.Env,
	_ repo.Config,
	it model.Item,
	a format.Annotation,
) error

Annotated follows a change to an indexed tarball's dist-tags or deprecation, by whichever route it came: the npm protocol, the properties API, AQL, jf rt sp, or a deploy, copy, move or restore carrying them. A tag the tarball gains is taken from the package's other versions in the same transaction, so a tag names one version, latest included (rule 1). A change that leaves the package without latest settles it anew (rule 4), so npm dist-tag rm latest hands latest to the version that outranks the others, which may be the one it was taken from.

func (Handler) Deleted

func (h Handler) Deleted(
	ctx context.Context,
	env format.Env,
	c repo.Config,
	removed []model.Item,
) error

Deleted settles latest in each package a delete or a move took a tarball from (rule 4): pruning has dropped the versions left without a file, and with them, perhaps, the version that carried latest. The removed tarballs' versions are gone by now, so their packages are found by the names their paths give; a package with no version left is gone too, and a stray tarball's package keeps its latest.

func (Handler) Deployed

func (h Handler) Deployed(ctx context.Context, env format.Env, c repo.Config, it model.Item) error

Deployed indexes a tarball stored at its canonical path, and drops the version of a file that no longer is one, such as a tarball overwritten with another version's content. A tarball stored anywhere but where the index reads (pkgindex.Stored) is not indexed.

func (Handler) Inspect

func (h Handler) Inspect(
	ctx context.Context,
	env format.Env,
	c repo.Config,
	w format.Write,
) (any, error)

Inspect reads a tarball written at a path that could be canonical, or one npm.enforce_path checks, before the write's transaction. Other content needs no reading.

func (Handler) Owed

Owed is nothing: a read waits for no reindex or conversion, as 7.x's reads do not, since the deploys keep the index current.

func (Handler) Reindex

func (h Handler) Reindex(ctx context.Context, env format.Env, c repo.Config) error

Reindex rebuilds the repository's npm index from its tarballs and their properties, a remote repository's from its cache. Which repositories the JFrog reindex operation accepts is the API's rule, not the index's: decision 0034's local-only 400 is answered in artifacts/adapters/http.

func (Handler) Work

func (Handler) Work(ctx context.Context, env format.Env, it work.WorkItem) error

Work runs a conversion of the repository the item names. A repository that is gone, or no longer an npm one, owes nothing, and nor does an item of a kind the format no longer runs, which an earlier build may have left in the queue: running it as a conversion would move files nobody asked about.

func (Handler) WorkKinds

func (Handler) WorkKinds() []string

WorkKinds are the kinds the npm format runs.

type Key

type Key struct {
	// Expires is always null: a 7.x key pair has no expiry, and pacote
	// treats null as a key that never expires [I] (#216).
	Expires *string `json:"expires"`
	KeyID   string  `json:"keyid"`
	// KeyType and Scheme are the key's SSH algorithm name, as npm's
	// registry gives them (ecdsa-sha2-nistp256 for a P-256 key).
	KeyType string `json:"keytype"`
	Scheme  string `json:"scheme"`
	// Key is the public key as base64 PKIX DER, which npm wraps in a PEM
	// "PUBLIC KEY" block to verify with.
	Key string `json:"key"`
}

Key is one entry of the keys document npm reads from <registry>/-/npm/v1/keys, with the fields and order of registry.npmjs.org's.

func Keys

func Keys(ctx context.Context, keys accessapi.Keys, c repo.Config) ([]Key, error)

Keys is what the keys endpoint lists for c: the public keys of its primary and secondary pairs that are ECDSA, the primary first, each once. It is empty when c names no ECDSA pair, or is a repository that signs nothing, and the endpoint then answers as before signing existed. Listing the secondary lets a rotation stage the next pair there, so that the versions a reindex has not yet signed again still verify [I] (#216).

func PublicKey

func PublicKey(pub *ecdsa.PublicKey) (Key, error)

PublicKey is pub as the keys document names it. Its keyid is the key's OpenSSH SHA-256 fingerprint, which is how registry.npmjs.org derives the keyids it publishes; 7.x's documentation shows only "SHA256:" and a placeholder [I] (#216).

type Metadata

type Metadata struct {
	// Manifest is the tarball's package.json, compacted. Its version is as
	// the author wrote it; the index's version is the cleaned one.
	Manifest json.RawMessage `json:"manifest"`
	Dist     Dist            `json:"dist"`
	// HasInstallScript is Tarball.HasInstallScript.
	HasInstallScript bool `json:"has_install_script,omitempty"`
	// Tags are the dist-tags the tarball carries (npm.disttag), sorted.
	Tags []string `json:"tags,omitempty"`
	// Deprecated is the tarball's npm.deprecated message.
	Deprecated string `json:"deprecated,omitempty"`
	// Published is when the tarball was last deployed, in UTC to the
	// microsecond the stores keep.
	Published time.Time `json:"published"`
}

Metadata is a version's document in the package index (PackageVersion.Metadata, decision 0034): what packuments and search are built from, so a read needs neither the tarball nor its properties. Everything in it follows from the tarball, its stored checksums and its properties, so a reindex writes the same bytes.

func DecodeMetadata

func DecodeMetadata(b []byte) (Metadata, error)

DecodeMetadata reads a version's Metadata.

type Settings

type Settings struct {
	// TagLatestByPublish is npm.tag_latest_by_publish, 7.x's
	// artifactory.npm.tag.tagLatestByPublish: in rules 2 and 4 the version
	// deployed last outranks the others, pre-releases included, rather
	// than the highest. 7.x has it off by default (6.20's ConstantValues;
	// RTFACT-9754, RTFACT-20127).
	TagLatestByPublish bool
	// EnforcePath is npm.enforce_path, 7.x's artifactory.npm.enforce.path:
	// a package tarball deployed to a local repository under a file name
	// other than the one its package.json gives is refused (Admit). 7.x
	// has it off by default (npm-repositories).
	EnforcePath bool
}

Settings are the npm format's server-wide options (decision 0034), which reach it through ecosystem.Settings. The zero value is 7.x's default.

func (Settings) Latest

func (s Settings) Latest(cs []Candidate) string

Latest is the version of cs that outranks every other, "" when cs is empty.

func (Settings) Outranks

func (s Settings) Outranks(a, b Candidate) bool

Outranks reports whether a outranks b for latest: by version (the package-level Outranks), or with TagLatestByPublish by deploy time, the later first. Two versions deployed at one moment fall back to the version order, so the choice never depends on the order they are read in. [I] How 7.x breaks that tie is unknown (#216).

type Signature

type Signature struct {
	KeyID string `json:"keyid"`
	// Sig is the ASN.1 DER ECDSA signature in standard base64, which pacote
	// passes to Node's crypto.verify as 'base64'.
	Sig string `json:"sig"`
}

Signature is one entry of a version's dist.signatures, the shape 7.x's npm documentation shows and npm's pacote verifies (decision 0034, "Signatures").

type Signer

type Signer struct {
	// contains filtered or unexported fields
}

Signer signs a repository's versions with its primary key pair. A nil Signer signs nothing.

func SignerFor

func SignerFor(ctx context.Context, keys accessapi.Keys, c repo.Config) (*Signer, error)

SignerFor is the signer of c: its primary key pair when that is an ECDSA pair and c is a local or federated repository, as 7.x signs local repositories only and a federated repository is a local one with members [I] (#216). It is nil otherwise: a remote repository relays its upstream's signatures, and a pair of another type signs nothing, since 7.x's repository page offers only ECDSA pairs for npm and the repositories API accepts any pair by name. Signing uses the primary pair alone: the secondary is listed by the keys endpoint so that versions signed before a rotation still verify until a reindex signs them again.

It reads the pair through keys with ctx, so inside a transaction the read takes part in it. A nil keys signs nothing: a caller that builds a format environment without the key store runs no work that signs.

func (*Signer) Sign

func (s *Signer) Sign(name, version, integrity string) ([]Signature, error)

Sign is the dist.signatures of name's version with integrity, its dist.integrity: one signature over "<name>@<version>:<integrity>", the message pacote verifies with SHA-256. The signature is deterministic (RFC 6979), so a reindex with the same pair writes the same bytes, as decision 0034 has every reindex do. A nil Signer gives nil.

type Tarball

type Tarball struct {
	// SHA256 is the content read, so a hook can tell the inspection of its
	// own write from one of other content.
	SHA256 string
	// Name is package.json's; Version is package.json's as CleanVersion
	// leaves it.
	Name, Version string
	// Manifest is package.json, compacted.
	Manifest json.RawMessage
	// Description and Keywords are package.json's, as npm normalises them:
	// a description that is no string is none, and keywords given as one
	// string are split at commas.
	Description string
	Keywords    []string
	// License is package.json's license when it is a string, an SPDX
	// expression such as "MIT" or "(MIT OR Apache-2.0)".
	License string
	// HasInstallScript is whether installing runs a script: a preinstall,
	// install or postinstall script, or a binding.gyp, for which npm runs
	// node-gyp rebuild unless gypfile is false.
	HasInstallScript bool
}

Tarball is what the format reads from a package tarball.

func ReadStored

func ReadStored(ctx context.Context, blobs format.Blobs, sha string) (*Tarball, bool, error)

ReadStored reads the stored content sha as a tarball; ok is false when it is none the format can index. The npm publish handler reads the tarball it uploaded with it, to check it against the body before storing it.

func ReadTarball

func ReadTarball(ra io.ReaderAt, size int64) (*Tarball, error)

ReadTarball reads the package tarball in ra, of size bytes, in one pass: package.json from its top-level folder, whatever that folder is called, and whether it has a binding.gyp. ErrNotTarball when the format cannot index it; any other error is a failure to read the content.

Directories

Path Synopsis
Package npmhttp serves the npm registry of npm repositories at /artifactory/api/npm/{repoKey}/ (decision 0034), mounted through the router kit (decision 0032).
Package npmhttp serves the npm registry of npm repositories at /artifactory/api/npm/{repoKey}/ (decision 0034), mounted through the router kit (decision 0032).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL