|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +import { ɵAngularAppEngine as AngularAppEngine } from '@angular/ssr'; |
| 10 | +import type { IncomingMessage } from 'http'; |
| 11 | +import { createWebRequestFromNodeRequest } from './request'; |
| 12 | + |
| 13 | +/** |
| 14 | + * Angular server application engine. |
| 15 | + * Manages Angular server applications (including localized ones) and handles rendering requests. |
| 16 | +
|
| 17 | + * @developerPreview |
| 18 | + */ |
| 19 | +export interface AngularNodeServerAppManager { |
| 20 | + /** |
| 21 | + * Renders an HTTP response based on the incoming request using the Angular server application. |
| 22 | + * |
| 23 | + * The method processes the incoming request, determines the appropriate route, and prepares the |
| 24 | + * rendering context to generate a response. If the request URL corresponds to a static file (excluding `/index.html`), |
| 25 | + * the method returns `null`. |
| 26 | + * |
| 27 | + * Example: A request to `https://www.example.com/page/index.html` will render the Angular route |
| 28 | + * associated with `https://www.example.com/page`. |
| 29 | + * |
| 30 | + * @param request - The incoming HTTP request object to be rendered. It can be a `Request` or `IncomingMessage` object. |
| 31 | + * @param requestContext - Optional additional context for the request, such as metadata or custom settings. |
| 32 | + * @returns A promise that resolves to a `Response` object, or `null` if the request URL is for a static file |
| 33 | + * (e.g., `./logo.png`) rather than an application route. |
| 34 | + */ |
| 35 | + render(request: Request | IncomingMessage, requestContext?: unknown): Promise<Response | null>; |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * Angular server application engine. |
| 40 | + * Manages Angular server applications (including localized ones), handles rendering requests, |
| 41 | + * and optionally transforms index HTML before rendering. |
| 42 | + */ |
| 43 | +export class AngularNodeAppEngine extends AngularAppEngine implements AngularNodeServerAppManager { |
| 44 | + /** |
| 45 | + * Renders an HTTP response based on the incoming request using the Angular server application. |
| 46 | + * |
| 47 | + * The method processes the incoming request, determines the appropriate route, and prepares the |
| 48 | + * rendering context to generate a response. If the request URL corresponds to a static file (excluding `/index.html`), |
| 49 | + * the method returns `null`. |
| 50 | + * |
| 51 | + * Example: A request to `https://www.example.com/page/index.html` will render the Angular route |
| 52 | + * associated with `https://www.example.com/page`. |
| 53 | + * |
| 54 | + * @param request - The incoming HTTP request object to be rendered. It can be a `Request` or `IncomingMessage` object. |
| 55 | + * @param requestContext - Optional additional context for the request, such as metadata or custom settings. |
| 56 | + * @returns A promise that resolves to a `Response` object, or `null` if the request URL is for a static file |
| 57 | + * (e.g., `./logo.png`) rather than an application route. |
| 58 | + */ |
| 59 | + override render( |
| 60 | + request: Request | IncomingMessage, |
| 61 | + requestContext?: unknown, |
| 62 | + ): Promise<Response | null> { |
| 63 | + const webReq = request instanceof Request ? request : createWebRequestFromNodeRequest(request); |
| 64 | + |
| 65 | + return super.render(webReq, requestContext); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +let angularNodeAppEngine: AngularNodeAppEngine | undefined; |
| 70 | + |
| 71 | +/** |
| 72 | + * Retrieves the existing `AngularNodeAppEngine` instance or creates a new one if it doesn't exist. |
| 73 | + * |
| 74 | + * This method ensures that a single instance of `AngularNodeAppEngine` is used throughout the application's lifecycle, |
| 75 | + * promoting efficient resource management. If an instance does not exist, it will be instantiated upon the first call. |
| 76 | + * |
| 77 | + * @developerPreview |
| 78 | + * @returns The existing or newly created instance of `AngularNodeAppEngine`. |
| 79 | + */ |
| 80 | +export function getOrCreateAngularNodeAppEngine(): AngularNodeServerAppManager { |
| 81 | + return (angularNodeAppEngine ??= new AngularNodeAppEngine()); |
| 82 | +} |
| 83 | + |
| 84 | +/** |
| 85 | + * Destroys the current `AngularNodeAppEngine` instance and releases any associated resources. |
| 86 | + * |
| 87 | + * This method resets the reference to the `AngularNodeAppEngine` instance to `undefined`, allowing for the creation |
| 88 | + * of a new instance on subsequent calls to `getOrCreateAngularNodeAppEngine()`. This is typically used when |
| 89 | + * reinitializing the server environment or refreshing the application state. |
| 90 | + * |
| 91 | + * @developerPreview |
| 92 | + */ |
| 93 | +export function destroyAngularNodeAppEngine(): void { |
| 94 | + angularNodeAppEngine = undefined; |
| 95 | +} |
0 commit comments