@hyperfrontend/versioning/registry

registry/

Package registry abstractions for querying npm and other package registries.

Overview

Unified interface for querying package registries with in-memory caching. Validates all user inputs before shell execution to prevent injection attacks.

Usage Examples

Querying npm Registry

import { createNpmRegistry } from '@hyperfrontend/versioning'

const registry = createNpmRegistry()

// Get latest version
const latest = await registry.getLatestVersion('@hyperfrontend/utils')
console.log(latest) // '1.2.3'

// Check if version is published
const exists = await registry.isVersionPublished('lodash', '4.17.21')
console.log(exists) // true

// Get full package info
const info = await registry.getPackageInfo('typescript')
console.log(info?.versions) // ['4.0.0', '4.1.0', ...]
console.log(info?.latestVersion) // '5.4.0'

Using the Factory

import { createRegistry } from '@hyperfrontend/versioning'

// Create registry by type
const registry = createRegistry('npm', {
  timeout: 5000,
  cacheTtl: 30000,
})

const versions = await registry.listVersions('react')

Custom Cache Configuration

import { createNpmRegistry } from '@hyperfrontend/versioning'

// 5 minute cache, 30 second timeout
const registry = createNpmRegistry({
  cacheTtl: 300000,
  timeout: 30000,
})

Version-Specific Information

import { createNpmRegistry } from '@hyperfrontend/versioning'

const registry = createNpmRegistry()
const versionInfo = await registry.getVersionInfo('lodash', '4.17.21')

console.log(versionInfo?.publishedAt) // '2021-02-20T15:42:16.891Z'
console.log(versionInfo?.dependencies) // { ... }
console.log(versionInfo?.engines) // { node: '>=0.10.0' }

Security

Input Validation

All package names and versions are validated character-by-character before being used in shell commands:

  • Package names: Only a-z, A-Z, 0-9, @, /, -, _, . allowed
  • Versions: Only 0-9, a-z, A-Z, ., -, + allowed
  • Length limits: Package names max 214 chars, versions max 256 chars

Invalid characters throw descriptive errors:

import { escapePackageName } from '@hyperfrontend/versioning'

escapePackageName('lodash') // 'lodash'
escapePackageName('@scope/pkg') // '@scope/pkg'
escapePackageName('pkg; rm -rf /') // throws Error

Caching

Responses are cached in memory to reduce registry load:

  • Default TTL: 60 seconds
  • Expired entries are cleaned up on access
  • Cache can be configured per-client

See Also

  • semver/ — Version parsing for registry queries
  • flow/ — Fetches registry versions in workflows
  • workspace/ — Package discovery and metadata
  • Main README — Package overview and quick start
  • ARCHITECTURE.md — Design principles and data flow

API Reference

ƒ Functions

§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

createPackageInfo(options: CreatePackageInfoOptions): PackageInfo

Creates a new PackageInfo object.

Parameters

NameTypeDescription
§options
CreatePackageInfoOptions
Configuration for the package info

Returns

PackageInfo
A new PackageInfo object

Example

Creating package info from registry data

const info = createPackageInfo({
  name: '@scope/my-package',
  latestVersion: '2.0.0',
  versions: ['1.0.0', '1.1.0', '2.0.0'],
  license: 'MIT'
})
§function

createRegistry(type: "npm", config: RegistryConfig): Registry

Creates a registry client.

Parameters

NameTypeDescription
§type
"npm"
Type of registry to create
(default: 'npm')
§config
RegistryConfig
Registry configuration
(default: {})

Returns

Registry
A Registry instance

Example

Creating an npm registry client
const registry = createRegistry('npm')
const version = await registry.getLatestVersion('lodash')
§function

createVersionInfo(options: CreateVersionInfoOptions): VersionInfo

Creates a new VersionInfo object.

Parameters

NameTypeDescription
§options
CreateVersionInfoOptions
Configuration for the release info

Returns

VersionInfo
A new VersionInfo object

Example

Creating version info from registry data

const release = createVersionInfo({
  version: '1.2.3',
  publishedAt: '2024-01-15T10:30:00Z',
  tarball: 'https://registry.npmjs.org/pkg/-/pkg-1.2.3.tgz'
})
§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

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

Maintainer

Maintainer information.

Properties

§readonly email?:string
Maintainer email
§readonly name:string
Maintainer name
§interface

PackageInfo

Package information from a registry.

Properties

§readonly description?:string
Package description
§readonly homepage?:string
Homepage URL
§readonly keywords?:unknown
Keywords
§readonly lastModified?:string
Time of last modification
§readonly latestVersion:string
Latest version
§readonly license?:string
SPDX license identifier
§readonly maintainers:unknown
Package maintainers
§readonly name:string
Package name
§readonly repository?:string
Repository URL
§readonly versions:unknown
All published versions
§interface

Registry

Abstract interface for package registries.

Properties

§readonly name:string
Registry name (e.g., "npm", "yarn")
§readonly url:string
Registry URL
§interface

RegistryConfig

Registry configuration options.

Properties

§readonly authToken?:string
Authentication token
§readonly cacheTtl?:number
Cache TTL in milliseconds
§readonly timeout?:number
Request timeout in milliseconds
§readonly url?:string
Registry URL
§interface

VersionInfo

Version-specific information from a registry.

Properties

§readonly dependencies?:Record<string, string>
Runtime dependencies
§readonly devDependencies?:Record<string, string>
Development dependencies
§readonly engines?:Record<string, string>
Engine requirements
§readonly gitHead?:string
Git commit hash at publish time. Used to determine commit range for changelog generation.
NOTE: This value comes from npm registry, making it immutable and independent of local git state.
§readonly integrity?:string
Subresource integrity hash
§readonly nodeVersion?:string
Node.js version range
§readonly npmVersion?:string
npm version used to publish
§readonly optionalDependencies?:Record<string, string>
Optional peer dependencies
§readonly peerDependencies?:Record<string, string>
Peer dependencies
§readonly publishedAt:string
ISO date when published
§readonly tarball:string
Tarball URL
§readonly version:string
Version string

Types

§type

RegistryType

Supported registry types.
type RegistryType = "npm"