import { PurlComponent } from './purl-component.js'; import { PurlQualifierNames } from './purl-qualifier-names.js'; import { PurlType } from './purl-type.js'; import { Err, Ok, ResultUtils, err, ok } from './result.js'; import { UrlConverter } from './url-converter.js'; import type { QualifiersObject } from './purl-component.js'; import type { Result } from './result.js'; import type { DownloadUrl, RepositoryUrl } from './url-converter.js'; /** * Type representing the possible values for PackageURL component properties. * Used for index signature to allow dynamic property access. */ export type PackageURLComponentValue = string | QualifiersObject | undefined; /** * Type representing a plain object representation of a PackageURL. * Contains all package URL components as properties. */ export type PackageURLObject = { type?: string | undefined; namespace?: string | undefined; name?: string | undefined; version?: string | undefined; qualifiers?: QualifiersObject | undefined; subpath?: string | undefined; }; /** * Type representing parsed PURL components as a tuple. * Returned by PackageURL.parseString() in the order: * [type, namespace, name, version, qualifiers, subpath] */ export type ParsedPurlComponents = [ type: string | undefined, namespace: string | undefined, name: string | undefined, version: string | undefined, qualifiers: URLSearchParams | undefined, subpath: string | undefined ]; /** * Package URL parser and constructor implementing the PURL specification. * Provides methods to parse, construct, and manipulate Package URLs with validation and normalization. */ declare class PackageURL { static Component: Record>; static KnownQualifierNames: { __proto__: null; Checksum: string; DownloadUrl: string; FileName: string; RepositoryUrl: string; VcsUrl: string; Vers: string; }; static Type: Record>; /** @internal Cached canonical string representation. */ _cachedString?: string | undefined; name?: string | undefined; namespace?: string | undefined; qualifiers?: QualifiersObject | undefined; subpath?: string | undefined; type?: string | undefined; version?: string | undefined; constructor(rawType: unknown, rawNamespace: unknown, rawName: unknown, rawVersion: unknown, rawQualifiers: unknown, rawSubpath: unknown); /** * Convert PackageURL to object for JSONStringify compatibility. */ toJSON(): PackageURLObject; /** * Convert PackageURL to JSON string representation. */ toJSONString(): string; /** * Convert PackageURL to a plain object representation. */ toObject(): PackageURLObject; /** * Get the package specifier string without the scheme and type prefix. * * Returns `namespace/name@version?qualifiers#subpath` — the package identity * without the `pkg:type/` prefix. * * @returns Spec string (e.g., '@babel/core@7.0.0' for pkg:npm/%40babel/core@7.0.0) */ toSpec(): string; toString(): string; /** * Create a new PackageURL with a different version. * Returns a new instance — the original is unchanged. * * @param version - New version string * @returns New PackageURL with the updated version */ withVersion(version: string | undefined): PackageURL; /** * Create a new PackageURL with a different namespace. * Returns a new instance — the original is unchanged. * * @param namespace - New namespace string * @returns New PackageURL with the updated namespace */ withNamespace(namespace: string | undefined): PackageURL; /** * Create a new PackageURL with a single qualifier added or updated. * Returns a new instance — the original is unchanged. * * @param key - Qualifier key * @param value - Qualifier value * @returns New PackageURL with the qualifier set */ withQualifier(key: string, value: string): PackageURL; /** * Create a new PackageURL with all qualifiers replaced. * Returns a new instance — the original is unchanged. * * @param qualifiers - New qualifiers object (or undefined to remove all) * @returns New PackageURL with the updated qualifiers */ withQualifiers(qualifiers: Record | undefined): PackageURL; /** * Create a new PackageURL with a different subpath. * Returns a new instance — the original is unchanged. * * @param subpath - New subpath string * @returns New PackageURL with the updated subpath */ withSubpath(subpath: string | undefined): PackageURL; /** * Compare this PackageURL with another for equality. * * Two PURLs are considered equal if their canonical string representations match. * This comparison is case-sensitive after normalization. * * @param other - The PackageURL to compare with * @returns true if the PURLs are equal, false otherwise */ equals(other: PackageURL): boolean; /** * Compare two PackageURLs for equality. * * Two PURLs are considered equal if their canonical string representations match. * * @param a - First PackageURL to compare * @param b - Second PackageURL to compare * @returns true if the PURLs are equal, false otherwise */ static equals(a: PackageURL, b: PackageURL): boolean; /** * Compare this PackageURL with another for sorting. * * Returns a number indicating sort order: * - Negative if this comes before other * - Zero if they are equal * - Positive if this comes after other * * @param other - The PackageURL to compare with * @returns -1, 0, or 1 for sort ordering */ compare(other: PackageURL): -1 | 0 | 1; /** * Compare two PackageURLs for sorting. * * Compares PURLs using their canonical string representations. * Returns a number indicating sort order: * - Negative if a comes before b * - Zero if they are equal * - Positive if a comes after b * * @param a - First PackageURL to compare * @param b - Second PackageURL to compare * @returns -1, 0, or 1 for sort ordering */ static compare(a: PackageURL, b: PackageURL): -1 | 0 | 1; /** * Create PackageURL from JSON string. */ static fromJSON(json: unknown): PackageURL; /** * Create PackageURL from a plain object. */ static fromObject(obj: unknown): PackageURL; static fromString(purlStr: unknown): PackageURL; /** * Create PackageURL from npm package specifier. * * Parses npm package specifiers and converts them to PackageURL format. * Handles scoped packages, version ranges, and normalizes version strings. * * **Supported formats:** * - Basic packages: `lodash`, `lodash@4.17.21` * - Scoped packages: `@babel/core`, `@babel/core@7.0.0` * - Version ranges: `^4.17.21`, `~1.2.3`, `>=1.0.0` (prefixes stripped) * - Dist-tags: `latest`, `next`, `beta` (passed through as version) * * **Not supported:** * - Git URLs: `git+https://...` (use PackageURL constructor directly) * - File paths: `file:../package.tgz` * - GitHub shortcuts: `user/repo#branch` * - Aliases: `npm:package@version` * * **Note:** Dist-tags like `latest` are mutable and should be resolved to * concrete versions for reproducible builds. This method passes them through * as-is for convenience. * * @param specifier - npm package specifier (e.g., 'lodash@4.17.21', '@babel/core@^7.0.0') * @returns PackageURL instance for the npm package * @throws {Error} If specifier is not a string or is empty * * @example * ```typescript * // Basic packages * PackageURL.fromNpm('lodash@4.17.21') * // -> pkg:npm/lodash@4.17.21 * * // Scoped packages * PackageURL.fromNpm('@babel/core@^7.0.0') * // -> pkg:npm/%40babel/core@7.0.0 * * // Dist-tags (passed through) * PackageURL.fromNpm('react@latest') * // -> pkg:npm/react@latest * * // No version * PackageURL.fromNpm('express') * // -> pkg:npm/express * ``` */ static fromNpm(specifier: unknown): PackageURL; /** * Create PackageURL from ecosystem-specific package specifier. * * This is a convenience wrapper that delegates to type-specific parsers. * Each ecosystem has its own specifier format and parsing rules. * * **Supported types:** * - `npm`: npm package specifiers (e.g., 'lodash@4.17.21', '@babel/core@^7.0.0') * * @param type - Package ecosystem type (e.g., 'npm', 'pypi', 'maven') * @param specifier - Ecosystem-specific package specifier string * @returns PackageURL instance for the package * @throws {Error} If type is not supported or specifier is invalid * * @example * ```typescript * // npm packages * PackageURL.fromSpec('npm', 'lodash@4.17.21') * // -> pkg:npm/lodash@4.17.21 * * PackageURL.fromSpec('npm', '@babel/core@^7.0.0') * // -> pkg:npm/%40babel/core@7.0.0 * ``` */ static fromSpec(type: string, specifier: unknown): PackageURL; static parseString(purlStr: unknown): ParsedPurlComponents; /** * Check if a string is a valid PURL without throwing. * * @param purlStr - String to validate * @returns true if the string is a valid PURL * * @example * ```typescript * PackageURL.isValid('pkg:npm/lodash@4.17.21') // true * PackageURL.isValid('not a purl') // false * ``` */ static isValid(purlStr: unknown): boolean; /** * Create PackageURL from a registry or repository URL. * * Convenience wrapper for UrlConverter.fromUrl(). Supports 27 hostnames * across 17 package types including npm, pypi, maven, github, and more. * * @param urlStr - Registry or repository URL * @returns PackageURL instance or undefined if URL is not recognized * * @example * ```typescript * PackageURL.fromUrl('https://www.npmjs.com/package/lodash') * // -> pkg:npm/lodash * * PackageURL.fromUrl('https://github.com/lodash/lodash') * // -> pkg:github/lodash/lodash * ``` */ static fromUrl(urlStr: string): PackageURL | undefined; static tryFromJSON(json: unknown): Result; static tryFromObject(obj: unknown): Result; static tryFromString(purlStr: unknown): Result; static tryParseString(purlStr: unknown): Result; } export { Err, Ok, PackageURL, PurlComponent, PurlQualifierNames, PurlType, ResultUtils, UrlConverter, err, ok, }; export type { DownloadUrl, RepositoryUrl, Result };