@ecopages/core 0.2.0-rc.9 → 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.
Files changed (211) hide show
  1. package/README.md +39 -38
  2. package/package.json +22 -152
  3. package/src/adapters/README.md +11 -3
  4. package/src/adapters/abstract/application-adapter.d.ts +32 -36
  5. package/src/adapters/abstract/application-adapter.js +61 -47
  6. package/src/adapters/abstract/server-adapter.d.ts +14 -0
  7. package/src/adapters/abstract/server-adapter.js +10 -0
  8. package/src/adapters/abstract/ws-pattern-matcher.d.ts +0 -5
  9. package/src/adapters/abstract/ws-pattern-matcher.js +0 -9
  10. package/src/adapters/bun/create-app.d.ts +12 -16
  11. package/src/adapters/bun/create-app.js +25 -55
  12. package/src/adapters/bun/hmr-manager.d.ts +0 -1
  13. package/src/adapters/bun/hmr-manager.js +2 -4
  14. package/src/adapters/bun/server-adapter.d.ts +15 -21
  15. package/src/adapters/bun/server-adapter.js +33 -67
  16. package/src/adapters/bun/static-preview-host.d.ts +1 -0
  17. package/src/adapters/bun/static-preview-host.js +1 -1
  18. package/src/adapters/create-app.d.ts +63 -8
  19. package/src/adapters/create-app.js +62 -5
  20. package/src/adapters/node/create-app.d.ts +8 -12
  21. package/src/adapters/node/create-app.js +22 -14
  22. package/src/adapters/node/runtime-host.d.ts +10 -10
  23. package/src/adapters/node/runtime-host.js +13 -8
  24. package/src/adapters/node/server-adapter-dependencies.d.ts +2 -10
  25. package/src/adapters/node/server-adapter-dependencies.js +8 -13
  26. package/src/adapters/node/server-adapter.d.ts +30 -32
  27. package/src/adapters/node/server-adapter.js +59 -65
  28. package/src/adapters/shared/hmr/shared-hmr-manager.d.ts +0 -4
  29. package/src/adapters/shared/hmr/shared-hmr-manager.js +0 -12
  30. package/src/adapters/shared/http/api-request-pipeline.d.ts +27 -1
  31. package/src/adapters/shared/http/api-request-pipeline.js +31 -16
  32. package/src/adapters/shared/http/explicit-static-route-matcher.js +5 -2
  33. package/src/adapters/shared/http/fs-server-response-factory.d.ts +2 -9
  34. package/src/adapters/shared/http/fs-server-response-factory.js +5 -33
  35. package/src/adapters/shared/http/fs-server-response-matcher.d.ts +24 -24
  36. package/src/adapters/shared/http/fs-server-response-matcher.js +59 -83
  37. package/src/adapters/shared/runtime/application-adapter.d.ts +1 -0
  38. package/src/adapters/shared/runtime/application-adapter.js +12 -0
  39. package/src/adapters/shared/runtime/bind-runtime-server.d.ts +17 -0
  40. package/src/adapters/shared/runtime/bind-runtime-server.js +50 -0
  41. package/src/adapters/shared/runtime/collect-dev-prewarm-plan.d.ts +1 -3
  42. package/src/adapters/shared/runtime/collect-dev-prewarm-plan.js +1 -4
  43. package/src/adapters/shared/runtime/port-manager-default-prompt.d.ts +6 -0
  44. package/src/adapters/shared/runtime/port-manager-default-prompt.js +41 -0
  45. package/src/adapters/shared/runtime/port-manager.d.ts +3 -3
  46. package/src/adapters/shared/runtime/port-manager.js +14 -45
  47. package/src/adapters/shared/runtime/runtime-app-bootstrap.d.ts +0 -10
  48. package/src/adapters/shared/runtime/runtime-app-bootstrap.js +0 -7
  49. package/src/adapters/shared/runtime/runtime-host.d.ts +5 -2
  50. package/src/adapters/shared/runtime/runtime-server-lifecycle.d.ts +1 -1
  51. package/src/adapters/shared/runtime/runtime-server-lifecycle.js +1 -1
  52. package/src/adapters/shared/runtime/server-adapter.d.ts +26 -5
  53. package/src/adapters/shared/runtime/server-adapter.js +35 -8
  54. package/src/adapters/shared/runtime/server-route-handler.js +11 -3
  55. package/src/adapters/shared/runtime/server-static-builder.d.ts +9 -3
  56. package/src/adapters/shared/runtime/server-static-builder.js +59 -37
  57. package/src/adapters/shared/ws/node-http-websocket-upgrades.d.ts +9 -1
  58. package/src/adapters/shared/ws/node-http-websocket-upgrades.js +8 -2
  59. package/src/build/README.md +12 -8
  60. package/src/build/cache/cache-constants.d.ts +0 -2
  61. package/src/build/cache/cache-constants.js +0 -2
  62. package/src/build/cache/cache-keys.d.ts +0 -2
  63. package/src/build/cache/cache-keys.js +0 -2
  64. package/src/build/cache/output-imports.d.ts +35 -0
  65. package/src/build/cache/output-imports.js +87 -0
  66. package/src/build/cache/pages-unified-graph-build.d.ts +15 -1
  67. package/src/build/cache/pages-unified-graph-build.js +73 -22
  68. package/src/build/cache/production-build-cache.d.ts +2 -2
  69. package/src/build/cache/production-build-cache.js +2 -2
  70. package/src/build/cache/server-entry-build-cache.d.ts +25 -3
  71. package/src/build/cache/server-entry-build-cache.js +129 -13
  72. package/src/build/contracts/build-contracts.d.ts +1 -13
  73. package/src/build/rolldown/runtime-build-output-normalizer.js +3 -6
  74. package/src/build/runtime/build-request-identity.d.ts +2 -6
  75. package/src/build/runtime/build-request-identity.js +2 -8
  76. package/src/build/runtime/build-request-policy.js +1 -1
  77. package/src/build/runtime/deduping-build-executor.d.ts +0 -1
  78. package/src/build/runtime/deduping-build-executor.js +0 -1
  79. package/src/build/server-bundle-publication.d.ts +14 -0
  80. package/src/build/server-bundle-publication.js +49 -0
  81. package/src/cache/index.d.ts +1 -1
  82. package/src/cache/index.js +1 -1
  83. package/src/cache/module-parse-cache.d.ts +2 -29
  84. package/src/cache/module-parse-cache.js +6 -39
  85. package/src/config/README.md +17 -2
  86. package/src/config/apply-user-config.d.ts +8 -0
  87. package/src/config/apply-user-config.js +68 -0
  88. package/src/config/config-builder.d.ts +11 -1
  89. package/src/config/config-builder.js +53 -12
  90. package/src/config/constants.d.ts +4 -4
  91. package/src/config/constants.js +4 -4
  92. package/src/config/define-config.d.ts +9 -0
  93. package/src/config/define-config.js +10 -0
  94. package/src/config/index.d.ts +5 -0
  95. package/src/config/index.js +4 -0
  96. package/src/config/is-finalized-app-config.d.ts +6 -0
  97. package/src/config/is-finalized-app-config.js +11 -0
  98. package/src/config/load-eco-config.d.ts +21 -0
  99. package/src/config/load-eco-config.js +107 -0
  100. package/src/config/resolve-eco-config-path.d.ts +32 -0
  101. package/src/config/resolve-eco-config-path.js +58 -0
  102. package/src/config/server-config-bundle.d.ts +25 -0
  103. package/src/config/server-config-bundle.js +148 -0
  104. package/src/config/user-config-types.d.ts +170 -0
  105. package/src/config/user-config-types.js +1 -0
  106. package/src/dev/README.md +4 -0
  107. package/src/dev/development-restart-watch-paths.d.ts +13 -0
  108. package/src/dev/development-restart-watch-paths.js +37 -0
  109. package/src/dev/development-restart.d.ts +13 -0
  110. package/src/dev/development-restart.js +21 -0
  111. package/src/dev/host-runtime.d.ts +0 -1
  112. package/src/dev/host-runtime.js +0 -3
  113. package/src/dev/sc-server.d.ts +4 -1
  114. package/src/dev/sc-server.js +11 -20
  115. package/src/dev/transform-server/dev-transform-import-rewriter.js +2 -2
  116. package/src/eco/eco.js +4 -0
  117. package/src/errors/http-error-page-contract.d.ts +77 -0
  118. package/src/errors/http-error-page-contract.js +106 -0
  119. package/src/hmr/hmr-file-change-prep.d.ts +0 -1
  120. package/src/hmr/hmr-file-change-prep.js +2 -5
  121. package/src/hmr/hmr-strategy.d.ts +3 -8
  122. package/src/hmr/hmr-strategy.js +3 -8
  123. package/src/index.browser.d.ts +1 -2
  124. package/src/index.browser.js +1 -2
  125. package/src/index.d.ts +5 -6
  126. package/src/index.js +5 -6
  127. package/src/islands/island-host.d.ts +0 -9
  128. package/src/islands/island-host.js +1 -13
  129. package/src/plugins/component-import-discovery.js +2 -2
  130. package/src/plugins/eco-component-meta-plugin.js +5 -5
  131. package/src/plugins/integration-plugin.d.ts +0 -1
  132. package/src/plugins/integration-plugin.js +0 -4
  133. package/src/plugins/processor.d.ts +6 -2
  134. package/src/route-renderer/README.md +4 -0
  135. package/src/{adapters/shared/http/explicit-static-render-preparation.d.ts → route-renderer/explicit-view-render-preparation.d.ts} +3 -3
  136. package/src/route-renderer/orchestration/document-shell/document-shell-render.service.js +1 -4
  137. package/src/route-renderer/orchestration/foreign-child/foreign-subtree-execution.service.d.ts +25 -24
  138. package/src/route-renderer/orchestration/foreign-child/foreign-subtree-execution.service.js +18 -20
  139. package/src/route-renderer/orchestration/integration-renderer.d.ts +24 -7
  140. package/src/route-renderer/orchestration/integration-renderer.js +44 -33
  141. package/src/route-renderer/orchestration/integration-renderer.test-fixtures.js +1 -1
  142. package/src/route-renderer/orchestration/page-browser-graph/page-browser-graph-session.d.ts +1 -1
  143. package/src/route-renderer/orchestration/page-browser-graph/page-browser-graph-session.js +1 -1
  144. package/src/route-renderer/route-renderer.d.ts +9 -0
  145. package/src/route-renderer/route-renderer.js +18 -0
  146. package/src/services/README.md +2 -1
  147. package/src/services/assets/browser-bundle.service.d.ts +1 -6
  148. package/src/services/error-pages/README.md +9 -0
  149. package/src/services/error-pages/default-error-pages.d.ts +39 -0
  150. package/src/services/error-pages/default-error-pages.js +199 -0
  151. package/src/services/error-pages/error-page-renderer.d.ts +58 -0
  152. package/src/services/error-pages/error-page-renderer.js +146 -0
  153. package/src/services/error-pages/semantic-error-page-exporter.d.ts +23 -0
  154. package/src/services/error-pages/semantic-error-page-exporter.js +35 -0
  155. package/src/services/html/html-rewriter.d.ts +77 -0
  156. package/src/services/html/html-rewriter.js +751 -0
  157. package/src/services/html/html-rewriter.parity.test.bun.d.ts +1 -0
  158. package/src/services/html/html-rewriter.parity.test.bun.js +74 -0
  159. package/src/services/html/html-transformer.service.d.ts +29 -42
  160. package/src/services/html/html-transformer.service.js +85 -157
  161. package/src/services/invalidation/development-invalidation.service.d.ts +9 -20
  162. package/src/services/invalidation/development-invalidation.service.js +26 -39
  163. package/src/services/module-loading/README.md +20 -15
  164. package/src/services/module-loading/app-module-loader.service.d.ts +0 -2
  165. package/src/services/module-loading/app-server-module-transpiler.service.d.ts +1 -1
  166. package/src/services/module-loading/app-server-module-transpiler.service.js +2 -10
  167. package/src/services/module-loading/collection-server-module-build.service.d.ts +0 -1
  168. package/src/services/module-loading/collection-server-module-build.service.js +0 -3
  169. package/src/services/module-loading/host-module-loader-registry.d.ts +0 -1
  170. package/src/services/module-loading/host-module-loader-registry.js +0 -3
  171. package/src/services/module-loading/route-module-build-cache-registry.d.ts +0 -4
  172. package/src/services/module-loading/route-module-build-cache-registry.js +0 -4
  173. package/src/services/module-loading/route-module-build-cache.store.d.ts +8 -0
  174. package/src/services/module-loading/route-module-build-cache.store.js +25 -7
  175. package/src/services/module-loading/route-module-build-manifest.d.ts +12 -1
  176. package/src/services/module-loading/route-module-build-manifest.js +7 -3
  177. package/src/services/module-loading/route-module-dependency-hasher.d.ts +3 -2
  178. package/src/services/module-loading/route-module-dependency-hasher.js +13 -1
  179. package/src/services/module-loading/server-module-transpiler.service.d.ts +1 -15
  180. package/src/services/module-loading/server-module-transpiler.service.js +0 -24
  181. package/src/services/module-loading/view-module-loader.d.ts +10 -0
  182. package/src/services/module-loading/view-module-loader.js +46 -0
  183. package/src/services/runtime-state/entrypoint-dependency-graph.service.js +0 -12
  184. package/src/services/runtime-state/server-invalidation-state.service.d.ts +4 -2
  185. package/src/services/runtime-state/server-invalidation-state.service.js +7 -14
  186. package/src/static-site-generator/README.md +12 -8
  187. package/src/static-site-generator/static-site-generator.d.ts +3 -2
  188. package/src/static-site-generator/static-site-generator.js +17 -3
  189. package/src/types/internal-types.d.ts +1 -38
  190. package/src/types/public-types.d.ts +28 -130
  191. package/src/watchers/project-watcher.d.ts +9 -16
  192. package/src/watchers/project-watcher.js +35 -28
  193. package/src/watchers/project-watcher.test-helpers.d.ts +10 -1
  194. package/src/watchers/project-watcher.test-helpers.js +12 -0
  195. package/src/adapters/abstract/router-adapter.d.ts +0 -26
  196. package/src/adapters/abstract/router-adapter.js +0 -13
  197. package/src/adapters/bun/index.d.ts +0 -2
  198. package/src/adapters/bun/index.js +0 -2
  199. package/src/adapters/index.d.ts +0 -2
  200. package/src/adapters/index.js +0 -2
  201. package/src/eco/eco.utils.d.ts +0 -1
  202. package/src/eco/eco.utils.js +0 -1
  203. package/src/services/cache/index.d.ts +0 -9
  204. package/src/services/cache/index.js +0 -8
  205. package/src/services/html/html-rewriter-provider.service.d.ts +0 -40
  206. package/src/services/html/html-rewriter-provider.service.js +0 -68
  207. package/src/services/module-loading/route-module-build-cache.d.ts +0 -4
  208. package/src/services/module-loading/route-module-build-cache.js +0 -3
  209. package/src/services/runtime-state/dev-graph.service.d.ts +0 -118
  210. package/src/services/runtime-state/dev-graph.service.js +0 -170
  211. /package/src/{adapters/shared/http/explicit-static-render-preparation.js → route-renderer/explicit-view-render-preparation.js} +0 -0
