@ecopages/core 0.2.0-alpha.39 → 0.2.0-alpha.40
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/package.json +2 -2
- package/src/adapters/bun/create-app.d.ts +8 -1
- package/src/adapters/bun/create-app.js +52 -65
- package/src/adapters/bun/hmr-manager.d.ts +19 -103
- package/src/adapters/bun/hmr-manager.js +26 -280
- package/src/adapters/bun/runtime-host.d.ts +52 -0
- package/src/adapters/bun/runtime-host.js +56 -0
- package/src/adapters/bun/server-adapter.d.ts +89 -28
- package/src/adapters/bun/server-adapter.js +113 -61
- package/src/adapters/bun/static-preview-host.d.ts +28 -0
- package/src/adapters/bun/static-preview-host.js +45 -0
- package/src/adapters/node/create-app.d.ts +9 -3
- package/src/adapters/node/create-app.js +24 -81
- package/src/adapters/node/http-request-bridge.d.ts +57 -0
- package/src/adapters/node/http-request-bridge.js +118 -0
- package/src/adapters/node/node-hmr-manager.d.ts +22 -91
- package/src/adapters/node/node-hmr-manager.js +26 -272
- package/src/adapters/node/runtime-host.d.ts +57 -0
- package/src/adapters/node/runtime-host.js +92 -0
- package/src/adapters/node/server-adapter-dependencies.d.ts +19 -0
- package/src/adapters/node/server-adapter-dependencies.js +18 -0
- package/src/adapters/node/server-adapter.d.ts +10 -37
- package/src/adapters/node/server-adapter.js +55 -125
- package/src/adapters/node/static-preview-host.d.ts +55 -0
- package/src/adapters/node/static-preview-host.js +68 -0
- package/src/adapters/shared/runtime-app-bootstrap.d.ts +26 -0
- package/src/adapters/shared/runtime-app-bootstrap.js +46 -0
- package/src/adapters/shared/runtime-host.d.ts +12 -0
- package/src/adapters/shared/runtime-host.js +0 -0
- package/src/adapters/shared/shared-hmr-manager.d.ts +59 -0
- package/src/adapters/shared/shared-hmr-manager.js +239 -0
- package/src/adapters/shared/static-preview-host.d.ts +10 -0
- package/src/adapters/shared/static-preview-host.js +0 -0
- package/src/build/build-adapter.js +12 -1
- package/src/build/esbuild-build-adapter.d.ts +1 -0
- package/src/build/esbuild-build-adapter.js +13 -0
- package/src/hmr/strategies/js-hmr-strategy.js +0 -4
- package/src/plugins/integration-plugin.d.ts +6 -1
- package/src/route-renderer/orchestration/integration-renderer.d.ts +32 -14
- package/src/route-renderer/orchestration/integration-renderer.js +80 -14
- package/src/route-renderer/orchestration/processed-asset-dedupe.d.ts +1 -0
- package/src/route-renderer/orchestration/processed-asset-dedupe.js +15 -11
- package/src/route-renderer/orchestration/route-render-orchestrator.d.ts +22 -8
- package/src/route-renderer/orchestration/route-render-orchestrator.js +59 -10
- package/src/services/assets/asset-processing-service/page-package.d.ts +4 -1
- package/src/services/assets/asset-processing-service/page-package.js +11 -5
- package/src/services/assets/asset-processing-service/processors/script/node-module-script.processor.js +3 -1
- package/src/services/html/html-rewriter-provider.service.d.ts +3 -0
- package/src/services/html/html-transformer.service.d.ts +10 -1
- package/src/services/html/html-transformer.service.js +80 -9
- package/src/services/module-loading/page-module-import.service.js +2 -2
- package/src/types/public-types.d.ts +24 -7
- package/src/adapters/bun/server-lifecycle.d.ts +0 -63
- package/src/adapters/bun/server-lifecycle.js +0 -92
- package/src/adapters/shared/runtime-bootstrap.d.ts +0 -38
- package/src/adapters/shared/runtime-bootstrap.js +0 -43
|
@@ -1,133 +1,47 @@
|
|
|
1
|
-
import fs from "node:fs";
|
|
2
|
-
import path from "node:path";
|
|
3
|
-
import { RESOLVED_ASSETS_DIR } from "../../config/constants.js";
|
|
4
|
-
import { getAppBuildExecutor } from "../../build/build-adapter.js";
|
|
5
|
-
import { fileSystem } from "@ecopages/file-system";
|
|
6
|
-
import { HmrStrategyType } from "../../hmr/hmr-strategy.js";
|
|
7
|
-
import { DefaultHmrStrategy } from "../../hmr/strategies/default-hmr-strategy.js";
|
|
8
|
-
import { JsHmrStrategy } from "../../hmr/strategies/js-hmr-strategy.js";
|
|
9
1
|
import { appLogger } from "../../global/app-logger.js";
|
|
10
|
-
import { HmrEntrypointRegistrar } from "../shared/hmr-entrypoint-registrar.js";
|
|
11
|
-
import { BrowserBundleService } from "../../services/assets/browser-bundle.service.js";
|
|
12
|
-
import { getAppServerModuleTranspiler } from "../../services/module-loading/app-server-module-transpiler.service.js";
|
|
13
2
|
import {
|
|
14
|
-
|
|
15
|
-
NoopEntrypointDependencyGraph,
|
|
16
|
-
setAppEntrypointDependencyGraph
|
|
3
|
+
InMemoryEntrypointDependencyGraph
|
|
17
4
|
} from "../../services/runtime-state/entrypoint-dependency-graph.service.js";
|
|
18
|
-
import {
|
|
19
|
-
class HmrManager {
|
|
20
|
-
static entrypointRegistrationTimeoutMs = 4e3;
|
|
21
|
-
appConfig;
|
|
22
|
-
bridge;
|
|
23
|
-
/** Keep track of watchers */
|
|
24
|
-
watchers = /* @__PURE__ */ new Map();
|
|
25
|
-
/** entrypoint -> output path */
|
|
26
|
-
watchedFiles = /* @__PURE__ */ new Map();
|
|
27
|
-
entrypointRegistrations = /* @__PURE__ */ new Map();
|
|
28
|
-
distDir;
|
|
29
|
-
plugins = [];
|
|
30
|
-
enabled = true;
|
|
31
|
-
strategies = [];
|
|
32
|
-
entrypointRegistrar;
|
|
33
|
-
browserBundleService;
|
|
34
|
-
entrypointDependencyGraph;
|
|
35
|
-
serverModuleTranspiler;
|
|
5
|
+
import { SharedHmrManager } from "../shared/shared-hmr-manager.js";
|
|
6
|
+
class HmrManager extends SharedHmrManager {
|
|
36
7
|
wsHandler;
|
|
37
|
-
constructor({ appConfig, bridge }) {
|
|
38
|
-
this.appConfig = appConfig;
|
|
39
|
-
this.bridge = bridge;
|
|
40
|
-
this.distDir = path.join(resolveInternalWorkDir(this.appConfig), RESOLVED_ASSETS_DIR, "_hmr");
|
|
41
|
-
this.entrypointRegistrar = new HmrEntrypointRegistrar({
|
|
42
|
-
srcDir: this.appConfig.absolutePaths.srcDir,
|
|
43
|
-
distDir: this.distDir,
|
|
44
|
-
entrypointRegistrations: this.entrypointRegistrations,
|
|
45
|
-
watchedFiles: this.watchedFiles,
|
|
46
|
-
clearFailedRegistration: (entrypointPath) => this.clearFailedEntrypointRegistration(entrypointPath),
|
|
47
|
-
registrationTimeoutMs: HmrManager.entrypointRegistrationTimeoutMs
|
|
48
|
-
});
|
|
49
|
-
this.browserBundleService = new BrowserBundleService(appConfig);
|
|
50
|
-
const existingEntrypointDependencyGraph = getAppEntrypointDependencyGraph(appConfig);
|
|
51
|
-
this.entrypointDependencyGraph = existingEntrypointDependencyGraph instanceof NoopEntrypointDependencyGraph ? existingEntrypointDependencyGraph : new NoopEntrypointDependencyGraph();
|
|
52
|
-
setAppEntrypointDependencyGraph(this.appConfig, this.entrypointDependencyGraph);
|
|
53
|
-
this.serverModuleTranspiler = getAppServerModuleTranspiler(this.appConfig);
|
|
54
|
-
this.cleanDistDir();
|
|
55
|
-
this.initializeStrategies();
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* Ensures the HMR output directory exists.
|
|
59
|
-
*
|
|
60
|
-
* This must not remove the directory because multiple app processes
|
|
61
|
-
* can share the same dist path during e2e runs.
|
|
62
|
-
*/
|
|
63
|
-
cleanDistDir() {
|
|
64
|
-
fileSystem.ensureDir(this.distDir);
|
|
65
|
-
}
|
|
66
8
|
/**
|
|
67
|
-
*
|
|
9
|
+
* Creates the Bun HMR manager around the shared HMR orchestration pipeline.
|
|
68
10
|
*
|
|
69
11
|
* @remarks
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
*
|
|
12
|
+
* Bun delegates route watching, rebuild dispatch, and runtime bundle
|
|
13
|
+
* generation to `SharedHmrManager`. The Bun subclass only supplies the
|
|
14
|
+
* transport-specific dependency graph policy and websocket hook surface.
|
|
73
15
|
*/
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
if (strategy.type !== HmrStrategyType.INTEGRATION || strategy.priority <= HmrStrategyType.SCRIPT) {
|
|
77
|
-
return false;
|
|
78
|
-
}
|
|
79
|
-
try {
|
|
80
|
-
return strategy.matches(entrypointPath);
|
|
81
|
-
} catch (error) {
|
|
82
|
-
appLogger.error(error);
|
|
83
|
-
return false;
|
|
84
|
-
}
|
|
85
|
-
});
|
|
16
|
+
constructor({ appConfig, bridge }) {
|
|
17
|
+
super({ appConfig, bridge });
|
|
86
18
|
}
|
|
87
19
|
/**
|
|
88
|
-
*
|
|
89
|
-
*
|
|
20
|
+
* Reuses the shared in-memory dependency graph when possible and otherwise
|
|
21
|
+
* creates the Bun-compatible default graph implementation.
|
|
90
22
|
*/
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
getWatchedFiles: () => this.watchedFiles,
|
|
94
|
-
getDistDir: () => this.distDir,
|
|
95
|
-
getPlugins: () => this.plugins,
|
|
96
|
-
getSrcDir: () => this.appConfig.absolutePaths.srcDir,
|
|
97
|
-
getPagesDir: () => this.appConfig.absolutePaths.pagesDir,
|
|
98
|
-
getLayoutsDir: () => this.appConfig.absolutePaths.layoutsDir,
|
|
99
|
-
getTemplateExtensions: () => this.appConfig.templatesExt,
|
|
100
|
-
getBrowserBundleService: () => this.browserBundleService,
|
|
101
|
-
getEntrypointDependencyGraph: () => this.entrypointDependencyGraph,
|
|
102
|
-
shouldProcessEntrypoint: (entrypointPath) => this.shouldJsStrategyProcessEntrypoint(entrypointPath)
|
|
103
|
-
};
|
|
104
|
-
this.strategies = [new JsHmrStrategy(jsContext), new DefaultHmrStrategy()];
|
|
23
|
+
createEntrypointDependencyGraph(existingEntrypointDependencyGraph) {
|
|
24
|
+
return existingEntrypointDependencyGraph instanceof InMemoryEntrypointDependencyGraph ? existingEntrypointDependencyGraph : new InMemoryEntrypointDependencyGraph();
|
|
105
25
|
}
|
|
106
26
|
/**
|
|
107
|
-
*
|
|
108
|
-
*
|
|
109
|
-
* @
|
|
27
|
+
* Returns the Bun websocket hooks that attach and detach live HMR subscribers.
|
|
28
|
+
*
|
|
29
|
+
* @remarks
|
|
30
|
+
* `SharedHmrManager` stores the bridge behind the transport-agnostic
|
|
31
|
+
* `IClientBridge` contract because most HMR coordination only needs broadcast
|
|
32
|
+
* behavior. Bun connection lifecycle wiring is the point where that abstraction
|
|
33
|
+
* intentionally narrows back to the concrete Bun bridge so websocket instances
|
|
34
|
+
* can be tracked directly.
|
|
110
35
|
*/
|
|
111
|
-
registerStrategy(strategy) {
|
|
112
|
-
this.strategies.push(strategy);
|
|
113
|
-
}
|
|
114
|
-
setPlugins(plugins) {
|
|
115
|
-
this.plugins = [...plugins];
|
|
116
|
-
}
|
|
117
|
-
setEnabled(enabled) {
|
|
118
|
-
this.enabled = enabled;
|
|
119
|
-
}
|
|
120
|
-
isEnabled() {
|
|
121
|
-
return this.enabled;
|
|
122
|
-
}
|
|
123
36
|
getWebSocketHandler() {
|
|
37
|
+
const bridge = this.bridge;
|
|
124
38
|
const open = (ws) => {
|
|
125
|
-
|
|
126
|
-
appLogger.debug(`[HmrManager] Connection opened. Subscribers: ${
|
|
39
|
+
bridge.subscribe(ws);
|
|
40
|
+
appLogger.debug(`[HmrManager] Connection opened. Subscribers: ${bridge.subscriberCount}`);
|
|
127
41
|
};
|
|
128
42
|
const close = (ws) => {
|
|
129
|
-
|
|
130
|
-
appLogger.debug(`[HmrManager] Connection closed. Subscribers: ${
|
|
43
|
+
bridge.unsubscribe(ws);
|
|
44
|
+
appLogger.debug(`[HmrManager] Connection closed. Subscribers: ${bridge.subscriberCount}`);
|
|
131
45
|
};
|
|
132
46
|
this.wsHandler = { open, close };
|
|
133
47
|
return {
|
|
@@ -138,174 +52,6 @@ class HmrManager {
|
|
|
138
52
|
}
|
|
139
53
|
};
|
|
140
54
|
}
|
|
141
|
-
/**
|
|
142
|
-
* Builds the client-side HMR runtime script.
|
|
143
|
-
*/
|
|
144
|
-
async buildRuntime() {
|
|
145
|
-
const runtimeSource = path.resolve(import.meta.dirname, "../../hmr/client/hmr-runtime.js");
|
|
146
|
-
const result = await this.browserBundleService.bundle({
|
|
147
|
-
profile: "hmr-runtime",
|
|
148
|
-
entrypoints: [runtimeSource],
|
|
149
|
-
outdir: this.distDir,
|
|
150
|
-
naming: "_hmr_runtime.js",
|
|
151
|
-
minify: false,
|
|
152
|
-
plugins: this.plugins
|
|
153
|
-
});
|
|
154
|
-
if (!result.success) {
|
|
155
|
-
appLogger.error("[HMR] Failed to build runtime script:", result.logs);
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
getRuntimePath() {
|
|
159
|
-
return path.join(this.distDir, "_hmr_runtime.js");
|
|
160
|
-
}
|
|
161
|
-
broadcast(event) {
|
|
162
|
-
appLogger.debug(
|
|
163
|
-
`[HMR] Broadcasting ${event.type} event, path=${event.path || "all"}, subscribers=${this.bridge.subscriberCount}`
|
|
164
|
-
);
|
|
165
|
-
this.bridge.broadcast(event);
|
|
166
|
-
}
|
|
167
|
-
async handleFileChange(filePath, options = {}) {
|
|
168
|
-
const sorted = [...this.strategies].sort((a, b) => b.priority - a.priority);
|
|
169
|
-
const strategy = sorted.find((s) => {
|
|
170
|
-
try {
|
|
171
|
-
return s.matches(filePath);
|
|
172
|
-
} catch (err) {
|
|
173
|
-
appLogger.error(err);
|
|
174
|
-
return false;
|
|
175
|
-
}
|
|
176
|
-
});
|
|
177
|
-
if (!strategy) {
|
|
178
|
-
appLogger.warn(`[HMR] No strategy found for ${filePath}`);
|
|
179
|
-
return;
|
|
180
|
-
}
|
|
181
|
-
appLogger.debug(`[HmrManager] Selected strategy: ${strategy.constructor.name}`);
|
|
182
|
-
const action = await strategy.process(filePath);
|
|
183
|
-
const shouldBroadcast = options.broadcast ?? true;
|
|
184
|
-
if (shouldBroadcast && action.type === "broadcast") {
|
|
185
|
-
if (action.events) {
|
|
186
|
-
for (const event of action.events) {
|
|
187
|
-
this.broadcast(event);
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
getOutputUrl(entrypointPath) {
|
|
193
|
-
return this.watchedFiles.get(entrypointPath);
|
|
194
|
-
}
|
|
195
|
-
getWatchedFiles() {
|
|
196
|
-
return this.watchedFiles;
|
|
197
|
-
}
|
|
198
|
-
getDistDir() {
|
|
199
|
-
return this.distDir;
|
|
200
|
-
}
|
|
201
|
-
getPlugins() {
|
|
202
|
-
return this.plugins;
|
|
203
|
-
}
|
|
204
|
-
getDefaultContext() {
|
|
205
|
-
return {
|
|
206
|
-
getWatchedFiles: () => this.watchedFiles,
|
|
207
|
-
getDistDir: () => this.distDir,
|
|
208
|
-
getPlugins: () => this.plugins,
|
|
209
|
-
getSrcDir: () => this.appConfig.absolutePaths.srcDir,
|
|
210
|
-
getLayoutsDir: () => this.appConfig.absolutePaths.layoutsDir,
|
|
211
|
-
getPagesDir: () => this.appConfig.absolutePaths.pagesDir,
|
|
212
|
-
getBuildExecutor: () => getAppBuildExecutor(this.appConfig),
|
|
213
|
-
getBrowserBundleService: () => this.browserBundleService,
|
|
214
|
-
importServerModule: async (filePath) => await this.serverModuleTranspiler.importModule({
|
|
215
|
-
filePath,
|
|
216
|
-
outdir: path.join(resolveInternalExecutionDir(this.appConfig), ".server-modules"),
|
|
217
|
-
externalPackages: true
|
|
218
|
-
})
|
|
219
|
-
};
|
|
220
|
-
}
|
|
221
|
-
clearFailedEntrypointRegistration(entrypointPath) {
|
|
222
|
-
this.watchedFiles.delete(entrypointPath);
|
|
223
|
-
}
|
|
224
|
-
/**
|
|
225
|
-
* Registers one integration-owned page entrypoint.
|
|
226
|
-
*
|
|
227
|
-
* @remarks
|
|
228
|
-
* Concurrent callers share one in-flight registration. The registration is
|
|
229
|
-
* cleared from the dedupe map when it settles so later callers cannot inherit a
|
|
230
|
-
* stale promise.
|
|
231
|
-
*/
|
|
232
|
-
async registerEntrypoint(entrypointPath) {
|
|
233
|
-
return await this.entrypointRegistrar.registerEntrypoint(entrypointPath, {
|
|
234
|
-
emit: async (normalizedEntrypoint) => await this.emitStrictEntrypoint(normalizedEntrypoint),
|
|
235
|
-
getMissingOutputError: (normalizedEntrypoint, outputPath) => new Error(
|
|
236
|
-
`[HMR] Integration failed to emit entrypoint ${normalizedEntrypoint} to ${outputPath}. Page entrypoints must be produced by their owning integration.`
|
|
237
|
-
)
|
|
238
|
-
});
|
|
239
|
-
}
|
|
240
|
-
/**
|
|
241
|
-
* Registers one generic script entrypoint.
|
|
242
|
-
*
|
|
243
|
-
* @remarks
|
|
244
|
-
* This explicit path keeps the page-entrypoint contract strict while still
|
|
245
|
-
* allowing generic script assets to use the fallback build path.
|
|
246
|
-
*/
|
|
247
|
-
async registerScriptEntrypoint(entrypointPath) {
|
|
248
|
-
return await this.entrypointRegistrar.registerEntrypoint(entrypointPath, {
|
|
249
|
-
emit: async (normalizedEntrypoint, outputPath) => await this.emitScriptEntrypoint(normalizedEntrypoint, outputPath),
|
|
250
|
-
getMissingOutputError: (normalizedEntrypoint) => new Error(`[HMR] Failed to register script entrypoint: ${normalizedEntrypoint}`)
|
|
251
|
-
});
|
|
252
|
-
}
|
|
253
|
-
/**
|
|
254
|
-
* Performs strict integration-owned registration for one normalized path.
|
|
255
|
-
*
|
|
256
|
-
* @remarks
|
|
257
|
-
* The manager reserves the output URL, removes any stale emitted file, runs
|
|
258
|
-
* strategy processing without broadcasting, and then verifies that the owning
|
|
259
|
-
* integration emitted the expected file.
|
|
260
|
-
*/
|
|
261
|
-
async emitStrictEntrypoint(entrypointPath) {
|
|
262
|
-
await this.handleFileChange(entrypointPath, { broadcast: false });
|
|
263
|
-
}
|
|
264
|
-
/**
|
|
265
|
-
* Performs registration for a generic script asset.
|
|
266
|
-
*
|
|
267
|
-
* @remarks
|
|
268
|
-
* This path performs a targeted browser bundle for the requested script
|
|
269
|
-
* entrypoint only. The resulting dependency graph is retained so later file
|
|
270
|
-
* changes can invalidate just the affected script entrypoints.
|
|
271
|
-
*/
|
|
272
|
-
async emitScriptEntrypoint(entrypointPath, outputPath) {
|
|
273
|
-
const naming = path.relative(this.distDir, outputPath).split(path.sep).join("/");
|
|
274
|
-
const buildResult = await this.browserBundleService.bundle({
|
|
275
|
-
profile: "hmr-entrypoint",
|
|
276
|
-
entrypoints: [entrypointPath],
|
|
277
|
-
outdir: this.distDir,
|
|
278
|
-
naming,
|
|
279
|
-
minify: false,
|
|
280
|
-
plugins: this.plugins
|
|
281
|
-
});
|
|
282
|
-
if (!buildResult.success) {
|
|
283
|
-
appLogger.error(`[HMR] Generic script entrypoint build failed for ${entrypointPath}:`, buildResult.logs);
|
|
284
|
-
return;
|
|
285
|
-
}
|
|
286
|
-
const entrypointDependencies = buildResult.dependencyGraph?.entrypoints?.[entrypointPath];
|
|
287
|
-
if (entrypointDependencies) {
|
|
288
|
-
this.entrypointDependencyGraph.setEntrypointDependencies(entrypointPath, entrypointDependencies);
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
/**
|
|
292
|
-
* Stops active watchers and releases retained registration state.
|
|
293
|
-
*
|
|
294
|
-
* @remarks
|
|
295
|
-
* Emitted `_hmr` files remain on disk because parallel app processes may share
|
|
296
|
-
* the same dist directory. The in-memory indexes are cleared so stale
|
|
297
|
-
* entrypoints cannot leak through a reused manager object.
|
|
298
|
-
*/
|
|
299
|
-
stop() {
|
|
300
|
-
this.entrypointRegistrations.clear();
|
|
301
|
-
for (const watcher of this.watchers.values()) {
|
|
302
|
-
watcher.close();
|
|
303
|
-
}
|
|
304
|
-
this.watchers.clear();
|
|
305
|
-
this.watchedFiles.clear();
|
|
306
|
-
this.entrypointDependencyGraph.reset();
|
|
307
|
-
this.plugins = [];
|
|
308
|
-
}
|
|
309
55
|
}
|
|
310
56
|
export {
|
|
311
57
|
HmrManager
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { Server } from 'bun';
|
|
2
|
+
import type { RuntimeHost, RuntimeHostStartOptions } from '../shared/runtime-host.js';
|
|
3
|
+
type BunRuntimeProvider = {
|
|
4
|
+
serve(options: Bun.Serve.Options<unknown>): Server<unknown>;
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* Bun runtime host that adapts the shared runtime lifecycle contract onto
|
|
8
|
+
* `Bun.serve()`.
|
|
9
|
+
*
|
|
10
|
+
* @remarks
|
|
11
|
+
* The injected runtime provider exists so tests and non-Bun call sites can
|
|
12
|
+
* construct the host without assuming `globalThis.Bun` is present. The provider
|
|
13
|
+
* returns `null` instead of `undefined` to keep that absence explicit at the
|
|
14
|
+
* host boundary.
|
|
15
|
+
*/
|
|
16
|
+
export declare class BunRuntimeHost<WebSocketData = undefined> implements RuntimeHost<Server<WebSocketData>, Bun.Serve.Options<WebSocketData>> {
|
|
17
|
+
private readonly runtimeProvider;
|
|
18
|
+
/**
|
|
19
|
+
* Creates a Bun runtime host with an injectable runtime lookup.
|
|
20
|
+
*
|
|
21
|
+
* @remarks
|
|
22
|
+
* Production code uses the default `getBunRuntime()` lookup. Tests may inject a
|
|
23
|
+
* fake provider that either supplies a `serve()` implementation or returns
|
|
24
|
+
* `null` to verify the runtime-required failure path.
|
|
25
|
+
*/
|
|
26
|
+
constructor(runtimeProvider?: () => BunRuntimeProvider | null);
|
|
27
|
+
/**
|
|
28
|
+
* Starts the Bun server using the already-composed serve options from the
|
|
29
|
+
* shared application bootstrap.
|
|
30
|
+
*/
|
|
31
|
+
start(options: RuntimeHostStartOptions<Bun.Serve.Options<WebSocketData>>): Promise<Server<WebSocketData>>;
|
|
32
|
+
/**
|
|
33
|
+
* Stops the active Bun server immediately.
|
|
34
|
+
*
|
|
35
|
+
* @remarks
|
|
36
|
+
* The shared runtime-host contract accepts optional stop options, but Bun's
|
|
37
|
+
* server API already models the shutdown behavior directly. This host always
|
|
38
|
+
* uses Bun's forceful stop path so preview and embedded runtime shutdowns do
|
|
39
|
+
* not linger on open connections.
|
|
40
|
+
*/
|
|
41
|
+
stop(server: Server<WebSocketData>): Promise<void>;
|
|
42
|
+
/**
|
|
43
|
+
* Resolves the externally reported runtime origin from the active server.
|
|
44
|
+
*
|
|
45
|
+
* @remarks
|
|
46
|
+
* Bun may finalize the hostname or port differently from the requested serve
|
|
47
|
+
* options. This method prefers the live server binding and only falls back to
|
|
48
|
+
* the requested options when Bun leaves a field unset.
|
|
49
|
+
*/
|
|
50
|
+
getOrigin(server: Server<WebSocketData>, fallbackServeOptions: Bun.Serve.Options<WebSocketData>): string;
|
|
51
|
+
}
|
|
52
|
+
export {};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { getBunRuntime } from "../../utils/runtime.js";
|
|
2
|
+
import { resolveServeRuntimeOrigin } from "../shared/runtime-app-bootstrap.js";
|
|
3
|
+
class BunRuntimeHost {
|
|
4
|
+
/**
|
|
5
|
+
* Creates a Bun runtime host with an injectable runtime lookup.
|
|
6
|
+
*
|
|
7
|
+
* @remarks
|
|
8
|
+
* Production code uses the default `getBunRuntime()` lookup. Tests may inject a
|
|
9
|
+
* fake provider that either supplies a `serve()` implementation or returns
|
|
10
|
+
* `null` to verify the runtime-required failure path.
|
|
11
|
+
*/
|
|
12
|
+
constructor(runtimeProvider = () => getBunRuntime() ?? null) {
|
|
13
|
+
this.runtimeProvider = runtimeProvider;
|
|
14
|
+
}
|
|
15
|
+
runtimeProvider;
|
|
16
|
+
/**
|
|
17
|
+
* Starts the Bun server using the already-composed serve options from the
|
|
18
|
+
* shared application bootstrap.
|
|
19
|
+
*/
|
|
20
|
+
async start(options) {
|
|
21
|
+
const bun = this.runtimeProvider();
|
|
22
|
+
if (!bun) {
|
|
23
|
+
throw new Error("Bun runtime is required for the Bun adapter");
|
|
24
|
+
}
|
|
25
|
+
return bun.serve(options.serveOptions);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Stops the active Bun server immediately.
|
|
29
|
+
*
|
|
30
|
+
* @remarks
|
|
31
|
+
* The shared runtime-host contract accepts optional stop options, but Bun's
|
|
32
|
+
* server API already models the shutdown behavior directly. This host always
|
|
33
|
+
* uses Bun's forceful stop path so preview and embedded runtime shutdowns do
|
|
34
|
+
* not linger on open connections.
|
|
35
|
+
*/
|
|
36
|
+
async stop(server) {
|
|
37
|
+
server.stop(true);
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Resolves the externally reported runtime origin from the active server.
|
|
41
|
+
*
|
|
42
|
+
* @remarks
|
|
43
|
+
* Bun may finalize the hostname or port differently from the requested serve
|
|
44
|
+
* options. This method prefers the live server binding and only falls back to
|
|
45
|
+
* the requested options when Bun leaves a field unset.
|
|
46
|
+
*/
|
|
47
|
+
getOrigin(server, fallbackServeOptions) {
|
|
48
|
+
return resolveServeRuntimeOrigin({
|
|
49
|
+
hostname: server.hostname ?? fallbackServeOptions.hostname,
|
|
50
|
+
port: server.port ?? fallbackServeOptions.port
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
export {
|
|
55
|
+
BunRuntimeHost
|
|
56
|
+
};
|
|
@@ -3,11 +3,9 @@ import type { EcoPagesAppConfig } from '../../types/internal-types.js';
|
|
|
3
3
|
import type { ApiHandler, ErrorHandler, StaticRoute } from '../../types/public-types.js';
|
|
4
4
|
import { SharedServerAdapter } from '../shared/server-adapter.js';
|
|
5
5
|
import type { ServerAdapterResult } from '../abstract/server-adapter.js';
|
|
6
|
-
import {
|
|
7
|
-
import { ServerStaticBuilder, type ServerStaticBuilderParams } from '../shared/server-static-builder.js';
|
|
6
|
+
import type { StaticPreviewHost } from '../shared/static-preview-host.js';
|
|
8
7
|
import { ClientBridge } from './client-bridge.js';
|
|
9
8
|
import { HmrManager } from './hmr-manager.js';
|
|
10
|
-
import { ServerLifecycle } from './server-lifecycle.js';
|
|
11
9
|
type BunServerInstance = Server<unknown>;
|
|
12
10
|
type BunNativeServeOptions = Bun.Serve.Options<unknown>;
|
|
13
11
|
export type BunServerRoutes = Bun.Serve.Routes<unknown, string>;
|
|
@@ -18,6 +16,15 @@ export type BunServeOptions = Omit<BunNativeServeOptions, 'fetch'> & {
|
|
|
18
16
|
fetch?: (this: BunServerInstance, request: Request, server: BunServerInstance) => Promise<void | Response>;
|
|
19
17
|
websocket?: WebSocketHandler<unknown>;
|
|
20
18
|
};
|
|
19
|
+
/**
|
|
20
|
+
* Construction parameters for the Bun server adapter.
|
|
21
|
+
*
|
|
22
|
+
* @remarks
|
|
23
|
+
* Callers normally provide only the app-facing fields such as routes, handlers,
|
|
24
|
+
* and `serveOptions`. The transport collaborators remain optional here because
|
|
25
|
+
* `createBunServerAdapter()` fills in Bun-specific defaults before the concrete
|
|
26
|
+
* adapter instance is created.
|
|
27
|
+
*/
|
|
21
28
|
export interface BunServerAdapterParams {
|
|
22
29
|
appConfig: EcoPagesAppConfig;
|
|
23
30
|
runtimeOrigin: string;
|
|
@@ -28,11 +35,9 @@ export interface BunServerAdapterParams {
|
|
|
28
35
|
options?: {
|
|
29
36
|
watch?: boolean;
|
|
30
37
|
};
|
|
31
|
-
lifecycle?: ServerLifecycle;
|
|
32
|
-
staticBuilderFactory?: (params: ServerStaticBuilderParams) => ServerStaticBuilder;
|
|
33
|
-
routeHandlerFactory?: (params: ServerRouteHandlerParams) => ServerRouteHandler;
|
|
34
38
|
hmrManager?: HmrManager;
|
|
35
39
|
bridge?: ClientBridge;
|
|
40
|
+
previewHost?: StaticPreviewHost;
|
|
36
41
|
}
|
|
37
42
|
export interface BunServerAdapterResult extends ServerAdapterResult {
|
|
38
43
|
getServerOptions: (options?: {
|
|
@@ -44,6 +49,16 @@ export interface BunServerAdapterResult extends ServerAdapterResult {
|
|
|
44
49
|
completeInitialization: (server?: BunServerInstance | null) => Promise<void>;
|
|
45
50
|
handleRequest: (request: Request) => Promise<Response>;
|
|
46
51
|
}
|
|
52
|
+
/**
|
|
53
|
+
* Bun transport adapter that wires shared Ecopages request handling onto a live
|
|
54
|
+
* `Bun.serve()` runtime.
|
|
55
|
+
*
|
|
56
|
+
* @remarks
|
|
57
|
+
* The adapter owns Bun-specific concerns that do not exist in the shared server
|
|
58
|
+
* abstraction: websocket-backed HMR transport, runtime plugin registration, and
|
|
59
|
+
* preview-host startup for static builds. Routing, rendering, and response
|
|
60
|
+
* composition still delegate to the shared server adapter base.
|
|
61
|
+
*/
|
|
47
62
|
export declare class BunServerAdapter extends SharedServerAdapter<BunServerAdapterParams, BunServerAdapterResult> {
|
|
48
63
|
appConfig: EcoPagesAppConfig;
|
|
49
64
|
options: BunServerAdapterParams['options'];
|
|
@@ -52,24 +67,37 @@ export declare class BunServerAdapter extends SharedServerAdapter<BunServerAdapt
|
|
|
52
67
|
protected staticRoutes: StaticRoute[];
|
|
53
68
|
protected errorHandler?: ErrorHandler;
|
|
54
69
|
private bridge;
|
|
55
|
-
private lifecycle;
|
|
56
70
|
hmrManager: HmrManager;
|
|
57
71
|
private initializationPromise;
|
|
58
72
|
private fullyInitialized;
|
|
59
73
|
serverInstance: BunServerInstance | null;
|
|
60
|
-
private readonly
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
*
|
|
68
|
-
*
|
|
74
|
+
private readonly previewHost;
|
|
75
|
+
/**
|
|
76
|
+
* Creates a Bun server adapter with already-resolved runtime collaborators.
|
|
77
|
+
*
|
|
78
|
+
* @remarks
|
|
79
|
+
* The public params interface keeps `hmrManager`, `bridge`, and `previewHost`
|
|
80
|
+
* optional so factory callers can omit them. By the time the concrete adapter
|
|
81
|
+
* is constructed, those collaborators are mandatory because the adapter cannot
|
|
82
|
+
* initialize Bun HMR or preview flows without them.
|
|
83
|
+
*/
|
|
84
|
+
constructor({ appConfig, runtimeOrigin, serveOptions, apiHandlers, staticRoutes, errorHandler, options, hmrManager, bridge, previewHost, }: BunServerAdapterParams & {
|
|
85
|
+
hmrManager: HmrManager;
|
|
86
|
+
bridge: ClientBridge;
|
|
87
|
+
previewHost: StaticPreviewHost;
|
|
88
|
+
});
|
|
89
|
+
/**
|
|
90
|
+
* Returns whether adapter-level HTML responses still need HMR runtime injection.
|
|
91
|
+
*
|
|
92
|
+
* @remarks
|
|
93
|
+
* Filesystem-routed pages are wrapped later in the shared route layer. This
|
|
94
|
+
* adapter-level check exists for explicit API handlers that return HTML and
|
|
95
|
+
* would otherwise bypass the route wrapper entirely.
|
|
69
96
|
*/
|
|
70
97
|
private shouldInjectHmrScript;
|
|
71
98
|
/**
|
|
72
|
-
*
|
|
99
|
+
* Delegates the HTML-response test to the shared response helper used by both
|
|
100
|
+
* adapters.
|
|
73
101
|
*/
|
|
74
102
|
private isHtmlResponse;
|
|
75
103
|
/**
|
|
@@ -78,20 +106,37 @@ export declare class BunServerAdapter extends SharedServerAdapter<BunServerAdapt
|
|
|
78
106
|
*/
|
|
79
107
|
private maybeInjectHmrScript;
|
|
80
108
|
/**
|
|
81
|
-
* Initializes the server adapter's core components.
|
|
82
|
-
* Delegates to ServerLifecycle for setup.
|
|
109
|
+
* Initializes the server adapter's core runtime components.
|
|
83
110
|
*/
|
|
84
111
|
initialize(): Promise<void>;
|
|
85
112
|
/**
|
|
86
|
-
*
|
|
113
|
+
* Copies the source `public` directory into the runtime output and ensures the
|
|
114
|
+
* HMR assets directory exists before any runtime bundles are emitted.
|
|
115
|
+
*/
|
|
116
|
+
private prepareRuntimePublicDir;
|
|
117
|
+
/**
|
|
118
|
+
* Registers runtime plugins and propagates the final HMR manager into each
|
|
119
|
+
* integration.
|
|
120
|
+
*
|
|
121
|
+
* @remarks
|
|
122
|
+
* This is where Bun's runtime-plugin registration path meets the integration
|
|
123
|
+
* lifecycle. A failure here leaves the runtime partially bootstrapped, so the
|
|
124
|
+
* method logs the underlying error and rethrows instead of trying to limp on.
|
|
125
|
+
*/
|
|
126
|
+
private initializeRuntimePlugins;
|
|
127
|
+
/**
|
|
128
|
+
* Rebuilds the shared routing state and hot-reloads the live Bun server when a
|
|
129
|
+
* watched route file changes.
|
|
87
130
|
*/
|
|
88
131
|
private refreshRouterRoutes;
|
|
89
132
|
private watch;
|
|
90
133
|
/**
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
*
|
|
134
|
+
* Builds the `Bun.serve()` options for the current adapter state.
|
|
135
|
+
*
|
|
136
|
+
* @remarks
|
|
137
|
+
* The HMR-enabled variant wraps the base fetch handler so one Bun server can
|
|
138
|
+
* serve normal requests, accept HMR websocket upgrades, and expose the HMR
|
|
139
|
+
* runtime asset without splitting responsibility across separate listeners.
|
|
95
140
|
*/
|
|
96
141
|
getServerOptions({ enableHmr }?: {
|
|
97
142
|
enableHmr?: boolean | undefined;
|
|
@@ -104,8 +149,12 @@ export declare class BunServerAdapter extends SharedServerAdapter<BunServerAdapt
|
|
|
104
149
|
*/
|
|
105
150
|
private retrieveBodyFromRequest;
|
|
106
151
|
/**
|
|
107
|
-
*
|
|
108
|
-
*
|
|
152
|
+
* Composes the base Bun server settings that all runtime modes build from.
|
|
153
|
+
*
|
|
154
|
+
* @remarks
|
|
155
|
+
* This method centralizes the Bun-specific error boundary. It preserves the
|
|
156
|
+
* shared route pipeline while still allowing adapter-level custom error-handler
|
|
157
|
+
* execution and `HttpError` passthrough.
|
|
109
158
|
*/
|
|
110
159
|
private buildServerSettings;
|
|
111
160
|
/**
|
|
@@ -122,7 +171,12 @@ export declare class BunServerAdapter extends SharedServerAdapter<BunServerAdapt
|
|
|
122
171
|
*/
|
|
123
172
|
completeInitialization(server?: BunServerInstance | null): Promise<void>;
|
|
124
173
|
/**
|
|
125
|
-
* Performs
|
|
174
|
+
* Performs the one-time post-bind initialization path for Bun servers.
|
|
175
|
+
*
|
|
176
|
+
* @remarks
|
|
177
|
+
* This is intentionally split from `initialize()` because shared route handling
|
|
178
|
+
* and file watching need the live server instance to exist before Bun can
|
|
179
|
+
* reload updated route handlers in place.
|
|
126
180
|
*/
|
|
127
181
|
private _performInitialization;
|
|
128
182
|
/**
|
|
@@ -149,7 +203,14 @@ export declare class BunServerAdapter extends SharedServerAdapter<BunServerAdapt
|
|
|
149
203
|
private handleNoMatch;
|
|
150
204
|
}
|
|
151
205
|
/**
|
|
152
|
-
*
|
|
206
|
+
* Creates the Bun server adapter and fills in the runtime-specific collaborators
|
|
207
|
+
* that Bun callers usually leave implicit.
|
|
208
|
+
*
|
|
209
|
+
* @remarks
|
|
210
|
+
* This is the canonical entry point for Bun server-adapter construction. It
|
|
211
|
+
* guarantees that the concrete adapter receives a Bun websocket bridge, HMR
|
|
212
|
+
* manager, and preview host even though those dependencies are optional on the
|
|
213
|
+
* public params type.
|
|
153
214
|
*/
|
|
154
215
|
export declare function createBunServerAdapter(params: BunServerAdapterParams): Promise<BunServerAdapterResult>;
|
|
155
216
|
export {};
|