PURL Specification & Parsing
Relevant source files
The following files were used as context for generating this wiki page:
Package URLs (PURLs) serve as the primary addressing mechanism within the regxa project. They provide a standardized way to identify software packages across disparate ecosystems such as npm, PyPI, and Cargo. By using the ECMA-427 specification, the project ensures that package references are portable and unambiguous across different registry adapters.
The core logic for handling these identifiers is centralized in the src/core/purl.ts module, which handles the decomposition of PURL strings into structured objects and the reconstruction of strings from package metadata. This normalization allows the library to offer a single API for multiple registries.
Sources: README.md:12-18, src/core/purl.ts:18-24
PURL Format and Components
A standard PURL in regxa follows the formal specification: pkg:<type>/<namespace>/<name>@<version>?<qualifiers>#<subpath>. While the type and name are mandatory, all other components are optional.
| Component | Description | Example |
|---|---|---|
| Scheme | Constant prefix pkg: | pkg: |
| Type | The package ecosystem/registry | npm, pypi, cargo |
| Namespace | Ecosystem-specific prefix (e.g., npm scope, composer vendor) | %40babel, laravel |
| Name | The name of the package | lodash, flask |
| Version | Specific version identifier | 4.17.21, 3.1.1 |
| Qualifiers | Key-value pairs for additional context (e.g., repository URL) | repository_url=https://... |
| Subpath | Path to a specific file or directory within the package | lib/index.js |
Sources: skills/regxa/references/purl-guide.md:7-12, src/core/purl.ts:18-24, skills/regxa/SKILL.md:14-25
Parsing Logic Flow
The parsing process involves sequential extraction of components from the PURL string, starting with the scheme and working through subpaths, qualifiers, versions, and finally the type/name structure.
The diagram shows the logical sequence used in the parsePURL function to decompose a string identifier into a structured data object.
Sources: src/core/purl.ts:25-88
Core Functions
parsePURL(purlStr: string)
This function is the "single source of truth" for PURL validation. It decodes percent-encoded components and applies ecosystem-specific normalization. For example, PyPI package names are lowercased and runs of characters like _ or . are collapsed into single dashes.
Sources: src/core/purl.ts:25-88, skills/regxa/references/purl-guide.md:46-51
buildPURL(parts: ParsedPURL)
The inverse of parsePURL, this function constructs a valid PURL string from a component object. It ensures that special characters in the namespace, name, and version are correctly percent-encoded (e.g., encoding @ as %40).
Sources: src/core/purl.ts:107-138, test/unit/purl.test.ts:167-185
createFromPURL(purlStr: string, client?: Client)
This high-level utility parses the PURL and immediately initializes the corresponding registry adapter. It extracts the repository_url from qualifiers if present to override the default registry endpoint.
Sources: src/core/purl.ts:98-105
Ecosystem-Specific Handling
Different ecosystems require specific formatting or normalization rules during the parsing and building phases.
Normalization Rules
- PyPI: Names are lowercased. Sequences of
[-_.]+are replaced with a single-. - ALPM: Package names are lowercased.
- npm/Cargo/Gem: No normalization is applied; these remain case-sensitive as per their respective registries.
Sources: src/core/purl.ts:89-95, skills/regxa/references/purl-guide.md:46-51
Scoping and Namespaces
Registry namespaces are mapped to PURL namespaces differently depending on the ecosystem.
The diagram illustrates how different ecosystem-specific groupings (scopes, vendors, repositories) are mapped to the PURL namespace component.
Sources: skills/regxa/references/purl-guide.md:14-30, skills/regxa-ai-tool/references/purl-cheatsheet.md:20-25
Special Character Encoding
Standard URL components must be percent-encoded within a PURL to avoid collision with separators like /, @, and ?.
| Character | Encoded | Usage Context |
|---|---|---|
@ | %40 | Required for npm scopes (e.g., %40babel/core) |
/ | %2F | Used when a name contains a literal slash (rare) |
+ | %2B | Often found in build metadata of versions |
| space | %20 | Used for packages with spaces in their name |
Sources: skills/regxa/references/purl-guide.md:32-40, test/unit/purl.test.ts:141-145
Error Handling
The parser strictly enforces the PURL specification. Failure to meet formatting requirements results in an InvalidPURLError.
| Error Condition | Cause |
|---|---|
| Missing Prefix | PURL does not start with pkg: |
| Missing Separator | No / between type and name |
| Empty Components | Type or Name components are zero-length |
| Malformed Encoding | Invalid percent-encoding sequences (e.g., %ZZ) |
| Context Violation | Attempting a dependency lookup without a version in the PURL |
Sources: src/core/purl.ts:26-34, src/core/purl.ts:68-80, src/helpers.ts:31-34
Shorthand vs. Full PURL
While the library core requires full PURL strings, the CLI and AI tools support shorthand.
- Library API: Requires
pkg:npm/lodash. Passingnpm/lodashwill throw an error. - CLI: Accepts
npm/lodashand automatically expands it topkg:npm/lodash. - AI Tool: The
packageToolgenerally expects full PURLs to ensure accuracy across ecosystems.
Sources: skills/regxa/references/purl-guide.md:42-44, skills/regxa/SKILL.md:27-30
PURL Resolution Sequence
This sequence shows how a PURL string travels from a high-level helper call through the parser and factory to eventually resolve into package data.
Sources: src/helpers.ts:8-14, src/core/purl.ts:98-105
The PURL system in regxa ensures that despite the diversity of package registries, every package can be addressed, parsed, and normalized through a singular, predictable pipeline.