UNPKG

snyk-mvn-plugin

Version:
66 lines 2.79 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CONCURRENCY = void 0; exports.collectM2Nodes = collectM2Nodes; exports.mapNodes = mapNodes; exports.buildLabelMap = buildLabelMap; const fingerprint_1 = require("../fingerprint"); /** * Number of artifacts processed per batch. We batch to avoid spawning too many * concurrent file reads. Note a single node may itself fan out into several * reads (e.g. one companion file per hash algorithm), so the in-flight read * count can be a small multiple of this. */ exports.CONCURRENCY = 5; /** * Collect the unique node IDs across all graphs and resolve each to its * artifact path in the local Maven repository. Done once so every per-node * pass (hashes, distribution URLs) can reuse the same node set and paths. */ function collectM2Nodes(mavenGraphs, repositoryPath) { const nodeIds = new Set(); for (const graph of mavenGraphs) { Object.keys(graph.nodes).forEach((nodeId) => nodeIds.add(nodeId)); } const nodes = []; for (const nodeId of nodeIds) { const artifactPath = (0, fingerprint_1.dependencyIdToArtifactPath)(nodeId, repositoryPath); // Drop nodes whose coordinate resolves outside the repository — the same // containment guard the fingerprint pass applies, so a crafted coordinate // can never drive the hash-label or distribution-url reads out of the repo. if (artifactPath !== undefined) { nodes.push({ nodeId, artifactPath }); } } return nodes; } /** * Run `read` for every node in bounded-concurrency batches and return a * Map<nodeId, result> for the nodes whose result `keep` accepts. Reads run * asynchronously in bounded batches so they never block the event loop. The * shared scaffold for any per-node async pass over the local Maven repository — * label sets, recorded repo ids, etc. */ async function mapNodes(nodes, read, keep) { const result = new Map(); for (let i = 0; i < nodes.length; i += exports.CONCURRENCY) { const batch = nodes.slice(i, i + exports.CONCURRENCY); const batchResults = await Promise.all(batch.map(read)); batch.forEach((node, j) => { const value = batchResults[j]; if (keep(value)) { result.set(node.nodeId, value); } }); } return result; } /** * Run `readLabels` for every node and return a Map<nodeId, labels> containing * only the nodes that produced a non-empty label set. Thin specialisation of * {@link mapNodes} for the per-node label-reading passes (hashes, etc.). */ function buildLabelMap(nodes, readLabels) { return mapNodes(nodes, readLabels, (labels) => Object.keys(labels).length > 0); } //# sourceMappingURL=m2-batch.js.map