@@ -25,46 +25,37 @@ function fastbootExpressMiddleware(distPath, options) {
2525 } ) ;
2626 }
2727
28- return function ( req , res , next ) {
28+ return async function ( req , res , next ) {
2929 let path = req . url ;
30- fastboot . visit ( path , { request : req , response : res } ) . then ( success , failure ) ;
31-
32- function success ( result ) {
33- let responseBody = opts . chunkedResponse ? result . chunks ( ) : result . html ( ) ;
34-
35- responseBody
36- . then ( body => {
37- let headers = result . headers ;
38- let statusMessage = result . error ? 'NOT OK ' : 'OK ' ;
39-
40- for ( var pair of headers . entries ( ) ) {
41- res . append ( pair [ 0 ] , pair [ 1 ] ) ;
42- }
43-
44- if ( result . error ) {
45- log ( 'RESILIENT MODE CAUGHT:' , result . error . stack ) ;
46- next ( result . error ) ;
47- }
48-
49- log ( result . statusCode , statusMessage + path ) ;
50- res . status ( result . statusCode ) ;
51-
52- if ( typeof body === 'string' ) {
53- res . send ( body ) ;
54- } else if ( result . error ) {
55- res . send ( body [ 0 ] ) ;
56- } else {
57- body . forEach ( chunk => res . write ( chunk ) ) ;
58- res . end ( ) ;
59- }
60- } )
61- . catch ( error => {
62- res . status ( 500 ) ;
63- next ( error ) ;
64- } ) ;
65- }
6630
67- function failure ( error ) {
31+ try {
32+ let result = await fastboot . visit ( path , { request : req , response : res } ) ;
33+ let body = opts . chunkedResponse ? await result . chunks ( ) : await result . html ( ) ;
34+
35+ if ( result . error ) {
36+ log ( 'RESILIENT MODE CAUGHT:' , result . error . stack ) ;
37+ next ( result . error ) ;
38+ }
39+
40+ let headers = result . headers ;
41+ let statusMessage = result . error ? 'NOT OK ' : 'OK ' ;
42+
43+ for ( var pair of headers . entries ( ) ) {
44+ res . append ( pair [ 0 ] , pair [ 1 ] ) ;
45+ }
46+
47+ log ( result . statusCode , statusMessage + path ) ;
48+ res . status ( result . statusCode ) ;
49+
50+ if ( typeof body === 'string' ) {
51+ res . send ( body ) ;
52+ } else if ( result . error ) {
53+ res . send ( body [ 0 ] ) ;
54+ } else {
55+ body . forEach ( chunk => res . write ( chunk ) ) ;
56+ res . end ( ) ;
57+ }
58+ } catch ( error ) {
6859 if ( error . name === 'UnrecognizedURLError' ) {
6960 next ( ) ;
7061 } else {
0 commit comments