@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,217 @@
|
|
|
1
|
+
import { appLogger } from '../global/app-logger.ts';
|
|
2
|
+
import type { AssetPosition, ProcessedAsset, ScriptAsset } from './asset-processing-service/assets.types';
|
|
3
|
+
|
|
4
|
+
type HtmlRewriterElement = {
|
|
5
|
+
append(content: string, options?: { html?: boolean }): void;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
type HtmlRewriterConstructor = new () => HtmlRewriterRuntime;
|
|
9
|
+
|
|
10
|
+
export type HtmlRewriterMode = 'auto' | 'native' | 'worker-tools' | 'fallback';
|
|
11
|
+
|
|
12
|
+
type HtmlRewriterRuntime = {
|
|
13
|
+
on(selector: 'head' | 'body', handler: { element: (element: HtmlRewriterElement) => void }): HtmlRewriterRuntime;
|
|
14
|
+
transform(response: Response): Response;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export interface HtmlTransformerServiceOptions {
|
|
18
|
+
htmlRewriterMode?: HtmlRewriterMode;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export class HtmlTransformerService {
|
|
22
|
+
private processedDependencies: ProcessedAsset[] = [];
|
|
23
|
+
private htmlRewriterConstructorPromise?: Promise<HtmlRewriterConstructor | null>;
|
|
24
|
+
private htmlRewriterMode: HtmlRewriterMode = 'auto';
|
|
25
|
+
|
|
26
|
+
constructor(options: HtmlTransformerServiceOptions = {}) {
|
|
27
|
+
this.setHtmlRewriterMode(options.htmlRewriterMode ?? 'auto');
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Overrides the HTML rewriter runtime selection.
|
|
32
|
+
*
|
|
33
|
+
* This is intended for internal/runtime tests that need deterministic
|
|
34
|
+
* selection between native, worker-tools, and string fallback behavior.
|
|
35
|
+
*
|
|
36
|
+
* @param mode Requested runtime selection strategy.
|
|
37
|
+
*/
|
|
38
|
+
setHtmlRewriterMode(mode: HtmlRewriterMode) {
|
|
39
|
+
this.htmlRewriterMode = mode;
|
|
40
|
+
this.htmlRewriterConstructorPromise = undefined;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Creates an HTML rewriter instance from the best available runtime.
|
|
45
|
+
*
|
|
46
|
+
* Resolution order is:
|
|
47
|
+
* 1. native `globalThis.HTMLRewriter`
|
|
48
|
+
* 2. `@worker-tools/html-rewriter/base64`
|
|
49
|
+
* 3. `null`, which triggers the string-based fallback path
|
|
50
|
+
*
|
|
51
|
+
* @returns HTML rewriter instance when available; otherwise `null`.
|
|
52
|
+
*/
|
|
53
|
+
private async createHtmlRewriter(): Promise<HtmlRewriterRuntime | null> {
|
|
54
|
+
const RuntimeHtmlRewriter = await this.resolveHtmlRewriterConstructor();
|
|
55
|
+
return RuntimeHtmlRewriter ? new RuntimeHtmlRewriter() : null;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Resolves the constructor used for HTML rewriting.
|
|
60
|
+
*
|
|
61
|
+
* The worker-tools fallback is loaded lazily so native runtimes avoid the WASM
|
|
62
|
+
* dependency cost unless it is actually needed.
|
|
63
|
+
*
|
|
64
|
+
* @returns Rewriter constructor when available; otherwise `null`.
|
|
65
|
+
*/
|
|
66
|
+
private async resolveHtmlRewriterConstructor(): Promise<HtmlRewriterConstructor | null> {
|
|
67
|
+
if (!this.htmlRewriterConstructorPromise) {
|
|
68
|
+
const mode = this.htmlRewriterMode;
|
|
69
|
+
const RuntimeHtmlRewriter = (globalThis as { HTMLRewriter?: HtmlRewriterConstructor }).HTMLRewriter;
|
|
70
|
+
|
|
71
|
+
if (mode === 'fallback') {
|
|
72
|
+
this.htmlRewriterConstructorPromise = Promise.resolve(null);
|
|
73
|
+
} else if (mode === 'native') {
|
|
74
|
+
if (RuntimeHtmlRewriter) {
|
|
75
|
+
this.htmlRewriterConstructorPromise = Promise.resolve(RuntimeHtmlRewriter);
|
|
76
|
+
} else {
|
|
77
|
+
appLogger.warn(
|
|
78
|
+
'[HtmlTransformerService] Native HTMLRewriter was forced but is unavailable, falling back to string injection.',
|
|
79
|
+
);
|
|
80
|
+
this.htmlRewriterConstructorPromise = Promise.resolve(null);
|
|
81
|
+
}
|
|
82
|
+
} else if (mode === 'auto' && RuntimeHtmlRewriter) {
|
|
83
|
+
this.htmlRewriterConstructorPromise = Promise.resolve(RuntimeHtmlRewriter);
|
|
84
|
+
} else {
|
|
85
|
+
this.htmlRewriterConstructorPromise = import('@worker-tools/html-rewriter/base64')
|
|
86
|
+
.then((module) => module.HTMLRewriter as HtmlRewriterConstructor)
|
|
87
|
+
.catch((error: unknown) => {
|
|
88
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
89
|
+
appLogger.warn(
|
|
90
|
+
`[HtmlTransformerService] Failed to load @worker-tools/html-rewriter/base64, falling back to string injection: ${message}`,
|
|
91
|
+
);
|
|
92
|
+
return null;
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return this.htmlRewriterConstructorPromise;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
private formatAttributes(attrs?: Record<string, string>): string {
|
|
101
|
+
if (!attrs) return '';
|
|
102
|
+
return ` ${Object.entries(attrs)
|
|
103
|
+
.map(([key, value]) => `${key}="${value}"`)
|
|
104
|
+
.join(' ')}`;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
private generateScriptTag(dep: ProcessedAsset & { kind: 'script' }): string {
|
|
108
|
+
return dep.inline
|
|
109
|
+
? `<script${this.formatAttributes(dep.attributes)}>${dep.content}</script>`
|
|
110
|
+
: `<script src="${dep.srcUrl}"${this.formatAttributes(dep.attributes)}></script>`;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
private generateStylesheetTag(dep: ProcessedAsset): string {
|
|
114
|
+
return dep.inline
|
|
115
|
+
? `<style${this.formatAttributes(dep.attributes)}>${dep.content}</style>`
|
|
116
|
+
: `<link rel="stylesheet" href="${dep.srcUrl}"${this.formatAttributes(dep.attributes)}>`;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
private appendDependencies(element: HtmlRewriterElement, dependencies: ProcessedAsset[]) {
|
|
120
|
+
for (const dep of dependencies) {
|
|
121
|
+
const tag =
|
|
122
|
+
dep.kind === 'script' ? this.generateScriptTag(dep as ScriptAsset) : this.generateStylesheetTag(dep);
|
|
123
|
+
element.append(tag, { html: true });
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
private buildDependencyTags(dependencies: ProcessedAsset[]): string {
|
|
128
|
+
return dependencies
|
|
129
|
+
.map((dep) =>
|
|
130
|
+
dep.kind === 'script' ? this.generateScriptTag(dep as ScriptAsset) : this.generateStylesheetTag(dep),
|
|
131
|
+
)
|
|
132
|
+
.join('');
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
private injectBeforeClosingTag(html: string, tag: 'head' | 'body', content: string): string {
|
|
136
|
+
if (!content) {
|
|
137
|
+
return html;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const closingTag = `</${tag}>`;
|
|
141
|
+
const lowerHtml = html.toLowerCase();
|
|
142
|
+
const closingTagIndex = lowerHtml.lastIndexOf(closingTag);
|
|
143
|
+
|
|
144
|
+
if (closingTagIndex !== -1) {
|
|
145
|
+
return `${html.slice(0, closingTagIndex)}${content}${html.slice(closingTagIndex)}`;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if (tag === 'head') {
|
|
149
|
+
return `${content}${html}`;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return `${html}${content}`;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
setProcessedDependencies(processedDependencies: ProcessedAsset[]) {
|
|
156
|
+
this.processedDependencies = processedDependencies;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
getProcessedDependencies(): ProcessedAsset[] {
|
|
160
|
+
return this.processedDependencies;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
async transform(res: Response): Promise<Response> {
|
|
164
|
+
const { head, body } = this.groupDependenciesByPosition();
|
|
165
|
+
const htmlRewriter = await this.createHtmlRewriter();
|
|
166
|
+
|
|
167
|
+
const html = await res.text();
|
|
168
|
+
const headers = new Headers(res.headers);
|
|
169
|
+
|
|
170
|
+
if (htmlRewriter) {
|
|
171
|
+
htmlRewriter
|
|
172
|
+
.on('head', {
|
|
173
|
+
element: (element) => this.appendDependencies(element, head),
|
|
174
|
+
})
|
|
175
|
+
.on('body', {
|
|
176
|
+
element: (element) => this.appendDependencies(element, body),
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
return htmlRewriter.transform(
|
|
180
|
+
new Response(html, {
|
|
181
|
+
headers,
|
|
182
|
+
status: res.status,
|
|
183
|
+
statusText: res.statusText,
|
|
184
|
+
}),
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const withHeadDependencies = this.injectBeforeClosingTag(html, 'head', this.buildDependencyTags(head));
|
|
189
|
+
const transformedHtml = this.injectBeforeClosingTag(
|
|
190
|
+
withHeadDependencies,
|
|
191
|
+
'body',
|
|
192
|
+
this.buildDependencyTags(body),
|
|
193
|
+
);
|
|
194
|
+
|
|
195
|
+
return new Response(transformedHtml, {
|
|
196
|
+
headers,
|
|
197
|
+
status: res.status,
|
|
198
|
+
statusText: res.statusText,
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
private groupDependenciesByPosition() {
|
|
203
|
+
return this.processedDependencies.reduce(
|
|
204
|
+
(acc, dep) => {
|
|
205
|
+
if (dep.kind === 'script') {
|
|
206
|
+
if (dep.excludeFromHtml) return acc;
|
|
207
|
+
const position = dep.position || 'body';
|
|
208
|
+
acc[position].push(dep);
|
|
209
|
+
} else if (dep.kind === 'stylesheet') {
|
|
210
|
+
acc.head.push(dep);
|
|
211
|
+
}
|
|
212
|
+
return acc;
|
|
213
|
+
},
|
|
214
|
+
{ head: [], body: [] } as Record<AssetPosition, ProcessedAsset[]>,
|
|
215
|
+
);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export interface PageModuleImportOptions {
|
|
2
|
+
filePath: string;
|
|
3
|
+
rootDir: string;
|
|
4
|
+
outdir: string;
|
|
5
|
+
externalPackages?: boolean;
|
|
6
|
+
transpileErrorMessage?: (details: string) => string;
|
|
7
|
+
noOutputMessage?: (filePath: string) => string;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Loads source page modules in a runtime-agnostic way.
|
|
11
|
+
*
|
|
12
|
+
* This service centralizes the Bun-vs-Node import strategy used by route
|
|
13
|
+
* scanning, page data loading, and request-time page inspection. In Bun it can
|
|
14
|
+
* import source files directly; in Node it transpiles the file into a dedicated
|
|
15
|
+
* output directory first and then imports the generated module.
|
|
16
|
+
*
|
|
17
|
+
* Keeping this logic in one place prevents subtle drift in cache-busting,
|
|
18
|
+
* transpilation settings, and error semantics across the different callers.
|
|
19
|
+
*/
|
|
20
|
+
export declare class PageModuleImportService {
|
|
21
|
+
private static readonly importCache;
|
|
22
|
+
static clearImportCache(): void;
|
|
23
|
+
/**
|
|
24
|
+
* Imports a page-like module from source.
|
|
25
|
+
*
|
|
26
|
+
* The caller controls the output directory and error wording so different
|
|
27
|
+
* subsystems can reuse the same loading mechanism while preserving their
|
|
28
|
+
* current diagnostics. Module identities stay stable for unchanged files and
|
|
29
|
+
* roll forward automatically when the source hash changes during watch mode.
|
|
30
|
+
*
|
|
31
|
+
* @typeParam T Expected module shape.
|
|
32
|
+
* @param options Runtime-specific import settings.
|
|
33
|
+
* @returns The loaded module.
|
|
34
|
+
*/
|
|
35
|
+
importModule<T = unknown>(options: PageModuleImportOptions): Promise<T>;
|
|
36
|
+
private loadModule;
|
|
37
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { pathToFileURL } from "node:url";
|
|
3
|
+
import { fileSystem } from "@ecopages/file-system";
|
|
4
|
+
import { defaultBuildAdapter } from "../build/build-adapter.js";
|
|
5
|
+
class PageModuleImportService {
|
|
6
|
+
static importCache = /* @__PURE__ */ new Map();
|
|
7
|
+
static clearImportCache() {
|
|
8
|
+
this.importCache.clear();
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Imports a page-like module from source.
|
|
12
|
+
*
|
|
13
|
+
* The caller controls the output directory and error wording so different
|
|
14
|
+
* subsystems can reuse the same loading mechanism while preserving their
|
|
15
|
+
* current diagnostics. Module identities stay stable for unchanged files and
|
|
16
|
+
* roll forward automatically when the source hash changes during watch mode.
|
|
17
|
+
*
|
|
18
|
+
* @typeParam T Expected module shape.
|
|
19
|
+
* @param options Runtime-specific import settings.
|
|
20
|
+
* @returns The loaded module.
|
|
21
|
+
*/
|
|
22
|
+
async importModule(options) {
|
|
23
|
+
const { filePath, rootDir, externalPackages } = options;
|
|
24
|
+
const fileHash = fileSystem.hash(filePath);
|
|
25
|
+
const runtime = typeof Bun !== "undefined" ? "bun" : "node";
|
|
26
|
+
const cacheKey = [runtime, filePath, rootDir, externalPackages ?? "default", fileHash].join("::");
|
|
27
|
+
const cachedModule = PageModuleImportService.importCache.get(cacheKey);
|
|
28
|
+
if (cachedModule) {
|
|
29
|
+
return await cachedModule;
|
|
30
|
+
}
|
|
31
|
+
const importPromise = this.loadModule({
|
|
32
|
+
...options,
|
|
33
|
+
fileHash
|
|
34
|
+
});
|
|
35
|
+
PageModuleImportService.importCache.set(cacheKey, importPromise);
|
|
36
|
+
try {
|
|
37
|
+
return await importPromise;
|
|
38
|
+
} catch (error) {
|
|
39
|
+
PageModuleImportService.importCache.delete(cacheKey);
|
|
40
|
+
throw error;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
async loadModule(options) {
|
|
44
|
+
const {
|
|
45
|
+
filePath,
|
|
46
|
+
rootDir,
|
|
47
|
+
outdir,
|
|
48
|
+
externalPackages,
|
|
49
|
+
transpileErrorMessage = (details) => `Error transpiling page module: ${details}`,
|
|
50
|
+
noOutputMessage = (targetFilePath) => `No transpiled output generated for page module: ${targetFilePath}`,
|
|
51
|
+
fileHash
|
|
52
|
+
} = options;
|
|
53
|
+
if (typeof Bun !== "undefined") {
|
|
54
|
+
const moduleUrl = pathToFileURL(filePath);
|
|
55
|
+
if (process.env.NODE_ENV === "development") {
|
|
56
|
+
moduleUrl.searchParams.set("update", fileHash);
|
|
57
|
+
}
|
|
58
|
+
return await import(moduleUrl.href);
|
|
59
|
+
}
|
|
60
|
+
const fileBaseName = path.basename(filePath, path.extname(filePath));
|
|
61
|
+
const outputFileName = `${fileBaseName}-${fileHash}.js`;
|
|
62
|
+
const buildResult = await defaultBuildAdapter.build({
|
|
63
|
+
entrypoints: [filePath],
|
|
64
|
+
root: rootDir,
|
|
65
|
+
outdir,
|
|
66
|
+
target: "node",
|
|
67
|
+
format: "esm",
|
|
68
|
+
sourcemap: "none",
|
|
69
|
+
splitting: false,
|
|
70
|
+
minify: false,
|
|
71
|
+
naming: outputFileName,
|
|
72
|
+
...externalPackages !== void 0 ? { externalPackages } : {}
|
|
73
|
+
});
|
|
74
|
+
if (!buildResult.success) {
|
|
75
|
+
const details = buildResult.logs.map((log) => log.message).join(" | ");
|
|
76
|
+
throw new Error(transpileErrorMessage(details));
|
|
77
|
+
}
|
|
78
|
+
const preferredOutputPath = path.join(outdir, outputFileName);
|
|
79
|
+
const compiledOutput = buildResult.outputs.find((output) => output.path === preferredOutputPath)?.path ?? buildResult.outputs.find((output) => output.path.endsWith(".js"))?.path;
|
|
80
|
+
if (!compiledOutput) {
|
|
81
|
+
throw new Error(noOutputMessage(filePath));
|
|
82
|
+
}
|
|
83
|
+
return await import(pathToFileURL(compiledOutput).href);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
export {
|
|
87
|
+
PageModuleImportService
|
|
88
|
+
};
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { pathToFileURL } from 'node:url';
|
|
3
|
+
import { fileSystem } from '@ecopages/file-system';
|
|
4
|
+
import { defaultBuildAdapter } from '../build/build-adapter.ts';
|
|
5
|
+
|
|
6
|
+
export interface PageModuleImportOptions {
|
|
7
|
+
filePath: string;
|
|
8
|
+
rootDir: string;
|
|
9
|
+
outdir: string;
|
|
10
|
+
externalPackages?: boolean;
|
|
11
|
+
transpileErrorMessage?: (details: string) => string;
|
|
12
|
+
noOutputMessage?: (filePath: string) => string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Loads source page modules in a runtime-agnostic way.
|
|
17
|
+
*
|
|
18
|
+
* This service centralizes the Bun-vs-Node import strategy used by route
|
|
19
|
+
* scanning, page data loading, and request-time page inspection. In Bun it can
|
|
20
|
+
* import source files directly; in Node it transpiles the file into a dedicated
|
|
21
|
+
* output directory first and then imports the generated module.
|
|
22
|
+
*
|
|
23
|
+
* Keeping this logic in one place prevents subtle drift in cache-busting,
|
|
24
|
+
* transpilation settings, and error semantics across the different callers.
|
|
25
|
+
*/
|
|
26
|
+
export class PageModuleImportService {
|
|
27
|
+
private static readonly importCache = new Map<string, Promise<unknown>>();
|
|
28
|
+
|
|
29
|
+
static clearImportCache(): void {
|
|
30
|
+
this.importCache.clear();
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Imports a page-like module from source.
|
|
35
|
+
*
|
|
36
|
+
* The caller controls the output directory and error wording so different
|
|
37
|
+
* subsystems can reuse the same loading mechanism while preserving their
|
|
38
|
+
* current diagnostics. Module identities stay stable for unchanged files and
|
|
39
|
+
* roll forward automatically when the source hash changes during watch mode.
|
|
40
|
+
*
|
|
41
|
+
* @typeParam T Expected module shape.
|
|
42
|
+
* @param options Runtime-specific import settings.
|
|
43
|
+
* @returns The loaded module.
|
|
44
|
+
*/
|
|
45
|
+
async importModule<T = unknown>(options: PageModuleImportOptions): Promise<T> {
|
|
46
|
+
const { filePath, rootDir, externalPackages } = options;
|
|
47
|
+
|
|
48
|
+
const fileHash = fileSystem.hash(filePath);
|
|
49
|
+
const runtime = typeof Bun !== 'undefined' ? 'bun' : 'node';
|
|
50
|
+
const cacheKey = [runtime, filePath, rootDir, externalPackages ?? 'default', fileHash].join('::');
|
|
51
|
+
const cachedModule = PageModuleImportService.importCache.get(cacheKey);
|
|
52
|
+
|
|
53
|
+
if (cachedModule) {
|
|
54
|
+
return (await cachedModule) as T;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const importPromise = this.loadModule<T>({
|
|
58
|
+
...options,
|
|
59
|
+
fileHash,
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
PageModuleImportService.importCache.set(cacheKey, importPromise);
|
|
63
|
+
|
|
64
|
+
try {
|
|
65
|
+
return await importPromise;
|
|
66
|
+
} catch (error) {
|
|
67
|
+
PageModuleImportService.importCache.delete(cacheKey);
|
|
68
|
+
throw error;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
private async loadModule<T = unknown>(
|
|
73
|
+
options: PageModuleImportOptions & {
|
|
74
|
+
fileHash: string;
|
|
75
|
+
},
|
|
76
|
+
): Promise<T> {
|
|
77
|
+
const {
|
|
78
|
+
filePath,
|
|
79
|
+
rootDir,
|
|
80
|
+
outdir,
|
|
81
|
+
externalPackages,
|
|
82
|
+
transpileErrorMessage = (details) => `Error transpiling page module: ${details}`,
|
|
83
|
+
noOutputMessage = (targetFilePath) => `No transpiled output generated for page module: ${targetFilePath}`,
|
|
84
|
+
fileHash,
|
|
85
|
+
} = options;
|
|
86
|
+
|
|
87
|
+
if (typeof Bun !== 'undefined') {
|
|
88
|
+
const moduleUrl = pathToFileURL(filePath);
|
|
89
|
+
|
|
90
|
+
if (process.env.NODE_ENV === 'development') {
|
|
91
|
+
moduleUrl.searchParams.set('update', fileHash);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return (await import(moduleUrl.href)) as T;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const fileBaseName = path.basename(filePath, path.extname(filePath));
|
|
98
|
+
const outputFileName = `${fileBaseName}-${fileHash}.js`;
|
|
99
|
+
|
|
100
|
+
const buildResult = await defaultBuildAdapter.build({
|
|
101
|
+
entrypoints: [filePath],
|
|
102
|
+
root: rootDir,
|
|
103
|
+
outdir,
|
|
104
|
+
target: 'node',
|
|
105
|
+
format: 'esm',
|
|
106
|
+
sourcemap: 'none',
|
|
107
|
+
splitting: false,
|
|
108
|
+
minify: false,
|
|
109
|
+
naming: outputFileName,
|
|
110
|
+
...(externalPackages !== undefined ? { externalPackages } : {}),
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
if (!buildResult.success) {
|
|
114
|
+
const details = buildResult.logs.map((log) => log.message).join(' | ');
|
|
115
|
+
throw new Error(transpileErrorMessage(details));
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const preferredOutputPath = path.join(outdir, outputFileName);
|
|
119
|
+
const compiledOutput =
|
|
120
|
+
buildResult.outputs.find((output) => output.path === preferredOutputPath)?.path ??
|
|
121
|
+
buildResult.outputs.find((output) => output.path.endsWith('.js'))?.path;
|
|
122
|
+
|
|
123
|
+
if (!compiledOutput) {
|
|
124
|
+
throw new Error(noOutputMessage(filePath));
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
return (await import(pathToFileURL(compiledOutput).href)) as T;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { type PageCacheService } from './cache/page-cache-service.js';
|
|
2
|
+
import type { CacheStrategy, RenderResult } from './cache/cache.types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Coordinates request-time page caching concerns around one render invocation.
|
|
5
|
+
*
|
|
6
|
+
* This service keeps `FileSystemResponseMatcher` from owning low-level cache
|
|
7
|
+
* policy mechanics such as cache key construction, `dynamic` bypass behavior,
|
|
8
|
+
* body normalization for cache storage, and final cache header generation.
|
|
9
|
+
*/
|
|
10
|
+
export declare class PageRequestCacheCoordinator {
|
|
11
|
+
private cacheService;
|
|
12
|
+
private defaultCacheStrategy;
|
|
13
|
+
constructor(cacheService: PageCacheService | null, defaultCacheStrategy: CacheStrategy);
|
|
14
|
+
/**
|
|
15
|
+
* Builds the cache key used for page lookups.
|
|
16
|
+
*
|
|
17
|
+
* Query parameters are part of the key so two requests that hit the same
|
|
18
|
+
* pathname but differ by search params do not share the same rendered entry.
|
|
19
|
+
*
|
|
20
|
+
* @param input Pathname plus optional query record.
|
|
21
|
+
* @returns Stable cache key for the request.
|
|
22
|
+
*/
|
|
23
|
+
buildCacheKey(input: {
|
|
24
|
+
pathname: string;
|
|
25
|
+
query?: Record<string, string>;
|
|
26
|
+
}): string;
|
|
27
|
+
/**
|
|
28
|
+
* Resolves a render request through the configured cache policy.
|
|
29
|
+
*
|
|
30
|
+
* Pages using `dynamic` rendering, or applications without a cache service,
|
|
31
|
+
* bypass cache lookup entirely and still receive the same response header
|
|
32
|
+
* contract as cached pages.
|
|
33
|
+
*
|
|
34
|
+
* @param options Cache coordination inputs for one page request.
|
|
35
|
+
* @returns HTTP response with cache headers applied.
|
|
36
|
+
*/
|
|
37
|
+
render(options: {
|
|
38
|
+
cacheKey: string;
|
|
39
|
+
pageCacheStrategy: CacheStrategy;
|
|
40
|
+
renderFn: () => Promise<RenderResult>;
|
|
41
|
+
}): Promise<Response>;
|
|
42
|
+
/**
|
|
43
|
+
* Exposes the underlying cache service for invalidation and adapter plumbing.
|
|
44
|
+
*
|
|
45
|
+
* @returns Configured cache service or `null` when caching is disabled.
|
|
46
|
+
*/
|
|
47
|
+
getCacheService(): PageCacheService | null;
|
|
48
|
+
/**
|
|
49
|
+
* Returns the default render strategy used when a page does not declare one.
|
|
50
|
+
*
|
|
51
|
+
* @returns Application-level fallback cache strategy.
|
|
52
|
+
*/
|
|
53
|
+
getDefaultCacheStrategy(): CacheStrategy;
|
|
54
|
+
/**
|
|
55
|
+
* Normalizes various route render body shapes into a cacheable string.
|
|
56
|
+
*
|
|
57
|
+
* Page rendering may produce strings, buffers, byte arrays, or streams. The
|
|
58
|
+
* matcher needs a single representation before passing HTML through the cache
|
|
59
|
+
* layer, so this method centralizes the conversion rules.
|
|
60
|
+
*
|
|
61
|
+
* @param body Render output body in any supported form.
|
|
62
|
+
* @returns HTML string representation.
|
|
63
|
+
*/
|
|
64
|
+
bodyToString(body: unknown): Promise<string>;
|
|
65
|
+
/**
|
|
66
|
+
* Creates the final HTML response with the current cache semantics encoded in
|
|
67
|
+
* response headers.
|
|
68
|
+
*
|
|
69
|
+
* @param html Rendered page HTML.
|
|
70
|
+
* @param strategy Effective cache strategy for the response.
|
|
71
|
+
* @param cacheStatus Status used for `X-Cache` and `Cache-Control` generation.
|
|
72
|
+
* @returns HTTP response ready to send to the client.
|
|
73
|
+
*/
|
|
74
|
+
private createCachedResponse;
|
|
75
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { getCacheControlHeader } from "./cache/page-cache-service.js";
|
|
2
|
+
class PageRequestCacheCoordinator {
|
|
3
|
+
constructor(cacheService, defaultCacheStrategy) {
|
|
4
|
+
this.cacheService = cacheService;
|
|
5
|
+
this.defaultCacheStrategy = defaultCacheStrategy;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Builds the cache key used for page lookups.
|
|
9
|
+
*
|
|
10
|
+
* Query parameters are part of the key so two requests that hit the same
|
|
11
|
+
* pathname but differ by search params do not share the same rendered entry.
|
|
12
|
+
*
|
|
13
|
+
* @param input Pathname plus optional query record.
|
|
14
|
+
* @returns Stable cache key for the request.
|
|
15
|
+
*/
|
|
16
|
+
buildCacheKey(input) {
|
|
17
|
+
let key = input.pathname;
|
|
18
|
+
if (input.query && Object.keys(input.query).length > 0) {
|
|
19
|
+
const queryString = new URLSearchParams(input.query).toString();
|
|
20
|
+
key += `?${queryString}`;
|
|
21
|
+
}
|
|
22
|
+
return key;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Resolves a render request through the configured cache policy.
|
|
26
|
+
*
|
|
27
|
+
* Pages using `dynamic` rendering, or applications without a cache service,
|
|
28
|
+
* bypass cache lookup entirely and still receive the same response header
|
|
29
|
+
* contract as cached pages.
|
|
30
|
+
*
|
|
31
|
+
* @param options Cache coordination inputs for one page request.
|
|
32
|
+
* @returns HTTP response with cache headers applied.
|
|
33
|
+
*/
|
|
34
|
+
async render(options) {
|
|
35
|
+
if (!this.cacheService || options.pageCacheStrategy === "dynamic") {
|
|
36
|
+
const { html, strategy } = await options.renderFn();
|
|
37
|
+
return this.createCachedResponse(html, strategy, "disabled");
|
|
38
|
+
}
|
|
39
|
+
const result = await this.cacheService.getOrCreate(
|
|
40
|
+
options.cacheKey,
|
|
41
|
+
options.pageCacheStrategy,
|
|
42
|
+
options.renderFn
|
|
43
|
+
);
|
|
44
|
+
return this.createCachedResponse(result.html, result.strategy, result.status);
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Exposes the underlying cache service for invalidation and adapter plumbing.
|
|
48
|
+
*
|
|
49
|
+
* @returns Configured cache service or `null` when caching is disabled.
|
|
50
|
+
*/
|
|
51
|
+
getCacheService() {
|
|
52
|
+
return this.cacheService;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Returns the default render strategy used when a page does not declare one.
|
|
56
|
+
*
|
|
57
|
+
* @returns Application-level fallback cache strategy.
|
|
58
|
+
*/
|
|
59
|
+
getDefaultCacheStrategy() {
|
|
60
|
+
return this.defaultCacheStrategy;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Normalizes various route render body shapes into a cacheable string.
|
|
64
|
+
*
|
|
65
|
+
* Page rendering may produce strings, buffers, byte arrays, or streams. The
|
|
66
|
+
* matcher needs a single representation before passing HTML through the cache
|
|
67
|
+
* layer, so this method centralizes the conversion rules.
|
|
68
|
+
*
|
|
69
|
+
* @param body Render output body in any supported form.
|
|
70
|
+
* @returns HTML string representation.
|
|
71
|
+
*/
|
|
72
|
+
async bodyToString(body) {
|
|
73
|
+
if (typeof body === "string") {
|
|
74
|
+
return body;
|
|
75
|
+
}
|
|
76
|
+
if (Buffer.isBuffer(body)) {
|
|
77
|
+
return body.toString("utf-8");
|
|
78
|
+
}
|
|
79
|
+
if (body instanceof ReadableStream) {
|
|
80
|
+
return new Response(body).text();
|
|
81
|
+
}
|
|
82
|
+
if (body instanceof Uint8Array) {
|
|
83
|
+
return new TextDecoder().decode(body);
|
|
84
|
+
}
|
|
85
|
+
return String(body);
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Creates the final HTML response with the current cache semantics encoded in
|
|
89
|
+
* response headers.
|
|
90
|
+
*
|
|
91
|
+
* @param html Rendered page HTML.
|
|
92
|
+
* @param strategy Effective cache strategy for the response.
|
|
93
|
+
* @param cacheStatus Status used for `X-Cache` and `Cache-Control` generation.
|
|
94
|
+
* @returns HTTP response ready to send to the client.
|
|
95
|
+
*/
|
|
96
|
+
createCachedResponse(html, strategy, cacheStatus) {
|
|
97
|
+
const headers = {
|
|
98
|
+
"Content-Type": "text/html",
|
|
99
|
+
"Cache-Control": getCacheControlHeader(cacheStatus === "disabled" ? "disabled" : strategy),
|
|
100
|
+
"X-Cache": cacheStatus.toUpperCase()
|
|
101
|
+
};
|
|
102
|
+
return new Response(html, { headers });
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
export {
|
|
106
|
+
PageRequestCacheCoordinator
|
|
107
|
+
};
|