@@ -77,6 +77,37 @@ export default {
7777
7878 const maybeSecondRequest = request . clone ( ) ;
7979
80+ const routeToUserWorker = async ( { asset } : { asset : "static_routing" | "none" } ) => {
81+ if ( ! config . has_user_worker ) {
82+ throw new Error (
83+ "Fetch for user worker without having a user worker binding"
84+ ) ;
85+ }
86+ analytics . setData ( { dispatchtype : DISPATCH_TYPE . WORKER } ) ;
87+ userWorkerInvocation = true ;
88+ return env . JAEGER . enterSpan ( "dispatch_worker" , async ( span ) => {
89+ span . setTags ( {
90+ hasUserWorker : true ,
91+ asset : asset ,
92+ dispatchType : DISPATCH_TYPE . WORKER ,
93+ } ) ;
94+ return env . USER_WORKER . fetch ( maybeSecondRequest ) ;
95+ } ) ;
96+ } ;
97+
98+ const routeToAssets = async ( { asset } : { asset : "static_routing" | "found" | "none" } ) => {
99+ analytics . setData ( { dispatchtype : DISPATCH_TYPE . ASSETS } ) ;
100+ return await env . JAEGER . enterSpan ( "dispatch_assets" , async ( span ) => {
101+ span . setTags ( {
102+ hasUserWorker : config . has_user_worker ,
103+ asset : asset ,
104+ dispatchType : DISPATCH_TYPE . ASSETS ,
105+ } ) ;
106+
107+ return env . ASSET_WORKER . fetch ( maybeSecondRequest ) ;
108+ } ) ;
109+ } ;
110+
80111 if ( config . static_routing ) {
81112 // evaluate "exclude" rules
82113 const excludeRulesMatcher = generateStaticRoutingRuleMatcher (
@@ -89,18 +120,9 @@ export default {
89120 ) {
90121 // direct to asset worker
91122 analytics . setData ( {
92- dispatchtype : DISPATCH_TYPE . ASSETS ,
93123 staticRoutingDecision : STATIC_ROUTING_DECISION . ROUTED ,
94124 } ) ;
95- return await env . JAEGER . enterSpan ( "dispatch_assets" , async ( span ) => {
96- span . setTags ( {
97- hasUserWorker : config . has_user_worker ,
98- asset : "static_routing" ,
99- dispatchType : DISPATCH_TYPE . ASSETS ,
100- } ) ;
101-
102- return env . ASSET_WORKER . fetch ( maybeSecondRequest ) ;
103- } ) ;
125+ return await routeToAssets ( { asset : "static_routing" } ) ;
104126 }
105127 // evaluate "include" rules
106128 const includeRulesMatcher = generateStaticRoutingRuleMatcher (
@@ -118,19 +140,9 @@ export default {
118140 }
119141 // direct to user worker
120142 analytics . setData ( {
121- dispatchtype : DISPATCH_TYPE . WORKER ,
122143 staticRoutingDecision : STATIC_ROUTING_DECISION . ROUTED ,
123144 } ) ;
124- return await env . JAEGER . enterSpan ( "dispatch_worker" , async ( span ) => {
125- span . setTags ( {
126- hasUserWorker : true ,
127- asset : "static_routing" ,
128- dispatchType : DISPATCH_TYPE . WORKER ,
129- } ) ;
130-
131- userWorkerInvocation = true ;
132- return env . USER_WORKER . fetch ( maybeSecondRequest ) ;
133- } ) ;
145+ return await routeToUserWorker ( { asset : "static_routing" } ) ;
134146 }
135147
136148 analytics . setData ( {
@@ -143,53 +155,17 @@ export default {
143155 // User's configuration indicates they want user-Worker to run ahead of any
144156 // assets. Do not provide any fallback logic.
145157 if ( config . invoke_user_worker_ahead_of_assets ) {
146- if ( ! config . has_user_worker ) {
147- throw new Error (
148- "Fetch for user worker without having a user worker binding"
149- ) ;
150- }
151-
152- analytics . setData ( { dispatchtype : DISPATCH_TYPE . WORKER } ) ;
153- return await env . JAEGER . enterSpan ( "dispatch_worker" , async ( span ) => {
154- span . setTags ( {
155- hasUserWorker : true ,
156- asset : "ignored" ,
157- dispatchType : DISPATCH_TYPE . WORKER ,
158- } ) ;
159-
160- userWorkerInvocation = true ;
161- return env . USER_WORKER . fetch ( maybeSecondRequest ) ;
162- } ) ;
158+ return await routeToUserWorker ( { asset : "static_routing" } ) ;
163159 }
164160
165161 // If we have a user-Worker, but no assets, dispatch to Worker script
166162 const assetsExist = await env . ASSET_WORKER . unstable_canFetch ( request ) ;
167163 if ( config . has_user_worker && ! assetsExist ) {
168- analytics . setData ( { dispatchtype : DISPATCH_TYPE . WORKER } ) ;
169-
170- return await env . JAEGER . enterSpan ( "dispatch_worker" , async ( span ) => {
171- span . setTags ( {
172- hasUserWorker : config . has_user_worker ,
173- asset : assetsExist ,
174- dispatchType : DISPATCH_TYPE . WORKER ,
175- } ) ;
176-
177- userWorkerInvocation = true ;
178- return env . USER_WORKER . fetch ( maybeSecondRequest ) ;
179- } ) ;
164+ return await routeToUserWorker ( { asset : "none" } ) ;
180165 }
181166
182167 // Otherwise, we either don't have a user worker, OR we have matching assets and should fetch from the assets binding
183- analytics . setData ( { dispatchtype : DISPATCH_TYPE . ASSETS } ) ;
184- return await env . JAEGER . enterSpan ( "dispatch_assets" , async ( span ) => {
185- span . setTags ( {
186- hasUserWorker : config . has_user_worker ,
187- asset : assetsExist ,
188- dispatchType : DISPATCH_TYPE . ASSETS ,
189- } ) ;
190-
191- return env . ASSET_WORKER . fetch ( maybeSecondRequest ) ;
192- } ) ;
168+ return await routeToAssets ( { asset : assetsExist ? "found" : "none" } ) ;
193169 } catch ( err ) {
194170 if ( userWorkerInvocation ) {
195171 // Don't send user Worker errors to sentry; we have no way to distinguish between
0 commit comments