MCPcopy Index your code
hub / github.com/TanStack/router / executeMiddleware

Function executeMiddleware

packages/start-server-core/src/createStartHandler.ts:256–302  ·  view source on GitHub ↗

* Execute a middleware chain

(middlewares: Array<TODO>, ctx: TODO)

Source from the content-addressed store, hash-verified

254 * Execute a middleware chain
255 */
256function executeMiddleware(middlewares: Array<TODO>, ctx: TODO): Promise<TODO> {
257 let index = -1
258
259 const next = async (nextCtx?: TODO): Promise<TODO> => {
260 // Merge context if provided using safeObjectMerge for prototype pollution prevention
261 if (nextCtx) {
262 if (nextCtx.context) {
263 ctx.context = safeObjectMerge(ctx.context, nextCtx.context)
264 }
265 // Copy own properties except context (Object.keys returns only own enumerable properties)
266 for (const key of Object.keys(nextCtx)) {
267 if (key !== 'context') {
268 ctx[key] = nextCtx[key]
269 }
270 }
271 }
272
273 index++
274 const middleware = middlewares[index]
275 if (!middleware) return ctx
276
277 let result: TODO
278 try {
279 result = await middleware({ ...ctx, next })
280 } catch (err) {
281 if (isSpecialResponse(err)) {
282 ctx.response = err
283 return ctx
284 }
285 throw err
286 }
287
288 const normalized = handleCtxResult(result)
289 if (normalized) {
290 if (normalized.response !== undefined) {
291 ctx.response = normalized.response
292 }
293 if (normalized.context) {
294 ctx.context = safeObjectMerge(ctx.context, normalized.context)
295 }
296 }
297
298 return ctx
299 }
300
301 return next()
302}
303
304/**
305 * Wrap a route handler as middleware

Callers 2

startRequestResolverFunction · 0.70
handleServerRoutesFunction · 0.70

Calls 1

nextFunction · 0.70

Tested by

no test coverage detected