@nestjs/websockets 12.0.0-alpha.2 → 12.0.0-alpha.4

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
@@ -29,7 +29,7 @@ Nest is a framework for building efficient, scalable <a href="https://nodejs.org
29
29
 
30
30
  ## Philosophy
31
31
 
32
- <p>In recent years, thanks to Node.js, JavaScript has become the “lingua franca” of the web for both front and backend applications, giving rise to awesome projects like <a href="https://angular.io/" target="_blank">Angular</a>, <a href="https://github.com/facebook/react" target="_blank">React</a>, and <a href="https://github.com/vuejs/vue" target="_blank">Vue</a>, which improve developer productivity and enable the construction of fast, testable, and extensible frontend applications. However, on the server-side, while there are a lot of superb libraries, helpers, and tools for Node, none of them effectively solve the main problem - the architecture.</p>
32
+ <p>In recent years, thanks to Node.js, JavaScript has become the “lingua franca” of the web for both front-end and back-end applications, giving rise to awesome projects like <a href="https://angular.dev/" target="_blank">Angular</a>, <a href="https://react.dev/" target="_blank">React</a>, and <a href="https://vuejs.org/" target="_blank">Vue</a>, which improve developer productivity and enable the construction of fast, testable, and extensible frontend applications. However, on the server-side, while there are a lot of superb libraries, helpers, and tools for Node, none of them effectively solve the main problem - the architecture.</p>
33
33
  <p>Nest aims to provide an application architecture out of the box which allows for effortless creation of highly testable, scalable, and loosely coupled and easily maintainable applications. The architecture is heavily inspired by Angular.</p>
34
34
 
35
35
  ## Getting started
@@ -95,6 +95,7 @@ Nest is an MIT-licensed open source project. It can grow thanks to the sponsors
95
95
  <td align="center" valign="middle"><a href="https://crawljobs.com" target="_blank"><img src="https://nestjs.com/img/logos/crawljobs-logo.svg" width="130" valign="middle" /></a></td>
96
96
  </tr><tr>
97
97
  <td align="center" valign="middle"><a href="https://pandektes.com" target="_blank"><img src="https://nestjs.com/img/logos/pandektes-logo.png" width="65" valign="middle" /></a></td>
98
+ <td align="center" valign="middle"><a href="https://www.fintechcrafts.com/" target="_blank"><img src="https://nestjs.com/img/logos/fintechcrafts-logo.svg" width="65" valign="middle" /></a></td>
98
99
  </tr>
99
100
  </table>
100
101
 
@@ -0,0 +1,16 @@
1
+ import { PipeTransform, Type } from '@nestjs/common';
2
+ /**
3
+ * WebSockets pattern parameter decorator.
4
+ * Extracts the subscribed message pattern from the current ws event.
5
+ *
6
+ * @example
7
+ * ```typescript
8
+ * @SubscribeMessage('events')
9
+ * onEvent(@WsPattern() pattern: string) {
10
+ * return { event: pattern, data: 'ok' };
11
+ * }
12
+ * ```
13
+ *
14
+ * @publicApi
15
+ */
16
+ export declare function WsPattern(...pipes: (Type<PipeTransform> | PipeTransform)[]): ParameterDecorator;
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.WsPattern = WsPattern;
4
+ const ws_paramtype_enum_1 = require("../enums/ws-paramtype.enum");
5
+ const param_utils_1 = require("../utils/param.utils");
6
+ /**
7
+ * WebSockets pattern parameter decorator.
8
+ * Extracts the subscribed message pattern from the current ws event.
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * @SubscribeMessage('events')
13
+ * onEvent(@WsPattern() pattern: string) {
14
+ * return { event: pattern, data: 'ok' };
15
+ * }
16
+ * ```
17
+ *
18
+ * @publicApi
19
+ */
20
+ function WsPattern(...pipes) {
21
+ return (0, param_utils_1.createPipesWsParamDecorator)(ws_paramtype_enum_1.WsParamtype.PATTERN)(undefined, ...pipes);
22
+ }
@@ -40,11 +40,33 @@ export declare class BaseWsExceptionFilter<TError = any> implements WsExceptionF
40
40
  constructor(options?: BaseWsExceptionFilterOptions);
41
41
  catch(exception: TError, host: ArgumentsHost): void;
42
42
  handleError<TClient extends {
43
- emit: Function;
44
- }>(client: TClient, exception: TError, cause: ErrorPayload['cause']): any;
43
+ emit?: Function;
44
+ send?: Function;
45
+ }>(client: TClient, exception: TError, cause: ErrorPayload['cause']): void;
45
46
  handleUnknownError<TClient extends {
46
- emit: Function;
47
+ emit?: Function;
48
+ send?: Function;
47
49
  }>(exception: TError, client: TClient, data: ErrorPayload['cause']): void;
48
50
  isExceptionObject(err: any): err is Error;
