depshield
Version:
Smart Dependency Analyzer & Optimizer - Find unused npm packages, reduce bundle size, and improve project health with AST-based detection.
57 lines (56 loc) • 1.92 kB
JavaScript
import https from 'https';
export async function getPackageSize(packageName, version) {
return new Promise((resolve) => {
// Use bundlephobia API
const url = `https://bundlephobia.com/api/size?package=${packageName}@${version}`;
https.get(url, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
try {
const parsed = JSON.parse(data);
resolve({
name: packageName,
version,
size: parsed.size || 0,
gzip: parsed.gzip || 0,
});
}
catch {
resolve(null);
}
});
}).on('error', () => {
resolve(null);
});
});
}
export async function getPackageSizes(packages) {
const sizeMap = new Map();
// Fetch sizes in parallel with a limit to avoid rate limiting
const BATCH_SIZE = 5;
for (let i = 0; i < packages.length; i += BATCH_SIZE) {
const batch = packages.slice(i, i + BATCH_SIZE);
const results = await Promise.all(batch.map(pkg => getPackageSize(pkg.name, pkg.version)));
results.forEach((result, idx) => {
if (result) {
sizeMap.set(batch[idx].name, result);
}
});
// Small delay between batches
if (i + BATCH_SIZE < packages.length) {
await new Promise(resolve => setTimeout(resolve, 500));
}
}
return sizeMap;
}
export function formatBytes(bytes) {
if (bytes === 0)
return '0 B';
const k = 1024;
const sizes = ['B', 'KB', 'MB', 'GB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return `${(bytes / Math.pow(k, i)).toFixed(1)} ${sizes[i]}`;
}