@moostjs/event-ws 0.6.0
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/LICENSE +21 -0
- package/README.md +136 -0
- package/dist/index.cjs +445 -0
- package/dist/index.d.ts +225 -0
- package/dist/index.mjs +298 -0
- package/package.json +62 -0
- package/scripts/setup-skills.js +78 -0
- package/skills/moostjs-event-ws/SKILL.md +42 -0
- package/skills/moostjs-event-ws/core.md +157 -0
- package/skills/moostjs-event-ws/handlers.md +162 -0
- package/skills/moostjs-event-ws/protocol.md +181 -0
- package/skills/moostjs-event-ws/request-data.md +185 -0
- package/skills/moostjs-event-ws/rooms.md +196 -0
- package/skills/moostjs-event-ws/routing.md +115 -0
- package/skills/moostjs-event-ws/testing.md +209 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 moostjs
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
# @moostjs/event-ws
|
|
2
|
+
|
|
3
|
+
<p align="center">
|
|
4
|
+
<img src="../../moost-logo.png" width="450px"><br>
|
|
5
|
+
<a href="https://github.com/moostjs/moostjs/blob/main/LICENSE">
|
|
6
|
+
<img src="https://img.shields.io/badge/License-MIT-green?style=for-the-badge" />
|
|
7
|
+
</a>
|
|
8
|
+
</p>
|
|
9
|
+
|
|
10
|
+
Welcome to `@moostjs/event-ws`, a Moostjs library that serves as a wrapper for [@wooksjs/event-ws](https://github.com/wooksjs/wooksjs/tree/main/packages/event-ws). This package provides decorators for composing WebSocket handlers, bringing decorator-based routing, dependency injection, interceptors, and pipes to your WebSocket application.
|
|
11
|
+
|
|
12
|
+
**Note:** As `@moostjs/event-ws` is under active development, breaking changes can be expected.
|
|
13
|
+
|
|
14
|
+
## Overview
|
|
15
|
+
|
|
16
|
+
The `@moostjs/event-ws` module makes Moost apps receptive to WebSocket events. It supports two modes:
|
|
17
|
+
|
|
18
|
+
- **Standalone** — dedicated WebSocket server using `WsApp` or `MoostWs` with `listen()`
|
|
19
|
+
- **HTTP-integrated** (recommended) — shares the HTTP port with `@moostjs/event-http` via `@Upgrade()` routes
|
|
20
|
+
|
|
21
|
+
## Installation
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
npm install @moostjs/event-ws
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
For HTTP-integrated mode (recommended):
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
npm install @moostjs/event-ws @moostjs/event-http
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Quick Start
|
|
34
|
+
|
|
35
|
+
### Standalone Mode
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
import { WsApp, Message, MessageData, Connect, ConnectionId } from '@moostjs/event-ws'
|
|
39
|
+
import { Controller } from 'moost'
|
|
40
|
+
|
|
41
|
+
@Controller()
|
|
42
|
+
class ChatController {
|
|
43
|
+
@Connect()
|
|
44
|
+
onConnect(@ConnectionId() id: string) {
|
|
45
|
+
console.log(`Connected: ${id}`)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
@Message('echo', '/echo')
|
|
49
|
+
echo(@MessageData() data: unknown) {
|
|
50
|
+
return data
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
new WsApp()
|
|
55
|
+
.controllers(ChatController)
|
|
56
|
+
.start(3000)
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### HTTP-Integrated Mode
|
|
60
|
+
|
|
61
|
+
```ts
|
|
62
|
+
import { MoostHttp, Upgrade } from '@moostjs/event-http'
|
|
63
|
+
import { MoostWs, Message, MessageData, useWsRooms } from '@moostjs/event-ws'
|
|
64
|
+
import { Moost, Controller, Param, Inject } from 'moost'
|
|
65
|
+
import type { WooksWs } from '@moostjs/event-ws'
|
|
66
|
+
|
|
67
|
+
@Controller()
|
|
68
|
+
class AppController {
|
|
69
|
+
constructor(@Inject('WooksWs') private ws: WooksWs) {}
|
|
70
|
+
|
|
71
|
+
@Upgrade('ws')
|
|
72
|
+
upgrade() { return this.ws.upgrade() }
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
@Controller('chat')
|
|
76
|
+
class ChatController {
|
|
77
|
+
@Message('message', ':room')
|
|
78
|
+
onMessage(@Param('room') room: string, @MessageData() data: unknown) {
|
|
79
|
+
const { broadcast } = useWsRooms()
|
|
80
|
+
broadcast('message', data)
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const app = new Moost()
|
|
85
|
+
const http = new MoostHttp()
|
|
86
|
+
const ws = new MoostWs({ httpApp: http.getHttpApp() })
|
|
87
|
+
app.adapter(http)
|
|
88
|
+
app.adapter(ws)
|
|
89
|
+
app.registerControllers(AppController, ChatController)
|
|
90
|
+
await http.listen(3000)
|
|
91
|
+
await app.init()
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## [Official Documentation](https://moost.org/wsapp/)
|
|
95
|
+
|
|
96
|
+
## AI Agent Skills
|
|
97
|
+
|
|
98
|
+
This package ships skills for AI coding agents (Claude Code, Cursor, Windsurf, Codex, OpenCode). After installing `@moostjs/event-ws`, set up the skills:
|
|
99
|
+
|
|
100
|
+
```bash
|
|
101
|
+
# Project-local (recommended — version-locked, commits with your repo)
|
|
102
|
+
npx moostjs-event-ws-skill
|
|
103
|
+
|
|
104
|
+
# Global (available across all your projects)
|
|
105
|
+
npx moostjs-event-ws-skill --global
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
To auto-install skills on `npm install`, add to your `package.json`:
|
|
109
|
+
|
|
110
|
+
```json
|
|
111
|
+
{
|
|
112
|
+
"scripts": {
|
|
113
|
+
"postinstall": "moostjs-event-ws-skill --postinstall"
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
## Contributing
|
|
119
|
+
|
|
120
|
+
We are excited to welcome contributors who are passionate about improving Moostjs. No matter your level of experience, your unique perspective and skills can make valuable contributions to our growing community.
|
|
121
|
+
|
|
122
|
+
Here are some basic steps to get you started:
|
|
123
|
+
|
|
124
|
+
1. **Fork the Repo:** Navigate to [moostjs](https://github.com/moostjs/moostjs) and fork the repository to your own GitHub account.
|
|
125
|
+
|
|
126
|
+
2. **Clone the Repo:** Clone the forked repository to your local machine.
|
|
127
|
+
|
|
128
|
+
3. **Create a Branch:** Make a new branch for your feature or bug fix.
|
|
129
|
+
|
|
130
|
+
4. **Make your Changes:** Implement your feature or fix the bug and commit the changes to your branch.
|
|
131
|
+
|
|
132
|
+
5. **Make a Pull Request:** Navigate back to your forked repo and press the "New pull request" button.
|
|
133
|
+
|
|
134
|
+
Don't hesitate to ask for help if you need it. We believe in fostering a friendly and respectful environment for all contributors.
|
|
135
|
+
|
|
136
|
+
Thank you for your interest in Moostjs. We look forward to building something amazing together!
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,445 @@
|
|
|
1
|
+
//#region rolldown:runtime
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
10
|
+
key = keys[i];
|
|
11
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
12
|
+
get: ((k) => from[k]).bind(null, key),
|
|
13
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
19
|
+
value: mod,
|
|
20
|
+
enumerable: true
|
|
21
|
+
}) : target, mod));
|
|
22
|
+
|
|
23
|
+
//#endregion
|
|
24
|
+
const moost = __toESM(require("moost"));
|
|
25
|
+
const __wooksjs_event_ws = __toESM(require("@wooksjs/event-ws"));
|
|
26
|
+
const __prostojs_infact = __toESM(require("@prostojs/infact"));
|
|
27
|
+
|
|
28
|
+
//#region packages/event-ws/src/decorators/ws-method.decorator.ts
|
|
29
|
+
/**
|
|
30
|
+
* ## Define WebSocket Message Handler
|
|
31
|
+
* ### @MethodDecorator
|
|
32
|
+
*
|
|
33
|
+
* Registers a handler for routed WebSocket messages.
|
|
34
|
+
* The event and path correspond to the WsClientMessage protocol fields.
|
|
35
|
+
*
|
|
36
|
+
* @param event - message event type (e.g. "message", "rpc", "subscribe")
|
|
37
|
+
* @param path - route path with optional params (e.g. "/chat/rooms/:roomId")
|
|
38
|
+
*/ function Message(event, path) {
|
|
39
|
+
return (0, moost.getMoostMate)().decorate("handlers", {
|
|
40
|
+
event,
|
|
41
|
+
path,
|
|
42
|
+
type: "WS_MESSAGE"
|
|
43
|
+
}, true);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* ## Define WebSocket Connection Handler
|
|
47
|
+
* ### @MethodDecorator
|
|
48
|
+
*
|
|
49
|
+
* Registers a handler that runs when a new WebSocket connection is established.
|
|
50
|
+
* Runs inside the connection context. Throwing or rejecting closes the connection.
|
|
51
|
+
*/ function Connect() {
|
|
52
|
+
return (0, moost.getMoostMate)().decorate("handlers", { type: "WS_CONNECT" }, true);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* ## Define WebSocket Disconnection Handler
|
|
56
|
+
* ### @MethodDecorator
|
|
57
|
+
*
|
|
58
|
+
* Registers a handler that runs when a WebSocket connection closes.
|
|
59
|
+
*/ function Disconnect() {
|
|
60
|
+
return (0, moost.getMoostMate)().decorate("handlers", { type: "WS_DISCONNECT" }, true);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
//#endregion
|
|
64
|
+
//#region packages/event-ws/src/decorators/resolve.decorator.ts
|
|
65
|
+
/**
|
|
66
|
+
* Get the parsed WebSocket message payload.
|
|
67
|
+
* Only available in message handlers (@Message).
|
|
68
|
+
* @decorator
|
|
69
|
+
* @paramType T
|
|
70
|
+
*/ function MessageData() {
|
|
71
|
+
return (0, moost.Resolve)(() => (0, __wooksjs_event_ws.useWsMessage)().data, "ws_data");
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Get the raw WebSocket message (Buffer or string before parsing).
|
|
75
|
+
* Only available in message handlers (@Message).
|
|
76
|
+
* @decorator
|
|
77
|
+
* @paramType Buffer | string
|
|
78
|
+
*/ function RawMessage() {
|
|
79
|
+
return (0, moost.Resolve)(() => (0, __wooksjs_event_ws.useWsMessage)().raw, "ws_raw");
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Get the WebSocket message correlation ID.
|
|
83
|
+
* Only available in message handlers (@Message).
|
|
84
|
+
* @decorator
|
|
85
|
+
* @paramType string | number | undefined
|
|
86
|
+
*/ function MessageId() {
|
|
87
|
+
return (0, moost.Resolve)(() => (0, __wooksjs_event_ws.useWsMessage)().id, "ws_message_id");
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Get the WebSocket message event type.
|
|
91
|
+
* Only available in message handlers (@Message).
|
|
92
|
+
* @decorator
|
|
93
|
+
* @paramType string
|
|
94
|
+
*/ function MessageType() {
|
|
95
|
+
return (0, moost.Resolve)(() => (0, __wooksjs_event_ws.useWsMessage)().event, "ws_event");
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Get the WebSocket message path.
|
|
99
|
+
* Only available in message handlers (@Message).
|
|
100
|
+
* @decorator
|
|
101
|
+
* @paramType string
|
|
102
|
+
*/ function MessagePath() {
|
|
103
|
+
return (0, moost.Resolve)(() => (0, __wooksjs_event_ws.useWsMessage)().path, "ws_path");
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Get the WebSocket connection ID (UUID).
|
|
107
|
+
* Available in all WS handlers (@Message, @Connect, @Disconnect).
|
|
108
|
+
* @decorator
|
|
109
|
+
* @paramType string
|
|
110
|
+
*/ function ConnectionId() {
|
|
111
|
+
return (0, moost.Resolve)(() => (0, __wooksjs_event_ws.useWsConnection)().id, "ws_connection_id");
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
//#endregion
|
|
115
|
+
//#region packages/event-ws/src/event-ws.ts
|
|
116
|
+
function _define_property$1(obj, key, value) {
|
|
117
|
+
if (key in obj) Object.defineProperty(obj, key, {
|
|
118
|
+
value,
|
|
119
|
+
enumerable: true,
|
|
120
|
+
configurable: true,
|
|
121
|
+
writable: true
|
|
122
|
+
});
|
|
123
|
+
else obj[key] = value;
|
|
124
|
+
return obj;
|
|
125
|
+
}
|
|
126
|
+
const LOGGER_TITLE = "moost-ws";
|
|
127
|
+
/**
|
|
128
|
+
* ## Moost WebSocket Adapter
|
|
129
|
+
*
|
|
130
|
+
* Moost Adapter for WebSocket events, wrapping @wooksjs/event-ws.
|
|
131
|
+
* Supports standalone and HTTP-integrated modes.
|
|
132
|
+
*
|
|
133
|
+
* ### HTTP-integrated mode (recommended)
|
|
134
|
+
* ```ts
|
|
135
|
+
* | import { MoostHttp, Upgrade } from '@moostjs/event-http'
|
|
136
|
+
* | import { MoostWs, Message, MessageData } from '@moostjs/event-ws'
|
|
137
|
+
* | import { Moost, Param, Controller, Injectable } from 'moost'
|
|
138
|
+
* |
|
|
139
|
+
* | @Controller()
|
|
140
|
+
* | class AppController {
|
|
141
|
+
* | constructor(private ws: WooksWs) {}
|
|
142
|
+
* |
|
|
143
|
+
* | @Upgrade('/ws')
|
|
144
|
+
* | handleUpgrade() { return this.ws.upgrade() }
|
|
145
|
+
* | }
|
|
146
|
+
* |
|
|
147
|
+
* | @Controller('chat')
|
|
148
|
+
* | class ChatController {
|
|
149
|
+
* | @Message('message', 'rooms/:roomId')
|
|
150
|
+
* | onMessage(@Param('roomId') roomId: string, @MessageData() data: unknown) {
|
|
151
|
+
* | return { received: true, roomId }
|
|
152
|
+
* | }
|
|
153
|
+
* | }
|
|
154
|
+
* |
|
|
155
|
+
* | const app = new Moost()
|
|
156
|
+
* | const http = new MoostHttp()
|
|
157
|
+
* | const ws = new MoostWs({ httpApp: http.getHttpApp() })
|
|
158
|
+
* | app.adapter(http)
|
|
159
|
+
* | app.adapter(ws)
|
|
160
|
+
* | app.registerControllers(AppController, ChatController)
|
|
161
|
+
* | http.listen(3000)
|
|
162
|
+
* | app.init()
|
|
163
|
+
* ```
|
|
164
|
+
*/ var MoostWs = class MoostWs {
|
|
165
|
+
getWsApp() {
|
|
166
|
+
return this.wsApp;
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Start a standalone WebSocket server (without HTTP integration).
|
|
170
|
+
*/ listen(port, hostname) {
|
|
171
|
+
return this.wsApp.listen(port, hostname);
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Stop the server, close all connections, clean up heartbeat.
|
|
175
|
+
*/ close() {
|
|
176
|
+
return this.wsApp.close();
|
|
177
|
+
}
|
|
178
|
+
onInit(moost$1) {
|
|
179
|
+
this.moost = moost$1;
|
|
180
|
+
}
|
|
181
|
+
getProvideRegistry() {
|
|
182
|
+
return (0, __prostojs_infact.createProvideRegistry)([MoostWs, () => this], ["MoostWs", () => this], [__wooksjs_event_ws.WooksWs, () => this.getWsApp()], ["WooksWs", () => this.getWsApp()]);
|
|
183
|
+
}
|
|
184
|
+
getLogger() {
|
|
185
|
+
return this.wsApp.getLogger("[moost-ws]");
|
|
186
|
+
}
|
|
187
|
+
bindHandler(opts) {
|
|
188
|
+
for (const handler of opts.handlers) if (handler.type === "WS_MESSAGE") this.bindMessageHandler(opts, handler);
|
|
189
|
+
else if (handler.type === "WS_CONNECT") this.bindConnectHandler(opts);
|
|
190
|
+
else if (handler.type === "WS_DISCONNECT") this.bindDisconnectHandler(opts);
|
|
191
|
+
}
|
|
192
|
+
bindMessageHandler(opts, handler) {
|
|
193
|
+
const event = handler.event;
|
|
194
|
+
const path = typeof handler.path === "string" ? handler.path : typeof opts.method === "string" ? opts.method : "";
|
|
195
|
+
const targetPath = `${`${opts.prefix || ""}/${path}`.replaceAll(/\/\/+/g, "/")}`;
|
|
196
|
+
const fn = (0, moost.defineMoostEventHandler)({
|
|
197
|
+
contextType: "WS_MESSAGE",
|
|
198
|
+
loggerTitle: LOGGER_TITLE,
|
|
199
|
+
getIterceptorHandler: opts.getIterceptorHandler,
|
|
200
|
+
getControllerInstance: opts.getInstance,
|
|
201
|
+
controllerMethod: opts.method,
|
|
202
|
+
controllerName: opts.controllerName,
|
|
203
|
+
resolveArgs: opts.resolveArgs,
|
|
204
|
+
targetPath,
|
|
205
|
+
handlerType: handler.type
|
|
206
|
+
});
|
|
207
|
+
this.wsApp.onMessage(event, targetPath, fn);
|
|
208
|
+
opts.logHandler(`[36m(ws:${event})[32m${targetPath}`);
|
|
209
|
+
opts.register(handler, targetPath, []);
|
|
210
|
+
}
|
|
211
|
+
bindConnectHandler(opts) {
|
|
212
|
+
const fn = (0, moost.defineMoostEventHandler)({
|
|
213
|
+
contextType: "WS_CONNECT",
|
|
214
|
+
loggerTitle: LOGGER_TITLE,
|
|
215
|
+
getIterceptorHandler: opts.getIterceptorHandler,
|
|
216
|
+
getControllerInstance: opts.getInstance,
|
|
217
|
+
controllerMethod: opts.method,
|
|
218
|
+
resolveArgs: opts.resolveArgs,
|
|
219
|
+
targetPath: "__ws_connect__",
|
|
220
|
+
handlerType: "WS_CONNECT"
|
|
221
|
+
});
|
|
222
|
+
this.wsApp.onConnect(fn);
|
|
223
|
+
opts.logHandler(`[36m(ws:connect)`);
|
|
224
|
+
opts.register({ type: "WS_CONNECT" }, "__ws_connect__", []);
|
|
225
|
+
}
|
|
226
|
+
bindDisconnectHandler(opts) {
|
|
227
|
+
const fn = (0, moost.defineMoostEventHandler)({
|
|
228
|
+
contextType: "WS_DISCONNECT",
|
|
229
|
+
loggerTitle: LOGGER_TITLE,
|
|
230
|
+
getIterceptorHandler: opts.getIterceptorHandler,
|
|
231
|
+
getControllerInstance: opts.getInstance,
|
|
232
|
+
controllerMethod: opts.method,
|
|
233
|
+
resolveArgs: opts.resolveArgs,
|
|
234
|
+
targetPath: "__ws_disconnect__",
|
|
235
|
+
handlerType: "WS_DISCONNECT"
|
|
236
|
+
});
|
|
237
|
+
this.wsApp.onDisconnect(fn);
|
|
238
|
+
opts.logHandler(`[36m(ws:disconnect)`);
|
|
239
|
+
opts.register({ type: "WS_DISCONNECT" }, "__ws_disconnect__", []);
|
|
240
|
+
}
|
|
241
|
+
constructor(opts) {
|
|
242
|
+
_define_property$1(this, "opts", void 0);
|
|
243
|
+
_define_property$1(this, "name", void 0);
|
|
244
|
+
_define_property$1(this, "wsApp", void 0);
|
|
245
|
+
_define_property$1(this, "moost", void 0);
|
|
246
|
+
this.opts = opts;
|
|
247
|
+
this.name = "ws";
|
|
248
|
+
const wsOpts = opts?.wooksWs;
|
|
249
|
+
if (wsOpts && wsOpts instanceof __wooksjs_event_ws.WooksWs) this.wsApp = wsOpts;
|
|
250
|
+
else {
|
|
251
|
+
const httpApp = opts?.httpApp && "getHttpApp" in opts.httpApp ? opts.httpApp.getHttpApp() : opts?.httpApp;
|
|
252
|
+
if (httpApp) this.wsApp = (0, __wooksjs_event_ws.createWsApp)(httpApp, wsOpts || {});
|
|
253
|
+
else this.wsApp = (0, __wooksjs_event_ws.createWsApp)(wsOpts || {});
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
};
|
|
257
|
+
|
|
258
|
+
//#endregion
|
|
259
|
+
//#region packages/event-ws/src/quick-ws.ts
|
|
260
|
+
function _define_property(obj, key, value) {
|
|
261
|
+
if (key in obj) Object.defineProperty(obj, key, {
|
|
262
|
+
value,
|
|
263
|
+
enumerable: true,
|
|
264
|
+
configurable: true,
|
|
265
|
+
writable: true
|
|
266
|
+
});
|
|
267
|
+
else obj[key] = value;
|
|
268
|
+
return obj;
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* Quick WS App factory class.
|
|
272
|
+
*
|
|
273
|
+
* Use this class to quickly build a standalone WebSocket application
|
|
274
|
+
* with controllers. It extends the Moost class and wraps MoostWs initialization.
|
|
275
|
+
*
|
|
276
|
+
* @example
|
|
277
|
+
* ```typescript
|
|
278
|
+
* import { WsApp } from '@moostjs/event-ws'
|
|
279
|
+
* import { ChatController } from './chat.controller.ts'
|
|
280
|
+
*
|
|
281
|
+
* new WsApp()
|
|
282
|
+
* .controllers(ChatController)
|
|
283
|
+
* .start(3000)
|
|
284
|
+
* ```
|
|
285
|
+
*/ var WsApp = class extends moost.Moost {
|
|
286
|
+
/**
|
|
287
|
+
* Registers one or more WS controllers.
|
|
288
|
+
*
|
|
289
|
+
* (Shortcut for `registerControllers` method.)
|
|
290
|
+
*/ controllers(...controllers) {
|
|
291
|
+
return this.registerControllers(...controllers);
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Configures the WS options.
|
|
295
|
+
*/ useWsOptions(wsOpts) {
|
|
296
|
+
this._wsOpts = wsOpts;
|
|
297
|
+
return this;
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
* Returns the underlying MoostWs adapter instance.
|
|
301
|
+
*/ getWsAdapter() {
|
|
302
|
+
return this._wsAdapter;
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* Starts the standalone WebSocket application.
|
|
306
|
+
*
|
|
307
|
+
* @param port - Port to listen on
|
|
308
|
+
* @param hostname - Optional hostname
|
|
309
|
+
*/ async start(port, hostname) {
|
|
310
|
+
this._wsAdapter = new MoostWs({ wooksWs: this._wsOpts?.ws });
|
|
311
|
+
this.adapter(this._wsAdapter);
|
|
312
|
+
await this.init();
|
|
313
|
+
return this._wsAdapter.listen(port, hostname);
|
|
314
|
+
}
|
|
315
|
+
constructor(...args) {
|
|
316
|
+
super(...args), _define_property(this, "_wsOpts", void 0), _define_property(this, "_wsAdapter", void 0);
|
|
317
|
+
}
|
|
318
|
+
};
|
|
319
|
+
|
|
320
|
+
//#endregion
|
|
321
|
+
exports.Connect = Connect;
|
|
322
|
+
exports.ConnectionId = ConnectionId;
|
|
323
|
+
Object.defineProperty(exports, 'Controller', {
|
|
324
|
+
enumerable: true,
|
|
325
|
+
get: function () {
|
|
326
|
+
return moost.Controller;
|
|
327
|
+
}
|
|
328
|
+
});
|
|
329
|
+
Object.defineProperty(exports, 'Description', {
|
|
330
|
+
enumerable: true,
|
|
331
|
+
get: function () {
|
|
332
|
+
return moost.Description;
|
|
333
|
+
}
|
|
334
|
+
});
|
|
335
|
+
exports.Disconnect = Disconnect;
|
|
336
|
+
Object.defineProperty(exports, 'Intercept', {
|
|
337
|
+
enumerable: true,
|
|
338
|
+
get: function () {
|
|
339
|
+
return moost.Intercept;
|
|
340
|
+
}
|
|
341
|
+
});
|
|
342
|
+
exports.Message = Message;
|
|
343
|
+
exports.MessageData = MessageData;
|
|
344
|
+
exports.MessageId = MessageId;
|
|
345
|
+
exports.MessagePath = MessagePath;
|
|
346
|
+
exports.MessageType = MessageType;
|
|
347
|
+
exports.MoostWs = MoostWs;
|
|
348
|
+
Object.defineProperty(exports, 'Param', {
|
|
349
|
+
enumerable: true,
|
|
350
|
+
get: function () {
|
|
351
|
+
return moost.Param;
|
|
352
|
+
}
|
|
353
|
+
});
|
|
354
|
+
exports.RawMessage = RawMessage;
|
|
355
|
+
Object.defineProperty(exports, 'WooksWs', {
|
|
356
|
+
enumerable: true,
|
|
357
|
+
get: function () {
|
|
358
|
+
return __wooksjs_event_ws.WooksWs;
|
|
359
|
+
}
|
|
360
|
+
});
|
|
361
|
+
exports.WsApp = WsApp;
|
|
362
|
+
Object.defineProperty(exports, 'WsError', {
|
|
363
|
+
enumerable: true,
|
|
364
|
+
get: function () {
|
|
365
|
+
return __wooksjs_event_ws.WsError;
|
|
366
|
+
}
|
|
367
|
+
});
|
|
368
|
+
Object.defineProperty(exports, 'WsRoomManager', {
|
|
369
|
+
enumerable: true,
|
|
370
|
+
get: function () {
|
|
371
|
+
return __wooksjs_event_ws.WsRoomManager;
|
|
372
|
+
}
|
|
373
|
+
});
|
|
374
|
+
Object.defineProperty(exports, 'currentConnection', {
|
|
375
|
+
enumerable: true,
|
|
376
|
+
get: function () {
|
|
377
|
+
return __wooksjs_event_ws.currentConnection;
|
|
378
|
+
}
|
|
379
|
+
});
|
|
380
|
+
Object.defineProperty(exports, 'defineAfterInterceptor', {
|
|
381
|
+
enumerable: true,
|
|
382
|
+
get: function () {
|
|
383
|
+
return moost.defineAfterInterceptor;
|
|
384
|
+
}
|
|
385
|
+
});
|
|
386
|
+
Object.defineProperty(exports, 'defineBeforeInterceptor', {
|
|
387
|
+
enumerable: true,
|
|
388
|
+
get: function () {
|
|
389
|
+
return moost.defineBeforeInterceptor;
|
|
390
|
+
}
|
|
391
|
+
});
|
|
392
|
+
Object.defineProperty(exports, 'defineInterceptor', {
|
|
393
|
+
enumerable: true,
|
|
394
|
+
get: function () {
|
|
395
|
+
return moost.defineInterceptor;
|
|
396
|
+
}
|
|
397
|
+
});
|
|
398
|
+
Object.defineProperty(exports, 'prepareTestWsConnectionContext', {
|
|
399
|
+
enumerable: true,
|
|
400
|
+
get: function () {
|
|
401
|
+
return __wooksjs_event_ws.prepareTestWsConnectionContext;
|
|
402
|
+
}
|
|
403
|
+
});
|
|
404
|
+
Object.defineProperty(exports, 'prepareTestWsMessageContext', {
|
|
405
|
+
enumerable: true,
|
|
406
|
+
get: function () {
|
|
407
|
+
return __wooksjs_event_ws.prepareTestWsMessageContext;
|
|
408
|
+
}
|
|
409
|
+
});
|
|
410
|
+
Object.defineProperty(exports, 'useWsConnection', {
|
|
411
|
+
enumerable: true,
|
|
412
|
+
get: function () {
|
|
413
|
+
return __wooksjs_event_ws.useWsConnection;
|
|
414
|
+
}
|
|
415
|
+
});
|
|
416
|
+
Object.defineProperty(exports, 'useWsMessage', {
|
|
417
|
+
enumerable: true,
|
|
418
|
+
get: function () {
|
|
419
|
+
return __wooksjs_event_ws.useWsMessage;
|
|
420
|
+
}
|
|
421
|
+
});
|
|
422
|
+
Object.defineProperty(exports, 'useWsRooms', {
|
|
423
|
+
enumerable: true,
|
|
424
|
+
get: function () {
|
|
425
|
+
return __wooksjs_event_ws.useWsRooms;
|
|
426
|
+
}
|
|
427
|
+
});
|
|
428
|
+
Object.defineProperty(exports, 'useWsServer', {
|
|
429
|
+
enumerable: true,
|
|
430
|
+
get: function () {
|
|
431
|
+
return __wooksjs_event_ws.useWsServer;
|
|
432
|
+
}
|
|
433
|
+
});
|
|
434
|
+
Object.defineProperty(exports, 'wsConnectionKind', {
|
|
435
|
+
enumerable: true,
|
|
436
|
+
get: function () {
|
|
437
|
+
return __wooksjs_event_ws.wsConnectionKind;
|
|
438
|
+
}
|
|
439
|
+
});
|
|
440
|
+
Object.defineProperty(exports, 'wsMessageKind', {
|
|
441
|
+
enumerable: true,
|
|
442
|
+
get: function () {
|
|
443
|
+
return __wooksjs_event_ws.wsMessageKind;
|
|
444
|
+
}
|
|
445
|
+
});
|