Skip to content
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(dev): cloudflare dev server adapter #892

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion packages/waku/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type { Config } from './config.js';
import { runner } from './lib/hono/runner.js';
import { build } from './lib/builder/build.js';
import { DIST_ENTRIES_JS, DIST_PUBLIC } from './lib/builder/constants.js';
import type { FetchHandler } from './lib/hono/adapter.js';

const require = createRequire(new URL('.', import.meta.url));

Expand Down Expand Up @@ -105,7 +106,15 @@ async function runDev() {
return c.text('404 Not Found', 404);
});
const port = parseInt(values.port || '3000', 10);
await startServer(app, port);
let fetchHandler: FetchHandler | undefined;
if (values['with-cloudflare']) {
const { cloudflareDevServer } = await import(
'./lib/hono/cloudflare-dev-server.js'
);
// @ts-expect-error wrangler dev server type is not included in Config
fetchHandler = cloudflareDevServer(app, config.wrangler);
}
await startServer({ ...app, fetch: fetchHandler || app.fetch } as Hono, port);
}

async function runBuild() {
Expand Down
8 changes: 8 additions & 0 deletions packages/waku/src/lib/hono/adapter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { ExecutionContext, Hono } from 'hono';

export type FetchAdapter = (app: Hono) => Promise<FetchHandler | undefined>;
export type FetchHandler = (
req: Request,
env?: unknown,
executionCtx?: ExecutionContext,
) => Promise<Response> | Response;
36 changes: 36 additions & 0 deletions packages/waku/src/lib/hono/cloudflare-dev-server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* eslint import/no-unresolved: 0 */
import type { Hono } from 'hono';

export const cloudflareDevServer = (app: Hono, cfOptions?: any) => {
try {
// @ts-expect-error: miniflare is a peer dependency (provided by miniflare)
const wsAssign = import('miniflare').then(({ WebSocketPair }) => {
Object.assign(globalThis, { WebSocketPair });
});

// @ts-expect-error: wrangler is a peer dependency
const proxy = import('wrangler').then(({ getPlatformProxy }) =>
getPlatformProxy({
...(cfOptions || {}),
}).then((proxy: any) => {
return proxy;
}),
);

return async (req: Request) => {
await wsAssign;
const awaitedProxy = await proxy;
Object.assign(req, { cf: awaitedProxy.cf });
Object.assign(globalThis, {
caches: awaitedProxy.caches,
});
return app.fetch(req, awaitedProxy.env, awaitedProxy.ctx);
};
} catch (e) {
console.warn(
'Unable to set up Cloudflare dev server. Try installing wrangler to your dev dependencies.',
e,
);
return undefined;
}
};
Loading