From 12d479f1726e85fcc7ea48ca236b75056c3f9877 Mon Sep 17 00:00:00 2001 From: daishi Date: Wed, 18 Sep 2024 22:09:45 +0900 Subject: [PATCH] use getContext from Hono --- packages/waku/src/cli.ts | 3 +++ packages/waku/src/lib/hono/runner.ts | 18 ++---------------- .../src/lib/plugins/vite-plugin-deploy-deno.ts | 1 + .../lib/plugins/vite-plugin-deploy-netlify.ts | 4 +++- .../lib/plugins/vite-plugin-deploy-vercel.ts | 4 +++- packages/waku/src/unstable_hono.ts | 3 ++- 6 files changed, 14 insertions(+), 19 deletions(-) diff --git a/packages/waku/src/cli.ts b/packages/waku/src/cli.ts index e4cc90547..0af3d3d38 100644 --- a/packages/waku/src/cli.ts +++ b/packages/waku/src/cli.ts @@ -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'; @@ -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? @@ -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) => { diff --git a/packages/waku/src/lib/hono/runner.ts b/packages/waku/src/lib/hono/runner.ts index c17836b6a..08cf89c8c 100644 --- a/packages/waku/src/lib/hono/runner.ts +++ b/packages/waku/src/lib/hono/runner.ts @@ -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) { @@ -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) => { @@ -70,11 +64,3 @@ export const runner = (options: MiddlewareOptions): MiddlewareHandler => { await next(); }; }; - -export const getHonoContext = () => { - const c = unstable_getCustomContext()[HONO_CONTEXT]; - if (!c) { - throw new Error('Hono context is not available'); - } - return c as C; -}; diff --git a/packages/waku/src/lib/plugins/vite-plugin-deploy-deno.ts b/packages/waku/src/lib/plugins/vite-plugin-deploy-deno.ts index abe2f4d7a..6708ec2e0 100644 --- a/packages/waku/src/lib/plugins/vite-plugin-deploy-deno.ts +++ b/packages/waku/src/lib/plugins/vite-plugin-deploy-deno.ts @@ -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) => { diff --git a/packages/waku/src/lib/plugins/vite-plugin-deploy-netlify.ts b/packages/waku/src/lib/plugins/vite-plugin-deploy-netlify.ts index 10e82e1e5..c915ee701 100644 --- a/packages/waku/src/lib/plugins/vite-plugin-deploy-netlify.ts +++ b/packages/waku/src/lib/plugins/vite-plugin-deploy-netlify.ts @@ -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__; diff --git a/packages/waku/src/lib/plugins/vite-plugin-deploy-vercel.ts b/packages/waku/src/lib/plugins/vite-plugin-deploy-vercel.ts index a259201bb..3e6f51f9f 100644 --- a/packages/waku/src/lib/plugins/vite-plugin-deploy-vercel.ts +++ b/packages/waku/src/lib/plugins/vite-plugin-deploy-vercel.ts @@ -15,9 +15,10 @@ 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}'; @@ -25,6 +26,7 @@ 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? diff --git a/packages/waku/src/unstable_hono.ts b/packages/waku/src/unstable_hono.ts index 72c962efe..197c8fb46 100644 --- a/packages/waku/src/unstable_hono.ts +++ b/packages/waku/src/unstable_hono.ts @@ -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');