@@ -8,8 +8,9 @@
8
8
  */
9
9
  import type { SourceModuleLoader } from '../../services/module-loading/module-loading-types.js';
10
10
  import type { EcoPagesAppConfig } from '../../types/internal-types.js';
11
- import type { ApiHandler, ApiHandlerContext, ErrorHandler, Middleware, RouteOptions, StaticRoute, ViewLoader, EcopagesRouteInfo } from '../../types/public-types.js';
11
+ import type { ApiHandler, ApiHandlerContext, ErrorHandler, Middleware, RouteOptions, StaticRoute, ViewLoader, ErrorPageLoaders, ErrorPageTemplateProps, EcopagesRouteInfo } from '../../types/public-types.js';
12
12
  import type { EcopagesWebSocketHandler } from '../../types/public-types.js';
13
+ import { type HttpErrorPageStatus } from '../../errors/http-error-page-contract.js';
13
14
  import { type EcopagesRuntimeLabel } from '../../dev/runtime-server-started-message.js';
14
15
  import { type ReturnParseCliArgs } from '../../utils/parse-cli-args.js';
15
16
  /**
@@ -55,14 +56,6 @@ export interface AppStartInfo {
55
56
  routes: EcopagesRouteInfo[];
56
57
  }
57
58
  export type OnAppStartCallback = (info: AppStartInfo) => void;
58
- /** @deprecated Use {@link AppStartInfo}. */
59
- export type ApplicationListeningInfo = AppStartInfo;
60
- /** @deprecated Use {@link OnAppStartCallback}. */
61
- export type StartCallback = OnAppStartCallback;
62
- /** @deprecated Use {@link OnAppStartCallback}. */
63
- export type ListenCallback = OnAppStartCallback;
64
- /** @deprecated Use {@link OnAppStartCallback}. */
65
- export type ApplicationListeningCallback = OnAppStartCallback;
66
59
  /**
67
60
  * Configuration options for application adapters
68
61
  */
