@pluv/platform-node 0.0.0-experimental-20250527040415
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/.turbo/turbo-build.log +19 -0
- package/.turbo/turbo-lint.log +5 -0
- package/CHANGELOG.md +1623 -0
- package/LICENSE +21 -0
- package/README.md +18 -0
- package/dist/index.d.mts +134 -0
- package/dist/index.d.ts +134 -0
- package/dist/index.js +370 -0
- package/dist/index.mjs +334 -0
- package/eslint.config.mjs +4 -0
- package/package.json +43 -0
- package/src/NodePlatform.ts +144 -0
- package/src/NodeWebSocket.ts +144 -0
- package/src/createPluvHandler.ts +202 -0
- package/src/index.ts +12 -0
- package/src/infer.ts +9 -0
- package/src/platformNode.ts +50 -0
- package/src/utils/identity.ts +1 -0
- package/src/utils/index.ts +1 -0
- package/tsconfig.json +8 -0
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
}
|
|
17
|
+
return a;
|
|
18
|
+
};
|
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
+
var __async = (__this, __arguments, generator) => {
|
|
21
|
+
return new Promise((resolve, reject) => {
|
|
22
|
+
var fulfilled = (value) => {
|
|
23
|
+
try {
|
|
24
|
+
step(generator.next(value));
|
|
25
|
+
} catch (e) {
|
|
26
|
+
reject(e);
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
var rejected = (value) => {
|
|
30
|
+
try {
|
|
31
|
+
step(generator.throw(value));
|
|
32
|
+
} catch (e) {
|
|
33
|
+
reject(e);
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
37
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
38
|
+
});
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
// src/createPluvHandler.ts
|
|
42
|
+
import Url from "url";
|
|
43
|
+
import { match } from "path-to-regexp";
|
|
44
|
+
import { WebSocketServer } from "ws";
|
|
45
|
+
var handle = (ws) => ({
|
|
46
|
+
invalidEndpoint: () => {
|
|
47
|
+
ws.close(1003, "Invalid WebSocket endpoint");
|
|
48
|
+
return { matched: false };
|
|
49
|
+
},
|
|
50
|
+
invalidRoom: () => {
|
|
51
|
+
ws.close(1003, "Invalid room name");
|
|
52
|
+
return { matched: true };
|
|
53
|
+
},
|
|
54
|
+
invalidToken: () => {
|
|
55
|
+
ws.close(1003, "Invalid authentication token");
|
|
56
|
+
return { matched: true };
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
var createPluvHandler = (config) => {
|
|
60
|
+
const { authorize, endpoint = "/api/pluv", io, server } = config;
|
|
61
|
+
if (io._defs.platform._config.registrationMode === "detached") {
|
|
62
|
+
throw new Error("Cannot use createPluvHandler in detached mode for Node.js");
|
|
63
|
+
}
|
|
64
|
+
const wsServer = new WebSocketServer({ server });
|
|
65
|
+
const rooms = /* @__PURE__ */ new Map();
|
|
66
|
+
const getRoom = (roomId) => {
|
|
67
|
+
Array.from(rooms.values()).forEach((room) => {
|
|
68
|
+
if (!room.getSize()) rooms.delete(room.id);
|
|
69
|
+
});
|
|
70
|
+
const existing = rooms.get(roomId);
|
|
71
|
+
if (existing) return existing;
|
|
72
|
+
const newRoom = io.createRoom(roomId, {
|
|
73
|
+
onDestroy: (event) => {
|
|
74
|
+
rooms.delete(event.room);
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
rooms.set(roomId, newRoom);
|
|
78
|
+
return newRoom;
|
|
79
|
+
};
|
|
80
|
+
const wsHandler = (ws, req) => __async(null, null, function* () {
|
|
81
|
+
var _a;
|
|
82
|
+
const url = req.url;
|
|
83
|
+
const onError = handle(ws);
|
|
84
|
+
if (!url) {
|
|
85
|
+
onError.invalidEndpoint();
|
|
86
|
+
return { matched: false };
|
|
87
|
+
}
|
|
88
|
+
const parsed = Url.parse(url, true);
|
|
89
|
+
const { pathname } = parsed;
|
|
90
|
+
if (!pathname) return onError.invalidEndpoint();
|
|
91
|
+
const matcher = match(`${endpoint}/room/:roomId`);
|
|
92
|
+
const matched = matcher(pathname);
|
|
93
|
+
if (!matched) return onError.invalidEndpoint();
|
|
94
|
+
const { roomId } = matched.params;
|
|
95
|
+
if (!roomId) return onError.invalidRoom();
|
|
96
|
+
const token = ((_a = parsed.query) == null ? void 0 : _a.token) || void 0;
|
|
97
|
+
if (Array.isArray(token)) return onError.invalidToken();
|
|
98
|
+
const room = getRoom(roomId);
|
|
99
|
+
yield room.register(ws, { req, token });
|
|
100
|
+
return { matched: true };
|
|
101
|
+
});
|
|
102
|
+
const createWsServer = () => {
|
|
103
|
+
return wsServer.on("connection", (ws, req) => __async(null, null, function* () {
|
|
104
|
+
yield wsHandler(ws, req);
|
|
105
|
+
}));
|
|
106
|
+
};
|
|
107
|
+
const handler = (req, res, next) => __async(null, null, function* () {
|
|
108
|
+
var _a, _b, _c, _d, _e, _f;
|
|
109
|
+
if (!authorize) return (_a = next == null ? void 0 : next()) != null ? _a : null;
|
|
110
|
+
if (req.method !== "GET") return (_b = next == null ? void 0 : next()) != null ? _b : null;
|
|
111
|
+
const url = req.url;
|
|
112
|
+
if (!url) return (_c = next == null ? void 0 : next()) != null ? _c : null;
|
|
113
|
+
const parsed = Url.parse(url, true);
|
|
114
|
+
const { pathname } = parsed;
|
|
115
|
+
if (!pathname) return (_d = next == null ? void 0 : next()) != null ? _d : null;
|
|
116
|
+
const matcher = match(`${endpoint}/authorize`);
|
|
117
|
+
const matched = matcher(pathname);
|
|
118
|
+
if (!matched) return (_e = next == null ? void 0 : next()) != null ? _e : null;
|
|
119
|
+
const room = ((_f = parsed.query) == null ? void 0 : _f.room) || void 0;
|
|
120
|
+
if (typeof room !== "string" || !room) {
|
|
121
|
+
res.writeHead(404, { "Content-Type": "text/plain" });
|
|
122
|
+
return res.end("Room does not exist");
|
|
123
|
+
}
|
|
124
|
+
try {
|
|
125
|
+
const user = yield authorize({ req, room });
|
|
126
|
+
if (!user) throw new Error();
|
|
127
|
+
const token = yield io.createToken({
|
|
128
|
+
req,
|
|
129
|
+
room,
|
|
130
|
+
user
|
|
131
|
+
});
|
|
132
|
+
res.writeHead(200, { "Content-Type": "text/plain" });
|
|
133
|
+
return res.end(token);
|
|
134
|
+
} catch (err) {
|
|
135
|
+
res.writeHead(403, { "Content-Type": "text/plain" });
|
|
136
|
+
return res.end(err instanceof Error ? err.message : "Unauthorized");
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
return {
|
|
140
|
+
createWsServer,
|
|
141
|
+
handler,
|
|
142
|
+
wsHandler
|
|
143
|
+
};
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
// src/infer.ts
|
|
147
|
+
var infer = (callback) => callback;
|
|
148
|
+
|
|
149
|
+
// src/NodePlatform.ts
|
|
150
|
+
import { AbstractPlatform } from "@pluv/io";
|
|
151
|
+
import crypto2 from "crypto";
|
|
152
|
+
import { TextDecoder } from "util";
|
|
153
|
+
|
|
154
|
+
// src/NodeWebSocket.ts
|
|
155
|
+
import { AbstractWebSocket } from "@pluv/io";
|
|
156
|
+
import crypto from "crypto";
|
|
157
|
+
var NodeWebSocket = class extends AbstractWebSocket {
|
|
158
|
+
constructor(webSocket, config) {
|
|
159
|
+
const { room } = config;
|
|
160
|
+
super(webSocket, config);
|
|
161
|
+
this._sessionId = null;
|
|
162
|
+
this._user = null;
|
|
163
|
+
this._state = {
|
|
164
|
+
presence: null,
|
|
165
|
+
quit: false,
|
|
166
|
+
room,
|
|
167
|
+
timers: {
|
|
168
|
+
ping: (/* @__PURE__ */ new Date()).getTime(),
|
|
169
|
+
presence: null
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
set presence(presence) {
|
|
174
|
+
this._state.presence = presence;
|
|
175
|
+
}
|
|
176
|
+
get readyState() {
|
|
177
|
+
return this.webSocket.readyState;
|
|
178
|
+
}
|
|
179
|
+
get session() {
|
|
180
|
+
const sessionId = this.sessionId;
|
|
181
|
+
const state = this._state;
|
|
182
|
+
const user = this._user;
|
|
183
|
+
return __spreadProps(__spreadValues({}, state), {
|
|
184
|
+
id: sessionId,
|
|
185
|
+
user,
|
|
186
|
+
webSocket: this
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
get sessionId() {
|
|
190
|
+
var _a;
|
|
191
|
+
if (this._sessionId) return this._sessionId;
|
|
192
|
+
const sessionId = (_a = this._sessionId) != null ? _a : `p_${crypto.randomUUID()}`;
|
|
193
|
+
this._sessionId = sessionId;
|
|
194
|
+
return sessionId;
|
|
195
|
+
}
|
|
196
|
+
get state() {
|
|
197
|
+
return this._state;
|
|
198
|
+
}
|
|
199
|
+
set state(state) {
|
|
200
|
+
this._state = state;
|
|
201
|
+
}
|
|
202
|
+
set user(user) {
|
|
203
|
+
this._user = user;
|
|
204
|
+
}
|
|
205
|
+
addEventListener(type, handler) {
|
|
206
|
+
switch (type) {
|
|
207
|
+
case "close":
|
|
208
|
+
this.webSocket.on("close", (code, buffer) => __async(this, null, function* () {
|
|
209
|
+
const reason = buffer.toString("utf-8");
|
|
210
|
+
yield Promise.resolve(handler({ code, reason }));
|
|
211
|
+
}));
|
|
212
|
+
return;
|
|
213
|
+
case "error":
|
|
214
|
+
this.webSocket.on("error", (error) => __async(this, null, function* () {
|
|
215
|
+
const message = error.message;
|
|
216
|
+
yield Promise.resolve(
|
|
217
|
+
handler({ error, message })
|
|
218
|
+
);
|
|
219
|
+
}));
|
|
220
|
+
return;
|
|
221
|
+
case "message":
|
|
222
|
+
this.webSocket.on("message", (data) => __async(this, null, function* () {
|
|
223
|
+
yield Promise.resolve(handler({ data }));
|
|
224
|
+
}));
|
|
225
|
+
return;
|
|
226
|
+
default:
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
close(code, reason) {
|
|
230
|
+
const canClose = [this.CONNECTING, this.OPEN].some(
|
|
231
|
+
(readyState) => readyState === this.readyState
|
|
232
|
+
);
|
|
233
|
+
if (!canClose) return;
|
|
234
|
+
this.webSocket.close(code, reason);
|
|
235
|
+
}
|
|
236
|
+
send(message) {
|
|
237
|
+
if (this.readyState !== this.OPEN) return;
|
|
238
|
+
this.webSocket.send(message, (error) => {
|
|
239
|
+
if (error) throw error;
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
terminate() {
|
|
243
|
+
this.webSocket.terminate();
|
|
244
|
+
}
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
// src/NodePlatform.ts
|
|
248
|
+
var NodePlatform = class _NodePlatform extends AbstractPlatform {
|
|
249
|
+
constructor(config = {}) {
|
|
250
|
+
const { roomContext, mode = "attached", persistence, pubSub } = config;
|
|
251
|
+
super(__spreadValues({
|
|
252
|
+
roomContext
|
|
253
|
+
}, persistence && pubSub ? { persistence, pubSub } : {}));
|
|
254
|
+
this.id = crypto2.randomUUID();
|
|
255
|
+
this._name = "platformNode";
|
|
256
|
+
this._config = {
|
|
257
|
+
authorize: {
|
|
258
|
+
secret: true
|
|
259
|
+
},
|
|
260
|
+
handleMode: "io",
|
|
261
|
+
registrationMode: mode,
|
|
262
|
+
requireAuth: false,
|
|
263
|
+
listeners: {
|
|
264
|
+
onRoomDeleted: true,
|
|
265
|
+
onRoomMessage: true,
|
|
266
|
+
onStorageUpdated: true,
|
|
267
|
+
onUserConnected: true,
|
|
268
|
+
onUserDisconnected: true
|
|
269
|
+
},
|
|
270
|
+
router: true
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
acceptWebSocket(webSocket) {
|
|
274
|
+
return Promise.resolve(void 0);
|
|
275
|
+
}
|
|
276
|
+
convertWebSocket(webSocket, config) {
|
|
277
|
+
const { room } = config;
|
|
278
|
+
return new NodeWebSocket(webSocket, {
|
|
279
|
+
persistence: this.persistence,
|
|
280
|
+
platform: this,
|
|
281
|
+
room
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
getLastPing(webSocket) {
|
|
285
|
+
return null;
|
|
286
|
+
}
|
|
287
|
+
getSerializedState(webSocket) {
|
|
288
|
+
return null;
|
|
289
|
+
}
|
|
290
|
+
getSessionId(webSocket) {
|
|
291
|
+
return null;
|
|
292
|
+
}
|
|
293
|
+
getWebSockets() {
|
|
294
|
+
return [];
|
|
295
|
+
}
|
|
296
|
+
initialize(config) {
|
|
297
|
+
return new _NodePlatform({
|
|
298
|
+
roomContext: config.roomContext,
|
|
299
|
+
mode: this._config.registrationMode,
|
|
300
|
+
persistence: this.persistence.initialize(config.roomContext),
|
|
301
|
+
pubSub: this.pubSub
|
|
302
|
+
})._initialize();
|
|
303
|
+
}
|
|
304
|
+
parseData(data) {
|
|
305
|
+
if (typeof data === "string") return JSON.parse(data);
|
|
306
|
+
const decoder = new TextDecoder("utf8");
|
|
307
|
+
return JSON.parse(decoder.decode(data));
|
|
308
|
+
}
|
|
309
|
+
randomUUID() {
|
|
310
|
+
return crypto2.randomUUID();
|
|
311
|
+
}
|
|
312
|
+
setSerializedState(webSocket, state) {
|
|
313
|
+
webSocket.state = state;
|
|
314
|
+
return webSocket.state;
|
|
315
|
+
}
|
|
316
|
+
};
|
|
317
|
+
|
|
318
|
+
// src/platformNode.ts
|
|
319
|
+
var platformNode = (config = {}) => {
|
|
320
|
+
const { authorize, context, crdt, debug, limits } = config;
|
|
321
|
+
return {
|
|
322
|
+
authorize,
|
|
323
|
+
context,
|
|
324
|
+
crdt,
|
|
325
|
+
debug,
|
|
326
|
+
limits,
|
|
327
|
+
platform: () => new NodePlatform(config)
|
|
328
|
+
};
|
|
329
|
+
};
|
|
330
|
+
export {
|
|
331
|
+
createPluvHandler,
|
|
332
|
+
infer,
|
|
333
|
+
platformNode
|
|
334
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pluv/platform-node",
|
|
3
|
+
"version": "0.0.0-experimental-20250527040415",
|
|
4
|
+
"description": "@pluv/io adapter for node.js",
|
|
5
|
+
"author": "leedavidcs",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"homepage": "https://github.com/pluv-io/pluv",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/pluv-io/pluv.git",
|
|
11
|
+
"directory": "packages/platform-node"
|
|
12
|
+
},
|
|
13
|
+
"main": "./dist/index.js",
|
|
14
|
+
"module": "./dist/index.mjs",
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"publishConfig": {
|
|
17
|
+
"access": "public"
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@types/node": "^22.15.21",
|
|
21
|
+
"@types/ws": "^8.18.1",
|
|
22
|
+
"path-to-regexp": "^8.2.0",
|
|
23
|
+
"@pluv/io": "^0.0.0-experimental-20250527040415",
|
|
24
|
+
"@pluv/types": "^0.0.0-experimental-20250527040415"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"eslint": "^9.27.0",
|
|
28
|
+
"tsup": "^8.5.0",
|
|
29
|
+
"typescript": "^5.8.3",
|
|
30
|
+
"ws": "^8.18.2",
|
|
31
|
+
"@pluv/tsconfig": "^0.0.0-experimental-20250527040415",
|
|
32
|
+
"eslint-config-pluv": "^0.0.0-experimental-20250527040415"
|
|
33
|
+
},
|
|
34
|
+
"peerDependencies": {
|
|
35
|
+
"ws": "^8.0.0"
|
|
36
|
+
},
|
|
37
|
+
"scripts": {
|
|
38
|
+
"build": "tsup src/index.ts --format esm,cjs --dts",
|
|
39
|
+
"dev": "tsup src/index.ts --format esm,cjs --watch --dts",
|
|
40
|
+
"lint": "eslint \"src/**/*.ts*\" --fix --max-warnings 0",
|
|
41
|
+
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist"
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AbstractPersistence,
|
|
3
|
+
AbstractPlatformConfig,
|
|
4
|
+
AbstractPubSub,
|
|
5
|
+
ConvertWebSocketConfig,
|
|
6
|
+
WebSocketRegistrationMode,
|
|
7
|
+
WebSocketSerializedState,
|
|
8
|
+
} from "@pluv/io";
|
|
9
|
+
import { AbstractPlatform } from "@pluv/io";
|
|
10
|
+
import type { IOAuthorize, Json } from "@pluv/types";
|
|
11
|
+
import crypto from "node:crypto";
|
|
12
|
+
import type { IncomingMessage } from "node:http";
|
|
13
|
+
import { TextDecoder } from "node:util";
|
|
14
|
+
import type { WebSocket } from "ws";
|
|
15
|
+
import { NodeWebSocket } from "./NodeWebSocket";
|
|
16
|
+
|
|
17
|
+
export type NodePlatformRoomContext<TMeta extends Record<string, Json>> = keyof TMeta extends never
|
|
18
|
+
? { meta?: undefined }
|
|
19
|
+
: { meta: TMeta };
|
|
20
|
+
|
|
21
|
+
export type NodePlatformConfig<TMeta extends Record<string, Json>> = {
|
|
22
|
+
mode?: WebSocketRegistrationMode;
|
|
23
|
+
} & (
|
|
24
|
+
| { persistence?: undefined; pubSub?: undefined }
|
|
25
|
+
| { persistence: AbstractPersistence; pubSub: AbstractPubSub }
|
|
26
|
+
) & { roomContext?: NodePlatformRoomContext<TMeta> };
|
|
27
|
+
|
|
28
|
+
export class NodePlatform<
|
|
29
|
+
TAuthorize extends IOAuthorize<any, any> | null = null,
|
|
30
|
+
TMeta extends Record<string, Json> = {},
|
|
31
|
+
> extends AbstractPlatform<
|
|
32
|
+
NodeWebSocket<TAuthorize>,
|
|
33
|
+
{ req: IncomingMessage },
|
|
34
|
+
NodePlatformRoomContext<TMeta>,
|
|
35
|
+
{
|
|
36
|
+
authorize: {
|
|
37
|
+
secret: true;
|
|
38
|
+
};
|
|
39
|
+
handleMode: "io";
|
|
40
|
+
requireAuth: false;
|
|
41
|
+
registrationMode: WebSocketRegistrationMode;
|
|
42
|
+
listeners: {
|
|
43
|
+
onRoomDeleted: true;
|
|
44
|
+
onRoomMessage: true;
|
|
45
|
+
onStorageUpdated: true;
|
|
46
|
+
onUserConnected: true;
|
|
47
|
+
onUserDisconnected: true;
|
|
48
|
+
};
|
|
49
|
+
router: true;
|
|
50
|
+
}
|
|
51
|
+
> {
|
|
52
|
+
public readonly id = crypto.randomUUID();
|
|
53
|
+
public readonly _config;
|
|
54
|
+
public readonly _name = "platformNode";
|
|
55
|
+
|
|
56
|
+
constructor(config: NodePlatformConfig<TMeta> = {}) {
|
|
57
|
+
const { roomContext, mode = "attached", persistence, pubSub } = config;
|
|
58
|
+
|
|
59
|
+
super({
|
|
60
|
+
roomContext,
|
|
61
|
+
...(persistence && pubSub ? { persistence, pubSub } : {}),
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
this._config = {
|
|
65
|
+
authorize: {
|
|
66
|
+
secret: true as const,
|
|
67
|
+
},
|
|
68
|
+
handleMode: "io" as const,
|
|
69
|
+
registrationMode: mode,
|
|
70
|
+
requireAuth: false as const,
|
|
71
|
+
listeners: {
|
|
72
|
+
onRoomDeleted: true as const,
|
|
73
|
+
onRoomMessage: true as const,
|
|
74
|
+
onStorageUpdated: true as const,
|
|
75
|
+
onUserConnected: true as const,
|
|
76
|
+
onUserDisconnected: true as const,
|
|
77
|
+
},
|
|
78
|
+
router: true as const,
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
public acceptWebSocket(webSocket: NodeWebSocket<TAuthorize>): Promise<void> {
|
|
83
|
+
return Promise.resolve(undefined);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
public convertWebSocket(
|
|
87
|
+
webSocket: WebSocket,
|
|
88
|
+
config: ConvertWebSocketConfig,
|
|
89
|
+
): NodeWebSocket<TAuthorize> {
|
|
90
|
+
const { room } = config;
|
|
91
|
+
|
|
92
|
+
return new NodeWebSocket<TAuthorize>(webSocket, {
|
|
93
|
+
persistence: this.persistence,
|
|
94
|
+
platform: this,
|
|
95
|
+
room,
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
public getLastPing(webSocket: NodeWebSocket<TAuthorize>): number | null {
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
public getSerializedState(webSocket: WebSocket): WebSocketSerializedState | null {
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
public getSessionId(webSocket: WebSocket): string | null {
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
public getWebSockets(): readonly WebSocket[] {
|
|
112
|
+
return [];
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
public initialize(config: AbstractPlatformConfig<NodePlatformRoomContext<TMeta>>): this {
|
|
116
|
+
return new NodePlatform({
|
|
117
|
+
roomContext: config.roomContext,
|
|
118
|
+
mode: this._config.registrationMode,
|
|
119
|
+
persistence: this.persistence.initialize(config.roomContext),
|
|
120
|
+
pubSub: this.pubSub,
|
|
121
|
+
} as NodePlatformConfig<TMeta>)._initialize() as this;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
public parseData(data: string | ArrayBuffer): Record<string, any> {
|
|
125
|
+
if (typeof data === "string") return JSON.parse(data);
|
|
126
|
+
|
|
127
|
+
const decoder = new TextDecoder("utf8");
|
|
128
|
+
|
|
129
|
+
return JSON.parse(decoder.decode(data));
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
public randomUUID(): string {
|
|
133
|
+
return crypto.randomUUID();
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
public setSerializedState(
|
|
137
|
+
webSocket: NodeWebSocket<TAuthorize>,
|
|
138
|
+
state: WebSocketSerializedState,
|
|
139
|
+
): WebSocketSerializedState {
|
|
140
|
+
webSocket.state = state;
|
|
141
|
+
|
|
142
|
+
return webSocket.state;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AbstractCloseEvent,
|
|
3
|
+
AbstractErrorEvent,
|
|
4
|
+
AbstractEventMap,
|
|
5
|
+
AbstractListener,
|
|
6
|
+
AbstractMessageEvent,
|
|
7
|
+
AbstractWebSocketConfig,
|
|
8
|
+
WebSocketSerializedState,
|
|
9
|
+
WebSocketSession,
|
|
10
|
+
} from "@pluv/io";
|
|
11
|
+
import { AbstractWebSocket } from "@pluv/io";
|
|
12
|
+
import type { InferIOAuthorizeUser, IOAuthorize, JsonObject } from "@pluv/types";
|
|
13
|
+
import crypto from "node:crypto";
|
|
14
|
+
import type { WebSocket } from "ws";
|
|
15
|
+
|
|
16
|
+
export interface NodeWebSocketEventMap {
|
|
17
|
+
close: AbstractCloseEvent;
|
|
18
|
+
message: AbstractMessageEvent;
|
|
19
|
+
error: AbstractErrorEvent;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export type NodeWebSocketConfig = AbstractWebSocketConfig;
|
|
23
|
+
|
|
24
|
+
export class NodeWebSocket<
|
|
25
|
+
TAuthorize extends IOAuthorize<any, any> | null = null,
|
|
26
|
+
> extends AbstractWebSocket<WebSocket> {
|
|
27
|
+
private _sessionId: string | null = null;
|
|
28
|
+
private _state: WebSocketSerializedState;
|
|
29
|
+
private _user: InferIOAuthorizeUser<TAuthorize> = null as InferIOAuthorizeUser<TAuthorize>;
|
|
30
|
+
|
|
31
|
+
public set presence(presence: JsonObject | null) {
|
|
32
|
+
this._state.presence = presence;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
public get readyState(): 0 | 1 | 2 | 3 {
|
|
36
|
+
return this.webSocket.readyState;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
public get session(): WebSocketSession<TAuthorize> {
|
|
40
|
+
const sessionId = this.sessionId;
|
|
41
|
+
const state = this._state;
|
|
42
|
+
const user = this._user;
|
|
43
|
+
|
|
44
|
+
return {
|
|
45
|
+
...state,
|
|
46
|
+
id: sessionId,
|
|
47
|
+
user,
|
|
48
|
+
webSocket: this,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
public get sessionId(): string {
|
|
53
|
+
if (this._sessionId) return this._sessionId;
|
|
54
|
+
|
|
55
|
+
const sessionId = this._sessionId ?? `p_${crypto.randomUUID()}`;
|
|
56
|
+
|
|
57
|
+
this._sessionId = sessionId;
|
|
58
|
+
|
|
59
|
+
return sessionId;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
public get state(): WebSocketSerializedState {
|
|
63
|
+
return this._state;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
public set state(state: WebSocketSerializedState) {
|
|
67
|
+
this._state = state;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
public set user(user: InferIOAuthorizeUser<TAuthorize>) {
|
|
71
|
+
this._user = user;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
constructor(webSocket: WebSocket, config: NodeWebSocketConfig) {
|
|
75
|
+
const { room } = config;
|
|
76
|
+
|
|
77
|
+
super(webSocket, config);
|
|
78
|
+
|
|
79
|
+
this._state = {
|
|
80
|
+
presence: null,
|
|
81
|
+
quit: false,
|
|
82
|
+
room,
|
|
83
|
+
timers: {
|
|
84
|
+
ping: new Date().getTime(),
|
|
85
|
+
presence: null,
|
|
86
|
+
},
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
public addEventListener<TType extends keyof AbstractEventMap>(
|
|
91
|
+
type: TType,
|
|
92
|
+
handler: AbstractListener<TType>,
|
|
93
|
+
): void {
|
|
94
|
+
switch (type) {
|
|
95
|
+
case "close":
|
|
96
|
+
this.webSocket.on("close", async (code, buffer) => {
|
|
97
|
+
const reason = buffer.toString("utf-8");
|
|
98
|
+
|
|
99
|
+
await Promise.resolve((handler as AbstractListener<"close">)({ code, reason }));
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
return;
|
|
103
|
+
case "error":
|
|
104
|
+
this.webSocket.on("error", async (error) => {
|
|
105
|
+
const message = error.message;
|
|
106
|
+
|
|
107
|
+
await Promise.resolve(
|
|
108
|
+
(handler as AbstractListener<"error">)({ error, message }),
|
|
109
|
+
);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
return;
|
|
113
|
+
case "message":
|
|
114
|
+
this.webSocket.on("message", async (data) => {
|
|
115
|
+
await Promise.resolve((handler as AbstractListener<"message">)({ data }));
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
return;
|
|
119
|
+
default:
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
public close(code?: number | undefined, reason?: string | undefined): void {
|
|
124
|
+
const canClose = [this.CONNECTING, this.OPEN].some(
|
|
125
|
+
(readyState) => readyState === this.readyState,
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
if (!canClose) return;
|
|
129
|
+
|
|
130
|
+
this.webSocket.close(code, reason);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
public send(message: string | ArrayBuffer | ArrayBufferView): void {
|
|
134
|
+
if (this.readyState !== this.OPEN) return;
|
|
135
|
+
|
|
136
|
+
this.webSocket.send(message, (error) => {
|
|
137
|
+
if (error) throw error;
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
public terminate(): void {
|
|
142
|
+
this.webSocket.terminate();
|
|
143
|
+
}
|
|
144
|
+
}
|