-
-
Notifications
You must be signed in to change notification settings - Fork 121
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: Add hono enhancer to cloudflare and aws lambda #923
Changes from all commits
12c72f9
228b843
dc4c5c6
d04b69c
fdf2ff8
0b70183
57ae520
afa0f8b
4df784b
fc337de
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,27 +23,40 @@ const { Hono } = await importHono(); | |
|
||
const loadEntries = () => import('${srcEntriesFile}'); | ||
let serve; | ||
|
||
const app = new Hono(); | ||
app.use((c, next) => serve(c, next)); | ||
app.notFound(async (c) => { | ||
const assetsFetcher = c.env.ASSETS; | ||
const url = new URL(c.req.raw.url); | ||
const errorHtmlUrl = url.origin + '/404.html'; | ||
const notFoundStaticAssetResponse = await assetsFetcher.fetch( | ||
new URL(errorHtmlUrl), | ||
); | ||
if (notFoundStaticAssetResponse && notFoundStaticAssetResponse.status < 400) { | ||
return c.body(notFoundStaticAssetResponse.body, 404); | ||
} | ||
return c.text('404 Not Found', 404); | ||
}); | ||
let app; | ||
|
||
const createApp = (app) => { | ||
app.use((c, next) => serve(c, next)); | ||
app.notFound(async (c) => { | ||
const assetsFetcher = c.env.ASSETS; | ||
const url = new URL(c.req.raw.url); | ||
const errorHtmlUrl = url.origin + '/404.html'; | ||
const notFoundStaticAssetResponse = await assetsFetcher.fetch( | ||
new URL(errorHtmlUrl), | ||
); | ||
if ( | ||
notFoundStaticAssetResponse && | ||
notFoundStaticAssetResponse.status < 400 | ||
) { | ||
return c.body(notFoundStaticAssetResponse.body, 404); | ||
} | ||
return c.text('404 Not Found', 404); | ||
}); | ||
return app; | ||
}; | ||
|
||
export default { | ||
async fetch(request, env, ctx) { | ||
if (!serve) { | ||
serve = serverEngine({ cmd: 'start', loadEntries, env }); | ||
} | ||
if (!app) { | ||
const entries = await loadEntries(); | ||
const config = await entries.loadConfig(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moving |
||
const honoEnhancer = | ||
config.unstable_honoEnhancer || ((createApp) => createApp); | ||
app = honoEnhancer(createApp)(new Hono()); | ||
} | ||
return app.fetch(request, env, ctx); | ||
}, | ||
}; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,30 +15,40 @@ const { Hono } = await importHono(); | |
|
||
const loadEntries = () => import('${srcEntriesFile}'); | ||
let serve; | ||
let app; | ||
|
||
const app = new Hono(); | ||
app.use((c, next) => serve(c, next)); | ||
app.notFound(async (c) => { | ||
const assetsFetcher = c.env.assets; | ||
// check if there's a 404.html in the static assets | ||
const notFoundStaticAssetResponse = await assetsFetcher.fetch('/404.html'); | ||
// if there is, return it | ||
if (notFoundStaticAssetResponse) { | ||
return new Response(notFoundStaticAssetResponse.body, { | ||
status: 404, | ||
statusText: 'Not Found', | ||
headers: notFoundStaticAssetResponse.headers, | ||
}); | ||
} | ||
// otherwise, return a simple 404 response | ||
return c.text('404 Not Found', 404); | ||
}); | ||
const createApp = (app) => { | ||
app.use((c, next) => serve(c, next)); | ||
app.notFound(async (c) => { | ||
const assetsFetcher = c.env.ASSETS; | ||
const url = new URL(c.req.raw.url); | ||
const errorHtmlUrl = url.origin + '/404.html'; | ||
const notFoundStaticAssetResponse = await assetsFetcher.fetch( | ||
new URL(errorHtmlUrl), | ||
); | ||
if ( | ||
notFoundStaticAssetResponse && | ||
notFoundStaticAssetResponse.status < 400 | ||
) { | ||
return c.body(notFoundStaticAssetResponse.body, 404); | ||
} | ||
return c.text('404 Not Found', 404); | ||
}); | ||
return app; | ||
}; | ||
|
||
export default { | ||
onFetch(request, lobby, ctx) { | ||
async onFetch(request, lobby, ctx) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. According to https://docs.partykit.io/reference/partyserver-api/#static-onfetch - async is supported for onFetch. |
||
if (!serve) { | ||
serve = serverEngine({ cmd: 'start', loadEntries, env: lobby }); | ||
} | ||
if (!app) { | ||
const entries = await loadEntries(); | ||
const config = await entries.loadConfig(); | ||
const honoEnhancer = | ||
config.unstable_honoEnhancer || ((createApp) => createApp); | ||
app = honoEnhancer(createApp)(new Hono()); | ||
} | ||
return app.fetch(request, lobby, ctx); | ||
}, | ||
}; | ||
|
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.
Thanks.