@@ -70,11 +63,6 @@ export interface ApplicationAdapterOptions {
70
63
  appConfig: EcoPagesAppConfig;
71
64
  serverOptions?: Record<string, any>;
72
65
  runtime?: ApplicationRuntimeOptions;
73
- /**
74
- * Options for clearing the output directory before starting the server
75
- * @default false
76
- */
77
- clearOutput?: boolean;
78
66
  }
79
67
  /**
80
68
  * Common interface for application adapters
@@ -105,6 +93,7 @@ export declare abstract class AbstractApplicationAdapter<TOptions extends Applic
105
93
  protected runtimeOptions: ApplicationRuntimeOptions;
106
94
  protected apiHandlers: ApiHandler[];
107
95
  protected staticRoutes: StaticRoute[];
96
+ protected errorPageLoaders: ErrorPageLoaders;
108
97
  protected errorHandler?: ErrorHandler;
109
98
  /**
110
99
  * App-level WebSocket handlers keyed by URL path pattern (e.g. '/ws/chat/:id').
@@ -114,7 +103,6 @@ export declare abstract class AbstractApplicationAdapter<TOptions extends Applic
114
103
  private onAppStartCallback?;
115
104
  protected readonly runtimeLabel: EcopagesRuntimeLabel;
116
105
  constructor(options: TOptions, runtimeLabel: EcopagesRuntimeLabel);
117
- private clearDistFolder;
118
106
  /**
119
107
  * Register a GET route handler.
120
108
  *
@@ -178,29 +166,41 @@ export declare abstract class AbstractApplicationAdapter<TOptions extends Applic
178
166
  middleware?: readonly Middleware<TRequest, TServer, any>[];
179
167
  }): this;
180
168
  abstract group(group: RouteGroupDefinition<TRequest, TServer>): this;
181
- /**
182
- * Get all registered API handlers
183
- */
184
- getApiHandlers(): ApiHandler[];
185
169
  /**
186
170
  * Register a view for static generation at build time.
187
171
  * The view must have staticPaths defined for dynamic routes.
188
172
  *
189
- * Uses a loader function to enable HMR in development.
173
+ * String and URL inputs load through the server-module transpiler so HMR and
174
+ * component identity transforms remain active in development.
190
175
  *
191
176
  * @param path - URL path pattern (e.g., '/posts/:slug')
192
- * @param loader - A function that dynamically imports the eco.page view module
177
+ * @param loader - A view loader function, source path, or URL for the eco.page view module
193
178
  * @example
194
179
  * ```typescript
195
- * app.static('/login', () => import('./src/views/login.kita'))
196
- * app.static('/posts/:slug', () => import('./src/views/post-view.kita'))
180
+ * app.static('/login', './src/views/login.kita')
181
+ * app.static('/posts/:slug', './src/views/post-view.kita')
197
182
  * ```
198
183
  */
