@ereo/server 0.2.26 → 0.2.29
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/dist/bun-server.d.ts +18 -0
- package/dist/bun-server.d.ts.map +1 -1
- package/dist/index.js +79 -24
- package/package.json +5 -5
package/dist/bun-server.d.ts
CHANGED
|
@@ -60,6 +60,20 @@ export interface ServerOptions {
|
|
|
60
60
|
middleware: MiddlewareHandler;
|
|
61
61
|
viewerHandler?: (req: Request) => Response;
|
|
62
62
|
tracesAPIHandler?: (req: Request) => Response;
|
|
63
|
+
/** WebSocket handler for live trace streaming */
|
|
64
|
+
traceWebSocket?: {
|
|
65
|
+
websocket: {
|
|
66
|
+
open: (ws: any) => void;
|
|
67
|
+
close: (ws: any) => void;
|
|
68
|
+
message: (ws: any, message: string | Buffer) => void;
|
|
69
|
+
};
|
|
70
|
+
};
|
|
71
|
+
/** Auto-instrumentation functions from @ereo/trace */
|
|
72
|
+
instrumentors?: {
|
|
73
|
+
getActiveSpan: (ctx: any) => any;
|
|
74
|
+
traceRouteMatch: <T>(span: any, fn: () => T) => T;
|
|
75
|
+
traceLoader: <T>(span: any, key: string, fn: () => T | Promise<T>) => T | Promise<T>;
|
|
76
|
+
};
|
|
63
77
|
};
|
|
64
78
|
}
|
|
65
79
|
/**
|
|
@@ -91,6 +105,10 @@ export declare class BunServer {
|
|
|
91
105
|
*/
|
|
92
106
|
use(handler: MiddlewareHandler): void;
|
|
93
107
|
use(path: string, handler: MiddlewareHandler): void;
|
|
108
|
+
/** Get auto-instrumentation helpers if tracing is enabled */
|
|
109
|
+
private get traceInstrumentors();
|
|
110
|
+
/** Generate a <script> tag to inject the trace ID into the page for client tracing */
|
|
111
|
+
private getTraceIdScript;
|
|
94
112
|
/**
|
|
95
113
|
* Register a WebSocket upgrade handler for a specific path.
|
|
96
114
|
* Plugins can use this to add WebSocket support alongside HMR.
|
package/dist/bun-server.d.ts.map
CHANGED
|
@@ -1 +1 @@
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;IACH,OAAO,IAAI;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAC;QAAC,WAAW,EAAE,OAAO,CAAA;KAAE;CAOpE;AAED;;GAEG;AACH,wBAAgB,YAAY,CAAC,OAAO,CAAC,EAAE,aAAa,GAAG,SAAS,CAE/D;AAED;;GAEG;AACH,wBAAsB,KAAK,CAAC,OAAO,CAAC,EAAE,aAAa,GAAG,OAAO,CAAC,SAAS,CAAC,CAIvE"}
|
package/dist/index.js
CHANGED
|
@@ -746,6 +746,21 @@ class BunServer {
|
|
|
746
746
|
this.middleware.use(pathOrHandler, maybeHandler);
|
|
747
747
|
}
|
|
748
748
|
}
|
|
749
|
+
get traceInstrumentors() {
|
|
750
|
+
if (this.options.trace && typeof this.options.trace === "object" && this.options.trace.instrumentors) {
|
|
751
|
+
return this.options.trace.instrumentors;
|
|
752
|
+
}
|
|
753
|
+
return null;
|
|
754
|
+
}
|
|
755
|
+
getTraceIdScript(context) {
|
|
756
|
+
const inst = this.traceInstrumentors;
|
|
757
|
+
if (!inst)
|
|
758
|
+
return "";
|
|
759
|
+
const span = inst.getActiveSpan(context);
|
|
760
|
+
if (!span?.traceId)
|
|
761
|
+
return "";
|
|
762
|
+
return `<script>window.__EREO_TRACE_ID__="${span.traceId}"</script>`;
|
|
763
|
+
}
|
|
749
764
|
addWebSocketUpgrade(path, upgrader, wsConfig) {
|
|
750
765
|
this.wsUpgradeHandlers.push({ path, upgrader, wsConfig });
|
|
751
766
|
}
|
|
@@ -788,7 +803,9 @@ class BunServer {
|
|
|
788
803
|
const pathname = new URL(request.url).pathname;
|
|
789
804
|
if (typeof this.router.getRoutes === "function") {
|
|
790
805
|
const routes = this.router.getRoutes();
|
|
791
|
-
const
|
|
806
|
+
const inst = this.traceInstrumentors;
|
|
807
|
+
const activeSpan = inst?.getActiveSpan(context);
|
|
808
|
+
const matchResult = activeSpan ? inst.traceRouteMatch(activeSpan, () => matchWithLayouts(pathname, routes)) : matchWithLayouts(pathname, routes);
|
|
792
809
|
if (!matchResult) {
|
|
793
810
|
return new Response("Not Found", { status: 404 });
|
|
794
811
|
}
|
|
@@ -942,10 +959,22 @@ class BunServer {
|
|
|
942
959
|
return new Response("Method Not Allowed", { status: 405 });
|
|
943
960
|
}
|
|
944
961
|
const loaderArgs = { request, params: match.params, context };
|
|
962
|
+
const inst = this.traceInstrumentors;
|
|
963
|
+
const activeSpan = inst?.getActiveSpan(context);
|
|
945
964
|
const loaderPromises = [];
|
|
946
|
-
|
|
965
|
+
if (module.loader) {
|
|
966
|
+
const loaderFn = () => module.loader(loaderArgs);
|
|
967
|
+
loaderPromises.push(activeSpan ? Promise.resolve(inst.traceLoader(activeSpan, match.route.id || "route", loaderFn)) : Promise.resolve(loaderFn()));
|
|
968
|
+
} else {
|
|
969
|
+
loaderPromises.push(Promise.resolve(null));
|
|
970
|
+
}
|
|
947
971
|
for (const layout of layouts) {
|
|
948
|
-
|
|
972
|
+
if (layout.module?.loader) {
|
|
973
|
+
const layoutLoaderFn = () => layout.module.loader(loaderArgs);
|
|
974
|
+
loaderPromises.push(activeSpan ? Promise.resolve(inst.traceLoader(activeSpan, `layout:${layout.id}`, layoutLoaderFn)) : Promise.resolve(layoutLoaderFn()));
|
|
975
|
+
} else {
|
|
976
|
+
loaderPromises.push(Promise.resolve(null));
|
|
977
|
+
}
|
|
949
978
|
}
|
|
950
979
|
let loaderResults;
|
|
951
980
|
try {
|
|
@@ -1005,13 +1034,27 @@ class BunServer {
|
|
|
1005
1034
|
});
|
|
1006
1035
|
return this.applyRouteHeaders(jsonResponse, routeHeaders);
|
|
1007
1036
|
}
|
|
1008
|
-
|
|
1037
|
+
let htmlResponse;
|
|
1038
|
+
if (activeSpan) {
|
|
1039
|
+
const renderSpan = activeSpan.child("render", "custom");
|
|
1040
|
+
try {
|
|
1041
|
+
htmlResponse = await this.renderPage(request, match, context, loaderData, layoutLoaderData);
|
|
1042
|
+
renderSpan.end();
|
|
1043
|
+
} catch (err) {
|
|
1044
|
+
renderSpan.error(err);
|
|
1045
|
+
renderSpan.end();
|
|
1046
|
+
throw err;
|
|
1047
|
+
}
|
|
1048
|
+
} else {
|
|
1049
|
+
htmlResponse = await this.renderPage(request, match, context, loaderData, layoutLoaderData);
|
|
1050
|
+
}
|
|
1009
1051
|
return this.applyRouteHeaders(htmlResponse, routeHeaders);
|
|
1010
1052
|
}
|
|
1011
1053
|
async renderPage(request, match, context, loaderData, layoutLoaderData = new Map, actionData) {
|
|
1054
|
+
const traceScript = this.getTraceIdScript(context);
|
|
1012
1055
|
const module = match.route.module;
|
|
1013
1056
|
if (!module?.default) {
|
|
1014
|
-
return this.renderMinimalPage(match, loaderData);
|
|
1057
|
+
return this.renderMinimalPage(match, loaderData, traceScript);
|
|
1015
1058
|
}
|
|
1016
1059
|
const url = new URL(request.url);
|
|
1017
1060
|
const metaDescriptors = this.buildMeta(module, loaderData, match.params, url);
|
|
@@ -1054,23 +1097,23 @@ class BunServer {
|
|
|
1054
1097
|
const hasRootLayout = layouts.length > 0 && layouts[0].module?.default;
|
|
1055
1098
|
if (hasRootLayout) {
|
|
1056
1099
|
if (this.options.renderMode === "streaming") {
|
|
1057
|
-
return this.renderStreamingPageDirect(element, allLoaderData, metaDescriptors);
|
|
1100
|
+
return this.renderStreamingPageDirect(element, allLoaderData, metaDescriptors, traceScript);
|
|
1058
1101
|
} else {
|
|
1059
|
-
return this.renderStringPageDirect(element, allLoaderData, metaDescriptors);
|
|
1102
|
+
return this.renderStringPageDirect(element, allLoaderData, metaDescriptors, traceScript);
|
|
1060
1103
|
}
|
|
1061
1104
|
}
|
|
1062
1105
|
if (this.options.renderMode === "streaming") {
|
|
1063
|
-
return this.renderStreamingPage(element, shell, allLoaderData);
|
|
1106
|
+
return this.renderStreamingPage(element, shell, allLoaderData, traceScript);
|
|
1064
1107
|
} else {
|
|
1065
|
-
return this.renderStringPage(element, shell, allLoaderData);
|
|
1108
|
+
return this.renderStringPage(element, shell, allLoaderData, traceScript);
|
|
1066
1109
|
}
|
|
1067
1110
|
}
|
|
1068
|
-
async renderStreamingPage(element, shell, loaderData) {
|
|
1111
|
+
async renderStreamingPage(element, shell, loaderData, traceScript = "") {
|
|
1069
1112
|
let timeoutId;
|
|
1070
1113
|
try {
|
|
1071
1114
|
const { renderToReadableStream } = await getStreamingRenderer();
|
|
1072
1115
|
if (!renderToReadableStream) {
|
|
1073
|
-
return this.renderStringPage(element, shell, loaderData);
|
|
1116
|
+
return this.renderStringPage(element, shell, loaderData, traceScript);
|
|
1074
1117
|
}
|
|
1075
1118
|
const hasDeferred = hasDeferredData2(loaderData);
|
|
1076
1119
|
const clientEntry = this.options.clientEntry;
|
|
@@ -1114,7 +1157,7 @@ class BunServer {
|
|
|
1114
1157
|
console.error("Deferred data resolution failed:", error);
|
|
1115
1158
|
resolvedData = null;
|
|
1116
1159
|
}
|
|
1117
|
-
const loaderScript =
|
|
1160
|
+
const loaderScript = `${traceScript}<script>window.__EREO_DATA__=${serializeLoaderData2(resolvedData)}</script>`;
|
|
1118
1161
|
const resolvedTail = `</div>
|
|
1119
1162
|
${loaderScript}
|
|
1120
1163
|
</body>
|
|
@@ -1147,17 +1190,17 @@ class BunServer {
|
|
|
1147
1190
|
} catch (error) {
|
|
1148
1191
|
clearTimeout(timeoutId);
|
|
1149
1192
|
console.error("Streaming render failed:", error);
|
|
1150
|
-
return this.renderStringPage(element, shell, loaderData);
|
|
1193
|
+
return this.renderStringPage(element, shell, loaderData, traceScript);
|
|
1151
1194
|
}
|
|
1152
1195
|
}
|
|
1153
|
-
async renderStringPage(element, shell, loaderData) {
|
|
1196
|
+
async renderStringPage(element, shell, loaderData, traceScript = "") {
|
|
1154
1197
|
try {
|
|
1155
1198
|
const { renderToString: reactRenderToString } = await import("react-dom/server");
|
|
1156
1199
|
const resolvedData = hasDeferredData2(loaderData) ? await resolveAllDeferred2(loaderData) : loaderData;
|
|
1157
1200
|
const scripts = [this.options.clientEntry];
|
|
1158
1201
|
const { head, tail } = createShell({ shell, scripts, loaderData: resolvedData });
|
|
1159
1202
|
const content = reactRenderToString(element);
|
|
1160
|
-
const html = head + content + tail;
|
|
1203
|
+
const html = head + content + (traceScript ? tail.replace("</body>", `${traceScript}</body>`) : tail);
|
|
1161
1204
|
const encoder = new TextEncoder;
|
|
1162
1205
|
const htmlBytes = encoder.encode(html);
|
|
1163
1206
|
return new Response(htmlBytes, {
|
|
@@ -1172,12 +1215,12 @@ class BunServer {
|
|
|
1172
1215
|
return this.renderErrorPage(error);
|
|
1173
1216
|
}
|
|
1174
1217
|
}
|
|
1175
|
-
async renderStreamingPageDirect(element, loaderData, metaDescriptors = []) {
|
|
1218
|
+
async renderStreamingPageDirect(element, loaderData, metaDescriptors = [], traceScript = "") {
|
|
1176
1219
|
let timeoutId;
|
|
1177
1220
|
try {
|
|
1178
1221
|
const { renderToReadableStream } = await getStreamingRenderer();
|
|
1179
1222
|
if (!renderToReadableStream) {
|
|
1180
|
-
return this.renderStringPageDirect(element, loaderData, metaDescriptors);
|
|
1223
|
+
return this.renderStringPageDirect(element, loaderData, metaDescriptors, traceScript);
|
|
1181
1224
|
}
|
|
1182
1225
|
const hasDeferred = hasDeferredData2(loaderData);
|
|
1183
1226
|
const clientEntry = this.options.clientEntry;
|
|
@@ -1191,7 +1234,7 @@ class BunServer {
|
|
|
1191
1234
|
console.error("Streaming render error:", error);
|
|
1192
1235
|
}
|
|
1193
1236
|
});
|
|
1194
|
-
const loaderScript = !hasDeferred && loaderData ? encoder.encode(
|
|
1237
|
+
const loaderScript = !hasDeferred && loaderData ? encoder.encode(`${traceScript}<script>window.__EREO_DATA__=${serializeLoaderData2(loaderData)}</script>`) : traceScript ? encoder.encode(traceScript) : null;
|
|
1195
1238
|
const metaHtml = metaDescriptors.length > 0 ? this.buildMetaHtml(metaDescriptors) : "";
|
|
1196
1239
|
let metaInjected = metaHtml.length === 0;
|
|
1197
1240
|
const decoder = new TextDecoder;
|
|
@@ -1215,7 +1258,7 @@ class BunServer {
|
|
|
1215
1258
|
console.error("Deferred data resolution failed:", error);
|
|
1216
1259
|
resolvedData = null;
|
|
1217
1260
|
}
|
|
1218
|
-
controller.enqueue(encoder.encode(
|
|
1261
|
+
controller.enqueue(encoder.encode(`${traceScript}<script>window.__EREO_DATA__=${serializeLoaderData2(resolvedData)}</script>`));
|
|
1219
1262
|
} else if (loaderScript) {
|
|
1220
1263
|
controller.enqueue(loaderScript);
|
|
1221
1264
|
}
|
|
@@ -1249,10 +1292,10 @@ class BunServer {
|
|
|
1249
1292
|
} catch (error) {
|
|
1250
1293
|
clearTimeout(timeoutId);
|
|
1251
1294
|
console.error("Streaming render failed:", error);
|
|
1252
|
-
return this.renderStringPageDirect(element, loaderData, metaDescriptors);
|
|
1295
|
+
return this.renderStringPageDirect(element, loaderData, metaDescriptors, traceScript);
|
|
1253
1296
|
}
|
|
1254
1297
|
}
|
|
1255
|
-
async renderStringPageDirect(element, loaderData, metaDescriptors = []) {
|
|
1298
|
+
async renderStringPageDirect(element, loaderData, metaDescriptors = [], traceScript = "") {
|
|
1256
1299
|
try {
|
|
1257
1300
|
const { renderToString: reactRenderToString } = await import("react-dom/server");
|
|
1258
1301
|
const resolvedData = hasDeferredData2(loaderData) ? await resolveAllDeferred2(loaderData) : loaderData;
|
|
@@ -1260,7 +1303,7 @@ class BunServer {
|
|
|
1260
1303
|
if (metaDescriptors.length > 0) {
|
|
1261
1304
|
html = this.injectMetaIntoHtml(html, metaDescriptors);
|
|
1262
1305
|
}
|
|
1263
|
-
const loaderScript = resolvedData ?
|
|
1306
|
+
const loaderScript = resolvedData ? `${traceScript}<script>window.__EREO_DATA__=${serializeLoaderData2(resolvedData)}</script>` : traceScript;
|
|
1264
1307
|
const clientScript = `<script type="module" src="${this.options.clientEntry}"></script>`;
|
|
1265
1308
|
if (html.includes("</body>")) {
|
|
1266
1309
|
html = html.replace("</body>", `${loaderScript}${clientScript}</body>`);
|
|
@@ -1281,7 +1324,7 @@ class BunServer {
|
|
|
1281
1324
|
return this.renderErrorPage(error);
|
|
1282
1325
|
}
|
|
1283
1326
|
}
|
|
1284
|
-
async renderMinimalPage(match, loaderData) {
|
|
1327
|
+
async renderMinimalPage(match, loaderData, traceScript = "") {
|
|
1285
1328
|
const resolvedData = hasDeferredData2(loaderData) ? await resolveAllDeferred2(loaderData) : loaderData;
|
|
1286
1329
|
const serializedData = serializeLoaderData2({
|
|
1287
1330
|
loaderData: resolvedData,
|
|
@@ -1296,7 +1339,7 @@ class BunServer {
|
|
|
1296
1339
|
</head>
|
|
1297
1340
|
<body>
|
|
1298
1341
|
<div id="root"></div>
|
|
1299
|
-
<script>window.__EREO_DATA__=${serializedData}</script>
|
|
1342
|
+
${traceScript}<script>window.__EREO_DATA__=${serializedData}</script>
|
|
1300
1343
|
<script type="module" src="${this.options.clientEntry}"></script>
|
|
1301
1344
|
</body>
|
|
1302
1345
|
</html>`;
|
|
@@ -1626,6 +1669,18 @@ class BunServer {
|
|
|
1626
1669
|
if (server.upgrade(request, { data: { _wsType: "hmr" } }))
|
|
1627
1670
|
return;
|
|
1628
1671
|
}
|
|
1672
|
+
if (url.pathname === "/__ereo/trace-ws" && typeof this.options.trace === "object" && this.options.trace.traceWebSocket) {
|
|
1673
|
+
const traceWsType = "__ereo_trace_ws";
|
|
1674
|
+
if (!upgradeHandlers.some((h) => h.path === traceWsType)) {
|
|
1675
|
+
upgradeHandlers.push({
|
|
1676
|
+
path: traceWsType,
|
|
1677
|
+
upgrader: () => true,
|
|
1678
|
+
wsConfig: this.options.trace.traceWebSocket.websocket
|
|
1679
|
+
});
|
|
1680
|
+
}
|
|
1681
|
+
if (server.upgrade(request, { data: { _wsType: traceWsType } }))
|
|
1682
|
+
return;
|
|
1683
|
+
}
|
|
1629
1684
|
const upgradeHeader = request.headers.get("Upgrade");
|
|
1630
1685
|
if (upgradeHeader?.toLowerCase() === "websocket") {
|
|
1631
1686
|
for (const handler of upgradeHandlers) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ereo/server",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.29",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "Ereo Team",
|
|
6
6
|
"homepage": "https://ereojs.github.io/ereoJS",
|
|
@@ -32,10 +32,10 @@
|
|
|
32
32
|
"typecheck": "tsc --noEmit"
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"@ereo/core": "^0.2.
|
|
36
|
-
"@ereo/client": "^0.2.
|
|
37
|
-
"@ereo/router": "^0.2.
|
|
38
|
-
"@ereo/data": "^0.2.
|
|
35
|
+
"@ereo/core": "^0.2.29",
|
|
36
|
+
"@ereo/client": "^0.2.29",
|
|
37
|
+
"@ereo/router": "^0.2.29",
|
|
38
|
+
"@ereo/data": "^0.2.29"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
41
|
"@types/bun": "^1.1.0",
|