-
-
Notifications
You must be signed in to change notification settings - Fork 124
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(deploy): add cloudflare context to platform object #872
Changes from all commits
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 | ||||
---|---|---|---|---|---|---|
@@ -1,41 +1,75 @@ | ||||||
import { Hono } from 'hono'; | ||||||
import { unstable_getPlatformObject } from 'waku/server'; | ||||||
import { runner } from '../hono/runner.js'; | ||||||
import type { | ||||||
ExportedHandler, | ||||||
fetch, | ||||||
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. This is confusing. So:
Suggested change
|
||||||
Request as CloudflareRequest, | ||||||
Response as CloudflareResponse, | ||||||
} from '@cloudflare/workers-types/experimental'; | ||||||
|
||||||
const loadEntries = () => import(import.meta.env.WAKU_ENTRIES_FILE!); | ||||||
let serveWaku: ReturnType<typeof runner> | undefined; | ||||||
|
||||||
export interface CloudflareEnv { | ||||||
ASSETS: { | ||||||
fetch: (input: RequestInit | URL, init?: RequestInit) => Promise<Response>; | ||||||
fetch: typeof fetch; | ||||||
}; | ||||||
} | ||||||
|
||||||
export const app = new Hono<{ | ||||||
Bindings: CloudflareEnv & { [k: string]: unknown }; | ||||||
}>(); | ||||||
app.use('*', (c, next) => serveWaku!(c, next)); | ||||||
app.use('*', (c, next) => { | ||||||
if (!serveWaku) { | ||||||
throw new Error('serveWaku is not initialized'); | ||||||
} | ||||||
const platform = unstable_getPlatformObject(); | ||||||
platform.honoContext = c; | ||||||
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. This is not good. |
||||||
platform.cf = (c.req.raw as unknown as CloudflareRequest).cf; | ||||||
platform.env = c.env; | ||||||
platform.executionContext = c.executionCtx; | ||||||
return serveWaku(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( | ||||||
const notFoundStaticAssetResponse = (await assetsFetcher.fetch( | ||||||
new URL(errorHtmlUrl), | ||||||
); | ||||||
)) as unknown as Response; | ||||||
if (notFoundStaticAssetResponse && notFoundStaticAssetResponse.status < 400) { | ||||||
return c.body(notFoundStaticAssetResponse.body, 404); | ||||||
} | ||||||
return c.text('404 Not Found', 404); | ||||||
}); | ||||||
|
||||||
export default { | ||||||
async fetch( | ||||||
request: Request, | ||||||
env: Record<string, string>, | ||||||
ctx: Parameters<typeof app.fetch>[2], | ||||||
) { | ||||||
// Waku getEnv only supports strings | ||||||
// Cloudflare injects bindings to env and JSON | ||||||
// Use unstable_getPlatformObject() to access cloudflare env and execution context | ||||||
// https://developers.cloudflare.com/workers/configuration/environment-variables/#add-environment-variables-via-wrangler | ||||||
// https://developers.cloudflare.com/workers/runtime-apis/bindings/ | ||||||
const extractWakuEnv = (env: Record<string, unknown>): Record<string, string> => | ||||||
Object.fromEntries( | ||||||
Object.entries(env).filter(([, value]) => typeof value === 'string'), | ||||||
) as Record<string, string>; | ||||||
|
||||||
const handler: ExportedHandler<CloudflareEnv & { [k: string]: never }> = { | ||||||
async fetch(request, env, ctx) { | ||||||
if (!serveWaku) { | ||||||
serveWaku = runner({ cmd: 'start', loadEntries, env }); | ||||||
serveWaku = runner({ | ||||||
cmd: 'start', | ||||||
loadEntries, | ||||||
env: extractWakuEnv(env), | ||||||
}); | ||||||
} | ||||||
return app.fetch(request, env, ctx); | ||||||
return app.fetch( | ||||||
request as unknown as Request, | ||||||
env, | ||||||
ctx, | ||||||
) as unknown as CloudflareResponse; | ||||||
}, | ||||||
// TODO ability to add other handlers or Durable Objects? | ||||||
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. Unless we do this, this PR doesn't provide any more capability than #852. |
||||||
}; | ||||||
|
||||||
export default handler; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -147,8 +147,11 @@ export function unstable_getHeaders(): Record<string, string> { | |
>; | ||
} | ||
|
||
type PlatformObject = { | ||
buildData?: Record<string, unknown>; // must be JSON serializable | ||
type PlatformObject< | ||
T = Record<string, unknown>, | ||
BuildData = Record<string, unknown>, | ||
> = { | ||
buildData?: BuildData; // must be JSON serializable | ||
buildOptions?: { | ||
deploy?: | ||
| 'vercel-static' | ||
|
@@ -167,11 +170,13 @@ type PlatformObject = { | |
| 'buildClientBundle' | ||
| 'buildDeploy'; | ||
}; | ||
} & Record<string, unknown>; | ||
} & T; | ||
|
||
(globalThis as any).__WAKU_PLATFORM_OBJECT__ ||= {}; | ||
|
||
// TODO tentative name | ||
export function unstable_getPlatformObject(): PlatformObject { | ||
export function unstable_getPlatformObject< | ||
T = Record<string, unknown>, | ||
>(): PlatformObject<T> { | ||
Comment on lines
+178
to
+180
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. I believe asserting types is more appropriate than annotating types, because it never be type safe. |
||
return (globalThis as any).__WAKU_PLATFORM_OBJECT__; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
It might not change anything real, but for consistency. (Will be refactored anyway.)