@pluv/platform-node 2.3.0 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +8 -8
- package/CHANGELOG.md +18 -0
- package/dist/index.d.mts +12 -41
- package/dist/index.d.ts +12 -41
- package/dist/index.js +0 -107
- package/dist/index.mjs +0 -106
- package/package.json +5 -5
- package/src/index.ts +0 -6
- package/src/createPluvHandler.ts +0 -202
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
|
|
2
|
-
> @pluv/platform-node@
|
|
2
|
+
> @pluv/platform-node@3.0.0 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
|
-
[
|
|
12
|
-
[
|
|
13
|
-
[
|
|
14
|
-
[
|
|
11
|
+
[32mESM[39m [1mdist/index.mjs [22m[32m5.92 KB[39m
|
|
12
|
+
[32mESM[39m ⚡️ Build success in 85ms
|
|
13
|
+
[32mCJS[39m [1mdist/index.js [22m[32m7.57 KB[39m
|
|
14
|
+
[32mCJS[39m ⚡️ Build success in 86ms
|
|
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 4681ms
|
|
17
|
+
[32mDTS[39m [1mdist/index.d.mts [22m[32m4.99 KB[39m
|
|
18
|
+
[32mDTS[39m [1mdist/index.d.ts [22m[32m4.99 KB[39m
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,23 @@
|
|
|
1
1
|
# @pluv/platform-node
|
|
2
2
|
|
|
3
|
+
## 3.0.0
|
|
4
|
+
|
|
5
|
+
### Major Changes
|
|
6
|
+
|
|
7
|
+
- bc1ae91: **BREAKING** Removed deprecated `createPluvHandler`.
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- @pluv/io@3.0.0
|
|
12
|
+
- @pluv/types@3.0.0
|
|
13
|
+
|
|
14
|
+
## 2.3.1
|
|
15
|
+
|
|
16
|
+
### Patch Changes
|
|
17
|
+
|
|
18
|
+
- @pluv/io@2.3.1
|
|
19
|
+
- @pluv/types@2.3.1
|
|
20
|
+
|
|
3
21
|
## 2.3.0
|
|
4
22
|
|
|
5
23
|
### Patch Changes
|
package/dist/index.d.mts
CHANGED
|
@@ -1,8 +1,15 @@
|
|
|
1
|
-
import { WebSocket
|
|
1
|
+
import { WebSocket } from 'ws';
|
|
2
2
|
export { WebSocket } from 'ws';
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import { IncomingMessage
|
|
3
|
+
import { Json, IOAuthorize, JsonObject, InferIOAuthorizeUser, BaseUser, Id } from '@pluv/types';
|
|
4
|
+
import { AbstractWebSocket, WebSocketSession, WebSocketSerializedState, AbstractWebSocketConfig, AbstractEventMap, AbstractListener, AbstractPlatform, WebSocketRegistrationMode, AbstractPersistence, AbstractPubSub, ConvertWebSocketConfig, AbstractPlatformConfig, CreateIOParams, PluvIOAuthorize, InferInitContextType, PluvContext } from '@pluv/io';
|
|
5
|
+
import { IncomingMessage } from 'node:http';
|
|
6
|
+
|
|
7
|
+
declare const identity: <T extends unknown>(x: T) => T;
|
|
8
|
+
|
|
9
|
+
type InferCallback<TMeta extends Record<string, Json> = {}> = (i: typeof identity) => {
|
|
10
|
+
meta?: (io: TMeta) => TMeta;
|
|
11
|
+
};
|
|
12
|
+
declare const infer: <TMeta extends Record<string, Json> = {}>(callback: InferCallback<TMeta>) => InferCallback<TMeta>;
|
|
6
13
|
|
|
7
14
|
type NodeWebSocketConfig = AbstractWebSocketConfig;
|
|
8
15
|
declare class NodeWebSocket<TAuthorize extends IOAuthorize<any, any> | null = null> extends AbstractWebSocket<WebSocket> {
|
|
@@ -88,42 +95,6 @@ declare class NodePlatform<TAuthorize extends IOAuthorize<any, any> | null = nul
|
|
|
88
95
|
setSerializedState(webSocket: NodeWebSocket<TAuthorize>, state: WebSocketSerializedState): WebSocketSerializedState;
|
|
89
96
|
}
|
|
90
97
|
|
|
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
98
|
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
99
|
authorize?: PluvIOAuthorize<NodePlatform<IOAuthorize<TUser, TContext>, TMeta>, TUser, InferInitContextType<NodePlatform<IOAuthorize<TUser, TContext>, TMeta>>>;
|
|
129
100
|
context?: PluvContext<NodePlatform<IOAuthorize<TUser, any>, TMeta>, TContext>;
|
|
@@ -131,4 +102,4 @@ type PlatformNodeCreateIOParams<TMeta extends Record<string, Json> = {}, TContex
|
|
|
131
102
|
}>;
|
|
132
103
|
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
104
|
|
|
134
|
-
export { type
|
|
105
|
+
export { type InferCallback, NodePlatform, type NodePlatformConfig, type PlatformNodeCreateIOParams, infer, platformNode };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,15 @@
|
|
|
1
|
-
import { WebSocket
|
|
1
|
+
import { WebSocket } from 'ws';
|
|
2
2
|
export { WebSocket } from 'ws';
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import { IncomingMessage
|
|
3
|
+
import { Json, IOAuthorize, JsonObject, InferIOAuthorizeUser, BaseUser, Id } from '@pluv/types';
|
|
4
|
+
import { AbstractWebSocket, WebSocketSession, WebSocketSerializedState, AbstractWebSocketConfig, AbstractEventMap, AbstractListener, AbstractPlatform, WebSocketRegistrationMode, AbstractPersistence, AbstractPubSub, ConvertWebSocketConfig, AbstractPlatformConfig, CreateIOParams, PluvIOAuthorize, InferInitContextType, PluvContext } from '@pluv/io';
|
|
5
|
+
import { IncomingMessage } from 'node:http';
|
|
6
|
+
|
|
7
|
+
declare const identity: <T extends unknown>(x: T) => T;
|
|
8
|
+
|
|
9
|
+
type InferCallback<TMeta extends Record<string, Json> = {}> = (i: typeof identity) => {
|
|
10
|
+
meta?: (io: TMeta) => TMeta;
|
|
11
|
+
};
|
|
12
|
+
declare const infer: <TMeta extends Record<string, Json> = {}>(callback: InferCallback<TMeta>) => InferCallback<TMeta>;
|
|
6
13
|
|
|
7
14
|
type NodeWebSocketConfig = AbstractWebSocketConfig;
|
|
8
15
|
declare class NodeWebSocket<TAuthorize extends IOAuthorize<any, any> | null = null> extends AbstractWebSocket<WebSocket> {
|
|
@@ -88,42 +95,6 @@ declare class NodePlatform<TAuthorize extends IOAuthorize<any, any> | null = nul
|
|
|
88
95
|
setSerializedState(webSocket: NodeWebSocket<TAuthorize>, state: WebSocketSerializedState): WebSocketSerializedState;
|
|
89
96
|
}
|
|
90
97
|
|
|
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
98
|
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
99
|
authorize?: PluvIOAuthorize<NodePlatform<IOAuthorize<TUser, TContext>, TMeta>, TUser, InferInitContextType<NodePlatform<IOAuthorize<TUser, TContext>, TMeta>>>;
|
|
129
100
|
context?: PluvContext<NodePlatform<IOAuthorize<TUser, any>, TMeta>, TContext>;
|
|
@@ -131,4 +102,4 @@ type PlatformNodeCreateIOParams<TMeta extends Record<string, Json> = {}, TContex
|
|
|
131
102
|
}>;
|
|
132
103
|
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
104
|
|
|
134
|
-
export { type
|
|
105
|
+
export { type InferCallback, NodePlatform, type NodePlatformConfig, type PlatformNodeCreateIOParams, infer, platformNode };
|
package/dist/index.js
CHANGED
|
@@ -67,117 +67,11 @@ var __async = (__this, __arguments, generator) => {
|
|
|
67
67
|
// src/index.ts
|
|
68
68
|
var index_exports = {};
|
|
69
69
|
__export(index_exports, {
|
|
70
|
-
createPluvHandler: () => createPluvHandler,
|
|
71
70
|
infer: () => infer,
|
|
72
71
|
platformNode: () => platformNode
|
|
73
72
|
});
|
|
74
73
|
module.exports = __toCommonJS(index_exports);
|
|
75
74
|
|
|
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
75
|
// src/infer.ts
|
|
182
76
|
var infer = (callback) => callback;
|
|
183
77
|
|
|
@@ -364,7 +258,6 @@ var platformNode = (config = {}) => {
|
|
|
364
258
|
};
|
|
365
259
|
// Annotate the CommonJS export names for ESM import in node:
|
|
366
260
|
0 && (module.exports = {
|
|
367
|
-
createPluvHandler,
|
|
368
261
|
infer,
|
|
369
262
|
platformNode
|
|
370
263
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -38,111 +38,6 @@ var __async = (__this, __arguments, generator) => {
|
|
|
38
38
|
});
|
|
39
39
|
};
|
|
40
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
41
|
// src/infer.ts
|
|
147
42
|
var infer = (callback) => callback;
|
|
148
43
|
|
|
@@ -328,7 +223,6 @@ var platformNode = (config = {}) => {
|
|
|
328
223
|
};
|
|
329
224
|
};
|
|
330
225
|
export {
|
|
331
|
-
createPluvHandler,
|
|
332
226
|
infer,
|
|
333
227
|
platformNode
|
|
334
228
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pluv/platform-node",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
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.15.21",
|
|
21
21
|
"@types/ws": "^8.18.1",
|
|
22
22
|
"path-to-regexp": "^8.2.0",
|
|
23
|
-
"@pluv/io": "^
|
|
24
|
-
"@pluv/types": "^
|
|
23
|
+
"@pluv/io": "^3.0.0",
|
|
24
|
+
"@pluv/types": "^3.0.0"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
27
|
"eslint": "^9.27.0",
|
|
28
28
|
"tsup": "^8.5.0",
|
|
29
29
|
"typescript": "^5.8.3",
|
|
30
30
|
"ws": "^8.18.2",
|
|
31
|
-
"@pluv/tsconfig": "^
|
|
32
|
-
"eslint-config-pluv": "^
|
|
31
|
+
"@pluv/tsconfig": "^3.0.0",
|
|
32
|
+
"eslint-config-pluv": "^3.0.0"
|
|
33
33
|
},
|
|
34
34
|
"peerDependencies": {
|
|
35
35
|
"ws": "^8.0.0"
|
package/src/index.ts
CHANGED
|
@@ -1,10 +1,4 @@
|
|
|
1
1
|
export type { WebSocket } from "ws";
|
|
2
|
-
export { createPluvHandler } from "./createPluvHandler";
|
|
3
|
-
export type {
|
|
4
|
-
AuthorizeFunction,
|
|
5
|
-
AuthorizeFunctionContext,
|
|
6
|
-
CreatePluvHandlerConfig,
|
|
7
|
-
} from "./createPluvHandler";
|
|
8
2
|
export { infer } from "./infer";
|
|
9
3
|
export type { InferCallback } from "./infer";
|
|
10
4
|
export type { NodePlatform, NodePlatformConfig } from "./NodePlatform";
|
package/src/createPluvHandler.ts
DELETED
|
@@ -1,202 +0,0 @@
|
|
|
1
|
-
import type { InferInitContextType, PluvServer } from "@pluv/io";
|
|
2
|
-
import type {
|
|
3
|
-
BaseUser,
|
|
4
|
-
InferIOAuthorize,
|
|
5
|
-
InferIOAuthorizeUser,
|
|
6
|
-
Maybe,
|
|
7
|
-
MaybePromise,
|
|
8
|
-
} from "@pluv/types";
|
|
9
|
-
import type { Server as HttpServer, IncomingMessage, ServerResponse } from "node:http";
|
|
10
|
-
import Url from "node:url";
|
|
11
|
-
import { match } from "path-to-regexp";
|
|
12
|
-
import type { WebSocket } from "ws";
|
|
13
|
-
import { WebSocketServer } from "ws";
|
|
14
|
-
import { NodePlatform } from "./NodePlatform";
|
|
15
|
-
|
|
16
|
-
export type AuthorizeFunctionContext<TPluvServer extends PluvServer<any, any, any, any>> = {
|
|
17
|
-
room: string;
|
|
18
|
-
} & InferInitContextType<
|
|
19
|
-
TPluvServer extends PluvServer<infer IPlatform, any, any, any> ? IPlatform : never
|
|
20
|
-
>;
|
|
21
|
-
|
|
22
|
-
export type AuthorizeFunction<TPluvServer extends PluvServer<any, any, any, any>> = (
|
|
23
|
-
ctx: AuthorizeFunctionContext<TPluvServer>,
|
|
24
|
-
) => MaybePromise<Maybe<InferIOAuthorizeUser<InferIOAuthorize<TPluvServer>>>>;
|
|
25
|
-
|
|
26
|
-
export type CreatePluvHandlerConfig<TPluvServer extends PluvServer<any, any, any, any>> = {
|
|
27
|
-
endpoint?: string;
|
|
28
|
-
io: TPluvServer;
|
|
29
|
-
server: HttpServer;
|
|
30
|
-
} & (InferIOAuthorizeUser<InferIOAuthorize<TPluvServer>> extends BaseUser
|
|
31
|
-
? { authorize: AuthorizeFunction<TPluvServer> }
|
|
32
|
-
: { authorize?: undefined });
|
|
33
|
-
|
|
34
|
-
const handle = (ws: WebSocket) => ({
|
|
35
|
-
invalidEndpoint: () => {
|
|
36
|
-
ws.close(1003, "Invalid WebSocket endpoint");
|
|
37
|
-
|
|
38
|
-
return { matched: false };
|
|
39
|
-
},
|
|
40
|
-
invalidRoom: () => {
|
|
41
|
-
ws.close(1003, "Invalid room name");
|
|
42
|
-
|
|
43
|
-
return { matched: true };
|
|
44
|
-
},
|
|
45
|
-
invalidToken: () => {
|
|
46
|
-
ws.close(1003, "Invalid authentication token");
|
|
47
|
-
|
|
48
|
-
return { matched: true };
|
|
49
|
-
},
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
export type RequestHandler = (req: IncomingMessage, res: ServerResponse, next?: () => void) => void;
|
|
53
|
-
|
|
54
|
-
export interface WebSocketHandlerResult {
|
|
55
|
-
matched: boolean;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
export type WebSocketHandler = (
|
|
59
|
-
ws: WebSocket,
|
|
60
|
-
req: IncomingMessage,
|
|
61
|
-
) => Promise<WebSocketHandlerResult>;
|
|
62
|
-
|
|
63
|
-
export interface CreatePluvHandlerResult {
|
|
64
|
-
createWsServer: () => WebSocketServer;
|
|
65
|
-
handler: RequestHandler;
|
|
66
|
-
wsHandler: WebSocketHandler;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* @deprecated Instructions will be provided on https://pluv.io on how to host this yourself instead.
|
|
71
|
-
* @date April 27, 2025
|
|
72
|
-
*/
|
|
73
|
-
export const createPluvHandler = <TPluvServer extends PluvServer<NodePlatform<any>, any, any, any>>(
|
|
74
|
-
config: CreatePluvHandlerConfig<TPluvServer>,
|
|
75
|
-
): CreatePluvHandlerResult => {
|
|
76
|
-
const { authorize, endpoint = "/api/pluv", io, server } = config;
|
|
77
|
-
|
|
78
|
-
if (io._defs.platform._config.registrationMode === "detached") {
|
|
79
|
-
throw new Error("Cannot use createPluvHandler in detached mode for Node.js");
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
const wsServer = new WebSocketServer({ server });
|
|
83
|
-
const rooms = new Map<string, ReturnType<typeof io.createRoom>>();
|
|
84
|
-
|
|
85
|
-
const getRoom = (roomId: string): ReturnType<typeof io.createRoom> => {
|
|
86
|
-
Array.from(rooms.values()).forEach((room) => {
|
|
87
|
-
if (!room.getSize()) rooms.delete(room.id);
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
const existing = rooms.get(roomId);
|
|
91
|
-
|
|
92
|
-
if (existing) return existing;
|
|
93
|
-
|
|
94
|
-
const newRoom = io.createRoom(roomId, {
|
|
95
|
-
onDestroy: (event) => {
|
|
96
|
-
rooms.delete(event.room);
|
|
97
|
-
},
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
rooms.set(roomId, newRoom);
|
|
101
|
-
|
|
102
|
-
return newRoom;
|
|
103
|
-
};
|
|
104
|
-
|
|
105
|
-
const wsHandler = async (
|
|
106
|
-
ws: WebSocket,
|
|
107
|
-
req: IncomingMessage,
|
|
108
|
-
): Promise<WebSocketHandlerResult> => {
|
|
109
|
-
const url = req.url;
|
|
110
|
-
const onError = handle(ws);
|
|
111
|
-
|
|
112
|
-
if (!url) {
|
|
113
|
-
onError.invalidEndpoint();
|
|
114
|
-
|
|
115
|
-
return { matched: false };
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
const parsed = Url.parse(url, true);
|
|
119
|
-
const { pathname } = parsed;
|
|
120
|
-
|
|
121
|
-
if (!pathname) return onError.invalidEndpoint();
|
|
122
|
-
|
|
123
|
-
const matcher = match<{ roomId: string }>(`${endpoint}/room/:roomId`);
|
|
124
|
-
const matched = matcher(pathname);
|
|
125
|
-
|
|
126
|
-
if (!matched) return onError.invalidEndpoint();
|
|
127
|
-
|
|
128
|
-
const { roomId } = matched.params;
|
|
129
|
-
|
|
130
|
-
if (!roomId) return onError.invalidRoom();
|
|
131
|
-
|
|
132
|
-
const token = parsed.query?.token || undefined;
|
|
133
|
-
|
|
134
|
-
if (Array.isArray(token)) return onError.invalidToken();
|
|
135
|
-
|
|
136
|
-
const room = getRoom(roomId);
|
|
137
|
-
|
|
138
|
-
await room.register(ws, { req, token });
|
|
139
|
-
|
|
140
|
-
return { matched: true };
|
|
141
|
-
};
|
|
142
|
-
|
|
143
|
-
const createWsServer = () => {
|
|
144
|
-
return wsServer.on("connection", async (ws, req) => {
|
|
145
|
-
await wsHandler(ws, req);
|
|
146
|
-
});
|
|
147
|
-
};
|
|
148
|
-
|
|
149
|
-
const handler = async (
|
|
150
|
-
req: IncomingMessage,
|
|
151
|
-
res: ServerResponse,
|
|
152
|
-
next?: () => void,
|
|
153
|
-
): Promise<ServerResponse | null> => {
|
|
154
|
-
if (!authorize) return next?.() ?? null;
|
|
155
|
-
if (req.method !== "GET") return next?.() ?? null;
|
|
156
|
-
|
|
157
|
-
const url = req.url;
|
|
158
|
-
|
|
159
|
-
if (!url) return next?.() ?? null;
|
|
160
|
-
|
|
161
|
-
const parsed = Url.parse(url, true);
|
|
162
|
-
const { pathname } = parsed;
|
|
163
|
-
|
|
164
|
-
if (!pathname) return next?.() ?? null;
|
|
165
|
-
|
|
166
|
-
const matcher = match<{}>(`${endpoint}/authorize`);
|
|
167
|
-
const matched = matcher(pathname);
|
|
168
|
-
|
|
169
|
-
if (!matched) return next?.() ?? null;
|
|
170
|
-
|
|
171
|
-
const room = parsed.query?.room || undefined;
|
|
172
|
-
|
|
173
|
-
if (typeof room !== "string" || !room) {
|
|
174
|
-
res.writeHead(404, { "Content-Type": "text/plain" });
|
|
175
|
-
return res.end("Room does not exist");
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
try {
|
|
179
|
-
const user = await authorize({ req, room } as AuthorizeFunctionContext<TPluvServer>);
|
|
180
|
-
|
|
181
|
-
if (!user) throw new Error();
|
|
182
|
-
|
|
183
|
-
const token = await io.createToken({
|
|
184
|
-
req,
|
|
185
|
-
room,
|
|
186
|
-
user: user as any,
|
|
187
|
-
});
|
|
188
|
-
|
|
189
|
-
res.writeHead(200, { "Content-Type": "text/plain" });
|
|
190
|
-
return res.end(token);
|
|
191
|
-
} catch (err) {
|
|
192
|
-
res.writeHead(403, { "Content-Type": "text/plain" });
|
|
193
|
-
return res.end(err instanceof Error ? err.message : "Unauthorized");
|
|
194
|
-
}
|
|
195
|
-
};
|
|
196
|
-
|
|
197
|
-
return {
|
|
198
|
-
createWsServer,
|
|
199
|
-
handler,
|
|
200
|
-
wsHandler,
|
|
201
|
-
};
|
|
202
|
-
};
|