@@ -46,33 +46,45 @@ export function createHandler<Context extends OperationContext = undefined>(
46
46
) : ( res : HttpResponse , req : HttpRequest ) => Promise < void > {
47
47
const handle = createRawHandler ( options ) ;
48
48
return async function requestListener ( res , req ) {
49
+ let aborted = false ;
50
+ res . onAborted ( ( ) => ( aborted = true ) ) ;
49
51
try {
52
+ let url = req . getUrl ( ) ;
53
+ const query = req . getQuery ( ) ;
54
+ if ( query ) {
55
+ url += '?' + query ;
56
+ }
50
57
const [ body , init ] = await handle ( {
51
- url : req . getUrl ( ) ,
52
- method : req . getMethod ( ) ,
53
- headers : { get : req . getHeader } ,
58
+ url,
59
+ method : req . getMethod ( ) . toUpperCase ( ) ,
60
+ headers : { get : ( key ) => req . getHeader ( key ) } ,
54
61
body : ( ) =>
55
62
new Promise < string > ( ( resolve ) => {
56
63
let body = '' ;
57
- res . onData ( ( chunk , isLast ) => {
58
- body += chunk ;
59
- if ( isLast ) {
60
- resolve ( body ) ;
61
- }
62
- } ) ;
64
+ if ( aborted ) {
65
+ resolve ( body ) ;
66
+ } else {
67
+ res . onData ( ( chunk , isLast ) => {
68
+ body += Buffer . from ( chunk , 0 , chunk . byteLength ) . toString ( ) ;
69
+ if ( isLast ) {
70
+ resolve ( body ) ;
71
+ }
72
+ } ) ;
73
+ }
63
74
} ) ,
64
75
raw : req ,
65
76
context : { res } ,
66
77
} ) ;
67
-
68
- res . writeStatus ( `${ init . status } ${ init . statusText } ` ) ;
69
- for ( const [ key , val ] of Object . entries ( init . headers || { } ) ) {
70
- res . writeHeader ( key , val ) ;
71
- }
72
- if ( body ) {
73
- res . end ( body ) ;
74
- } else {
75
- res . endWithoutBody ( ) ;
78
+ if ( ! aborted ) {
79
+ res . writeStatus ( `${ init . status } ${ init . statusText } ` ) ;
80
+ for ( const [ key , val ] of Object . entries ( init . headers || { } ) ) {
81
+ res . writeHeader ( key , val ) ;
82
+ }
83
+ if ( body ) {
84
+ res . end ( body ) ;
85
+ } else {
86
+ res . endWithoutBody ( ) ;
87
+ }
76
88
}
77
89
} catch ( err ) {
78
90
// The handler shouldnt throw errors.
@@ -82,7 +94,9 @@ export function createHandler<Context extends OperationContext = undefined>(
82
94
'Please check your implementation.' ,
83
95
err ,
84
96
) ;
85
- res . writeStatus ( '500 Internal Server Error' ) . endWithoutBody ( ) ;
97
+ if ( ! aborted ) {
98
+ res . writeStatus ( '500 Internal Server Error' ) . endWithoutBody ( ) ;
99
+ }
86
100
}
87
101
} ;
88
102
}
0 commit comments