@@ -44,52 +44,49 @@ import { NoopSpanProcessor } from '@opentelemetry/sdk-trace-base'
44
44
* @import { Environment } from './bindings.js'
45
45
*/
46
46
47
- const handler = {
48
- /** @type {Handler<Context, Environment> } */
49
- fetch ( request , env , ctx ) {
50
- console . log ( request . method , request . url )
51
- const middleware = composeMiddleware (
52
- // Prepare the Context
53
- withCdnCache ,
54
- withContext ,
55
- withCorsHeaders ,
56
- withVersionHeader ,
57
- withErrorHandler ,
58
- withParsedIpfsUrl ,
59
- createWithHttpMethod ( 'GET' , 'HEAD' ) ,
60
- withAuthToken ,
61
- withLocator ,
62
- withGatewayIdentity ,
63
- // TODO: replace this with a handler to fetch the real delegations
64
- withDelegationStubs ,
47
+ /**
48
+ * The middleware stack
49
+ */
50
+ const middleware = composeMiddleware (
51
+ // Prepare the Context
52
+ withCdnCache ,
53
+ withContext ,
54
+ withCorsHeaders ,
55
+ withVersionHeader ,
56
+ withErrorHandler ,
57
+ withParsedIpfsUrl ,
58
+ createWithHttpMethod ( 'GET' , 'HEAD' ) ,
59
+ withAuthToken ,
60
+ withLocator ,
61
+ withGatewayIdentity ,
62
+ // TODO: replace this with a handler to fetch the real delegations
63
+ withDelegationStubs ,
65
64
66
- // Rate-limit requests
67
- withRateLimit ,
65
+ // Rate-limit requests
66
+ withRateLimit ,
68
67
69
- // Fetch CAR data - Double-check why this can't be placed after the authorized space middleware
70
- withCarBlockHandler ,
68
+ // Fetch CAR data - Double-check why this can't be placed after the authorized space middleware
69
+ withCarBlockHandler ,
71
70
72
- // Authorize requests
73
- withAuthorizedSpace ,
71
+ // Authorize requests
72
+ withAuthorizedSpace ,
74
73
75
- // Track Egress
76
- withEgressClient ,
77
- withEgressTracker ,
74
+ // Track Egress
75
+ withEgressClient ,
76
+ withEgressTracker ,
78
77
79
- // Fetch data
80
- withContentClaimsDagula ,
81
- withFormatRawHandler ,
82
- withFormatCarHandler ,
78
+ // Fetch data
79
+ withContentClaimsDagula ,
80
+ withFormatRawHandler ,
81
+ withFormatCarHandler ,
83
82
84
- // Prepare the Response
85
- withContentDispositionHeader ,
86
- withFixedLengthStream
87
- )
88
- return middleware ( handleUnixfs ) ( request , env , ctx )
89
- }
90
- }
83
+ // Prepare the Response
84
+ withContentDispositionHeader ,
85
+ withFixedLengthStream
86
+ )
91
87
92
88
/**
89
+ * Configure the OpenTelemetry exporter based on the environment
93
90
*
94
91
* @param {Environment } env
95
92
* @param {* } _trigger
@@ -109,9 +106,42 @@ function config (env, _trigger) {
109
106
service : { name : 'freeway' }
110
107
}
111
108
}
112
- export default process . env . FF_TELEMETRY_ENABLED === 'true'
113
- ? instrument ( handler , config )
114
- : handler
109
+
110
+ /**
111
+ * The promise to the pre-configured handler
112
+ *
113
+ * @type {Promise<Handler<Context, Environment>> | null }
114
+ */
115
+ let handlerPromise = null
116
+
117
+ /**
118
+ * Pre-configure the handler based on the environment.
119
+ *
120
+ * @param {Environment } env
121
+ * @returns {Promise<Handler<Context, Environment>> }
122
+ */
123
+ async function initializeHandler ( env ) {
124
+ const baseHandler = middleware ( handleUnixfs )
125
+ const finalHandler = env . FF_TELEMETRY_ENABLED === 'true'
126
+ ? instrument ( baseHandler , config )
127
+ : baseHandler
128
+ return finalHandler
129
+ }
130
+
131
+ const handler = {
132
+ /** @type {Handler<Context, Environment> } */
133
+ async fetch ( request , env , ctx ) {
134
+ console . log ( request . method , request . url )
135
+ // Initialize the handler only once and reuse the promise
136
+ if ( ! handlerPromise ) {
137
+ handlerPromise = initializeHandler ( env )
138
+ }
139
+ const handler = await handlerPromise
140
+ return handler ( request , env , ctx )
141
+ }
142
+ }
143
+
144
+ export default handler
115
145
116
146
/**
117
147
* @type {Middleware<BlockContext & UnixfsContext & IpfsUrlContext, BlockContext & UnixfsContext & IpfsUrlContext, Environment> }
0 commit comments