import Sbom from '../sbom.js'; import Manifest from './manifest.js'; export type Provider = import('../provider').Provider; export type Provided = import('../provider').Provided; /** @typedef {import('../provider').Provider} Provider */ /** @typedef {import('../provider').Provided} Provided */ /** * The ecosystem identifier for JavaScript/npm packages * @type {string} */ export declare const purlType: string; /** * Converts a Subresource Integrity (SRI) string to a CycloneDX hash object. * SRI is formatted as `-` (e.g. `sha512-Ab3d...`). CycloneDX * expects an uppercased algorithm label (e.g. `SHA-512`) and a hex-encoded * digest. When the SRI string contains multiple space-separated hashes, only * the first is used. * @param {string} integrity - SRI string from a lock file * @returns {{alg: string, content: string}|null} CycloneDX hash, or null when unparseable */ export declare function sriToHash(integrity: string): { alg: string; content: string; } | null; /** * Base class for JavaScript package manager providers. * This class provides common functionality for different JavaScript package managers * (npm, pnpm, yarn) to generate SBOMs and handle package dependencies. * @abstract */ export default class Base_javascript { #private; /** * Sets up the provider with the manifest path and options * @param {string} manifestPath - Path to the package.json manifest file * @param {Object} opts - Configuration options for the provider * @param {Manifest} [manifest] - Manifest already loaded by the provider * @protected */ protected _setUp(manifestPath: string, opts: Object, manifest?: Manifest): void; /** * Gets the current manifest object * @returns {Manifest} The manifest object * @protected */ protected _getManifest(): Manifest; /** * Sets the ecosystem value * @param {string} ecosystem - The ecosystem identifier * @protected */ protected _setEcosystem(ecosystem: string): void; /** * Returns the name of the lock file for the specific implementation * @returns {string} The lock file name * @abstract * @protected */ protected _lockFileName(): string; /** * Returns the command name to use for the specific JS package manager * @returns {string} The command name * @abstract * @protected */ protected _cmdName(): string; /** * Returns the package manager name (e.g. npm, yarn, pnpm, bun) * @returns {string} The package manager name */ packageManagerName(): string; /** * Returns the command arguments for listing dependencies * @returns {Array} The command arguments * @abstract * @protected */ protected _listCmdArgs(): Array; /** * Returns the command arguments for updating the lock file * @returns {Array} The command arguments * @abstract * @protected */ protected _updateLockFileCmdArgs(): Array; /** * Parses the provider's lock file into a hash map keyed by `name@version`. * Subclasses override this (accepting the lock directory) to support their * lock file format. The default implementation returns an empty map so * providers without lock parsing (or without a lock file present) gracefully * omit hashes. * @returns {Map>} Hash map * @protected */ protected _parseLockFileHashes(): Map>; /** * Loads lock-file hashes for the given directory into internal state. * Parsing failures degrade gracefully to an empty map (no hashes emitted). * @param {string} lockDir - Directory containing the lock file * @protected */ protected _loadHashes(lockDir: string): void; /** * Looks up CycloneDX hashes for a package by name and resolved version. * @param {string} name - The package name (including scope, e.g. `@hapi/joi`) * @param {string} version - The resolved package version * @returns {Array<{alg: string, content: string}>|undefined} Hashes, or undefined * @protected */ protected _hashesFor(name: string, version: string): Array<{ alg: string; content: string; }> | undefined; /** * Looks up CycloneDX hashes for a package described by a PackageURL. * @param {PackageURL} purl - The package URL * @returns {Array<{alg: string, content: string}>|undefined} Hashes, or undefined * @protected */ protected _hashesForPurl(purl: PackageURL): Array<{ alg: string; content: string; }> | undefined; /** * Checks if the provider supports the given manifest name * @param {string} manifestName - The manifest name to check * @returns {boolean} True if the manifest is supported */ isSupported(manifestName: string): boolean; /** * Walks up the directory tree from manifestDir looking for the lock file. * Stops when the lock file is found, when a package.json with a "workspaces" * field is encountered without a lock file (workspace root boundary), or * when the filesystem root is reached. * * When TRUSTIFY_DA_WORKSPACE_DIR is set, checks only that directory (no walk-up). * * @param {string} manifestDir - The directory to start searching from * @param {Object} [opts={}] - optional; may contain TRUSTIFY_DA_WORKSPACE_DIR * @returns {string|null} The directory containing the lock file, or null * @protected */ protected _isWorkspaceRoot(dir: any): string | null; _findLockFileDir(manifestDir: any, opts?: {}): string | null; /** * @param {string} manifestDir - The base directory where the manifest is located * @param {Object} [opts={}] - optional; may contain TRUSTIFY_DA_WORKSPACE_DIR * @returns {boolean} True if the lock file exists */ validateLockFile(manifestDir: string, opts?: Object): boolean; /** * Provides content and content type for stack analysis * @param {string} manifestPath - The manifest path or name * @param {Object} [opts={}] - Optional configuration options * @returns {Provided} The provided data for stack analysis */ provideStack(manifestPath: string, opts?: Object): Provided; /** * Provides content and content type for component analysis * @param {string} manifestPath - Path to package.json for component report * @param {Object} [opts={}] - Optional configuration options * @returns {Provided} The provided data for component analysis */ provideComponent(manifestPath: string, opts?: Object): Provided; /** * Read license from manifest (package.json). Reused by npm, pnpm, yarn. * @param {string} manifestPath - path to package.json * @returns {string|null} */ readLicenseFromManifest(manifestPath: string): string | null; /** * Builds the dependency tree for the project * @param {boolean} includeTransitive - Whether to include transitive dependencies * @param {Object} [opts={}] - Configuration options; when `TRUSTIFY_DA_WORKSPACE_DIR` is set, commands run from workspace root * @returns {Object} The dependency tree * @protected */ protected _buildDependencyTree(includeTransitive: boolean, opts?: Object): Object; /** * Creates SBOM json string for npm Package * @param {Object} [opts={}] - Optional configuration options * @returns {string} The SBOM json content * @private */ private #getSBOM; /** * Ensures peer and optional dependencies declared in the manifest are * present in the SBOM, even when the package manager does not resolve them * (e.g. yarn does not include peer deps in its dependency listing). * @param {Sbom} sbom - The SBOM to supplement * @private */ private #ensurePeerAndOptionalDeps; /** * Recursively builds the Sbom from the JSON that npm listing returns * @param {Sbom} sbom - The SBOM object to add dependencies to * @param {Object} depTree - The current dependency tree * @protected */ protected _addDependenciesToSbom(sbom: Sbom, depTree: Object): void; /** * Adds dependencies of a specific package to the SBOM * @param {Sbom} sbom - The SBOM object to add dependencies to * @param {PackageURL} from - The package URL to add dependencies for * @param {Object} artifact - The artifact containing dependencies * @private */ private #addDependenciesOf; /** * Creates a SBOM containing only direct dependencies * @param {Object} [opts={}] - Optional configuration options * @returns {string} The SBOM as a JSON string * @private */ private #getDirectDependencySbom; /** * Extracts root dependencies from the dependency tree * @param {Object} depTree - The dependency tree object * @returns {Map} Map of dependency names to their PackageURL objects * @protected */ protected _getRootDependencies(depTree: Object): Map; /** * Executes the list command to get dependencies * @param {boolean} includeTransitive - Whether to include transitive dependencies * @param {string} manifestDir - The manifest directory * @returns {string} The command output * @private */ private #executeListCmd; /** * Gets the version of the package manager * @param {Object} [opts={}] - Command options, including the workspace directory * @returns {string} The version string of the package manager * @protected */ protected _version(opts?: Object): string; /** * Creates or updates the lock file for the package manager * @param {string} manifestDir - Directory containing the manifest file * @protected */ protected _createLockFile(manifestDir: string): void; /** * Invokes a command with the given arguments * @param {string[]} args - Command arguments * @param {Object} [opts={}] - Optional configuration options * @returns {string} Command output * @throws {Error} If command execution fails or command is not found * @private */ private #invokeCommand; /** * Parses the dependency tree output * @param {string} output - The output to parse * @returns {string} The parsed output * @protected */ protected _parseDepTreeOutput(output: string): string; }