199
- static<P>(path: string, loader: ViewLoader<P>): this;
184
+ static<P>(path: string, loader: ViewLoader<P> | string | URL): this;
185
+ private resolveViewLoader;
186
+ /**
187
+ * Registers a semantic HTML error page used when no filesystem `pages/{status}.*`
188
+ * file exists.
189
+ */
190
+ errorPage(status: HttpErrorPageStatus, loader: ViewLoader<ErrorPageTemplateProps> | string | URL): this;
191
+ badRequest(loader: ViewLoader<ErrorPageTemplateProps> | string | URL): this;
192
+ unauthorized(loader: ViewLoader<ErrorPageTemplateProps> | string | URL): this;
193
+ forbidden(loader: ViewLoader<ErrorPageTemplateProps> | string | URL): this;
194
+ /**
195
+ * Registers the not-found page used when no filesystem `pages/404.*` file exists.
196
+ */
197
+ notFound(loader: ViewLoader<ErrorPageTemplateProps> | string | URL): this;
198
+ conflict(loader: ViewLoader<ErrorPageTemplateProps> | string | URL): this;
200
199
  /**
201
- * Get all registered static routes
200
+ * Registers the server-error page used when no filesystem `pages/500.*` file exists.
202
201
  */
203
- getStaticRoutes(): StaticRoute[];
202
+ serverError(loader: ViewLoader<ErrorPageTemplateProps> | string | URL): this;
203
+ getErrorPageLoaders(): ErrorPageLoaders;
204
204
  /**
205
205
  * Register a WebSocket handler for the given path pattern.
206
206
  *
@@ -235,16 +235,16 @@ export declare abstract class AbstractApplicationAdapter<TOptions extends Applic
235
235
  * ```
236
236
  */
237
237
  websocket<TContext = unknown, TParams extends Record<string, string> = Record<string, string>>(path: string, handler: EcopagesWebSocketHandler<TContext, TParams>): this;
238
- /**
239
- * Get the registered WebSocket handlers map.
240
- *
241
- * @returns The map of WebSocket route patterns to handlers
242
- */
243
- getWebsocketHandlers(): Map<string, EcopagesWebSocketHandler<any, any>>;
244
238
  /**
245
239
  * Register a global error handler for all routes.
246
240
  * Useful for logging, monitoring integration, and custom error formatting.
247
241
  *
242
+ * @remarks
243
+ * It receives errors thrown by API handlers and their middleware, and errors
244
+ * that escape the request pipeline. Page render failures render the error
245
+ * page instead and do not reach it. Client disconnects answer 499 without
246
+ * calling it.
247
+ *
248
248
  * @example
249
249
  * ```typescript
250
250
  * app.onError(async (error, ctx) => {
@@ -254,10 +254,6 @@ export declare abstract class AbstractApplicationAdapter<TOptions extends Applic
254
254
  * ```
255
255
  */
256
256
  onError(handler: ErrorHandler<TRequest, TServer>): this;
