next-ws 1.2.0 → 2.0.1
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/README.md +100 -0
- package/dist/chunk-3RG5ZIWI.js +10 -0
- package/dist/cli.cjs +33 -0
- package/dist/client/index.cjs +64 -26
- package/dist/client/index.js +26 -17
- package/dist/server/index.cjs +157 -106
- package/dist/server/index.d.cts +2 -8
- package/dist/server/index.d.ts +2 -8
- package/dist/server/index.js +120 -78
- package/package.json +38 -15
- package/dist/chunk-PFW3KWBF.cjs +0 -14
- package/dist/chunk-PFW3KWBF.cjs.map +0 -1
- package/dist/chunk-WO25ABG2.js +0 -11
- package/dist/chunk-WO25ABG2.js.map +0 -1
- package/dist/client/index.cjs.map +0 -1
- package/dist/client/index.js.map +0 -1
- package/dist/server/index.cjs.map +0 -1
- package/dist/server/index.js.map +0 -1
- package/readme.md +0 -246
package/dist/server/index.cjs
CHANGED
|
@@ -1,167 +1,218 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
var
|
|
4
|
-
var
|
|
5
|
-
var
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
var
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
});
|
|
18
|
-
}
|
|
19
|
-
});
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
20
17
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
24
29
|
|
|
25
|
-
|
|
30
|
+
// src/server/index.ts
|
|
31
|
+
var server_exports = {};
|
|
32
|
+
__export(server_exports, {
|
|
33
|
+
getHttpServer: () => getHttpServer,
|
|
34
|
+
getWebSocketServer: () => getWebSocketServer,
|
|
35
|
+
setHttpServer: () => setHttpServer,
|
|
36
|
+
setWebSocketServer: () => setWebSocketServer,
|
|
37
|
+
setupWebSocketServer: () => setupWebSocketServer
|
|
38
|
+
});
|
|
39
|
+
module.exports = __toCommonJS(server_exports);
|
|
40
|
+
|
|
41
|
+
// src/server/setup.ts
|
|
42
|
+
var logger3 = __toESM(require("next/dist/build/output/log.js"), 1);
|
|
43
|
+
var import_ws = require("ws");
|
|
26
44
|
|
|
45
|
+
// src/server/helpers/persistent.ts
|
|
46
|
+
var logger = __toESM(require("next/dist/build/output/log.js"), 1);
|
|
27
47
|
function getEnvironmentMeta() {
|
|
28
|
-
|
|
48
|
+
const isCustomServer = !process.title.startsWith("next-");
|
|
49
|
+
const isMainProcess = process.env.NEXT_WS_MAIN_PROCESS === "1";
|
|
50
|
+
const isDevelopment = process.env.NODE_ENV === "development";
|
|
29
51
|
return { isCustomServer, isMainProcess, isDevelopment };
|
|
30
52
|
}
|
|
31
|
-
chunkPFW3KWBF_cjs.__name(getEnvironmentMeta, "getEnvironmentMeta");
|
|
32
53
|
function mainProcessOnly(fnName) {
|
|
33
54
|
if (process.env.NEXT_WS_SKIP_ENVIRONMENT_CHECK === "1") return;
|
|
34
|
-
|
|
35
|
-
if (meta.isMainProcess)
|
|
36
|
-
|
|
55
|
+
const meta = getEnvironmentMeta();
|
|
56
|
+
if (!meta.isMainProcess) {
|
|
57
|
+
throw new Error(
|
|
58
|
+
`[next-ws] Attempt to invoke '${fnName}' outside the main process.
|
|
59
|
+
You may be attempting to interact with the WebSocket server outside of a SOCKET handler. This will fail in production, as Next.js employs a worker process for routing, which do not have access to the WebSocket server on the main process.
|
|
60
|
+
You can resolve this by using a custom server.`
|
|
61
|
+
);
|
|
62
|
+
} else if (!meta.isCustomServer) {
|
|
63
|
+
logger.warnOnce(
|
|
37
64
|
`[next-ws] Caution: The function '${fnName}' was invoked without a custom server.
|
|
38
65
|
This could lead to unintended behaviour, especially if you're attempting to interact with the WebSocket server outside of a SOCKET handler.
|
|
39
66
|
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.
|
|
40
67
|
You can resolve this by using a custom server.`
|
|
41
68
|
);
|
|
42
|
-
|
|
43
|
-
`[next-ws] Attempt to invoke '${fnName}' outside the main process.
|
|
44
|
-
You may be attempting to interact with the WebSocket server outside of a SOCKET handler. This will fail in production, as Next.js employs a worker process for routing, which do not have access to the WebSocket server on the main process.
|
|
45
|
-
You can resolve this by using a custom server.`
|
|
46
|
-
);
|
|
69
|
+
}
|
|
47
70
|
}
|
|
48
|
-
chunkPFW3KWBF_cjs.__name(mainProcessOnly, "mainProcessOnly");
|
|
49
71
|
var NextWsHttpServer = Symbol.for("NextWs_HttpServer");
|
|
50
72
|
function setHttpServer(server) {
|
|
51
73
|
Reflect.set(globalThis, NextWsHttpServer, server);
|
|
52
74
|
}
|
|
53
|
-
chunkPFW3KWBF_cjs.__name(setHttpServer, "setHttpServer");
|
|
54
75
|
function getHttpServer() {
|
|
55
|
-
|
|
76
|
+
mainProcessOnly("getHttpServer");
|
|
77
|
+
return Reflect.get(globalThis, NextWsHttpServer);
|
|
56
78
|
}
|
|
57
|
-
chunkPFW3KWBF_cjs.__name(getHttpServer, "getHttpServer");
|
|
58
79
|
function useHttpServer(server) {
|
|
59
|
-
|
|
60
|
-
|
|
80
|
+
const existing = getHttpServer();
|
|
81
|
+
if (existing) return existing;
|
|
82
|
+
if (server) setHttpServer(server);
|
|
83
|
+
return server;
|
|
61
84
|
}
|
|
62
|
-
chunkPFW3KWBF_cjs.__name(useHttpServer, "useHttpServer");
|
|
63
85
|
var NextWsWebSocketServer = Symbol.for("NextWs_WebSocketServer");
|
|
64
86
|
function setWebSocketServer(wsServer) {
|
|
65
87
|
Reflect.set(globalThis, NextWsWebSocketServer, wsServer);
|
|
66
88
|
}
|
|
67
|
-
chunkPFW3KWBF_cjs.__name(setWebSocketServer, "setWebSocketServer");
|
|
68
89
|
function getWebSocketServer() {
|
|
69
|
-
|
|
90
|
+
mainProcessOnly("getWebSocketServer");
|
|
91
|
+
return Reflect.get(globalThis, NextWsWebSocketServer);
|
|
70
92
|
}
|
|
71
|
-
chunkPFW3KWBF_cjs.__name(getWebSocketServer, "getWebSocketServer");
|
|
72
93
|
function useWebSocketServer(wsServer) {
|
|
73
|
-
|
|
74
|
-
|
|
94
|
+
const existing = getWebSocketServer();
|
|
95
|
+
if (existing) return existing;
|
|
96
|
+
if (wsServer) setWebSocketServer(wsServer);
|
|
97
|
+
return wsServer;
|
|
75
98
|
}
|
|
76
|
-
|
|
99
|
+
|
|
100
|
+
// src/server/helpers/route.ts
|
|
101
|
+
var import_node_url = require("url");
|
|
102
|
+
var logger2 = __toESM(require("next/dist/build/output/log.js"), 1);
|
|
77
103
|
function createRouteRegex(routePattern) {
|
|
78
|
-
|
|
104
|
+
const escapedPattern = routePattern.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&");
|
|
105
|
+
const paramRegex = escapedPattern.replace(/\\\[([a-zA-Z0-9_]+)\\\]/g, "(?<$1>[^/]+)").replace(/\\\[(?:\\\.){3}([a-zA-Z0-9_]+)\\\]/g, "(?<rest_$1>.+)");
|
|
79
106
|
return new RegExp(`^${paramRegex}$`);
|
|
80
107
|
}
|
|
81
|
-
chunkPFW3KWBF_cjs.__name(createRouteRegex, "createRouteRegex");
|
|
82
108
|
function getRouteParams(routePattern, routePath) {
|
|
83
|
-
|
|
109
|
+
const routeRegex = createRouteRegex(routePattern);
|
|
110
|
+
const match = routePath.match(routeRegex);
|
|
84
111
|
if (!match) return null;
|
|
85
112
|
if (!match.groups) return {};
|
|
86
|
-
|
|
87
|
-
for (let [k, v] of Object.entries(match.groups))
|
|
88
|
-
k.startsWith("rest_")
|
|
113
|
+
const params = {};
|
|
114
|
+
for (let [k, v] of Object.entries(match.groups)) {
|
|
115
|
+
if (k.startsWith("rest_")) {
|
|
116
|
+
k = k.slice(5);
|
|
117
|
+
v = v.split("/");
|
|
118
|
+
}
|
|
119
|
+
Reflect.set(params, k, v);
|
|
120
|
+
}
|
|
89
121
|
return params;
|
|
90
122
|
}
|
|
91
|
-
chunkPFW3KWBF_cjs.__name(getRouteParams, "getRouteParams");
|
|
92
123
|
function resolvePathToRoute(nextServer, requestPath) {
|
|
93
|
-
|
|
124
|
+
const routes = {
|
|
94
125
|
// @ts-expect-error - appPathRoutes is protected
|
|
95
126
|
...nextServer.appPathRoutes,
|
|
96
127
|
// @ts-expect-error - getAppPathRoutes is protected
|
|
97
128
|
...nextServer.getAppPathRoutes()
|
|
98
129
|
};
|
|
99
|
-
for (
|
|
100
|
-
|
|
130
|
+
for (const [routePath, [filePath]] of Object.entries(routes)) {
|
|
131
|
+
const routeParams = getRouteParams(routePath, requestPath);
|
|
101
132
|
if (routeParams) return { filePath, routeParams };
|
|
102
133
|
}
|
|
103
134
|
return null;
|
|
104
135
|
}
|
|
105
|
-
chunkPFW3KWBF_cjs.__name(resolvePathToRoute, "resolvePathToRoute");
|
|
106
136
|
async function importRouteModule(nextServer, filePath) {
|
|
107
137
|
try {
|
|
108
|
-
"hotReloader" in nextServer
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
)
|
|
138
|
+
if ("hotReloader" in nextServer) {
|
|
139
|
+
await nextServer.hotReloader?.ensurePage({
|
|
140
|
+
page: filePath,
|
|
141
|
+
clientOnly: false
|
|
142
|
+
});
|
|
143
|
+
} else if ("ensurePage" in nextServer) {
|
|
144
|
+
await nextServer.ensurePage({ page: filePath, clientOnly: false });
|
|
145
|
+
} else {
|
|
146
|
+
logger2.warnOnce(
|
|
147
|
+
"[next-ws] unable to ensure page, you may need to open the route in your browser first so Next.js compiles it"
|
|
148
|
+
);
|
|
149
|
+
}
|
|
114
150
|
} catch {
|
|
115
151
|
}
|
|
116
|
-
|
|
117
|
-
return
|
|
152
|
+
const buildPagePath = nextServer.getPagePath(filePath);
|
|
153
|
+
return importModule(buildPagePath);
|
|
154
|
+
}
|
|
155
|
+
async function importModule(modulePath) {
|
|
156
|
+
const moduleUrl = (0, import_node_url.pathToFileURL)(modulePath).toString();
|
|
157
|
+
try {
|
|
158
|
+
return import(moduleUrl);
|
|
159
|
+
} catch (requireError) {
|
|
160
|
+
try {
|
|
161
|
+
return require(modulePath);
|
|
162
|
+
} catch (requireError2) {
|
|
163
|
+
console.error(`Both import and require failed for ${modulePath}`);
|
|
164
|
+
throw requireError2;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
function getSocketHandler(routeModule) {
|
|
169
|
+
return routeModule?.default?.routeModule?.userland?.SOCKET ?? routeModule?.routeModule?.userland?.SOCKET ?? routeModule?.default?.handlers?.SOCKET ?? routeModule?.handlers?.SOCKET;
|
|
118
170
|
}
|
|
119
|
-
chunkPFW3KWBF_cjs.__name(importRouteModule, "importRouteModule");
|
|
120
171
|
|
|
121
172
|
// src/server/setup.ts
|
|
122
173
|
function setupWebSocketServer(nextServer) {
|
|
123
|
-
process.env.NEXT_WS_MAIN_PROCESS = String(1)
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
174
|
+
process.env.NEXT_WS_MAIN_PROCESS = String(1);
|
|
175
|
+
process.env.NEXT_WS_SKIP_ENVIRONMENT_CHECK = String(1);
|
|
176
|
+
const httpServer = useHttpServer(nextServer.serverOptions?.httpServer);
|
|
177
|
+
const wsServer = useWebSocketServer(new import_ws.WebSocketServer({ noServer: true }));
|
|
178
|
+
process.env.NEXT_WS_SKIP_ENVIRONMENT_CHECK = String(0);
|
|
179
|
+
if (!httpServer)
|
|
180
|
+
return logger3.error("[next-ws] was not able to find the HTTP server");
|
|
127
181
|
if (!wsServer)
|
|
128
|
-
return
|
|
129
|
-
|
|
130
|
-
|
|
182
|
+
return logger3.error("[next-ws] was not able to find the WebSocket server");
|
|
183
|
+
logger3.ready("[next-ws] has started the WebSocket server");
|
|
184
|
+
httpServer.on("upgrade", async (request, socket, head) => {
|
|
185
|
+
const url = new URL(request.url ?? "", "ws://next");
|
|
186
|
+
const pathname = url.pathname;
|
|
131
187
|
if (pathname.startsWith("/_next")) return;
|
|
132
|
-
|
|
133
|
-
if (!routeInfo)
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
188
|
+
const routeInfo = resolvePathToRoute(nextServer, pathname);
|
|
189
|
+
if (!routeInfo) {
|
|
190
|
+
logger3.error(`[next-ws] could not find module for page ${pathname}`);
|
|
191
|
+
return socket.destroy();
|
|
192
|
+
}
|
|
193
|
+
const routeModule = await importRouteModule(nextServer, routeInfo.filePath);
|
|
194
|
+
if (!routeModule) {
|
|
195
|
+
logger3.error(`[next-ws] could not find module for page ${pathname}`);
|
|
196
|
+
return socket.destroy();
|
|
197
|
+
}
|
|
198
|
+
const socketHandler = getSocketHandler(routeModule);
|
|
199
|
+
if (!socketHandler || typeof socketHandler !== "function") {
|
|
200
|
+
logger3.error(`[next-ws] ${pathname} does not export a SOCKET handler`);
|
|
201
|
+
return socket.destroy();
|
|
202
|
+
}
|
|
203
|
+
return wsServer.handleUpgrade(request, socket, head, async (c, r) => {
|
|
204
|
+
const routeContext = { params: routeInfo.routeParams };
|
|
205
|
+
const handleClose = await socketHandler(c, r, wsServer, routeContext);
|
|
206
|
+
if (typeof handleClose === "function")
|
|
207
|
+
c.once("close", () => handleClose());
|
|
142
208
|
});
|
|
143
209
|
});
|
|
144
210
|
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
throw new Error(
|
|
154
|
-
"The 'verifyPatch' function has been deprecated in favour of the `npx next-ws-cli@latest verify` command."
|
|
155
|
-
);
|
|
156
|
-
}
|
|
157
|
-
chunkPFW3KWBF_cjs.__name(verifyPatch, "verifyPatch");
|
|
158
|
-
|
|
159
|
-
exports.getHttpServer = getHttpServer;
|
|
160
|
-
exports.getWebSocketServer = getWebSocketServer;
|
|
161
|
-
exports.hookNextNodeServer = hookNextNodeServer;
|
|
162
|
-
exports.setHttpServer = setHttpServer;
|
|
163
|
-
exports.setWebSocketServer = setWebSocketServer;
|
|
164
|
-
exports.setupWebSocketServer = setupWebSocketServer;
|
|
165
|
-
exports.verifyPatch = verifyPatch;
|
|
166
|
-
//# sourceMappingURL=out.js.map
|
|
167
|
-
//# sourceMappingURL=index.cjs.map
|
|
211
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
212
|
+
0 && (module.exports = {
|
|
213
|
+
getHttpServer,
|
|
214
|
+
getWebSocketServer,
|
|
215
|
+
setHttpServer,
|
|
216
|
+
setWebSocketServer,
|
|
217
|
+
setupWebSocketServer
|
|
218
|
+
});
|
package/dist/server/index.d.cts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import NextNodeServer from 'next/dist/server/next-server';
|
|
1
|
+
import NextNodeServer from 'next/dist/server/next-server.js';
|
|
2
2
|
import { Server } from 'node:http';
|
|
3
3
|
import { WebSocketServer } from 'ws';
|
|
4
4
|
|
|
5
5
|
declare function setupWebSocketServer(nextServer: NextNodeServer): void;
|
|
6
|
-
declare function hookNextNodeServer(this: NextNodeServer): void;
|
|
7
6
|
|
|
8
7
|
/**
|
|
9
8
|
* Set the HTTP server that the WebSocket server should listen on, must be called before the WebSocket server is created.
|
|
@@ -30,9 +29,4 @@ declare function setWebSocketServer(wsServer: WebSocketServer): void;
|
|
|
30
29
|
*/
|
|
31
30
|
declare function getWebSocketServer(): WebSocketServer;
|
|
32
31
|
|
|
33
|
-
|
|
34
|
-
* @deprecated
|
|
35
|
-
*/
|
|
36
|
-
declare function verifyPatch(): void;
|
|
37
|
-
|
|
38
|
-
export { getHttpServer, getWebSocketServer, hookNextNodeServer, setHttpServer, setWebSocketServer, setupWebSocketServer, verifyPatch };
|
|
32
|
+
export { getHttpServer, getWebSocketServer, setHttpServer, setWebSocketServer, setupWebSocketServer };
|
package/dist/server/index.d.ts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import NextNodeServer from 'next/dist/server/next-server';
|
|
1
|
+
import NextNodeServer from 'next/dist/server/next-server.js';
|
|
2
2
|
import { Server } from 'node:http';
|
|
3
3
|
import { WebSocketServer } from 'ws';
|
|
4
4
|
|
|
5
5
|
declare function setupWebSocketServer(nextServer: NextNodeServer): void;
|
|
6
|
-
declare function hookNextNodeServer(this: NextNodeServer): void;
|
|
7
6
|
|
|
8
7
|
/**
|
|
9
8
|
* Set the HTTP server that the WebSocket server should listen on, must be called before the WebSocket server is created.
|
|
@@ -30,9 +29,4 @@ declare function setWebSocketServer(wsServer: WebSocketServer): void;
|
|
|
30
29
|
*/
|
|
31
30
|
declare function getWebSocketServer(): WebSocketServer;
|
|
32
31
|
|
|
33
|
-
|
|
34
|
-
* @deprecated
|
|
35
|
-
*/
|
|
36
|
-
declare function verifyPatch(): void;
|
|
37
|
-
|
|
38
|
-
export { getHttpServer, getWebSocketServer, hookNextNodeServer, setHttpServer, setWebSocketServer, setupWebSocketServer, verifyPatch };
|
|
32
|
+
export { getHttpServer, getWebSocketServer, setHttpServer, setWebSocketServer, setupWebSocketServer };
|
package/dist/server/index.js
CHANGED
|
@@ -1,139 +1,181 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import {
|
|
2
|
+
__require
|
|
3
|
+
} from "../chunk-3RG5ZIWI.js";
|
|
4
4
|
|
|
5
|
+
// src/server/setup.ts
|
|
6
|
+
import * as logger3 from "next/dist/build/output/log.js";
|
|
7
|
+
import { WebSocketServer } from "ws";
|
|
8
|
+
|
|
9
|
+
// src/server/helpers/persistent.ts
|
|
10
|
+
import * as logger from "next/dist/build/output/log.js";
|
|
5
11
|
function getEnvironmentMeta() {
|
|
6
|
-
|
|
12
|
+
const isCustomServer = !process.title.startsWith("next-");
|
|
13
|
+
const isMainProcess = process.env.NEXT_WS_MAIN_PROCESS === "1";
|
|
14
|
+
const isDevelopment = process.env.NODE_ENV === "development";
|
|
7
15
|
return { isCustomServer, isMainProcess, isDevelopment };
|
|
8
16
|
}
|
|
9
|
-
__name(getEnvironmentMeta, "getEnvironmentMeta");
|
|
10
17
|
function mainProcessOnly(fnName) {
|
|
11
18
|
if (process.env.NEXT_WS_SKIP_ENVIRONMENT_CHECK === "1") return;
|
|
12
|
-
|
|
13
|
-
if (meta.isMainProcess)
|
|
14
|
-
|
|
19
|
+
const meta = getEnvironmentMeta();
|
|
20
|
+
if (!meta.isMainProcess) {
|
|
21
|
+
throw new Error(
|
|
22
|
+
`[next-ws] Attempt to invoke '${fnName}' outside the main process.
|
|
23
|
+
You may be attempting to interact with the WebSocket server outside of a SOCKET handler. This will fail in production, as Next.js employs a worker process for routing, which do not have access to the WebSocket server on the main process.
|
|
24
|
+
You can resolve this by using a custom server.`
|
|
25
|
+
);
|
|
26
|
+
} else if (!meta.isCustomServer) {
|
|
27
|
+
logger.warnOnce(
|
|
15
28
|
`[next-ws] Caution: The function '${fnName}' was invoked without a custom server.
|
|
16
29
|
This could lead to unintended behaviour, especially if you're attempting to interact with the WebSocket server outside of a SOCKET handler.
|
|
17
30
|
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.
|
|
18
31
|
You can resolve this by using a custom server.`
|
|
19
32
|
);
|
|
20
|
-
|
|
21
|
-
`[next-ws] Attempt to invoke '${fnName}' outside the main process.
|
|
22
|
-
You may be attempting to interact with the WebSocket server outside of a SOCKET handler. This will fail in production, as Next.js employs a worker process for routing, which do not have access to the WebSocket server on the main process.
|
|
23
|
-
You can resolve this by using a custom server.`
|
|
24
|
-
);
|
|
33
|
+
}
|
|
25
34
|
}
|
|
26
|
-
__name(mainProcessOnly, "mainProcessOnly");
|
|
27
35
|
var NextWsHttpServer = Symbol.for("NextWs_HttpServer");
|
|
28
36
|
function setHttpServer(server) {
|
|
29
37
|
Reflect.set(globalThis, NextWsHttpServer, server);
|
|
30
38
|
}
|
|
31
|
-
__name(setHttpServer, "setHttpServer");
|
|
32
39
|
function getHttpServer() {
|
|
33
|
-
|
|
40
|
+
mainProcessOnly("getHttpServer");
|
|
41
|
+
return Reflect.get(globalThis, NextWsHttpServer);
|
|
34
42
|
}
|
|
35
|
-
__name(getHttpServer, "getHttpServer");
|
|
36
43
|
function useHttpServer(server) {
|
|
37
|
-
|
|
38
|
-
|
|
44
|
+
const existing = getHttpServer();
|
|
45
|
+
if (existing) return existing;
|
|
46
|
+
if (server) setHttpServer(server);
|
|
47
|
+
return server;
|
|
39
48
|
}
|
|
40
|
-
__name(useHttpServer, "useHttpServer");
|
|
41
49
|
var NextWsWebSocketServer = Symbol.for("NextWs_WebSocketServer");
|
|
42
50
|
function setWebSocketServer(wsServer) {
|
|
43
51
|
Reflect.set(globalThis, NextWsWebSocketServer, wsServer);
|
|
44
52
|
}
|
|
45
|
-
__name(setWebSocketServer, "setWebSocketServer");
|
|
46
53
|
function getWebSocketServer() {
|
|
47
|
-
|
|
54
|
+
mainProcessOnly("getWebSocketServer");
|
|
55
|
+
return Reflect.get(globalThis, NextWsWebSocketServer);
|
|
48
56
|
}
|
|
49
|
-
__name(getWebSocketServer, "getWebSocketServer");
|
|
50
57
|
function useWebSocketServer(wsServer) {
|
|
51
|
-
|
|
52
|
-
|
|
58
|
+
const existing = getWebSocketServer();
|
|
59
|
+
if (existing) return existing;
|
|
60
|
+
if (wsServer) setWebSocketServer(wsServer);
|
|
61
|
+
return wsServer;
|
|
53
62
|
}
|
|
54
|
-
|
|
63
|
+
|
|
64
|
+
// src/server/helpers/route.ts
|
|
65
|
+
import { pathToFileURL } from "node:url";
|
|
66
|
+
import * as logger2 from "next/dist/build/output/log.js";
|
|
55
67
|
function createRouteRegex(routePattern) {
|
|
56
|
-
|
|
68
|
+
const escapedPattern = routePattern.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&");
|
|
69
|
+
const paramRegex = escapedPattern.replace(/\\\[([a-zA-Z0-9_]+)\\\]/g, "(?<$1>[^/]+)").replace(/\\\[(?:\\\.){3}([a-zA-Z0-9_]+)\\\]/g, "(?<rest_$1>.+)");
|
|
57
70
|
return new RegExp(`^${paramRegex}$`);
|
|
58
71
|
}
|
|
59
|
-
__name(createRouteRegex, "createRouteRegex");
|
|
60
72
|
function getRouteParams(routePattern, routePath) {
|
|
61
|
-
|
|
73
|
+
const routeRegex = createRouteRegex(routePattern);
|
|
74
|
+
const match = routePath.match(routeRegex);
|
|
62
75
|
if (!match) return null;
|
|
63
76
|
if (!match.groups) return {};
|
|
64
|
-
|
|
65
|
-
for (let [k, v] of Object.entries(match.groups))
|
|
66
|
-
k.startsWith("rest_")
|
|
77
|
+
const params = {};
|
|
78
|
+
for (let [k, v] of Object.entries(match.groups)) {
|
|
79
|
+
if (k.startsWith("rest_")) {
|
|
80
|
+
k = k.slice(5);
|
|
81
|
+
v = v.split("/");
|
|
82
|
+
}
|
|
83
|
+
Reflect.set(params, k, v);
|
|
84
|
+
}
|
|
67
85
|
return params;
|
|
68
86
|
}
|
|
69
|
-
__name(getRouteParams, "getRouteParams");
|
|
70
87
|
function resolvePathToRoute(nextServer, requestPath) {
|
|
71
|
-
|
|
88
|
+
const routes = {
|
|
72
89
|
// @ts-expect-error - appPathRoutes is protected
|
|
73
90
|
...nextServer.appPathRoutes,
|
|
74
91
|
// @ts-expect-error - getAppPathRoutes is protected
|
|
75
92
|
...nextServer.getAppPathRoutes()
|
|
76
93
|
};
|
|
77
|
-
for (
|
|
78
|
-
|
|
94
|
+
for (const [routePath, [filePath]] of Object.entries(routes)) {
|
|
95
|
+
const routeParams = getRouteParams(routePath, requestPath);
|
|
79
96
|
if (routeParams) return { filePath, routeParams };
|
|
80
97
|
}
|
|
81
98
|
return null;
|
|
82
99
|
}
|
|
83
|
-
__name(resolvePathToRoute, "resolvePathToRoute");
|
|
84
100
|
async function importRouteModule(nextServer, filePath) {
|
|
85
101
|
try {
|
|
86
|
-
"hotReloader" in nextServer
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
)
|
|
102
|
+
if ("hotReloader" in nextServer) {
|
|
103
|
+
await nextServer.hotReloader?.ensurePage({
|
|
104
|
+
page: filePath,
|
|
105
|
+
clientOnly: false
|
|
106
|
+
});
|
|
107
|
+
} else if ("ensurePage" in nextServer) {
|
|
108
|
+
await nextServer.ensurePage({ page: filePath, clientOnly: false });
|
|
109
|
+
} else {
|
|
110
|
+
logger2.warnOnce(
|
|
111
|
+
"[next-ws] unable to ensure page, you may need to open the route in your browser first so Next.js compiles it"
|
|
112
|
+
);
|
|
113
|
+
}
|
|
92
114
|
} catch {
|
|
93
115
|
}
|
|
94
|
-
|
|
95
|
-
return
|
|
116
|
+
const buildPagePath = nextServer.getPagePath(filePath);
|
|
117
|
+
return importModule(buildPagePath);
|
|
118
|
+
}
|
|
119
|
+
async function importModule(modulePath) {
|
|
120
|
+
const moduleUrl = pathToFileURL(modulePath).toString();
|
|
121
|
+
try {
|
|
122
|
+
return import(moduleUrl);
|
|
123
|
+
} catch (requireError) {
|
|
124
|
+
try {
|
|
125
|
+
return __require(modulePath);
|
|
126
|
+
} catch (requireError2) {
|
|
127
|
+
console.error(`Both import and require failed for ${modulePath}`);
|
|
128
|
+
throw requireError2;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
function getSocketHandler(routeModule) {
|
|
133
|
+
return routeModule?.default?.routeModule?.userland?.SOCKET ?? routeModule?.routeModule?.userland?.SOCKET ?? routeModule?.default?.handlers?.SOCKET ?? routeModule?.handlers?.SOCKET;
|
|
96
134
|
}
|
|
97
|
-
__name(importRouteModule, "importRouteModule");
|
|
98
135
|
|
|
99
136
|
// src/server/setup.ts
|
|
100
137
|
function setupWebSocketServer(nextServer) {
|
|
101
|
-
process.env.NEXT_WS_MAIN_PROCESS = String(1)
|
|
102
|
-
|
|
103
|
-
|
|
138
|
+
process.env.NEXT_WS_MAIN_PROCESS = String(1);
|
|
139
|
+
process.env.NEXT_WS_SKIP_ENVIRONMENT_CHECK = String(1);
|
|
140
|
+
const httpServer = useHttpServer(nextServer.serverOptions?.httpServer);
|
|
141
|
+
const wsServer = useWebSocketServer(new WebSocketServer({ noServer: true }));
|
|
142
|
+
process.env.NEXT_WS_SKIP_ENVIRONMENT_CHECK = String(0);
|
|
143
|
+
if (!httpServer)
|
|
104
144
|
return logger3.error("[next-ws] was not able to find the HTTP server");
|
|
105
145
|
if (!wsServer)
|
|
106
146
|
return logger3.error("[next-ws] was not able to find the WebSocket server");
|
|
107
|
-
logger3.ready("[next-ws] has started the WebSocket server")
|
|
108
|
-
|
|
147
|
+
logger3.ready("[next-ws] has started the WebSocket server");
|
|
148
|
+
httpServer.on("upgrade", async (request, socket, head) => {
|
|
149
|
+
const url = new URL(request.url ?? "", "ws://next");
|
|
150
|
+
const pathname = url.pathname;
|
|
109
151
|
if (pathname.startsWith("/_next")) return;
|
|
110
|
-
|
|
111
|
-
if (!routeInfo)
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
152
|
+
const routeInfo = resolvePathToRoute(nextServer, pathname);
|
|
153
|
+
if (!routeInfo) {
|
|
154
|
+
logger3.error(`[next-ws] could not find module for page ${pathname}`);
|
|
155
|
+
return socket.destroy();
|
|
156
|
+
}
|
|
157
|
+
const routeModule = await importRouteModule(nextServer, routeInfo.filePath);
|
|
158
|
+
if (!routeModule) {
|
|
159
|
+
logger3.error(`[next-ws] could not find module for page ${pathname}`);
|
|
160
|
+
return socket.destroy();
|
|
161
|
+
}
|
|
162
|
+
const socketHandler = getSocketHandler(routeModule);
|
|
163
|
+
if (!socketHandler || typeof socketHandler !== "function") {
|
|
164
|
+
logger3.error(`[next-ws] ${pathname} does not export a SOCKET handler`);
|
|
165
|
+
return socket.destroy();
|
|
166
|
+
}
|
|
167
|
+
return wsServer.handleUpgrade(request, socket, head, async (c, r) => {
|
|
168
|
+
const routeContext = { params: routeInfo.routeParams };
|
|
169
|
+
const handleClose = await socketHandler(c, r, wsServer, routeContext);
|
|
170
|
+
if (typeof handleClose === "function")
|
|
171
|
+
c.once("close", () => handleClose());
|
|
120
172
|
});
|
|
121
173
|
});
|
|
122
174
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
function verifyPatch() {
|
|
131
|
-
throw new Error(
|
|
132
|
-
"The 'verifyPatch' function has been deprecated in favour of the `npx next-ws-cli@latest verify` command."
|
|
133
|
-
);
|
|
134
|
-
}
|
|
135
|
-
__name(verifyPatch, "verifyPatch");
|
|
136
|
-
|
|
137
|
-
export { getHttpServer, getWebSocketServer, hookNextNodeServer, setHttpServer, setWebSocketServer, setupWebSocketServer, verifyPatch };
|
|
138
|
-
//# sourceMappingURL=out.js.map
|
|
139
|
-
//# sourceMappingURL=index.js.map
|
|
175
|
+
export {
|
|
176
|
+
getHttpServer,
|
|
177
|
+
getWebSocketServer,
|
|
178
|
+
setHttpServer,
|
|
179
|
+
setWebSocketServer,
|
|
180
|
+
setupWebSocketServer
|
|
181
|
+
};
|