UNPKG

pushduck

Version:

The fastest way to add file uploads to any web application. Enterprise security, edge-ready. Works with 16+ frameworks and 5+ storage providers. No heavy AWS SDK required.

16 lines (14 loc) 7 kB
const e=require(`./chunk-CEIYHHoD.js`),t=require(`./upload-config-E8LfxmuC.js`),n=require(`./client-CrLuHZwl.js`);var r=e.__toESM(n.require_defineProperty(),1),i=class{constructor(){(0,r.default)(this,`checks`,[])}async runHealthChecks(e){this.checks=[];let t=Date.now();n.logger.info(`Starting health checks`),await this.checkConfiguration(e),await this.checkEnvironment(),e&&await this.checkConnectivity(e),await this.checkPerformance();let r=this.calculateSummary(),i=this.determineOverallStatus(r),a=Date.now()-t;return n.logger.info(`Health checks completed in ${a}ms`,{status:i,summary:r}),{status:i,checks:[...this.checks],summary:r,timestamp:new Date}}async checkConfiguration(e){let t=performance.now();try{if(!e){this.addCheck(`Configuration`,`warn`,`No configuration provided for validation`);return}let t=[`provider`,`accessKeyId`,`secretAccessKey`,`bucket`],n=t.filter(t=>!e[t]);if(n.length>0){this.addCheck(`Configuration`,`fail`,`Missing required fields: ${n.join(`, `)}`,{missingFields:n});return}let r=this.validateProviderConfig(e);if(!r.valid){this.addCheck(`Configuration`,`fail`,`Provider configuration invalid: ${r.errors.join(`, `)}`,{errors:r.errors});return}this.addCheck(`Configuration`,`pass`,`Configuration is valid`)}catch(e){this.addCheck(`Configuration`,`fail`,e instanceof Error?e.message:`Configuration check failed`)}finally{this.updateCheckDuration(`Configuration`,performance.now()-t)}}async checkEnvironment(){let e=performance.now();try{let e=process.version,t=parseInt(e.slice(1).split(`.`)[0]);t<16?this.addCheck(`Node.js Version`,`fail`,`Node.js ${e} is not supported. Minimum version: 16.0.0`):t<18?this.addCheck(`Node.js Version`,`warn`,`Node.js ${e} is supported but consider upgrading to v18+ for better performance`):this.addCheck(`Node.js Version`,`pass`,`Node.js ${e} is supported`);let n=process.memoryUsage(),r=Math.round(n.heapUsed/1024/1024),i=Math.round(n.heapTotal/1024/1024);r>512?this.addCheck(`Memory Usage`,`warn`,`High memory usage: ${r}MB used of ${i}MB total`,{memoryUsage:n}):this.addCheck(`Memory Usage`,`pass`,`Memory usage is normal: ${r}MB used of ${i}MB total`),typeof performance>`u`?this.addCheck(`Performance API`,`warn`,`Performance API not available - metrics collection may be limited`):this.addCheck(`Performance API`,`pass`,`Performance API is available`)}catch(e){this.addCheck(`Environment`,`fail`,e instanceof Error?e.message:`Environment check failed`)}finally{this.updateCheckDuration(`Environment`,performance.now()-e)}}async checkConnectivity(e){let t=performance.now();try{let{validateS3Connection:t}=await Promise.resolve().then(()=>require(`./client-DIoAjxid.js`)),n=await t(e);n.success?this.addCheck(`S3 Connectivity`,`pass`,`Successfully connected to S3`):this.addCheck(`S3 Connectivity`,`fail`,`Failed to connect to S3: ${n.error}`,{error:n.error})}catch(e){this.addCheck(`S3 Connectivity`,`fail`,e instanceof Error?e.message:`Connectivity check failed`)}finally{this.updateCheckDuration(`S3 Connectivity`,performance.now()-t)}}async checkPerformance(){let e=performance.now();try{if(!t.metrics.isMetricsEnabled()){this.addCheck(`Performance Metrics`,`warn`,`Metrics collection is disabled`);return}let e=t.metrics.getAggregatedMetrics();if(e.totalOperations===0){this.addCheck(`Performance Metrics`,`pass`,`No operations recorded yet`);return}e.successRate<90?this.addCheck(`Success Rate`,`warn`,`Success rate is ${e.successRate.toFixed(1)}% (below 90%)`,{metrics:e}):this.addCheck(`Success Rate`,`pass`,`Success rate is ${e.successRate.toFixed(1)}%`),e.averageDuration>5e3?this.addCheck(`Performance`,`warn`,`Average operation duration is ${e.averageDuration.toFixed(1)}ms (above 5s)`,{metrics:e}):this.addCheck(`Performance`,`pass`,`Average operation duration is ${e.averageDuration.toFixed(1)}ms`)}catch(e){this.addCheck(`Performance Metrics`,`fail`,e instanceof Error?e.message:`Performance check failed`)}finally{this.updateCheckDuration(`Performance Metrics`,performance.now()-e)}}validateProviderConfig(e){let t=[];switch(e.provider.provider){case`aws`:e.provider.region||t.push(`AWS region is required`);break;case`cloudflare-r2`:e.provider.endpoint||t.push(`Cloudflare R2 endpoint is required`);break;case`digitalocean-spaces`:e.provider.endpoint||t.push(`DigitalOcean Spaces endpoint is required`);break;case`minio`:e.provider.endpoint||t.push(`MinIO endpoint is required`);break;case`s3-compatible`:e.provider.endpoint||t.push(`S3-compatible endpoint is required`);break;default:t.push(`Unsupported provider: ${e.provider.provider}`)}return{valid:t.length===0,errors:t}}addCheck(e,t,n,r){this.checks.push({name:e,status:t,message:n,details:r})}updateCheckDuration(e,t){let n=this.checks.find(t=>t.name===e);n&&(n.duration=t)}calculateSummary(){let e=this.checks.length,t=this.checks.filter(e=>e.status===`pass`).length,n=this.checks.filter(e=>e.status===`warn`).length,r=this.checks.filter(e=>e.status===`fail`).length;return{total:e,passed:t,warnings:n,failures:r}}determineOverallStatus(e){return e.failures>0?`error`:e.warnings>0?`warning`:`healthy`}getHealthReport(e){let t={healthy:`✅`,warning:`⚠️`,error:`❌`},n={pass:`✅`,warn:`⚠️`,fail:`❌`};return` ${t[e.status]} Pushduck Health Check Report ========================================== Status: ${e.status.toUpperCase()} Timestamp: ${e.timestamp.toISOString()} Summary: - Total Checks: ${e.summary.total} - Passed: ${e.summary.passed} - Warnings: ${e.summary.warnings} - Failures: ${e.summary.failures} Detailed Results: ${e.checks.map(e=>{let t=e.duration?` (${e.duration.toFixed(1)}ms)`:``;return`${n[e.status]} ${e.name}: ${e.message}${t}`}).join(` `)} `.trim()}};const a=new i,o=e=>a.runHealthChecks(e),s=e=>a.getHealthReport(e);exports.PushduckError=n.PushduckError,exports.S3ArraySchema=t.S3ArraySchema,exports.S3FileSchema=t.S3FileSchema,exports.S3ImageSchema=t.S3ImageSchema,exports.S3ObjectSchema=t.S3ObjectSchema,exports.S3Route=t.S3Route,exports.S3Schema=t.S3Schema,exports.StorageInstance=t.StorageInstance,exports.createConfigError=n.createConfigError,exports.createFileError=n.createFileError,exports.createNetworkError=n.createNetworkError,exports.createProvider=t.createProvider,exports.createS3Client=n.createS3Client,exports.createS3Error=n.createS3Error,exports.createS3RouterWithConfig=t.createS3RouterWithConfig,exports.createStorage=t.createStorage,exports.createUploadConfig=t.createUploadConfig,exports.endOperation=t.endOperation,exports.getHealthReport=s,exports.getProviderEndpoint=t.getProviderEndpoint,exports.healthChecker=a,exports.isConfigError=n.isConfigError,exports.isFileError=n.isFileError,exports.isPushduckError=n.isPushduckError,exports.isS3Error=n.isS3Error,exports.logger=n.logger,exports.metrics=t.metrics,exports.recordOperation=t.recordOperation,exports.resetS3Client=n.resetS3Client,exports.runHealthCheck=o,exports.startOperation=t.startOperation,exports.trackOperation=t.trackOperation,exports.validateProviderConfig=t.validateProviderConfig,exports.wrapAsync=n.wrapAsync;