(nextCtx?: 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 | } |
no test coverage detected