Data Normalization (Licenses & URLs)
Relevant source files
The following files were used as context for generating this wiki page:
Data normalization is a core architectural pillar of the regxa library. Since different package registries (npm, PyPI, Cargo, etc.) use varying formats for metadata, regxa provides a unified layer to sanitize and standardize license identifiers and repository URLs. This ensures that regardless of the upstream source, the consumer receives predictable, SPDX-compliant license strings and canonical HTTPS repository links.
This system is primarily implemented within the src/core directory, specifically through utility functions that ecosystem-specific adapters call during the package fetching process.
License Normalization
The license normalization system converts arbitrary license strings—often containing descriptive text or non-standard naming—into valid SPDX identifiers. It also handles the combination of multiple licenses into single boolean expressions.
Normalization Logic
The logic primarily resides in normalizeLicense, which uses case-insensitive matching and whitespace trimming to map common variants to a canonical form. For example, "Apache License 2.0" or "Apache2" are both normalized to "Apache-2.0". If no mapping is found, the string is passed through as-is, assuming it might already be a valid but less common SPDX identifier.
Sources: src/core/license.ts, test/unit/license.test.ts:5-104
License Combination
When a package is licensed under multiple terms, the combineLicenses utility merges an array of license strings into a single string using the OR operator. This was specifically updated in version v0.1.7 to prefer OR over AND for better compatibility with common registry practices.
Sources: src/core/license.ts, CHANGELOG.md:12, test/unit/license.test.ts:107-150
Mapping Examples
The following table illustrates how various registry inputs are transformed:
| Original Input | Normalized Output |
|---|---|
MIT License | MIT |
Apache 2.0 | Apache-2.0 |
Simplified BSD License | BSD-2-Clause |
Public Domain | Unlicense |
GPL-3.0 | GPL-3.0-only |
Sources: test/unit/license.test.ts:5-104
Repository URL Normalization
Repository normalization ensures that diverse git protocols and shorthand formats are converted into a clean, web-accessible HTTPS URL. This logic is encapsulated in the normalizeRepositoryURL function.
URL Cleaning Pipeline
The normalization process follows a specific flow to strip protocol noise and standardize the host:
- Extraction: If the input is an object (common in
package.json), theurlproperty is extracted. - Protocol Conversion: Protocols like
git://,git+https://, andssh://git@are converted tohttps://. - Shorthand Resolution: Shorthand for major providers (GitHub, GitLab, Bitbucket) is expanded to full URLs.
- Sanitization: Trailing
.gitsuffixes and trailing slashes are removed.
Sources: src/core/repository.ts, test/unit/repository.test.ts:55-100
Logic Flow
The following diagram represents how an raw repository string is processed:
The pipeline ensures that a complex SSH URL like git@github.com:foo/bar.git results in the same output as the shorthand github:foo/bar.
Sources: src/core/repository.ts, test/unit/repository.test.ts:70-140
Integration in Registries
Ecosystem adapters (registries) utilize these core utilities to populate the Package and Version data structures defined in the core types.
Data Flow Diagram
This sequence shows how a registry adapter (e.g., npm.ts) processes an upstream API response before returning it to the user.
Implementation Snippets
Adapters import these helpers to maintain consistency across the codebase:
// From src/registries/npm.ts
import { normalizeLicense } from "../core/license.ts";
import { normalizeRepositoryURL } from "../core/repository.ts";
// Inside fetchPackage:
return {
name: data.name,
repository: normalizeRepositoryURL(data.repository || ""),
licenses: this.extractLicense(data.license || latestVersionData?.license),
// ...other fields
};
Sources: src/registries/npm.ts:11-12, src/registries/npm.ts:111-125
Summary
By centralizing normalization in src/core/license.ts and src/core/repository.ts, regxa provides a consistent developer experience. Consumers do not need to handle the nuances of git+ssh protocols or varied license naming conventions, as the library guarantees a sanitized output compatible with standard tools and SPDX scanners.