257
- /**
258
- * Get the registered error handler
259
- */
260
- getErrorHandler(): ErrorHandler | undefined;
261
257
  /**
262
258
  * Initialize the server adapter based on the runtime
263
259
  */
@@ -8,12 +8,14 @@
8
8
  */
9
9
  import { appLogger } from '../../global/app-logger.js';
10
10
  import { getAppModuleLoader, setAppHostModuleLoader, } from '../../services/module-loading/app-server-module-transpiler.service.js';
11
+ import { createViewModuleLoader } from '../../services/module-loading/view-module-loader.js';
11
12
  import { getHostModuleLoader } from '../../services/module-loading/host-module-loader-registry.js';
12
13
  import { invariant } from '../../utils/invariant.js';
13
- import { fileSystem } from '@ecopages/file-system';
14
+ import { ERROR_PAGE_KIND_BY_STATUS } from '../../errors/http-error-page-contract.js';
14
15
  import { formatRuntimeServerStartedMessage, } from '../../dev/runtime-server-started-message.js';
15
16
  import { parseCliArgs } from '../../utils/parse-cli-args.js';
16
17
  import { startupTrace } from '../../diagnostics/startup-trace.js';
18
+ import { ECOPAGES_DEV_RESTART_REASON_ENV } from '../../dev/development-restart.js';
17
19
  /**
18
20
  * Abstract base class for application adapters across different runtimes
19
21
  */
