next-ws 0.1.1 → 0.1.3

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.
Files changed (3) hide show
  1. package/package.json +2 -3
  2. package/patch.js +14 -0
  3. package/server.js +8 -7
package/package.json CHANGED
@@ -1,7 +1,6 @@
1
1
  {
2
2
  "name": "next-ws",
3
- "private": false,
4
- "version": "0.1.1",
3
+ "version": "0.1.3",
5
4
  "description": "Add support for WebSockets in Next.js 13 app directory",
6
5
  "packageManager": "yarn@3.6.0",
7
6
  "license": "MIT",
@@ -20,7 +19,7 @@
20
19
  "clean": "rimraf ./*.{js,d.ts,js.map}",
21
20
  "lint": "eslint --ext .ts,.tsx src",
22
21
  "format": "prettier --write src/**/*.{ts,tsx} && eslint --fix --ext .ts,.tsx src/",
23
- "postinstall": "node patch.js"
22
+ "postinstall": "node -e \"try{require('./patch.js')}catch{}\""
24
23
  },
25
24
  "dependencies": {
26
25
  "@babel/generator": "^7.22.3",
package/patch.js CHANGED
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const tslib_1 = require("tslib");
4
+ /* eslint-disable no-template-curly-in-string */
4
5
  const fs = tslib_1.__importStar(require("node:fs/promises"));
5
6
  const generator_1 = tslib_1.__importDefault(require("@babel/generator"));
6
7
  const parser = tslib_1.__importStar(require("@babel/parser"));
@@ -8,6 +9,7 @@ const template_1 = tslib_1.__importDefault(require("@babel/template"));
8
9
  void main();
9
10
  async function main() {
10
11
  await patchNextNodeServer();
12
+ await patchNextTypesPlugin();
11
13
  }
12
14
  const mod = template_1.default.expression.ast `require("next-ws/server")`;
13
15
  // Add `require('next-ws/server').hookNextNodeServer.call(this)` to the
@@ -33,4 +35,16 @@ async function patchNextNodeServer() {
33
35
  constructorMethod.body.body.push(statement);
34
36
  await fs.writeFile(filePath, (0, generator_1.default)(ast).code);
35
37
  }
38
+ // Add `SOCKET?: Function` to the page module interface check field thing in
39
+ // `next/dist/build/webpack/plugins/next-types-plugin.js`
40
+ async function patchNextTypesPlugin() {
41
+ const filePath = require.resolve('next/dist/build/webpack/plugins/next-types-plugin.js');
42
+ const content = await fs.readFile(filePath, 'utf8');
43
+ if (content.includes('SOCKET?: Function'))
44
+ return;
45
+ const toFind = '.map((method)=>`${method}?: Function`).join("\\n ")';
46
+ const replaceWith = `${toFind} + "; SOCKET?: Function"`;
47
+ const newContent = content.replace(toFind, replaceWith);
48
+ await fs.writeFile(filePath, newContent);
49
+ }
36
50
  //# sourceMappingURL=patch.js.map
package/server.js CHANGED
@@ -5,11 +5,11 @@ const tslib_1 = require("tslib");
5
5
  const node_http_1 = require("node:http");
6
6
  const node_path_1 = tslib_1.__importDefault(require("node:path"));
7
7
  const Log = tslib_1.__importStar(require("next/dist/build/output/log"));
8
- const is_api_route_1 = require("next/dist/lib/is-api-route");
9
8
  const next_server_1 = tslib_1.__importDefault(require("next/dist/server/next-server"));
10
9
  const is_dynamic_1 = require("next/dist/shared/lib/router/utils/is-dynamic");
11
10
  const ws_1 = require("ws");
12
11
  const openSockets = new Set();
12
+ let existingWebSocketServer;
13
13
  function hookNextNodeServer() {
14
14
  var _a;
15
15
  if (!this || !(this instanceof next_server_1.default))
@@ -17,8 +17,11 @@ function hookNextNodeServer() {
17
17
  const server = (_a = this.serverOptions) === null || _a === void 0 ? void 0 : _a.httpServer;
18
18
  if (!server || !(server instanceof node_http_1.Server))
19
19
  throw new Error("[next-ws] Failed to find NextNodeServer's HTTP server");
20
+ if (existingWebSocketServer)
21
+ return;
20
22
  const wss = new ws_1.WebSocketServer({ noServer: true });
21
23
  Log.ready('[next-ws] loaded successfully');
24
+ existingWebSocketServer = wss;
22
25
  server.on('upgrade', async (request, socket, head) => {
23
26
  var _a;
24
27
  const url = new URL((_a = request.url) !== null && _a !== void 0 ? _a : '', 'http://next-ws');
@@ -59,14 +62,12 @@ function hookNextNodeServer() {
59
62
  }
60
63
  exports.hookNextNodeServer = hookNextNodeServer;
61
64
  async function isPageFound(pathname) {
62
- const isPageFound = !(0, is_dynamic_1.isDynamicRoute)(pathname) && (await this.hasPage(pathname));
63
- if (isPageFound)
65
+ var _a;
66
+ if (!(0, is_dynamic_1.isDynamicRoute)(pathname) && (await this.hasPage(pathname)))
64
67
  return pathname;
65
- else if (!this.dynamicRoutes)
66
- return false;
67
- for (const route of this.dynamicRoutes) {
68
+ for (const route of (_a = this.dynamicRoutes) !== null && _a !== void 0 ? _a : []) {
68
69
  const params = route.match(pathname) || undefined;
69
- if ((0, is_api_route_1.isAPIRoute)(route.page) && params)
70
+ if (params)
70
71
  return route.page;
71
72
  }
72
73
  return false;