51
+ /**
52
+ * Sends an error message to the client. Supports both Socket.IO clients
53
+ * (which use `emit`) and native WebSocket clients (which use `send`).
54
+ *
55
+ * Native WebSocket clients (e.g. from the `ws` package) inherit from
56
+ * EventEmitter and therefore also have an `emit` method, but that method
57
+ * only dispatches events locally. To distinguish native WebSocket clients
58
+ * from Socket.IO clients, we check for a numeric `readyState` property
59
+ * (part of the WebSocket specification) before falling back to `emit`.
60
+ */
61
+ protected emitMessage<TClient extends {
62
+ emit?: Function;
63
+ send?: Function;
64
+ }>(client: TClient, event: string, payload: unknown): void;
65
+ /**
66
+ * Determines whether the given client is a native WebSocket (e.g. from the
67
+ * `ws` package) as opposed to a Socket.IO socket. Native WebSocket objects
68
+ * expose a numeric `readyState` property per the WebSocket specification.
69
+ */
70
+ private isNativeWebSocket;
49
71
  }
50
72
  export {};
@@ -1,6 +1,6 @@
1
1
  import { IntrinsicException, Logger, } from '@nestjs/common';
2
2
  import { WsException } from '../errors/ws-exception.js';
3
- import { isObject } from '@nestjs/common/internal';
3
+ import { isFunction, isNumber, isObject } from '@nestjs/common/internal';
4
4
  import { MESSAGES } from '@nestjs/core/internal';
5
5
  /**
6
6
  * @publicApi
@@ -30,7 +30,7 @@ export class BaseWsExceptionFilter {
30
30
  const status = 'error';
31
31
  const result = exception.getError();
32
32
  if (isObject(result)) {
33
- return client.emit('exception', result);
33
+ return this.emitMessage(client, 'exception', result);
34
34
  }
35
35
  const payload = {
36
36
  status,
@@ -39,7 +39,7 @@ export class BaseWsExceptionFilter {
39
39
  if (this.options?.includeCause && cause) {
40
40
  payload.cause = this.options.causeFactory(cause.pattern, cause.data);
41
41
  }
42
- client.emit('exception', payload);
42
+ this.emitMessage(client, 'exception', payload);
43
43
  }
44
44
  handleUnknownError(exception, client, data) {
45
45
  const status = 'error';
@@ -50,7 +50,7 @@ export class BaseWsExceptionFilter {
50
50
  if (this.options?.includeCause && data) {
51
51
  payload.cause = this.options.causeFactory(data.pattern, data.data);
52
52
  }
53
- client.emit('exception', payload);
53
+ this.emitMessage(client, 'exception', payload);
54
54
  if (!(exception instanceof IntrinsicException)) {
55
55
  const logger = BaseWsExceptionFilter.logger;
56
56
  logger.error(exception);
@@ -59,4 +59,33 @@ export class BaseWsExceptionFilter {
59
59
  isExceptionObject(err) {
60
60
  return isObject(err) && !!err.message;
61
61
  }
62
+ /**
63
+ * Sends an error message to the client. Supports both Socket.IO clients
64
+ * (which use `emit`) and native WebSocket clients (which use `send`).
65
+ *
66
+ * Native WebSocket clients (e.g. from the `ws` package) inherit from
67
+ * EventEmitter and therefore also have an `emit` method, but that method
68
+ * only dispatches events locally. To distinguish native WebSocket clients
69
+ * from Socket.IO clients, we check for a numeric `readyState` property
70
+ * (part of the WebSocket specification) before falling back to `emit`.
71
+ */
72
+ emitMessage(client, event, payload) {
73
+ if (this.isNativeWebSocket(client)) {
74
+ client.send(JSON.stringify({
75
+ event,
76
+ data: payload,
77
+ }));
78
+ }
79
+ else if (isFunction(client.emit)) {
80
+ client.emit(event, payload);
81
+ }
82
+ }
83
+ /**
84
+ * Determines whether the given client is a native WebSocket (e.g. from the
85
+ * `ws` package) as opposed to a Socket.IO socket. Native WebSocket objects
86
+ * expose a numeric `readyState` property per the WebSocket specification.
87
+ */
88
+ isNativeWebSocket(client) {
89
+ return isNumber(client.readyState) && isFunction(client.send);
90
+ }
62
91
  }
@@ -8,7 +8,7 @@ export class WsExceptionsHandler extends BaseWsExceptionFilter {
8
8
  filters = [];
9
9
  handle(exception, host) {
10
10
  const client = host.switchToWs().getClient();
11
- if (this.invokeCustomFilters(exception, host) || !client.emit) {
11
+ if (this.invokeCustomFilters(exception, host) || !client) {
12
12
  return;
13
13
  }
14
14
  super.catch(exception, host);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nestjs/websockets",
3
- "version": "12.0.0-alpha.2",
3
+ "version": "12.0.0-alpha.4",
4
4
  "description": "Nest - modern, fast, powerful node.js web framework (@websockets)",
5
5
  "author": "Kamil Mysliwiec",
6
6
  "license": "MIT",
@@ -26,8 +26,8 @@
26
26
  "tslib": "2.8.1"
27
27
  },
28
28
  "devDependencies": {
29
- "@nestjs/common": "^12.0.0-alpha.2",
30
- "@nestjs/core": "^12.0.0-alpha.2"
29
+ "@nestjs/common": "^12.0.0-alpha.4",
30
+ "@nestjs/core": "^12.0.0-alpha.4"
31
31
  },
32
32
  "peerDependencies": {
33
33
  "@nestjs/common": "^11.0.0",
@@ -41,5 +41,5 @@
41
41
  "optional": true
42
42
  }
43
43
  },
44
- "gitHead": "011bff66014e24e82b25f7acf9302b9efb70b02b"
44
+ "gitHead": "415e4d33707e74998d8456be7bd305cedb6508e3"
45
45
  }