compose.ts
This file
Path
src/compose.ts
65 lines · 1.7 KiB · TypeScript
Last changed by
Merge pull request #1774 from honojs/nextYusuke Wadabefore adoptionb21f33c
History of this fileTake it
The walk stopped after 200 commits: lines older than 2f9a07d point at it.
perf(hono-base): use `instanceOf` (#1565)Yusuke Wadabefore adoption2f9a07d
line 1why, one step back1import { Context } from './context'Merge pull request #1589 from honojs/nextYusuke Wadabefore adoption70d83cb
line 2why, one step back2import type { ParamIndexMap, Params } from './router'perf(hono-base): use `instanceOf` (#1565)Yusuke Wadabefore adoption2f9a07d
lines 3–11why, one step back3import type { Env, NotFoundHandler, ErrorHandler } from './types'45interface ComposeContext {6 finalized: boolean7 res: unknown8}910// Based on the code in the MIT licensed `koa-compose` package.11export const compose = <C extends ComposeContext, E extends Env = Env>(Merge pull request #1774 from honojs/nextYusuke Wadabefore adoptionb21f33c
line 12why, one step back12 middleware: [[Function, unknown], ParamIndexMap | Params][],perf(hono-base): use `instanceOf` (#1565)Yusuke Wadabefore adoption2f9a07d
lines 13–19why, one step back13 onError?: ErrorHandler<E>,14 onNotFound?: NotFoundHandler<E>15) => {16 return (context: C, next?: Function) => {17 let index = -118 return dispatch(0)19perf(compose): simplify (#1701)Yusuke Wadabefore adoption16396bc
line 20why, one step back20 async function dispatch(i: number): Promise<C> {perf(hono-base): use `instanceOf` (#1565)Yusuke Wadabefore adoption2f9a07d
lines 21–27why, one step back21 if (i <= index) {22 throw new Error('next() called multiple times')23 }24 index = i2526 let res27 let isError = falseMerge pull request #1589 from honojs/nextYusuke Wadabefore adoption70d83cb
lines 28–30why, one step back28 let handler2930 if (middleware[i]) {Merge pull request #1774 from honojs/nextYusuke Wadabefore adoptionb21f33c
line 31why, one step back31 handler = middleware[i][0][0]Merge pull request #1589 from honojs/nextYusuke Wadabefore adoption70d83cb
line 32why, one step back32 if (context instanceof Context) {Merge pull request #1774 from honojs/nextYusuke Wadabefore adoptionb21f33c
line 33why, one step back33 context.req.routeIndex = iMerge pull request #1589 from honojs/nextYusuke Wadabefore adoption70d83cb
lines 34–37why, one step back34 }35 } else {36 handler = (i === middleware.length && next) || undefined37 }perf(hono-base): use `instanceOf` (#1565)Yusuke Wadabefore adoption2f9a07d
lines 38–40why, one step back3839 if (!handler) {40 if (context instanceof Context && context.finalized === false && onNotFound) {fix(compose): allow notFound handler to be async (#1729)Russell Schickbefore adoption710992f
line 41why, one step back41 res = await onNotFound(context)perf(hono-base): use `instanceOf` (#1565)Yusuke Wadabefore adoption2f9a07d
lines 42–44why, one step back42 }43 } else {44 try {perf(compose): simplify (#1701)Yusuke Wadabefore adoption16396bc
lines 45–46why, one step back45 res = await handler(context, () => {46 return dispatch(i + 1)perf(hono-base): use `instanceOf` (#1565)Yusuke Wadabefore adoption2f9a07d
lines 47–50why, one step back47 })48 } catch (err) {49 if (err instanceof Error && context instanceof Context && onError) {50 context.error = errfix(compose): allow `onError()` handler to be async (#1730)Yusuke Wadabefore adoption257d571
line 51why, one step back51 res = await onError(err, context)perf(hono-base): use `instanceOf` (#1565)Yusuke Wadabefore adoption2f9a07d
lines 52–58why, one step back52 isError = true53 } else {54 throw err55 }56 }57 }58perf(compose): simplify (#1701)Yusuke Wadabefore adoption16396bc
lines 59–62why, one step back59 if (res && (context.finalized === false || isError)) {60 context.res = res61 }62 return contextperf(hono-base): use `instanceOf` (#1565)Yusuke Wadabefore adoption2f9a07d
lines 63–65why, one step back63 }64 }65}