9
9
finish,
10
10
getHeadersSent,
11
11
getOutgoing,
12
- getReadyReadableStreamState,
13
12
getRequestHeader,
14
13
getRequestMethod,
15
14
getRequestURL,
@@ -135,30 +134,33 @@ const MAX_MAX_AGE = 31536000000;
135
134
*/
136
135
function wrapper ( context ) {
137
136
return async function middleware ( req , res , next ) {
138
- const acceptedMethods = context . options . methods || [ "GET" , "HEAD" ] ;
139
-
140
- initState ( res ) ;
141
-
142
137
/**
138
+ * @param {NodeJS.ErrnoException= } err an error
143
139
* @returns {Promise<void> }
144
140
*/
145
- async function goNext ( ) {
141
+ async function goNext ( err ) {
146
142
if ( ! context . options . serverSideRender ) {
147
- return next ( ) ;
143
+ return next ( err ) ;
148
144
}
149
145
150
146
return new Promise ( ( resolve ) => {
151
147
ready (
152
148
context ,
153
149
( ) => {
154
150
setState ( res , "webpack" , { devMiddleware : context } ) ;
155
- resolve ( next ( ) ) ;
151
+ resolve ( next ( err ) ) ;
156
152
} ,
157
153
req ,
158
154
) ;
159
155
} ) ;
160
156
}
161
157
158
+ const acceptedMethods = context . options . methods || [ "GET" , "HEAD" ] ;
159
+ // TODO do we need an option here?
160
+ const forwardError = false ;
161
+
162
+ initState ( res ) ;
163
+
162
164
const method = getRequestMethod ( req ) ;
163
165
164
166
if ( method && ! acceptedMethods . includes ( method ) ) {
@@ -167,11 +169,21 @@ function wrapper(context) {
167
169
}
168
170
169
171
/**
172
+ * @param {NodeJS.ErrnoException } err ann error
170
173
* @param {number } status status
171
174
* @param {Partial<SendErrorOptions<Request, Response>>= } options options
172
- * @returns {void }
175
+ * @returns {Promise< void> }
173
176
*/
174
- function sendError ( status , options ) {
177
+ async function sendError ( err , status , options ) {
178
+ if ( forwardError ) {
179
+ const error =
180
+ /** @type {Error & { statusCode: number } } */
181
+ ( new Error ( err . message ) ) ;
182
+ error . statusCode = status ;
183
+
184
+ await goNext ( error ) ;
185
+ }
186
+
175
187
const escapeHtml = require ( "./utils/escapeHtml" ) ;
176
188
177
189
const content = statuses [ status ] || String ( status ) ;
@@ -230,19 +242,19 @@ function wrapper(context) {
230
242
231
243
/**
232
244
* @param {NodeJS.ErrnoException } error error
233
- * @returns {void }
245
+ * @returns {Promise< void> }
234
246
*/
235
- function errorHandler ( error ) {
247
+ async function errorHandler ( error ) {
236
248
switch ( error . code ) {
237
249
case "ENAMETOOLONG" :
238
250
case "ENOENT" :
239
251
case "ENOTDIR" :
240
- sendError ( 404 , {
252
+ await sendError ( error , 404 , {
241
253
modifyResponseData : context . options . modifyResponseData ,
242
254
} ) ;
243
255
break ;
244
256
default :
245
- sendError ( 500 , {
257
+ await sendError ( error , 500 , {
246
258
modifyResponseData : context . options . modifyResponseData ,
247
259
} ) ;
248
260
break ;
@@ -496,10 +508,15 @@ function wrapper(context) {
496
508
context . logger . error ( `Malicious path "${ filename } ".` ) ;
497
509
}
498
510
499
- sendError ( extra . errorCode , {
500
- modifyResponseData : context . options . modifyResponseData ,
501
- } ) ;
502
- await goNext ( ) ;
511
+ await sendError (
512
+ extra . errorCode === 400
513
+ ? new Error ( "Bad Request" )
514
+ : new Error ( "Forbidden" ) ,
515
+ extra . errorCode ,
516
+ {
517
+ modifyResponseData : context . options . modifyResponseData ,
518
+ } ,
519
+ ) ;
503
520
return ;
504
521
}
505
522
@@ -649,8 +666,7 @@ function wrapper(context) {
649
666
value = result . bufferOrStream ;
650
667
( { bufferOrStream, byteLength } = result ) ;
651
668
} catch ( error ) {
652
- errorHandler ( /** @type {NodeJS.ErrnoException } */ ( error ) ) ;
653
- await goNext ( ) ;
669
+ await errorHandler ( /** @type {NodeJS.ErrnoException } */ ( error ) ) ;
654
670
return ;
655
671
}
656
672
}
@@ -691,10 +707,9 @@ function wrapper(context) {
691
707
// Conditional GET support
692
708
if ( isConditionalGET ( ) ) {
693
709
if ( isPreconditionFailure ( ) ) {
694
- sendError ( 412 , {
710
+ await sendError ( new Error ( "Precondition Failed" ) , 412 , {
695
711
modifyResponseData : context . options . modifyResponseData ,
696
712
} ) ;
697
- await goNext ( ) ;
698
713
return ;
699
714
}
700
715
@@ -744,13 +759,12 @@ function wrapper(context) {
744
759
getValueContentRangeHeader ( "bytes" , size ) ,
745
760
) ;
746
761
747
- sendError ( 416 , {
762
+ await sendError ( new Error ( "Range Not Satisfiable" ) , 416 , {
748
763
headers : {
749
764
"Content-Range" : getResponseHeader ( res , "Content-Range" ) ,
750
765
} ,
751
766
modifyResponseData : context . options . modifyResponseData ,
752
767
} ) ;
753
- await goNext ( ) ;
754
768
return ;
755
769
} else if ( parsedRanges === - 2 ) {
756
770
context . logger . error (
@@ -793,8 +807,7 @@ function wrapper(context) {
793
807
end ,
794
808
) ) ;
795
809
} catch ( error ) {
796
- errorHandler ( /** @type {NodeJS.ErrnoException } */ ( error ) ) ;
797
- await goNext ( ) ;
810
+ await errorHandler ( /** @type {NodeJS.ErrnoException } */ ( error ) ) ;
798
811
return ;
799
812
}
800
813
}
@@ -823,7 +836,6 @@ function wrapper(context) {
823
836
}
824
837
825
838
finish ( res ) ;
826
- await goNext ( ) ;
827
839
return ;
828
840
}
829
841
@@ -838,7 +850,6 @@ function wrapper(context) {
838
850
839
851
if ( ! isPipeSupports ) {
840
852
send ( res , /** @type {Buffer } */ ( bufferOrStream ) ) ;
841
- await goNext ( ) ;
842
853
return ;
843
854
}
844
855
@@ -852,16 +863,11 @@ function wrapper(context) {
852
863
853
864
// Error handling
854
865
/** @type {import("fs").ReadStream } */
855
- ( bufferOrStream )
856
- . on ( "error" , ( error ) => {
857
- // clean up stream early
858
- cleanup ( ) ;
859
- errorHandler ( error ) ;
860
- goNext ( ) ;
861
- } )
862
- . on ( getReadyReadableStreamState ( res ) , ( ) => {
863
- goNext ( ) ;
864
- } ) ;
866
+ ( bufferOrStream ) . on ( "error" , ( error ) => {
867
+ // clean up stream early
868
+ cleanup ( ) ;
869
+ errorHandler ( error ) ;
870
+ } ) ;
865
871
866
872
pipe ( res , /** @type {ReadStream } */ ( bufferOrStream ) ) ;
867
873
0 commit comments