@hyperfrontend/versioning/registry/npm

npm

NPM registry client: fetches package metadata from a registry endpoint with in-memory caching and safe URL encoding.

createNpmRegistry(config) returns a registry binding that resolves packages via the standard https://registry.npmjs.org/<package> API. escapePackageName and escapeVersion produce URL-safe path segments for scoped packages and prerelease tags. The cache (createCache, Cache, CacheEntry) is a small TTL-bound in-memory store so repeated lookups within a release flow do not refetch the same package metadata.

API Reference§

ƒ Functions

§function

classifyNpmError(error: unknown): NpmLookupFailure

Decides whether a failed npm view proves absence or leaves the answer unknown.
Only a 404 from the registry proves absence, and npm reports both a missing package and a missing version that way. Every other outcome, including a timeout that produces no output at all, leaves the published state unknown and must not be read as "not published".

Parameters

NameTypeDescription
§error
unknown
The error thrown by the invocation

Returns

NpmLookupFailure
Whether the package is absent, or why the registry could not answer

Example

Classifying a lookup failure

try {
  execFileSync('npm', ['view', name, 'version'])
} catch (error) {
  const failure = classifyNpmError(error)
  if (failure.kind === 'absent') return null
  throw createRegistryUnavailableError({ ...failure, packageName: name })
}
§function

createCache(ttl: number): Cache

Creates a new cache instance.

Parameters

NameTypeDescription
§ttl
number
Time-to-live in milliseconds (default: 60000 = 1 minute)
(default: 60000)

Returns

Cache
A new Cache instance

Example

Using a cache with custom TTL

const cache = createCache(30000) // 30 second TTL
cache.set('key', { data: 'value' })
const value = cache.get<{ data: string }>('key')
§function

createNpmRegistry(config: RegistryConfig): Registry

Creates an npm registry client.

Parameters

NameTypeDescription
§config
RegistryConfig
Registry configuration
(default: {})

Returns

Registry
A Registry implementation for npm

Example

Creating an npm registry client
const registry = createNpmRegistry()
const version = await registry.getLatestVersion('@hyperfrontend/utils')
§function

escapePackageName(name: string): string

Escapes a package name for safe use in shell commands. Uses character-by-character validation to prevent injection.

Parameters

NameTypeDescription
§name
string
Package name to escape

Returns

string
Safe package name

Example

Validating package names for shell safety

escapePackageName('@scope/package') // => '@scope/package'
escapePackageName('simple-pkg') // => 'simple-pkg'
escapePackageName('pkg$invalid') // throws - '$' is invalid
§function

escapeVersion(version: string): string

Escapes a version string for safe use in shell commands.

Parameters

NameTypeDescription
§version
string
Version string to escape

Returns

string
Safe version string

Example

Validating version strings for shell safety

escapeVersion('1.2.3') // => '1.2.3'
escapeVersion('1.0.0-beta.1') // => '1.0.0-beta.1'
escapeVersion('1.0.0+build') // => '1.0.0+build'

Interfaces

§interface

AbsentLookupFailure

The registry answered and the package or version is not there.

Properties

§readonly kind:"absent"
Discriminant.
§interface

Cache

Simple in-memory cache with TTL support.

Properties

§interface

CacheEntry

Cache entry with data and expiration time.

Properties

§readonly data:T
Cached data
§readonly timestamp:number
Timestamp when entry was created
§interface

UnavailableLookupFailure

The registry did not answer, so nothing may be concluded.

Properties

§readonly detail:string
Diagnostic text pulled from the failed invocation.
§readonly kind:"unavailable"
Discriminant.
§readonly reason:RegistryFailureReason
Why the registry could not answer.

Types

§type

NpmLookupFailure

Outcome of a failed npm view invocation.