@dangao/bun-server 3.0.2 → 3.0.4
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/index.js +11 -1
- package/dist/index.node.mjs +11 -1
- package/package.json +7 -4
- package/src/platform/node/http.ts +23 -2
package/dist/index.js
CHANGED
|
@@ -12697,7 +12697,17 @@ function sendWebResponse(res, webResponse) {
|
|
|
12697
12697
|
pump();
|
|
12698
12698
|
}
|
|
12699
12699
|
function setupWebSocket(httpServer, handlers) {
|
|
12700
|
-
|
|
12700
|
+
let wsModule;
|
|
12701
|
+
try {
|
|
12702
|
+
wsModule = __require("ws");
|
|
12703
|
+
} catch {
|
|
12704
|
+
throw new Error(`[bun-server] WebSocket on Node.js requires the ws package.
|
|
12705
|
+
` + "Install it with: bun add ws");
|
|
12706
|
+
}
|
|
12707
|
+
const WebSocketServer = wsModule.WebSocketServer ?? wsModule.default?.WebSocketServer ?? wsModule.Server;
|
|
12708
|
+
if (!WebSocketServer) {
|
|
12709
|
+
throw new Error("[bun-server] Could not find WebSocketServer in the ws module. " + "Please ensure ws >= 7.5.0 is installed.");
|
|
12710
|
+
}
|
|
12701
12711
|
const wss = new WebSocketServer({ noServer: true });
|
|
12702
12712
|
httpServer.on("upgrade", (request, socket, head) => {
|
|
12703
12713
|
wss.handleUpgrade(request, socket, head, (ws) => {
|
package/dist/index.node.mjs
CHANGED
|
@@ -658,7 +658,17 @@ function sendWebResponse(res, webResponse) {
|
|
|
658
658
|
pump();
|
|
659
659
|
}
|
|
660
660
|
function setupWebSocket(httpServer, handlers) {
|
|
661
|
-
|
|
661
|
+
let wsModule;
|
|
662
|
+
try {
|
|
663
|
+
wsModule = __require("ws");
|
|
664
|
+
} catch {
|
|
665
|
+
throw new Error(`[bun-server] WebSocket on Node.js requires the ws package.
|
|
666
|
+
` + "Install it with: bun add ws");
|
|
667
|
+
}
|
|
668
|
+
const WebSocketServer = wsModule.WebSocketServer ?? wsModule.default?.WebSocketServer ?? wsModule.Server;
|
|
669
|
+
if (!WebSocketServer) {
|
|
670
|
+
throw new Error("[bun-server] Could not find WebSocketServer in the ws module. " + "Please ensure ws >= 7.5.0 is installed.");
|
|
671
|
+
}
|
|
662
672
|
const wss = new WebSocketServer({ noServer: true });
|
|
663
673
|
httpServer.on("upgrade", (request, socket, head) => {
|
|
664
674
|
wss.handleUpgrade(request, socket, head, (ws) => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dangao/bun-server",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.4",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -66,11 +66,15 @@
|
|
|
66
66
|
"vitest": "^4.1.4"
|
|
67
67
|
},
|
|
68
68
|
"peerDependencies": {
|
|
69
|
-
"@vscode/sqlite3": "5.1.12-vscode"
|
|
69
|
+
"@vscode/sqlite3": "5.1.12-vscode",
|
|
70
|
+
"ws": "^8.0.0"
|
|
70
71
|
},
|
|
71
72
|
"peerDependenciesMeta": {
|
|
72
73
|
"@vscode/sqlite3": {
|
|
73
74
|
"optional": true
|
|
75
|
+
},
|
|
76
|
+
"ws": {
|
|
77
|
+
"optional": true
|
|
74
78
|
}
|
|
75
79
|
},
|
|
76
80
|
"dependencies": {
|
|
@@ -82,7 +86,6 @@
|
|
|
82
86
|
"mime-types": "^3.0.2",
|
|
83
87
|
"mysql2": "^3.21.0",
|
|
84
88
|
"postgres": "^3.4.9",
|
|
85
|
-
"reflect-metadata": "^0.2.2"
|
|
86
|
-
"ws": "^8.20.0"
|
|
89
|
+
"reflect-metadata": "^0.2.2"
|
|
87
90
|
}
|
|
88
91
|
}
|
|
@@ -168,8 +168,29 @@ function setupWebSocket<T>(
|
|
|
168
168
|
httpServer: import('node:http').Server,
|
|
169
169
|
handlers: WebSocketHandlers<T>,
|
|
170
170
|
): void {
|
|
171
|
-
|
|
172
|
-
|
|
171
|
+
let wsModule: typeof import('ws');
|
|
172
|
+
try {
|
|
173
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
174
|
+
wsModule = require('ws');
|
|
175
|
+
} catch {
|
|
176
|
+
throw new Error(
|
|
177
|
+
'[bun-server] WebSocket on Node.js requires the ws package.\n' +
|
|
178
|
+
'Install it with: bun add ws',
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
// Handle CJS (WebSocketServer as class property) and ESM interop (named export)
|
|
182
|
+
// Also falls back to .Server for ws < 7.5.0
|
|
183
|
+
const WebSocketServer =
|
|
184
|
+
(wsModule as unknown as Record<string, unknown>).WebSocketServer ??
|
|
185
|
+
(wsModule as unknown as { default?: Record<string, unknown> }).default?.WebSocketServer ??
|
|
186
|
+
(wsModule as unknown as Record<string, unknown>).Server;
|
|
187
|
+
if (!WebSocketServer) {
|
|
188
|
+
throw new Error(
|
|
189
|
+
'[bun-server] Could not find WebSocketServer in the ws module. ' +
|
|
190
|
+
'Please ensure ws >= 7.5.0 is installed.',
|
|
191
|
+
);
|
|
192
|
+
}
|
|
193
|
+
const wss = new (WebSocketServer as new (opts: unknown) => import('ws').WebSocketServer)({ noServer: true });
|
|
173
194
|
|
|
174
195
|
httpServer.on('upgrade', (request, socket, head) => {
|
|
175
196
|
wss.handleUpgrade(request, socket, head, (ws) => {
|