@vercube/ws 0.0.33 → 0.0.35
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 +30 -24
- package/dist/index.d.mts +7 -8
- package/dist/index.mjs +8 -8
- package/package.json +7 -7
package/README.md
CHANGED
|
@@ -1,34 +1,40 @@
|
|
|
1
1
|
<div align="center">
|
|
2
|
-
<
|
|
2
|
+
<img src="https://raw.githubusercontent.com/vercube/vercube/refs/heads/main/.github/assets/cover.png" width="100%" alt="Vercube - Unleash your server development." />
|
|
3
3
|
<br>
|
|
4
4
|
<br>
|
|
5
5
|
|
|
6
|
-
#
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
<a href="https://codecov.io/gh/vercube/vercube" target="_blank">
|
|
20
|
-
<img src="https://img.shields.io/codecov/c/github/vercube/vercube?style=for-the-badge&color=%23767eff" alt="Coverage"/>
|
|
21
|
-
</a>
|
|
22
|
-
<br/>
|
|
23
|
-
<br/>
|
|
6
|
+
# @vercube/ws
|
|
7
|
+
|
|
8
|
+
### WebSockets for Vercube apps
|
|
9
|
+
|
|
10
|
+
[&labelColor=%23000&color=%232f2f2f>)](https://deepwiki.com/vercube/vercube)
|
|
11
|
+
&labelColor=%23000&color=%232e2e2e&link=https%3A%2F%2Fwww.npmjs.com%2Fpackage%2F%40vercube%2Fws>)
|
|
12
|
+
&labelColor=%23000&color=%232f2f2f>)
|
|
13
|
+
&labelColor=%23000&color=%232f2f2f>)
|
|
14
|
+
|
|
15
|
+
**Real-time communication with decorators - `@Message` to listen, `@Emit` to respond, `@Broadcast` to send to everyone. Built on [crossws](https://crossws.unjs.io/).**
|
|
16
|
+
|
|
17
|
+
[Website](https://vercube.dev) • [Documentation](https://vercube.dev/docs/getting-started)
|
|
18
|
+
|
|
24
19
|
</div>
|
|
25
20
|
|
|
26
|
-
|
|
21
|
+
## ✨ Features
|
|
22
|
+
|
|
23
|
+
- **Decorator-based** - `@Namespace`, `@Message`, `@Emit`, `@Broadcast`
|
|
24
|
+
- **Connection control** - validate who can connect with `@OnConnectionAttempt`
|
|
25
|
+
- **Message validation** - use Zod schemas to validate incoming data
|
|
26
|
+
- **Namespaces** - organize connections into logical groups
|
|
27
|
+
|
|
28
|
+
## 📦 Installation
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
pnpm add @vercube/ws
|
|
32
|
+
```
|
|
27
33
|
|
|
28
|
-
##
|
|
34
|
+
## 📖 Usage
|
|
29
35
|
|
|
30
|
-
|
|
36
|
+
Check out the full [documentation](https://vercube.dev/docs/modules/web-sockets/overview)
|
|
31
37
|
|
|
32
|
-
##
|
|
38
|
+
## 📜 License
|
|
33
39
|
|
|
34
|
-
|
|
40
|
+
[MIT](https://github.com/vercube/vercube/blob/main/LICENSE)
|
package/dist/index.d.mts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { App, BasePlugin, ValidationTypes } from "@vercube/core";
|
|
2
|
-
import { Peer } from "crossws";
|
|
2
|
+
import { Message as Message$1, Peer, WSError } from "crossws";
|
|
3
3
|
|
|
4
4
|
//#region src/Decorators/Namespace.d.ts
|
|
5
|
-
|
|
6
5
|
/**
|
|
7
6
|
* A decorator function for defining a websocket namespace and accepting websocket connections.
|
|
8
7
|
*
|
|
@@ -122,7 +121,7 @@ declare class WebsocketPlugin<T = unknown> extends BasePlugin<T> {
|
|
|
122
121
|
declare namespace WebsocketTypes {
|
|
123
122
|
enum HandlerAction {
|
|
124
123
|
CONNECTION = "connection",
|
|
125
|
-
MESSAGE = "message"
|
|
124
|
+
MESSAGE = "message"
|
|
126
125
|
}
|
|
127
126
|
type HandlerAttributes = {
|
|
128
127
|
callback: Function;
|
|
@@ -176,7 +175,7 @@ declare class WebsocketService {
|
|
|
176
175
|
/**
|
|
177
176
|
* Broadcast a message to all peers in the same namespace (including the sender).
|
|
178
177
|
*
|
|
179
|
-
* @param {
|
|
178
|
+
* @param {WSPeer} peer - The sender peer (used to determine the namespace).
|
|
180
179
|
* @param {unknown} message - The message to broadcast.
|
|
181
180
|
* @returns {void}
|
|
182
181
|
*/
|
|
@@ -184,7 +183,7 @@ declare class WebsocketService {
|
|
|
184
183
|
/**
|
|
185
184
|
* Emit a message to a single peer.
|
|
186
185
|
*
|
|
187
|
-
* @param {
|
|
186
|
+
* @param {WSPeer} peer - The peer to send the message to.
|
|
188
187
|
* @param {unknown} message - The message to send.
|
|
189
188
|
* @returns {void}
|
|
190
189
|
*/
|
|
@@ -192,7 +191,7 @@ declare class WebsocketService {
|
|
|
192
191
|
/**
|
|
193
192
|
* Broadcast a message to all peers in the same namespace except the sender.
|
|
194
193
|
*
|
|
195
|
-
* @param {
|
|
194
|
+
* @param {WSPeer} peer - The sender peer (used to determine the namespace).
|
|
196
195
|
* @param {unknown} message - The message to broadcast.
|
|
197
196
|
* @returns {void}
|
|
198
197
|
*/
|
|
@@ -206,7 +205,7 @@ declare class WebsocketService {
|
|
|
206
205
|
/**
|
|
207
206
|
* Handle an incoming websocket message for a peer.
|
|
208
207
|
*
|
|
209
|
-
* @param {
|
|
208
|
+
* @param {WSPeer} peer - The peer receiving the message.
|
|
210
209
|
* @param {Message} rawMessage - The raw websocket message.
|
|
211
210
|
* @returns {Promise<void>}
|
|
212
211
|
*/
|
|
@@ -216,4 +215,4 @@ declare class WebsocketService {
|
|
|
216
215
|
//#region src/Symbols/WebsocketSymbols.d.ts
|
|
217
216
|
declare const $WebsocketService: symbol;
|
|
218
217
|
//#endregion
|
|
219
|
-
export { $WebsocketService, Broadcast, BroadcastOthers, Emit, Message, Namespace, OnConnectionAttempt, WebsocketPlugin, WebsocketService, WebsocketTypes };
|
|
218
|
+
export { $WebsocketService, Broadcast, BroadcastOthers, Emit, Message, Namespace, OnConnectionAttempt, type WSError, type Message$1 as WSMessage, type Peer as WSPeer, WebsocketPlugin, WebsocketService, WebsocketTypes };
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { BadRequestError, BasePlugin, HttpServer, ValidationProvider, initializeMetadata, initializeMetadataMethod } from "@vercube/core";
|
|
1
|
+
import { BadRequestError, BasePlugin, HttpServer, ValidationProvider, initializeMetadata, initializeMetadataMethod, safeJsonParse, sanitizeObject } from "@vercube/core";
|
|
2
2
|
import { BaseDecorator, Identity, Inject, InjectOptional, createDecorator } from "@vercube/di";
|
|
3
3
|
import { Logger } from "@vercube/logger";
|
|
4
4
|
import { defineHooks } from "crossws";
|
|
@@ -36,7 +36,7 @@ let WebsocketTypes;
|
|
|
36
36
|
})(WebsocketTypes || (WebsocketTypes = {}));
|
|
37
37
|
|
|
38
38
|
//#endregion
|
|
39
|
-
//#region \0@oxc-project+runtime@0.
|
|
39
|
+
//#region \0@oxc-project+runtime@0.110.0/helpers/decorate.js
|
|
40
40
|
function __decorate(decorators, target, key, desc) {
|
|
41
41
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
42
42
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
@@ -108,7 +108,7 @@ var WebsocketService = class {
|
|
|
108
108
|
/**
|
|
109
109
|
* Broadcast a message to all peers in the same namespace (including the sender).
|
|
110
110
|
*
|
|
111
|
-
* @param {
|
|
111
|
+
* @param {WSPeer} peer - The sender peer (used to determine the namespace).
|
|
112
112
|
* @param {unknown} message - The message to broadcast.
|
|
113
113
|
* @returns {void}
|
|
114
114
|
*/
|
|
@@ -122,7 +122,7 @@ var WebsocketService = class {
|
|
|
122
122
|
/**
|
|
123
123
|
* Emit a message to a single peer.
|
|
124
124
|
*
|
|
125
|
-
* @param {
|
|
125
|
+
* @param {WSPeer} peer - The peer to send the message to.
|
|
126
126
|
* @param {unknown} message - The message to send.
|
|
127
127
|
* @returns {void}
|
|
128
128
|
*/
|
|
@@ -132,7 +132,7 @@ var WebsocketService = class {
|
|
|
132
132
|
/**
|
|
133
133
|
* Broadcast a message to all peers in the same namespace except the sender.
|
|
134
134
|
*
|
|
135
|
-
* @param {
|
|
135
|
+
* @param {WSPeer} peer - The sender peer (used to determine the namespace).
|
|
136
136
|
* @param {unknown} message - The message to broadcast.
|
|
137
137
|
* @returns {void}
|
|
138
138
|
*/
|
|
@@ -153,7 +153,7 @@ var WebsocketService = class {
|
|
|
153
153
|
upgrade: async (request) => {
|
|
154
154
|
const url = new URL(request.url);
|
|
155
155
|
const namespace = url.pathname;
|
|
156
|
-
const parameters = Object.fromEntries(url.searchParams
|
|
156
|
+
const parameters = sanitizeObject(Object.fromEntries(url.searchParams));
|
|
157
157
|
if (!!!this.fNamespaces?.[namespace?.toLowerCase()]) {
|
|
158
158
|
this.gLogger?.warn("WebsocketService::initialize", `Namespace "${namespace}" is not registered. Connection rejected.`);
|
|
159
159
|
return new Response("Namespace not registered", { status: 403 });
|
|
@@ -193,13 +193,13 @@ var WebsocketService = class {
|
|
|
193
193
|
/**
|
|
194
194
|
* Handle an incoming websocket message for a peer.
|
|
195
195
|
*
|
|
196
|
-
* @param {
|
|
196
|
+
* @param {WSPeer} peer - The peer receiving the message.
|
|
197
197
|
* @param {Message} rawMessage - The raw websocket message.
|
|
198
198
|
* @returns {Promise<void>}
|
|
199
199
|
*/
|
|
200
200
|
async handleMessage(peer, rawMessage) {
|
|
201
201
|
try {
|
|
202
|
-
const msg =
|
|
202
|
+
const msg = safeJsonParse(rawMessage.text());
|
|
203
203
|
const namespace = peer.namespace?.toLowerCase();
|
|
204
204
|
const event = msg.event;
|
|
205
205
|
const data = msg.data;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vercube/ws",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.35",
|
|
4
4
|
"description": "Websocket module for Vercube framework",
|
|
5
5
|
"repository": "@vercube/ws",
|
|
6
6
|
"license": "MIT",
|
|
@@ -18,14 +18,14 @@
|
|
|
18
18
|
"README.md"
|
|
19
19
|
],
|
|
20
20
|
"devDependencies": {
|
|
21
|
-
"zod": "4.
|
|
21
|
+
"zod": "4.3.6"
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"crossws": "0.4.
|
|
25
|
-
"srvx": "0.
|
|
26
|
-
"@vercube/
|
|
27
|
-
"@vercube/
|
|
28
|
-
"@vercube/
|
|
24
|
+
"crossws": "0.4.3",
|
|
25
|
+
"srvx": "0.10.1",
|
|
26
|
+
"@vercube/core": "0.0.35",
|
|
27
|
+
"@vercube/di": "0.0.35",
|
|
28
|
+
"@vercube/logger": "0.0.35"
|
|
29
29
|
},
|
|
30
30
|
"publishConfig": {
|
|
31
31
|
"access": "public"
|