-
-
Notifications
You must be signed in to change notification settings - Fork 619
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(cloudflare-pages): Add Cloudflare Pages middleware handler #3028
feat(cloudflare-pages): Add Cloudflare Pages middleware handler #3028
Conversation
TOKEN: 'HONOISCOOL', | ||
} | ||
const handler = handleMiddleware(async (c, next) => { | ||
const cookie = getCookie(c, 'my_cookie') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Included this as an example of my use-case, but it's not entirely necessary as part of the test
Thank you for the PR. I'll review this later. |
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## next #3028 +/- ##
==========================================
+ Coverage 96.02% 96.05% +0.02%
==========================================
Files 142 142
Lines 14271 14358 +87
Branches 2445 2580 +135
==========================================
+ Hits 13704 13791 +87
Misses 567 567 ☔ View full report in Codecov by Sentry. |
The current code can't handle an import { basicAuth } from 'hono/basic-auth'
export const onRequest = handleMiddleware(
basicAuth({
username: 'foo',
password: 'bar'
})
) To handle diff --git a/src/adapter/cloudflare-pages/handler.ts b/src/adapter/cloudflare-pages/handler.ts
index 8d3325a..13e6916 100644
--- a/src/adapter/cloudflare-pages/handler.ts
+++ b/src/adapter/cloudflare-pages/handler.ts
@@ -1,5 +1,6 @@
import { Context } from '../../context'
import type { Hono } from '../../hono'
+import { HTTPException } from '../../http-exception'
import { HonoRequest } from '../../request'
import type { Env, Input, MiddlewareHandler } from '../../types'
@@ -53,9 +54,17 @@ export function handleMiddleware<E extends Env = any, P extends string = any, I
executionCtx,
})
- const response = await middleware(context, async () => {
- context.res = await executionCtx.next()
- })
+ let response: Response | void = undefined
+
+ try {
+ response = await middleware(context, async () => {
+ context.res = await executionCtx.next()
+ })
+ } catch (e) {
+ if (e instanceof HTTPException) {
+ response = e.getResponse()
+ }
+ }
return response ?? context.res
} |
This is a super cool feature! I've tried it, and it feels good. I've left some comments. Check them! |
0acd56c
to
cb19747
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had a bit of trouble reconciling the changes in both the main
and next
branches, so I have rebased my changes onto next
, assuming #3046 will also be in next
soon 👀
cb19747
to
7bd318f
Compare
try { | ||
response = await middleware(context, async () => { | ||
try { | ||
context.res = await executionCtx.next() | ||
} catch (error) { | ||
if (error instanceof Error) { | ||
context.error = error | ||
} else { | ||
throw error | ||
} | ||
} | ||
}) | ||
} catch (error) { | ||
if (error instanceof Error) { | ||
context.error = error | ||
} else { | ||
throw error | ||
} | ||
} | ||
|
||
if (response) { | ||
return response | ||
} | ||
|
||
if (context.error instanceof HTTPException) { | ||
return context.error.getResponse() | ||
} | ||
|
||
if (context.error) { | ||
throw context.error | ||
} | ||
|
||
return context.res | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm still not sure all of this is necessary.. I added all the test cases I could think of, so hopefully all the main use cases are covered
@@ -49,7 +49,7 @@ describe('getConnInfo', () => { | |||
}) | |||
it('should return undefined when addressType is invalid string', () => { | |||
const { server } = createRandomBunServer({ family: 'invalid' }) | |||
const c = new Context(new HonoRequest(new Request('http://localhost/')), { env: { server } }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like next
is failing due to HonoRequest
being removed 👀
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
Awesome work! It's wonderful that we'll be able to use a lot of Hono's middleware and helpers in the Cloudflare Pages function. Thank you very much. I'll merge this into the |
Adds an adapter for Cloudflare Pages middleware
This provides a convenient way to use
Context
and helpers fromhono
within Cloudflare Pages middlewareFixes #3015