@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
package/src/services/asset-processing-service/processors/stylesheet/file-stylesheet.processor.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { fileSystem } from "@ecopages/file-system";
|
|
3
|
+
import { BaseProcessor } from "../base/base-processor";
|
|
4
|
+
class FileStylesheetProcessor extends BaseProcessor {
|
|
5
|
+
static PROCESSABLE_STYLESHEET_EXTENSIONS = /* @__PURE__ */ new Set([".css", ".scss", ".sass", ".less"]);
|
|
6
|
+
getStyleContent = (srcUrl) => {
|
|
7
|
+
return fileSystem.readFileAsBuffer(srcUrl);
|
|
8
|
+
};
|
|
9
|
+
isProcessableStylesheet(filepath) {
|
|
10
|
+
return FileStylesheetProcessor.PROCESSABLE_STYLESHEET_EXTENSIONS.has(path.extname(filepath));
|
|
11
|
+
}
|
|
12
|
+
async applyStylesheetProcessors(content, filepath) {
|
|
13
|
+
if (!this.isProcessableStylesheet(filepath)) {
|
|
14
|
+
return content;
|
|
15
|
+
}
|
|
16
|
+
let transformedContent = content;
|
|
17
|
+
for (const processor of this.appConfig.processors.values()) {
|
|
18
|
+
const hasCapabilities = processor.getAssetCapabilities().length > 0;
|
|
19
|
+
const canProcessStylesheet = processor.canProcessAsset("stylesheet", filepath);
|
|
20
|
+
if (!canProcessStylesheet && (hasCapabilities || !processor.getName().includes("postcss"))) {
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
23
|
+
if (!processor.matchesFileFilter(filepath)) {
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
const result = await processor.process(transformedContent, filepath);
|
|
27
|
+
if (typeof result === "string") {
|
|
28
|
+
transformedContent = result;
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
if (result instanceof Buffer) {
|
|
32
|
+
transformedContent = result.toString();
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return transformedContent;
|
|
36
|
+
}
|
|
37
|
+
async process(dep) {
|
|
38
|
+
const buffer = this.getStyleContent(dep.filepath);
|
|
39
|
+
const rawContent = buffer.toString();
|
|
40
|
+
const processedContent = await this.applyStylesheetProcessors(rawContent, dep.filepath);
|
|
41
|
+
const hash = this.generateHash(processedContent);
|
|
42
|
+
const cachekey = this.buildCacheKey(dep.filepath, hash, dep);
|
|
43
|
+
return this.getOrProcess(cachekey, () => {
|
|
44
|
+
const filepath = path.join(this.getAssetsDir(), path.relative(this.appConfig.srcDir, dep.filepath));
|
|
45
|
+
const outputBuffer = Buffer.from(processedContent);
|
|
46
|
+
if (!dep.inline) {
|
|
47
|
+
fileSystem.ensureDir(path.dirname(filepath));
|
|
48
|
+
fileSystem.write(filepath, outputBuffer);
|
|
49
|
+
}
|
|
50
|
+
return {
|
|
51
|
+
filepath,
|
|
52
|
+
content: dep.inline ? processedContent : void 0,
|
|
53
|
+
kind: "stylesheet",
|
|
54
|
+
position: dep.position,
|
|
55
|
+
attributes: dep.attributes,
|
|
56
|
+
inline: dep.inline
|
|
57
|
+
};
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
export {
|
|
62
|
+
FileStylesheetProcessor
|
|
63
|
+
};
|
package/src/services/asset-processing-service/processors/stylesheet/file-stylesheet.processor.ts
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { fileSystem } from '@ecopages/file-system';
|
|
3
|
+
import type { FileStylesheetAsset, ProcessedAsset } from '../../assets.types';
|
|
4
|
+
import { BaseProcessor } from '../base/base-processor';
|
|
5
|
+
|
|
6
|
+
export class FileStylesheetProcessor extends BaseProcessor<FileStylesheetAsset> {
|
|
7
|
+
private static readonly PROCESSABLE_STYLESHEET_EXTENSIONS = new Set(['.css', '.scss', '.sass', '.less']);
|
|
8
|
+
|
|
9
|
+
getStyleContent = (srcUrl: string): Buffer => {
|
|
10
|
+
return fileSystem.readFileAsBuffer(srcUrl);
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
private isProcessableStylesheet(filepath: string): boolean {
|
|
14
|
+
return FileStylesheetProcessor.PROCESSABLE_STYLESHEET_EXTENSIONS.has(path.extname(filepath));
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
private async applyStylesheetProcessors(content: string, filepath: string): Promise<string> {
|
|
18
|
+
if (!this.isProcessableStylesheet(filepath)) {
|
|
19
|
+
return content;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
let transformedContent = content;
|
|
23
|
+
|
|
24
|
+
for (const processor of this.appConfig.processors.values()) {
|
|
25
|
+
const hasCapabilities = processor.getAssetCapabilities().length > 0;
|
|
26
|
+
const canProcessStylesheet = processor.canProcessAsset('stylesheet', filepath);
|
|
27
|
+
|
|
28
|
+
if (!canProcessStylesheet && (hasCapabilities || !processor.getName().includes('postcss'))) {
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (!processor.matchesFileFilter(filepath)) {
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const result = await processor.process(transformedContent, filepath);
|
|
37
|
+
|
|
38
|
+
if (typeof result === 'string') {
|
|
39
|
+
transformedContent = result;
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (result instanceof Buffer) {
|
|
44
|
+
transformedContent = result.toString();
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return transformedContent;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async process(dep: FileStylesheetAsset): Promise<ProcessedAsset> {
|
|
52
|
+
const buffer = this.getStyleContent(dep.filepath);
|
|
53
|
+
const rawContent = buffer.toString();
|
|
54
|
+
const processedContent = await this.applyStylesheetProcessors(rawContent, dep.filepath);
|
|
55
|
+
const hash = this.generateHash(processedContent);
|
|
56
|
+
const cachekey = this.buildCacheKey(dep.filepath, hash, dep);
|
|
57
|
+
|
|
58
|
+
return this.getOrProcess(cachekey, () => {
|
|
59
|
+
const filepath = path.join(this.getAssetsDir(), path.relative(this.appConfig.srcDir, dep.filepath));
|
|
60
|
+
const outputBuffer = Buffer.from(processedContent);
|
|
61
|
+
|
|
62
|
+
if (!dep.inline) {
|
|
63
|
+
fileSystem.ensureDir(path.dirname(filepath));
|
|
64
|
+
fileSystem.write(filepath, outputBuffer);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return {
|
|
68
|
+
filepath: filepath,
|
|
69
|
+
content: dep.inline ? processedContent : undefined,
|
|
70
|
+
kind: 'stylesheet',
|
|
71
|
+
position: dep.position,
|
|
72
|
+
attributes: dep.attributes,
|
|
73
|
+
inline: dep.inline,
|
|
74
|
+
};
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cache types and interfaces for page caching and ISR
|
|
3
|
+
* @module
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Result from rendering a page, including HTML and cache strategy.
|
|
7
|
+
*/
|
|
8
|
+
export interface RenderResult {
|
|
9
|
+
html: string;
|
|
10
|
+
strategy: CacheStrategy;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Render strategy configuration for pages.
|
|
14
|
+
* - `'static'`: Render once, cache indefinitely
|
|
15
|
+
* - `'dynamic'`: No caching, render on every request
|
|
16
|
+
* - `{ revalidate, tags }`: Cache with time-based revalidation and optional tags
|
|
17
|
+
*/
|
|
18
|
+
export type CacheStrategy = 'static' | 'dynamic' | {
|
|
19
|
+
/** Seconds until cache is considered stale */
|
|
20
|
+
revalidate: number;
|
|
21
|
+
/** Tags for on-demand invalidation */
|
|
22
|
+
tags?: string[];
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* Cache entry stored in the cache store.
|
|
26
|
+
*/
|
|
27
|
+
export interface CacheEntry {
|
|
28
|
+
/** The rendered HTML content */
|
|
29
|
+
html: string;
|
|
30
|
+
/** Timestamp when the entry was created */
|
|
31
|
+
createdAt: number;
|
|
32
|
+
/** Timestamp when the entry should be revalidated (null = never stale) */
|
|
33
|
+
revalidateAfter: number | null;
|
|
34
|
+
/** Tags associated with this entry for invalidation */
|
|
35
|
+
tags: string[];
|
|
36
|
+
/** The cache strategy used (for generating headers on HIT) */
|
|
37
|
+
strategy: CacheStrategy;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Cache statistics for debugging and monitoring.
|
|
41
|
+
*/
|
|
42
|
+
export interface CacheStats {
|
|
43
|
+
/** Number of entries in the cache */
|
|
44
|
+
entries: number;
|
|
45
|
+
/** Memory usage in bytes (if available) */
|
|
46
|
+
memoryUsage?: number;
|
|
47
|
+
/** Cache hit rate (if tracked) */
|
|
48
|
+
hitRate?: number;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Abstract interface for cache storage backends.
|
|
52
|
+
* Implementations must handle serialization/deserialization internally.
|
|
53
|
+
*/
|
|
54
|
+
export interface CacheStore {
|
|
55
|
+
/** Retrieve an entry by key */
|
|
56
|
+
get(key: string): Promise<CacheEntry | null>;
|
|
57
|
+
/** Store an entry */
|
|
58
|
+
set(key: string, entry: CacheEntry): Promise<void>;
|
|
59
|
+
/** Delete a specific entry */
|
|
60
|
+
delete(key: string): Promise<boolean>;
|
|
61
|
+
/** Delete all entries matching any of the provided tags */
|
|
62
|
+
invalidateByTags(tags: string[]): Promise<number>;
|
|
63
|
+
/** Delete entries by exact path */
|
|
64
|
+
invalidateByPaths(paths: string[]): Promise<number>;
|
|
65
|
+
/** Clear all entries */
|
|
66
|
+
clear(): Promise<void>;
|
|
67
|
+
/** Get cache statistics (optional, for debugging) */
|
|
68
|
+
stats?(): Promise<CacheStats>;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Configuration for the cache system.
|
|
72
|
+
*/
|
|
73
|
+
export interface CacheConfig {
|
|
74
|
+
/**
|
|
75
|
+
* Cache store implementation.
|
|
76
|
+
* @default 'memory'
|
|
77
|
+
*/
|
|
78
|
+
store?: 'memory' | CacheStore;
|
|
79
|
+
/**
|
|
80
|
+
* Default cache strategy for pages that don't specify one.
|
|
81
|
+
* @default 'static'
|
|
82
|
+
*/
|
|
83
|
+
defaultStrategy?: CacheStrategy;
|
|
84
|
+
/**
|
|
85
|
+
* Whether caching is enabled.
|
|
86
|
+
* Automatically disabled in dev mode unless explicitly set.
|
|
87
|
+
* @default true (production), false (development)
|
|
88
|
+
*/
|
|
89
|
+
enabled?: boolean;
|
|
90
|
+
/**
|
|
91
|
+
* Maximum number of entries in the memory cache before LRU eviction.
|
|
92
|
+
* Only applies when using the built-in memory store.
|
|
93
|
+
* @default 1000
|
|
94
|
+
*/
|
|
95
|
+
maxEntries?: number;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Result of a cache lookup operation.
|
|
99
|
+
*/
|
|
100
|
+
export interface CacheResult {
|
|
101
|
+
/** The cached HTML content */
|
|
102
|
+
html: string;
|
|
103
|
+
/** Cache status for X-Cache header */
|
|
104
|
+
status: 'hit' | 'miss' | 'stale';
|
|
105
|
+
/** The cache strategy (for generating Cache-Control headers) */
|
|
106
|
+
strategy: CacheStrategy;
|
|
107
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cache types and interfaces for page caching and ISR
|
|
3
|
+
* @module
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Result from rendering a page, including HTML and cache strategy.
|
|
8
|
+
*/
|
|
9
|
+
export interface RenderResult {
|
|
10
|
+
html: string;
|
|
11
|
+
strategy: CacheStrategy;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Render strategy configuration for pages.
|
|
16
|
+
* - `'static'`: Render once, cache indefinitely
|
|
17
|
+
* - `'dynamic'`: No caching, render on every request
|
|
18
|
+
* - `{ revalidate, tags }`: Cache with time-based revalidation and optional tags
|
|
19
|
+
*/
|
|
20
|
+
export type CacheStrategy =
|
|
21
|
+
| 'static'
|
|
22
|
+
| 'dynamic'
|
|
23
|
+
| {
|
|
24
|
+
/** Seconds until cache is considered stale */
|
|
25
|
+
revalidate: number;
|
|
26
|
+
/** Tags for on-demand invalidation */
|
|
27
|
+
tags?: string[];
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Cache entry stored in the cache store.
|
|
32
|
+
*/
|
|
33
|
+
export interface CacheEntry {
|
|
34
|
+
/** The rendered HTML content */
|
|
35
|
+
html: string;
|
|
36
|
+
/** Timestamp when the entry was created */
|
|
37
|
+
createdAt: number;
|
|
38
|
+
/** Timestamp when the entry should be revalidated (null = never stale) */
|
|
39
|
+
revalidateAfter: number | null;
|
|
40
|
+
/** Tags associated with this entry for invalidation */
|
|
41
|
+
tags: string[];
|
|
42
|
+
/** The cache strategy used (for generating headers on HIT) */
|
|
43
|
+
strategy: CacheStrategy;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Cache statistics for debugging and monitoring.
|
|
48
|
+
*/
|
|
49
|
+
export interface CacheStats {
|
|
50
|
+
/** Number of entries in the cache */
|
|
51
|
+
entries: number;
|
|
52
|
+
/** Memory usage in bytes (if available) */
|
|
53
|
+
memoryUsage?: number;
|
|
54
|
+
/** Cache hit rate (if tracked) */
|
|
55
|
+
hitRate?: number;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Abstract interface for cache storage backends.
|
|
60
|
+
* Implementations must handle serialization/deserialization internally.
|
|
61
|
+
*/
|
|
62
|
+
export interface CacheStore {
|
|
63
|
+
/** Retrieve an entry by key */
|
|
64
|
+
get(key: string): Promise<CacheEntry | null>;
|
|
65
|
+
|
|
66
|
+
/** Store an entry */
|
|
67
|
+
set(key: string, entry: CacheEntry): Promise<void>;
|
|
68
|
+
|
|
69
|
+
/** Delete a specific entry */
|
|
70
|
+
delete(key: string): Promise<boolean>;
|
|
71
|
+
|
|
72
|
+
/** Delete all entries matching any of the provided tags */
|
|
73
|
+
invalidateByTags(tags: string[]): Promise<number>;
|
|
74
|
+
|
|
75
|
+
/** Delete entries by exact path */
|
|
76
|
+
invalidateByPaths(paths: string[]): Promise<number>;
|
|
77
|
+
|
|
78
|
+
/** Clear all entries */
|
|
79
|
+
clear(): Promise<void>;
|
|
80
|
+
|
|
81
|
+
/** Get cache statistics (optional, for debugging) */
|
|
82
|
+
stats?(): Promise<CacheStats>;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Configuration for the cache system.
|
|
87
|
+
*/
|
|
88
|
+
export interface CacheConfig {
|
|
89
|
+
/**
|
|
90
|
+
* Cache store implementation.
|
|
91
|
+
* @default 'memory'
|
|
92
|
+
*/
|
|
93
|
+
store?: 'memory' | CacheStore;
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Default cache strategy for pages that don't specify one.
|
|
97
|
+
* @default 'static'
|
|
98
|
+
*/
|
|
99
|
+
defaultStrategy?: CacheStrategy;
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Whether caching is enabled.
|
|
103
|
+
* Automatically disabled in dev mode unless explicitly set.
|
|
104
|
+
* @default true (production), false (development)
|
|
105
|
+
*/
|
|
106
|
+
enabled?: boolean;
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Maximum number of entries in the memory cache before LRU eviction.
|
|
110
|
+
* Only applies when using the built-in memory store.
|
|
111
|
+
* @default 1000
|
|
112
|
+
*/
|
|
113
|
+
maxEntries?: number;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Result of a cache lookup operation.
|
|
118
|
+
*/
|
|
119
|
+
export interface CacheResult {
|
|
120
|
+
/** The cached HTML content */
|
|
121
|
+
html: string;
|
|
122
|
+
/** Cache status for X-Cache header */
|
|
123
|
+
status: 'hit' | 'miss' | 'stale';
|
|
124
|
+
/** The cache strategy (for generating Cache-Control headers) */
|
|
125
|
+
strategy: CacheStrategy;
|
|
126
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cache services exports
|
|
3
|
+
* @module
|
|
4
|
+
*/
|
|
5
|
+
export type { CacheConfig, CacheEntry, CacheResult, CacheStats, CacheStore, CacheStrategy, RenderResult, } from './cache.types.js';
|
|
6
|
+
export { MemoryCacheStore, type MemoryCacheStoreOptions } from './memory-cache-store.js';
|
|
7
|
+
export { getCacheControlHeader, PageCacheService, type PageCacheServiceOptions } from './page-cache-service.js';
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cache services exports
|
|
3
|
+
* @module
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export type {
|
|
7
|
+
CacheConfig,
|
|
8
|
+
CacheEntry,
|
|
9
|
+
CacheResult,
|
|
10
|
+
CacheStats,
|
|
11
|
+
CacheStore,
|
|
12
|
+
CacheStrategy,
|
|
13
|
+
RenderResult,
|
|
14
|
+
} from './cache.types.ts';
|
|
15
|
+
|
|
16
|
+
export { MemoryCacheStore, type MemoryCacheStoreOptions } from './memory-cache-store.ts';
|
|
17
|
+
|
|
18
|
+
export { getCacheControlHeader, PageCacheService, type PageCacheServiceOptions } from './page-cache-service.ts';
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* In-memory cache store with LRU eviction.
|
|
3
|
+
* Suitable for single-instance deployments and development.
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import type { CacheEntry, CacheStats, CacheStore } from './cache.types.js';
|
|
7
|
+
export interface MemoryCacheStoreOptions {
|
|
8
|
+
/** Maximum number of entries before LRU eviction. @default 1000 */
|
|
9
|
+
maxEntries?: number;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Simple in-memory cache store with LRU eviction.
|
|
13
|
+
* Uses Map insertion order for LRU tracking.
|
|
14
|
+
*/
|
|
15
|
+
export declare class MemoryCacheStore implements CacheStore {
|
|
16
|
+
private cache;
|
|
17
|
+
private tagIndex;
|
|
18
|
+
private readonly maxEntries;
|
|
19
|
+
constructor(options?: MemoryCacheStoreOptions);
|
|
20
|
+
/**
|
|
21
|
+
* Retrieve an entry by key.
|
|
22
|
+
* Uses Map's insertion order for LRU tracking - accessed entries are
|
|
23
|
+
* deleted and re-inserted to move them to the "most recently used" position.
|
|
24
|
+
*/
|
|
25
|
+
get(key: string): Promise<CacheEntry | null>;
|
|
26
|
+
/**
|
|
27
|
+
* Store an entry, evicting the oldest if at capacity.
|
|
28
|
+
* When maxEntries is reached and a new key is added, the first key in
|
|
29
|
+
* the Map (oldest/least-recently-used) is evicted to make room.
|
|
30
|
+
* Updating an existing key refreshes its LRU position.
|
|
31
|
+
*/
|
|
32
|
+
set(key: string, entry: CacheEntry): Promise<void>;
|
|
33
|
+
/**
|
|
34
|
+
* Delete an entry and clean up its tag index references.
|
|
35
|
+
* Removes empty tag sets to prevent memory leaks from accumulated tags.
|
|
36
|
+
*/
|
|
37
|
+
delete(key: string): Promise<boolean>;
|
|
38
|
+
invalidateByTags(tags: string[]): Promise<number>;
|
|
39
|
+
invalidateByPaths(paths: string[]): Promise<number>;
|
|
40
|
+
clear(): Promise<void>;
|
|
41
|
+
stats(): Promise<CacheStats>;
|
|
42
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
class MemoryCacheStore {
|
|
2
|
+
cache = /* @__PURE__ */ new Map();
|
|
3
|
+
tagIndex = /* @__PURE__ */ new Map();
|
|
4
|
+
maxEntries;
|
|
5
|
+
constructor(options = {}) {
|
|
6
|
+
this.maxEntries = options.maxEntries ?? 1e3;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Retrieve an entry by key.
|
|
10
|
+
* Uses Map's insertion order for LRU tracking - accessed entries are
|
|
11
|
+
* deleted and re-inserted to move them to the "most recently used" position.
|
|
12
|
+
*/
|
|
13
|
+
async get(key) {
|
|
14
|
+
const entry = this.cache.get(key);
|
|
15
|
+
if (!entry) return null;
|
|
16
|
+
this.cache.delete(key);
|
|
17
|
+
this.cache.set(key, entry);
|
|
18
|
+
return entry;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Store an entry, evicting the oldest if at capacity.
|
|
22
|
+
* When maxEntries is reached and a new key is added, the first key in
|
|
23
|
+
* the Map (oldest/least-recently-used) is evicted to make room.
|
|
24
|
+
* Updating an existing key refreshes its LRU position.
|
|
25
|
+
*/
|
|
26
|
+
async set(key, entry) {
|
|
27
|
+
const existingEntry = this.cache.get(key);
|
|
28
|
+
if (existingEntry) {
|
|
29
|
+
for (const tag of existingEntry.tags) {
|
|
30
|
+
this.tagIndex.get(tag)?.delete(key);
|
|
31
|
+
if (this.tagIndex.get(tag)?.size === 0) {
|
|
32
|
+
this.tagIndex.delete(tag);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
this.cache.delete(key);
|
|
36
|
+
} else if (this.cache.size >= this.maxEntries) {
|
|
37
|
+
const oldestKey = this.cache.keys().next().value;
|
|
38
|
+
if (oldestKey) await this.delete(oldestKey);
|
|
39
|
+
}
|
|
40
|
+
this.cache.set(key, entry);
|
|
41
|
+
for (const tag of entry.tags) {
|
|
42
|
+
if (!this.tagIndex.has(tag)) {
|
|
43
|
+
this.tagIndex.set(tag, /* @__PURE__ */ new Set());
|
|
44
|
+
}
|
|
45
|
+
this.tagIndex.get(tag).add(key);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Delete an entry and clean up its tag index references.
|
|
50
|
+
* Removes empty tag sets to prevent memory leaks from accumulated tags.
|
|
51
|
+
*/
|
|
52
|
+
async delete(key) {
|
|
53
|
+
const entry = this.cache.get(key);
|
|
54
|
+
if (!entry) return false;
|
|
55
|
+
for (const tag of entry.tags) {
|
|
56
|
+
this.tagIndex.get(tag)?.delete(key);
|
|
57
|
+
if (this.tagIndex.get(tag)?.size === 0) {
|
|
58
|
+
this.tagIndex.delete(tag);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return this.cache.delete(key);
|
|
62
|
+
}
|
|
63
|
+
async invalidateByTags(tags) {
|
|
64
|
+
let count = 0;
|
|
65
|
+
const keysToDelete = /* @__PURE__ */ new Set();
|
|
66
|
+
for (const tag of tags) {
|
|
67
|
+
const keys = this.tagIndex.get(tag);
|
|
68
|
+
if (keys) {
|
|
69
|
+
for (const key of keys) {
|
|
70
|
+
keysToDelete.add(key);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
for (const key of keysToDelete) {
|
|
75
|
+
if (await this.delete(key)) count++;
|
|
76
|
+
}
|
|
77
|
+
return count;
|
|
78
|
+
}
|
|
79
|
+
async invalidateByPaths(paths) {
|
|
80
|
+
let count = 0;
|
|
81
|
+
for (const path of paths) {
|
|
82
|
+
if (await this.delete(path)) count++;
|
|
83
|
+
}
|
|
84
|
+
return count;
|
|
85
|
+
}
|
|
86
|
+
async clear() {
|
|
87
|
+
this.cache.clear();
|
|
88
|
+
this.tagIndex.clear();
|
|
89
|
+
}
|
|
90
|
+
async stats() {
|
|
91
|
+
return {
|
|
92
|
+
entries: this.cache.size
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
export {
|
|
97
|
+
MemoryCacheStore
|
|
98
|
+
};
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* In-memory cache store with LRU eviction.
|
|
3
|
+
* Suitable for single-instance deployments and development.
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { CacheEntry, CacheStats, CacheStore } from './cache.types.ts';
|
|
8
|
+
|
|
9
|
+
export interface MemoryCacheStoreOptions {
|
|
10
|
+
/** Maximum number of entries before LRU eviction. @default 1000 */
|
|
11
|
+
maxEntries?: number;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Simple in-memory cache store with LRU eviction.
|
|
16
|
+
* Uses Map insertion order for LRU tracking.
|
|
17
|
+
*/
|
|
18
|
+
export class MemoryCacheStore implements CacheStore {
|
|
19
|
+
private cache = new Map<string, CacheEntry>();
|
|
20
|
+
private tagIndex = new Map<string, Set<string>>();
|
|
21
|
+
private readonly maxEntries: number;
|
|
22
|
+
|
|
23
|
+
constructor(options: MemoryCacheStoreOptions = {}) {
|
|
24
|
+
this.maxEntries = options.maxEntries ?? 1000;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Retrieve an entry by key.
|
|
29
|
+
* Uses Map's insertion order for LRU tracking - accessed entries are
|
|
30
|
+
* deleted and re-inserted to move them to the "most recently used" position.
|
|
31
|
+
*/
|
|
32
|
+
async get(key: string): Promise<CacheEntry | null> {
|
|
33
|
+
const entry = this.cache.get(key);
|
|
34
|
+
if (!entry) return null;
|
|
35
|
+
|
|
36
|
+
this.cache.delete(key);
|
|
37
|
+
this.cache.set(key, entry);
|
|
38
|
+
|
|
39
|
+
return entry;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Store an entry, evicting the oldest if at capacity.
|
|
44
|
+
* When maxEntries is reached and a new key is added, the first key in
|
|
45
|
+
* the Map (oldest/least-recently-used) is evicted to make room.
|
|
46
|
+
* Updating an existing key refreshes its LRU position.
|
|
47
|
+
*/
|
|
48
|
+
async set(key: string, entry: CacheEntry): Promise<void> {
|
|
49
|
+
const existingEntry = this.cache.get(key);
|
|
50
|
+
|
|
51
|
+
if (existingEntry) {
|
|
52
|
+
for (const tag of existingEntry.tags) {
|
|
53
|
+
this.tagIndex.get(tag)?.delete(key);
|
|
54
|
+
if (this.tagIndex.get(tag)?.size === 0) {
|
|
55
|
+
this.tagIndex.delete(tag);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
this.cache.delete(key);
|
|
59
|
+
} else if (this.cache.size >= this.maxEntries) {
|
|
60
|
+
const oldestKey = this.cache.keys().next().value;
|
|
61
|
+
if (oldestKey) await this.delete(oldestKey);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
this.cache.set(key, entry);
|
|
65
|
+
|
|
66
|
+
for (const tag of entry.tags) {
|
|
67
|
+
if (!this.tagIndex.has(tag)) {
|
|
68
|
+
this.tagIndex.set(tag, new Set());
|
|
69
|
+
}
|
|
70
|
+
this.tagIndex.get(tag)!.add(key);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Delete an entry and clean up its tag index references.
|
|
76
|
+
* Removes empty tag sets to prevent memory leaks from accumulated tags.
|
|
77
|
+
*/
|
|
78
|
+
async delete(key: string): Promise<boolean> {
|
|
79
|
+
const entry = this.cache.get(key);
|
|
80
|
+
if (!entry) return false;
|
|
81
|
+
|
|
82
|
+
for (const tag of entry.tags) {
|
|
83
|
+
this.tagIndex.get(tag)?.delete(key);
|
|
84
|
+
if (this.tagIndex.get(tag)?.size === 0) {
|
|
85
|
+
this.tagIndex.delete(tag);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return this.cache.delete(key);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async invalidateByTags(tags: string[]): Promise<number> {
|
|
93
|
+
let count = 0;
|
|
94
|
+
const keysToDelete = new Set<string>();
|
|
95
|
+
|
|
96
|
+
for (const tag of tags) {
|
|
97
|
+
const keys = this.tagIndex.get(tag);
|
|
98
|
+
if (keys) {
|
|
99
|
+
for (const key of keys) {
|
|
100
|
+
keysToDelete.add(key);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
for (const key of keysToDelete) {
|
|
106
|
+
if (await this.delete(key)) count++;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return count;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
async invalidateByPaths(paths: string[]): Promise<number> {
|
|
113
|
+
let count = 0;
|
|
114
|
+
for (const path of paths) {
|
|
115
|
+
if (await this.delete(path)) count++;
|
|
116
|
+
}
|
|
117
|
+
return count;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
async clear(): Promise<void> {
|
|
121
|
+
this.cache.clear();
|
|
122
|
+
this.tagIndex.clear();
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
async stats(): Promise<CacheStats> {
|
|
126
|
+
return {
|
|
127
|
+
entries: this.cache.size,
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
}
|