@ecopages/core 0.2.0-rc.8 → 0.2.0
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/README.md +39 -38
- package/package.json +22 -152
- package/src/adapters/README.md +11 -3
- package/src/adapters/abstract/application-adapter.d.ts +32 -36
- package/src/adapters/abstract/application-adapter.js +61 -47
- package/src/adapters/abstract/server-adapter.d.ts +14 -0
- package/src/adapters/abstract/server-adapter.js +10 -0
- package/src/adapters/abstract/ws-pattern-matcher.d.ts +0 -5
- package/src/adapters/abstract/ws-pattern-matcher.js +0 -9
- package/src/adapters/bun/create-app.d.ts +12 -16
- package/src/adapters/bun/create-app.js +25 -55
- package/src/adapters/bun/hmr-manager.d.ts +0 -1
- package/src/adapters/bun/hmr-manager.js +2 -4
- package/src/adapters/bun/server-adapter.d.ts +15 -21
- package/src/adapters/bun/server-adapter.js +33 -67
- package/src/adapters/bun/static-preview-host.d.ts +1 -0
- package/src/adapters/bun/static-preview-host.js +1 -1
- package/src/adapters/create-app.d.ts +63 -8
- package/src/adapters/create-app.js +62 -5
- package/src/adapters/node/create-app.d.ts +8 -12
- package/src/adapters/node/create-app.js +22 -14
- package/src/adapters/node/runtime-host.d.ts +10 -10
- package/src/adapters/node/runtime-host.js +13 -8
- package/src/adapters/node/server-adapter-dependencies.d.ts +2 -10
- package/src/adapters/node/server-adapter-dependencies.js +8 -13
- package/src/adapters/node/server-adapter.d.ts +30 -32
- package/src/adapters/node/server-adapter.js +59 -65
- package/src/adapters/shared/hmr/shared-hmr-manager.d.ts +0 -4
- package/src/adapters/shared/hmr/shared-hmr-manager.js +0 -12
- package/src/adapters/shared/http/api-request-pipeline.d.ts +27 -1
- package/src/adapters/shared/http/api-request-pipeline.js +31 -16
- package/src/adapters/shared/http/explicit-static-route-matcher.js +5 -2
- package/src/adapters/shared/http/fs-server-response-factory.d.ts +2 -9
- package/src/adapters/shared/http/fs-server-response-factory.js +5 -33
- package/src/adapters/shared/http/fs-server-response-matcher.d.ts +24 -24
- package/src/adapters/shared/http/fs-server-response-matcher.js +59 -83
- package/src/adapters/shared/runtime/application-adapter.d.ts +1 -0
- package/src/adapters/shared/runtime/application-adapter.js +12 -0
- package/src/adapters/shared/runtime/bind-runtime-server.d.ts +17 -0
- package/src/adapters/shared/runtime/bind-runtime-server.js +50 -0
- package/src/adapters/shared/runtime/collect-dev-prewarm-plan.d.ts +1 -3
- package/src/adapters/shared/runtime/collect-dev-prewarm-plan.js +1 -4
- package/src/adapters/shared/runtime/port-manager-default-prompt.d.ts +6 -0
- package/src/adapters/shared/runtime/port-manager-default-prompt.js +41 -0
- package/src/adapters/shared/runtime/port-manager.d.ts +3 -3
- package/src/adapters/shared/runtime/port-manager.js +14 -45
- package/src/adapters/shared/runtime/runtime-app-bootstrap.d.ts +0 -10
- package/src/adapters/shared/runtime/runtime-app-bootstrap.js +0 -7
- package/src/adapters/shared/runtime/runtime-host.d.ts +5 -2
- package/src/adapters/shared/runtime/runtime-server-lifecycle.d.ts +1 -1
- package/src/adapters/shared/runtime/runtime-server-lifecycle.js +1 -1
- package/src/adapters/shared/runtime/server-adapter.d.ts +26 -5
- package/src/adapters/shared/runtime/server-adapter.js +35 -8
- package/src/adapters/shared/runtime/server-route-handler.js +11 -3
- package/src/adapters/shared/runtime/server-static-builder.d.ts +9 -3
- package/src/adapters/shared/runtime/server-static-builder.js +59 -37
- package/src/adapters/shared/ws/node-http-websocket-upgrades.d.ts +9 -1
- package/src/adapters/shared/ws/node-http-websocket-upgrades.js +8 -2
- package/src/build/README.md +12 -8
- package/src/build/cache/cache-constants.d.ts +0 -2
- package/src/build/cache/cache-constants.js +0 -2
- package/src/build/cache/cache-keys.d.ts +0 -2
- package/src/build/cache/cache-keys.js +0 -2
- package/src/build/cache/output-imports.d.ts +35 -0
- package/src/build/cache/output-imports.js +87 -0
- package/src/build/cache/pages-unified-graph-build.d.ts +15 -1
- package/src/build/cache/pages-unified-graph-build.js +73 -22
- package/src/build/cache/production-build-cache.d.ts +2 -2
- package/src/build/cache/production-build-cache.js +2 -2
- package/src/build/cache/server-entry-build-cache.d.ts +25 -3
- package/src/build/cache/server-entry-build-cache.js +129 -13
- package/src/build/contracts/build-contracts.d.ts +1 -13
- package/src/build/rolldown/runtime-build-output-normalizer.js +3 -6
- package/src/build/runtime/build-request-identity.d.ts +2 -6
- package/src/build/runtime/build-request-identity.js +2 -8
- package/src/build/runtime/build-request-policy.js +1 -1
- package/src/build/runtime/deduping-build-executor.d.ts +0 -1
- package/src/build/runtime/deduping-build-executor.js +0 -1
- package/src/build/server-bundle-publication.d.ts +14 -0
- package/src/build/server-bundle-publication.js +49 -0
- package/src/cache/index.d.ts +1 -1
- package/src/cache/index.js +1 -1
- package/src/cache/module-parse-cache.d.ts +2 -29
- package/src/cache/module-parse-cache.js +6 -39
- package/src/config/README.md +17 -2
- package/src/config/apply-user-config.d.ts +8 -0
- package/src/config/apply-user-config.js +68 -0
- package/src/config/config-builder.d.ts +11 -1
- package/src/config/config-builder.js +53 -12
- package/src/config/constants.d.ts +4 -4
- package/src/config/constants.js +4 -4
- package/src/config/define-config.d.ts +9 -0
- package/src/config/define-config.js +10 -0
- package/src/config/index.d.ts +5 -0
- package/src/config/index.js +4 -0
- package/src/config/is-finalized-app-config.d.ts +6 -0
- package/src/config/is-finalized-app-config.js +11 -0
- package/src/config/load-eco-config.d.ts +21 -0
- package/src/config/load-eco-config.js +107 -0
- package/src/config/resolve-eco-config-path.d.ts +32 -0
- package/src/config/resolve-eco-config-path.js +58 -0
- package/src/config/server-config-bundle.d.ts +25 -0
- package/src/config/server-config-bundle.js +148 -0
- package/src/config/user-config-types.d.ts +170 -0
- package/src/config/user-config-types.js +1 -0
- package/src/dev/README.md +4 -0
- package/src/dev/development-restart-watch-paths.d.ts +13 -0
- package/src/dev/development-restart-watch-paths.js +37 -0
- package/src/dev/development-restart.d.ts +13 -0
- package/src/dev/development-restart.js +21 -0
- package/src/dev/host-runtime.d.ts +0 -1
- package/src/dev/host-runtime.js +0 -3
- package/src/dev/sc-server.d.ts +4 -1
- package/src/dev/sc-server.js +11 -20
- package/src/dev/transform-server/dev-transform-import-rewriter.js +2 -2
- package/src/eco/eco.js +4 -0
- package/src/errors/http-error-page-contract.d.ts +77 -0
- package/src/errors/http-error-page-contract.js +106 -0
- package/src/hmr/hmr-file-change-prep.d.ts +0 -1
- package/src/hmr/hmr-file-change-prep.js +2 -5
- package/src/hmr/hmr-strategy.d.ts +3 -8
- package/src/hmr/hmr-strategy.js +3 -8
- package/src/index.browser.d.ts +1 -2
- package/src/index.browser.js +1 -2
- package/src/index.d.ts +5 -6
- package/src/index.js +5 -6
- package/src/islands/island-host.d.ts +0 -9
- package/src/islands/island-host.js +1 -13
- package/src/plugins/component-import-discovery.js +2 -2
- package/src/plugins/eco-component-meta-plugin.js +5 -5
- package/src/plugins/integration-plugin.d.ts +0 -1
- package/src/plugins/integration-plugin.js +0 -4
- package/src/plugins/processor.d.ts +6 -2
- package/src/route-renderer/README.md +4 -0
- package/src/{adapters/shared/http/explicit-static-render-preparation.d.ts → route-renderer/explicit-view-render-preparation.d.ts} +3 -3
- package/src/route-renderer/orchestration/custom-element-scripts/README.md +2 -2
- package/src/route-renderer/orchestration/custom-element-scripts/custom-element-script-preloader.d.ts +14 -2
- package/src/route-renderer/orchestration/custom-element-scripts/custom-element-script-preloader.js +12 -5
- package/src/route-renderer/orchestration/custom-element-scripts/custom-element-server-module-importer.d.ts +8 -0
- package/src/route-renderer/orchestration/custom-element-scripts/custom-element-server-module-importer.js +19 -0
- package/src/route-renderer/orchestration/document-shell/document-shell-render.service.js +1 -4
- package/src/route-renderer/orchestration/foreign-child/foreign-subtree-execution.service.d.ts +25 -24
- package/src/route-renderer/orchestration/foreign-child/foreign-subtree-execution.service.js +18 -20
- package/src/route-renderer/orchestration/integration-renderer.d.ts +24 -7
- package/src/route-renderer/orchestration/integration-renderer.js +44 -33
- package/src/route-renderer/orchestration/integration-renderer.test-fixtures.js +1 -1
- package/src/route-renderer/orchestration/page-browser-graph/page-browser-graph-session.d.ts +1 -1
- package/src/route-renderer/orchestration/page-browser-graph/page-browser-graph-session.js +1 -1
- package/src/route-renderer/route-renderer.d.ts +9 -0
- package/src/route-renderer/route-renderer.js +18 -0
- package/src/services/README.md +2 -1
- package/src/services/assets/browser-bundle.service.d.ts +1 -6
- package/src/services/error-pages/README.md +9 -0
- package/src/services/error-pages/default-error-pages.d.ts +39 -0
- package/src/services/error-pages/default-error-pages.js +199 -0
- package/src/services/error-pages/error-page-renderer.d.ts +58 -0
- package/src/services/error-pages/error-page-renderer.js +146 -0
- package/src/services/error-pages/semantic-error-page-exporter.d.ts +23 -0
- package/src/services/error-pages/semantic-error-page-exporter.js +35 -0
- package/src/services/html/html-rewriter.d.ts +77 -0
- package/src/services/html/html-rewriter.js +751 -0
- package/src/services/html/html-rewriter.parity.test.bun.d.ts +1 -0
- package/src/services/html/html-rewriter.parity.test.bun.js +74 -0
- package/src/services/html/html-transformer.service.d.ts +29 -42
- package/src/services/html/html-transformer.service.js +85 -157
- package/src/services/invalidation/development-invalidation.service.d.ts +9 -20
- package/src/services/invalidation/development-invalidation.service.js +26 -39
- package/src/services/module-loading/README.md +20 -15
- package/src/services/module-loading/app-module-loader.service.d.ts +0 -2
- package/src/services/module-loading/app-server-module-transpiler.service.d.ts +1 -1
- package/src/services/module-loading/app-server-module-transpiler.service.js +2 -10
- package/src/services/module-loading/collection-server-module-build.service.d.ts +0 -1
- package/src/services/module-loading/collection-server-module-build.service.js +0 -3
- package/src/services/module-loading/host-module-loader-registry.d.ts +0 -1
- package/src/services/module-loading/host-module-loader-registry.js +0 -3
- package/src/services/module-loading/route-module-build-cache-registry.d.ts +0 -4
- package/src/services/module-loading/route-module-build-cache-registry.js +0 -4
- package/src/services/module-loading/route-module-build-cache.store.d.ts +8 -0
- package/src/services/module-loading/route-module-build-cache.store.js +25 -7
- package/src/services/module-loading/route-module-build-manifest.d.ts +12 -1
- package/src/services/module-loading/route-module-build-manifest.js +7 -3
- package/src/services/module-loading/route-module-dependency-hasher.d.ts +3 -2
- package/src/services/module-loading/route-module-dependency-hasher.js +13 -1
- package/src/services/module-loading/server-module-transpiler.service.d.ts +1 -15
- package/src/services/module-loading/server-module-transpiler.service.js +0 -24
- package/src/services/module-loading/view-module-loader.d.ts +10 -0
- package/src/services/module-loading/view-module-loader.js +46 -0
- package/src/services/runtime-state/entrypoint-dependency-graph.service.js +0 -12
- package/src/services/runtime-state/server-invalidation-state.service.d.ts +4 -2
- package/src/services/runtime-state/server-invalidation-state.service.js +7 -14
- package/src/static-site-generator/README.md +12 -8
- package/src/static-site-generator/static-site-generator.d.ts +3 -2
- package/src/static-site-generator/static-site-generator.js +17 -3
- package/src/types/internal-types.d.ts +1 -38
- package/src/types/public-types.d.ts +28 -130
- package/src/watchers/project-watcher.d.ts +9 -16
- package/src/watchers/project-watcher.js +35 -28
- package/src/watchers/project-watcher.test-helpers.d.ts +10 -1
- package/src/watchers/project-watcher.test-helpers.js +12 -0
- package/src/adapters/abstract/router-adapter.d.ts +0 -26
- package/src/adapters/abstract/router-adapter.js +0 -13
- package/src/adapters/bun/index.d.ts +0 -2
- package/src/adapters/bun/index.js +0 -2
- package/src/adapters/index.d.ts +0 -2
- package/src/adapters/index.js +0 -2
- package/src/eco/eco.utils.d.ts +0 -1
- package/src/eco/eco.utils.js +0 -1
- package/src/services/cache/index.d.ts +0 -9
- package/src/services/cache/index.js +0 -8
- package/src/services/html/html-rewriter-provider.service.d.ts +0 -40
- package/src/services/html/html-rewriter-provider.service.js +0 -68
- package/src/services/module-loading/route-module-build-cache.d.ts +0 -4
- package/src/services/module-loading/route-module-build-cache.js +0 -3
- package/src/services/runtime-state/dev-graph.service.d.ts +0 -118
- package/src/services/runtime-state/dev-graph.service.js +0 -170
- /package/src/{adapters/shared/http/explicit-static-render-preparation.js → route-renderer/explicit-view-render-preparation.js} +0 -0
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import { isDevelopmentRuntime } from '../../utils/runtime.js';
|
|
2
|
+
import { ERROR_PAGE_COPY, isHttpErrorPageStatus, isHttpErrorStatus } from '../../errors/http-error-page-contract.js';
|
|
3
|
+
function escapeHtml(value) {
|
|
4
|
+
return value
|
|
5
|
+
.replaceAll('&', '&')
|
|
6
|
+
.replaceAll('<', '<')
|
|
7
|
+
.replaceAll('>', '>')
|
|
8
|
+
.replaceAll('"', '"')
|
|
9
|
+
.replaceAll("'", ''');
|
|
10
|
+
}
|
|
11
|
+
/** Stable class hooks exposed by the framework-owned error documents. */
|
|
12
|
+
export const DEFAULT_ERROR_PAGE_CLASS_NAMES = {
|
|
13
|
+
root: 'eco-error-page',
|
|
14
|
+
notFoundModifier: 'eco-error-page--not-found',
|
|
15
|
+
serverErrorModifier: 'eco-error-page--server-error',
|
|
16
|
+
badRequestModifier: 'eco-error-page--bad-request',
|
|
17
|
+
unauthorizedModifier: 'eco-error-page--unauthorized',
|
|
18
|
+
forbiddenModifier: 'eco-error-page--forbidden',
|
|
19
|
+
conflictModifier: 'eco-error-page--conflict',
|
|
20
|
+
content: 'eco-error-page__content',
|
|
21
|
+
status: 'eco-error-page__status',
|
|
22
|
+
title: 'eco-error-page__title',
|
|
23
|
+
message: 'eco-error-page__message',
|
|
24
|
+
diagnostics: 'eco-error-page__diagnostics',
|
|
25
|
+
diagnosticsHeader: 'eco-error-page__diagnostics-header',
|
|
26
|
+
diagnosticsLabel: 'eco-error-page__diagnostics-label',
|
|
27
|
+
stack: 'eco-error-page__stack',
|
|
28
|
+
copyButton: 'eco-error-page__copy',
|
|
29
|
+
copyButtonCopied: 'eco-error-page__copy--copied',
|
|
30
|
+
copyButtonFailed: 'eco-error-page__copy--failed',
|
|
31
|
+
copyIcon: 'eco-error-page__copy-icon',
|
|
32
|
+
copyIconClipboard: 'eco-error-page__copy-icon--clipboard',
|
|
33
|
+
copyIconSuccess: 'eco-error-page__copy-icon--success',
|
|
34
|
+
copyLabel: 'eco-error-page__copy-label',
|
|
35
|
+
};
|
|
36
|
+
const sharedStyles = `
|
|
37
|
+
.eco-error-page { box-sizing: border-box; display: grid; place-items: center; margin: 0; padding: 2rem; color: #18181b; background: radial-gradient(circle at top, #f4f4f5, #fff 42rem); font-family: ui-sans-serif, system-ui, sans-serif; line-height: 1.5; }
|
|
38
|
+
.eco-error-page__content { width: min(100%, 42rem); padding: clamp(2rem, 8vw, 4.5rem); border: 1px solid #e4e4e7; border-radius: 1rem; background: #fff; }
|
|
39
|
+
.eco-error-page__status { margin: 0 0 1rem; color: #71717a; font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace; font-size: .75rem; font-weight: 700; letter-spacing: .12em; }
|
|
40
|
+
.eco-error-page__title { margin: 0; font-size: clamp(2rem, 6vw, 3.5rem); font-weight: 650; letter-spacing: -.05em; line-height: 1.05; }
|
|
41
|
+
.eco-error-page__message { max-width: 36rem; margin: 1rem 0 0; color: #52525b; font-size: 1rem; }
|
|
42
|
+
.eco-error-page__diagnostics { margin-top: 2rem; border: 1px solid #e4e4e7; border-radius: .75rem; overflow: hidden; background: #fafafa; }
|
|
43
|
+
.eco-error-page__diagnostics-header { display: flex; align-items: center; justify-content: space-between; gap: 1rem; min-height: 3rem; padding: 0 .75rem 0 1rem; border-bottom: 1px solid #e4e4e7; background: #fff; }
|
|
44
|
+
.eco-error-page__diagnostics-label { color: #52525b; font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace; font-size: .75rem; font-weight: 650; }
|
|
45
|
+
.eco-error-page__stack { max-height: 22rem; overflow: auto; box-sizing: border-box; margin: 0; padding: 1rem; color: #3f3f46; font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace; font-size: .8125rem; line-height: 1.65; white-space: pre-wrap; word-break: break-word; }
|
|
46
|
+
.eco-error-page__copy { font: inherit; cursor: pointer; display: inline-flex; align-items: center; gap: .5rem; min-height: 2rem; padding: .375rem .625rem; border: 1px solid #d4d4d8; border-radius: .4375rem; color: #3f3f46; background: #fff; font-size: .8125rem; font-weight: 600; transition: border-color .15s ease, background-color .15s ease, color .15s ease; }
|
|
47
|
+
.eco-error-page__copy:hover { border-color: #a1a1aa; background: #fafafa; }
|
|
48
|
+
.eco-error-page__copy--copied { border-color: #86efac; background: #f0fdf4; color: #166534; }
|
|
49
|
+
.eco-error-page__copy--failed { border-color: #fecaca; background: #fef2f2; color: #b91c1c; }
|
|
50
|
+
.eco-error-page__copy-icon { display: inline-flex; flex-shrink: 0; width: 1rem; height: 1rem; }
|
|
51
|
+
.eco-error-page__copy-icon svg { display: block; width: 100%; height: 100%; }
|
|
52
|
+
.eco-error-page__copy-icon--success { display: none; }
|
|
53
|
+
.eco-error-page__copy--copied .eco-error-page__copy-icon--clipboard { display: none; }
|
|
54
|
+
.eco-error-page__copy--copied .eco-error-page__copy-icon--success { display: inline-flex; }
|
|
55
|
+
.eco-error-page__copy:focus-visible { outline: 2px solid #2563eb; outline-offset: 2px; }
|
|
56
|
+
@media (max-width: 40rem) { .eco-error-page { padding: 1rem; } .eco-error-page__content { padding: 2rem 1.5rem; border-radius: .75rem; } }
|
|
57
|
+
@media (prefers-color-scheme: dark) { .eco-error-page { color: #f4f4f5; background: radial-gradient(circle at top, #27272a, #09090b 42rem); } .eco-error-page__content { border-color: #3f3f46; background: #18181b; } .eco-error-page__status, .eco-error-page__message { color: #a1a1aa; } .eco-error-page__diagnostics { border-color: #3f3f46; background: #18181b; } .eco-error-page__diagnostics-header { border-color: #3f3f46; background: #27272a; } .eco-error-page__diagnostics-label, .eco-error-page__stack { color: #d4d4d8; } .eco-error-page__copy { border-color: #52525b; color: #e4e4e7; background: #27272a; } .eco-error-page__copy:hover { border-color: #a1a1aa; background: #3f3f46; } .eco-error-page__copy--copied { border-color: #166534; background: #052e16; color: #bbf7d0; } .eco-error-page__copy--failed { border-color: #991b1b; background: #450a0a; color: #fecaca; } }
|
|
58
|
+
`.trim();
|
|
59
|
+
const clipboardIconSvg = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><rect width="8" height="4" x="8" y="2" rx="1" ry="1"/><path d="M16 4h2a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2h2"/></svg>';
|
|
60
|
+
const checkIconSvg = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M20 6 9 17l-5-5"/></svg>';
|
|
61
|
+
function buildDefaultCopyButtonMarkup(copyPayload) {
|
|
62
|
+
const names = DEFAULT_ERROR_PAGE_CLASS_NAMES;
|
|
63
|
+
return `<pre id="eco-copy-error-payload" hidden>${escapeHtml(copyPayload)}</pre>
|
|
64
|
+
<button type="button" class="${names.copyButton}" id="eco-copy-error" aria-label="Copy error details to clipboard">
|
|
65
|
+
<span class="${names.copyIcon} ${names.copyIconClipboard}" aria-hidden="true">${clipboardIconSvg}</span>
|
|
66
|
+
<span class="${names.copyIcon} ${names.copyIconSuccess}" aria-hidden="true">${checkIconSvg}</span>
|
|
67
|
+
<span class="${names.copyLabel}" aria-live="polite">Copy error</span>
|
|
68
|
+
</button>
|
|
69
|
+
<script>
|
|
70
|
+
(function () {
|
|
71
|
+
var button = document.getElementById('eco-copy-error');
|
|
72
|
+
var payload = document.getElementById('eco-copy-error-payload');
|
|
73
|
+
if (!button || !payload) return;
|
|
74
|
+
var label = button.querySelector('.${names.copyLabel}');
|
|
75
|
+
var text = payload.textContent || '';
|
|
76
|
+
var resetTimer;
|
|
77
|
+
function setState(state) {
|
|
78
|
+
button.classList.toggle('${names.copyButtonCopied}', state === 'copied');
|
|
79
|
+
button.classList.toggle('${names.copyButtonFailed}', state === 'failed');
|
|
80
|
+
if (label) label.textContent = state === 'copied' ? 'Copied' : state === 'failed' ? 'Copy failed' : 'Copy error';
|
|
81
|
+
button.setAttribute('aria-label', state === 'copied' ? 'Error details copied' : state === 'failed' ? 'Could not copy error details' : 'Copy error details to clipboard');
|
|
82
|
+
}
|
|
83
|
+
function fallbackCopy(value) {
|
|
84
|
+
var area = document.createElement('textarea');
|
|
85
|
+
area.value = value;
|
|
86
|
+
area.setAttribute('readonly', '');
|
|
87
|
+
area.style.position = 'absolute';
|
|
88
|
+
area.style.left = '-9999px';
|
|
89
|
+
document.body.appendChild(area);
|
|
90
|
+
area.select();
|
|
91
|
+
try {
|
|
92
|
+
if (!document.execCommand('copy')) throw new Error('Clipboard copy failed');
|
|
93
|
+
} finally { document.body.removeChild(area); }
|
|
94
|
+
}
|
|
95
|
+
function copyToClipboard(value) {
|
|
96
|
+
if (navigator.clipboard && navigator.clipboard.writeText) return navigator.clipboard.writeText(value);
|
|
97
|
+
return Promise.resolve().then(function () { fallbackCopy(value); });
|
|
98
|
+
}
|
|
99
|
+
button.addEventListener('click', function () {
|
|
100
|
+
copyToClipboard(text).then(function () {
|
|
101
|
+
setState('copied');
|
|
102
|
+
clearTimeout(resetTimer);
|
|
103
|
+
resetTimer = setTimeout(function () { setState('idle'); }, 2000);
|
|
104
|
+
}).catch(function () {
|
|
105
|
+
setState('failed');
|
|
106
|
+
clearTimeout(resetTimer);
|
|
107
|
+
resetTimer = setTimeout(function () { setState('idle'); }, 3000);
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
})();
|
|
111
|
+
</script>`;
|
|
112
|
+
}
|
|
113
|
+
function copyForStatus(status) {
|
|
114
|
+
if (isHttpErrorPageStatus(status)) {
|
|
115
|
+
return ERROR_PAGE_COPY[status];
|
|
116
|
+
}
|
|
117
|
+
if (isHttpErrorStatus(status) && status >= 500) {
|
|
118
|
+
return ERROR_PAGE_COPY[500];
|
|
119
|
+
}
|
|
120
|
+
return {
|
|
121
|
+
title: `Error ${status}`,
|
|
122
|
+
message: 'An error occurred while handling this request.',
|
|
123
|
+
modifier: `status-${status}`,
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
function normalizeDetail(value) {
|
|
127
|
+
const trimmed = value?.trim();
|
|
128
|
+
return trimmed ? trimmed : undefined;
|
|
129
|
+
}
|
|
130
|
+
function formatDiagnosticsCopyPayload(message, stack) {
|
|
131
|
+
const parts = [];
|
|
132
|
+
if (message)
|
|
133
|
+
parts.push(`Message: ${message}`);
|
|
134
|
+
if (stack)
|
|
135
|
+
parts.push(`Stack:\n${stack}`);
|
|
136
|
+
return parts.join('\n\n');
|
|
137
|
+
}
|
|
138
|
+
function createDefaultErrorPageViewModel(status, details) {
|
|
139
|
+
const copy = copyForStatus(status);
|
|
140
|
+
const isServerError = status >= 500;
|
|
141
|
+
const stack = isServerError ? normalizeDetail(details?.stack) : undefined;
|
|
142
|
+
const showDiagnostics = isServerError && isDevelopmentRuntime() && Boolean(details?.message || stack);
|
|
143
|
+
const diagnosticMessage = showDiagnostics ? normalizeDetail(details?.message) : undefined;
|
|
144
|
+
return {
|
|
145
|
+
status,
|
|
146
|
+
title: copy.title,
|
|
147
|
+
modifierClass: `eco-error-page--${copy.modifier}`,
|
|
148
|
+
bodyMessage: isServerError ? copy.message : normalizeDetail(details?.message) || copy.message,
|
|
149
|
+
diagnostics: showDiagnostics
|
|
150
|
+
? {
|
|
151
|
+
message: diagnosticMessage,
|
|
152
|
+
stack,
|
|
153
|
+
copyPayload: formatDiagnosticsCopyPayload(diagnosticMessage, stack),
|
|
154
|
+
}
|
|
155
|
+
: undefined,
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
function renderDiagnosticsMarkup(diagnostics) {
|
|
159
|
+
const names = DEFAULT_ERROR_PAGE_CLASS_NAMES;
|
|
160
|
+
const copyMarkup = buildDefaultCopyButtonMarkup(diagnostics.copyPayload);
|
|
161
|
+
const messageMarkup = diagnostics.message
|
|
162
|
+
? `<p class="${names.message}" role="alert">${escapeHtml(diagnostics.message)}</p>`
|
|
163
|
+
: '';
|
|
164
|
+
if (!diagnostics.stack) {
|
|
165
|
+
return `${messageMarkup}${copyMarkup}`;
|
|
166
|
+
}
|
|
167
|
+
return `${messageMarkup}<section class="${names.diagnostics}" aria-label="Error stack trace"><div class="${names.diagnosticsHeader}"><span class="${names.diagnosticsLabel}">Stack trace</span>${copyMarkup}</div><pre class="${names.stack}">${escapeHtml(diagnostics.stack)}</pre></section>`;
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Self-contained HTML for a framework default error response.
|
|
171
|
+
*
|
|
172
|
+
* @remarks
|
|
173
|
+
* Development 5xx pages may include escaped `message` / `stack` and a copy
|
|
174
|
+
* button. 4xx pages never serialize a stack.
|
|
175
|
+
*/
|
|
176
|
+
export function buildDefaultErrorHtml(status, details) {
|
|
177
|
+
const names = DEFAULT_ERROR_PAGE_CLASS_NAMES;
|
|
178
|
+
const view = createDefaultErrorPageViewModel(status, details);
|
|
179
|
+
const detailsBlock = view.diagnostics ? renderDiagnosticsMarkup(view.diagnostics) : '';
|
|
180
|
+
return `<!DOCTYPE html>
|
|
181
|
+
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>${escapeHtml(view.title)}</title><style>${sharedStyles}</style></head>
|
|
182
|
+
<body class="${names.root} ${view.modifierClass}"><main class="${names.content}"><p class="${names.status}" aria-hidden="true">ERROR ${view.status}</p><h1 class="${names.title}">${escapeHtml(view.title)}</h1><p class="${names.message}">${escapeHtml(view.bodyMessage)}</p>${detailsBlock}</main></body></html>`;
|
|
183
|
+
}
|
|
184
|
+
export function getDefaultServerErrorDetails(error) {
|
|
185
|
+
if (!isDevelopmentRuntime() || error === undefined) {
|
|
186
|
+
return undefined;
|
|
187
|
+
}
|
|
188
|
+
if (error instanceof Error) {
|
|
189
|
+
return { message: error.message, stack: error.stack };
|
|
190
|
+
}
|
|
191
|
+
return { message: String(error) };
|
|
192
|
+
}
|
|
193
|
+
export function getPublicErrorMessage(error) {
|
|
194
|
+
if (error instanceof Error) {
|
|
195
|
+
const message = error.message.trim();
|
|
196
|
+
return message.length > 0 ? message : undefined;
|
|
197
|
+
}
|
|
198
|
+
return undefined;
|
|
199
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { EcoPagesAppConfig } from '../../types/internal-types.js';
|
|
2
|
+
import type { ErrorPageLoaders, RouteRendererBody } from '../../types/public-types.js';
|
|
3
|
+
import type { StaticGenerationRendererResolver } from '../../route-renderer/route-renderer.js';
|
|
4
|
+
import { type ErrorPageKind } from '../../errors/http-error-page-contract.js';
|
|
5
|
+
export type { ErrorPageKind };
|
|
6
|
+
export type ErrorPageRenderResult = {
|
|
7
|
+
body: RouteRendererBody;
|
|
8
|
+
sourceFile?: string;
|
|
9
|
+
};
|
|
10
|
+
export type ErrorPageRenderInput = {
|
|
11
|
+
kind?: ErrorPageKind;
|
|
12
|
+
status?: number;
|
|
13
|
+
error?: unknown;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Owns error-page source precedence and rendering for runtime and static paths.
|
|
17
|
+
*
|
|
18
|
+
* @remarks
|
|
19
|
+
* Filesystem semantic pages win over explicitly registered views. A missing
|
|
20
|
+
* custom source is the only case that uses a framework default; rendering
|
|
21
|
+
* failures are intentionally propagated so callers can apply their own retry
|
|
22
|
+
* or fallback policy without hiding the original failure.
|
|
23
|
+
*/
|
|
24
|
+
export declare class ErrorPageRenderer {
|
|
25
|
+
private readonly appConfig;
|
|
26
|
+
private readonly routeRendererFactory?;
|
|
27
|
+
private readonly loaders;
|
|
28
|
+
constructor({ appConfig, routeRendererFactory, errorPageLoaders, }: {
|
|
29
|
+
appConfig: EcoPagesAppConfig;
|
|
30
|
+
routeRendererFactory?: StaticGenerationRendererResolver;
|
|
31
|
+
errorPageLoaders?: ErrorPageLoaders;
|
|
32
|
+
});
|
|
33
|
+
render(input: ErrorPageRenderInput): Promise<ErrorPageRenderResult>;
|
|
34
|
+
/**
|
|
35
|
+
* Renders the framework-owned document, ignoring custom filesystem pages and
|
|
36
|
+
* registered views.
|
|
37
|
+
*
|
|
38
|
+
* @remarks
|
|
39
|
+
* Request-time callers use this after a custom 500 page itself fails so the
|
|
40
|
+
* fallback cannot re-enter the custom source.
|
|
41
|
+
*/
|
|
42
|
+
renderBuiltIn(input: ErrorPageRenderInput): ErrorPageRenderResult;
|
|
43
|
+
/**
|
|
44
|
+
* Returns the source file used for incremental static-export reuse, without
|
|
45
|
+
* rendering the page.
|
|
46
|
+
*/
|
|
47
|
+
resolveSourceFile(kind: ErrorPageKind): Promise<string | undefined>;
|
|
48
|
+
private resolveCustomSource;
|
|
49
|
+
private renderFileSystemPage;
|
|
50
|
+
private renderRegisteredPage;
|
|
51
|
+
private getTemplatePath;
|
|
52
|
+
private getCustomPageProps;
|
|
53
|
+
private resolveViewSourceFile;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Logs a custom error-page failure without replacing the original request error.
|
|
57
|
+
*/
|
|
58
|
+
export declare function logErrorPageFailure(kind: ErrorPageKind | undefined, error: unknown): void;
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { fileSystem } from '@ecopages/file-system';
|
|
3
|
+
import { appLogger } from '../../global/app-logger.js';
|
|
4
|
+
import { prepareExplicitStaticRender } from '../../route-renderer/explicit-view-render-preparation.js';
|
|
5
|
+
import { ERROR_PAGE_STATUS_BY_KIND, kindForStatus } from '../../errors/http-error-page-contract.js';
|
|
6
|
+
import { buildDefaultErrorHtml, getDefaultServerErrorDetails, getPublicErrorMessage, } from './default-error-pages.js';
|
|
7
|
+
const ERROR_PAGE_RENDER_ERRORS = {
|
|
8
|
+
missingIntegration: (routePath) => `View at ${routePath} is missing component identity integration. Ensure it's defined with eco.page() and exported as default.`,
|
|
9
|
+
noRendererForIntegration: (integrationName) => `No renderer found for integration: ${integrationName}`,
|
|
10
|
+
};
|
|
11
|
+
function resolveRenderTarget(input) {
|
|
12
|
+
const status = input.status ?? (input.kind ? ERROR_PAGE_STATUS_BY_KIND[input.kind] : 500);
|
|
13
|
+
return {
|
|
14
|
+
status,
|
|
15
|
+
kind: input.kind ?? kindForStatus(status),
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Owns error-page source precedence and rendering for runtime and static paths.
|
|
20
|
+
*
|
|
21
|
+
* @remarks
|
|
22
|
+
* Filesystem semantic pages win over explicitly registered views. A missing
|
|
23
|
+
* custom source is the only case that uses a framework default; rendering
|
|
24
|
+
* failures are intentionally propagated so callers can apply their own retry
|
|
25
|
+
* or fallback policy without hiding the original failure.
|
|
26
|
+
*/
|
|
27
|
+
export class ErrorPageRenderer {
|
|
28
|
+
appConfig;
|
|
29
|
+
routeRendererFactory;
|
|
30
|
+
loaders;
|
|
31
|
+
constructor({ appConfig, routeRendererFactory, errorPageLoaders = {}, }) {
|
|
32
|
+
this.appConfig = appConfig;
|
|
33
|
+
this.routeRendererFactory = routeRendererFactory;
|
|
34
|
+
this.loaders = errorPageLoaders;
|
|
35
|
+
}
|
|
36
|
+
async render(input) {
|
|
37
|
+
const { status, kind } = resolveRenderTarget(input);
|
|
38
|
+
const source = kind ? this.resolveCustomSource(kind) : undefined;
|
|
39
|
+
const props = this.getCustomPageProps(status, input.error);
|
|
40
|
+
if (source?.kind === 'filesystem') {
|
|
41
|
+
return await this.renderFileSystemPage(source.templatePath, props);
|
|
42
|
+
}
|
|
43
|
+
if (source?.kind === 'loader') {
|
|
44
|
+
return await this.renderRegisteredPage(source.loader, status, props);
|
|
45
|
+
}
|
|
46
|
+
return this.renderBuiltIn(input);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Renders the framework-owned document, ignoring custom filesystem pages and
|
|
50
|
+
* registered views.
|
|
51
|
+
*
|
|
52
|
+
* @remarks
|
|
53
|
+
* Request-time callers use this after a custom 500 page itself fails so the
|
|
54
|
+
* fallback cannot re-enter the custom source.
|
|
55
|
+
*/
|
|
56
|
+
renderBuiltIn(input) {
|
|
57
|
+
const { status } = resolveRenderTarget(input);
|
|
58
|
+
return {
|
|
59
|
+
body: buildDefaultErrorHtml(status, this.getCustomPageProps(status, input.error)),
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Returns the source file used for incremental static-export reuse, without
|
|
64
|
+
* rendering the page.
|
|
65
|
+
*/
|
|
66
|
+
async resolveSourceFile(kind) {
|
|
67
|
+
const source = this.resolveCustomSource(kind);
|
|
68
|
+
if (source?.kind === 'filesystem') {
|
|
69
|
+
return source.templatePath;
|
|
70
|
+
}
|
|
71
|
+
if (source?.kind === 'loader') {
|
|
72
|
+
const view = (await source.loader()).default;
|
|
73
|
+
return this.resolveViewSourceFile(view.config?.identity?.file);
|
|
74
|
+
}
|
|
75
|
+
return undefined;
|
|
76
|
+
}
|
|
77
|
+
resolveCustomSource(kind) {
|
|
78
|
+
const templatePath = this.getTemplatePath(kind);
|
|
79
|
+
if (templatePath && fileSystem.exists(templatePath)) {
|
|
80
|
+
return { kind: 'filesystem', templatePath };
|
|
81
|
+
}
|
|
82
|
+
const loader = this.loaders[kind];
|
|
83
|
+
if (loader) {
|
|
84
|
+
return { kind: 'loader', loader };
|
|
85
|
+
}
|
|
86
|
+
return undefined;
|
|
87
|
+
}
|
|
88
|
+
async renderFileSystemPage(templatePath, props) {
|
|
89
|
+
if (!this.routeRendererFactory) {
|
|
90
|
+
throw new Error(`No route renderer available for ${templatePath}`);
|
|
91
|
+
}
|
|
92
|
+
const result = await this.routeRendererFactory.getPageRenderer(templatePath).execute({
|
|
93
|
+
file: templatePath,
|
|
94
|
+
props,
|
|
95
|
+
locals: {},
|
|
96
|
+
});
|
|
97
|
+
return { body: result.body, sourceFile: templatePath };
|
|
98
|
+
}
|
|
99
|
+
async renderRegisteredPage(loader, status, props) {
|
|
100
|
+
if (!this.routeRendererFactory) {
|
|
101
|
+
throw new Error(`No route renderer available for __error__/${status}`);
|
|
102
|
+
}
|
|
103
|
+
const view = (await loader()).default;
|
|
104
|
+
const { renderer, props: staticProps, view: renderableView, } = await prepareExplicitStaticRender({
|
|
105
|
+
routePath: `__error__/${status}`,
|
|
106
|
+
view,
|
|
107
|
+
params: {},
|
|
108
|
+
appConfig: this.appConfig,
|
|
109
|
+
runtimeOrigin: this.appConfig.baseUrl,
|
|
110
|
+
routeRendererFactory: this.routeRendererFactory,
|
|
111
|
+
errors: ERROR_PAGE_RENDER_ERRORS,
|
|
112
|
+
});
|
|
113
|
+
const response = await renderer.renderToResponse(renderableView, { ...staticProps, ...props }, { status });
|
|
114
|
+
return {
|
|
115
|
+
body: await response.text(),
|
|
116
|
+
sourceFile: this.resolveViewSourceFile(view.config?.identity?.file),
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
getTemplatePath(kind) {
|
|
120
|
+
const status = ERROR_PAGE_STATUS_BY_KIND[kind];
|
|
121
|
+
return (this.appConfig.absolutePaths.errorPageTemplatePaths?.[status] ||
|
|
122
|
+
(kind === 'notFound' ? this.appConfig.absolutePaths.error404TemplatePath : '') ||
|
|
123
|
+
(kind === 'serverError' ? this.appConfig.absolutePaths.error500TemplatePath : '') ||
|
|
124
|
+
'');
|
|
125
|
+
}
|
|
126
|
+
getCustomPageProps(status, error) {
|
|
127
|
+
if (status >= 500) {
|
|
128
|
+
return { status, ...getDefaultServerErrorDetails(error) };
|
|
129
|
+
}
|
|
130
|
+
const message = getPublicErrorMessage(error);
|
|
131
|
+
return message ? { status, message } : { status };
|
|
132
|
+
}
|
|
133
|
+
resolveViewSourceFile(sourceFile) {
|
|
134
|
+
if (!sourceFile) {
|
|
135
|
+
return undefined;
|
|
136
|
+
}
|
|
137
|
+
return path.isAbsolute(sourceFile) ? sourceFile : path.join(this.appConfig.rootDir, sourceFile);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Logs a custom error-page failure without replacing the original request error.
|
|
142
|
+
*/
|
|
143
|
+
export function logErrorPageFailure(kind, error) {
|
|
144
|
+
const status = kind ? ERROR_PAGE_STATUS_BY_KIND[kind] : 500;
|
|
145
|
+
appLogger.error(`Custom ${status} page failed`, error);
|
|
146
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { SEMANTIC_ERROR_PAGE_PATHNAMES } from '../../errors/http-error-page-contract.js';
|
|
2
|
+
import { ErrorPageRenderer } from './error-page-renderer.js';
|
|
3
|
+
type StaticArtifactWriter = (input: {
|
|
4
|
+
pathname: string;
|
|
5
|
+
sourceFile?: string;
|
|
6
|
+
createContents: () => Promise<string | Buffer>;
|
|
7
|
+
activeStaticPathnames: Set<string>;
|
|
8
|
+
}) => Promise<void>;
|
|
9
|
+
/**
|
|
10
|
+
* Emits semantic error-page artifacts using the same renderer as requests.
|
|
11
|
+
*/
|
|
12
|
+
export declare class SemanticErrorPageExporter {
|
|
13
|
+
private readonly errorPageRenderer;
|
|
14
|
+
private readonly writeArtifact;
|
|
15
|
+
constructor({ errorPageRenderer, writeArtifact, }: {
|
|
16
|
+
errorPageRenderer: ErrorPageRenderer;
|
|
17
|
+
writeArtifact: StaticArtifactWriter;
|
|
18
|
+
});
|
|
19
|
+
export(activeStaticPathnames: Set<string>): Promise<void>;
|
|
20
|
+
private exportOne;
|
|
21
|
+
private bodyToStaticContents;
|
|
22
|
+
}
|
|
23
|
+
export { SEMANTIC_ERROR_PAGE_PATHNAMES };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { ERROR_PAGE_KIND_BY_STATUS, HTTP_ERROR_PAGE_STATUSES, SEMANTIC_ERROR_PAGE_PATHNAMES, } from '../../errors/http-error-page-contract.js';
|
|
2
|
+
import { ErrorPageRenderer } from './error-page-renderer.js';
|
|
3
|
+
/**
|
|
4
|
+
* Emits semantic error-page artifacts using the same renderer as requests.
|
|
5
|
+
*/
|
|
6
|
+
export class SemanticErrorPageExporter {
|
|
7
|
+
errorPageRenderer;
|
|
8
|
+
writeArtifact;
|
|
9
|
+
constructor({ errorPageRenderer, writeArtifact, }) {
|
|
10
|
+
this.errorPageRenderer = errorPageRenderer;
|
|
11
|
+
this.writeArtifact = writeArtifact;
|
|
12
|
+
}
|
|
13
|
+
async export(activeStaticPathnames) {
|
|
14
|
+
await Promise.all(HTTP_ERROR_PAGE_STATUSES.map((status) => this.exportOne(ERROR_PAGE_KIND_BY_STATUS[status], `/${status}`, activeStaticPathnames)));
|
|
15
|
+
}
|
|
16
|
+
async exportOne(kind, pathname, activeStaticPathnames) {
|
|
17
|
+
if (activeStaticPathnames.has(pathname)) {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
const sourceFile = await this.errorPageRenderer.resolveSourceFile(kind);
|
|
21
|
+
await this.writeArtifact({
|
|
22
|
+
pathname,
|
|
23
|
+
sourceFile,
|
|
24
|
+
activeStaticPathnames,
|
|
25
|
+
createContents: async () => this.bodyToStaticContents((await this.errorPageRenderer.render({ kind })).body),
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
async bodyToStaticContents(body) {
|
|
29
|
+
if (typeof body === 'string' || Buffer.isBuffer(body)) {
|
|
30
|
+
return body;
|
|
31
|
+
}
|
|
32
|
+
return await new Response(body).text();
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
export { SEMANTIC_ERROR_PAGE_PATHNAMES };
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
export type HtmlContentOptions = {
|
|
2
|
+
/** Insert `content` as raw markup. Otherwise it is inserted as escaped text. */
|
|
3
|
+
html?: boolean;
|
|
4
|
+
};
|
|
5
|
+
/**
|
|
6
|
+
* Element passed to {@link HtmlRewriterElementHandlers.element}; a subset of Bun's
|
|
7
|
+
* `HTMLRewriter` element API.
|
|
8
|
+
*/
|
|
9
|
+
export interface HtmlRewriterElement {
|
|
10
|
+
/** Lowercased tag name. */
|
|
11
|
+
readonly tagName: string;
|
|
12
|
+
/** Attribute name/value pairs, names lowercased. */
|
|
13
|
+
readonly attributes: IterableIterator<[string, string]>;
|
|
14
|
+
readonly selfClosing: boolean;
|
|
15
|
+
/** `false` for void elements (and self-closing foreign elements), which ignore `prepend`/`append`. */
|
|
16
|
+
readonly canHaveContent: boolean;
|
|
17
|
+
getAttribute(name: string): string | null;
|
|
18
|
+
hasAttribute(name: string): boolean;
|
|
19
|
+
setAttribute(name: string, value: string): HtmlRewriterElement;
|
|
20
|
+
removeAttribute(name: string): HtmlRewriterElement;
|
|
21
|
+
before(content: string, options?: HtmlContentOptions): HtmlRewriterElement;
|
|
22
|
+
/** Inserts right after the start tag; repeated calls insert in reverse call order. */
|
|
23
|
+
prepend(content: string, options?: HtmlContentOptions): HtmlRewriterElement;
|
|
24
|
+
/** Inserts right before the end tag; dropped when the element never closes. */
|
|
25
|
+
append(content: string, options?: HtmlContentOptions): HtmlRewriterElement;
|
|
26
|
+
/** Inserts after the end tag in reverse call order; dropped when the element never closes. */
|
|
27
|
+
after(content: string, options?: HtmlContentOptions): HtmlRewriterElement;
|
|
28
|
+
}
|
|
29
|
+
export interface HtmlRewriterElementHandlers {
|
|
30
|
+
element?(element: HtmlRewriterElement): void;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Streaming HTML rewriter implementing the subset of Bun's `HTMLRewriter` API that
|
|
34
|
+
* Ecopages uses, so the same rewrite runs on Bun and Node without a wasm build of
|
|
35
|
+
* lol-html.
|
|
36
|
+
*
|
|
37
|
+
* @remarks
|
|
38
|
+
* Supported selectors are tag names and `*`; anything else throws so an
|
|
39
|
+
* unsupported selector never silently matches nothing. Handlers are synchronous.
|
|
40
|
+
*
|
|
41
|
+
* Output follows lol-html (checked against Bun's native `HTMLRewriter`) on the
|
|
42
|
+
* behaviour Ecopages relies on: comments, doctypes and the content of raw-text
|
|
43
|
+
* elements (`script` with its escape states, `style`, `textarea`, `title`,
|
|
44
|
+
* `noscript`, `iframe`, `xmp`, `noembed`, `noframes`, `plaintext`) never produce
|
|
45
|
+
* tags; `>` inside quoted attribute values does not end a tag; tag matching is
|
|
46
|
+
* case-insensitive; untouched attributes keep their original text.
|
|
47
|
+
*
|
|
48
|
+
* There is no tree construction. An end tag closes the nearest open element with
|
|
49
|
+
* the same name, and `append`/`after` content of elements that it closes
|
|
50
|
+
* implicitly, or that are still open at the end of the document, is dropped.
|
|
51
|
+
*/
|
|
52
|
+
export declare class HtmlRewriter {
|
|
53
|
+
private readonly handlers;
|
|
54
|
+
on(selector: string, handlers: HtmlRewriterElementHandlers): this;
|
|
55
|
+
transform(input: string): string;
|
|
56
|
+
transform(input: Response): Response;
|
|
57
|
+
}
|
|
58
|
+
/** Attribute access on the element passed to {@link rewriteFirstElement}. */
|
|
59
|
+
export type HtmlRewriterAttributeTarget = Pick<HtmlRewriterElement, 'tagName' | 'attributes' | 'getAttribute' | 'hasAttribute' | 'setAttribute' | 'removeAttribute'>;
|
|
60
|
+
/**
|
|
61
|
+
* Rewrites the attributes of the first start tag that `isTarget` accepts and
|
|
62
|
+
* returns the rest of `html` unchanged.
|
|
63
|
+
*
|
|
64
|
+
* @remarks
|
|
65
|
+
* Uses the same tokenizer as {@link HtmlRewriter}, so tags inside comments, raw
|
|
66
|
+
* text and quoted attribute values never match. Scanning stops once the target
|
|
67
|
+
* is rewritten, so the cost grows with the markup before the target rather than
|
|
68
|
+
* with the whole document. Only attribute changes are offered: content placed
|
|
69
|
+
* with `append` or `after` needs the element's end tag, which is never reached.
|
|
70
|
+
*
|
|
71
|
+
* With `leadingOnly`, the target must directly follow the previous start tag
|
|
72
|
+
* (or the start of `html`), whitespace aside. When text, a comment or an end tag
|
|
73
|
+
* comes first, scanning stops and nothing is rewritten.
|
|
74
|
+
*/
|
|
75
|
+
export declare function rewriteFirstElement(html: string, isTarget: (element: HtmlRewriterAttributeTarget) => boolean, rewrite: (element: HtmlRewriterAttributeTarget) => void, options?: {
|
|
76
|
+
leadingOnly?: boolean;
|
|
77
|
+
}): string;
|