next-ws 0.2.1-next.b55c6fe → 0.2.2
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 +1 -2
- package/package.json +4 -15
- package/server/setup.js +16 -14
- package/server/utilities/next.js +12 -9
package/README.md
CHANGED
|
@@ -159,8 +159,7 @@ import { useCallback, useEffect, useState } from 'react';
|
|
|
159
159
|
|
|
160
160
|
export default function Page() {
|
|
161
161
|
const ws = useWebSocket();
|
|
162
|
-
// WebSocket
|
|
163
|
-
// It will be null when state is closed or connecting
|
|
162
|
+
// ^? WebSocket on the client, null on the server
|
|
164
163
|
|
|
165
164
|
const [value, setValue] = useState('');
|
|
166
165
|
const [message, setMessage] = useState<string | null>('');
|
package/package.json
CHANGED
|
@@ -1,14 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "next-ws",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.2",
|
|
4
4
|
"description": "Add support for WebSockets in Next.js 13 app directory",
|
|
5
|
-
"keywords": [
|
|
6
|
-
"next",
|
|
7
|
-
"websocket",
|
|
8
|
-
"ws",
|
|
9
|
-
"server",
|
|
10
|
-
"client"
|
|
11
|
-
],
|
|
5
|
+
"keywords": ["next", "websocket", "ws", "server", "client"],
|
|
12
6
|
"license": "MIT",
|
|
13
7
|
"homepage": "https://github.com/apteryxxyz/next-ws#readme",
|
|
14
8
|
"repository": {
|
|
@@ -19,12 +13,7 @@
|
|
|
19
13
|
"bugs": {
|
|
20
14
|
"url": "https://github.com/apteryxxyz/next-ws/issues"
|
|
21
15
|
},
|
|
22
|
-
"files": [
|
|
23
|
-
"index.js",
|
|
24
|
-
"index.d.ts",
|
|
25
|
-
"client",
|
|
26
|
-
"server"
|
|
27
|
-
],
|
|
16
|
+
"files": ["index.js", "index.d.ts", "client", "server"],
|
|
28
17
|
"main": "./index.js",
|
|
29
18
|
"types": "./index.d.ts",
|
|
30
19
|
"scripts": {
|
|
@@ -54,4 +43,4 @@
|
|
|
54
43
|
"rimraf": "^5.0.1",
|
|
55
44
|
"typescript": "<5.1.0"
|
|
56
45
|
}
|
|
57
|
-
}
|
|
46
|
+
}
|
package/server/setup.js
CHANGED
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.hookNextNodeServer = exports.setupWebSocketServer = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
|
-
const node_path_1 = tslib_1.__importDefault(require("node:path"));
|
|
6
5
|
const Log = tslib_1.__importStar(require("next/dist/build/output/log"));
|
|
7
6
|
const ws_1 = require("ws");
|
|
8
7
|
const next_1 = require("./utilities/next");
|
|
@@ -12,21 +11,24 @@ function setupWebSocketServer(nextServer) {
|
|
|
12
11
|
Log.ready('[next-ws] websocket server started successfully');
|
|
13
12
|
httpServer.on('upgrade', async (request, socket, head) => {
|
|
14
13
|
const url = new URL(request.url ?? '', 'http://next-ws');
|
|
15
|
-
const pathname =
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
.
|
|
20
|
-
|
|
14
|
+
const pathname = url.pathname;
|
|
15
|
+
const fsPathname = await (0, next_1.resolvePathname)(nextServer, pathname);
|
|
16
|
+
if (!fsPathname) {
|
|
17
|
+
Log.error(`[next-ws] could not find module for page ${pathname}`);
|
|
18
|
+
return socket.destroy();
|
|
19
|
+
}
|
|
21
20
|
const pageModule = await (0, next_1.getPageModule)(nextServer, fsPathname);
|
|
22
|
-
if (!pageModule)
|
|
23
|
-
|
|
21
|
+
if (!pageModule) {
|
|
22
|
+
Log.error(`[next-ws] could not find module for page ${pathname}`);
|
|
23
|
+
return socket.destroy();
|
|
24
|
+
}
|
|
24
25
|
const socketHandler = pageModule?.routeModule?.userland?.SOCKET;
|
|
25
|
-
if (!socketHandler || typeof socketHandler !== 'function')
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
26
|
+
if (!socketHandler || typeof socketHandler !== 'function') {
|
|
27
|
+
Log.error(`[next-ws] ${pathname} does not export a SOCKET handler`);
|
|
28
|
+
return socket.destroy();
|
|
29
|
+
}
|
|
30
|
+
// prettier-ignore
|
|
31
|
+
return wsServer.handleUpgrade(request, socket, head, (client, request) => socketHandler(client, request, wsServer));
|
|
30
32
|
});
|
|
31
33
|
}
|
|
32
34
|
exports.setupWebSocketServer = setupWebSocketServer;
|
package/server/utilities/next.js
CHANGED
|
@@ -4,7 +4,6 @@ exports.getPageModule = exports.resolvePathname = exports.getHttpServer = void 0
|
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
5
|
const node_http_1 = require("node:http");
|
|
6
6
|
const next_server_1 = tslib_1.__importDefault(require("next/dist/server/next-server"));
|
|
7
|
-
const is_dynamic_1 = require("next/dist/shared/lib/router/utils/is-dynamic");
|
|
8
7
|
/**
|
|
9
8
|
* Get the http.Server instance from the NextNodeServer.
|
|
10
9
|
* @param nextServer The NextNodeServer instance.
|
|
@@ -30,14 +29,18 @@ exports.getHttpServer = getHttpServer;
|
|
|
30
29
|
async function resolvePathname(nextServer, pathname) {
|
|
31
30
|
if (pathname.startsWith('/_next'))
|
|
32
31
|
return null;
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
32
|
+
const appRoutes = {
|
|
33
|
+
// @ts-expect-error appPathRoutes is protected
|
|
34
|
+
...nextServer.appPathRoutes,
|
|
35
|
+
// @ts-expect-error getAppPathRoutes is protected
|
|
36
|
+
...nextServer.getAppPathRoutes(),
|
|
37
|
+
};
|
|
38
|
+
// TODO: 'appRoutes[pathname]' is an array of routes, need to investigate in which case that array has more than one item
|
|
39
|
+
if (pathname in appRoutes) {
|
|
40
|
+
const route = appRoutes[pathname][0];
|
|
41
|
+
if (!route?.endsWith('/route'))
|
|
42
|
+
return null;
|
|
43
|
+
return route;
|
|
41
44
|
}
|
|
42
45
|
return null;
|
|
43
46
|
}
|