@@ -2,7 +2,7 @@ import * as http2 from 'node:http2';
22import send , { SendOptions } from 'send' ;
33import { Readable } from 'stream' ;
44import { IncomingMessage , ServerResponse , IncomingHttpHeaders as NodeIncomingHeaders , OutgoingHttpHeaders , OutgoingHttpHeader } from 'node:http' ;
5- import { is } from './utils' ;
5+ import { caughtPromise , is } from './utils' ;
66import { createReadStream } from 'node:fs' ;
77import { Writable } from 'node:stream' ;
88import Debug from "debug" ;
@@ -361,19 +361,32 @@ export class Streamer {
361361 } ) ;
362362 return new Promise < typeof STREAM_ENDED > ( ( resolve , reject ) => {
363363
364- sender . on ( "error" , ( err ) => Promise . resolve ( ) . then ( async ( ) : Promise < typeof STREAM_ENDED > => {
364+ sender . on ( "error" , caughtPromise ( async ( err ) => {
365+ interface SendError {
366+ errno : - 2 ,
367+ code : 'ENOENT' ,
368+ syscall : 'stat' ,
369+ /** The absolute file path that was resolved and didn't exist */
370+ path : string ,
371+ expose : boolean ,
372+ /** status and statusCode are identical */
373+ statusCode : number ,
374+ /** status and statusCode are identical */
375+ status : number
376+ }
365377 if ( err === 404 || err ?. statusCode === 404 ) {
366- return ( await on404 ?.( ) ) ?? this . sendEmpty ( 404 ) ;
378+ if ( on404 ) on404 ( ) ;
379+ else this . sendEmpty ( 404 ) ;
367380 } else {
368381 console . log ( err ) ;
369- throw this . sendEmpty ( 500 ) ;
382+ this . sendEmpty ( 500 ) ;
370383 }
371- } ) . then ( resolve , reject ) ) ;
384+ } , reject ) ) ;
372385
373- sender . on ( "directory" , ( ) => Promise . resolve ( ) . then ( async ( ) : Promise < typeof STREAM_ENDED > => {
374- return ( await onDir ?. ( ) )
375- ?? this . sendEmpty ( 404 , { "x-reason" : "Directory listing not allowed" } )
376- } ) . then ( resolve , reject ) ) ;
386+ sender . on ( "directory" , caughtPromise ( async ( ) => {
387+ if ( onDir ) onDir ( ) ;
388+ else this . sendEmpty ( 404 , { "x-reason" : "Directory listing not allowed" } )
389+ } , reject ) ) ;
377390
378391 sender . on ( "stream" , ( fileStream ) => {
379392 this . compressor . beforeWriteHead ( ) ;
0 commit comments