import { runWithAutoSuspense } from '@kitajs/html/suspense' import type { RequestHandler } from 'express' import { handleHtml } from './html' import { kAutoDoctype } from './utils' function buildDefaultGenReqId() { // Match Fastify's default request id generator to keep ids compact and on the V8 SMI path. const maxInt = 2147483647 let nextReqId = 0 return function defaultGenReqId() { nextReqId = (nextReqId + 1) & maxInt return `req-${nextReqId.toString(36)}` } } /** Options for @kitajs/express-html-plugin middleware. */ export interface ExpressKitaHtmlOptions { /** * Whether to automatically add `` to a response starting with , if * not found. * * @default true */ autoDoctype?: boolean /** * Whether the middleware should avoid assigning `req.id`. * * Disable this if another middleware already owns request ids. When disabled, you must * ensure `req.id` is available before using `Suspense rid={req.id}`. * * @default false */ disableRequestId?: boolean /** * Whether to provide the request ID to `AutoSuspense` components automatically. * * When `disableRequestId` is also enabled, earlier middleware must assign `req.id`. * Regular `Suspense` components with an explicit `rid` do not require this option. * * @default false */ autoSuspense?: boolean } /** * The `@kitajs/express-html-plugin` middleware. * * It decorates `res.html()`, optionally assigns `req.id`, and enables Suspense streaming * by matching that request id with `Suspense rid={req.id}`. * * @example * * ```tsx * import express from 'express' * import { expressKitaHtml } from '@kitajs/express-html-plugin' * * const app = express() * * app.use(expressKitaHtml()) * * app.get('/', (req, res) => { * res.html( * *
*