next-ws 1.2.0 → 2.0.0
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 +5399 -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 +32 -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/client/index.cjs
CHANGED
|
@@ -1,38 +1,76 @@
|
|
|
1
|
-
|
|
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 });
|
|
17
|
+
}
|
|
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);
|
|
2
29
|
|
|
3
|
-
|
|
4
|
-
var
|
|
30
|
+
// src/client/index.ts
|
|
31
|
+
var client_exports = {};
|
|
32
|
+
__export(client_exports, {
|
|
33
|
+
WebSocketConsumer: () => WebSocketConsumer,
|
|
34
|
+
WebSocketContext: () => WebSocketContext,
|
|
35
|
+
WebSocketProvider: () => WebSocketProvider,
|
|
36
|
+
useWebSocket: () => useWebSocket
|
|
37
|
+
});
|
|
38
|
+
module.exports = __toCommonJS(client_exports);
|
|
5
39
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
var
|
|
9
|
-
|
|
10
|
-
var WebSocketContext = React.createContext(null);
|
|
40
|
+
// src/client/context.tsx
|
|
41
|
+
var import_react = __toESM(require("react"), 1);
|
|
42
|
+
var import_react2 = require("react");
|
|
43
|
+
var WebSocketContext = (0, import_react2.createContext)(null);
|
|
11
44
|
WebSocketContext.displayName = "WebSocketContext";
|
|
12
45
|
var WebSocketConsumer = WebSocketContext.Consumer;
|
|
13
46
|
function WebSocketProvider(p) {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
47
|
+
const clientRef = (0, import_react.useRef)(null);
|
|
48
|
+
(0, import_react2.useEffect)(() => {
|
|
49
|
+
if (typeof window === "undefined") return;
|
|
50
|
+
if (clientRef.current) {
|
|
51
|
+
clientRef.current.close();
|
|
52
|
+
clientRef.current = null;
|
|
53
|
+
}
|
|
54
|
+
const client = new WebSocket(p.url, p.protocols);
|
|
55
|
+
if (p.binaryType) client.binaryType = p.binaryType;
|
|
56
|
+
clientRef.current = client;
|
|
57
|
+
return () => {
|
|
58
|
+
client.close();
|
|
59
|
+
clientRef.current = null;
|
|
60
|
+
};
|
|
18
61
|
}, [p.url, p.protocols, p.binaryType]);
|
|
19
|
-
return
|
|
20
|
-
if (client?.readyState === WebSocket.OPEN)
|
|
21
|
-
return () => client.close();
|
|
22
|
-
}, [client]), /* @__PURE__ */ React__default.default.createElement(WebSocketContext.Provider, { value: client }, p.children);
|
|
62
|
+
return /* @__PURE__ */ import_react.default.createElement(WebSocketContext.Provider, { value: clientRef.current }, p.children);
|
|
23
63
|
}
|
|
24
|
-
chunkPFW3KWBF_cjs.__name(WebSocketProvider, "WebSocketProvider");
|
|
25
64
|
function useWebSocket() {
|
|
26
|
-
|
|
65
|
+
const context = (0, import_react2.useContext)(WebSocketContext);
|
|
27
66
|
if (context === void 0)
|
|
28
67
|
throw new Error("useWebSocket must be used within a WebSocketProvider");
|
|
29
68
|
return context;
|
|
30
69
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
//# sourceMappingURL=index.cjs.map
|
|
70
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
71
|
+
0 && (module.exports = {
|
|
72
|
+
WebSocketConsumer,
|
|
73
|
+
WebSocketContext,
|
|
74
|
+
WebSocketProvider,
|
|
75
|
+
useWebSocket
|
|
76
|
+
});
|
package/dist/client/index.js
CHANGED
|
@@ -1,29 +1,38 @@
|
|
|
1
|
-
import
|
|
2
|
-
import React, { createContext, useMemo, useEffect, useContext } from 'react';
|
|
1
|
+
import "../chunk-3RG5ZIWI.js";
|
|
3
2
|
|
|
3
|
+
// src/client/context.tsx
|
|
4
|
+
import React, { useRef } from "react";
|
|
5
|
+
import { createContext, useContext, useEffect } from "react";
|
|
4
6
|
var WebSocketContext = createContext(null);
|
|
5
7
|
WebSocketContext.displayName = "WebSocketContext";
|
|
6
8
|
var WebSocketConsumer = WebSocketContext.Consumer;
|
|
7
9
|
function WebSocketProvider(p) {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
const clientRef = useRef(null);
|
|
11
|
+
useEffect(() => {
|
|
12
|
+
if (typeof window === "undefined") return;
|
|
13
|
+
if (clientRef.current) {
|
|
14
|
+
clientRef.current.close();
|
|
15
|
+
clientRef.current = null;
|
|
16
|
+
}
|
|
17
|
+
const client = new WebSocket(p.url, p.protocols);
|
|
18
|
+
if (p.binaryType) client.binaryType = p.binaryType;
|
|
19
|
+
clientRef.current = client;
|
|
20
|
+
return () => {
|
|
21
|
+
client.close();
|
|
22
|
+
clientRef.current = null;
|
|
23
|
+
};
|
|
12
24
|
}, [p.url, p.protocols, p.binaryType]);
|
|
13
|
-
return
|
|
14
|
-
if (client?.readyState === WebSocket.OPEN)
|
|
15
|
-
return () => client.close();
|
|
16
|
-
}, [client]), /* @__PURE__ */ React.createElement(WebSocketContext.Provider, { value: client }, p.children);
|
|
25
|
+
return /* @__PURE__ */ React.createElement(WebSocketContext.Provider, { value: clientRef.current }, p.children);
|
|
17
26
|
}
|
|
18
|
-
__name(WebSocketProvider, "WebSocketProvider");
|
|
19
27
|
function useWebSocket() {
|
|
20
|
-
|
|
28
|
+
const context = useContext(WebSocketContext);
|
|
21
29
|
if (context === void 0)
|
|
22
30
|
throw new Error("useWebSocket must be used within a WebSocketProvider");
|
|
23
31
|
return context;
|
|
24
32
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
33
|
+
export {
|
|
34
|
+
WebSocketConsumer,
|
|
35
|
+
WebSocketContext,
|
|
36
|
+
WebSocketProvider,
|
|
37
|
+
useWebSocket
|
|
38
|
+
};
|
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 await import(moduleUrl).then((m) => m.default);
|
|
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?.routeModule?.userland?.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 };
|