next-ws 0.2.1 → 0.2.3-next.93c5894

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 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 instance only exists when connected
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,8 +1,14 @@
1
1
  {
2
2
  "name": "next-ws",
3
- "version": "0.2.1",
3
+ "version": "0.2.3-next.93c5894",
4
4
  "description": "Add support for WebSockets in Next.js 13 app directory",
5
- "keywords": ["next", "websocket", "ws", "server", "client"],
5
+ "keywords": [
6
+ "next",
7
+ "websocket",
8
+ "ws",
9
+ "server",
10
+ "client"
11
+ ],
6
12
  "license": "MIT",
7
13
  "homepage": "https://github.com/apteryxxyz/next-ws#readme",
8
14
  "repository": {
@@ -13,7 +19,12 @@
13
19
  "bugs": {
14
20
  "url": "https://github.com/apteryxxyz/next-ws/issues"
15
21
  },
16
- "files": ["index.js", "index.d.ts", "client", "server"],
22
+ "files": [
23
+ "index.js",
24
+ "index.d.ts",
25
+ "client",
26
+ "server"
27
+ ],
17
28
  "main": "./index.js",
18
29
  "types": "./index.d.ts",
19
30
  "scripts": {
@@ -43,4 +54,4 @@
43
54
  "rimraf": "^5.0.1",
44
55
  "typescript": "<5.1.0"
45
56
  }
46
- }
57
+ }
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 = await (0, next_1.resolvePathname)(nextServer, url.pathname);
16
- if (!pathname)
17
- return;
18
- const fsPathname = node_path_1.default
19
- .join(pathname, 'route')
20
- .replaceAll(node_path_1.default.sep, '/');
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
- return Log.error(`[next-ws] could not find module for page ${pathname}`);
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
- return Log.error(`[next-ws] could not find SOCKET handler for page ${pathname}`);
27
- wsServer.handleUpgrade(request, socket, head, (client, request) => {
28
- socketHandler(client, request, wsServer);
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;
@@ -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
- // @ts-expect-error hasPage is protected
34
- if (!(0, is_dynamic_1.isDynamicRoute)(pathname) && (await nextServer.hasPage(pathname)))
35
- return pathname;
36
- // @ts-expect-error dynamicRoutes is protected
37
- for (const route of nextServer.dynamicRoutes ?? []) {
38
- const params = route.match(pathname) || undefined;
39
- if (params)
40
- return route.page;
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
  }