@ecopages/core 0.2.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +89 -0
- package/LICENSE +21 -0
- package/README.md +32 -0
- package/package.json +279 -0
- package/src/adapters/abstract/application-adapter.d.ts +168 -0
- package/src/adapters/abstract/application-adapter.js +109 -0
- package/src/adapters/abstract/application-adapter.ts +337 -0
- package/src/adapters/abstract/router-adapter.d.ts +26 -0
- package/src/adapters/abstract/router-adapter.js +5 -0
- package/src/adapters/abstract/router-adapter.ts +30 -0
- package/src/adapters/abstract/server-adapter.d.ts +69 -0
- package/src/adapters/abstract/server-adapter.js +15 -0
- package/src/adapters/abstract/server-adapter.ts +79 -0
- package/src/adapters/bun/client-bridge.d.ts +34 -0
- package/src/adapters/bun/client-bridge.js +48 -0
- package/src/adapters/bun/client-bridge.ts +62 -0
- package/src/adapters/bun/create-app.d.ts +60 -0
- package/src/adapters/bun/create-app.js +117 -0
- package/src/adapters/bun/create-app.ts +189 -0
- package/src/adapters/bun/define-api-handler.d.ts +61 -0
- package/src/adapters/bun/define-api-handler.js +15 -0
- package/src/adapters/bun/define-api-handler.ts +114 -0
- package/src/adapters/bun/hmr-manager.d.ts +84 -0
- package/src/adapters/bun/hmr-manager.js +227 -0
- package/src/adapters/bun/hmr-manager.ts +281 -0
- package/src/adapters/bun/index.d.ts +3 -0
- package/src/adapters/bun/index.js +8 -0
- package/src/adapters/bun/index.ts +3 -0
- package/src/adapters/bun/server-adapter.d.ts +155 -0
- package/src/adapters/bun/server-adapter.js +368 -0
- package/src/adapters/bun/server-adapter.ts +492 -0
- package/src/adapters/bun/server-lifecycle.d.ts +52 -0
- package/src/adapters/bun/server-lifecycle.js +120 -0
- package/src/adapters/bun/server-lifecycle.ts +154 -0
- package/src/adapters/index.d.ts +6 -0
- package/src/adapters/index.js +14 -0
- package/src/adapters/index.ts +6 -0
- package/src/adapters/node/create-app.d.ts +21 -0
- package/src/adapters/node/create-app.js +143 -0
- package/src/adapters/node/create-app.ts +179 -0
- package/src/adapters/node/index.d.ts +4 -0
- package/src/adapters/node/index.js +8 -0
- package/src/adapters/node/index.ts +9 -0
- package/src/adapters/node/node-client-bridge.d.ts +26 -0
- package/src/adapters/node/node-client-bridge.js +66 -0
- package/src/adapters/node/node-client-bridge.ts +79 -0
- package/src/adapters/node/node-hmr-manager.d.ts +62 -0
- package/src/adapters/node/node-hmr-manager.js +221 -0
- package/src/adapters/node/node-hmr-manager.ts +271 -0
- package/src/adapters/node/server-adapter.d.ts +190 -0
- package/src/adapters/node/server-adapter.js +420 -0
- package/src/adapters/node/server-adapter.ts +561 -0
- package/src/adapters/node/static-content-server.d.ts +24 -0
- package/src/adapters/node/static-content-server.js +166 -0
- package/src/adapters/node/static-content-server.ts +203 -0
- package/src/adapters/shared/api-response.d.ts +52 -0
- package/src/adapters/shared/api-response.js +96 -0
- package/src/adapters/shared/api-response.ts +104 -0
- package/src/adapters/shared/application-adapter.d.ts +18 -0
- package/src/adapters/shared/application-adapter.js +90 -0
- package/src/adapters/shared/application-adapter.ts +199 -0
- package/src/adapters/shared/explicit-static-route-matcher.d.ts +38 -0
- package/src/adapters/shared/explicit-static-route-matcher.js +100 -0
- package/src/adapters/shared/explicit-static-route-matcher.ts +134 -0
- package/src/adapters/shared/file-route-middleware-pipeline.d.ts +65 -0
- package/src/adapters/shared/file-route-middleware-pipeline.js +98 -0
- package/src/adapters/shared/file-route-middleware-pipeline.ts +123 -0
- package/src/adapters/shared/fs-server-response-factory.d.ts +19 -0
- package/src/adapters/shared/fs-server-response-factory.js +97 -0
- package/src/adapters/shared/fs-server-response-factory.ts +118 -0
- package/src/adapters/shared/fs-server-response-matcher.d.ts +71 -0
- package/src/adapters/shared/fs-server-response-matcher.js +155 -0
- package/src/adapters/shared/fs-server-response-matcher.ts +198 -0
- package/src/adapters/shared/render-context.d.ts +14 -0
- package/src/adapters/shared/render-context.js +69 -0
- package/src/adapters/shared/render-context.ts +105 -0
- package/src/adapters/shared/server-adapter.d.ts +87 -0
- package/src/adapters/shared/server-adapter.js +353 -0
- package/src/adapters/shared/server-adapter.ts +442 -0
- package/src/adapters/shared/server-route-handler.d.ts +89 -0
- package/src/adapters/shared/server-route-handler.js +120 -0
- package/src/adapters/shared/server-route-handler.ts +166 -0
- package/src/adapters/shared/server-static-builder.d.ts +38 -0
- package/src/adapters/shared/server-static-builder.js +46 -0
- package/src/adapters/shared/server-static-builder.ts +82 -0
- package/src/build/build-adapter.d.ts +74 -0
- package/src/build/build-adapter.js +54 -0
- package/src/build/build-adapter.ts +132 -0
- package/src/build/build-types.d.ts +57 -0
- package/src/build/build-types.js +0 -0
- package/src/build/build-types.ts +83 -0
- package/src/build/esbuild-build-adapter.d.ts +69 -0
- package/src/build/esbuild-build-adapter.js +390 -0
- package/src/build/esbuild-build-adapter.ts +510 -0
- package/src/config/config-builder.d.ts +227 -0
- package/src/config/config-builder.js +392 -0
- package/src/config/config-builder.ts +474 -0
- package/src/constants.d.ts +32 -0
- package/src/constants.js +21 -0
- package/src/constants.ts +39 -0
- package/src/create-app.d.ts +17 -0
- package/src/create-app.js +66 -0
- package/src/create-app.ts +87 -0
- package/src/declarations.d.ts +26 -0
- package/src/define-api-handler.d.ts +25 -0
- package/src/define-api-handler.js +15 -0
- package/src/define-api-handler.ts +66 -0
- package/src/dev/sc-server.d.ts +30 -0
- package/src/dev/sc-server.js +111 -0
- package/src/dev/sc-server.ts +143 -0
- package/src/eco/README.md +636 -0
- package/src/eco/component-render-context.d.ts +105 -0
- package/src/eco/component-render-context.js +77 -0
- package/src/eco/component-render-context.ts +202 -0
- package/src/eco/eco.d.ts +9 -0
- package/src/eco/eco.js +110 -0
- package/src/eco/eco.ts +221 -0
- package/src/eco/eco.types.d.ts +170 -0
- package/src/eco/eco.types.js +0 -0
- package/src/eco/eco.types.ts +202 -0
- package/src/eco/eco.utils.d.ts +40 -0
- package/src/eco/eco.utils.js +40 -0
- package/src/eco/eco.utils.ts +89 -0
- package/src/eco/global-injector-map.d.ts +16 -0
- package/src/eco/global-injector-map.js +80 -0
- package/src/eco/global-injector-map.ts +112 -0
- package/src/eco/lazy-injector-map.d.ts +8 -0
- package/src/eco/lazy-injector-map.js +70 -0
- package/src/eco/lazy-injector-map.ts +120 -0
- package/src/eco/module-dependencies.d.ts +18 -0
- package/src/eco/module-dependencies.js +49 -0
- package/src/eco/module-dependencies.ts +75 -0
- package/src/env.d.ts +20 -0
- package/src/errors/http-error.d.ts +31 -0
- package/src/errors/http-error.js +50 -0
- package/src/errors/http-error.ts +72 -0
- package/src/errors/index.d.ts +2 -0
- package/src/errors/index.js +4 -0
- package/src/errors/index.ts +2 -0
- package/src/errors/locals-access-error.d.ts +4 -0
- package/src/errors/locals-access-error.js +9 -0
- package/src/errors/locals-access-error.ts +7 -0
- package/src/global/app-logger.d.ts +2 -0
- package/src/global/app-logger.js +6 -0
- package/src/global/app-logger.ts +4 -0
- package/src/hmr/client/__screenshots__/hmr-runtime.test.browser.ts/HMR-Runtime-HMR-Server-Integration-should-have-HMR-script-injected-in-page-1.png +0 -0
- package/src/hmr/client/__screenshots__/hmr-runtime.test.browser.ts/HMR-Runtime-HMR-Server-Integration-should-load-fixture-app-page-1.png +0 -0
- package/src/hmr/client/__screenshots__/hmr-runtime.test.browser.ts/HMR-Runtime-WebSocket-Connection-should-connect-to-correct-HMR-endpoint-1.png +0 -0
- package/src/hmr/client/hmr-runtime.d.ts +10 -0
- package/src/hmr/client/hmr-runtime.js +86 -0
- package/src/hmr/client/hmr-runtime.ts +121 -0
- package/src/hmr/hmr-strategy.d.ts +159 -0
- package/src/hmr/hmr-strategy.js +29 -0
- package/src/hmr/hmr-strategy.ts +172 -0
- package/src/hmr/hmr.test.e2e.d.ts +1 -0
- package/src/hmr/hmr.test.e2e.js +50 -0
- package/src/hmr/hmr.test.e2e.ts +75 -0
- package/src/hmr/strategies/default-hmr-strategy.d.ts +43 -0
- package/src/hmr/strategies/default-hmr-strategy.js +34 -0
- package/src/hmr/strategies/default-hmr-strategy.ts +60 -0
- package/src/hmr/strategies/js-hmr-strategy.d.ts +136 -0
- package/src/hmr/strategies/js-hmr-strategy.js +179 -0
- package/src/hmr/strategies/js-hmr-strategy.ts +308 -0
- package/src/index.browser.d.ts +3 -0
- package/src/index.browser.js +4 -0
- package/src/index.browser.ts +3 -0
- package/src/index.d.ts +5 -0
- package/src/index.js +10 -0
- package/src/index.ts +5 -0
- package/src/integrations/ghtml/ghtml-renderer.d.ts +15 -0
- package/src/integrations/ghtml/ghtml-renderer.js +60 -0
- package/src/integrations/ghtml/ghtml-renderer.ts +93 -0
- package/src/integrations/ghtml/ghtml.plugin.d.ts +20 -0
- package/src/integrations/ghtml/ghtml.plugin.js +21 -0
- package/src/integrations/ghtml/ghtml.plugin.ts +32 -0
- package/src/internal-types.d.ts +200 -0
- package/src/internal-types.js +0 -0
- package/src/internal-types.ts +212 -0
- package/src/plugins/alias-resolver-plugin.d.ts +2 -0
- package/src/plugins/alias-resolver-plugin.js +39 -0
- package/src/plugins/alias-resolver-plugin.ts +45 -0
- package/src/plugins/eco-component-meta-plugin.d.ts +95 -0
- package/src/plugins/eco-component-meta-plugin.js +157 -0
- package/src/plugins/eco-component-meta-plugin.ts +474 -0
- package/src/plugins/integration-plugin.d.ts +102 -0
- package/src/plugins/integration-plugin.js +100 -0
- package/src/plugins/integration-plugin.ts +184 -0
- package/src/plugins/processor.d.ts +82 -0
- package/src/plugins/processor.js +122 -0
- package/src/plugins/processor.ts +220 -0
- package/src/public-types.d.ts +1094 -0
- package/src/public-types.js +0 -0
- package/src/public-types.ts +1255 -0
- package/src/route-renderer/GRAPH.md +387 -0
- package/src/route-renderer/README.md +135 -0
- package/src/route-renderer/component-graph-executor.d.ts +32 -0
- package/src/route-renderer/component-graph-executor.js +31 -0
- package/src/route-renderer/component-graph-executor.ts +84 -0
- package/src/route-renderer/component-graph.d.ts +42 -0
- package/src/route-renderer/component-graph.js +72 -0
- package/src/route-renderer/component-graph.ts +159 -0
- package/src/route-renderer/component-marker.d.ts +52 -0
- package/src/route-renderer/component-marker.js +46 -0
- package/src/route-renderer/component-marker.ts +117 -0
- package/src/route-renderer/dependency-resolver.d.ts +24 -0
- package/src/route-renderer/dependency-resolver.js +428 -0
- package/src/route-renderer/dependency-resolver.ts +596 -0
- package/src/route-renderer/html-post-processing.service.d.ts +40 -0
- package/src/route-renderer/html-post-processing.service.js +86 -0
- package/src/route-renderer/html-post-processing.service.ts +103 -0
- package/src/route-renderer/integration-renderer.d.ts +339 -0
- package/src/route-renderer/integration-renderer.js +526 -0
- package/src/route-renderer/integration-renderer.ts +696 -0
- package/src/route-renderer/marker-graph-resolver.d.ts +76 -0
- package/src/route-renderer/marker-graph-resolver.js +93 -0
- package/src/route-renderer/marker-graph-resolver.ts +153 -0
- package/src/route-renderer/page-module-loader.d.ts +61 -0
- package/src/route-renderer/page-module-loader.js +102 -0
- package/src/route-renderer/page-module-loader.ts +153 -0
- package/src/route-renderer/render-execution.service.d.ts +69 -0
- package/src/route-renderer/render-execution.service.js +91 -0
- package/src/route-renderer/render-execution.service.ts +158 -0
- package/src/route-renderer/render-preparation.service.d.ts +112 -0
- package/src/route-renderer/render-preparation.service.js +243 -0
- package/src/route-renderer/render-preparation.service.ts +358 -0
- package/src/route-renderer/route-renderer.d.ts +26 -0
- package/src/route-renderer/route-renderer.js +68 -0
- package/src/route-renderer/route-renderer.ts +80 -0
- package/src/router/fs-router-scanner.d.ts +41 -0
- package/src/router/fs-router-scanner.js +155 -0
- package/src/router/fs-router-scanner.ts +217 -0
- package/src/router/fs-router.d.ts +26 -0
- package/src/router/fs-router.js +100 -0
- package/src/router/fs-router.ts +122 -0
- package/src/services/asset-processing-service/asset-processing.service.d.ts +41 -0
- package/src/services/asset-processing-service/asset-processing.service.js +250 -0
- package/src/services/asset-processing-service/asset-processing.service.ts +306 -0
- package/src/services/asset-processing-service/asset.factory.d.ts +17 -0
- package/src/services/asset-processing-service/asset.factory.js +82 -0
- package/src/services/asset-processing-service/asset.factory.ts +105 -0
- package/src/services/asset-processing-service/assets.types.d.ts +88 -0
- package/src/services/asset-processing-service/assets.types.js +0 -0
- package/src/services/asset-processing-service/assets.types.ts +112 -0
- package/src/services/asset-processing-service/index.d.ts +3 -0
- package/src/services/asset-processing-service/index.js +3 -0
- package/src/services/asset-processing-service/index.ts +3 -0
- package/src/services/asset-processing-service/processor.interface.d.ts +22 -0
- package/src/services/asset-processing-service/processor.interface.js +6 -0
- package/src/services/asset-processing-service/processor.interface.ts +27 -0
- package/src/services/asset-processing-service/processor.registry.d.ts +8 -0
- package/src/services/asset-processing-service/processor.registry.js +15 -0
- package/src/services/asset-processing-service/processor.registry.ts +18 -0
- package/src/services/asset-processing-service/processors/base/base-processor.d.ts +24 -0
- package/src/services/asset-processing-service/processors/base/base-processor.js +59 -0
- package/src/services/asset-processing-service/processors/base/base-processor.ts +76 -0
- package/src/services/asset-processing-service/processors/base/base-script-processor.d.ts +16 -0
- package/src/services/asset-processing-service/processors/base/base-script-processor.js +80 -0
- package/src/services/asset-processing-service/processors/base/base-script-processor.ts +105 -0
- package/src/services/asset-processing-service/processors/index.d.ts +5 -0
- package/src/services/asset-processing-service/processors/index.js +5 -0
- package/src/services/asset-processing-service/processors/index.ts +5 -0
- package/src/services/asset-processing-service/processors/script/content-script.processor.d.ts +5 -0
- package/src/services/asset-processing-service/processors/script/content-script.processor.js +57 -0
- package/src/services/asset-processing-service/processors/script/content-script.processor.ts +66 -0
- package/src/services/asset-processing-service/processors/script/file-script.processor.d.ts +8 -0
- package/src/services/asset-processing-service/processors/script/file-script.processor.js +76 -0
- package/src/services/asset-processing-service/processors/script/file-script.processor.ts +88 -0
- package/src/services/asset-processing-service/processors/script/node-module-script.processor.d.ts +7 -0
- package/src/services/asset-processing-service/processors/script/node-module-script.processor.js +74 -0
- package/src/services/asset-processing-service/processors/script/node-module-script.processor.ts +84 -0
- package/src/services/asset-processing-service/processors/stylesheet/content-stylesheet.processor.d.ts +5 -0
- package/src/services/asset-processing-service/processors/stylesheet/content-stylesheet.processor.js +25 -0
- package/src/services/asset-processing-service/processors/stylesheet/content-stylesheet.processor.ts +27 -0
- package/src/services/asset-processing-service/processors/stylesheet/file-stylesheet.processor.d.ts +9 -0
- package/src/services/asset-processing-service/processors/stylesheet/file-stylesheet.processor.js +63 -0
- package/src/services/asset-processing-service/processors/stylesheet/file-stylesheet.processor.ts +77 -0
- package/src/services/cache/cache.types.d.ts +107 -0
- package/src/services/cache/cache.types.js +0 -0
- package/src/services/cache/cache.types.ts +126 -0
- package/src/services/cache/index.d.ts +7 -0
- package/src/services/cache/index.js +7 -0
- package/src/services/cache/index.ts +18 -0
- package/src/services/cache/memory-cache-store.d.ts +42 -0
- package/src/services/cache/memory-cache-store.js +98 -0
- package/src/services/cache/memory-cache-store.ts +130 -0
- package/src/services/cache/page-cache-service.d.ts +70 -0
- package/src/services/cache/page-cache-service.js +152 -0
- package/src/services/cache/page-cache-service.ts +202 -0
- package/src/services/html-transformer.service.d.ts +50 -0
- package/src/services/html-transformer.service.js +163 -0
- package/src/services/html-transformer.service.ts +217 -0
- package/src/services/page-module-import.service.d.ts +37 -0
- package/src/services/page-module-import.service.js +88 -0
- package/src/services/page-module-import.service.ts +129 -0
- package/src/services/page-request-cache-coordinator.service.d.ts +75 -0
- package/src/services/page-request-cache-coordinator.service.js +107 -0
- package/src/services/page-request-cache-coordinator.service.ts +128 -0
- package/src/services/schema-validation-service.d.ts +122 -0
- package/src/services/schema-validation-service.js +101 -0
- package/src/services/schema-validation-service.ts +204 -0
- package/src/services/validation/standard-schema.types.d.ts +65 -0
- package/src/services/validation/standard-schema.types.js +0 -0
- package/src/services/validation/standard-schema.types.ts +68 -0
- package/src/static-site-generator/static-site-generator.d.ts +57 -0
- package/src/static-site-generator/static-site-generator.js +272 -0
- package/src/static-site-generator/static-site-generator.ts +359 -0
- package/src/utils/css.d.ts +1 -0
- package/src/utils/css.js +7 -0
- package/src/utils/css.ts +5 -0
- package/src/utils/deep-merge.d.ts +14 -0
- package/src/utils/deep-merge.js +32 -0
- package/src/utils/deep-merge.ts +47 -0
- package/src/utils/hash.d.ts +1 -0
- package/src/utils/hash.js +7 -0
- package/src/utils/hash.ts +5 -0
- package/src/utils/html.d.ts +1 -0
- package/src/utils/html.js +4 -0
- package/src/utils/html.ts +1 -0
- package/src/utils/invariant.d.ts +5 -0
- package/src/utils/invariant.js +11 -0
- package/src/utils/invariant.ts +15 -0
- package/src/utils/locals-utils.d.ts +15 -0
- package/src/utils/locals-utils.js +24 -0
- package/src/utils/locals-utils.ts +37 -0
- package/src/utils/parse-cli-args.d.ts +24 -0
- package/src/utils/parse-cli-args.js +47 -0
- package/src/utils/parse-cli-args.ts +83 -0
- package/src/utils/path-utils.module.d.ts +5 -0
- package/src/utils/path-utils.module.js +14 -0
- package/src/utils/path-utils.module.ts +14 -0
- package/src/utils/runtime.d.ts +11 -0
- package/src/utils/runtime.js +40 -0
- package/src/utils/runtime.ts +44 -0
- package/src/utils/server-utils.module.d.ts +19 -0
- package/src/utils/server-utils.module.js +56 -0
- package/src/utils/server-utils.module.ts +67 -0
- package/src/watchers/project-watcher.d.ts +120 -0
- package/src/watchers/project-watcher.js +238 -0
- package/src/watchers/project-watcher.test-helpers.d.ts +4 -0
- package/src/watchers/project-watcher.test-helpers.js +51 -0
- package/src/watchers/project-watcher.test-helpers.ts +40 -0
- package/src/watchers/project-watcher.ts +306 -0
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import { type Server as NodeHttpServer } from 'node:http';
|
|
2
|
+
import type { EcoPagesAppConfig } from '../../internal-types.js';
|
|
3
|
+
import type { ApiHandler, ErrorHandler, StaticRoute } from '../../public-types.js';
|
|
4
|
+
import { SharedServerAdapter } from '../shared/server-adapter.js';
|
|
5
|
+
import type { ServerAdapterResult } from '../abstract/server-adapter.js';
|
|
6
|
+
export type NodeServerInstance = NodeHttpServer;
|
|
7
|
+
export type NodeServeAdapterServerOptions = {
|
|
8
|
+
port?: number;
|
|
9
|
+
hostname?: string;
|
|
10
|
+
[key: string]: unknown;
|
|
11
|
+
};
|
|
12
|
+
export interface NodeServerAdapterParams {
|
|
13
|
+
appConfig: EcoPagesAppConfig;
|
|
14
|
+
runtimeOrigin: string;
|
|
15
|
+
serveOptions: NodeServeAdapterServerOptions;
|
|
16
|
+
apiHandlers?: ApiHandler[];
|
|
17
|
+
staticRoutes?: StaticRoute[];
|
|
18
|
+
errorHandler?: ErrorHandler;
|
|
19
|
+
options?: {
|
|
20
|
+
watch?: boolean;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
export interface NodeServerAdapterResult extends ServerAdapterResult {
|
|
24
|
+
completeInitialization: (server: NodeServerInstance) => Promise<void>;
|
|
25
|
+
handleRequest: (request: Request) => Promise<Response>;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Node.js HTTP server adapter for the Ecopages runtime.
|
|
29
|
+
*
|
|
30
|
+
* `NodeServerAdapter` bridges the Node.js `http` module and the Ecopages
|
|
31
|
+
* `SharedServerAdapter` abstraction, translating between Node's
|
|
32
|
+
* `IncomingMessage`/`ServerResponse` API and the platform-agnostic Web
|
|
33
|
+
* `Request`/`Response` model.
|
|
34
|
+
*
|
|
35
|
+
* Lifecycle:
|
|
36
|
+
* 1. `createAdapter()` — calls `initialize()` and returns the public adapter result.
|
|
37
|
+
* 2. `completeInitialization(server)` — called once the HTTP server is listening.
|
|
38
|
+
* Conditionally wires HMR, WebSocket upgrades, and the file watcher when
|
|
39
|
+
* `options.watch` is `true`.
|
|
40
|
+
* 3. `handleRequest(request)` — delegates to `handleSharedRequest` for routing;
|
|
41
|
+
* intercepts `ClientAbortError` to return 499 instead of 500.
|
|
42
|
+
* 4. `buildStatic()` — spins up an ephemeral runtime server, generates all static
|
|
43
|
+
* pages against it, then tears it down.
|
|
44
|
+
*
|
|
45
|
+
* @see SharedServerAdapter for routing, caching and response handler logic.
|
|
46
|
+
*/
|
|
47
|
+
export declare class NodeServerAdapter extends SharedServerAdapter<NodeServerAdapterParams, NodeServerAdapterResult> {
|
|
48
|
+
private serverInstance;
|
|
49
|
+
private initialized;
|
|
50
|
+
private apiHandlers;
|
|
51
|
+
private staticRoutes;
|
|
52
|
+
private errorHandler?;
|
|
53
|
+
private previewServer;
|
|
54
|
+
private bridge;
|
|
55
|
+
private hmrManager;
|
|
56
|
+
private processorBuildPlugins;
|
|
57
|
+
constructor(options: NodeServerAdapterParams);
|
|
58
|
+
/**
|
|
59
|
+
* Prepares the adapter for use.
|
|
60
|
+
*
|
|
61
|
+
* Order is intentional:
|
|
62
|
+
* 1. **Loaders** are registered first so processors and integrations can
|
|
63
|
+
* reference loader-provided file types in their own plugins.
|
|
64
|
+
* 2. **Public dir** is copied before any build so static assets are in `distDir`
|
|
65
|
+
* before the first request arrives.
|
|
66
|
+
* 3. **Plugins** (processors, then integrations) are set up after the public dir
|
|
67
|
+
* is in place so they can safely reference dist-relative paths.
|
|
68
|
+
* 4. **Router** is initialised last because it may depend on files written by
|
|
69
|
+
* processors during their `setup()` calls.
|
|
70
|
+
*/
|
|
71
|
+
initialize(): Promise<void>;
|
|
72
|
+
/**
|
|
73
|
+
* Registers every configured file loader as a build plugin on the shared
|
|
74
|
+
* `defaultBuildAdapter`.
|
|
75
|
+
*
|
|
76
|
+
* Loaders are registered on the *shared* adapter (not on a per-build instance)
|
|
77
|
+
* because they must be available globally to both the SSR build and any dynamic
|
|
78
|
+
* transpile passes that happen outside of a top-level `build()` call (e.g. HMR
|
|
79
|
+
* incremental rebuilds).
|
|
80
|
+
*/
|
|
81
|
+
private setupLoaders;
|
|
82
|
+
private copyPublicDir;
|
|
83
|
+
/**
|
|
84
|
+
* Sets up all configured processors and integrations in two distinct phases.
|
|
85
|
+
*
|
|
86
|
+
* **Phase 1 — Processors:**
|
|
87
|
+
* Each processor's `setup()` is called first. A processor may expose two
|
|
88
|
+
* plugin lists:
|
|
89
|
+
* - `plugins` — transform plugins used during SSR rendering (e.g. PostCSS).
|
|
90
|
+
* - `buildPlugins` — esbuild plugins used during the client bundle step.
|
|
91
|
+
* Both are registered on `defaultBuildAdapter` so later build calls pick them up.
|
|
92
|
+
*
|
|
93
|
+
* **Phase 2 — Integrations:**
|
|
94
|
+
* Integrations receive the fully-resolved app config, the runtime origin, and
|
|
95
|
+
* (if already initialised) the HMR manager before their own `setup()` is called.
|
|
96
|
+
* This ordering ensures integrations can query config values that processors
|
|
97
|
+
* may have mutated during phase 1.
|
|
98
|
+
*/
|
|
99
|
+
private initializePlugins;
|
|
100
|
+
getServerOptions(): NodeServeAdapterServerOptions;
|
|
101
|
+
buildStatic(options?: {
|
|
102
|
+
preview?: boolean;
|
|
103
|
+
}): Promise<void>;
|
|
104
|
+
/**
|
|
105
|
+
* Converts a Node.js `IncomingMessage` into a Web API `Request`.
|
|
106
|
+
*
|
|
107
|
+
* Multi-value headers (e.g. `set-cookie`) are appended individually so no
|
|
108
|
+
* value is silently dropped.
|
|
109
|
+
*
|
|
110
|
+
* For methods that carry a body (`POST`, `PUT`, `PATCH`, …), the raw
|
|
111
|
+
* `IncomingMessage` stream is wrapped in a `ReadableStream` rather than
|
|
112
|
+
* cast directly to `BodyInit`. See the inline doc block inside the `if`
|
|
113
|
+
* branch for the rationale (client-abort handling).
|
|
114
|
+
*
|
|
115
|
+
* `duplex: 'half'` is required by the `fetch` spec when a streaming body is
|
|
116
|
+
* provided — without it Node.js 18+ throws a `TypeError`.
|
|
117
|
+
*/
|
|
118
|
+
private createWebRequest;
|
|
119
|
+
/**
|
|
120
|
+
* Writes a Web `Response` back to a Node.js `ServerResponse`.
|
|
121
|
+
*
|
|
122
|
+
* The entire body is buffered via `arrayBuffer()` before writing. This is
|
|
123
|
+
* intentional for the current use-case (SSR pages and API routes), where
|
|
124
|
+
* responses are typically small and fully materialised. Streaming responses
|
|
125
|
+
* are not yet supported.
|
|
126
|
+
*/
|
|
127
|
+
private sendNodeResponse;
|
|
128
|
+
/**
|
|
129
|
+
* Starts an ephemeral HTTP server used *only* during a static site generation
|
|
130
|
+
* run.
|
|
131
|
+
*
|
|
132
|
+
* Static generation works by having the `StaticSiteGenerator` issue real HTTP
|
|
133
|
+
* requests to a live server for each route, capturing the rendered HTML. This
|
|
134
|
+
* approach reuses the normal request pipeline (middleware, caching, API
|
|
135
|
+
* handlers) without any special-casing for the build path.
|
|
136
|
+
*
|
|
137
|
+
* The server is torn down immediately after generation completes via
|
|
138
|
+
* `stopBuildRuntimeServer`, so it never overlaps with the actual dev/prod server.
|
|
139
|
+
*/
|
|
140
|
+
private startBuildRuntimeServer;
|
|
141
|
+
/**
|
|
142
|
+
* Gracefully shuts down the ephemeral build runtime server.
|
|
143
|
+
*
|
|
144
|
+
* `closeAllConnections()` is called *before* `close()` because `server.close()`
|
|
145
|
+
* only stops accepting new connections — it waits for existing keep-alive
|
|
146
|
+
* connections to finish naturally, which can stall the build indefinitely.
|
|
147
|
+
* `closeAllConnections()` force-closes any lingering sockets immediately so
|
|
148
|
+
* the `close()` callback fires promptly.
|
|
149
|
+
*
|
|
150
|
+
* The `NodeClientBridge` heartbeat is also destroyed here so its `setInterval`
|
|
151
|
+
* does not prevent the Node.js process from exiting cleanly after the build.
|
|
152
|
+
*/
|
|
153
|
+
private stopBuildRuntimeServer;
|
|
154
|
+
createAdapter(): Promise<NodeServerAdapterResult>;
|
|
155
|
+
/**
|
|
156
|
+
* Handles a single incoming Web `Request` and returns a Web `Response`.
|
|
157
|
+
*
|
|
158
|
+
* Delegates to `handleSharedRequest` for all routing, caching, and response
|
|
159
|
+
* handler logic. The only Node-specific concern here is translating a
|
|
160
|
+
* `ClientAbortError` — which the body `ReadableStream` raises when the
|
|
161
|
+
* underlying socket closes early — into a 499 response so it does not
|
|
162
|
+
* incorrectly surface as a 500 in application logs.
|
|
163
|
+
*/
|
|
164
|
+
handleRequest(_request: Request): Promise<Response>;
|
|
165
|
+
/**
|
|
166
|
+
* Called once the HTTP server is bound and listening.
|
|
167
|
+
*
|
|
168
|
+
* When `options.watch` is `true` this method wires the full HMR pipeline:
|
|
169
|
+
* - A `WebSocketServer` is attached to the existing HTTP server via the
|
|
170
|
+
* `upgrade` event (no separate port needed).
|
|
171
|
+
* - `NodeClientBridge` tracks active WebSocket connections and handles
|
|
172
|
+
* broadcast + heartbeat cleanup.
|
|
173
|
+
* - `NodeHmrManager` watches the filesystem and triggers incremental esbuild
|
|
174
|
+
* rebuilds, notifying connected clients via the bridge.
|
|
175
|
+
* - `ProjectWatcher` listens for route-level file changes and refreshes the
|
|
176
|
+
* router and response handlers when pages are added or removed.
|
|
177
|
+
*
|
|
178
|
+
* WebSocket upgrade requests that do not target `/_hmr` are rejected with an
|
|
179
|
+
* immediate socket destroy to prevent unhandled upgrade leaks.
|
|
180
|
+
*/
|
|
181
|
+
completeInitialization(_server: NodeServerInstance): Promise<void>;
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Factory function that creates and fully initialises a `NodeServerAdapter`.
|
|
185
|
+
*
|
|
186
|
+
* `runtimeOrigin` is derived from `serveOptions` when not explicitly provided,
|
|
187
|
+
* so callers only need to set it when the server is behind a reverse proxy that
|
|
188
|
+
* changes the effective host or port.
|
|
189
|
+
*/
|
|
190
|
+
export declare function createNodeServerAdapter(params: NodeServerAdapterParams): Promise<NodeServerAdapterResult>;
|
|
@@ -0,0 +1,420 @@
|
|
|
1
|
+
import { createServer } from "node:http";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { fileSystem } from "@ecopages/file-system";
|
|
4
|
+
import { RESOLVED_ASSETS_DIR } from "../../constants.js";
|
|
5
|
+
import { defaultBuildAdapter } from "../../build/build-adapter.js";
|
|
6
|
+
import { appLogger } from "../../global/app-logger.js";
|
|
7
|
+
import { ProjectWatcher } from "../../watchers/project-watcher.js";
|
|
8
|
+
import { NodeClientBridge } from "./node-client-bridge.js";
|
|
9
|
+
import { NodeHmrManager } from "./node-hmr-manager.js";
|
|
10
|
+
import { StaticSiteGenerator } from "../../static-site-generator/static-site-generator.js";
|
|
11
|
+
import { SharedServerAdapter } from "../shared/server-adapter.js";
|
|
12
|
+
import { ServerStaticBuilder } from "../shared/server-static-builder.js";
|
|
13
|
+
import { NodeStaticContentServer } from "./static-content-server.js";
|
|
14
|
+
class ClientAbortError extends Error {
|
|
15
|
+
constructor() {
|
|
16
|
+
super("Client closed the request");
|
|
17
|
+
this.name = "ClientAbortError";
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
class NodeServerAdapter extends SharedServerAdapter {
|
|
21
|
+
serverInstance = null;
|
|
22
|
+
initialized = false;
|
|
23
|
+
apiHandlers;
|
|
24
|
+
staticRoutes;
|
|
25
|
+
errorHandler;
|
|
26
|
+
previewServer = null;
|
|
27
|
+
bridge = null;
|
|
28
|
+
hmrManager = null;
|
|
29
|
+
processorBuildPlugins = [];
|
|
30
|
+
constructor(options) {
|
|
31
|
+
super(options);
|
|
32
|
+
this.apiHandlers = options.apiHandlers || [];
|
|
33
|
+
this.staticRoutes = options.staticRoutes || [];
|
|
34
|
+
this.errorHandler = options.errorHandler;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Prepares the adapter for use.
|
|
38
|
+
*
|
|
39
|
+
* Order is intentional:
|
|
40
|
+
* 1. **Loaders** are registered first so processors and integrations can
|
|
41
|
+
* reference loader-provided file types in their own plugins.
|
|
42
|
+
* 2. **Public dir** is copied before any build so static assets are in `distDir`
|
|
43
|
+
* before the first request arrives.
|
|
44
|
+
* 3. **Plugins** (processors, then integrations) are set up after the public dir
|
|
45
|
+
* is in place so they can safely reference dist-relative paths.
|
|
46
|
+
* 4. **Router** is initialised last because it may depend on files written by
|
|
47
|
+
* processors during their `setup()` calls.
|
|
48
|
+
*/
|
|
49
|
+
async initialize() {
|
|
50
|
+
this.setupLoaders();
|
|
51
|
+
this.copyPublicDir();
|
|
52
|
+
await this.initializePlugins();
|
|
53
|
+
await this.initSharedRouter();
|
|
54
|
+
this.configureSharedResponseHandlers(this.staticRoutes, this.hmrManager ?? void 0);
|
|
55
|
+
this.staticSiteGenerator = new StaticSiteGenerator({ appConfig: this.appConfig });
|
|
56
|
+
this.staticBuilder = new ServerStaticBuilder({
|
|
57
|
+
appConfig: this.appConfig,
|
|
58
|
+
staticSiteGenerator: this.staticSiteGenerator,
|
|
59
|
+
serveOptions: this.serveOptions
|
|
60
|
+
});
|
|
61
|
+
this.initialized = true;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Registers every configured file loader as a build plugin on the shared
|
|
65
|
+
* `defaultBuildAdapter`.
|
|
66
|
+
*
|
|
67
|
+
* Loaders are registered on the *shared* adapter (not on a per-build instance)
|
|
68
|
+
* because they must be available globally to both the SSR build and any dynamic
|
|
69
|
+
* transpile passes that happen outside of a top-level `build()` call (e.g. HMR
|
|
70
|
+
* incremental rebuilds).
|
|
71
|
+
*/
|
|
72
|
+
setupLoaders() {
|
|
73
|
+
for (const loader of this.appConfig.loaders.values()) {
|
|
74
|
+
defaultBuildAdapter.registerPlugin(loader);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
copyPublicDir() {
|
|
78
|
+
const srcPublicDir = path.join(this.appConfig.rootDir, this.appConfig.srcDir, this.appConfig.publicDir);
|
|
79
|
+
if (fileSystem.exists(srcPublicDir)) {
|
|
80
|
+
fileSystem.copyDir(srcPublicDir, path.join(this.appConfig.rootDir, this.appConfig.distDir));
|
|
81
|
+
}
|
|
82
|
+
fileSystem.ensureDir(path.join(this.appConfig.absolutePaths.distDir, RESOLVED_ASSETS_DIR));
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Sets up all configured processors and integrations in two distinct phases.
|
|
86
|
+
*
|
|
87
|
+
* **Phase 1 — Processors:**
|
|
88
|
+
* Each processor's `setup()` is called first. A processor may expose two
|
|
89
|
+
* plugin lists:
|
|
90
|
+
* - `plugins` — transform plugins used during SSR rendering (e.g. PostCSS).
|
|
91
|
+
* - `buildPlugins` — esbuild plugins used during the client bundle step.
|
|
92
|
+
* Both are registered on `defaultBuildAdapter` so later build calls pick them up.
|
|
93
|
+
*
|
|
94
|
+
* **Phase 2 — Integrations:**
|
|
95
|
+
* Integrations receive the fully-resolved app config, the runtime origin, and
|
|
96
|
+
* (if already initialised) the HMR manager before their own `setup()` is called.
|
|
97
|
+
* This ordering ensures integrations can query config values that processors
|
|
98
|
+
* may have mutated during phase 1.
|
|
99
|
+
*/
|
|
100
|
+
async initializePlugins() {
|
|
101
|
+
const processorBuildPlugins = [];
|
|
102
|
+
for (const processor of this.appConfig.processors.values()) {
|
|
103
|
+
await processor.setup();
|
|
104
|
+
if (processor.plugins) {
|
|
105
|
+
for (const plugin of processor.plugins) {
|
|
106
|
+
defaultBuildAdapter.registerPlugin(plugin);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
if (processor.buildPlugins) {
|
|
110
|
+
processorBuildPlugins.push(...processor.buildPlugins);
|
|
111
|
+
for (const plugin of processor.buildPlugins) {
|
|
112
|
+
defaultBuildAdapter.registerPlugin(plugin);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
for (const integration of this.appConfig.integrations) {
|
|
117
|
+
integration.setConfig(this.appConfig);
|
|
118
|
+
integration.setRuntimeOrigin(this.runtimeOrigin);
|
|
119
|
+
if (this.hmrManager) {
|
|
120
|
+
integration.setHmrManager(this.hmrManager);
|
|
121
|
+
}
|
|
122
|
+
await integration.setup();
|
|
123
|
+
for (const plugin of integration.plugins) {
|
|
124
|
+
defaultBuildAdapter.registerPlugin(plugin);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
this.processorBuildPlugins = processorBuildPlugins;
|
|
128
|
+
}
|
|
129
|
+
getServerOptions() {
|
|
130
|
+
return {
|
|
131
|
+
...this.serveOptions
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
async buildStatic(options) {
|
|
135
|
+
if (!this.initialized) {
|
|
136
|
+
await this.initialize();
|
|
137
|
+
}
|
|
138
|
+
const buildServer = await this.startBuildRuntimeServer();
|
|
139
|
+
try {
|
|
140
|
+
await this.staticBuilder.build(
|
|
141
|
+
{ preview: false },
|
|
142
|
+
{
|
|
143
|
+
router: this.router,
|
|
144
|
+
routeRendererFactory: this.routeRendererFactory,
|
|
145
|
+
staticRoutes: this.staticRoutes
|
|
146
|
+
}
|
|
147
|
+
);
|
|
148
|
+
} finally {
|
|
149
|
+
await this.stopBuildRuntimeServer(buildServer);
|
|
150
|
+
}
|
|
151
|
+
if (!options?.preview) {
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
if (this.previewServer) {
|
|
155
|
+
await this.previewServer.stop();
|
|
156
|
+
}
|
|
157
|
+
this.previewServer = new NodeStaticContentServer({
|
|
158
|
+
appConfig: this.appConfig,
|
|
159
|
+
options: {
|
|
160
|
+
hostname: this.serveOptions.hostname,
|
|
161
|
+
port: Number(this.serveOptions.port || 3e3)
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
await this.previewServer.start();
|
|
165
|
+
const previewHostname = this.serveOptions.hostname || "localhost";
|
|
166
|
+
const previewPort = this.serveOptions.port || 3e3;
|
|
167
|
+
appLogger.info(`Preview running at http://${previewHostname}:${previewPort}`);
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Converts a Node.js `IncomingMessage` into a Web API `Request`.
|
|
171
|
+
*
|
|
172
|
+
* Multi-value headers (e.g. `set-cookie`) are appended individually so no
|
|
173
|
+
* value is silently dropped.
|
|
174
|
+
*
|
|
175
|
+
* For methods that carry a body (`POST`, `PUT`, `PATCH`, …), the raw
|
|
176
|
+
* `IncomingMessage` stream is wrapped in a `ReadableStream` rather than
|
|
177
|
+
* cast directly to `BodyInit`. See the inline doc block inside the `if`
|
|
178
|
+
* branch for the rationale (client-abort handling).
|
|
179
|
+
*
|
|
180
|
+
* `duplex: 'half'` is required by the `fetch` spec when a streaming body is
|
|
181
|
+
* provided — without it Node.js 18+ throws a `TypeError`.
|
|
182
|
+
*/
|
|
183
|
+
createWebRequest(req) {
|
|
184
|
+
const url = new URL(req.url ?? "/", this.runtimeOrigin);
|
|
185
|
+
const headers = new Headers();
|
|
186
|
+
for (const [key, value] of Object.entries(req.headers)) {
|
|
187
|
+
if (Array.isArray(value)) {
|
|
188
|
+
for (const item of value) {
|
|
189
|
+
headers.append(key, item);
|
|
190
|
+
}
|
|
191
|
+
continue;
|
|
192
|
+
}
|
|
193
|
+
if (value !== void 0) {
|
|
194
|
+
headers.set(key, value);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
const method = (req.method ?? "GET").toUpperCase();
|
|
198
|
+
const requestInit = {
|
|
199
|
+
method,
|
|
200
|
+
headers
|
|
201
|
+
};
|
|
202
|
+
if (method !== "GET" && method !== "HEAD") {
|
|
203
|
+
const body = new ReadableStream({
|
|
204
|
+
start(controller) {
|
|
205
|
+
req.on("data", (chunk) => controller.enqueue(chunk));
|
|
206
|
+
req.once("end", () => controller.close());
|
|
207
|
+
req.once("aborted", () => {
|
|
208
|
+
controller.error(new ClientAbortError());
|
|
209
|
+
});
|
|
210
|
+
req.once("error", (err) => {
|
|
211
|
+
const isClientAbort = err.code === "ECONNRESET";
|
|
212
|
+
controller.error(isClientAbort ? new ClientAbortError() : err);
|
|
213
|
+
});
|
|
214
|
+
},
|
|
215
|
+
cancel() {
|
|
216
|
+
req.destroy();
|
|
217
|
+
}
|
|
218
|
+
});
|
|
219
|
+
requestInit.body = body;
|
|
220
|
+
requestInit.duplex = "half";
|
|
221
|
+
}
|
|
222
|
+
return new Request(url, requestInit);
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Writes a Web `Response` back to a Node.js `ServerResponse`.
|
|
226
|
+
*
|
|
227
|
+
* The entire body is buffered via `arrayBuffer()` before writing. This is
|
|
228
|
+
* intentional for the current use-case (SSR pages and API routes), where
|
|
229
|
+
* responses are typically small and fully materialised. Streaming responses
|
|
230
|
+
* are not yet supported.
|
|
231
|
+
*/
|
|
232
|
+
async sendNodeResponse(res, response) {
|
|
233
|
+
res.statusCode = response.status;
|
|
234
|
+
response.headers.forEach((value, key) => {
|
|
235
|
+
res.setHeader(key, value);
|
|
236
|
+
});
|
|
237
|
+
if (!response.body) {
|
|
238
|
+
res.end();
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
const body = Buffer.from(await response.arrayBuffer());
|
|
242
|
+
res.end(body);
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* Starts an ephemeral HTTP server used *only* during a static site generation
|
|
246
|
+
* run.
|
|
247
|
+
*
|
|
248
|
+
* Static generation works by having the `StaticSiteGenerator` issue real HTTP
|
|
249
|
+
* requests to a live server for each route, capturing the rendered HTML. This
|
|
250
|
+
* approach reuses the normal request pipeline (middleware, caching, API
|
|
251
|
+
* handlers) without any special-casing for the build path.
|
|
252
|
+
*
|
|
253
|
+
* The server is torn down immediately after generation completes via
|
|
254
|
+
* `stopBuildRuntimeServer`, so it never overlaps with the actual dev/prod server.
|
|
255
|
+
*/
|
|
256
|
+
async startBuildRuntimeServer() {
|
|
257
|
+
const hostname = String(this.serveOptions.hostname || "localhost");
|
|
258
|
+
const port = Number(this.serveOptions.port || 3e3);
|
|
259
|
+
const server = createServer(async (req, res) => {
|
|
260
|
+
try {
|
|
261
|
+
const webRequest = this.createWebRequest(req);
|
|
262
|
+
const response = await this.handleRequest(webRequest);
|
|
263
|
+
await this.sendNodeResponse(res, response);
|
|
264
|
+
} catch (error) {
|
|
265
|
+
appLogger.error("Node static build runtime request failed", error);
|
|
266
|
+
res.statusCode = 500;
|
|
267
|
+
res.end("Internal Server Error");
|
|
268
|
+
}
|
|
269
|
+
});
|
|
270
|
+
await new Promise((resolve, reject) => {
|
|
271
|
+
server.once("error", reject);
|
|
272
|
+
server.listen(port, hostname, () => {
|
|
273
|
+
server.off("error", reject);
|
|
274
|
+
resolve();
|
|
275
|
+
});
|
|
276
|
+
});
|
|
277
|
+
this.serverInstance = server;
|
|
278
|
+
appLogger.info(`Server running at http://${hostname}:${port}`);
|
|
279
|
+
return server;
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* Gracefully shuts down the ephemeral build runtime server.
|
|
283
|
+
*
|
|
284
|
+
* `closeAllConnections()` is called *before* `close()` because `server.close()`
|
|
285
|
+
* only stops accepting new connections — it waits for existing keep-alive
|
|
286
|
+
* connections to finish naturally, which can stall the build indefinitely.
|
|
287
|
+
* `closeAllConnections()` force-closes any lingering sockets immediately so
|
|
288
|
+
* the `close()` callback fires promptly.
|
|
289
|
+
*
|
|
290
|
+
* The `NodeClientBridge` heartbeat is also destroyed here so its `setInterval`
|
|
291
|
+
* does not prevent the Node.js process from exiting cleanly after the build.
|
|
292
|
+
*/
|
|
293
|
+
async stopBuildRuntimeServer(server) {
|
|
294
|
+
await new Promise((resolve, reject) => {
|
|
295
|
+
server.close((error) => {
|
|
296
|
+
if (error) {
|
|
297
|
+
reject(error);
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
resolve();
|
|
301
|
+
});
|
|
302
|
+
server.closeAllConnections();
|
|
303
|
+
});
|
|
304
|
+
if (this.serverInstance === server) {
|
|
305
|
+
this.serverInstance = null;
|
|
306
|
+
}
|
|
307
|
+
this.bridge?.destroy();
|
|
308
|
+
this.bridge = null;
|
|
309
|
+
}
|
|
310
|
+
async createAdapter() {
|
|
311
|
+
await this.initialize();
|
|
312
|
+
return {
|
|
313
|
+
getServerOptions: this.getServerOptions.bind(this),
|
|
314
|
+
buildStatic: this.buildStatic.bind(this),
|
|
315
|
+
completeInitialization: this.completeInitialization.bind(this),
|
|
316
|
+
handleRequest: this.handleRequest.bind(this)
|
|
317
|
+
};
|
|
318
|
+
}
|
|
319
|
+
/**
|
|
320
|
+
* Handles a single incoming Web `Request` and returns a Web `Response`.
|
|
321
|
+
*
|
|
322
|
+
* Delegates to `handleSharedRequest` for all routing, caching, and response
|
|
323
|
+
* handler logic. The only Node-specific concern here is translating a
|
|
324
|
+
* `ClientAbortError` — which the body `ReadableStream` raises when the
|
|
325
|
+
* underlying socket closes early — into a 499 response so it does not
|
|
326
|
+
* incorrectly surface as a 500 in application logs.
|
|
327
|
+
*/
|
|
328
|
+
async handleRequest(_request) {
|
|
329
|
+
if (!this.initialized) {
|
|
330
|
+
throw new Error("Node server adapter is not initialized. Call createAdapter() first.");
|
|
331
|
+
}
|
|
332
|
+
try {
|
|
333
|
+
return await this.handleSharedRequest(_request, {
|
|
334
|
+
apiHandlers: this.apiHandlers,
|
|
335
|
+
errorHandler: this.errorHandler,
|
|
336
|
+
serverInstance: this.serverInstance,
|
|
337
|
+
hmrManager: this.hmrManager
|
|
338
|
+
});
|
|
339
|
+
} catch (error) {
|
|
340
|
+
if (error instanceof ClientAbortError) {
|
|
341
|
+
return new Response(null, { status: 499 });
|
|
342
|
+
}
|
|
343
|
+
throw error;
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* Called once the HTTP server is bound and listening.
|
|
348
|
+
*
|
|
349
|
+
* When `options.watch` is `true` this method wires the full HMR pipeline:
|
|
350
|
+
* - A `WebSocketServer` is attached to the existing HTTP server via the
|
|
351
|
+
* `upgrade` event (no separate port needed).
|
|
352
|
+
* - `NodeClientBridge` tracks active WebSocket connections and handles
|
|
353
|
+
* broadcast + heartbeat cleanup.
|
|
354
|
+
* - `NodeHmrManager` watches the filesystem and triggers incremental esbuild
|
|
355
|
+
* rebuilds, notifying connected clients via the bridge.
|
|
356
|
+
* - `ProjectWatcher` listens for route-level file changes and refreshes the
|
|
357
|
+
* router and response handlers when pages are added or removed.
|
|
358
|
+
*
|
|
359
|
+
* WebSocket upgrade requests that do not target `/_hmr` are rejected with an
|
|
360
|
+
* immediate socket destroy to prevent unhandled upgrade leaks.
|
|
361
|
+
*/
|
|
362
|
+
async completeInitialization(_server) {
|
|
363
|
+
this.serverInstance = _server;
|
|
364
|
+
if (this.options?.watch) {
|
|
365
|
+
const { WebSocketServer } = await import("ws");
|
|
366
|
+
const wss = new WebSocketServer({ noServer: true });
|
|
367
|
+
this.bridge = new NodeClientBridge();
|
|
368
|
+
this.hmrManager = new NodeHmrManager({ appConfig: this.appConfig, bridge: this.bridge });
|
|
369
|
+
this.hmrManager.setEnabled(true);
|
|
370
|
+
await this.hmrManager.buildRuntime();
|
|
371
|
+
_server.on("upgrade", (req, socket, head) => {
|
|
372
|
+
const url = new URL(req.url ?? "/", this.runtimeOrigin);
|
|
373
|
+
if (url.pathname === "/_hmr") {
|
|
374
|
+
wss.handleUpgrade(req, socket, head, (ws) => {
|
|
375
|
+
this.bridge.subscribe(ws);
|
|
376
|
+
ws.on("close", () => this.bridge.unsubscribe(ws));
|
|
377
|
+
ws.on("error", (err) => appLogger.error("[HMR] WebSocket error:", err));
|
|
378
|
+
});
|
|
379
|
+
} else {
|
|
380
|
+
socket.destroy();
|
|
381
|
+
}
|
|
382
|
+
});
|
|
383
|
+
const loaderPlugins = Array.from(this.appConfig.loaders.values());
|
|
384
|
+
const hmrBuildPlugins = [...loaderPlugins, ...this.processorBuildPlugins];
|
|
385
|
+
this.hmrManager.setPlugins(hmrBuildPlugins);
|
|
386
|
+
for (const integration of this.appConfig.integrations) {
|
|
387
|
+
integration.setHmrManager(this.hmrManager);
|
|
388
|
+
}
|
|
389
|
+
this.configureSharedResponseHandlers(this.staticRoutes, this.hmrManager);
|
|
390
|
+
const watcher = new ProjectWatcher({
|
|
391
|
+
config: this.appConfig,
|
|
392
|
+
refreshRouterRoutesCallback: async () => {
|
|
393
|
+
await this.initSharedRouter();
|
|
394
|
+
this.configureSharedResponseHandlers(this.staticRoutes, this.hmrManager);
|
|
395
|
+
},
|
|
396
|
+
hmrManager: this.hmrManager,
|
|
397
|
+
bridge: this.bridge
|
|
398
|
+
});
|
|
399
|
+
await watcher.createWatcherSubscription();
|
|
400
|
+
}
|
|
401
|
+
appLogger.debug("Node server adapter initialization completed", {
|
|
402
|
+
apiHandlers: this.apiHandlers.length,
|
|
403
|
+
staticRoutes: this.staticRoutes.length,
|
|
404
|
+
hasErrorHandler: !!this.errorHandler,
|
|
405
|
+
hmrEnabled: !!this.hmrManager?.isEnabled()
|
|
406
|
+
});
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
async function createNodeServerAdapter(params) {
|
|
410
|
+
const runtimeOrigin = params.runtimeOrigin ?? `http://${params.serveOptions.hostname || "localhost"}:${params.serveOptions.port || 3e3}`;
|
|
411
|
+
const adapter = new NodeServerAdapter({
|
|
412
|
+
...params,
|
|
413
|
+
runtimeOrigin
|
|
414
|
+
});
|
|
415
|
+
return adapter.createAdapter();
|
|
416
|
+
}
|
|
417
|
+
export {
|
|
418
|
+
NodeServerAdapter,
|
|
419
|
+
createNodeServerAdapter
|
|
420
|
+
};
|