import * as _homer0_jimple from '@homer0/jimple'; import { i as Events, J as Jimpex, D as MiddlewareConnectFn } from '../../index-DAUqbQ6h.js'; import { AsyncExpressMiddleware } from '../../types/express.js'; import { SendFile } from '../../services/common/sendFile.js'; import { HTMLGenerator } from '../../services/html/htmlGenerator.js'; import { Response, NextFunction } from 'express'; import '../../types/http.js'; import 'https'; import 'http'; import 'spdy'; import 'node-fetch'; import '../../types/utils.js'; import '@homer0/path-utils'; import '@homer0/simple-logger'; import '@homer0/simple-config'; import '@homer0/events-hub'; import '../../services/frontend/frontendFs.js'; import 'node:fs/promises'; import '@homer0/deferred'; /** * The options to customize the behavior of the middleware. * * @group Middlewares/FastHTML */ type FastHTMLOptions = { /** * The name of the file the middleware will serve. If the {@link HTMLGenerator} service * is available, it will be overridden by the service. * * @default 'index.html' */ file: string; /** * A list of regular expressions to match request paths that should be ignored by the * middleware. * * @default [/\.ico$/i] */ ignoredRoutes: RegExp[]; /** * If `true`, {@link FastHTML} will get the list of all the routes controlled by the * application, and will use them to validate the incoming requests (in addition to the * `ignore` list): If a request URL doesn't match with any of the "controlled routes", * it will serve the HTML file. * * @default true */ useAppRoutes: boolean; }; /** * The options to construct a {@link FastHTML}. * * @group Middlewares/FastHTML */ type FastHTMLConstructorOptions = Partial & { /** * A dictionary with the dependencies to inject. */ inject: { sendFile: SendFile; events: Events; /** * A function to get a possible {@link HTMLGenerator}. This is injected as a "getter" * to not interrupt the DIC "lifecycle": middlewares are initialized right when the * app starts, and injecting a reference would force the service to be initialized * too, even if a request is not being made. */ getHTMLGenerator?: () => HTMLGenerator | undefined; }; }; /** * The options for the middleware creator that will mount an instance of {@link FastHTML}. * * @group Middlewares/FastHTML */ type FastHTMLMiddlewareOptions = Partial & { /** * The name of an {@link HTMLGenerator} service already available in the application. * * @default 'htmlGenerator' */ htmlGeneratorServiceName?: string; }; /** * It's common for an app to show an HTML view when no route was able to handle a request, * so the idea behind this middleware is to avoid going to every middleware and controller * and just specify that if the request is not for a route handled by a controller, just * serve the HTML and avoid processing unnecessary data. * * A simple example: The app has a route `/backend` that a frontend uses to get * information. * This middleware can be used to only allow the execution of middlewares and controllers * when the request route is for `/backend`. * * **Disclaimer**: Managing statics files with Express is not a best practice, but there * are scenarios where there is not other choice. * * @group Middleware Classes * @group Middlewares/FastHTML * @prettierignore */ declare class FastHTML { /** * The service that serves a file. */ protected readonly _sendFile: SendFile; /** * The application event bus, to listen and get the list of "controlled routes" after * the application is started. */ protected readonly _events: Events; /** * A function to get a possible {@link HTMLGenerator}. This is injected as a "getter" * to not interrupt the DIC "lifecycle": middlewares are initialized right when the * app starts, and injecting a reference would force the service to be initialized * too, even if a request is not being made. */ protected readonly _getHTMLGenerator: () => HTMLGenerator | undefined; /** * The customization options for the middleware. */ protected _options: FastHTMLOptions; /** * Whether or not the file is ready to be served. In case the middleware uses an * {@link HTMLGenerator} service, the file needs to be generated before being available, * and that's why this flag exists. */ protected _fileReady: boolean; /** * A list of regular expression that match the routes controlled by the application. * This is in case the `useAppRoutes` option is set to `true`; when the application gets * started, an event listener will obtain all the top controlled routes, create regular * expressions, and save them on this property. */ protected _routeExpressions: RegExp[]; /** * @param options The options to construct the class. * @throws If `ignoredRoutes` is empty and `useAppRoutes` is set to `false`. */ constructor({ inject, ...options }: FastHTMLConstructorOptions); /** * Generates the middleware that serves the HTML file. */ getMiddleware(): AsyncExpressMiddleware; /** * The customization options. */ get options(): Readonly; /** * Serves the HTML file to the response. * * @param res The response object generated by the application. * @param next The function to call the next middleware. */ protected _sendResponse(res: Response, next: NextFunction): void; /** * Adds the event listener that obtains the list of "controlled routes" when * `useAppRoutes` is set to `true`. */ protected _setupEvents(): void; /** * Validates whether a route should be ignored or not. The method checks first against * the `ignore` option, and then against the list of "controlled routes" if * `useAppRoutes` is set to `true`. * * @param route The route to validate. */ protected _shouldIgnoreRoute(route: string): boolean; } /** * Creates the middleware that filters the routes and serves an HTML before the * application gets to evaluate whether there's a controller for the request or not. * * @group Middlewares * @group Middlewares/FastHTML */ declare const fastHTMLMiddleware: _homer0_jimple.ResourceCreator<"middleware", "connect", (options?: FastHTMLMiddlewareOptions) => (app: ContainerType) => AsyncExpressMiddleware, MiddlewareConnectFn>; export { FastHTML, type FastHTMLConstructorOptions, type FastHTMLMiddlewareOptions, type FastHTMLOptions, fastHTMLMiddleware };