npm

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 11, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	SourceNpmrc  = "npmrc"  // Discovered from .npmrc file
	SourceEnv    = "env"    // From NPM_TOKEN environment variable
	SourceManual = "manual" // Entered interactively
)

Token source constants.

View Source
const DefaultRegistry = "registry.npmjs.org"

DefaultRegistry is the default npm registry host.

View Source
const NpmTokenPlaceholder = "npm_moatProxyInjected00000000"

NpmTokenPlaceholder is a format-valid npm token placeholder for container .npmrc files. The proxy replaces the real Authorization header at the network layer. npm tokens use the format "npm_" followed by base36 characters.

Variables

This section is empty.

Functions

func GenerateNpmrc

func GenerateNpmrc(entries []RegistryEntry, placeholder string) string

GenerateNpmrc generates a stub .npmrc for use inside containers. Scope-to-registry routing is real (npm needs this), but tokens are placeholders.

func MarshalEntries

func MarshalEntries(entries []RegistryEntry) (string, error)

MarshalEntries encodes registry entries as JSON for storage in the Token field.

func WithGrantOptions

func WithGrantOptions(ctx context.Context, host string) context.Context

WithGrantOptions returns a context with npm grant options.

Types

type Provider

type Provider struct{}

Provider implements provider.CredentialProvider for npm registries.

func (*Provider) Cleanup

func (p *Provider) Cleanup(cleanupPath string)

Cleanup cleans up npm resources.

func (*Provider) ConfigureProxy

func (p *Provider) ConfigureProxy(proxy provider.ProxyConfigurer, cred *provider.Credential)

ConfigureProxy sets up proxy headers for npm registries. Parses the Token JSON and sets per-host Bearer credentials.

func (*Provider) ContainerEnv

func (p *Provider) ContainerEnv(cred *provider.Credential) []string

ContainerEnv returns environment variables for npm. No env vars needed — npm reads ~/.npmrc by default, and ContainerMounts places the generated .npmrc at containerHome/.npmrc.

func (*Provider) ContainerMounts

func (p *Provider) ContainerMounts(cred *provider.Credential, containerHome string) ([]provider.MountConfig, string, error)

ContainerMounts generates a stub .npmrc in a temp directory and returns mount configs. The .npmrc contains real scope-to-registry routing but placeholder tokens.

func (*Provider) Grant

func (p *Provider) Grant(ctx context.Context) (*provider.Credential, error)

Grant acquires npm credentials by discovering registries or prompting for a specific host.

When called without --host: discovers registries from .npmrc and NPM_TOKEN env var. When called with --host: prompts for token and scopes for a specific registry.

func (*Provider) ImpliedDependencies

func (p *Provider) ImpliedDependencies() []string

ImpliedDependencies returns dependencies implied by this provider. node provides the runtime; npm upgrades to version 11 (node bundles an older npm).

func (*Provider) Name

func (p *Provider) Name() string

Name returns the provider identifier.

type RegistryEntry

type RegistryEntry struct {
	Host        string   `json:"host"`                   // "registry.npmjs.org"
	Token       string   `json:"token"`                  // "npm_aaaa..."
	Scopes      []string `json:"scopes,omitempty"`       // ["@myorg"] — empty for default registry
	TokenSource string   `json:"token_source,omitempty"` // "npmrc", "env", "manual"
}

RegistryEntry represents a single npm registry with its authentication token.

func MergeEntry

func MergeEntry(entries []RegistryEntry, newEntry RegistryEntry) []RegistryEntry

MergeEntry merges a new entry into the existing entries, replacing any with the same host.

func ParseNpmrc

func ParseNpmrc(r io.Reader) ([]RegistryEntry, error)

ParseNpmrc parses an .npmrc file and extracts registry entries.

It handles two types of lines:

Returns the discovered entries with host-to-token and scope-to-host mappings merged.

func UnmarshalEntries

func UnmarshalEntries(token string) ([]RegistryEntry, error)

UnmarshalEntries decodes registry entries from the JSON-encoded Token field.

Jump to

Keyboard shortcuts

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