// dep-auditor/license-registry.ts — SPEC-025 tech debt: license detection from registries
// Fetches license info from npm, PyPI, and crates.io JSON APIs.
const TIMEOUT_MS = 5000;
// ---------------------------------------------------------------------------
// npm registry
// ---------------------------------------------------------------------------
/**
 * Fetch the license string for an npm package version.
 * GET https://registry.npmjs.org/${pkg}/${version}
 * Returns null on any error (network, timeout, parse, missing field).
 */
export async function fetchNpmLicense(pkg, version) {
    return makeHttpsGet(`https://registry.npmjs.org/${encodeURIComponent(pkg)}/${encodeURIComponent(version)}`, (raw) => {
        const parsed = JSON.parse(raw);
        return typeof parsed.license === 'string' ? parsed.license : null;
    });
}
// ---------------------------------------------------------------------------
// PyPI registry
// ---------------------------------------------------------------------------
/**
 * Fetch the license string for a PyPI package version.
 * GET https://pypi.org/pypi/${pkg}/${version}/json
 * Extracts info.license field.
 * Returns null on any error.
 */
export async function fetchPypiLicense(pkg, version) {
    return makeHttpsGet(`https://pypi.org/pypi/${encodeURIComponent(pkg)}/${encodeURIComponent(version)}/json`, (raw) => {
        const parsed = JSON.parse(raw);
        const license = parsed.info?.license;
        return typeof license === 'string' && license.length > 0 ? license : null;
    });
}
// ---------------------------------------------------------------------------
// crates.io registry
// ---------------------------------------------------------------------------
/**
 * Fetch the license string for a Rust crate version.
 * GET https://crates.io/api/v1/crates/${pkg}/${version}
 * Extracts version.license field.
 * Returns null on any error.
 */
export async function fetchCratesLicense(pkg, version) {
    return makeHttpsGet(`https://crates.io/api/v1/crates/${encodeURIComponent(pkg)}/${encodeURIComponent(version)}`, (raw) => {
        const parsed = JSON.parse(raw);
        const license = parsed.version?.license;
        return typeof license === 'string' && license.length > 0 ? license : null;
    });
}
// ---------------------------------------------------------------------------
// Shared HTTP helper
// ---------------------------------------------------------------------------
async function makeHttpsGet(url, parser) {
    return new Promise((resolve) => {
        import('node:https')
            .then(({ default: https }) => {
            /* v8 ignore start */
            const req = https.get(url, { timeout: TIMEOUT_MS }, (res) => {
                let data = '';
                res.on('data', (chunk) => {
                    data += chunk.toString();
                });
                res.on('end', () => {
                    try {
                        resolve(parser(data));
                    }
                    catch {
                        resolve(null);
                    }
                });
            });
            req.on('error', () => {
                resolve(null);
            });
            req.on('timeout', () => {
                req.destroy();
                resolve(null);
            });
            /* v8 ignore stop */
        })
            .catch(() => {
            resolve(null);
        });
    });
}
//# sourceMappingURL=license-registry.js.map