@@ -24,6 +26,7 @@ export class AbstractApplicationAdapter {
24
26
  runtimeOptions;
25
27
  apiHandlers = [];
26
28
  staticRoutes = [];
29
+ errorPageLoaders = {};
27
30
  errorHandler;
28
31
  /**
29
32
  * App-level WebSocket handlers keyed by URL path pattern (e.g. '/ws/chat/:id').
@@ -50,24 +53,6 @@ export class AbstractApplicationAdapter {
50
53
  }
51
54
  getAppModuleLoader(this.appConfig);
52
55
  startupTrace.markConfigReady();
53
- if (options.clearOutput) {
54
- this.clearDistFolder().catch((error) => {
55
- appLogger.error('Error clearing dist folder', error);
56
- });
57
- }
58
- }
59
- async clearDistFolder() {
60
- const distPath = this.appConfig.absolutePaths.distDir;
61
- const distExists = fileSystem.exists(distPath);
62
- if (!distExists)
63
- return;
64
- try {
65
- await fileSystem.removeAsync(distPath);
66
- appLogger.debug(`Cleared dist folder: ${distPath}`);
67
- }
68
- catch (error) {
69
- appLogger.error(`Error clearing dist folder: ${distPath}`, error);
70
- }
71
56
  }
72
57
  /**
73
58
  * Internal method to add route handlers to the API handlers array
@@ -84,35 +69,67 @@ export class AbstractApplicationAdapter {
84
69
  });
85
70
  return this;
86
71
  }
87
- /**
88
- * Get all registered API handlers
89
- */
90
- getApiHandlers() {
91
- return this.apiHandlers;
92
- }
93
72
  /**
94
73
  * Register a view for static generation at build time.
95
74
  * The view must have staticPaths defined for dynamic routes.
96
75
  *
97
- * Uses a loader function to enable HMR in development.
76
+ * String and URL inputs load through the server-module transpiler so HMR and
77
+ * component identity transforms remain active in development.
98
78
  *
99
79
  * @param path - URL path pattern (e.g., '/posts/:slug')
100
- * @param loader - A function that dynamically imports the eco.page view module
80
+ * @param loader - A view loader function, source path, or URL for the eco.page view module
101
81
  * @example
102
82
  * ```typescript
103
- * app.static('/login', () => import('./src/views/login.kita'))
104
- * app.static('/posts/:slug', () => import('./src/views/post-view.kita'))
83
+ * app.static('/login', './src/views/login.kita')
84
+ * app.static('/posts/:slug', './src/views/post-view.kita')
105
85
  * ```
106
86
  */
107
87
  static(path, loader) {
108
- this.staticRoutes.push({ path, loader });
88
+ const resolvedLoader = this.resolveViewLoader(loader);
89
+ this.staticRoutes.push({ path, loader: resolvedLoader });
109
90
  return this;
110
91
  }
92
+ resolveViewLoader(loader) {
93
+ return typeof loader === 'string' || loader instanceof URL
94
+ ? createViewModuleLoader(this.appConfig, loader)
95
+ : loader;
96
+ }
111
97
  /**
112
- * Get all registered static routes
98
+ * Registers a semantic HTML error page used when no filesystem `pages/{status}.*`
99
+ * file exists.
113
100
  */
114
- getStaticRoutes() {
115
- return this.staticRoutes;
101
+ errorPage(status, loader) {
102
+ const kind = ERROR_PAGE_KIND_BY_STATUS[status];
103
+ const resolvedLoader = this.resolveViewLoader(loader);
104
+ this.errorPageLoaders = { ...this.errorPageLoaders, [kind]: resolvedLoader };
105
+ return this;
106
+ }
107
+ badRequest(loader) {
108
+ return this.errorPage(400, loader);
109
+ }
110
+ unauthorized(loader) {
111
+ return this.errorPage(401, loader);
112
+ }
113
+ forbidden(loader) {
114
+ return this.errorPage(403, loader);
115
+ }
116
+ /**
117
+ * Registers the not-found page used when no filesystem `pages/404.*` file exists.
118
+ */
119
+ notFound(loader) {
120
+ return this.errorPage(404, loader);
121
+ }
122
+ conflict(loader) {
123
+ return this.errorPage(409, loader);
124
+ }
125
+ /**
126
+ * Registers the server-error page used when no filesystem `pages/500.*` file exists.
127
+ */
128
+ serverError(loader) {
129
+ return this.errorPage(500, loader);
130
+ }
131
+ getErrorPageLoaders() {
132
+ return this.errorPageLoaders;
116
133
  }
117
134
  /**
118
135
  * Register a WebSocket handler for the given path pattern.
@@ -166,18 +183,16 @@ export class AbstractApplicationAdapter {
166
183
  this.websocketHandlers.set(path, handler);
167
184
  return this;
168
185
  }
169
- /**
170
- * Get the registered WebSocket handlers map.
171
- *
172
- * @returns The map of WebSocket route patterns to handlers
173
- */
174
- getWebsocketHandlers() {
175
- return this.websocketHandlers;
176
- }
177
186
  /**
178
187
  * Register a global error handler for all routes.
179
188
  * Useful for logging, monitoring integration, and custom error formatting.
180
189
  *
190
+ * @remarks
191
+ * It receives errors thrown by API handlers and their middleware, and errors
192
+ * that escape the request pipeline. Page render failures render the error
193
+ * page instead and do not reach it. Client disconnects answer 499 without
194
+ * calling it.
195
+ *
181
196
  * @example
182
197
  * ```typescript
183
198
  * app.onError(async (error, ctx) => {
@@ -190,12 +205,6 @@ export class AbstractApplicationAdapter {
190
205
  this.errorHandler = handler;
191
206
  return this;
192
207
  }
193
- /**
194
- * Get the registered error handler
195
- */
196
- getErrorHandler() {
197
- return this.errorHandler;
198
- }
199
208
  /**
200
209
  * Boot the server. When `onAppStart` is passed, it runs once the runtime can take traffic.
201
210
  * Embedded apps only register the callback — the host (for example Vite) boots the port.
@@ -221,6 +230,11 @@ export class AbstractApplicationAdapter {
221
230
  async notifyListening(origin) {
222
231
  const normalizedOrigin = origin.replace(/\/$/, '');
223
232
  startupTrace.markServerListening();
233
+ const restartReason = process.env[ECOPAGES_DEV_RESTART_REASON_ENV];
234
+ if (restartReason) {
235
+ appLogger.info(`Development server restarted (${restartReason}).`);
236
+ delete process.env[ECOPAGES_DEV_RESTART_REASON_ENV];
237
+ }
224
238
  if (!this.onAppStartCallback) {
225
239
  this.logServerStarted(normalizedOrigin);
226
240
  return;
@@ -36,6 +36,13 @@ export interface ServerAdapterResult {
36
36
  }) => Promise<string | undefined>;
37
37
  servePreviewOnly: () => Promise<string | undefined>;
38
38
  dispose(): Promise<void>;
39
+ /**
40
+ * Updates listen metadata after the runtime binds its actual port.
41
+ */
42
+ applyBoundPort: (input: {
43
+ port: number;
44
+ runtimeOrigin: string;
45
+ }) => void;
39
46
  /**
40
47
  * Lists static-generation routes for app-start consumers.
41
48
  *
@@ -55,6 +62,13 @@ export declare abstract class AbstractServerAdapter<TOptions extends ServerAdapt
55
62
  protected serveOptions: TOptions['serveOptions'];
56
63
  protected runtimeOrigin: string;
57
64
  constructor(options: TOptions);
65
+ /**
66
+ * Updates listen metadata after the runtime binds its actual port.
67
+ */
68
+ applyBoundPort(input: {
69
+ port: number;
70
+ runtimeOrigin: string;
71
+ }): void;
58
72
  /**
59
73
  * Initialize the server adapter
60
74
  */
@@ -20,4 +20,14 @@ export class AbstractServerAdapter {
20
20
  this.serveOptions = options.serveOptions || {};
21
21
  this.runtimeOrigin = options.runtimeOrigin;
22
22
  }
23
+ /**
24
+ * Updates listen metadata after the runtime binds its actual port.
25
+ */
26
+ applyBoundPort(input) {
27
+ this.runtimeOrigin = input.runtimeOrigin;
28
+ this.serveOptions = {
29
+ ...this.serveOptions,
30
+ port: input.port,
31
+ };
32
+ }
23
33
  }
@@ -4,9 +4,4 @@ export type WebSocketRouteMatch = {
4
4
  params: Record<string, string>;
5
5
  kind: string;
6
6
  };
7
- /**
8
- * Pattern matcher for WebSocket route paths.
9
- */
10
- export declare function matchWebSocketPath(pattern: string, pathname: string): Record<string, string> | null;
11
- export declare function scoreWebSocketPattern(pattern: string): number;
12
7
  export declare function findWebSocketRoute(handlers: Map<string, EcopagesWebSocketHandler<any, any>>, pathname: string): WebSocketRouteMatch | null;
@@ -1,13 +1,4 @@
1
1
  import { matchColonSegmentPath, scoreColonSegmentPath } from './segment-path-matcher.js';
2
- /**
3
- * Pattern matcher for WebSocket route paths.
4
- */
5
- export function matchWebSocketPath(pattern, pathname) {
6
- return matchColonSegmentPath(pattern, pathname);
7
- }
8
- export function scoreWebSocketPattern(pattern) {
9
- return scoreColonSegmentPath(pattern);
10
- }
11
2
  export function findWebSocketRoute(handlers, pathname) {
12
3
  let best = null;
13
4
  let index = 0;
@@ -10,8 +10,9 @@
10
10
  import type { Server } from 'bun';
11
11
  import type { ApiHandlerContext, EcopagesRouteInfo, RouteGroupBuilder } from '../../types/public-types.js';
12
12
  import { SharedApplicationAdapter } from '../shared/runtime/application-adapter.js';
13
+ import type { WebSocketUpgradeOptions } from '../shared/ws/node-http-websocket-upgrades.js';
13
14
  import type { RuntimeHost } from '../shared/runtime/runtime-host.js';
14
- import type { EcopagesAppOptions } from '../create-app.js';
15
+ import type { ResolvedEcopagesAppOptions } from '../create-app.js';
15
16
  import { type BunServerAdapterResult } from './server-adapter.js';
16
17
  /**
17
18
  * Bun-specific route group builder that properly infers route params from path patterns.
@@ -27,23 +28,16 @@ export type BunRouteGroupBuilder<WebSocketData = undefined, TContext extends Api
27
28
  * This class extends the {@link AbstractApplicationAdapter}
28
29
  * and provides methods for handling HTTP requests and managing the server.
29
30
  */
30
- export declare class BunEcopagesApp<WebSocketData = undefined> extends SharedApplicationAdapter<EcopagesAppOptions, Server<WebSocketData>, Request> {
31
+ export declare class BunEcopagesApp<WebSocketData = undefined> extends SharedApplicationAdapter<ResolvedEcopagesAppOptions, Server<WebSocketData>, Request> {
31
32
  serverAdapter: BunServerAdapterResult | undefined;
32
33
  private server;
33
34
  private stopped;
34
35
  private readonly runtimeHost;
35
- constructor(options: EcopagesAppOptions, dependencies: {
36
+ constructor(options: ResolvedEcopagesAppOptions, dependencies: {
36
37
  runtimeHost: RuntimeHost<Server<WebSocketData>, Bun.Serve.Options<WebSocketData>>;
37
38
  });
38
39
  fetch(request: Request): Promise<Response>;
39
- attachWebSocketUpgrades(httpServer: import('node:http').Server, options?: {
40
- passthroughUnmatched?: boolean;
41
- }): Promise<void>;
42
- /**
43
- * Complete the initialization of the server adapter by processing dynamic routes
44
- * @param server The Bun server instance
45
- */
46
- completeInitialization(server: Server<WebSocketData>): Promise<void>;
40
+ attachWebSocketUpgrades(httpServer: import('node:http').Server, options?: WebSocketUpgradeOptions): Promise<void>;
47
41
  /**
48
42
  * Initialize the Bun server adapter
49
43
  */
@@ -51,11 +45,13 @@ export declare class BunEcopagesApp<WebSocketData = undefined> extends SharedApp
51
45
  private ensureServerAdapterReady;
52
46
  private bootPreviewServeOnly;
53
47
  private bootStaticWithoutRuntimeServer;
54
- private runStaticBuildOnRuntimeServer;
55
48
  /**
56
- * Start the Bun application server
57
- * @param options Optional settings
58
- * @param options.autoCompleteInitialization Whether to automatically complete initialization with dynamic routes after server start (defaults to true)
49
+ * Starts the Bun application server, or runs the preview/build flow when the
50
+ * CLI requested one.
51
+ *
52
+ * @remarks
53
+ * HMR endpoints and Bun's `development` serve mode follow the `dev` flag only,
54
+ * matching the options `completeInitialization()` reloads the server with.
59
55
  */
60
56
  protected bootServer(): Promise<Server<WebSocketData> | void>;
61
57
  stop(force?: boolean): Promise<void>;
@@ -64,4 +60,4 @@ export declare class BunEcopagesApp<WebSocketData = undefined> extends SharedApp
64
60
  /**
65
61
  * Factory function to create a Bun application
66
62
  */
67
- export declare function createApp<WebSocketData = undefined>(options: EcopagesAppOptions): Promise<BunEcopagesApp<WebSocketData>>;
63
+ export declare function createApp<WebSocketData = undefined>(options: ResolvedEcopagesAppOptions): Promise<BunEcopagesApp<WebSocketData>>;
@@ -9,7 +9,8 @@
9
9
  */
10
10
  import { appLogger } from '../../global/app-logger.js';
11
11
  import { SharedApplicationAdapter } from '../shared/runtime/application-adapter.js';
12
- import { resolveRuntimeBinding, resolveStaticRuntimeMode } from '../shared/runtime/runtime-app-bootstrap.js';
12
+ import { resolveRuntimeBinding } from '../shared/runtime/runtime-app-bootstrap.js';
13
+ import { bindRuntimeServer } from '../shared/runtime/bind-runtime-server.js';
13
14
  import { startupTrace } from '../../diagnostics/startup-trace.js';
14
15
  import { createBunServerAdapter } from './server-adapter.js';
15
16
  import { BunRuntimeHost } from './runtime-host.js';
@@ -42,16 +43,6 @@ export class BunEcopagesApp extends SharedApplicationAdapter {
42
43
  }
43
44
  this.serverAdapter.attachUserWebSocketUpgrades(httpServer, options);
44
45
  }
45
- /**
46
- * Complete the initialization of the server adapter by processing dynamic routes
47
- * @param server The Bun server instance
48
- */
49
- async completeInitialization(server) {
50
- if (!this.serverAdapter) {
51
- throw new Error('Server adapter not initialized. Call start() first.');
52
- }
53
- await this.serverAdapter.completeInitialization(server);
54
- }
55
46
  /**
56
47
  * Initialize the Bun server adapter
57
48
  */
@@ -65,16 +56,15 @@ export class BunEcopagesApp extends SharedApplicationAdapter {
65
56
  appConfig: this.appConfig,
66
57
  apiHandlers: this.apiHandlers,
67
58
  staticRoutes: this.staticRoutes,
59
+ errorPageLoaders: this.getErrorPageLoaders(),
68
60
  errorHandler: this.errorHandler,
69
61
  websocketHandlers: this.websocketHandlers.size > 0 ? this.websocketHandlers : undefined,
70
62
  options: { watch: binding.watch },
71
63
  serveOptions: binding.serveOptions,
72
64
  hostOwnsDevClient: hostOwnsDevClient(this.runtimeOptions),
73
- deferRuntimeAssetSetup: resolveStaticRuntimeMode({
74
- appConfig: this.appConfig,
75
- cliArgs: this.cliArgs,
76
- }).canBuildWithoutRuntimeServer,
65
+ deferRuntimeAssetSetup: this.cliArgs.build || this.cliArgs.preview,
77
66
  allowPortFallback: binding.allowPortFallback,
67
+ onDevelopmentRestart: this.createDevelopmentRestartHandler(),
78
68
  });
79
69
  }
80
70
  async ensureServerAdapterReady() {
@@ -104,66 +94,46 @@ export class BunEcopagesApp extends SharedApplicationAdapter {
104
94
  process.exit(0);
105
95
  }
106
96
  }
107
- async runStaticBuildOnRuntimeServer(serverAdapter, preview, build, force) {
108
- appLogger.debugTime('Building static pages');
109
- const previewOrigin = await serverAdapter.buildStatic({ preview, force });
110
- const buildRuntimeServer = this.server;
111
- this.server = null;
112
- if (buildRuntimeServer) {
113
- await this.runtimeHost.stop(buildRuntimeServer, { force: true });
114
- }
115
- appLogger.debugTimeEnd('Building static pages');
116
- if (build) {
117
- process.exit(0);
118
- }
119
- return previewOrigin;
120
- }
121
97
  /**
122
- * Start the Bun application server
123
- * @param options Optional settings
124
- * @param options.autoCompleteInitialization Whether to automatically complete initialization with dynamic routes after server start (defaults to true)
98
+ * Starts the Bun application server, or runs the preview/build flow when the
99
+ * CLI requested one.
100
+ *
101
+ * @remarks
102
+ * HMR endpoints and Bun's `development` serve mode follow the `dev` flag only,
103
+ * matching the options `completeInitialization()` reloads the server with.
125
104
  */
126
105
  async bootServer() {
127
106
  const serverAdapter = await this.ensureServerAdapterReady();
128
107
  const { dev, preview, build, force, serveOnly } = this.cliArgs;
129
- const staticRuntimeMode = resolveStaticRuntimeMode({
130
- appConfig: this.appConfig,
131
- cliArgs: this.cliArgs,
132
- });
133
108
  if (preview && serveOnly) {
134
109
  await this.bootPreviewServeOnly(serverAdapter);
135
110
  return;
136
111
  }
137
- if (staticRuntimeMode.canBuildWithoutRuntimeServer) {
112
+ if (build || preview) {
138
113
  await this.bootStaticWithoutRuntimeServer(serverAdapter, preview, build, force);
139
114
  return;
140
115
  }
141
- const enableHmr = dev || (!preview && !build);
142
- const runtimeServerOptions = serverAdapter.getServerOptions({ enableHmr });
116
+ const runtimeServerOptions = serverAdapter.getServerOptions({ enableHmr: dev });
117
+ const binding = resolveRuntimeBinding({
118
+ cliArgs: this.cliArgs,
119
+ serverOptions: this.serverOptions,
120
+ });
143
121
  startupTrace.beginServerListen();
144
- this.server = await this.runtimeHost.start({
145
- serveOptions: runtimeServerOptions,
146
- handleRequest: async () => new Response(null, { status: 500 }),
147
- onError: async () => { },
122
+ const bindingResult = await bindRuntimeServer(this.runtimeHost, {
123
+ startOptions: { serveOptions: runtimeServerOptions },
124
+ allowPortFallback: binding.allowPortFallback,
125
+ usePortManager: dev,
148
126
  });
127
+ this.server = bindingResult.server;
128
+ serverAdapter.applyBoundPort(bindingResult);
149
129
  await serverAdapter.completeInitialization(this.server).catch((error) => {
150
130
  appLogger.error(`Failed to complete initialization: ${error}`);
151
131
  });
152
132
  if (!this.server) {
153
133
  throw new Error('Server failed to start');
154
134
  }
155
- const runtimeOrigin = this.runtimeHost.getOrigin(this.server, runtimeServerOptions);
156
- let previewOrigin;
157
- if (build || preview) {
158
- previewOrigin = await this.runStaticBuildOnRuntimeServer(serverAdapter, preview, build, force);
159
- }
160
- else {
161
- await this.notifyListening(runtimeOrigin);
162
- }
163
- if (preview && previewOrigin) {
164
- await this.notifyListening(previewOrigin);
165
- }
166
- return this.server ?? undefined;
135
+ await this.notifyListening(bindingResult.runtimeOrigin);
136
+ return this.server;
167
137
  }
168
138
  async stop(force = true) {
169
139
  if (this.stopped) {
@@ -17,7 +17,6 @@ export interface HmrManagerParams {
17
17
  * own explicit registration path.
18
18
  */
19
19
  export declare class HmrManager extends SharedHmrManager {
20
- private wsHandler;
21
20
  /**
22
21
  * Creates the Bun HMR manager around the shared HMR orchestration pipeline.
23
22
  *
@@ -10,7 +10,6 @@ import { SharedHmrManager } from '../shared/hmr/shared-hmr-manager.js';
10
10
  * own explicit registration path.
11
11
  */
12
12
  export class HmrManager extends SharedHmrManager {
13
- wsHandler;
14
13
  /**
15
14
  * Creates the Bun HMR manager around the shared HMR orchestration pipeline.
16
15
  *
@@ -51,10 +50,9 @@ export class HmrManager extends SharedHmrManager {
51
50
  bridge.unsubscribe(ws);
52
51
  appLogger.debug(`[HmrManager] Connection closed. Subscribers: ${bridge.subscriberCount}`);
53
52
  };
54
- this.wsHandler = { open, close };
55
53
  return {
56
- open: this.wsHandler.open,
57
- close: this.wsHandler.close,
54
+ open,
55
+ close,
58
56
  message: (_ws, message) => {
59
57
  appLogger.debug('[HMR] Received message from client:', message);
60
58
  },