// src/lib/concurrency.ts
import { AsyncLocalStorage } from "async_hooks";
var outputStorage = new AsyncLocalStorage();
async function runWithConcurrency(items, concurrency, fn) {
  let next = 0;
  async function worker() {
    while (next < items.length) {
      const i = next++;
      await fn(items[i]);
    }
  }
  await Promise.all(Array.from({ length: Math.min(concurrency, items.length) }, () => worker()));
}

// src/lib/updo/fetchRegistryInfo.ts
var REGISTRY_BASE = "https://registry.npmjs.org";
async function fetchRegistryInfo(packageName) {
  try {
    const url = `${REGISTRY_BASE}/${encodeURIComponent(packageName)}`;
    const res = await fetch(url, { headers: { Accept: "application/vnd.npm.install-v1+json" } });
    if (!res.ok) {
      return { distTags: {}, versions: [] };
    }
    const data = await res.json();
    return {
      distTags: data["dist-tags"] ?? {},
      versions: Object.keys(data.versions ?? {})
    };
  } catch {
    return { distTags: {}, versions: [] };
  }
}
async function fetchAllRegistryInfo(packageNames, concurrency = 16) {
  const results = /* @__PURE__ */ new Map();
  let completed = 0;
  const total = packageNames.length;
  await runWithConcurrency(packageNames, concurrency, async (name) => {
    results.set(name, await fetchRegistryInfo(name));
    completed++;
    if (completed % 25 === 0 || completed === total) {
      process.stderr.write(`\r  Fetched ${completed}/${total} packages...`);
    }
  });
  if (total > 0) {
    process.stderr.write("\n");
  }
  return results;
}
export {
  fetchAllRegistryInfo,
  fetchRegistryInfo
};
//# sourceMappingURL=fetchRegistryInfo.mjs.map