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: Add hono enhancer to cloudflare and aws lambda #923

Merged
merged 10 commits into from
Oct 3, 2024
2 changes: 1 addition & 1 deletion packages/waku/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export interface Config {
*/
middleware?: () => Promise<{ default: Middleware }>[];
/**
* Enhander for Hono
* Enhancer for Hono
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks.

* Defaults to `undefined`
*/
unstable_honoEnhancer?:
Expand Down
36 changes: 24 additions & 12 deletions packages/waku/src/lib/plugins/vite-plugin-deploy-aws-lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ const getServeJsContent = (
) => `
import path from 'node:path';
import { existsSync, readFileSync } from 'node:fs';
import { serverEngine, importHono, importHonoNodeServerServeStatic, importHonoAwsLambda } from 'waku/unstable_hono';
import {
serverEngine,
importHono,
importHonoNodeServerServeStatic,
importHonoAwsLambda,
} from 'waku/unstable_hono';

const { Hono } = await importHono();
const { serveStatic } = await importHonoNodeServerServeStatic();
Expand All @@ -27,18 +32,25 @@ const distDir = '${distDir}';
const publicDir = '${distPublic}';
const loadEntries = () => import('${srcEntriesFile}');

const app = new Hono();
app.use(serveStatic({ root: distDir + '/' + publicDir }));
app.use(serverEngine({ cmd: 'start', loadEntries, env: process.env }));
app.notFound(async (c) => {
const file = path.join(distDir, publicDir, '404.html');
if (existsSync(file)) {
return c.html(readFileSync(file, 'utf8'), 404);
}
return c.text('404 Not Found', 404);
});
const configPromise = loadEntries().then((entries) => entries.loadConfig());

export const handler = handle(app);
const createApp = (app) => {
app.use(serveStatic({ root: distDir + '/' + publicDir }));
app.use(serverEngine({ cmd: 'start', loadEntries, env: process.env }));
app.notFound(async (c) => {
const file = path.join(distDir, publicDir, '404.html');
if (existsSync(file)) {
return c.html(readFileSync(file, 'utf8'), 404);
}
return c.text('404 Not Found', 404);
});
return app;
};

const honoEnhancer =
(await configPromise).unstable_honoEnhancer || ((createApp) => createApp);

export const handler = handle(honoEnhancer(createApp)(new Hono()));
`;

export function deployAwsLambdaPlugin(opts: {
Expand Down
43 changes: 28 additions & 15 deletions packages/waku/src/lib/plugins/vite-plugin-deploy-cloudflare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving entries.loadConfig() from the top-level to into the fetch handler was the fix for the strange "Cannot access 'loadConfig' before initialization" ReferenceError it was hitting when deployed to Cloudflare.

const honoEnhancer =
config.unstable_honoEnhancer || ((createApp) => createApp);
app = honoEnhancer(createApp)(new Hono());
}
return app.fetch(request, env, ctx);
},
};
Expand Down
46 changes: 28 additions & 18 deletions packages/waku/src/lib/plugins/vite-plugin-deploy-partykit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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);
},
};
Expand Down
Loading