Skip to content
Myr

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 file

Take 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 back
1import { Context } from './context'

Merge pull request #1589 from honojs/nextYusuke Wadabefore adoption70d83cb

line 2why, one step back
2import type { ParamIndexMap, Params } from './router'

perf(hono-base): use `instanceOf` (#1565)Yusuke Wadabefore adoption2f9a07d

lines 3–11why, one step back
3import 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 back
12 middleware: [[Function, unknown], ParamIndexMap | Params][],

perf(hono-base): use `instanceOf` (#1565)Yusuke Wadabefore adoption2f9a07d

lines 13–19why, one step back
13 onError?: ErrorHandler<E>,
14 onNotFound?: NotFoundHandler<E>15) => {16 return (context: C, next?: Function) => {17 let index = -118 return dispatch(0)19

perf(compose): simplify (#1701)Yusuke Wadabefore adoption16396bc

line 20why, one step back
20 async function dispatch(i: number): Promise<C> {

perf(hono-base): use `instanceOf` (#1565)Yusuke Wadabefore adoption2f9a07d

lines 21–27why, one step back
21 if (i <= index) {
22 throw new Error('next() called multiple times')23 }24 index = i2526 let res27 let isError = false

Merge pull request #1589 from honojs/nextYusuke Wadabefore adoption70d83cb

lines 28–30why, one step back
28 let handler
2930 if (middleware[i]) {

Merge pull request #1774 from honojs/nextYusuke Wadabefore adoptionb21f33c

line 31why, one step back
31 handler = middleware[i][0][0]

Merge pull request #1589 from honojs/nextYusuke Wadabefore adoption70d83cb

line 32why, one step back
32 if (context instanceof Context) {

Merge pull request #1774 from honojs/nextYusuke Wadabefore adoptionb21f33c

line 33why, one step back
33 context.req.routeIndex = i

Merge pull request #1589 from honojs/nextYusuke Wadabefore adoption70d83cb

lines 34–37why, one step back
34 }
35 } else {36 handler = (i === middleware.length && next) || undefined37 }

perf(hono-base): use `instanceOf` (#1565)Yusuke Wadabefore adoption2f9a07d

lines 38–40why, one step back
38
39 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 back
41 res = await onNotFound(context)

perf(hono-base): use `instanceOf` (#1565)Yusuke Wadabefore adoption2f9a07d

lines 42–44why, one step back
42 }
43 } else {44 try {

perf(compose): simplify (#1701)Yusuke Wadabefore adoption16396bc

lines 45–46why, one step back
45 res = await handler(context, () => {
46 return dispatch(i + 1)

perf(hono-base): use `instanceOf` (#1565)Yusuke Wadabefore adoption2f9a07d

lines 47–50why, one step back
47 })
48 } catch (err) {49 if (err instanceof Error && context instanceof Context && onError) {50 context.error = err

fix(compose): allow `onError()` handler to be async (#1730)Yusuke Wadabefore adoption257d571

line 51why, one step back
51 res = await onError(err, context)

perf(hono-base): use `instanceOf` (#1565)Yusuke Wadabefore adoption2f9a07d

lines 52–58why, one step back
52 isError = true
53 } else {54 throw err55 }56 }57 }58

perf(compose): simplify (#1701)Yusuke Wadabefore adoption16396bc

lines 59–62why, one step back
59 if (res && (context.finalized === false || isError)) {
60 context.res = res61 }62 return context

perf(hono-base): use `instanceOf` (#1565)Yusuke Wadabefore adoption2f9a07d

lines 63–65why, one step back
63 }
64 }65}