@pluv/platform-node 0.30.2 → 0.32.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 +140 -0
- package/dist/index.d.mts +10 -10
- package/dist/index.d.ts +10 -10
- package/dist/index.js +5 -5
- package/dist/index.mjs +5 -5
- package/package.json +5 -5
- package/src/NodePlatform.ts +3 -2
- package/src/createPluvHandler.ts +11 -12
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
|
|
2
|
-
> @pluv/platform-node@0.
|
|
2
|
+
> @pluv/platform-node@0.32.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
|
-
[32mESM[39m [1mdist/index.mjs [22m[32m7.
|
|
12
|
-
[32mESM[39m ⚡️ Build success in
|
|
13
|
-
[32mCJS[39m [1mdist/index.js [22m[32m9.
|
|
14
|
-
[32mCJS[39m ⚡️ Build success in
|
|
11
|
+
[32mESM[39m [1mdist/index.mjs [22m[32m7.41 KB[39m
|
|
12
|
+
[32mESM[39m ⚡️ Build success in 84ms
|
|
13
|
+
[32mCJS[39m [1mdist/index.js [22m[32m9.26 KB[39m
|
|
14
|
+
[32mCJS[39m ⚡️ Build success in 84ms
|
|
15
15
|
[34mDTS[39m Build start
|
|
16
|
-
[32mDTS[39m ⚡️ Build success in
|
|
17
|
-
[32mDTS[39m [1mdist/index.d.mts [22m[32m3.
|
|
18
|
-
[32mDTS[39m [1mdist/index.d.ts [22m[32m3.
|
|
16
|
+
[32mDTS[39m ⚡️ Build success in 5013ms
|
|
17
|
+
[32mDTS[39m [1mdist/index.d.mts [22m[32m3.78 KB[39m
|
|
18
|
+
[32mDTS[39m [1mdist/index.d.ts [22m[32m3.78 KB[39m
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,145 @@
|
|
|
1
1
|
# @pluv/platform-node
|
|
2
2
|
|
|
3
|
+
## 0.32.0
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Updated dependencies [cde5305]
|
|
8
|
+
- @pluv/io@0.32.0
|
|
9
|
+
- @pluv/types@0.32.0
|
|
10
|
+
|
|
11
|
+
## 0.31.0
|
|
12
|
+
|
|
13
|
+
### Minor Changes
|
|
14
|
+
|
|
15
|
+
- b3c31d7: **BREAKING**
|
|
16
|
+
|
|
17
|
+
Fixed platform context types. This will require additional properties when registering a websocket and creating authorization tokens. See example below:
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
// @pluv/platform-node example
|
|
21
|
+
|
|
22
|
+
import { platformNode } from "@pluv/platform-node";
|
|
23
|
+
import { createIO } from "@pluv/io";
|
|
24
|
+
import type { IncomingMessage } from "node:http";
|
|
25
|
+
import { z } from "zod";
|
|
26
|
+
|
|
27
|
+
const io = createIO({
|
|
28
|
+
// If using a function authorize parameter, `req` is now available as a param
|
|
29
|
+
authorize: ({ req }) => ({
|
|
30
|
+
required: true,
|
|
31
|
+
secret: "MY-CUSTOM-SECRET",
|
|
32
|
+
user: z.object({
|
|
33
|
+
id: z.string(),
|
|
34
|
+
}),
|
|
35
|
+
}),
|
|
36
|
+
platformNode(),
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
// Before
|
|
40
|
+
io.createToken({
|
|
41
|
+
room: "my-custom-room",
|
|
42
|
+
user: { id: "abc123" },
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
// After
|
|
46
|
+
io.createToken({
|
|
47
|
+
room: "my-custom-room",
|
|
48
|
+
user: { id: "abc123" },
|
|
49
|
+
|
|
50
|
+
// Previously not required, but now required
|
|
51
|
+
req: req as IncomingMessage,
|
|
52
|
+
});
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
```ts
|
|
56
|
+
// @pluv/platform-cloudflare example
|
|
57
|
+
|
|
58
|
+
import { platformCloudflare } from "@pluv/platform-cloudflare";
|
|
59
|
+
import { createIO } from "@pluv/io";
|
|
60
|
+
import { z } from "zod";
|
|
61
|
+
|
|
62
|
+
const io = createIO({
|
|
63
|
+
// If using a function authorize parameter, `env` and `request` are now available as params
|
|
64
|
+
authorize: ({ env, request }) => ({
|
|
65
|
+
required: true,
|
|
66
|
+
secret: "MY-CUSTOM-SECRET",
|
|
67
|
+
user: z.object({
|
|
68
|
+
id: z.string(),
|
|
69
|
+
}),
|
|
70
|
+
}),
|
|
71
|
+
platformCloudflare(),
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
// Before
|
|
75
|
+
io.createToken({
|
|
76
|
+
room: "my-custom-room",
|
|
77
|
+
user: { id: "abc123" },
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
// After
|
|
81
|
+
io.createToken({
|
|
82
|
+
room: "my-custom-room",
|
|
83
|
+
user: { id: "abc123" },
|
|
84
|
+
|
|
85
|
+
// Previously not required, but now required
|
|
86
|
+
env: env as Env,
|
|
87
|
+
request: request as Request,
|
|
88
|
+
});
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
- 0f98064: **BREAKING**
|
|
92
|
+
|
|
93
|
+
Update `authorize` params so that `roomId` is renamed to `room` and more platform-specific parameters are exposed. See example below:
|
|
94
|
+
|
|
95
|
+
```ts
|
|
96
|
+
// @pluv/platform-cloudflare example
|
|
97
|
+
import { createPluvHandler } from "@pluv/platform-cloudflare";
|
|
98
|
+
|
|
99
|
+
// Before
|
|
100
|
+
createPluvHandler({
|
|
101
|
+
// ...
|
|
102
|
+
authorize: ({ roomId }) => {
|
|
103
|
+
// ...
|
|
104
|
+
},
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
// After
|
|
108
|
+
createPluvHandler({
|
|
109
|
+
// ...
|
|
110
|
+
authorize: ({ env, request, room }) => {
|
|
111
|
+
// ...
|
|
112
|
+
},
|
|
113
|
+
});
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
```ts
|
|
117
|
+
// @pluv/platform-node example
|
|
118
|
+
import { createPluvHandler } from "@pluv/platform-node";
|
|
119
|
+
|
|
120
|
+
// Before
|
|
121
|
+
createPluvHandler({
|
|
122
|
+
// ...
|
|
123
|
+
authorize: ({ roomId }) => {
|
|
124
|
+
// ...
|
|
125
|
+
},
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
// After
|
|
129
|
+
createPluvHandler({
|
|
130
|
+
// ...
|
|
131
|
+
authorize: ({ req, room }) => {
|
|
132
|
+
// ...
|
|
133
|
+
},
|
|
134
|
+
});
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
### Patch Changes
|
|
138
|
+
|
|
139
|
+
- Updated dependencies [b3c31d7]
|
|
140
|
+
- @pluv/io@0.31.0
|
|
141
|
+
- @pluv/types@0.31.0
|
|
142
|
+
|
|
3
143
|
## 0.30.2
|
|
4
144
|
|
|
5
145
|
### Patch Changes
|
package/dist/index.d.mts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { WebSocket, Server as Server$1 } from 'ws';
|
|
2
2
|
export { WebSocket } from 'ws';
|
|
3
|
-
import { AbstractWebSocket, WebSocketSerializedState, AbstractEventMap, AbstractListener, AbstractWebSocketConfig, WebSocketRegistrationMode, AbstractPersistence, AbstractPubSub, AbstractPlatform, ConvertWebSocketConfig, AbstractPlatformConfig, PluvServer } from '@pluv/io';
|
|
3
|
+
import { AbstractWebSocket, WebSocketSerializedState, AbstractEventMap, AbstractListener, AbstractWebSocketConfig, WebSocketRegistrationMode, AbstractPersistence, AbstractPubSub, AbstractPlatform, ConvertWebSocketConfig, AbstractPlatformConfig, PluvServer, InferInitContextType } from '@pluv/io';
|
|
4
4
|
import { JsonObject, MaybePromise, Maybe, InferIOAuthorizeUser, InferIOAuthorize, InferIOAuthorizeRequired } from '@pluv/types';
|
|
5
|
-
import { IncomingMessage,
|
|
5
|
+
import { IncomingMessage, Server, ServerResponse } from 'node:http';
|
|
6
6
|
|
|
7
7
|
type NodeWebSocketConfig = AbstractWebSocketConfig;
|
|
8
8
|
declare class NodeWebSocket extends AbstractWebSocket<WebSocket> {
|
|
@@ -29,7 +29,9 @@ type NodePlatformConfig = {
|
|
|
29
29
|
persistence: AbstractPersistence;
|
|
30
30
|
pubSub: AbstractPubSub;
|
|
31
31
|
});
|
|
32
|
-
declare class NodePlatform extends AbstractPlatform<NodeWebSocket
|
|
32
|
+
declare class NodePlatform extends AbstractPlatform<NodeWebSocket, {
|
|
33
|
+
req: IncomingMessage;
|
|
34
|
+
}, {}> {
|
|
33
35
|
readonly _registrationMode: WebSocketRegistrationMode;
|
|
34
36
|
constructor(config?: NodePlatformConfig);
|
|
35
37
|
acceptWebSocket(webSocket: NodeWebSocket): Promise<void>;
|
|
@@ -38,18 +40,16 @@ declare class NodePlatform extends AbstractPlatform<NodeWebSocket> {
|
|
|
38
40
|
getSerializedState(webSocket: WebSocket): WebSocketSerializedState | null;
|
|
39
41
|
getSessionId(webSocket: WebSocket): string | null;
|
|
40
42
|
getWebSockets(): readonly WebSocket[];
|
|
41
|
-
initialize(config: AbstractPlatformConfig<{}
|
|
43
|
+
initialize(config: AbstractPlatformConfig<{}>): this;
|
|
42
44
|
parseData(data: string | ArrayBuffer): Record<string, any>;
|
|
43
45
|
randomUUID(): string;
|
|
44
46
|
setSerializedState(webSocket: NodeWebSocket, state: WebSocketSerializedState): void;
|
|
45
47
|
}
|
|
46
48
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
}
|
|
52
|
-
type AuthorizeFunction<TPluvServer extends PluvServer<NodePlatform, any, any, any>> = (ctx: AuthorizeFunctionContext) => MaybePromise<Maybe<InferIOAuthorizeUser<InferIOAuthorize<TPluvServer>>>>;
|
|
49
|
+
type AuthorizeFunctionContext<TPluvServer extends PluvServer<any, any, any, any>> = {
|
|
50
|
+
room: string;
|
|
51
|
+
} & InferInitContextType<TPluvServer extends PluvServer<infer IPlatform, any, any, any> ? IPlatform : never>;
|
|
52
|
+
type AuthorizeFunction<TPluvServer extends PluvServer<NodePlatform, any, any, any>> = (ctx: AuthorizeFunctionContext<TPluvServer>) => MaybePromise<Maybe<InferIOAuthorizeUser<InferIOAuthorize<TPluvServer>>>>;
|
|
53
53
|
type CreatePluvHandlerConfig<TPluvServer extends PluvServer<NodePlatform, any, any, any>> = {
|
|
54
54
|
endpoint?: string;
|
|
55
55
|
io: TPluvServer;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { WebSocket, Server as Server$1 } from 'ws';
|
|
2
2
|
export { WebSocket } from 'ws';
|
|
3
|
-
import { AbstractWebSocket, WebSocketSerializedState, AbstractEventMap, AbstractListener, AbstractWebSocketConfig, WebSocketRegistrationMode, AbstractPersistence, AbstractPubSub, AbstractPlatform, ConvertWebSocketConfig, AbstractPlatformConfig, PluvServer } from '@pluv/io';
|
|
3
|
+
import { AbstractWebSocket, WebSocketSerializedState, AbstractEventMap, AbstractListener, AbstractWebSocketConfig, WebSocketRegistrationMode, AbstractPersistence, AbstractPubSub, AbstractPlatform, ConvertWebSocketConfig, AbstractPlatformConfig, PluvServer, InferInitContextType } from '@pluv/io';
|
|
4
4
|
import { JsonObject, MaybePromise, Maybe, InferIOAuthorizeUser, InferIOAuthorize, InferIOAuthorizeRequired } from '@pluv/types';
|
|
5
|
-
import { IncomingMessage,
|
|
5
|
+
import { IncomingMessage, Server, ServerResponse } from 'node:http';
|
|
6
6
|
|
|
7
7
|
type NodeWebSocketConfig = AbstractWebSocketConfig;
|
|
8
8
|
declare class NodeWebSocket extends AbstractWebSocket<WebSocket> {
|
|
@@ -29,7 +29,9 @@ type NodePlatformConfig = {
|
|
|
29
29
|
persistence: AbstractPersistence;
|
|
30
30
|
pubSub: AbstractPubSub;
|
|
31
31
|
});
|
|
32
|
-
declare class NodePlatform extends AbstractPlatform<NodeWebSocket
|
|
32
|
+
declare class NodePlatform extends AbstractPlatform<NodeWebSocket, {
|
|
33
|
+
req: IncomingMessage;
|
|
34
|
+
}, {}> {
|
|
33
35
|
readonly _registrationMode: WebSocketRegistrationMode;
|
|
34
36
|
constructor(config?: NodePlatformConfig);
|
|
35
37
|
acceptWebSocket(webSocket: NodeWebSocket): Promise<void>;
|
|
@@ -38,18 +40,16 @@ declare class NodePlatform extends AbstractPlatform<NodeWebSocket> {
|
|
|
38
40
|
getSerializedState(webSocket: WebSocket): WebSocketSerializedState | null;
|
|
39
41
|
getSessionId(webSocket: WebSocket): string | null;
|
|
40
42
|
getWebSockets(): readonly WebSocket[];
|
|
41
|
-
initialize(config: AbstractPlatformConfig<{}
|
|
43
|
+
initialize(config: AbstractPlatformConfig<{}>): this;
|
|
42
44
|
parseData(data: string | ArrayBuffer): Record<string, any>;
|
|
43
45
|
randomUUID(): string;
|
|
44
46
|
setSerializedState(webSocket: NodeWebSocket, state: WebSocketSerializedState): void;
|
|
45
47
|
}
|
|
46
48
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
}
|
|
52
|
-
type AuthorizeFunction<TPluvServer extends PluvServer<NodePlatform, any, any, any>> = (ctx: AuthorizeFunctionContext) => MaybePromise<Maybe<InferIOAuthorizeUser<InferIOAuthorize<TPluvServer>>>>;
|
|
49
|
+
type AuthorizeFunctionContext<TPluvServer extends PluvServer<any, any, any, any>> = {
|
|
50
|
+
room: string;
|
|
51
|
+
} & InferInitContextType<TPluvServer extends PluvServer<infer IPlatform, any, any, any> ? IPlatform : never>;
|
|
52
|
+
type AuthorizeFunction<TPluvServer extends PluvServer<NodePlatform, any, any, any>> = (ctx: AuthorizeFunctionContext<TPluvServer>) => MaybePromise<Maybe<InferIOAuthorizeUser<InferIOAuthorize<TPluvServer>>>>;
|
|
53
53
|
type CreatePluvHandlerConfig<TPluvServer extends PluvServer<NodePlatform, any, any, any>> = {
|
|
54
54
|
endpoint?: string;
|
|
55
55
|
io: TPluvServer;
|
package/dist/index.js
CHANGED
|
@@ -113,7 +113,7 @@ var createPluvHandler = (config) => {
|
|
|
113
113
|
const token = ((_a = parsed.query) == null ? void 0 : _a.token) || void 0;
|
|
114
114
|
if (Array.isArray(token)) return onError.invalidToken();
|
|
115
115
|
const room = getRoom(roomId);
|
|
116
|
-
yield room.register(ws, { token });
|
|
116
|
+
yield room.register(ws, { req, token });
|
|
117
117
|
return { matched: true };
|
|
118
118
|
});
|
|
119
119
|
const createWsServer = () => {
|
|
@@ -133,15 +133,15 @@ var createPluvHandler = (config) => {
|
|
|
133
133
|
const matcher = (0, import_path_to_regexp.match)(`${endpoint}/authorize`);
|
|
134
134
|
const matched = matcher(pathname);
|
|
135
135
|
if (!matched) return (_e = next == null ? void 0 : next()) != null ? _e : null;
|
|
136
|
-
const
|
|
137
|
-
if (typeof
|
|
136
|
+
const room = ((_f = parsed.query) == null ? void 0 : _f.room) || void 0;
|
|
137
|
+
if (typeof room !== "string" || !room) {
|
|
138
138
|
res.writeHead(404, { "Content-Type": "text/plain" });
|
|
139
139
|
return res.end("Room does not exist");
|
|
140
140
|
}
|
|
141
141
|
try {
|
|
142
|
-
const user = yield authorize({
|
|
142
|
+
const user = yield authorize({ req, room });
|
|
143
143
|
if (!user) throw new Error();
|
|
144
|
-
const token = yield io.createToken({ room
|
|
144
|
+
const token = yield io.createToken({ req, room, user });
|
|
145
145
|
res.writeHead(200, { "Content-Type": "text/plain" });
|
|
146
146
|
return res.end(token);
|
|
147
147
|
} catch (err) {
|
package/dist/index.mjs
CHANGED
|
@@ -77,7 +77,7 @@ var createPluvHandler = (config) => {
|
|
|
77
77
|
const token = ((_a = parsed.query) == null ? void 0 : _a.token) || void 0;
|
|
78
78
|
if (Array.isArray(token)) return onError.invalidToken();
|
|
79
79
|
const room = getRoom(roomId);
|
|
80
|
-
yield room.register(ws, { token });
|
|
80
|
+
yield room.register(ws, { req, token });
|
|
81
81
|
return { matched: true };
|
|
82
82
|
});
|
|
83
83
|
const createWsServer = () => {
|
|
@@ -97,15 +97,15 @@ var createPluvHandler = (config) => {
|
|
|
97
97
|
const matcher = match(`${endpoint}/authorize`);
|
|
98
98
|
const matched = matcher(pathname);
|
|
99
99
|
if (!matched) return (_e = next == null ? void 0 : next()) != null ? _e : null;
|
|
100
|
-
const
|
|
101
|
-
if (typeof
|
|
100
|
+
const room = ((_f = parsed.query) == null ? void 0 : _f.room) || void 0;
|
|
101
|
+
if (typeof room !== "string" || !room) {
|
|
102
102
|
res.writeHead(404, { "Content-Type": "text/plain" });
|
|
103
103
|
return res.end("Room does not exist");
|
|
104
104
|
}
|
|
105
105
|
try {
|
|
106
|
-
const user = yield authorize({
|
|
106
|
+
const user = yield authorize({ req, room });
|
|
107
107
|
if (!user) throw new Error();
|
|
108
|
-
const token = yield io.createToken({ room
|
|
108
|
+
const token = yield io.createToken({ req, room, user });
|
|
109
109
|
res.writeHead(200, { "Content-Type": "text/plain" });
|
|
110
110
|
return res.end(token);
|
|
111
111
|
} catch (err) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pluv/platform-node",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.32.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.7.4",
|
|
21
21
|
"@types/ws": "^8.5.12",
|
|
22
22
|
"path-to-regexp": "^8.2.0",
|
|
23
|
-
"@pluv/io": "^0.
|
|
24
|
-
"@pluv/types": "^0.
|
|
23
|
+
"@pluv/io": "^0.32.0",
|
|
24
|
+
"@pluv/types": "^0.32.0"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
27
|
"eslint": "^8.57.0",
|
|
28
28
|
"tsup": "^8.3.0",
|
|
29
29
|
"typescript": "^5.6.2",
|
|
30
30
|
"ws": "^8.18.0",
|
|
31
|
-
"@pluv/tsconfig": "^0.
|
|
32
|
-
"eslint-config-pluv": "^0.
|
|
31
|
+
"@pluv/tsconfig": "^0.32.0",
|
|
32
|
+
"eslint-config-pluv": "^0.32.0"
|
|
33
33
|
},
|
|
34
34
|
"peerDependencies": {
|
|
35
35
|
"ws": "^8.0.0"
|
package/src/NodePlatform.ts
CHANGED
|
@@ -8,6 +8,7 @@ import type {
|
|
|
8
8
|
} from "@pluv/io";
|
|
9
9
|
import { AbstractPlatform } from "@pluv/io";
|
|
10
10
|
import crypto from "node:crypto";
|
|
11
|
+
import type { IncomingMessage } from "node:http";
|
|
11
12
|
import { TextDecoder } from "node:util";
|
|
12
13
|
import type { WebSocket } from "ws";
|
|
13
14
|
import { NodeWebSocket } from "./NodeWebSocket";
|
|
@@ -17,7 +18,7 @@ export type NodePlatformConfig = { mode?: WebSocketRegistrationMode } & (
|
|
|
17
18
|
| { persistence: AbstractPersistence; pubSub: AbstractPubSub }
|
|
18
19
|
);
|
|
19
20
|
|
|
20
|
-
export class NodePlatform extends AbstractPlatform<NodeWebSocket> {
|
|
21
|
+
export class NodePlatform extends AbstractPlatform<NodeWebSocket, { req: IncomingMessage }, {}> {
|
|
21
22
|
readonly _registrationMode: WebSocketRegistrationMode;
|
|
22
23
|
|
|
23
24
|
constructor(config: NodePlatformConfig = {}) {
|
|
@@ -54,7 +55,7 @@ export class NodePlatform extends AbstractPlatform<NodeWebSocket> {
|
|
|
54
55
|
return [];
|
|
55
56
|
}
|
|
56
57
|
|
|
57
|
-
public initialize(config: AbstractPlatformConfig<{}
|
|
58
|
+
public initialize(config: AbstractPlatformConfig<{}>): this {
|
|
58
59
|
return new NodePlatform({
|
|
59
60
|
context: config.context,
|
|
60
61
|
mode: this._registrationMode,
|
package/src/createPluvHandler.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { PluvServer } from "@pluv/io";
|
|
1
|
+
import type { InferInitContextType, PluvServer } from "@pluv/io";
|
|
2
2
|
import type {
|
|
3
3
|
InferIOAuthorize,
|
|
4
4
|
InferIOAuthorizeRequired,
|
|
@@ -13,13 +13,12 @@ import type { WebSocket } from "ws";
|
|
|
13
13
|
import { Server as WsServer } from "ws";
|
|
14
14
|
import { NodePlatform } from "./NodePlatform";
|
|
15
15
|
|
|
16
|
-
export
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}
|
|
16
|
+
export type AuthorizeFunctionContext<TPluvServer extends PluvServer<any, any, any, any>> = {
|
|
17
|
+
room: string;
|
|
18
|
+
} & InferInitContextType<TPluvServer extends PluvServer<infer IPlatform, any, any, any> ? IPlatform : never>;
|
|
19
|
+
|
|
21
20
|
export type AuthorizeFunction<TPluvServer extends PluvServer<NodePlatform, any, any, any>> = (
|
|
22
|
-
ctx: AuthorizeFunctionContext
|
|
21
|
+
ctx: AuthorizeFunctionContext<TPluvServer>,
|
|
23
22
|
) => MaybePromise<Maybe<InferIOAuthorizeUser<InferIOAuthorize<TPluvServer>>>>;
|
|
24
23
|
|
|
25
24
|
export type CreatePluvHandlerConfig<TPluvServer extends PluvServer<NodePlatform, any, any, any>> = {
|
|
@@ -124,7 +123,7 @@ export const createPluvHandler = <TPluvServer extends PluvServer<NodePlatform, a
|
|
|
124
123
|
|
|
125
124
|
const room = getRoom(roomId);
|
|
126
125
|
|
|
127
|
-
await room.register(ws, { token });
|
|
126
|
+
await room.register(ws, { req, token });
|
|
128
127
|
|
|
129
128
|
return { matched: true };
|
|
130
129
|
};
|
|
@@ -157,19 +156,19 @@ export const createPluvHandler = <TPluvServer extends PluvServer<NodePlatform, a
|
|
|
157
156
|
|
|
158
157
|
if (!matched) return next?.() ?? null;
|
|
159
158
|
|
|
160
|
-
const
|
|
159
|
+
const room = parsed.query?.room || undefined;
|
|
161
160
|
|
|
162
|
-
if (typeof
|
|
161
|
+
if (typeof room !== "string" || !room) {
|
|
163
162
|
res.writeHead(404, { "Content-Type": "text/plain" });
|
|
164
163
|
return res.end("Room does not exist");
|
|
165
164
|
}
|
|
166
165
|
|
|
167
166
|
try {
|
|
168
|
-
const user = await authorize({
|
|
167
|
+
const user = await authorize({ req, room } as AuthorizeFunctionContext<TPluvServer>);
|
|
169
168
|
|
|
170
169
|
if (!user) throw new Error();
|
|
171
170
|
|
|
172
|
-
const token = await io.createToken({ room
|
|
171
|
+
const token = await io.createToken({ req, room, user });
|
|
173
172
|
|
|
174
173
|
res.writeHead(200, { "Content-Type": "text/plain" });
|
|
175
174
|
return res.end(token);
|