Skip to content

Commit

Permalink
feat: add unstable_honoEnhancer to cloudflare build
Browse files Browse the repository at this point in the history
  • Loading branch information
rmarscher committed Oct 2, 2024
1 parent 8b56318 commit 12c72f9
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 30 deletions.
28 changes: 20 additions & 8 deletions packages/waku/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,20 @@ if (values.version) {

async function runDev() {
const config = await loadConfig();
const honoEnhancer =
config.unstable_honoEnhancer || ((createApp) => createApp);
const honoEnhancer = config.unstable_honoEnhancer
? config.unstable_honoEnhancer
: async (createApp: (app: Hono) => Hono) => createApp;
const createApp = (app: Hono) => {
if (values['experimental-compress']) {
app.use(compress());
}
app.use(serverEngine({ cmd: 'dev', config, env: process.env as any }));
app.use(
serverEngine({
cmd: 'dev',
config,
env: process.env as any,
}),
);
app.notFound((c) => {
// FIXME can we avoid hardcoding the public path?
const file = path.join('public', '404.html');
Expand All @@ -116,7 +123,7 @@ async function runDev() {
return app;
};
const port = parseInt(values.port || '3000', 10);
await startServer(honoEnhancer(createApp)(new Hono()), port);
await startServer((await honoEnhancer(createApp))!(new Hono()), port);
}

async function runBuild() {
Expand Down Expand Up @@ -147,8 +154,9 @@ async function runBuild() {
async function runStart() {
const config = await loadConfig();
const { distDir = 'dist' } = config;
const honoEnhancer =
config.unstable_honoEnhancer || ((createApp) => createApp);
const honoEnhancer = config.unstable_honoEnhancer
? config.unstable_honoEnhancer
: async (createApp: (app: Hono) => Hono) => (app: Hono) => createApp(app);
const loadEntries = () =>
import(pathToFileURL(path.resolve(distDir, DIST_ENTRIES_JS)).toString());
const createApp = (app: Hono) => {
Expand All @@ -157,7 +165,11 @@ async function runStart() {
}
app.use(serveStatic({ root: path.join(distDir, DIST_PUBLIC) }));
app.use(
serverEngine({ cmd: 'start', loadEntries, env: process.env as any }),
serverEngine({
cmd: 'start',
loadEntries,
env: process.env as any,
}),
);
app.notFound((c) => {
// FIXME better implementation using node stream?
Expand All @@ -170,7 +182,7 @@ async function runStart() {
return app;
};
const port = parseInt(values.port || '8080', 10);
await startServer(honoEnhancer(createApp)(new Hono()), port);
await startServer((await honoEnhancer(createApp))!(new Hono()), port);
}

function startServer(app: Hono, port: number) {
Expand Down
8 changes: 4 additions & 4 deletions packages/waku/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ export interface Config {
*/
middleware?: () => Promise<{ default: Middleware }>[];
/**
* Enhander for Hono
* Enhancer for Hono
* Defaults to `undefined`
*/
unstable_honoEnhancer?:
| (<Hono>(createApp: (app: Hono) => Hono) => (app: Hono) => Hono)
| undefined;
unstable_honoEnhancer?: <Hono>(
createApp: (app: Hono) => Hono,
) => Promise<(app: Hono) => Hono> | undefined;
}

export function defineConfig(config: Config) {
Expand Down
51 changes: 33 additions & 18 deletions packages/waku/src/lib/plugins/vite-plugin-deploy-cloudflare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,49 @@ import { DIST_ENTRIES_JS, DIST_PUBLIC } from '../builder/constants.js';
const SERVE_JS = 'serve-cloudflare.js';

const getServeJsContent = (srcEntriesFile: string) => `
import { serverEngine, importHono } from 'waku/unstable_hono';
import { serverEngine, importHono } from "waku/unstable_hono";
const { Hono } = await importHono();
const loadEntries = () => import('${srcEntriesFile}');
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);
});
const configPromise = loadEntries().then((entries) => entries.loadConfig());
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;
};
let honoEnhanced;
export default {
async fetch(request, env, ctx) {
if (!serve) {
serve = serverEngine({ cmd: 'start', loadEntries, env });
serve = serverEngine({ cmd: "start", loadEntries, env });
}
if (!honoEnhanced) {
const honoEnhancer =
(await configPromise).unstable_honoEnhancer ||
(async (createApp) => createApp);
honoEnhanced = (await honoEnhancer(createApp))(new Hono(), serve);
}
return app.fetch(request, env, ctx);
return honoEnhanced.fetch(request, env, ctx);
},
};
`;
Expand Down

0 comments on commit 12c72f9

Please sign in to comment.