From 9447ef48a16c9b36fb2e899ae8c4f3d7eae7c5fa Mon Sep 17 00:00:00 2001 From: yjl9903 Date: Tue, 1 Oct 2024 15:33:35 +0800 Subject: [PATCH] feat(web): add node adapter --- apps/frontend/web/cloudflare/entry.ts | 8 +++--- apps/frontend/web/cloudflare/remix.ts | 2 ++ apps/frontend/web/node/api.ts | 7 +++++ apps/frontend/web/node/entry.ts | 26 +++++++++++++++++++ apps/frontend/web/node/remix.ts | 22 ++++++++++++++++ .../web/{cloudflare => node}/types.ts | 0 6 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 apps/frontend/web/node/api.ts create mode 100644 apps/frontend/web/node/entry.ts create mode 100644 apps/frontend/web/node/remix.ts rename apps/frontend/web/{cloudflare => node}/types.ts (100%) diff --git a/apps/frontend/web/cloudflare/entry.ts b/apps/frontend/web/cloudflare/entry.ts index 31593b47..aac9542a 100644 --- a/apps/frontend/web/cloudflare/entry.ts +++ b/apps/frontend/web/cloudflare/entry.ts @@ -8,7 +8,9 @@ import * as build from '../build/server'; // eslint-disable-line import/no-unres // @ts-ignore import __STATIC_CONTENT_MANIFEST from '__STATIC_CONTENT_MANIFEST'; -import type { Bindings } from './types'; +import type { Bindings } from '../node/types'; + +import { api } from '../node/api'; import { remix } from './remix'; @@ -16,9 +18,7 @@ export const app = new Hono<{ Bindings: Bindings }>(); app.all('*', remix({ build, manifest: __STATIC_CONTENT_MANIFEST })); -app.all('/api/*', (c) => { - return c.json({ ok: false }); -}); +app.all('/api/*', api()); app.onError((err, c) => { if (err.message) { diff --git a/apps/frontend/web/cloudflare/remix.ts b/apps/frontend/web/cloudflare/remix.ts index 5b2cc6f0..b1d381d2 100644 --- a/apps/frontend/web/cloudflare/remix.ts +++ b/apps/frontend/web/cloudflare/remix.ts @@ -13,6 +13,8 @@ export const remix = (options: RemixHandlerOptions): Handle const handleRemixRequest = createRequestHandler(options.build); return async (ctx) => { + console.log('ctx', ctx, ctx.env, ctx.var); + const waitUntil = ctx.event.waitUntil.bind(ctx.event); const passThroughOnException = ctx.event.passThroughOnException.bind(ctx.event); const request = ctx.req.raw; diff --git a/apps/frontend/web/node/api.ts b/apps/frontend/web/node/api.ts new file mode 100644 index 00000000..9e939551 --- /dev/null +++ b/apps/frontend/web/node/api.ts @@ -0,0 +1,7 @@ +import type { Handler, Env } from 'hono'; + +export const api = (): Handler => { + return async (ctx) => { + return ctx.json({ ok: false }); + }; +}; diff --git a/apps/frontend/web/node/entry.ts b/apps/frontend/web/node/entry.ts new file mode 100644 index 00000000..e6d4e53b --- /dev/null +++ b/apps/frontend/web/node/entry.ts @@ -0,0 +1,26 @@ +import { Hono } from 'hono'; + +// eslint-disable-next-line @typescript-eslint/ban-ts-comment +// @ts-ignore This file won’t exist if it hasn’t yet been built +import * as build from '../build/server'; // eslint-disable-line import/no-unresolved + +import type { Bindings } from './types'; + +import { api } from './api'; +import { remix } from './remix'; + +export const app = new Hono<{ Bindings: Bindings }>(); + +app.all('*', remix({ build })); + +app.all('/api/*', api()); + +app.onError((err, c) => { + if (err.message) { + console.log(...err.message.trim().split('\n')); + } + if (err.stack) { + console.log(...err.stack.trim().split('\n')); + } + return c.json({ status: 500, messsage: err?.message ?? 'Internal Error' }, 500); +}); diff --git a/apps/frontend/web/node/remix.ts b/apps/frontend/web/node/remix.ts new file mode 100644 index 00000000..6bd516ed --- /dev/null +++ b/apps/frontend/web/node/remix.ts @@ -0,0 +1,22 @@ +import type { Handler, Env } from 'hono'; + +import { createRequestHandler, type ServerBuild } from '@remix-run/node'; + +export interface RemixHandlerOptions { + build: ServerBuild; +} + +export const remix = (options: RemixHandlerOptions): Handler => { + const handleRemixRequest = createRequestHandler(options.build); + + return async (ctx) => { + const request = ctx.req.raw; + + try { + const loadContext = {}; + return await handleRemixRequest(request, loadContext); + } catch (error) { + throw error; + } + }; +}; diff --git a/apps/frontend/web/cloudflare/types.ts b/apps/frontend/web/node/types.ts similarity index 100% rename from apps/frontend/web/cloudflare/types.ts rename to apps/frontend/web/node/types.ts