@pluv/platform-node 0.40.2 → 0.41.1
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 +8 -8
- package/CHANGELOG.md +23 -0
- package/dist/index.d.mts +18 -15
- package/dist/index.d.ts +18 -15
- package/dist/index.js +36 -12
- package/dist/index.mjs +37 -13
- package/package.json +7 -7
- package/src/NodePlatform.ts +14 -8
- package/src/NodeWebSocket.ts +35 -6
- package/src/createPluvHandler.ts +4 -4
- package/src/platformNode.ts +13 -6
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
|
|
2
|
-
> @pluv/platform-node@0.
|
|
2
|
+
> @pluv/platform-node@0.41.1 build /home/runner/work/pluv/pluv/packages/platform-node
|
|
3
3
|
> tsup src/index.ts --format esm,cjs --dts
|
|
4
4
|
|
|
5
5
|
[34mCLI[39m Building entry: src/index.ts
|
|
@@ -8,11 +8,11 @@
|
|
|
8
8
|
[34mCLI[39m Target: es6
|
|
9
9
|
[34mESM[39m Build start
|
|
10
10
|
[34mCJS[39m Build start
|
|
11
|
-
[32mCJS[39m [1mdist/index.js [22m[
|
|
12
|
-
[32mCJS[39m ⚡️ Build success in
|
|
13
|
-
[32mESM[39m [1mdist/index.mjs [22m[
|
|
14
|
-
[32mESM[39m ⚡️ Build success in
|
|
11
|
+
[32mCJS[39m [1mdist/index.js [22m[32m11.23 KB[39m
|
|
12
|
+
[32mCJS[39m ⚡️ Build success in 92ms
|
|
13
|
+
[32mESM[39m [1mdist/index.mjs [22m[32m9.44 KB[39m
|
|
14
|
+
[32mESM[39m ⚡️ Build success in 93ms
|
|
15
15
|
[34mDTS[39m Build start
|
|
16
|
-
[32mDTS[39m ⚡️ Build success in
|
|
17
|
-
[32mDTS[39m [1mdist/index.d.mts [22m[
|
|
18
|
-
[32mDTS[39m [1mdist/index.d.ts [22m[
|
|
16
|
+
[32mDTS[39m ⚡️ Build success in 5062ms
|
|
17
|
+
[32mDTS[39m [1mdist/index.d.mts [22m[32m6.37 KB[39m
|
|
18
|
+
[32mDTS[39m [1mdist/index.d.ts [22m[32m6.37 KB[39m
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,28 @@
|
|
|
1
1
|
# @pluv/platform-node
|
|
2
2
|
|
|
3
|
+
## 0.41.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 324710f: Fix inferred context type.
|
|
8
|
+
- @pluv/io@0.41.1
|
|
9
|
+
- @pluv/types@0.41.1
|
|
10
|
+
|
|
11
|
+
## 0.41.0
|
|
12
|
+
|
|
13
|
+
### Patch Changes
|
|
14
|
+
|
|
15
|
+
- d53bbdb: Fixed the return type of `createWsServer` from `createPluvHandler` to be `ws.WebSocketServer` instead of `ws.Server`.
|
|
16
|
+
- Updated dependencies [73adf21]
|
|
17
|
+
- Updated dependencies [f02a430]
|
|
18
|
+
- Updated dependencies [0b908b3]
|
|
19
|
+
- Updated dependencies [6422f3b]
|
|
20
|
+
- Updated dependencies [555b88d]
|
|
21
|
+
- Updated dependencies [a663c65]
|
|
22
|
+
- Updated dependencies [555b88d]
|
|
23
|
+
- @pluv/io@0.41.0
|
|
24
|
+
- @pluv/types@0.41.0
|
|
25
|
+
|
|
3
26
|
## 0.40.2
|
|
4
27
|
|
|
5
28
|
### Patch Changes
|
package/dist/index.d.mts
CHANGED
|
@@ -1,18 +1,21 @@
|
|
|
1
|
-
import { WebSocket,
|
|
1
|
+
import { WebSocket, WebSocketServer } from 'ws';
|
|
2
2
|
export { WebSocket } from 'ws';
|
|
3
|
-
import { AbstractWebSocket, WebSocketSerializedState, AbstractWebSocketConfig, AbstractEventMap, AbstractListener, AbstractPlatform, WebSocketRegistrationMode, AbstractPersistence, AbstractPubSub, ConvertWebSocketConfig, AbstractPlatformConfig, PluvServer, InferInitContextType, CreateIOParams, PluvIOAuthorize, PluvContext } from '@pluv/io';
|
|
4
|
-
import {
|
|
3
|
+
import { AbstractWebSocket, WebSocketSession, WebSocketSerializedState, AbstractWebSocketConfig, AbstractEventMap, AbstractListener, AbstractPlatform, WebSocketRegistrationMode, AbstractPersistence, AbstractPubSub, ConvertWebSocketConfig, AbstractPlatformConfig, PluvServer, InferInitContextType, CreateIOParams, PluvIOAuthorize, PluvContext } from '@pluv/io';
|
|
4
|
+
import { IOAuthorize, JsonObject, InferIOAuthorizeUser, Json, InferIOAuthorize, BaseUser, MaybePromise, Maybe, Id } from '@pluv/types';
|
|
5
5
|
import { IncomingMessage, Server, ServerResponse } from 'node:http';
|
|
6
6
|
|
|
7
7
|
type NodeWebSocketConfig = AbstractWebSocketConfig;
|
|
8
|
-
declare class NodeWebSocket extends AbstractWebSocket<WebSocket> {
|
|
8
|
+
declare class NodeWebSocket<TAuthorize extends IOAuthorize<any, any> | null = null> extends AbstractWebSocket<WebSocket> {
|
|
9
9
|
private _sessionId;
|
|
10
10
|
private _state;
|
|
11
|
+
private _user;
|
|
11
12
|
set presence(presence: JsonObject | null);
|
|
12
13
|
get readyState(): 0 | 1 | 2 | 3;
|
|
14
|
+
get session(): WebSocketSession<TAuthorize>;
|
|
13
15
|
get sessionId(): string;
|
|
14
16
|
get state(): WebSocketSerializedState;
|
|
15
17
|
set state(state: WebSocketSerializedState);
|
|
18
|
+
set user(user: InferIOAuthorizeUser<TAuthorize>);
|
|
16
19
|
constructor(webSocket: WebSocket, config: NodeWebSocketConfig);
|
|
17
20
|
addEventListener<TType extends keyof AbstractEventMap>(type: TType, handler: AbstractListener<TType>): void;
|
|
18
21
|
close(code?: number | undefined, reason?: string | undefined): void;
|
|
@@ -36,7 +39,7 @@ type NodePlatformConfig<TMeta extends Record<string, Json>> = {
|
|
|
36
39
|
}) & {
|
|
37
40
|
roomContext?: NodePlatformRoomContext<TMeta>;
|
|
38
41
|
};
|
|
39
|
-
declare class NodePlatform<TMeta extends Record<string, Json> = {}> extends AbstractPlatform<NodeWebSocket
|
|
42
|
+
declare class NodePlatform<TAuthorize extends IOAuthorize<any, any> | null = null, TMeta extends Record<string, Json> = {}> extends AbstractPlatform<NodeWebSocket<TAuthorize>, {
|
|
40
43
|
req: IncomingMessage;
|
|
41
44
|
}, NodePlatformRoomContext<TMeta>, {
|
|
42
45
|
authorize: {
|
|
@@ -72,16 +75,16 @@ declare class NodePlatform<TMeta extends Record<string, Json> = {}> extends Abst
|
|
|
72
75
|
};
|
|
73
76
|
readonly _name = "platformNode";
|
|
74
77
|
constructor(config?: NodePlatformConfig<TMeta>);
|
|
75
|
-
acceptWebSocket(webSocket: NodeWebSocket): Promise<void>;
|
|
76
|
-
convertWebSocket(webSocket: WebSocket, config: ConvertWebSocketConfig): NodeWebSocket
|
|
77
|
-
getLastPing(webSocket: NodeWebSocket): number | null;
|
|
78
|
+
acceptWebSocket(webSocket: NodeWebSocket<TAuthorize>): Promise<void>;
|
|
79
|
+
convertWebSocket(webSocket: WebSocket, config: ConvertWebSocketConfig): NodeWebSocket<TAuthorize>;
|
|
80
|
+
getLastPing(webSocket: NodeWebSocket<TAuthorize>): number | null;
|
|
78
81
|
getSerializedState(webSocket: WebSocket): WebSocketSerializedState | null;
|
|
79
82
|
getSessionId(webSocket: WebSocket): string | null;
|
|
80
83
|
getWebSockets(): readonly WebSocket[];
|
|
81
84
|
initialize(config: AbstractPlatformConfig<NodePlatformRoomContext<TMeta>>): this;
|
|
82
85
|
parseData(data: string | ArrayBuffer): Record<string, any>;
|
|
83
86
|
randomUUID(): string;
|
|
84
|
-
setSerializedState(webSocket: NodeWebSocket
|
|
87
|
+
setSerializedState(webSocket: NodeWebSocket<TAuthorize>, state: WebSocketSerializedState): WebSocketSerializedState;
|
|
85
88
|
}
|
|
86
89
|
|
|
87
90
|
type AuthorizeFunctionContext<TPluvServer extends PluvServer<any, any, any, any>> = {
|
|
@@ -103,11 +106,11 @@ interface WebSocketHandlerResult {
|
|
|
103
106
|
}
|
|
104
107
|
type WebSocketHandler = (ws: WebSocket, req: IncomingMessage) => Promise<WebSocketHandlerResult>;
|
|
105
108
|
interface CreatePluvHandlerResult {
|
|
106
|
-
createWsServer: () =>
|
|
109
|
+
createWsServer: () => WebSocketServer;
|
|
107
110
|
handler: RequestHandler;
|
|
108
111
|
wsHandler: WebSocketHandler;
|
|
109
112
|
}
|
|
110
|
-
declare const createPluvHandler: <TPluvServer extends PluvServer<NodePlatform
|
|
113
|
+
declare const createPluvHandler: <TPluvServer extends PluvServer<NodePlatform<any>, any, any, any>>(config: CreatePluvHandlerConfig<TPluvServer>) => CreatePluvHandlerResult;
|
|
111
114
|
|
|
112
115
|
declare const identity: <T extends unknown>(x: T) => T;
|
|
113
116
|
|
|
@@ -116,11 +119,11 @@ type InferCallback<TMeta extends Record<string, Json> = {}> = (i: typeof identit
|
|
|
116
119
|
};
|
|
117
120
|
declare const infer: <TMeta extends Record<string, Json> = {}>(callback: InferCallback<TMeta>) => InferCallback<TMeta>;
|
|
118
121
|
|
|
119
|
-
type PlatformNodeCreateIOParams<TMeta extends Record<string, Json> = {}, TContext extends Record<string, any> = {}, TUser extends BaseUser | null = null> = Id<NodePlatformConfig<TMeta> & Omit<CreateIOParams<NodePlatform<TMeta>, TContext, TUser>, "authorize" | "context" | "platform"> & {
|
|
120
|
-
authorize?: PluvIOAuthorize<NodePlatform<TMeta>, TUser, InferInitContextType<NodePlatform<TMeta>>>;
|
|
121
|
-
context?: PluvContext<NodePlatform<TMeta>, TContext>;
|
|
122
|
+
type PlatformNodeCreateIOParams<TMeta extends Record<string, Json> = {}, TContext extends Record<string, any> = {}, TUser extends BaseUser | null = null> = Id<NodePlatformConfig<TMeta> & Omit<CreateIOParams<NodePlatform<IOAuthorize<TUser, TContext>, TMeta>, TContext, TUser>, "authorize" | "context" | "platform"> & {
|
|
123
|
+
authorize?: PluvIOAuthorize<NodePlatform<IOAuthorize<TUser, TContext>, TMeta>, TUser, InferInitContextType<NodePlatform<IOAuthorize<TUser, TContext>, TMeta>>>;
|
|
124
|
+
context?: PluvContext<NodePlatform<IOAuthorize<TUser, any>, TMeta>, TContext>;
|
|
122
125
|
types?: InferCallback<TMeta>;
|
|
123
126
|
}>;
|
|
124
|
-
declare const platformNode: <TMeta extends Record<string, Json> = {}, TContext extends Record<string, any> = {}, TUser extends BaseUser | null = null>(config?: PlatformNodeCreateIOParams<TMeta, TContext, TUser>) => CreateIOParams<NodePlatform<TMeta>, TContext, TUser>;
|
|
127
|
+
declare const platformNode: <TMeta extends Record<string, Json> = {}, TContext extends Record<string, any> = {}, TUser extends BaseUser | null = null>(config?: PlatformNodeCreateIOParams<TMeta, TContext, TUser>) => CreateIOParams<NodePlatform<IOAuthorize<TUser, TContext>, TMeta>, TContext, TUser>;
|
|
125
128
|
|
|
126
129
|
export { type AuthorizeFunction, type AuthorizeFunctionContext, type CreatePluvHandlerConfig, type InferCallback, NodePlatform, type NodePlatformConfig, type PlatformNodeCreateIOParams, createPluvHandler, infer, platformNode };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,18 +1,21 @@
|
|
|
1
|
-
import { WebSocket,
|
|
1
|
+
import { WebSocket, WebSocketServer } from 'ws';
|
|
2
2
|
export { WebSocket } from 'ws';
|
|
3
|
-
import { AbstractWebSocket, WebSocketSerializedState, AbstractWebSocketConfig, AbstractEventMap, AbstractListener, AbstractPlatform, WebSocketRegistrationMode, AbstractPersistence, AbstractPubSub, ConvertWebSocketConfig, AbstractPlatformConfig, PluvServer, InferInitContextType, CreateIOParams, PluvIOAuthorize, PluvContext } from '@pluv/io';
|
|
4
|
-
import {
|
|
3
|
+
import { AbstractWebSocket, WebSocketSession, WebSocketSerializedState, AbstractWebSocketConfig, AbstractEventMap, AbstractListener, AbstractPlatform, WebSocketRegistrationMode, AbstractPersistence, AbstractPubSub, ConvertWebSocketConfig, AbstractPlatformConfig, PluvServer, InferInitContextType, CreateIOParams, PluvIOAuthorize, PluvContext } from '@pluv/io';
|
|
4
|
+
import { IOAuthorize, JsonObject, InferIOAuthorizeUser, Json, InferIOAuthorize, BaseUser, MaybePromise, Maybe, Id } from '@pluv/types';
|
|
5
5
|
import { IncomingMessage, Server, ServerResponse } from 'node:http';
|
|
6
6
|
|
|
7
7
|
type NodeWebSocketConfig = AbstractWebSocketConfig;
|
|
8
|
-
declare class NodeWebSocket extends AbstractWebSocket<WebSocket> {
|
|
8
|
+
declare class NodeWebSocket<TAuthorize extends IOAuthorize<any, any> | null = null> extends AbstractWebSocket<WebSocket> {
|
|
9
9
|
private _sessionId;
|
|
10
10
|
private _state;
|
|
11
|
+
private _user;
|
|
11
12
|
set presence(presence: JsonObject | null);
|
|
12
13
|
get readyState(): 0 | 1 | 2 | 3;
|
|
14
|
+
get session(): WebSocketSession<TAuthorize>;
|
|
13
15
|
get sessionId(): string;
|
|
14
16
|
get state(): WebSocketSerializedState;
|
|
15
17
|
set state(state: WebSocketSerializedState);
|
|
18
|
+
set user(user: InferIOAuthorizeUser<TAuthorize>);
|
|
16
19
|
constructor(webSocket: WebSocket, config: NodeWebSocketConfig);
|
|
17
20
|
addEventListener<TType extends keyof AbstractEventMap>(type: TType, handler: AbstractListener<TType>): void;
|
|
18
21
|
close(code?: number | undefined, reason?: string | undefined): void;
|
|
@@ -36,7 +39,7 @@ type NodePlatformConfig<TMeta extends Record<string, Json>> = {
|
|
|
36
39
|
}) & {
|
|
37
40
|
roomContext?: NodePlatformRoomContext<TMeta>;
|
|
38
41
|
};
|
|
39
|
-
declare class NodePlatform<TMeta extends Record<string, Json> = {}> extends AbstractPlatform<NodeWebSocket
|
|
42
|
+
declare class NodePlatform<TAuthorize extends IOAuthorize<any, any> | null = null, TMeta extends Record<string, Json> = {}> extends AbstractPlatform<NodeWebSocket<TAuthorize>, {
|
|
40
43
|
req: IncomingMessage;
|
|
41
44
|
}, NodePlatformRoomContext<TMeta>, {
|
|
42
45
|
authorize: {
|
|
@@ -72,16 +75,16 @@ declare class NodePlatform<TMeta extends Record<string, Json> = {}> extends Abst
|
|
|
72
75
|
};
|
|
73
76
|
readonly _name = "platformNode";
|
|
74
77
|
constructor(config?: NodePlatformConfig<TMeta>);
|
|
75
|
-
acceptWebSocket(webSocket: NodeWebSocket): Promise<void>;
|
|
76
|
-
convertWebSocket(webSocket: WebSocket, config: ConvertWebSocketConfig): NodeWebSocket
|
|
77
|
-
getLastPing(webSocket: NodeWebSocket): number | null;
|
|
78
|
+
acceptWebSocket(webSocket: NodeWebSocket<TAuthorize>): Promise<void>;
|
|
79
|
+
convertWebSocket(webSocket: WebSocket, config: ConvertWebSocketConfig): NodeWebSocket<TAuthorize>;
|
|
80
|
+
getLastPing(webSocket: NodeWebSocket<TAuthorize>): number | null;
|
|
78
81
|
getSerializedState(webSocket: WebSocket): WebSocketSerializedState | null;
|
|
79
82
|
getSessionId(webSocket: WebSocket): string | null;
|
|
80
83
|
getWebSockets(): readonly WebSocket[];
|
|
81
84
|
initialize(config: AbstractPlatformConfig<NodePlatformRoomContext<TMeta>>): this;
|
|
82
85
|
parseData(data: string | ArrayBuffer): Record<string, any>;
|
|
83
86
|
randomUUID(): string;
|
|
84
|
-
setSerializedState(webSocket: NodeWebSocket
|
|
87
|
+
setSerializedState(webSocket: NodeWebSocket<TAuthorize>, state: WebSocketSerializedState): WebSocketSerializedState;
|
|
85
88
|
}
|
|
86
89
|
|
|
87
90
|
type AuthorizeFunctionContext<TPluvServer extends PluvServer<any, any, any, any>> = {
|
|
@@ -103,11 +106,11 @@ interface WebSocketHandlerResult {
|
|
|
103
106
|
}
|
|
104
107
|
type WebSocketHandler = (ws: WebSocket, req: IncomingMessage) => Promise<WebSocketHandlerResult>;
|
|
105
108
|
interface CreatePluvHandlerResult {
|
|
106
|
-
createWsServer: () =>
|
|
109
|
+
createWsServer: () => WebSocketServer;
|
|
107
110
|
handler: RequestHandler;
|
|
108
111
|
wsHandler: WebSocketHandler;
|
|
109
112
|
}
|
|
110
|
-
declare const createPluvHandler: <TPluvServer extends PluvServer<NodePlatform
|
|
113
|
+
declare const createPluvHandler: <TPluvServer extends PluvServer<NodePlatform<any>, any, any, any>>(config: CreatePluvHandlerConfig<TPluvServer>) => CreatePluvHandlerResult;
|
|
111
114
|
|
|
112
115
|
declare const identity: <T extends unknown>(x: T) => T;
|
|
113
116
|
|
|
@@ -116,11 +119,11 @@ type InferCallback<TMeta extends Record<string, Json> = {}> = (i: typeof identit
|
|
|
116
119
|
};
|
|
117
120
|
declare const infer: <TMeta extends Record<string, Json> = {}>(callback: InferCallback<TMeta>) => InferCallback<TMeta>;
|
|
118
121
|
|
|
119
|
-
type PlatformNodeCreateIOParams<TMeta extends Record<string, Json> = {}, TContext extends Record<string, any> = {}, TUser extends BaseUser | null = null> = Id<NodePlatformConfig<TMeta> & Omit<CreateIOParams<NodePlatform<TMeta>, TContext, TUser>, "authorize" | "context" | "platform"> & {
|
|
120
|
-
authorize?: PluvIOAuthorize<NodePlatform<TMeta>, TUser, InferInitContextType<NodePlatform<TMeta>>>;
|
|
121
|
-
context?: PluvContext<NodePlatform<TMeta>, TContext>;
|
|
122
|
+
type PlatformNodeCreateIOParams<TMeta extends Record<string, Json> = {}, TContext extends Record<string, any> = {}, TUser extends BaseUser | null = null> = Id<NodePlatformConfig<TMeta> & Omit<CreateIOParams<NodePlatform<IOAuthorize<TUser, TContext>, TMeta>, TContext, TUser>, "authorize" | "context" | "platform"> & {
|
|
123
|
+
authorize?: PluvIOAuthorize<NodePlatform<IOAuthorize<TUser, TContext>, TMeta>, TUser, InferInitContextType<NodePlatform<IOAuthorize<TUser, TContext>, TMeta>>>;
|
|
124
|
+
context?: PluvContext<NodePlatform<IOAuthorize<TUser, any>, TMeta>, TContext>;
|
|
122
125
|
types?: InferCallback<TMeta>;
|
|
123
126
|
}>;
|
|
124
|
-
declare const platformNode: <TMeta extends Record<string, Json> = {}, TContext extends Record<string, any> = {}, TUser extends BaseUser | null = null>(config?: PlatformNodeCreateIOParams<TMeta, TContext, TUser>) => CreateIOParams<NodePlatform<TMeta>, TContext, TUser>;
|
|
127
|
+
declare const platformNode: <TMeta extends Record<string, Json> = {}, TContext extends Record<string, any> = {}, TUser extends BaseUser | null = null>(config?: PlatformNodeCreateIOParams<TMeta, TContext, TUser>) => CreateIOParams<NodePlatform<IOAuthorize<TUser, TContext>, TMeta>, TContext, TUser>;
|
|
125
128
|
|
|
126
129
|
export { type AuthorizeFunction, type AuthorizeFunctionContext, type CreatePluvHandlerConfig, type InferCallback, NodePlatform, type NodePlatformConfig, type PlatformNodeCreateIOParams, createPluvHandler, infer, platformNode };
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
var __create = Object.create;
|
|
3
3
|
var __defProp = Object.defineProperty;
|
|
4
|
+
var __defProps = Object.defineProperties;
|
|
4
5
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
5
7
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
8
|
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
7
9
|
var __getProtoOf = Object.getPrototypeOf;
|
|
@@ -19,6 +21,7 @@ var __spreadValues = (a, b) => {
|
|
|
19
21
|
}
|
|
20
22
|
return a;
|
|
21
23
|
};
|
|
24
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
22
25
|
var __export = (target, all) => {
|
|
23
26
|
for (var name in all)
|
|
24
27
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
@@ -93,7 +96,7 @@ var createPluvHandler = (config) => {
|
|
|
93
96
|
if (io._defs.platform._config.registrationMode === "detached") {
|
|
94
97
|
throw new Error("Cannot use createPluvHandler in detached mode for Node.js");
|
|
95
98
|
}
|
|
96
|
-
const wsServer = new import_ws.
|
|
99
|
+
const wsServer = new import_ws.WebSocketServer({ server });
|
|
97
100
|
const rooms = /* @__PURE__ */ new Map();
|
|
98
101
|
const getRoom = (roomId) => {
|
|
99
102
|
Array.from(rooms.values()).forEach((room) => {
|
|
@@ -187,14 +190,43 @@ var import_node_util = require("util");
|
|
|
187
190
|
var import_io = require("@pluv/io");
|
|
188
191
|
var import_node_crypto = __toESM(require("crypto"));
|
|
189
192
|
var NodeWebSocket = class extends import_io.AbstractWebSocket {
|
|
193
|
+
constructor(webSocket, config) {
|
|
194
|
+
const { room } = config;
|
|
195
|
+
super(webSocket, config);
|
|
196
|
+
this._sessionId = null;
|
|
197
|
+
this._user = null;
|
|
198
|
+
this._state = {
|
|
199
|
+
presence: null,
|
|
200
|
+
quit: false,
|
|
201
|
+
room,
|
|
202
|
+
timers: {
|
|
203
|
+
ping: (/* @__PURE__ */ new Date()).getTime(),
|
|
204
|
+
presence: null
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
}
|
|
190
208
|
set presence(presence) {
|
|
191
209
|
this._state.presence = presence;
|
|
192
210
|
}
|
|
193
211
|
get readyState() {
|
|
194
212
|
return this.webSocket.readyState;
|
|
195
213
|
}
|
|
214
|
+
get session() {
|
|
215
|
+
const sessionId = this.sessionId;
|
|
216
|
+
const state = this._state;
|
|
217
|
+
const user = this._user;
|
|
218
|
+
return __spreadProps(__spreadValues({}, state), {
|
|
219
|
+
id: sessionId,
|
|
220
|
+
user,
|
|
221
|
+
webSocket: this
|
|
222
|
+
});
|
|
223
|
+
}
|
|
196
224
|
get sessionId() {
|
|
197
|
-
|
|
225
|
+
var _a;
|
|
226
|
+
if (this._sessionId) return this._sessionId;
|
|
227
|
+
const sessionId = (_a = this._sessionId) != null ? _a : `p_${import_node_crypto.default.randomUUID()}`;
|
|
228
|
+
this._sessionId = sessionId;
|
|
229
|
+
return sessionId;
|
|
198
230
|
}
|
|
199
231
|
get state() {
|
|
200
232
|
return this._state;
|
|
@@ -202,16 +234,8 @@ var NodeWebSocket = class extends import_io.AbstractWebSocket {
|
|
|
202
234
|
set state(state) {
|
|
203
235
|
this._state = state;
|
|
204
236
|
}
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
super(webSocket, config);
|
|
208
|
-
this._sessionId = import_node_crypto.default.randomUUID();
|
|
209
|
-
this._state = {
|
|
210
|
-
presence: null,
|
|
211
|
-
quit: false,
|
|
212
|
-
room,
|
|
213
|
-
timers: { ping: (/* @__PURE__ */ new Date()).getTime() }
|
|
214
|
-
};
|
|
237
|
+
set user(user) {
|
|
238
|
+
this._user = user;
|
|
215
239
|
}
|
|
216
240
|
addEventListener(type, handler) {
|
|
217
241
|
switch (type) {
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
2
4
|
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
3
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
4
6
|
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
@@ -14,6 +16,7 @@ var __spreadValues = (a, b) => {
|
|
|
14
16
|
}
|
|
15
17
|
return a;
|
|
16
18
|
};
|
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
17
20
|
var __async = (__this, __arguments, generator) => {
|
|
18
21
|
return new Promise((resolve, reject) => {
|
|
19
22
|
var fulfilled = (value) => {
|
|
@@ -38,7 +41,7 @@ var __async = (__this, __arguments, generator) => {
|
|
|
38
41
|
// src/createPluvHandler.ts
|
|
39
42
|
import Url from "node:url";
|
|
40
43
|
import { match } from "path-to-regexp";
|
|
41
|
-
import {
|
|
44
|
+
import { WebSocketServer } from "ws";
|
|
42
45
|
var handle = (ws) => ({
|
|
43
46
|
invalidEndpoint: () => {
|
|
44
47
|
ws.close(1003, "Invalid WebSocket endpoint");
|
|
@@ -58,7 +61,7 @@ var createPluvHandler = (config) => {
|
|
|
58
61
|
if (io._defs.platform._config.registrationMode === "detached") {
|
|
59
62
|
throw new Error("Cannot use createPluvHandler in detached mode for Node.js");
|
|
60
63
|
}
|
|
61
|
-
const wsServer = new
|
|
64
|
+
const wsServer = new WebSocketServer({ server });
|
|
62
65
|
const rooms = /* @__PURE__ */ new Map();
|
|
63
66
|
const getRoom = (roomId) => {
|
|
64
67
|
Array.from(rooms.values()).forEach((room) => {
|
|
@@ -152,14 +155,43 @@ import { TextDecoder } from "node:util";
|
|
|
152
155
|
import { AbstractWebSocket } from "@pluv/io";
|
|
153
156
|
import crypto from "node:crypto";
|
|
154
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
|
+
}
|
|
155
173
|
set presence(presence) {
|
|
156
174
|
this._state.presence = presence;
|
|
157
175
|
}
|
|
158
176
|
get readyState() {
|
|
159
177
|
return this.webSocket.readyState;
|
|
160
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
|
+
}
|
|
161
189
|
get sessionId() {
|
|
162
|
-
|
|
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;
|
|
163
195
|
}
|
|
164
196
|
get state() {
|
|
165
197
|
return this._state;
|
|
@@ -167,16 +199,8 @@ var NodeWebSocket = class extends AbstractWebSocket {
|
|
|
167
199
|
set state(state) {
|
|
168
200
|
this._state = state;
|
|
169
201
|
}
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
super(webSocket, config);
|
|
173
|
-
this._sessionId = crypto.randomUUID();
|
|
174
|
-
this._state = {
|
|
175
|
-
presence: null,
|
|
176
|
-
quit: false,
|
|
177
|
-
room,
|
|
178
|
-
timers: { ping: (/* @__PURE__ */ new Date()).getTime() }
|
|
179
|
-
};
|
|
202
|
+
set user(user) {
|
|
203
|
+
this._user = user;
|
|
180
204
|
}
|
|
181
205
|
addEventListener(type, handler) {
|
|
182
206
|
switch (type) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pluv/platform-node",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.41.1",
|
|
4
4
|
"description": "@pluv/io adapter for node.js",
|
|
5
5
|
"author": "leedavidcs",
|
|
6
6
|
"license": "MIT",
|
|
@@ -20,16 +20,16 @@
|
|
|
20
20
|
"@types/node": "^22.14.1",
|
|
21
21
|
"@types/ws": "^8.18.1",
|
|
22
22
|
"path-to-regexp": "^8.2.0",
|
|
23
|
-
"@pluv/io": "^0.
|
|
24
|
-
"@pluv/types": "^0.
|
|
23
|
+
"@pluv/io": "^0.41.1",
|
|
24
|
+
"@pluv/types": "^0.41.1"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
|
-
"eslint": "^9.
|
|
27
|
+
"eslint": "^9.25.0",
|
|
28
28
|
"tsup": "^8.4.0",
|
|
29
29
|
"typescript": "^5.8.3",
|
|
30
30
|
"ws": "^8.18.1",
|
|
31
|
-
"@pluv/tsconfig": "^0.
|
|
32
|
-
"eslint-config-pluv": "^0.
|
|
31
|
+
"@pluv/tsconfig": "^0.41.1",
|
|
32
|
+
"eslint-config-pluv": "^0.41.1"
|
|
33
33
|
},
|
|
34
34
|
"peerDependencies": {
|
|
35
35
|
"ws": "^8.0.0"
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
"scripts": {
|
|
38
38
|
"build": "tsup src/index.ts --format esm,cjs --dts",
|
|
39
39
|
"dev": "tsup src/index.ts --format esm,cjs --watch --dts",
|
|
40
|
-
"lint": "eslint src/**/*.ts
|
|
40
|
+
"lint": "eslint \"src/**/*.ts*\" --fix --max-warnings 0",
|
|
41
41
|
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist"
|
|
42
42
|
}
|
|
43
43
|
}
|
package/src/NodePlatform.ts
CHANGED
|
@@ -7,7 +7,7 @@ import type {
|
|
|
7
7
|
WebSocketSerializedState,
|
|
8
8
|
} from "@pluv/io";
|
|
9
9
|
import { AbstractPlatform } from "@pluv/io";
|
|
10
|
-
import type { Json } from "@pluv/types";
|
|
10
|
+
import type { IOAuthorize, Json } from "@pluv/types";
|
|
11
11
|
import crypto from "node:crypto";
|
|
12
12
|
import type { IncomingMessage } from "node:http";
|
|
13
13
|
import { TextDecoder } from "node:util";
|
|
@@ -23,8 +23,11 @@ export type NodePlatformConfig<TMeta extends Record<string, Json>> = { mode?: We
|
|
|
23
23
|
| { persistence: AbstractPersistence; pubSub: AbstractPubSub }
|
|
24
24
|
) & { roomContext?: NodePlatformRoomContext<TMeta> };
|
|
25
25
|
|
|
26
|
-
export class NodePlatform<
|
|
27
|
-
|
|
26
|
+
export class NodePlatform<
|
|
27
|
+
TAuthorize extends IOAuthorize<any, any> | null = null,
|
|
28
|
+
TMeta extends Record<string, Json> = {},
|
|
29
|
+
> extends AbstractPlatform<
|
|
30
|
+
NodeWebSocket<TAuthorize>,
|
|
28
31
|
{ req: IncomingMessage },
|
|
29
32
|
NodePlatformRoomContext<TMeta>,
|
|
30
33
|
{
|
|
@@ -73,17 +76,17 @@ export class NodePlatform<TMeta extends Record<string, Json> = {}> extends Abstr
|
|
|
73
76
|
};
|
|
74
77
|
}
|
|
75
78
|
|
|
76
|
-
public acceptWebSocket(webSocket: NodeWebSocket): Promise<void> {
|
|
79
|
+
public acceptWebSocket(webSocket: NodeWebSocket<TAuthorize>): Promise<void> {
|
|
77
80
|
return Promise.resolve(undefined);
|
|
78
81
|
}
|
|
79
82
|
|
|
80
|
-
public convertWebSocket(webSocket: WebSocket, config: ConvertWebSocketConfig): NodeWebSocket {
|
|
83
|
+
public convertWebSocket(webSocket: WebSocket, config: ConvertWebSocketConfig): NodeWebSocket<TAuthorize> {
|
|
81
84
|
const { room } = config;
|
|
82
85
|
|
|
83
|
-
return new NodeWebSocket(webSocket, { persistence: this.persistence, platform: this, room });
|
|
86
|
+
return new NodeWebSocket<TAuthorize>(webSocket, { persistence: this.persistence, platform: this, room });
|
|
84
87
|
}
|
|
85
88
|
|
|
86
|
-
public getLastPing(webSocket: NodeWebSocket): number | null {
|
|
89
|
+
public getLastPing(webSocket: NodeWebSocket<TAuthorize>): number | null {
|
|
87
90
|
return null;
|
|
88
91
|
}
|
|
89
92
|
|
|
@@ -120,7 +123,10 @@ export class NodePlatform<TMeta extends Record<string, Json> = {}> extends Abstr
|
|
|
120
123
|
return crypto.randomUUID();
|
|
121
124
|
}
|
|
122
125
|
|
|
123
|
-
public setSerializedState(
|
|
126
|
+
public setSerializedState(
|
|
127
|
+
webSocket: NodeWebSocket<TAuthorize>,
|
|
128
|
+
state: WebSocketSerializedState,
|
|
129
|
+
): WebSocketSerializedState {
|
|
124
130
|
webSocket.state = state;
|
|
125
131
|
|
|
126
132
|
return webSocket.state;
|
package/src/NodeWebSocket.ts
CHANGED
|
@@ -6,9 +6,10 @@ import type {
|
|
|
6
6
|
AbstractMessageEvent,
|
|
7
7
|
AbstractWebSocketConfig,
|
|
8
8
|
WebSocketSerializedState,
|
|
9
|
+
WebSocketSession,
|
|
9
10
|
} from "@pluv/io";
|
|
10
11
|
import { AbstractWebSocket } from "@pluv/io";
|
|
11
|
-
import type { JsonObject } from "@pluv/types";
|
|
12
|
+
import type { InferIOAuthorizeUser, IOAuthorize, JsonObject } from "@pluv/types";
|
|
12
13
|
import crypto from "node:crypto";
|
|
13
14
|
import type { WebSocket } from "ws";
|
|
14
15
|
|
|
@@ -20,9 +21,12 @@ export interface NodeWebSocketEventMap {
|
|
|
20
21
|
|
|
21
22
|
export type NodeWebSocketConfig = AbstractWebSocketConfig;
|
|
22
23
|
|
|
23
|
-
export class NodeWebSocket
|
|
24
|
-
|
|
24
|
+
export class NodeWebSocket<
|
|
25
|
+
TAuthorize extends IOAuthorize<any, any> | null = null,
|
|
26
|
+
> extends AbstractWebSocket<WebSocket> {
|
|
27
|
+
private _sessionId: string | null = null;
|
|
25
28
|
private _state: WebSocketSerializedState;
|
|
29
|
+
private _user: InferIOAuthorizeUser<TAuthorize> = null as InferIOAuthorizeUser<TAuthorize>;
|
|
26
30
|
|
|
27
31
|
public set presence(presence: JsonObject | null) {
|
|
28
32
|
this._state.presence = presence;
|
|
@@ -32,8 +36,27 @@ export class NodeWebSocket extends AbstractWebSocket<WebSocket> {
|
|
|
32
36
|
return this.webSocket.readyState;
|
|
33
37
|
}
|
|
34
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
|
+
|
|
35
52
|
public get sessionId(): string {
|
|
36
|
-
return this._sessionId;
|
|
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;
|
|
37
60
|
}
|
|
38
61
|
|
|
39
62
|
public get state(): WebSocketSerializedState {
|
|
@@ -44,17 +67,23 @@ export class NodeWebSocket extends AbstractWebSocket<WebSocket> {
|
|
|
44
67
|
this._state = state;
|
|
45
68
|
}
|
|
46
69
|
|
|
70
|
+
public set user(user: InferIOAuthorizeUser<TAuthorize>) {
|
|
71
|
+
this._user = user;
|
|
72
|
+
}
|
|
73
|
+
|
|
47
74
|
constructor(webSocket: WebSocket, config: NodeWebSocketConfig) {
|
|
48
75
|
const { room } = config;
|
|
49
76
|
|
|
50
77
|
super(webSocket, config);
|
|
51
78
|
|
|
52
|
-
this._sessionId = crypto.randomUUID();
|
|
53
79
|
this._state = {
|
|
54
80
|
presence: null,
|
|
55
81
|
quit: false,
|
|
56
82
|
room,
|
|
57
|
-
timers: {
|
|
83
|
+
timers: {
|
|
84
|
+
ping: new Date().getTime(),
|
|
85
|
+
presence: null,
|
|
86
|
+
},
|
|
58
87
|
};
|
|
59
88
|
}
|
|
60
89
|
|
package/src/createPluvHandler.ts
CHANGED
|
@@ -4,7 +4,7 @@ import type { Server as HttpServer, IncomingMessage, ServerResponse } from "node
|
|
|
4
4
|
import Url from "node:url";
|
|
5
5
|
import { match } from "path-to-regexp";
|
|
6
6
|
import type { WebSocket } from "ws";
|
|
7
|
-
import {
|
|
7
|
+
import { WebSocketServer } from "ws";
|
|
8
8
|
import { NodePlatform } from "./NodePlatform";
|
|
9
9
|
|
|
10
10
|
export type AuthorizeFunctionContext<TPluvServer extends PluvServer<any, any, any, any>> = {
|
|
@@ -50,12 +50,12 @@ export interface WebSocketHandlerResult {
|
|
|
50
50
|
export type WebSocketHandler = (ws: WebSocket, req: IncomingMessage) => Promise<WebSocketHandlerResult>;
|
|
51
51
|
|
|
52
52
|
export interface CreatePluvHandlerResult {
|
|
53
|
-
createWsServer: () =>
|
|
53
|
+
createWsServer: () => WebSocketServer;
|
|
54
54
|
handler: RequestHandler;
|
|
55
55
|
wsHandler: WebSocketHandler;
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
-
export const createPluvHandler = <TPluvServer extends PluvServer<NodePlatform
|
|
58
|
+
export const createPluvHandler = <TPluvServer extends PluvServer<NodePlatform<any>, any, any, any>>(
|
|
59
59
|
config: CreatePluvHandlerConfig<TPluvServer>,
|
|
60
60
|
): CreatePluvHandlerResult => {
|
|
61
61
|
const { authorize, endpoint = "/api/pluv", io, server } = config;
|
|
@@ -64,7 +64,7 @@ export const createPluvHandler = <TPluvServer extends PluvServer<NodePlatform, a
|
|
|
64
64
|
throw new Error("Cannot use createPluvHandler in detached mode for Node.js");
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
-
const wsServer = new
|
|
67
|
+
const wsServer = new WebSocketServer({ server });
|
|
68
68
|
const rooms = new Map<string, ReturnType<typeof io.createRoom>>();
|
|
69
69
|
|
|
70
70
|
const getRoom = (roomId: string): ReturnType<typeof io.createRoom> => {
|
package/src/platformNode.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { CreateIOParams, InferInitContextType, PluvContext, PluvIOAuthorize } from "@pluv/io";
|
|
2
|
-
import type { BaseUser, Id, Json } from "@pluv/types";
|
|
2
|
+
import type { BaseUser, Id, IOAuthorize, Json } from "@pluv/types";
|
|
3
3
|
import type { NodePlatformConfig } from "./NodePlatform";
|
|
4
4
|
import { NodePlatform } from "./NodePlatform";
|
|
5
5
|
import type { InferCallback } from "./infer";
|
|
@@ -10,9 +10,16 @@ export type PlatformNodeCreateIOParams<
|
|
|
10
10
|
TUser extends BaseUser | null = null,
|
|
11
11
|
> = Id<
|
|
12
12
|
NodePlatformConfig<TMeta> &
|
|
13
|
-
Omit<
|
|
14
|
-
|
|
15
|
-
context
|
|
13
|
+
Omit<
|
|
14
|
+
CreateIOParams<NodePlatform<IOAuthorize<TUser, TContext>, TMeta>, TContext, TUser>,
|
|
15
|
+
"authorize" | "context" | "platform"
|
|
16
|
+
> & {
|
|
17
|
+
authorize?: PluvIOAuthorize<
|
|
18
|
+
NodePlatform<IOAuthorize<TUser, TContext>, TMeta>,
|
|
19
|
+
TUser,
|
|
20
|
+
InferInitContextType<NodePlatform<IOAuthorize<TUser, TContext>, TMeta>>
|
|
21
|
+
>;
|
|
22
|
+
context?: PluvContext<NodePlatform<IOAuthorize<TUser, any>, TMeta>, TContext>;
|
|
16
23
|
types?: InferCallback<TMeta>;
|
|
17
24
|
}
|
|
18
25
|
>;
|
|
@@ -23,7 +30,7 @@ export const platformNode = <
|
|
|
23
30
|
TUser extends BaseUser | null = null,
|
|
24
31
|
>(
|
|
25
32
|
config: PlatformNodeCreateIOParams<TMeta, TContext, TUser> = {},
|
|
26
|
-
): CreateIOParams<NodePlatform<TMeta>, TContext, TUser> => {
|
|
33
|
+
): CreateIOParams<NodePlatform<IOAuthorize<TUser, TContext>, TMeta>, TContext, TUser> => {
|
|
27
34
|
const { authorize, context, crdt, debug } = config;
|
|
28
35
|
|
|
29
36
|
return {
|
|
@@ -31,6 +38,6 @@ export const platformNode = <
|
|
|
31
38
|
context,
|
|
32
39
|
crdt,
|
|
33
40
|
debug,
|
|
34
|
-
platform: new NodePlatform<TMeta>(config),
|
|
41
|
+
platform: new NodePlatform<IOAuthorize<TUser, TContext>, TMeta>(config),
|
|
35
42
|
};
|
|
36
43
|
};
|