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(hono): enable context storage #891

Merged
merged 1 commit into from
Sep 19, 2024
Merged
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
3 changes: 3 additions & 0 deletions packages/waku/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { pathToFileURL } from 'node:url';
import { parseArgs } from 'node:util';
import { createRequire } from 'node:module';
import { Hono } from 'hono';
import { contextStorage } from 'hono/context-storage';
import { serve } from '@hono/node-server';
import { serveStatic } from '@hono/node-server/serve-static';
import * as dotenv from 'dotenv';
Expand Down Expand Up @@ -95,6 +96,7 @@ if (values.version) {
async function runDev() {
const config = await loadConfig();
const app = new Hono();
app.use(contextStorage());
app.use('*', runner({ cmd: 'dev', config, env: process.env as any }));
app.notFound((c) => {
// FIXME can we avoid hardcoding the public path?
Expand Down Expand Up @@ -138,6 +140,7 @@ async function runStart() {
const loadEntries = () =>
import(pathToFileURL(path.resolve(distDir, DIST_ENTRIES_JS)).toString());
const app = new Hono();
app.use(contextStorage());
app.use('*', serveStatic({ root: path.join(distDir, DIST_PUBLIC) }));
app.use('*', runner({ cmd: 'start', loadEntries, env: process.env as any }));
app.notFound((c) => {
Expand Down
18 changes: 2 additions & 16 deletions packages/waku/src/lib/hono/runner.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import type { Context, MiddlewareHandler } from 'hono';
import type { MiddlewareHandler } from 'hono';

import { unstable_getCustomContext } from '../../server.js';
import { resolveConfig } from '../config.js';
import type { HandlerContext, MiddlewareOptions } from '../middleware/types.js';

// Internal context key
const HONO_CONTEXT = '__hono_context';

const createEmptyReadableStream = () =>
new ReadableStream({
start(controller) {
Expand Down Expand Up @@ -42,9 +38,7 @@ export const runner = (options: MiddlewareOptions): MiddlewareHandler => {
headers: c.req.header(),
},
res: {},
context: {
[HONO_CONTEXT]: c,
},
context: {},
};
const handlers = await handlersPromise;
const run = async (index: number) => {
Expand All @@ -70,11 +64,3 @@ export const runner = (options: MiddlewareOptions): MiddlewareHandler => {
await next();
};
};

export const getHonoContext = <C extends Context = Context>() => {
const c = unstable_getCustomContext()[HONO_CONTEXT];
if (!c) {
throw new Error('Hono context is not available');
}
return c as C;
};
1 change: 1 addition & 0 deletions packages/waku/src/lib/plugins/vite-plugin-deploy-deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const loadEntries = () => import('${srcEntriesFile}');
const env = Deno.env.toObject();

const app = new Hono();
// app.use(contextStorage()); // Hono v4.6 is not available on deno.land
app.use('*', serveStatic({ root: distDir + '/' + publicDir }));
app.use('*', runner({ cmd: 'start', loadEntries, env }));
app.notFound(async (c) => {
Expand Down
4 changes: 3 additions & 1 deletion packages/waku/src/lib/plugins/vite-plugin-deploy-netlify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import { DIST_PUBLIC } from '../builder/constants.js';
const SERVE_JS = 'serve-netlify.js';

const getServeJsContent = (srcEntriesFile: string) => `
import { runner, importHono } from 'waku/unstable_hono';
import { runner, importHono, importHonoContextStorage } from 'waku/unstable_hono';

const { Hono } = await importHono();
const { contextStorage } = await importHonoContextStorage();

const loadEntries = () => import('${srcEntriesFile}');

const app = new Hono();
app.use(contextStorage());
app.use('*', runner({ cmd: 'start', loadEntries, env: process.env }));
app.notFound((c) => {
const notFoundHtml = globalThis.__WAKU_NOT_FOUND_HTML__;
Expand Down
4 changes: 3 additions & 1 deletion packages/waku/src/lib/plugins/vite-plugin-deploy-vercel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,18 @@ const getServeJsContent = (
) => `
import path from 'node:path';
import { existsSync, readFileSync } from 'node:fs';
import { runner, importHono, importHonoNodeServer } from 'waku/unstable_hono';
import { runner, importHono, importHonoContextStorage, importHonoNodeServer } from 'waku/unstable_hono';

const { Hono } = await importHono();
const { contextStorage } = await importHonoContextStorage();
const { getRequestListener } = await importHonoNodeServer();

const distDir = '${distDir}';
const publicDir = '${distPublic}';
const loadEntries = () => import('${srcEntriesFile}');

const app = new Hono();
app.use(contextStorage());
app.use('*', runner({ cmd: 'start', loadEntries, env: process.env }));
app.notFound((c) => {
// FIXME better implementation using node stream?
Expand Down
3 changes: 2 additions & 1 deletion packages/waku/src/unstable_hono.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// These exports are for internal use only and subject to change without notice.

export { runner, getHonoContext } from './lib/hono/runner.js';
export { runner } from './lib/hono/runner.js';

export const importHono = () => import('hono');
export const importHonoContextStorage = () => import('hono/context-storage');
export const importHonoNodeServer: any = () => import('@hono/node-server');
export const importHonoNodeServerServeStatic = () =>
import('@hono/node-server/serve-static');
Loading