@@ -33,17 +33,16 @@ declare const routes: RouteHandler[];
33
33
declare const __FALLBACK_SERVICE__ : string ;
34
34
35
35
// expect an ASSETS fetcher binding pointing to the asset-server stage
36
- type Env = {
37
- [ name : string ] : unknown ;
38
- ASSETS : { fetch ( url : string , init : RequestInit ) : Promise < Response > } ;
36
+ type FetchEnv = {
37
+ [ name : string ] : { fetch : typeof fetch } ;
38
+ ASSETS : { fetch : typeof fetch } ;
39
39
} ;
40
40
41
41
type WorkerContext = {
42
42
waitUntil : ( promise : Promise < unknown > ) => void ;
43
43
} ;
44
44
45
- // eslint-disable-next-line @typescript-eslint/no-unused-vars -- `env` can be used by __FALLBACK_SERVICE_FETCH__
46
- function * executeRequest ( request : Request , env : Env ) {
45
+ function * executeRequest ( request : Request , _env : FetchEnv ) {
47
46
const requestPath = new URL ( request . url ) . pathname ;
48
47
49
48
// First, iterate through the routes (backwards) and execute "middlewares" on partial route matches
@@ -88,35 +87,22 @@ function* executeRequest(request: Request, env: Env) {
88
87
break ;
89
88
}
90
89
}
91
-
92
- // Finally, yield to the fallback service (`env.ASSETS.fetch` in Pages' case)
93
- return {
94
- handler : ( ) =>
95
- __FALLBACK_SERVICE__
96
- ? // @ts -expect-error expecting __FALLBACK_SERVICE__ to be the name of a service binding, so fetch should be defined
97
- env [ __FALLBACK_SERVICE__ ] . fetch ( request )
98
- : fetch ( request ) ,
99
- params : { } as Params ,
100
- } ;
101
90
}
102
91
103
92
export default {
104
- async fetch ( request : Request , env : Env , workerContext : WorkerContext ) {
93
+ async fetch ( request : Request , env : FetchEnv , workerContext : WorkerContext ) {
105
94
const handlerIterator = executeRequest ( request , env ) ;
106
95
const data = { } ; // arbitrary data the user can set between functions
107
96
const next = async ( input ?: RequestInfo , init ?: RequestInit ) => {
108
97
if ( input !== undefined ) {
109
98
request = new Request ( input , init ) ;
110
99
}
111
100
112
- const { value } = handlerIterator . next ( ) ;
113
- if ( value ) {
114
- const { handler, params } = value ;
115
- const context : EventContext <
116
- unknown ,
117
- string ,
118
- Record < string , unknown >
119
- > = {
101
+ const result = handlerIterator . next ( ) ;
102
+ // Note we can't use `!result.done` because this doesn't narrow to the correct type
103
+ if ( result . done == false ) {
104
+ const { handler, params } = result . value ;
105
+ const context = {
120
106
request : new Request ( request . clone ( ) ) ,
121
107
next,
122
108
params,
@@ -132,6 +118,12 @@ export default {
132
118
[ 101 , 204 , 205 , 304 ] . includes ( response . status ) ? null : response . body ,
133
119
response
134
120
) ;
121
+ } else if ( __FALLBACK_SERVICE__ ) {
122
+ // There are no more handlers so finish with the fallback service (`env.ASSETS.fetch` in Pages' case)
123
+ return env [ __FALLBACK_SERVICE__ ] . fetch ( request ) ;
124
+ } else {
125
+ // There was not fallback service so actually make the request to the origin.
126
+ return fetch ( request ) ;
135
127
}
136
128
} ;
137
129
0 commit comments