@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/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Shoubhit Dash
|
|
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,18 @@
|
|
|
1
|
+
## `@pluv/platform-node`
|
|
2
|
+
|
|
3
|
+
> Enables [@pluv/io](https://www.npmjs.com/package/@pluv/io) to run on [Node.js](https://nodejs.org/).
|
|
4
|
+
|
|
5
|
+
**👉 See full documentation on [pluv.io](https://pluv.io/docs/introduction). 👈**
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
# npm
|
|
11
|
+
npm install @pluv/platform-node ws @types/ws
|
|
12
|
+
|
|
13
|
+
# yarn
|
|
14
|
+
yarn add @pluv/platform-node ws @types/ws
|
|
15
|
+
|
|
16
|
+
# pnpm
|
|
17
|
+
pnpm add @pluv/platform-node ws @types/ws
|
|
18
|
+
```
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { WebSocket, WebSocketServer } from 'ws';
|
|
2
|
+
export { WebSocket } from 'ws';
|
|
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
|
+
import { IncomingMessage, Server, ServerResponse } from 'node:http';
|
|
6
|
+
|
|
7
|
+
type NodeWebSocketConfig = AbstractWebSocketConfig;
|
|
8
|
+
declare class NodeWebSocket<TAuthorize extends IOAuthorize<any, any> | null = null> extends AbstractWebSocket<WebSocket> {
|
|
9
|
+
private _sessionId;
|
|
10
|
+
private _state;
|
|
11
|
+
private _user;
|
|
12
|
+
set presence(presence: JsonObject | null);
|
|
13
|
+
get readyState(): 0 | 1 | 2 | 3;
|
|
14
|
+
get session(): WebSocketSession<TAuthorize>;
|
|
15
|
+
get sessionId(): string;
|
|
16
|
+
get state(): WebSocketSerializedState;
|
|
17
|
+
set state(state: WebSocketSerializedState);
|
|
18
|
+
set user(user: InferIOAuthorizeUser<TAuthorize>);
|
|
19
|
+
constructor(webSocket: WebSocket, config: NodeWebSocketConfig);
|
|
20
|
+
addEventListener<TType extends keyof AbstractEventMap>(type: TType, handler: AbstractListener<TType>): void;
|
|
21
|
+
close(code?: number | undefined, reason?: string | undefined): void;
|
|
22
|
+
send(message: string | ArrayBuffer | ArrayBufferView): void;
|
|
23
|
+
terminate(): void;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
type NodePlatformRoomContext<TMeta extends Record<string, Json>> = keyof TMeta extends never ? {
|
|
27
|
+
meta?: undefined;
|
|
28
|
+
} : {
|
|
29
|
+
meta: TMeta;
|
|
30
|
+
};
|
|
31
|
+
type NodePlatformConfig<TMeta extends Record<string, Json>> = {
|
|
32
|
+
mode?: WebSocketRegistrationMode;
|
|
33
|
+
} & ({
|
|
34
|
+
persistence?: undefined;
|
|
35
|
+
pubSub?: undefined;
|
|
36
|
+
} | {
|
|
37
|
+
persistence: AbstractPersistence;
|
|
38
|
+
pubSub: AbstractPubSub;
|
|
39
|
+
}) & {
|
|
40
|
+
roomContext?: NodePlatformRoomContext<TMeta>;
|
|
41
|
+
};
|
|
42
|
+
declare class NodePlatform<TAuthorize extends IOAuthorize<any, any> | null = null, TMeta extends Record<string, Json> = {}> extends AbstractPlatform<NodeWebSocket<TAuthorize>, {
|
|
43
|
+
req: IncomingMessage;
|
|
44
|
+
}, NodePlatformRoomContext<TMeta>, {
|
|
45
|
+
authorize: {
|
|
46
|
+
secret: true;
|
|
47
|
+
};
|
|
48
|
+
handleMode: "io";
|
|
49
|
+
requireAuth: false;
|
|
50
|
+
registrationMode: WebSocketRegistrationMode;
|
|
51
|
+
listeners: {
|
|
52
|
+
onRoomDeleted: true;
|
|
53
|
+
onRoomMessage: true;
|
|
54
|
+
onStorageUpdated: true;
|
|
55
|
+
onUserConnected: true;
|
|
56
|
+
onUserDisconnected: true;
|
|
57
|
+
};
|
|
58
|
+
router: true;
|
|
59
|
+
}> {
|
|
60
|
+
readonly id: `${string}-${string}-${string}-${string}-${string}`;
|
|
61
|
+
readonly _config: {
|
|
62
|
+
authorize: {
|
|
63
|
+
secret: true;
|
|
64
|
+
};
|
|
65
|
+
handleMode: "io";
|
|
66
|
+
registrationMode: WebSocketRegistrationMode;
|
|
67
|
+
requireAuth: false;
|
|
68
|
+
listeners: {
|
|
69
|
+
onRoomDeleted: true;
|
|
70
|
+
onRoomMessage: true;
|
|
71
|
+
onStorageUpdated: true;
|
|
72
|
+
onUserConnected: true;
|
|
73
|
+
onUserDisconnected: true;
|
|
74
|
+
};
|
|
75
|
+
router: true;
|
|
76
|
+
};
|
|
77
|
+
readonly _name = "platformNode";
|
|
78
|
+
constructor(config?: NodePlatformConfig<TMeta>);
|
|
79
|
+
acceptWebSocket(webSocket: NodeWebSocket<TAuthorize>): Promise<void>;
|
|
80
|
+
convertWebSocket(webSocket: WebSocket, config: ConvertWebSocketConfig): NodeWebSocket<TAuthorize>;
|
|
81
|
+
getLastPing(webSocket: NodeWebSocket<TAuthorize>): number | null;
|
|
82
|
+
getSerializedState(webSocket: WebSocket): WebSocketSerializedState | null;
|
|
83
|
+
getSessionId(webSocket: WebSocket): string | null;
|
|
84
|
+
getWebSockets(): readonly WebSocket[];
|
|
85
|
+
initialize(config: AbstractPlatformConfig<NodePlatformRoomContext<TMeta>>): this;
|
|
86
|
+
parseData(data: string | ArrayBuffer): Record<string, any>;
|
|
87
|
+
randomUUID(): string;
|
|
88
|
+
setSerializedState(webSocket: NodeWebSocket<TAuthorize>, state: WebSocketSerializedState): WebSocketSerializedState;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
type AuthorizeFunctionContext<TPluvServer extends PluvServer<any, any, any, any>> = {
|
|
92
|
+
room: string;
|
|
93
|
+
} & InferInitContextType<TPluvServer extends PluvServer<infer IPlatform, any, any, any> ? IPlatform : never>;
|
|
94
|
+
type AuthorizeFunction<TPluvServer extends PluvServer<any, any, any, any>> = (ctx: AuthorizeFunctionContext<TPluvServer>) => MaybePromise<Maybe<InferIOAuthorizeUser<InferIOAuthorize<TPluvServer>>>>;
|
|
95
|
+
type CreatePluvHandlerConfig<TPluvServer extends PluvServer<any, any, any, any>> = {
|
|
96
|
+
endpoint?: string;
|
|
97
|
+
io: TPluvServer;
|
|
98
|
+
server: Server;
|
|
99
|
+
} & (InferIOAuthorizeUser<InferIOAuthorize<TPluvServer>> extends BaseUser ? {
|
|
100
|
+
authorize: AuthorizeFunction<TPluvServer>;
|
|
101
|
+
} : {
|
|
102
|
+
authorize?: undefined;
|
|
103
|
+
});
|
|
104
|
+
type RequestHandler = (req: IncomingMessage, res: ServerResponse, next?: () => void) => void;
|
|
105
|
+
interface WebSocketHandlerResult {
|
|
106
|
+
matched: boolean;
|
|
107
|
+
}
|
|
108
|
+
type WebSocketHandler = (ws: WebSocket, req: IncomingMessage) => Promise<WebSocketHandlerResult>;
|
|
109
|
+
interface CreatePluvHandlerResult {
|
|
110
|
+
createWsServer: () => WebSocketServer;
|
|
111
|
+
handler: RequestHandler;
|
|
112
|
+
wsHandler: WebSocketHandler;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* @deprecated Instructions will be provided on https://pluv.io on how to host this yourself instead.
|
|
116
|
+
* @date April 27, 2025
|
|
117
|
+
*/
|
|
118
|
+
declare const createPluvHandler: <TPluvServer extends PluvServer<NodePlatform<any>, any, any, any>>(config: CreatePluvHandlerConfig<TPluvServer>) => CreatePluvHandlerResult;
|
|
119
|
+
|
|
120
|
+
declare const identity: <T extends unknown>(x: T) => T;
|
|
121
|
+
|
|
122
|
+
type InferCallback<TMeta extends Record<string, Json> = {}> = (i: typeof identity) => {
|
|
123
|
+
meta?: (io: TMeta) => TMeta;
|
|
124
|
+
};
|
|
125
|
+
declare const infer: <TMeta extends Record<string, Json> = {}>(callback: InferCallback<TMeta>) => InferCallback<TMeta>;
|
|
126
|
+
|
|
127
|
+
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"> & {
|
|
128
|
+
authorize?: PluvIOAuthorize<NodePlatform<IOAuthorize<TUser, TContext>, TMeta>, TUser, InferInitContextType<NodePlatform<IOAuthorize<TUser, TContext>, TMeta>>>;
|
|
129
|
+
context?: PluvContext<NodePlatform<IOAuthorize<TUser, any>, TMeta>, TContext>;
|
|
130
|
+
types?: InferCallback<TMeta>;
|
|
131
|
+
}>;
|
|
132
|
+
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>;
|
|
133
|
+
|
|
134
|
+
export { type AuthorizeFunction, type AuthorizeFunctionContext, type CreatePluvHandlerConfig, type InferCallback, NodePlatform, type NodePlatformConfig, type PlatformNodeCreateIOParams, createPluvHandler, infer, platformNode };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { WebSocket, WebSocketServer } from 'ws';
|
|
2
|
+
export { WebSocket } from 'ws';
|
|
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
|
+
import { IncomingMessage, Server, ServerResponse } from 'node:http';
|
|
6
|
+
|
|
7
|
+
type NodeWebSocketConfig = AbstractWebSocketConfig;
|
|
8
|
+
declare class NodeWebSocket<TAuthorize extends IOAuthorize<any, any> | null = null> extends AbstractWebSocket<WebSocket> {
|
|
9
|
+
private _sessionId;
|
|
10
|
+
private _state;
|
|
11
|
+
private _user;
|
|
12
|
+
set presence(presence: JsonObject | null);
|
|
13
|
+
get readyState(): 0 | 1 | 2 | 3;
|
|
14
|
+
get session(): WebSocketSession<TAuthorize>;
|
|
15
|
+
get sessionId(): string;
|
|
16
|
+
get state(): WebSocketSerializedState;
|
|
17
|
+
set state(state: WebSocketSerializedState);
|
|
18
|
+
set user(user: InferIOAuthorizeUser<TAuthorize>);
|
|
19
|
+
constructor(webSocket: WebSocket, config: NodeWebSocketConfig);
|
|
20
|
+
addEventListener<TType extends keyof AbstractEventMap>(type: TType, handler: AbstractListener<TType>): void;
|
|
21
|
+
close(code?: number | undefined, reason?: string | undefined): void;
|
|
22
|
+
send(message: string | ArrayBuffer | ArrayBufferView): void;
|
|
23
|
+
terminate(): void;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
type NodePlatformRoomContext<TMeta extends Record<string, Json>> = keyof TMeta extends never ? {
|
|
27
|
+
meta?: undefined;
|
|
28
|
+
} : {
|
|
29
|
+
meta: TMeta;
|
|
30
|
+
};
|
|
31
|
+
type NodePlatformConfig<TMeta extends Record<string, Json>> = {
|
|
32
|
+
mode?: WebSocketRegistrationMode;
|
|
33
|
+
} & ({
|
|
34
|
+
persistence?: undefined;
|
|
35
|
+
pubSub?: undefined;
|
|
36
|
+
} | {
|
|
37
|
+
persistence: AbstractPersistence;
|
|
38
|
+
pubSub: AbstractPubSub;
|
|
39
|
+
}) & {
|
|
40
|
+
roomContext?: NodePlatformRoomContext<TMeta>;
|
|
41
|
+
};
|
|
42
|
+
declare class NodePlatform<TAuthorize extends IOAuthorize<any, any> | null = null, TMeta extends Record<string, Json> = {}> extends AbstractPlatform<NodeWebSocket<TAuthorize>, {
|
|
43
|
+
req: IncomingMessage;
|
|
44
|
+
}, NodePlatformRoomContext<TMeta>, {
|
|
45
|
+
authorize: {
|
|
46
|
+
secret: true;
|
|
47
|
+
};
|
|
48
|
+
handleMode: "io";
|
|
49
|
+
requireAuth: false;
|
|
50
|
+
registrationMode: WebSocketRegistrationMode;
|
|
51
|
+
listeners: {
|
|
52
|
+
onRoomDeleted: true;
|
|
53
|
+
onRoomMessage: true;
|
|
54
|
+
onStorageUpdated: true;
|
|
55
|
+
onUserConnected: true;
|
|
56
|
+
onUserDisconnected: true;
|
|
57
|
+
};
|
|
58
|
+
router: true;
|
|
59
|
+
}> {
|
|
60
|
+
readonly id: `${string}-${string}-${string}-${string}-${string}`;
|
|
61
|
+
readonly _config: {
|
|
62
|
+
authorize: {
|
|
63
|
+
secret: true;
|
|
64
|
+
};
|
|
65
|
+
handleMode: "io";
|
|
66
|
+
registrationMode: WebSocketRegistrationMode;
|
|
67
|
+
requireAuth: false;
|
|
68
|
+
listeners: {
|
|
69
|
+
onRoomDeleted: true;
|
|
70
|
+
onRoomMessage: true;
|
|
71
|
+
onStorageUpdated: true;
|
|
72
|
+
onUserConnected: true;
|
|
73
|
+
onUserDisconnected: true;
|
|
74
|
+
};
|
|
75
|
+
router: true;
|
|
76
|
+
};
|
|
77
|
+
readonly _name = "platformNode";
|
|
78
|
+
constructor(config?: NodePlatformConfig<TMeta>);
|
|
79
|
+
acceptWebSocket(webSocket: NodeWebSocket<TAuthorize>): Promise<void>;
|
|
80
|
+
convertWebSocket(webSocket: WebSocket, config: ConvertWebSocketConfig): NodeWebSocket<TAuthorize>;
|
|
81
|
+
getLastPing(webSocket: NodeWebSocket<TAuthorize>): number | null;
|
|
82
|
+
getSerializedState(webSocket: WebSocket): WebSocketSerializedState | null;
|
|
83
|
+
getSessionId(webSocket: WebSocket): string | null;
|
|
84
|
+
getWebSockets(): readonly WebSocket[];
|
|
85
|
+
initialize(config: AbstractPlatformConfig<NodePlatformRoomContext<TMeta>>): this;
|
|
86
|
+
parseData(data: string | ArrayBuffer): Record<string, any>;
|
|
87
|
+
randomUUID(): string;
|
|
88
|
+
setSerializedState(webSocket: NodeWebSocket<TAuthorize>, state: WebSocketSerializedState): WebSocketSerializedState;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
type AuthorizeFunctionContext<TPluvServer extends PluvServer<any, any, any, any>> = {
|
|
92
|
+
room: string;
|
|
93
|
+
} & InferInitContextType<TPluvServer extends PluvServer<infer IPlatform, any, any, any> ? IPlatform : never>;
|
|
94
|
+
type AuthorizeFunction<TPluvServer extends PluvServer<any, any, any, any>> = (ctx: AuthorizeFunctionContext<TPluvServer>) => MaybePromise<Maybe<InferIOAuthorizeUser<InferIOAuthorize<TPluvServer>>>>;
|
|
95
|
+
type CreatePluvHandlerConfig<TPluvServer extends PluvServer<any, any, any, any>> = {
|
|
96
|
+
endpoint?: string;
|
|
97
|
+
io: TPluvServer;
|
|
98
|
+
server: Server;
|
|
99
|
+
} & (InferIOAuthorizeUser<InferIOAuthorize<TPluvServer>> extends BaseUser ? {
|
|
100
|
+
authorize: AuthorizeFunction<TPluvServer>;
|
|
101
|
+
} : {
|
|
102
|
+
authorize?: undefined;
|
|
103
|
+
});
|
|
104
|
+
type RequestHandler = (req: IncomingMessage, res: ServerResponse, next?: () => void) => void;
|
|
105
|
+
interface WebSocketHandlerResult {
|
|
106
|
+
matched: boolean;
|
|
107
|
+
}
|
|
108
|
+
type WebSocketHandler = (ws: WebSocket, req: IncomingMessage) => Promise<WebSocketHandlerResult>;
|
|
109
|
+
interface CreatePluvHandlerResult {
|
|
110
|
+
createWsServer: () => WebSocketServer;
|
|
111
|
+
handler: RequestHandler;
|
|
112
|
+
wsHandler: WebSocketHandler;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* @deprecated Instructions will be provided on https://pluv.io on how to host this yourself instead.
|
|
116
|
+
* @date April 27, 2025
|
|
117
|
+
*/
|
|
118
|
+
declare const createPluvHandler: <TPluvServer extends PluvServer<NodePlatform<any>, any, any, any>>(config: CreatePluvHandlerConfig<TPluvServer>) => CreatePluvHandlerResult;
|
|
119
|
+
|
|
120
|
+
declare const identity: <T extends unknown>(x: T) => T;
|
|
121
|
+
|
|
122
|
+
type InferCallback<TMeta extends Record<string, Json> = {}> = (i: typeof identity) => {
|
|
123
|
+
meta?: (io: TMeta) => TMeta;
|
|
124
|
+
};
|
|
125
|
+
declare const infer: <TMeta extends Record<string, Json> = {}>(callback: InferCallback<TMeta>) => InferCallback<TMeta>;
|
|
126
|
+
|
|
127
|
+
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"> & {
|
|
128
|
+
authorize?: PluvIOAuthorize<NodePlatform<IOAuthorize<TUser, TContext>, TMeta>, TUser, InferInitContextType<NodePlatform<IOAuthorize<TUser, TContext>, TMeta>>>;
|
|
129
|
+
context?: PluvContext<NodePlatform<IOAuthorize<TUser, any>, TMeta>, TContext>;
|
|
130
|
+
types?: InferCallback<TMeta>;
|
|
131
|
+
}>;
|
|
132
|
+
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>;
|
|
133
|
+
|
|
134
|
+
export { type AuthorizeFunction, type AuthorizeFunctionContext, type CreatePluvHandlerConfig, type InferCallback, NodePlatform, type NodePlatformConfig, type PlatformNodeCreateIOParams, createPluvHandler, infer, platformNode };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __defProps = Object.defineProperties;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
7
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
8
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
9
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
10
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
11
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
12
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
13
|
+
var __spreadValues = (a, b) => {
|
|
14
|
+
for (var prop in b || (b = {}))
|
|
15
|
+
if (__hasOwnProp.call(b, prop))
|
|
16
|
+
__defNormalProp(a, prop, b[prop]);
|
|
17
|
+
if (__getOwnPropSymbols)
|
|
18
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
19
|
+
if (__propIsEnum.call(b, prop))
|
|
20
|
+
__defNormalProp(a, prop, b[prop]);
|
|
21
|
+
}
|
|
22
|
+
return a;
|
|
23
|
+
};
|
|
24
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
25
|
+
var __export = (target, all) => {
|
|
26
|
+
for (var name in all)
|
|
27
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
28
|
+
};
|
|
29
|
+
var __copyProps = (to, from, except, desc) => {
|
|
30
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
31
|
+
for (let key of __getOwnPropNames(from))
|
|
32
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
33
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
34
|
+
}
|
|
35
|
+
return to;
|
|
36
|
+
};
|
|
37
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
38
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
39
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
40
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
41
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
42
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
43
|
+
mod
|
|
44
|
+
));
|
|
45
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
46
|
+
var __async = (__this, __arguments, generator) => {
|
|
47
|
+
return new Promise((resolve, reject) => {
|
|
48
|
+
var fulfilled = (value) => {
|
|
49
|
+
try {
|
|
50
|
+
step(generator.next(value));
|
|
51
|
+
} catch (e) {
|
|
52
|
+
reject(e);
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
var rejected = (value) => {
|
|
56
|
+
try {
|
|
57
|
+
step(generator.throw(value));
|
|
58
|
+
} catch (e) {
|
|
59
|
+
reject(e);
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
63
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
64
|
+
});
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
// src/index.ts
|
|
68
|
+
var index_exports = {};
|
|
69
|
+
__export(index_exports, {
|
|
70
|
+
createPluvHandler: () => createPluvHandler,
|
|
71
|
+
infer: () => infer,
|
|
72
|
+
platformNode: () => platformNode
|
|
73
|
+
});
|
|
74
|
+
module.exports = __toCommonJS(index_exports);
|
|
75
|
+
|
|
76
|
+
// src/createPluvHandler.ts
|
|
77
|
+
var import_node_url = __toESM(require("url"));
|
|
78
|
+
var import_path_to_regexp = require("path-to-regexp");
|
|
79
|
+
var import_ws = require("ws");
|
|
80
|
+
var handle = (ws) => ({
|
|
81
|
+
invalidEndpoint: () => {
|
|
82
|
+
ws.close(1003, "Invalid WebSocket endpoint");
|
|
83
|
+
return { matched: false };
|
|
84
|
+
},
|
|
85
|
+
invalidRoom: () => {
|
|
86
|
+
ws.close(1003, "Invalid room name");
|
|
87
|
+
return { matched: true };
|
|
88
|
+
},
|
|
89
|
+
invalidToken: () => {
|
|
90
|
+
ws.close(1003, "Invalid authentication token");
|
|
91
|
+
return { matched: true };
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
var createPluvHandler = (config) => {
|
|
95
|
+
const { authorize, endpoint = "/api/pluv", io, server } = config;
|
|
96
|
+
if (io._defs.platform._config.registrationMode === "detached") {
|
|
97
|
+
throw new Error("Cannot use createPluvHandler in detached mode for Node.js");
|
|
98
|
+
}
|
|
99
|
+
const wsServer = new import_ws.WebSocketServer({ server });
|
|
100
|
+
const rooms = /* @__PURE__ */ new Map();
|
|
101
|
+
const getRoom = (roomId) => {
|
|
102
|
+
Array.from(rooms.values()).forEach((room) => {
|
|
103
|
+
if (!room.getSize()) rooms.delete(room.id);
|
|
104
|
+
});
|
|
105
|
+
const existing = rooms.get(roomId);
|
|
106
|
+
if (existing) return existing;
|
|
107
|
+
const newRoom = io.createRoom(roomId, {
|
|
108
|
+
onDestroy: (event) => {
|
|
109
|
+
rooms.delete(event.room);
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
rooms.set(roomId, newRoom);
|
|
113
|
+
return newRoom;
|
|
114
|
+
};
|
|
115
|
+
const wsHandler = (ws, req) => __async(null, null, function* () {
|
|
116
|
+
var _a;
|
|
117
|
+
const url = req.url;
|
|
118
|
+
const onError = handle(ws);
|
|
119
|
+
if (!url) {
|
|
120
|
+
onError.invalidEndpoint();
|
|
121
|
+
return { matched: false };
|
|
122
|
+
}
|
|
123
|
+
const parsed = import_node_url.default.parse(url, true);
|
|
124
|
+
const { pathname } = parsed;
|
|
125
|
+
if (!pathname) return onError.invalidEndpoint();
|
|
126
|
+
const matcher = (0, import_path_to_regexp.match)(`${endpoint}/room/:roomId`);
|
|
127
|
+
const matched = matcher(pathname);
|
|
128
|
+
if (!matched) return onError.invalidEndpoint();
|
|
129
|
+
const { roomId } = matched.params;
|
|
130
|
+
if (!roomId) return onError.invalidRoom();
|
|
131
|
+
const token = ((_a = parsed.query) == null ? void 0 : _a.token) || void 0;
|
|
132
|
+
if (Array.isArray(token)) return onError.invalidToken();
|
|
133
|
+
const room = getRoom(roomId);
|
|
134
|
+
yield room.register(ws, { req, token });
|
|
135
|
+
return { matched: true };
|
|
136
|
+
});
|
|
137
|
+
const createWsServer = () => {
|
|
138
|
+
return wsServer.on("connection", (ws, req) => __async(null, null, function* () {
|
|
139
|
+
yield wsHandler(ws, req);
|
|
140
|
+
}));
|
|
141
|
+
};
|
|
142
|
+
const handler = (req, res, next) => __async(null, null, function* () {
|
|
143
|
+
var _a, _b, _c, _d, _e, _f;
|
|
144
|
+
if (!authorize) return (_a = next == null ? void 0 : next()) != null ? _a : null;
|
|
145
|
+
if (req.method !== "GET") return (_b = next == null ? void 0 : next()) != null ? _b : null;
|
|
146
|
+
const url = req.url;
|
|
147
|
+
if (!url) return (_c = next == null ? void 0 : next()) != null ? _c : null;
|
|
148
|
+
const parsed = import_node_url.default.parse(url, true);
|
|
149
|
+
const { pathname } = parsed;
|
|
150
|
+
if (!pathname) return (_d = next == null ? void 0 : next()) != null ? _d : null;
|
|
151
|
+
const matcher = (0, import_path_to_regexp.match)(`${endpoint}/authorize`);
|
|
152
|
+
const matched = matcher(pathname);
|
|
153
|
+
if (!matched) return (_e = next == null ? void 0 : next()) != null ? _e : null;
|
|
154
|
+
const room = ((_f = parsed.query) == null ? void 0 : _f.room) || void 0;
|
|
155
|
+
if (typeof room !== "string" || !room) {
|
|
156
|
+
res.writeHead(404, { "Content-Type": "text/plain" });
|
|
157
|
+
return res.end("Room does not exist");
|
|
158
|
+
}
|
|
159
|
+
try {
|
|
160
|
+
const user = yield authorize({ req, room });
|
|
161
|
+
if (!user) throw new Error();
|
|
162
|
+
const token = yield io.createToken({
|
|
163
|
+
req,
|
|
164
|
+
room,
|
|
165
|
+
user
|
|
166
|
+
});
|
|
167
|
+
res.writeHead(200, { "Content-Type": "text/plain" });
|
|
168
|
+
return res.end(token);
|
|
169
|
+
} catch (err) {
|
|
170
|
+
res.writeHead(403, { "Content-Type": "text/plain" });
|
|
171
|
+
return res.end(err instanceof Error ? err.message : "Unauthorized");
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
return {
|
|
175
|
+
createWsServer,
|
|
176
|
+
handler,
|
|
177
|
+
wsHandler
|
|
178
|
+
};
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
// src/infer.ts
|
|
182
|
+
var infer = (callback) => callback;
|
|
183
|
+
|
|
184
|
+
// src/NodePlatform.ts
|
|
185
|
+
var import_io2 = require("@pluv/io");
|
|
186
|
+
var import_node_crypto2 = __toESM(require("crypto"));
|
|
187
|
+
var import_node_util = require("util");
|
|
188
|
+
|
|
189
|
+
// src/NodeWebSocket.ts
|
|
190
|
+
var import_io = require("@pluv/io");
|
|
191
|
+
var import_node_crypto = __toESM(require("crypto"));
|
|
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
|
+
}
|
|
208
|
+
set presence(presence) {
|
|
209
|
+
this._state.presence = presence;
|
|
210
|
+
}
|
|
211
|
+
get readyState() {
|
|
212
|
+
return this.webSocket.readyState;
|
|
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
|
+
}
|
|
224
|
+
get sessionId() {
|
|
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;
|
|
230
|
+
}
|
|
231
|
+
get state() {
|
|
232
|
+
return this._state;
|
|
233
|
+
}
|
|
234
|
+
set state(state) {
|
|
235
|
+
this._state = state;
|
|
236
|
+
}
|
|
237
|
+
set user(user) {
|
|
238
|
+
this._user = user;
|
|
239
|
+
}
|
|
240
|
+
addEventListener(type, handler) {
|
|
241
|
+
switch (type) {
|
|
242
|
+
case "close":
|
|
243
|
+
this.webSocket.on("close", (code, buffer) => __async(this, null, function* () {
|
|
244
|
+
const reason = buffer.toString("utf-8");
|
|
245
|
+
yield Promise.resolve(handler({ code, reason }));
|
|
246
|
+
}));
|
|
247
|
+
return;
|
|
248
|
+
case "error":
|
|
249
|
+
this.webSocket.on("error", (error) => __async(this, null, function* () {
|
|
250
|
+
const message = error.message;
|
|
251
|
+
yield Promise.resolve(
|
|
252
|
+
handler({ error, message })
|
|
253
|
+
);
|
|
254
|
+
}));
|
|
255
|
+
return;
|
|
256
|
+
case "message":
|
|
257
|
+
this.webSocket.on("message", (data) => __async(this, null, function* () {
|
|
258
|
+
yield Promise.resolve(handler({ data }));
|
|
259
|
+
}));
|
|
260
|
+
return;
|
|
261
|
+
default:
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
close(code, reason) {
|
|
265
|
+
const canClose = [this.CONNECTING, this.OPEN].some(
|
|
266
|
+
(readyState) => readyState === this.readyState
|
|
267
|
+
);
|
|
268
|
+
if (!canClose) return;
|
|
269
|
+
this.webSocket.close(code, reason);
|
|
270
|
+
}
|
|
271
|
+
send(message) {
|
|
272
|
+
if (this.readyState !== this.OPEN) return;
|
|
273
|
+
this.webSocket.send(message, (error) => {
|
|
274
|
+
if (error) throw error;
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
terminate() {
|
|
278
|
+
this.webSocket.terminate();
|
|
279
|
+
}
|
|
280
|
+
};
|
|
281
|
+
|
|
282
|
+
// src/NodePlatform.ts
|
|
283
|
+
var NodePlatform = class _NodePlatform extends import_io2.AbstractPlatform {
|
|
284
|
+
constructor(config = {}) {
|
|
285
|
+
const { roomContext, mode = "attached", persistence, pubSub } = config;
|
|
286
|
+
super(__spreadValues({
|
|
287
|
+
roomContext
|
|
288
|
+
}, persistence && pubSub ? { persistence, pubSub } : {}));
|
|
289
|
+
this.id = import_node_crypto2.default.randomUUID();
|
|
290
|
+
this._name = "platformNode";
|
|
291
|
+
this._config = {
|
|
292
|
+
authorize: {
|
|
293
|
+
secret: true
|
|
294
|
+
},
|
|
295
|
+
handleMode: "io",
|
|
296
|
+
registrationMode: mode,
|
|
297
|
+
requireAuth: false,
|
|
298
|
+
listeners: {
|
|
299
|
+
onRoomDeleted: true,
|
|
300
|
+
onRoomMessage: true,
|
|
301
|
+
onStorageUpdated: true,
|
|
302
|
+
onUserConnected: true,
|
|
303
|
+
onUserDisconnected: true
|
|
304
|
+
},
|
|
305
|
+
router: true
|
|
306
|
+
};
|
|
307
|
+
}
|
|
308
|
+
acceptWebSocket(webSocket) {
|
|
309
|
+
return Promise.resolve(void 0);
|
|
310
|
+
}
|
|
311
|
+
convertWebSocket(webSocket, config) {
|
|
312
|
+
const { room } = config;
|
|
313
|
+
return new NodeWebSocket(webSocket, {
|
|
314
|
+
persistence: this.persistence,
|
|
315
|
+
platform: this,
|
|
316
|
+
room
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
getLastPing(webSocket) {
|
|
320
|
+
return null;
|
|
321
|
+
}
|
|
322
|
+
getSerializedState(webSocket) {
|
|
323
|
+
return null;
|
|
324
|
+
}
|
|
325
|
+
getSessionId(webSocket) {
|
|
326
|
+
return null;
|
|
327
|
+
}
|
|
328
|
+
getWebSockets() {
|
|
329
|
+
return [];
|
|
330
|
+
}
|
|
331
|
+
initialize(config) {
|
|
332
|
+
return new _NodePlatform({
|
|
333
|
+
roomContext: config.roomContext,
|
|
334
|
+
mode: this._config.registrationMode,
|
|
335
|
+
persistence: this.persistence.initialize(config.roomContext),
|
|
336
|
+
pubSub: this.pubSub
|
|
337
|
+
})._initialize();
|
|
338
|
+
}
|
|
339
|
+
parseData(data) {
|
|
340
|
+
if (typeof data === "string") return JSON.parse(data);
|
|
341
|
+
const decoder = new import_node_util.TextDecoder("utf8");
|
|
342
|
+
return JSON.parse(decoder.decode(data));
|
|
343
|
+
}
|
|
344
|
+
randomUUID() {
|
|
345
|
+
return import_node_crypto2.default.randomUUID();
|
|
346
|
+
}
|
|
347
|
+
setSerializedState(webSocket, state) {
|
|
348
|
+
webSocket.state = state;
|
|
349
|
+
return webSocket.state;
|
|
350
|
+
}
|
|
351
|
+
};
|
|
352
|
+
|
|
353
|
+
// src/platformNode.ts
|
|
354
|
+
var platformNode = (config = {}) => {
|
|
355
|
+
const { authorize, context, crdt, debug, limits } = config;
|
|
356
|
+
return {
|
|
357
|
+
authorize,
|
|
358
|
+
context,
|
|
359
|
+
crdt,
|
|
360
|
+
debug,
|
|
361
|
+
limits,
|
|
362
|
+
platform: () => new NodePlatform(config)
|
|
363
|
+
};
|
|
364
|
+
};
|
|
365
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
366
|
+
0 && (module.exports = {
|
|
367
|
+
createPluvHandler,
|
|
368
|
+
infer,
|
|
369
|
+
platformNode
|
|
370
|
+
});
|