@ereo/server 0.2.16 → 0.2.18
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 +6 -0
- package/dist/bun-server.d.ts.map +1 -1
- package/dist/index.js +63 -7
- package/package.json +5 -5
package/dist/bun-server.d.ts
CHANGED
|
@@ -72,6 +72,7 @@ export declare class BunServer {
|
|
|
72
72
|
private middleware;
|
|
73
73
|
private staticHandler;
|
|
74
74
|
private options;
|
|
75
|
+
private wsUpgradeHandlers;
|
|
75
76
|
constructor(options?: ServerOptions);
|
|
76
77
|
/**
|
|
77
78
|
* Setup default middleware.
|
|
@@ -90,6 +91,11 @@ export declare class BunServer {
|
|
|
90
91
|
*/
|
|
91
92
|
use(handler: MiddlewareHandler): void;
|
|
92
93
|
use(path: string, handler: MiddlewareHandler): void;
|
|
94
|
+
/**
|
|
95
|
+
* Register a WebSocket upgrade handler for a specific path.
|
|
96
|
+
* Plugins can use this to add WebSocket support alongside HMR.
|
|
97
|
+
*/
|
|
98
|
+
addWebSocketUpgrade(path: string, upgrader: (server: any, request: Request) => boolean, wsConfig: any): void;
|
|
93
99
|
/**
|
|
94
100
|
* Handle incoming request.
|
|
95
101
|
*/
|
package/dist/bun-server.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bun-server.d.ts","sourceRoot":"","sources":["../src/bun-server.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,KAAK,CAAC;AAClC,OAAO,KAAK,EAAmE,iBAAiB,EAA0C,MAAM,YAAY,CAAC;AAC7J,OAAO,EAAiC,OAAO,EAAiB,MAAM,YAAY,CAAC;AACnF,OAAO,EAAE,UAAU,EAAwD,MAAM,cAAc,CAAC;AAChG,OAAO,EAIL,IAAI,EACJ,eAAe,EAChB,MAAM,cAAc,CAAC;AACtB,OAAO,EAAe,KAAK,aAAa,EAAE,MAA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gB;IA8BxB;;;;OAIG;IACH,OAAO,CAAC,iBAAiB;IA4CzB;;OAEG;IACH,OAAO,CAAC,iBAAiB;IAqBzB;;OAEG;IACH,OAAO,CAAC,UAAU;IASlB;;;OAGG;YACW,iBAAiB;IAmD/B;;OAEG;YACW,uBAAuB;IA2DrC;;OAEG;IACH,OAAO,CAAC,WAAW;IA0CnB;;OAEG;IACG,KAAK,IAAI,OAAO,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;IAgGvC;;OAEG;IACH,IAAI,IAAI,IAAI;IAOZ;;OAEG;IACG,MAAM,IAAI,OAAO,CAAC,IAAI,CAAC;IAQ7B;;OAEG;IACH,SAAS,IAAI,MAAM,CAAC,OAAO,CAAC,GAAG,IAAI;IAInC;;OAEG;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
|
@@ -696,6 +696,7 @@ class BunServer {
|
|
|
696
696
|
middleware;
|
|
697
697
|
staticHandler = null;
|
|
698
698
|
options;
|
|
699
|
+
wsUpgradeHandlers = [];
|
|
699
700
|
constructor(options = {}) {
|
|
700
701
|
this.options = {
|
|
701
702
|
port: 3000,
|
|
@@ -745,6 +746,9 @@ class BunServer {
|
|
|
745
746
|
this.middleware.use(pathOrHandler, maybeHandler);
|
|
746
747
|
}
|
|
747
748
|
}
|
|
749
|
+
addWebSocketUpgrade(path, upgrader, wsConfig) {
|
|
750
|
+
this.wsUpgradeHandlers.push({ path, upgrader, wsConfig });
|
|
751
|
+
}
|
|
748
752
|
async handleRequest(request) {
|
|
749
753
|
const context = createContext(request);
|
|
750
754
|
try {
|
|
@@ -1576,15 +1580,67 @@ class BunServer {
|
|
|
1576
1580
|
}
|
|
1577
1581
|
async start() {
|
|
1578
1582
|
const { port, hostname, tls, websocket } = this.options;
|
|
1583
|
+
const pluginUpgradeHandlers = this.wsUpgradeHandlers;
|
|
1584
|
+
const hasPluginWs = pluginUpgradeHandlers.length > 0;
|
|
1585
|
+
let mergedWebSocket = websocket || undefined;
|
|
1586
|
+
if (hasPluginWs && websocket) {
|
|
1587
|
+
mergedWebSocket = {
|
|
1588
|
+
message(ws, message) {
|
|
1589
|
+
const handler = ws.data?._wsType === "hmr" ? websocket : pluginUpgradeHandlers.find((h) => h.path === ws.data?._wsType)?.wsConfig;
|
|
1590
|
+
handler?.message?.(ws, message);
|
|
1591
|
+
},
|
|
1592
|
+
open(ws) {
|
|
1593
|
+
const handler = ws.data?._wsType === "hmr" ? websocket : pluginUpgradeHandlers.find((h) => h.path === ws.data?._wsType)?.wsConfig;
|
|
1594
|
+
handler?.open?.(ws);
|
|
1595
|
+
},
|
|
1596
|
+
close(ws) {
|
|
1597
|
+
const handler = ws.data?._wsType === "hmr" ? websocket : pluginUpgradeHandlers.find((h) => h.path === ws.data?._wsType)?.wsConfig;
|
|
1598
|
+
handler?.close?.(ws);
|
|
1599
|
+
},
|
|
1600
|
+
drain(ws) {
|
|
1601
|
+
const handler = ws.data?._wsType === "hmr" ? websocket : pluginUpgradeHandlers.find((h) => h.path === ws.data?._wsType)?.wsConfig;
|
|
1602
|
+
handler?.drain?.(ws);
|
|
1603
|
+
}
|
|
1604
|
+
};
|
|
1605
|
+
} else if (hasPluginWs) {
|
|
1606
|
+
mergedWebSocket = {
|
|
1607
|
+
message(ws, message) {
|
|
1608
|
+
const handler = pluginUpgradeHandlers.find((h) => h.path === ws.data?._wsType)?.wsConfig;
|
|
1609
|
+
handler?.message?.(ws, message);
|
|
1610
|
+
},
|
|
1611
|
+
open(ws) {
|
|
1612
|
+
const handler = pluginUpgradeHandlers.find((h) => h.path === ws.data?._wsType)?.wsConfig;
|
|
1613
|
+
handler?.open?.(ws);
|
|
1614
|
+
},
|
|
1615
|
+
close(ws) {
|
|
1616
|
+
const handler = pluginUpgradeHandlers.find((h) => h.path === ws.data?._wsType)?.wsConfig;
|
|
1617
|
+
handler?.close?.(ws);
|
|
1618
|
+
},
|
|
1619
|
+
drain(ws) {
|
|
1620
|
+
const handler = pluginUpgradeHandlers.find((h) => h.path === ws.data?._wsType)?.wsConfig;
|
|
1621
|
+
handler?.drain?.(ws);
|
|
1622
|
+
}
|
|
1623
|
+
};
|
|
1624
|
+
}
|
|
1579
1625
|
const serverOptions = {
|
|
1580
1626
|
port,
|
|
1581
1627
|
hostname,
|
|
1582
1628
|
fetch: (request, server) => {
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
if (
|
|
1586
|
-
|
|
1587
|
-
|
|
1629
|
+
const url = new URL(request.url);
|
|
1630
|
+
if (websocket && url.pathname === "/__hmr") {
|
|
1631
|
+
if (server.upgrade(request, { data: { _wsType: "hmr" } }))
|
|
1632
|
+
return;
|
|
1633
|
+
}
|
|
1634
|
+
if (hasPluginWs) {
|
|
1635
|
+
const upgradeHeader = request.headers.get("Upgrade");
|
|
1636
|
+
if (upgradeHeader?.toLowerCase() === "websocket") {
|
|
1637
|
+
for (const handler of pluginUpgradeHandlers) {
|
|
1638
|
+
if (url.pathname === handler.path) {
|
|
1639
|
+
const data = { _wsType: handler.path, subscriptions: new Map, ctx: {}, originalRequest: request };
|
|
1640
|
+
if (server.upgrade(request, { data }))
|
|
1641
|
+
return;
|
|
1642
|
+
}
|
|
1643
|
+
}
|
|
1588
1644
|
}
|
|
1589
1645
|
}
|
|
1590
1646
|
return this.handleRequest(request);
|
|
@@ -1594,8 +1650,8 @@ class BunServer {
|
|
|
1594
1650
|
if (tls) {
|
|
1595
1651
|
serverOptions.tls = tls;
|
|
1596
1652
|
}
|
|
1597
|
-
if (
|
|
1598
|
-
serverOptions.websocket =
|
|
1653
|
+
if (mergedWebSocket) {
|
|
1654
|
+
serverOptions.websocket = mergedWebSocket;
|
|
1599
1655
|
}
|
|
1600
1656
|
this.server = Bun.serve(serverOptions);
|
|
1601
1657
|
const protocol = tls ? "https" : "http";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ereo/server",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.18",
|
|
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.18",
|
|
36
|
+
"@ereo/client": "^0.2.18",
|
|
37
|
+
"@ereo/router": "^0.2.18",
|
|
38
|
+
"@ereo/data": "^0.2.18"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
41
|
"@types/bun": "^1.1.0",
|