Skip to content
Myr

compose.ts

This file

Path

src/compose.ts

96 lines · 2.7 KiB · TypeScript

Last changed by

perf(compose): check if it is a instance only once (#3585)EdamAmexbefore adoptionefd3d04

History of this file

Take it

The walk stopped after 200 commits: lines older than 7ee829b point at it.

v4.4.7Yusuke Wadabefore adoption7ee829b

lines 1–36why, one step back
1import { Context } from './context'
2import type { ParamIndexMap, Params } from './router'3import type { Env, ErrorHandler, NotFoundHandler } from './types'45/**6 * Interface representing the context for a composition operation.7 */8interface ComposeContext {9 /**10 * Indicates whether the composition process has been finalized.11 */12 finalized: boolean1314 /**15 * The result of the composition process. The type is unknown and should be16 * specified based on the context where this interface is used.17 */18 res: unknown19}2021/**22 * Compose middleware functions into a single function based on `koa-compose` package.23 *24 * @template C - The context type.25 * @template E - The environment type.26 *27 * @param {[[Function, unknown], ParamIndexMap | Params][]} middleware - An array of middleware functions and their corresponding parameters.28 * @param {ErrorHandler<E>} [onError] - An optional error handler function.29 * @param {NotFoundHandler<E>} [onNotFound] - An optional not-found handler function.30 *31 * @returns {(context: C, next?: Function) => Promise<C>} - A composed middleware function.32 */33export const compose = <C extends ComposeContext, E extends Env = Env>(34 middleware: [[Function, unknown], ParamIndexMap | Params][],35 onError?: ErrorHandler<E>,36 onNotFound?: NotFoundHandler<E>

Merge pull request #3144 from honojs/nextYusuke Wadabefore adoption2f4bb3e

lines 37–38why, one step back
37): ((context: C, next?: Function) => Promise<C>) => {
38 return (context, next) => {

v4.4.7Yusuke Wadabefore adoption7ee829b

line 39why, one step back
39 let index = -1

perf(compose): check if it is a instance only once (#3585)EdamAmexbefore adoptionefd3d04

lines 40–41why, one step back
40 const isContext = context instanceof Context
41

v4.4.7Yusuke Wadabefore adoption7ee829b

lines 42–62why, one step back
42 return dispatch(0)
4344 /**45 * Dispatch the middleware functions.46 *47 * @param {number} i - The current index in the middleware array.48 *49 * @returns {Promise<C>} - A promise that resolves to the context.50 */51 async function dispatch(i: number): Promise<C> {52 if (i <= index) {53 throw new Error('next() called multiple times')54 }55 index = i5657 let res58 let isError = false59 let handler6061 if (middleware[i]) {62 handler = middleware[i][0][0]

perf(compose): check if it is a instance only once (#3585)EdamAmexbefore adoptionefd3d04

line 63why, one step back
63 if (isContext) {

v4.4.7Yusuke Wadabefore adoption7ee829b

lines 64–70why, one step back
64 context.req.routeIndex = i
65 }66 } else {67 handler = (i === middleware.length && next) || undefined68 }6970 if (!handler) {

perf(compose): check if it is a instance only once (#3585)EdamAmexbefore adoptionefd3d04

line 71why, one step back
71 if (isContext && context.finalized === false && onNotFound) {

v4.4.7Yusuke Wadabefore adoption7ee829b

lines 72–79why, one step back
72 res = await onNotFound(context)
73 }74 } else {75 try {76 res = await handler(context, () => {77 return dispatch(i + 1)78 })79 } catch (err) {

perf(compose): check if it is a instance only once (#3585)EdamAmexbefore adoptionefd3d04

line 80why, one step back
80 if (err instanceof Error && isContext && onError) {

v4.4.7Yusuke Wadabefore adoption7ee829b

lines 81–96why, one step back
81 context.error = err
82 res = await onError(err, context)83 isError = true84 } else {85 throw err86 }87 }88 }8990 if (res && (context.finalized === false || isError)) {91 context.res = res92 }93 return context94 }95 }96}