Skip to content

Commit

Permalink
refactor: rename hono runner (#926)
Browse files Browse the repository at this point in the history
to serverEngine.
  • Loading branch information
dai-shi authored Oct 2, 2024
1 parent 228421b commit 8b56318
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 24 deletions.
8 changes: 5 additions & 3 deletions packages/waku/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { serveStatic } from '@hono/node-server/serve-static';
import * as dotenv from 'dotenv';

import type { Config } from './config.js';
import { runner } from './lib/hono/runner.js';
import { serverEngine } from './lib/hono/engine.js';
import { build } from './lib/builder/build.js';
import { DIST_ENTRIES_JS, DIST_PUBLIC } from './lib/builder/constants.js';

Expand Down Expand Up @@ -104,7 +104,7 @@ async function runDev() {
if (values['experimental-compress']) {
app.use(compress());
}
app.use(runner({ 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 Down Expand Up @@ -156,7 +156,9 @@ async function runStart() {
app.use(compress());
}
app.use(serveStatic({ root: path.join(distDir, DIST_PUBLIC) }));
app.use(runner({ cmd: 'start', loadEntries, env: process.env as any }));
app.use(
serverEngine({ cmd: 'start', loadEntries, env: process.env as any }),
);
app.notFound((c) => {
// FIXME better implementation using node stream?
const file = path.join(distDir, DIST_PUBLIC, '404.html');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ const createEmptyReadableStream = () =>
},
});

// Middleware runner (Is there a better name?)
export const runner = (options: MiddlewareOptions): MiddlewareHandler => {
// serverEngine returns hono middleware that runs Waku middleware.
export const serverEngine = (options: MiddlewareOptions): MiddlewareHandler => {
const entriesPromise =
options.cmd === 'start'
? options.loadEntries()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const getServeJsContent = (
) => `
import path from 'node:path';
import { existsSync, readFileSync } from 'node:fs';
import { runner, 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 @@ -29,7 +29,7 @@ const loadEntries = () => import('${srcEntriesFile}');
const app = new Hono();
app.use(serveStatic({ root: distDir + '/' + publicDir }));
app.use(runner({ cmd: 'start', loadEntries, env: process.env }));
app.use(serverEngine({ cmd: 'start', loadEntries, env: process.env }));
app.notFound(async (c) => {
const file = path.join(distDir, publicDir, '404.html');
if (existsSync(file)) {
Expand Down
10 changes: 5 additions & 5 deletions packages/waku/src/lib/plugins/vite-plugin-deploy-cloudflare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ import { DIST_ENTRIES_JS, DIST_PUBLIC } from '../builder/constants.js';
const SERVE_JS = 'serve-cloudflare.js';

const getServeJsContent = (srcEntriesFile: string) => `
import { runner, importHono } from 'waku/unstable_hono';
import { serverEngine, importHono } from 'waku/unstable_hono';
const { Hono } = await importHono();
const loadEntries = () => import('${srcEntriesFile}');
let serveWaku;
let serve;
const app = new Hono();
app.use((c, next) => serveWaku(c, next));
app.use((c, next) => serve(c, next));
app.notFound(async (c) => {
const assetsFetcher = c.env.ASSETS;
const url = new URL(c.req.raw.url);
Expand All @@ -41,8 +41,8 @@ app.notFound(async (c) => {
export default {
async fetch(request, env, ctx) {
if (!serveWaku) {
serveWaku = runner({ cmd: 'start', loadEntries, env });
if (!serve) {
serve = serverEngine({ cmd: 'start', loadEntries, env });
}
return app.fetch(request, env, ctx);
},
Expand Down
4 changes: 2 additions & 2 deletions packages/waku/src/lib/plugins/vite-plugin-deploy-deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const getServeJsContent = (
) => `
import { Hono } from 'jsr:@hono/hono';
import { serveStatic } from 'jsr:@hono/hono/deno';
import { runner } from 'waku/unstable_hono';
import { serverEngine } from 'waku/unstable_hono';
const distDir = '${distDir}';
const publicDir = '${distPublic}';
Expand All @@ -22,7 +22,7 @@ const env = Deno.env.toObject();
const app = new Hono();
app.use(serveStatic({ root: distDir + '/' + publicDir }));
app.use(runner({ cmd: 'start', loadEntries, env }));
app.use(serverEngine({ cmd: 'start', loadEntries, env }));
app.notFound(async (c) => {
const file = distDir + '/' + publicDir + '/404.html';
try {
Expand Down
4 changes: 2 additions & 2 deletions packages/waku/src/lib/plugins/vite-plugin-deploy-netlify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ 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 { serverEngine, importHono } from 'waku/unstable_hono';
const { Hono } = await importHono();
const loadEntries = () => import('${srcEntriesFile}');
const app = new Hono();
app.use(runner({ cmd: 'start', loadEntries, env: process.env }));
app.use(serverEngine({ cmd: 'start', loadEntries, env: process.env }));
app.notFound((c) => {
const notFoundHtml = globalThis.__WAKU_NOT_FOUND_HTML__;
if (typeof notFoundHtml === 'string') {
Expand Down
10 changes: 5 additions & 5 deletions packages/waku/src/lib/plugins/vite-plugin-deploy-partykit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import { DIST_PUBLIC } from '../builder/constants.js';
const SERVE_JS = 'serve-partykit.js';

const getServeJsContent = (srcEntriesFile: string) => `
import { runner, importHono } from 'waku/unstable_hono';
import { serverEngine, importHono } from 'waku/unstable_hono';
const { Hono } = await importHono();
const loadEntries = () => import('${srcEntriesFile}');
let serveWaku;
let serve;
const app = new Hono();
app.use((c, next) => serveWaku(c, next));
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
Expand All @@ -36,8 +36,8 @@ app.notFound(async (c) => {
export default {
onFetch(request, lobby, ctx) {
if (!serveWaku) {
serveWaku = runner({ cmd: 'start', loadEntries, env: lobby });
if (!serve) {
serve = serverEngine({ cmd: 'start', loadEntries, env: lobby });
}
return app.fetch(request, lobby, ctx);
},
Expand Down
4 changes: 2 additions & 2 deletions packages/waku/src/lib/plugins/vite-plugin-deploy-vercel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const getServeJsContent = (
) => `
import path from 'node:path';
import { existsSync, readFileSync } from 'node:fs';
import { runner, importHono, importHonoNodeServer } from 'waku/unstable_hono';
import { serverEngine, importHono, importHonoNodeServer } from 'waku/unstable_hono';
const { Hono } = await importHono();
const { getRequestListener } = await importHonoNodeServer();
Expand All @@ -25,7 +25,7 @@ const publicDir = '${distPublic}';
const loadEntries = () => import('${srcEntriesFile}');
const app = new Hono();
app.use(runner({ cmd: 'start', loadEntries, env: process.env }));
app.use(serverEngine({ cmd: 'start', loadEntries, env: process.env }));
app.notFound((c) => {
// FIXME better implementation using node stream?
const file = path.join(distDir, publicDir, '404.html');
Expand Down
2 changes: 1 addition & 1 deletion packages/waku/src/unstable_hono.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// These exports are for internal use only and subject to change without notice.

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

export const importHono = () => import('hono');
export const importHonoNodeServer: any = () => import('@hono/node-server');
Expand Down

0 comments on commit 8b56318

Please sign in to comment.