feat(context): add executionCtx to Context (#326)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Hans Otto Wirtz · committed by GitHub · parent b82a75c · Browse files · 3 files·+28 added−9 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+28 added−9 removed
- other:+28 added−9 removed
Provenance
- Commit
157e4ea- Tree
7ac9db0- Author
Hans Otto Wirtz <hansottowirtz@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
b82a75c
3 files changed+28 added−9 removed
modified:src/context.ts+9 added-2 removed
9 unmodified lines1011121314151617187 unmodified lines2627282929303132333435363738394041374243444546474888 unmodified lines9 unmodified linesexport class Context<RequestParamKeyType extends string = string, E = Env> { req: Request<RequestParamKeyType> env: E event: FetchEvent | undefined executionCtx: ExecutionContext | undefined finalized: boolean
private _status: StatusCode = 200 private _pretty: boolean = false7 unmodified lines
constructor( req: Request<RequestParamKeyType>, env: E | undefined = undefined, event: FetchEvent | undefined = undefined, eventOrExecutionCtx: FetchEvent | ExecutionContext | undefined = undefined, notFoundHandler: NotFoundHandler = () => new Response() ) { this.req = req
if (env) { this.env = env }
this.executionCtx = eventOrExecutionCtx
if (eventOrExecutionCtx && 'respondWith' in eventOrExecutionCtx) { this.event = event this.event = eventOrExecutionCtx }
this.notFoundHandler = notFoundHandler this.finalized = false }
88 unmodified linesmodified:src/hono.ts+4 added-4 removed
158 unmodified lines159 private matchRoute(method: string, path: string) {160 return this.router.match(method, path)161 }162 163- private async dispatch(request: Request, event?: FetchEvent, env?: E): Promise<Response> {163+ private async dispatch(request: Request, executionCtx?: ExecutionContext, env?: E): Promise<Response> {164 const path = getPathFromURL(request.url, this.strict)165 const method = request.method166 167 const result = this.matchRoute(method, path)1 unmodified line169 request.paramData = result?.params170 171 const handlers = result ? result.handlers : [this.notFoundHandler]172 173- const c = new Context<string, E>(request, env, event, this.notFoundHandler)173+ const c = new Context<string, E>(request, env, executionCtx, this.notFoundHandler)174 175 const composed = compose<Context>(handlers, this.errorHandler, this.notFoundHandler)176 let context: Context177 try {16 unmodified lines194 async handleEvent(event: FetchEvent): Promise<Response> {195 return this.dispatch(event.request, event)196 }197 198- async fetch(request: Request, env?: E, event?: FetchEvent): Promise<Response> {199- return this.dispatch(request, event, env)198+ async fetch(request: Request, env?: E, executionCtx?: ExecutionContext): Promise<Response> {199+ return this.dispatch(request, executionCtx, env)200 }201 202 request(input: RequestInfo, requestInit?: RequestInit): Promise<Response> {203 const req = input instanceof Request ? input : new Request(input, requestInit)9 unmodified linesmodified:README.md+15 added-3 removed
481 unmodified lines482 c.res.headers.append('X-Debug', 'Debug message')483 })484 ```485 486+### c.executionCtx487+488+```ts489+// ExecutionContext object490+app.get('/foo', async (c) => {491+ c.executionCtx.waitUntil(492+ c.env.KV.put(key, data)493+ )494+ ...495+})496+```497+498 ### c.event499 500 ```ts489-// FetchEvent object501+// FetchEvent object (only set when using Service Worker syntax)502 app.get('/foo', async (c) => {503 c.event.waitUntil(504 c.env.KV.put(key, data)505 )28 unmodified lines534 `app.fetch` for Cloudflare Module Worker syntax.535 536 ```ts537 export default {526- fetch(request: Request, env: Env, event: FetchEvent) {527- return app.fetch(request, env, event)538+ fetch(request: Request, env: Env, ctx: ExecutionContext) {539+ return app.fetch(request, env, ctx)540 },541 }542 ```543 226 unmodified lines