Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions goldens/public-api/angular/ssr/node/index.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ export interface CommonEngineRenderOptions {
// @public
export function createWebRequestFromNodeRequest(nodeRequest: IncomingMessage): Request;

// @public
export function isMainModule(url: string): boolean;

// @public
export function writeResponseToNodeResponse(source: Response, destination: ServerResponse): Promise<void>;

Expand Down
1 change: 1 addition & 0 deletions packages/angular/ssr/node/public_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ export { AngularNodeAppEngine } from './src/app-engine';

export { writeResponseToNodeResponse } from './src/response';
export { createWebRequestFromNodeRequest } from './src/request';
export { isMainModule } from './src/module';
30 changes: 30 additions & 0 deletions packages/angular/ssr/node/src/module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/

import { argv } from 'node:process';
import { fileURLToPath } from 'node:url';

/**
* Determines whether the provided URL represents the main entry point module.
*
* This function checks if the provided URL corresponds to the main ESM module being executed directly.
* It's useful for conditionally executing code that should only run when a module is the entry point,
* such as starting a server or initializing an application.
*
* It performs two key checks:
* 1. Verifies if the URL starts with 'file:', ensuring it is a local file.
* 2. Compares the URL's resolved file path with the first command-line argument (`process.argv[1]`),
* which points to the file being executed.
*
* @param url The URL of the module to check. This should typically be `import.meta.url`.
* @returns `true` if the provided URL represents the main entry point, otherwise `false`.
* @developerPreview
*/
export function isMainModule(url: string): boolean {
return url.startsWith('file:') && argv[1] === fileURLToPath(url);
}