diff --git a/packages/vike-node/src/connect.ts b/packages/vike-node/src/connect.ts deleted file mode 100644 index d10bf7f..0000000 --- a/packages/vike-node/src/connect.ts +++ /dev/null @@ -1 +0,0 @@ -export { vike, vike as default } from './runtime/frameworks/connect.js' diff --git a/packages/vike-node/src/elysia.ts b/packages/vike-node/src/elysia.ts deleted file mode 100644 index a5a013c..0000000 --- a/packages/vike-node/src/elysia.ts +++ /dev/null @@ -1 +0,0 @@ -export { vike, vike as default } from './runtime/frameworks/elysia.js' diff --git a/packages/vike-node/src/fastify.ts b/packages/vike-node/src/fastify.ts deleted file mode 100644 index 924fcee..0000000 --- a/packages/vike-node/src/fastify.ts +++ /dev/null @@ -1 +0,0 @@ -export { vike, vike as default } from './runtime/frameworks/fastify.js' diff --git a/packages/vike-node/src/h3.ts b/packages/vike-node/src/h3.ts deleted file mode 100644 index 3e8c60f..0000000 --- a/packages/vike-node/src/h3.ts +++ /dev/null @@ -1 +0,0 @@ -export { vike, vike as default } from './runtime/frameworks/h3.js' diff --git a/packages/vike-node/src/hono.ts b/packages/vike-node/src/hono.ts deleted file mode 100644 index 0f46171..0000000 --- a/packages/vike-node/src/hono.ts +++ /dev/null @@ -1 +0,0 @@ -export { vike, vike as default } from './runtime/frameworks/hono.js' diff --git a/packages/vike-node/src/runtime/frameworks/connect.ts b/packages/vike-node/src/runtime/frameworks/connect.ts deleted file mode 100644 index 8c30782..0000000 --- a/packages/vike-node/src/runtime/frameworks/connect.ts +++ /dev/null @@ -1,44 +0,0 @@ -export { vike } - -import type { IncomingMessage, ServerResponse } from 'http' -import { globalStore } from '../globalStore.js' -import type { NextFunction, VikeOptions } from '../types.js' - -/** - * Creates middleware for Express-like frameworks to handle Vike requests. - * - * @template PlatformRequest - The type of the request object, extending IncomingMessage. - * @template PlatformResponse - The type of the response object, extending ServerResponse. - * - * @param {VikeOptions} [options] - Configuration options for Vike. - * - * @returns {(req: PlatformRequest, res: PlatformResponse, next?: NextFunction) => void} - * A single middleware function that handles Vike requests. This function: - * 1. Checks for and handles HMR WebSocket upgrade requests. - * 2. Processes regular requests using Vike's handler. - * 3. Calls the next middleware if the request is not handled by Vike. - * - * @example - * ```js - * import express from 'express' - * import { vike } from 'vike-node/connect' - * - * const app = express() - * app.use(vike()) - * ``` - * - */ -function vike( - options?: VikeOptions -): (req: PlatformRequest, res: PlatformResponse, next?: NextFunction) => void { - const handler = createHandler(options) - return (req, res, next) => { - globalStore.setupHMRProxy(req) - handler({ - req, - res, - next, - platformRequest: req - }) - } -} diff --git a/packages/vike-node/src/runtime/frameworks/elysia.ts b/packages/vike-node/src/runtime/frameworks/elysia.ts deleted file mode 100644 index a189dda..0000000 --- a/packages/vike-node/src/runtime/frameworks/elysia.ts +++ /dev/null @@ -1,44 +0,0 @@ -export { vike } - -import { type Context, Elysia, NotFoundError } from 'elysia' -import { createHandler } from '../handler-web-and-node.js' -import type { VikeOptions } from '../types.js' - -/** - * Creates an Elysia plugin to handle Vike requests. - * - * @param {VikeOptions} [options] - Configuration options for Vike. - * - * @returns {Elysia} An Elysia plugin that handles all GET requests and processes them with Vike. - * - * @description - * The plugin: - * 1. Sets up a catch-all GET route handler that processes requests using Vike's handler. - * 2. Throws a NotFoundError if Vike doesn't handle the request, allowing Elysia to manage 404 responses. - * - * @example - * ```js - * import { Elysia } from 'elysia' - * import { vike } from 'vike-node/elysia' - * - * const app = new Elysia() - * app.use(vike()) - * app.listen(3000) - * ``` - * - * @throws {NotFoundError} Thrown when Vike doesn't handle the request, allowing Elysia to manage 404 responses. - */ -function vike(options?: VikeOptions): Elysia { - const handler = createHandler(options) - return new Elysia({ - name: 'vike-node:elysia' - }).get('*', async (ctx) => { - const response = await handler({ request: ctx.request, platformRequest: ctx }) - - if (response) { - return response - } - - throw new NotFoundError() - }) -} diff --git a/packages/vike-node/src/runtime/frameworks/fastify.ts b/packages/vike-node/src/runtime/frameworks/fastify.ts deleted file mode 100644 index ec8333d..0000000 --- a/packages/vike-node/src/runtime/frameworks/fastify.ts +++ /dev/null @@ -1,56 +0,0 @@ -export { vike } - -import type { FastifyPluginCallback, FastifyRequest } from 'fastify' -import { createServerResponse } from '../adapters/createServerResponse.js' -import { globalStore } from '../globalStore.js' -import { createHandler } from '../handler-node-only.js' -import type { VikeOptions } from '../types.js' - -/** - * Creates a Fastify plugin to handle Vike requests and Hot Module Replacement (HMR). - * - * @param {VikeOptions} [options] - Configuration options for Vike. - * - * @returns {FastifyPluginCallback} A Fastify plugin that handles all GET requests and processes them with Vike. - * - * @description - * This function creates a Fastify plugin that integrates Vike's server-side rendering capabilities - * and handles Hot Module Replacement (HMR) for development environments. The plugin: - * 1. Checks for and handles HMR WebSocket upgrade requests. - * 2. Processes regular requests using Vike's handler. - * 3. If Vike doesn't handle the request, it calls Fastify's `reply.callNotFound()`. - * - * @example - * ```js - * import fastify from 'fastify' - * import { vike } from 'vike-node/fastify' - * - * const app = fastify() - * app.register(vike()) - * ``` - * - */ -function vike(options?: VikeOptions): FastifyPluginCallback { - const handler = createHandler(options) - return function plugin(instance, _options, done) { - instance.get('*', async (req, reply) => { - globalStore.setupHMRProxy(req.raw) - const { res, onReadable } = createServerResponse(req.raw) - onReadable(({ readable, headers, statusCode }) => { - reply.code(statusCode) - reply.headers(headers) - reply.send(readable) - }) - await handler({ - req: req.raw, - res, - platformRequest: req, - next() { - reply.callNotFound() - } - }) - }) - - done() - } -} diff --git a/packages/vike-node/src/runtime/frameworks/h3.ts b/packages/vike-node/src/runtime/frameworks/h3.ts deleted file mode 100644 index 0c60e88..0000000 --- a/packages/vike-node/src/runtime/frameworks/h3.ts +++ /dev/null @@ -1,50 +0,0 @@ -export { vike } - -import { eventHandler, EventHandler } from 'h3' -import type { IncomingMessage } from 'node:http' -import { globalStore } from '../globalStore.js' -import { createHandler } from '../handler-node-only.js' -import type { VikeOptions } from '../types.js' - -/** - * Creates an h3 event handler to process Vike requests. - * - * @param {VikeOptions} [options] - Configuration options for Vike. - * - * @returns {EventHandler} An h3 event handler that processes requests with Vike. - * - * @description - * This function creates an h3 event handler that integrates Vike's server-side rendering capabilities. - * The handler: - * 1. Checks for and handles HMR WebSocket upgrade requests. - * 2. Processes regular requests using Vike's handler. - * - * @example - * ```js - * import { createApp } from 'h3' - * import { vike } from 'vike-node/h3' - * - * const app = createApp() - * app.use(vike()) - * - * ``` - * - * @remarks - * - This handler directly uses Node.js' IncomingMessage and ServerResponse objects from the h3 event. - * - */ -function vike(options?: VikeOptions): EventHandler { - const handler = createHandler(options) - return eventHandler(async (event) => { - const { - node: { req, res } - } = event - - globalStore.setupHMRProxy(req) - await handler({ - req, - res, - platformRequest: req - }) - }) -} diff --git a/packages/vike-node/src/runtime/frameworks/hono.ts b/packages/vike-node/src/runtime/frameworks/hono.ts deleted file mode 100644 index b4355c3..0000000 --- a/packages/vike-node/src/runtime/frameworks/hono.ts +++ /dev/null @@ -1,58 +0,0 @@ -export { vike } - -import type { Context, MiddlewareHandler } from 'hono' -import type { IncomingMessage } from 'http' -import { globalStore } from '../globalStore.js' -import type { VikeOptions } from '../types.js' - -/** - * Creates a Hono middleware to handle Vike requests and HMR (Hot Module Replacement). - * - * @param {VikeOptions} [options] - Configuration options for Vike. - * - * @returns {MiddlewareHandler} A Hono middleware function that processes requests with Vike. - * - * @description - * This function creates a Hono middleware that integrates Vike's server-side rendering capabilities - * and handles Hot Module Replacement (HMR) for development environments. The middleware: - * - * 1. Checks for and handles HMR WebSocket upgrade requests. - * 2. Processes regular requests using Vike's handler. - * 3. Adapts Node.js-style request handling to work with Web standard Response objects. - * 4. Allows pass-through to next middleware if Vike doesn't handle the request. - * - * @example - * ```js - * import { Hono } from 'hono' - * import { vike } from 'vike-node/hono' - * - * const app = new Hono() - * app.use('*', vike()) - * ``` - * - */ -function vike(options?: VikeOptions): MiddlewareHandler { - let handler: ReturnType> | undefined = undefined - return async function middleware(ctx, next) { - if (ctx.env.incoming) { - const req = ctx.env.incoming as IncomingMessage - globalStore.setupHMRProxy(req) - } - - if (!handler) { - const { createHandler } = await import('vike-node/__handler') - handler = createHandler(options) - } - const response = await handler({ - request: ctx.req.raw, - platformRequest: ctx - }) - - if (response) { - return response - } - - // If not handled by Vike, continue to next middleware - await next() - } -} diff --git a/packages/vike-node/src/runtime/vike-handler.ts b/packages/vike-node/src/runtime/vike-handler.ts index 50f6718..77ac5cb 100644 --- a/packages/vike-node/src/runtime/vike-handler.ts +++ b/packages/vike-node/src/runtime/vike-handler.ts @@ -1,6 +1,6 @@ import type { IncomingMessage, ServerResponse } from 'http' import compressMiddlewareFactory from '@universal-middleware/compress' -import { type Get, type UniversalHandler, type UniversalMiddleware, pipe } from '@universal-middleware/core' +import { type Get, type UniversalHandler, type UniversalMiddleware } from '@universal-middleware/core' import { renderPage as _renderPage } from 'vike/server' import { assert } from '../utils/assert.js' import { isVercel } from '../utils/isVercel.js' @@ -9,9 +9,9 @@ import { globalStore } from './globalStore.js' import type { ConnectMiddleware, VikeHttpResponse, VikeOptions } from './types.js' import { parseHeaders } from './utils/header-utils.js' -export { renderPage, renderPageWeb } +export { renderPage } -async function renderPage({ +async function renderPage({ url, headers, options @@ -33,30 +33,7 @@ async function renderPage({ return pageContext.httpResponse } -async function renderPageWeb({ - url, - headers, - options -}: { - url: string - headers: [string, string][] - platformRequest: PlatformRequest - options: VikeOptions -}) { - const httpResponse = await renderPage({ - url, - headers, - options - }) - if (!httpResponse) return undefined - - const { readable, writable } = new TransformStream() - httpResponse.pipe(writable) - - return new Response(readable, { status: httpResponse.statusCode, headers: httpResponse.headers }) -} - -export const renderPageCompress = ((options?) => async (request, context, runtime: any) => { +export const renderPageCompress = ((options?) => async (request, _context, runtime: any) => { const nodeReq: IncomingMessage | undefined = runtime.req const compressionType = options?.compress ?? !isVercel() const compressMiddleware = compressMiddlewareFactory()(request) @@ -130,9 +107,6 @@ export const renderPageHandler = ((options?) => async (request, context, runtime }) }) satisfies Get<[options: VikeOptions], UniversalHandler> -export const renderPageUniversal = ((options?) => - pipe(renderPageCompress(options), renderPageHandler(options))) satisfies Get<[options: VikeOptions], UniversalHandler> - const web = connectToWeb(handleViteDevServer) function handleViteDevServer(req: IncomingMessage, res: ServerResponse): Promise { diff --git a/packages/vike-node/src/vike.handler.ts b/packages/vike-node/src/vike.handler.ts index 0fed8f5..b497854 100644 --- a/packages/vike-node/src/vike.handler.ts +++ b/packages/vike-node/src/vike.handler.ts @@ -1,3 +1,10 @@ -import { renderPageUniversal } from './runtime/vike-handler.js' +import { Get, UniversalHandler, pipe } from '@universal-middleware/core' +import { VikeOptions } from './runtime/types.js' +import { renderPageCompress, renderPageHandler } from './runtime/vike-handler.js' + +const renderPageUniversal = ((options?) => pipe(renderPageCompress(options), renderPageHandler(options))) satisfies Get< + [options: VikeOptions], + UniversalHandler +> export default renderPageUniversal