Advanced scenarios, error handling patterns, and edge cases for npq.
const Marshall = require('npq/lib/marshall');
// Handle network timeouts
async function auditWithTimeout(packages, timeoutMs = 30000) {
const marshall = new Marshall({ pkgs: packages });
const timeoutPromise = new Promise((_, reject) => {
setTimeout(() => reject(new Error('Audit timeout')), timeoutMs);
});
try {
const results = await Promise.race([
marshall.run(),
timeoutPromise
]);
return results;
} catch (error) {
if (error.message === 'Audit timeout') {
console.error('Security audit timed out');
throw error;
}
throw error;
}
}// Handle partial failures
async function auditWithPartialResults(packages) {
const marshall = new Marshall({ pkgs: packages });
try {
const results = await marshall.run();
return results;
} catch (error) {
// Even if some marshalls fail, return partial results
if (error.partialResults) {
console.warn('Some marshalls failed, returning partial results');
return error.partialResults;
}
throw error;
}
}// Handle rate limiting
async function auditWithRetry(packages, maxRetries = 3) {
const marshall = new Marshall({ pkgs: packages });
for (let attempt = 1; attempt <= maxRetries; attempt++) {
try {
return await marshall.run();
} catch (error) {
if (error.statusCode === 429 && attempt < maxRetries) {
const delay = Math.pow(2, attempt) * 1000; // Exponential backoff
console.warn(`Rate limited, retrying in ${delay}ms...`);
await new Promise(resolve => setTimeout(resolve, delay));
continue;
}
throw error;
}
}
}const Marshall = require('npq/lib/marshall');
try {
const marshall = new Marshall({
pkgs: ['nonexistent-package@latest']
});
const results = await marshall.run();
} catch (error) {
if (error.code === 'PACKAGE_NOT_FOUND') {
console.error(`Package not found: ${error.packageName}`);
} else if (error.code === 'USER_ABORT') {
console.log('Operation aborted by user');
} else if (error.code === 'ENOTFOUND' || error.code === 'ECONNREFUSED') {
console.error('Network error:', error.message);
console.error('URL:', error.url);
} else if (error.code === 'EMISSINGSIGNATUREKEY') {
console.error('Missing signature key for package verification');
} else if (error.code === 'EEXPIREDSIGNATUREKEY') {
console.warn('Expired signature key detected');
} else if (error.code === 'EINTEGRITYSIGNATURE') {
console.error('Invalid package signature detected');
} else {
console.error('Unexpected error:', error.message);
console.error('Error code:', error.code);
console.error('Stack:', error.stack);
}
}const Marshall = require('npq/lib/marshall');
const ora = require('ora');
// Create spinner for progress indication
const spinner = ora('Running security checks...').start();
const marshall = new Marshall({
pkgs: ['express@latest'],
progressManager: spinner
});
try {
const results = await marshall.run();
spinner.succeed('Security checks completed');
// Process results...
} catch (error) {
spinner.fail('Security checks failed');
throw error;
}time metadata: Falls back to version-specific time_npmUser field as fallback