next-ws 0.0.0-beta-20250823112013 → 0.0.0-beta-20250824054249
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/cli.cjs +1 -1
- package/dist/server/index.cjs +36 -72
- package/dist/server/index.d.ts +4 -6
- package/package.json +1 -1
package/dist/cli.cjs
CHANGED
|
@@ -27,7 +27,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
27
27
|
var import_minimist = __toESM(require("minimist"));
|
|
28
28
|
|
|
29
29
|
// package.json
|
|
30
|
-
var version = "0.0.0-beta-
|
|
30
|
+
var version = "0.0.0-beta-20250824054249";
|
|
31
31
|
|
|
32
32
|
// src/commands/helpers/define.ts
|
|
33
33
|
function defineCommandGroup(definition) {
|
package/dist/server/index.cjs
CHANGED
|
@@ -39,66 +39,35 @@ __export(server_exports, {
|
|
|
39
39
|
module.exports = __toCommonJS(server_exports);
|
|
40
40
|
|
|
41
41
|
// src/server/persistent.ts
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
);
|
|
58
|
-
} else if (!meta.isCustomServer) {
|
|
59
|
-
logger.warnOnce(
|
|
60
|
-
`[next-ws] The function '${callerName}' was called without a custom server.
|
|
61
|
-
This could lead to unintended behaviour, especially if you're attempting to interact with the WebSocket server outside of an UPGRADE handler.
|
|
62
|
-
Please note, while such configurations might function during development, they will fail in production. This is because Next.js employs a worker process for routing in production, which do not have access to the WebSocket server on the main process.
|
|
63
|
-
You can resolve this by using a custom server.`
|
|
64
|
-
);
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
var kHttpServer = Symbol.for("kHttpServer");
|
|
68
|
-
function getHttpServer() {
|
|
69
|
-
mainProcessOnly("getHttpServer");
|
|
70
|
-
return Reflect.get(globalThis, kHttpServer);
|
|
71
|
-
}
|
|
72
|
-
function setHttpServer(server) {
|
|
73
|
-
mainProcessOnly("setHttpServer");
|
|
74
|
-
Reflect.set(globalThis, kHttpServer, server);
|
|
75
|
-
return getHttpServer();
|
|
76
|
-
}
|
|
77
|
-
function useHttpServer(server) {
|
|
78
|
-
mainProcessOnly("useHttpServer");
|
|
79
|
-
const existing = getHttpServer();
|
|
80
|
-
if (existing) return existing;
|
|
81
|
-
return setHttpServer(server());
|
|
82
|
-
}
|
|
83
|
-
var kWebSocketServer = Symbol.for("kWebSocketServer");
|
|
84
|
-
function getWebSocketServer() {
|
|
85
|
-
mainProcessOnly("getWebSocketServer");
|
|
86
|
-
return Reflect.get(globalThis, kWebSocketServer);
|
|
87
|
-
}
|
|
88
|
-
function setWebSocketServer(server) {
|
|
89
|
-
mainProcessOnly("setWebSocketServer");
|
|
90
|
-
Reflect.set(globalThis, kWebSocketServer, server);
|
|
91
|
-
return getWebSocketServer();
|
|
92
|
-
}
|
|
93
|
-
function useWebSocketServer(server) {
|
|
94
|
-
mainProcessOnly("useWebSocketServer");
|
|
95
|
-
const existing = getWebSocketServer();
|
|
96
|
-
if (existing) return existing;
|
|
97
|
-
return setWebSocketServer(server());
|
|
42
|
+
function useGlobal(key) {
|
|
43
|
+
return [
|
|
44
|
+
function get() {
|
|
45
|
+
return Reflect.get(globalThis, key);
|
|
46
|
+
},
|
|
47
|
+
function set(value) {
|
|
48
|
+
return Reflect.set(globalThis, key, value);
|
|
49
|
+
},
|
|
50
|
+
function use(getter) {
|
|
51
|
+
const existing = Reflect.get(globalThis, key);
|
|
52
|
+
if (existing) return existing;
|
|
53
|
+
Reflect.set(globalThis, key, getter());
|
|
54
|
+
return Reflect.get(globalThis, key);
|
|
55
|
+
}
|
|
56
|
+
];
|
|
98
57
|
}
|
|
58
|
+
var [getHttpServer, setHttpServer, useHttpServer] = (
|
|
59
|
+
//
|
|
60
|
+
useGlobal(
|
|
61
|
+
Symbol.for("next-ws.http-server")
|
|
62
|
+
//
|
|
63
|
+
)
|
|
64
|
+
);
|
|
65
|
+
var [getWebSocketServer, setWebSocketServer, useWebSocketServer] = useGlobal(
|
|
66
|
+
Symbol.for("next-ws.websocket-server")
|
|
67
|
+
);
|
|
99
68
|
|
|
100
69
|
// src/server/setup.ts
|
|
101
|
-
var
|
|
70
|
+
var logger2 = __toESM(require("next/dist/build/output/log.js"));
|
|
102
71
|
var import_ws = require("ws");
|
|
103
72
|
|
|
104
73
|
// src/server/helpers/match.ts
|
|
@@ -142,7 +111,7 @@ function findMatchingRoute(nextServer, requestPathname) {
|
|
|
142
111
|
}
|
|
143
112
|
|
|
144
113
|
// src/server/helpers/module.ts
|
|
145
|
-
var
|
|
114
|
+
var logger = __toESM(require("next/dist/build/output/log.js"));
|
|
146
115
|
async function importRouteModule(nextServer, filePath) {
|
|
147
116
|
try {
|
|
148
117
|
if ("hotReloader" in nextServer) {
|
|
@@ -153,7 +122,7 @@ async function importRouteModule(nextServer, filePath) {
|
|
|
153
122
|
} else if ("ensurePage" in nextServer) {
|
|
154
123
|
await nextServer.ensurePage({ page: filePath, clientOnly: false });
|
|
155
124
|
} else {
|
|
156
|
-
|
|
125
|
+
logger.warnOnce(
|
|
157
126
|
"[next-ws] unable to ensure page, you may need to open the route in your browser first so Next.js compiles it"
|
|
158
127
|
);
|
|
159
128
|
}
|
|
@@ -187,24 +156,19 @@ function toNextRequest(message) {
|
|
|
187
156
|
|
|
188
157
|
// src/server/setup.ts
|
|
189
158
|
function setupWebSocketServer(nextServer) {
|
|
190
|
-
process.env.NEXT_WS_MAIN_PROCESS = String(1);
|
|
191
|
-
process.env.NEXT_WS_SKIP_ENVIRONMENT_CHECK = String(1);
|
|
192
159
|
const httpServer = (
|
|
193
160
|
//
|
|
194
161
|
// @ts-expect-error - serverOptions is protected
|
|
195
162
|
useHttpServer(() => nextServer.serverOptions?.httpServer)
|
|
196
163
|
);
|
|
197
164
|
if (!httpServer)
|
|
198
|
-
return
|
|
165
|
+
return logger2.error("[next-ws] was not able to find the HTTP server");
|
|
199
166
|
const wsServer = (
|
|
200
167
|
//
|
|
201
168
|
useWebSocketServer(() => new import_ws.WebSocketServer({ noServer: true }))
|
|
202
169
|
);
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
process.env.NEXT_WS_SKIP_ENVIRONMENT_CHECK = String(0);
|
|
206
|
-
logger3.ready("[next-ws] has started the WebSocket server");
|
|
207
|
-
const kInstalled = Symbol.for("kInstalled");
|
|
170
|
+
logger2.ready("[next-ws] has started the WebSocket server");
|
|
171
|
+
const kInstalled = Symbol.for("next-ws.http-server.attached");
|
|
208
172
|
if (Reflect.has(httpServer, kInstalled)) return;
|
|
209
173
|
Reflect.set(httpServer, kInstalled, true);
|
|
210
174
|
httpServer.on("upgrade", async (message, socket, head) => {
|
|
@@ -213,22 +177,22 @@ function setupWebSocketServer(nextServer) {
|
|
|
213
177
|
if (pathname.includes("/_next")) return;
|
|
214
178
|
const route = findMatchingRoute(nextServer, pathname);
|
|
215
179
|
if (!route) {
|
|
216
|
-
|
|
180
|
+
logger2.error(`[next-ws] could not find route for page ${pathname}`);
|
|
217
181
|
return socket.end();
|
|
218
182
|
}
|
|
219
183
|
const module2 = await importRouteModule(nextServer, route.filename);
|
|
220
184
|
if (!module2) {
|
|
221
|
-
|
|
185
|
+
logger2.error(`[next-ws] could not import module for page ${pathname}`);
|
|
222
186
|
return socket.end();
|
|
223
187
|
}
|
|
224
188
|
const handleUpgrade = module2.userland.UPGRADE;
|
|
225
189
|
const handleSocket = module2.userland.SOCKET;
|
|
226
190
|
if ((!handleUpgrade || typeof handleUpgrade !== "function") && (!handleSocket || typeof handleSocket !== "function")) {
|
|
227
|
-
|
|
191
|
+
logger2.error(`[next-ws] route '${pathname}' does not export a handler`);
|
|
228
192
|
return socket.end();
|
|
229
193
|
}
|
|
230
194
|
if (handleSocket)
|
|
231
|
-
|
|
195
|
+
logger2.warnOnce(
|
|
232
196
|
"DeprecationWarning: [next-ws] SOCKET is deprecated, use UPGRADE instead, see https://github.com/apteryxxyz/next-ws#-usage"
|
|
233
197
|
);
|
|
234
198
|
wsServer.handleUpgrade(message, socket, head, async (client) => {
|
|
@@ -246,7 +210,7 @@ function setupWebSocketServer(nextServer) {
|
|
|
246
210
|
client.once("close", () => handleClose());
|
|
247
211
|
}
|
|
248
212
|
} catch (cause) {
|
|
249
|
-
|
|
213
|
+
logger2.error(
|
|
250
214
|
`[next-ws] error in socket handler for '${pathname}'`,
|
|
251
215
|
cause
|
|
252
216
|
);
|
package/dist/server/index.d.ts
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import * as next_server from 'next/server';
|
|
2
2
|
import * as http from 'http';
|
|
3
3
|
import * as ws from 'ws';
|
|
4
|
-
import { WebSocketServer } from 'ws';
|
|
5
|
-
import { Server } from 'node:http';
|
|
6
4
|
import NextNodeServer from 'next/dist/server/next-server.js';
|
|
7
5
|
|
|
8
6
|
/** @deprecated Prefer UPGRADE and {@link UpgradeHandler} */
|
|
@@ -22,10 +20,10 @@ type RouteContext<Path extends string> = {
|
|
|
22
20
|
params: Record<string, string | string[] | undefined> & RouteParams<Path> & RouteParams<Path>;
|
|
23
21
|
};
|
|
24
22
|
|
|
25
|
-
declare
|
|
26
|
-
declare
|
|
27
|
-
declare
|
|
28
|
-
declare
|
|
23
|
+
declare const getHttpServer: () => http.Server<typeof http.IncomingMessage, typeof http.ServerResponse> | undefined;
|
|
24
|
+
declare const setHttpServer: (value: http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>) => boolean;
|
|
25
|
+
declare const getWebSocketServer: () => ws.WebSocketServer | undefined;
|
|
26
|
+
declare const setWebSocketServer: (value: ws.WebSocketServer) => boolean;
|
|
29
27
|
|
|
30
28
|
declare function setupWebSocketServer(nextServer: NextNodeServer): void;
|
|
31
29
|
|