@pluv/platform-node 0.1.17 → 0.2.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 +7 -7
- package/CHANGELOG.md +28 -0
- package/dist/index.d.ts +32 -3
- package/dist/index.js +95 -0
- package/dist/index.mjs +94 -0
- package/package.json +11 -6
- package/src/createPluvHandler.ts +173 -0
- package/src/index.ts +6 -0
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
|
|
2
|
-
> @pluv/platform-node@0.
|
|
2
|
+
> @pluv/platform-node@0.2.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,10 +8,10 @@
|
|
|
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[
|
|
12
|
-
[32mESM[39m ⚡️ Build success in
|
|
13
|
-
[32mCJS[39m [1mdist/index.js [22m[
|
|
14
|
-
[32mCJS[39m ⚡️ Build success in
|
|
11
|
+
[32mESM[39m [1mdist/index.mjs [22m[32m6.15 KB[39m
|
|
12
|
+
[32mESM[39m ⚡️ Build success in 84ms
|
|
13
|
+
[32mCJS[39m [1mdist/index.js [22m[32m7.97 KB[39m
|
|
14
|
+
[32mCJS[39m ⚡️ Build success in 85ms
|
|
15
15
|
[34mDTS[39m Build start
|
|
16
|
-
[32mDTS[39m ⚡️ Build success in
|
|
17
|
-
[32mDTS[39m [1mdist/index.d.ts [22m[
|
|
16
|
+
[32mDTS[39m ⚡️ Build success in 5893ms
|
|
17
|
+
[32mDTS[39m [1mdist/index.d.ts [22m[32m2.72 KB[39m
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,33 @@
|
|
|
1
1
|
# @pluv/platform-node
|
|
2
2
|
|
|
3
|
+
## 0.2.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 5bbfb98: added createPluvHandler to automatically set-up a pluv server with reasonable defaults
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- 5bbfb98: require ws as a peer dependency
|
|
12
|
+
- b85a232: bumped dependencies
|
|
13
|
+
- Updated dependencies [b85a232]
|
|
14
|
+
- @pluv/io@0.4.1
|
|
15
|
+
- @pluv/types@0.2.0
|
|
16
|
+
|
|
17
|
+
## 0.1.18
|
|
18
|
+
|
|
19
|
+
### Patch Changes
|
|
20
|
+
|
|
21
|
+
- 41943cf: bumped dependencies
|
|
22
|
+
- 3518a83: bumped dependencies
|
|
23
|
+
- Updated dependencies [41943cf]
|
|
24
|
+
- Updated dependencies [0dd847e]
|
|
25
|
+
- Updated dependencies [bb2886b]
|
|
26
|
+
- Updated dependencies [ae679a8]
|
|
27
|
+
- Updated dependencies [3518a83]
|
|
28
|
+
- @pluv/io@0.4.0
|
|
29
|
+
- @pluv/types@0.2.0
|
|
30
|
+
|
|
3
31
|
## 0.1.17
|
|
4
32
|
|
|
5
33
|
### Patch Changes
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { AbstractWebSocket, AbstractEventMap, AbstractListener, AbstractWebSocketConfig, AbstractPersistance, AbstractPubSub, AbstractPlatform, ConvertWebSocketConfig, PluvIO } from '@pluv/io';
|
|
2
|
+
import { MaybePromise, Maybe, InferIOAuthorizeUser, InferIOAuthorize, InferIOAuthorizeRequired } from '@pluv/types';
|
|
3
|
+
import Http from 'http';
|
|
4
|
+
import WS, { WebSocket } from 'ws';
|
|
2
5
|
export { WebSocket } from 'ws';
|
|
3
|
-
import { AbstractWebSocket, AbstractEventMap, AbstractListener, AbstractWebSocketConfig, AbstractPersistance, AbstractPubSub, AbstractPlatform, ConvertWebSocketConfig } from '@pluv/io';
|
|
4
6
|
|
|
5
7
|
type NodeWebSocketConfig = AbstractWebSocketConfig;
|
|
6
8
|
declare class NodeWebSocket extends AbstractWebSocket {
|
|
@@ -28,6 +30,33 @@ declare class NodePlatform extends AbstractPlatform<WebSocket> {
|
|
|
28
30
|
randomUUID(): string;
|
|
29
31
|
}
|
|
30
32
|
|
|
33
|
+
interface AuthorizeFunctionContext {
|
|
34
|
+
req: Http.IncomingMessage;
|
|
35
|
+
res: Http.ServerResponse;
|
|
36
|
+
roomId: string;
|
|
37
|
+
}
|
|
38
|
+
type AuthorizeFunction<TPluv extends PluvIO<NodePlatform>> = (ctx: AuthorizeFunctionContext) => MaybePromise<Maybe<InferIOAuthorizeUser<InferIOAuthorize<TPluv>>>>;
|
|
39
|
+
type CreatePluvHandlerConfig<TPluv extends PluvIO<NodePlatform>> = {
|
|
40
|
+
endpoint?: string;
|
|
41
|
+
io: TPluv;
|
|
42
|
+
server: Http.Server;
|
|
43
|
+
} & (InferIOAuthorizeRequired<InferIOAuthorize<TPluv>> extends true ? {
|
|
44
|
+
authorize: AuthorizeFunction<TPluv>;
|
|
45
|
+
} : {
|
|
46
|
+
authorize?: undefined;
|
|
47
|
+
});
|
|
48
|
+
type RequestHandler = (req: Http.IncomingMessage, res: Http.ServerResponse, next?: () => void) => void;
|
|
49
|
+
interface WebSocketHandlerResult {
|
|
50
|
+
matched: boolean;
|
|
51
|
+
}
|
|
52
|
+
type WebSocketHandler = (ws: WS.WebSocket, req: Http.IncomingMessage) => Promise<WebSocketHandlerResult>;
|
|
53
|
+
interface CreatePluvHandlerResult {
|
|
54
|
+
createWsServer: () => WS.Server<WS.WebSocket>;
|
|
55
|
+
handler: RequestHandler;
|
|
56
|
+
wsHandler: WebSocketHandler;
|
|
57
|
+
}
|
|
58
|
+
declare const createPluvHandler: <TPluv extends PluvIO<NodePlatform, any, any, any, any, any, any>>(config: CreatePluvHandlerConfig<TPluv>) => CreatePluvHandlerResult;
|
|
59
|
+
|
|
31
60
|
declare const platformNode: (options?: NodePlatformOptions) => NodePlatform;
|
|
32
61
|
|
|
33
|
-
export { NodePlatformOptions, platformNode };
|
|
62
|
+
export { AuthorizeFunction, AuthorizeFunctionContext, CreatePluvHandlerConfig, NodePlatformOptions, createPluvHandler, platformNode };
|
package/dist/index.js
CHANGED
|
@@ -50,10 +50,104 @@ var __async = (__this, __arguments, generator) => {
|
|
|
50
50
|
// src/index.ts
|
|
51
51
|
var src_exports = {};
|
|
52
52
|
__export(src_exports, {
|
|
53
|
+
createPluvHandler: () => createPluvHandler,
|
|
53
54
|
platformNode: () => platformNode
|
|
54
55
|
});
|
|
55
56
|
module.exports = __toCommonJS(src_exports);
|
|
56
57
|
|
|
58
|
+
// src/createPluvHandler.ts
|
|
59
|
+
var import_path_to_regexp = require("path-to-regexp");
|
|
60
|
+
var import_url = __toESM(require("url"));
|
|
61
|
+
var import_ws = __toESM(require("ws"));
|
|
62
|
+
var handle = (ws) => ({
|
|
63
|
+
invalidEndpoint: () => {
|
|
64
|
+
ws.close(1011, "Invalid WebSocket endpoint");
|
|
65
|
+
return { matched: false };
|
|
66
|
+
},
|
|
67
|
+
invalidRoom: () => {
|
|
68
|
+
ws.close(1011, "Invalid room name");
|
|
69
|
+
return { matched: true };
|
|
70
|
+
},
|
|
71
|
+
invalidToken: () => {
|
|
72
|
+
ws.close(1011, "Invalid authentication token");
|
|
73
|
+
return { matched: true };
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
var createPluvHandler = (config) => {
|
|
77
|
+
const { authorize, endpoint = "/api/pluv", io, server } = config;
|
|
78
|
+
const wsServer = new import_ws.default.Server({ server });
|
|
79
|
+
const wsHandler = (ws, req) => __async(void 0, null, function* () {
|
|
80
|
+
var _a;
|
|
81
|
+
const url = req.url;
|
|
82
|
+
const onError = handle(ws);
|
|
83
|
+
if (!url) {
|
|
84
|
+
onError.invalidEndpoint();
|
|
85
|
+
return { matched: false };
|
|
86
|
+
}
|
|
87
|
+
const parsed = import_url.default.parse(url, true);
|
|
88
|
+
const { pathname } = parsed;
|
|
89
|
+
if (!pathname)
|
|
90
|
+
return onError.invalidEndpoint();
|
|
91
|
+
const matcher = (0, import_path_to_regexp.match)(`${endpoint}/room/:roomId`);
|
|
92
|
+
const matched = matcher(pathname);
|
|
93
|
+
if (!matched)
|
|
94
|
+
return onError.invalidEndpoint();
|
|
95
|
+
const { roomId } = matched.params;
|
|
96
|
+
if (!roomId)
|
|
97
|
+
return onError.invalidRoom();
|
|
98
|
+
const token = ((_a = parsed.query) == null ? void 0 : _a.token) || void 0;
|
|
99
|
+
if (Array.isArray(token))
|
|
100
|
+
return onError.invalidToken();
|
|
101
|
+
const room = io.getRoom(roomId);
|
|
102
|
+
yield room.register(ws, { token });
|
|
103
|
+
return { matched: true };
|
|
104
|
+
});
|
|
105
|
+
const createWsServer = () => {
|
|
106
|
+
return wsServer.on("connection", (ws, req) => __async(void 0, null, function* () {
|
|
107
|
+
yield wsHandler(ws, req);
|
|
108
|
+
}));
|
|
109
|
+
};
|
|
110
|
+
const handler = (req, res, next) => __async(void 0, null, function* () {
|
|
111
|
+
var _a, _b, _c, _d, _e, _f;
|
|
112
|
+
if (!authorize)
|
|
113
|
+
return (_a = next == null ? void 0 : next()) != null ? _a : null;
|
|
114
|
+
if (req.method !== "GET")
|
|
115
|
+
return (_b = next == null ? void 0 : next()) != null ? _b : null;
|
|
116
|
+
const url = req.url;
|
|
117
|
+
if (!url)
|
|
118
|
+
return (_c = next == null ? void 0 : next()) != null ? _c : null;
|
|
119
|
+
const parsed = import_url.default.parse(url, true);
|
|
120
|
+
const { pathname } = parsed;
|
|
121
|
+
if (!pathname)
|
|
122
|
+
return (_d = next == null ? void 0 : next()) != null ? _d : null;
|
|
123
|
+
const matcher = (0, import_path_to_regexp.match)(`${endpoint}/authorize`);
|
|
124
|
+
const matched = matcher(pathname);
|
|
125
|
+
if (!matched)
|
|
126
|
+
return (_e = next == null ? void 0 : next()) != null ? _e : null;
|
|
127
|
+
const roomId = ((_f = parsed.query) == null ? void 0 : _f.room) || void 0;
|
|
128
|
+
if (typeof roomId !== "string" || !roomId) {
|
|
129
|
+
res.writeHead(404, { "Content-Type": "text/plain" });
|
|
130
|
+
return res.end("Room does not exist");
|
|
131
|
+
}
|
|
132
|
+
try {
|
|
133
|
+
const user = yield authorize({ res, req, roomId });
|
|
134
|
+
if (!user)
|
|
135
|
+
throw new Error();
|
|
136
|
+
const token = yield io.createToken({ room: roomId, user });
|
|
137
|
+
res.writeHead(200, { "Content-Type": "text/plain" });
|
|
138
|
+
return res.end(token);
|
|
139
|
+
} catch (err) {
|
|
140
|
+
res.writeHead(403, { "Content-Type": "text/plain" });
|
|
141
|
+
return res.end(err instanceof Error ? err.message : "Unauthorized");
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
return {
|
|
145
|
+
createWsServer,
|
|
146
|
+
handler,
|
|
147
|
+
wsHandler
|
|
148
|
+
};
|
|
149
|
+
};
|
|
150
|
+
|
|
57
151
|
// src/NodePlatform.ts
|
|
58
152
|
var import_io2 = require("@pluv/io");
|
|
59
153
|
var import_crypto = __toESM(require("crypto"));
|
|
@@ -157,5 +251,6 @@ var platformNode = (options = {}) => {
|
|
|
157
251
|
};
|
|
158
252
|
// Annotate the CommonJS export names for ESM import in node:
|
|
159
253
|
0 && (module.exports = {
|
|
254
|
+
createPluvHandler,
|
|
160
255
|
platformNode
|
|
161
256
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -19,6 +19,99 @@ var __async = (__this, __arguments, generator) => {
|
|
|
19
19
|
});
|
|
20
20
|
};
|
|
21
21
|
|
|
22
|
+
// src/createPluvHandler.ts
|
|
23
|
+
import { match } from "path-to-regexp";
|
|
24
|
+
import Url from "url";
|
|
25
|
+
import WS from "ws";
|
|
26
|
+
var handle = (ws) => ({
|
|
27
|
+
invalidEndpoint: () => {
|
|
28
|
+
ws.close(1011, "Invalid WebSocket endpoint");
|
|
29
|
+
return { matched: false };
|
|
30
|
+
},
|
|
31
|
+
invalidRoom: () => {
|
|
32
|
+
ws.close(1011, "Invalid room name");
|
|
33
|
+
return { matched: true };
|
|
34
|
+
},
|
|
35
|
+
invalidToken: () => {
|
|
36
|
+
ws.close(1011, "Invalid authentication token");
|
|
37
|
+
return { matched: true };
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
var createPluvHandler = (config) => {
|
|
41
|
+
const { authorize, endpoint = "/api/pluv", io, server } = config;
|
|
42
|
+
const wsServer = new WS.Server({ server });
|
|
43
|
+
const wsHandler = (ws, req) => __async(void 0, null, function* () {
|
|
44
|
+
var _a;
|
|
45
|
+
const url = req.url;
|
|
46
|
+
const onError = handle(ws);
|
|
47
|
+
if (!url) {
|
|
48
|
+
onError.invalidEndpoint();
|
|
49
|
+
return { matched: false };
|
|
50
|
+
}
|
|
51
|
+
const parsed = Url.parse(url, true);
|
|
52
|
+
const { pathname } = parsed;
|
|
53
|
+
if (!pathname)
|
|
54
|
+
return onError.invalidEndpoint();
|
|
55
|
+
const matcher = match(`${endpoint}/room/:roomId`);
|
|
56
|
+
const matched = matcher(pathname);
|
|
57
|
+
if (!matched)
|
|
58
|
+
return onError.invalidEndpoint();
|
|
59
|
+
const { roomId } = matched.params;
|
|
60
|
+
if (!roomId)
|
|
61
|
+
return onError.invalidRoom();
|
|
62
|
+
const token = ((_a = parsed.query) == null ? void 0 : _a.token) || void 0;
|
|
63
|
+
if (Array.isArray(token))
|
|
64
|
+
return onError.invalidToken();
|
|
65
|
+
const room = io.getRoom(roomId);
|
|
66
|
+
yield room.register(ws, { token });
|
|
67
|
+
return { matched: true };
|
|
68
|
+
});
|
|
69
|
+
const createWsServer = () => {
|
|
70
|
+
return wsServer.on("connection", (ws, req) => __async(void 0, null, function* () {
|
|
71
|
+
yield wsHandler(ws, req);
|
|
72
|
+
}));
|
|
73
|
+
};
|
|
74
|
+
const handler = (req, res, next) => __async(void 0, null, function* () {
|
|
75
|
+
var _a, _b, _c, _d, _e, _f;
|
|
76
|
+
if (!authorize)
|
|
77
|
+
return (_a = next == null ? void 0 : next()) != null ? _a : null;
|
|
78
|
+
if (req.method !== "GET")
|
|
79
|
+
return (_b = next == null ? void 0 : next()) != null ? _b : null;
|
|
80
|
+
const url = req.url;
|
|
81
|
+
if (!url)
|
|
82
|
+
return (_c = next == null ? void 0 : next()) != null ? _c : null;
|
|
83
|
+
const parsed = Url.parse(url, true);
|
|
84
|
+
const { pathname } = parsed;
|
|
85
|
+
if (!pathname)
|
|
86
|
+
return (_d = next == null ? void 0 : next()) != null ? _d : null;
|
|
87
|
+
const matcher = match(`${endpoint}/authorize`);
|
|
88
|
+
const matched = matcher(pathname);
|
|
89
|
+
if (!matched)
|
|
90
|
+
return (_e = next == null ? void 0 : next()) != null ? _e : null;
|
|
91
|
+
const roomId = ((_f = parsed.query) == null ? void 0 : _f.room) || void 0;
|
|
92
|
+
if (typeof roomId !== "string" || !roomId) {
|
|
93
|
+
res.writeHead(404, { "Content-Type": "text/plain" });
|
|
94
|
+
return res.end("Room does not exist");
|
|
95
|
+
}
|
|
96
|
+
try {
|
|
97
|
+
const user = yield authorize({ res, req, roomId });
|
|
98
|
+
if (!user)
|
|
99
|
+
throw new Error();
|
|
100
|
+
const token = yield io.createToken({ room: roomId, user });
|
|
101
|
+
res.writeHead(200, { "Content-Type": "text/plain" });
|
|
102
|
+
return res.end(token);
|
|
103
|
+
} catch (err) {
|
|
104
|
+
res.writeHead(403, { "Content-Type": "text/plain" });
|
|
105
|
+
return res.end(err instanceof Error ? err.message : "Unauthorized");
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
return {
|
|
109
|
+
createWsServer,
|
|
110
|
+
handler,
|
|
111
|
+
wsHandler
|
|
112
|
+
};
|
|
113
|
+
};
|
|
114
|
+
|
|
22
115
|
// src/NodePlatform.ts
|
|
23
116
|
import { AbstractPlatform } from "@pluv/io";
|
|
24
117
|
import crypto from "crypto";
|
|
@@ -121,5 +214,6 @@ var platformNode = (options = {}) => {
|
|
|
121
214
|
return new NodePlatform(options);
|
|
122
215
|
};
|
|
123
216
|
export {
|
|
217
|
+
createPluvHandler,
|
|
124
218
|
platformNode
|
|
125
219
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pluv/platform-node",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "@pluv/io adapter for node.js",
|
|
5
5
|
"author": "leedavidcs",
|
|
6
6
|
"license": "MIT",
|
|
@@ -17,16 +17,21 @@
|
|
|
17
17
|
"access": "public"
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@types/node": "^
|
|
20
|
+
"@types/node": "^20.1.4",
|
|
21
21
|
"@types/ws": "^8.5.4",
|
|
22
|
-
"
|
|
23
|
-
"@pluv/
|
|
22
|
+
"path-to-regexp": "^6.2.1",
|
|
23
|
+
"@pluv/io": "^0.4.1",
|
|
24
|
+
"@pluv/types": "^0.2.0"
|
|
24
25
|
},
|
|
25
26
|
"devDependencies": {
|
|
26
27
|
"tsup": "^6.7.0",
|
|
27
28
|
"typescript": "^5.0.4",
|
|
28
|
-
"
|
|
29
|
-
"
|
|
29
|
+
"ws": "^8.13.0",
|
|
30
|
+
"@pluv/tsconfig": "^0.1.6",
|
|
31
|
+
"eslint-config-pluv": "^0.1.12"
|
|
32
|
+
},
|
|
33
|
+
"peerDependencies": {
|
|
34
|
+
"ws": "^8.0.0"
|
|
30
35
|
},
|
|
31
36
|
"scripts": {
|
|
32
37
|
"build": "tsup src/index.ts --format esm,cjs --dts",
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import type { PluvIO } from "@pluv/io";
|
|
2
|
+
import {
|
|
3
|
+
InferIOAuthorize,
|
|
4
|
+
InferIOAuthorizeRequired,
|
|
5
|
+
InferIOAuthorizeUser,
|
|
6
|
+
Maybe,
|
|
7
|
+
MaybePromise,
|
|
8
|
+
} from "@pluv/types";
|
|
9
|
+
import Http from "http";
|
|
10
|
+
import { match } from "path-to-regexp";
|
|
11
|
+
import Url from "url";
|
|
12
|
+
import WS from "ws";
|
|
13
|
+
import { NodePlatform } from "./NodePlatform";
|
|
14
|
+
|
|
15
|
+
export interface AuthorizeFunctionContext {
|
|
16
|
+
req: Http.IncomingMessage;
|
|
17
|
+
res: Http.ServerResponse;
|
|
18
|
+
roomId: string;
|
|
19
|
+
}
|
|
20
|
+
export type AuthorizeFunction<TPluv extends PluvIO<NodePlatform>> = (
|
|
21
|
+
ctx: AuthorizeFunctionContext
|
|
22
|
+
) => MaybePromise<Maybe<InferIOAuthorizeUser<InferIOAuthorize<TPluv>>>>;
|
|
23
|
+
|
|
24
|
+
export type CreatePluvHandlerConfig<TPluv extends PluvIO<NodePlatform>> = {
|
|
25
|
+
endpoint?: string;
|
|
26
|
+
io: TPluv;
|
|
27
|
+
server: Http.Server;
|
|
28
|
+
} & (InferIOAuthorizeRequired<InferIOAuthorize<TPluv>> extends true
|
|
29
|
+
? { authorize: AuthorizeFunction<TPluv> }
|
|
30
|
+
: { authorize?: undefined });
|
|
31
|
+
|
|
32
|
+
const handle = (ws: WS.WebSocket) => ({
|
|
33
|
+
invalidEndpoint: () => {
|
|
34
|
+
ws.close(1011, "Invalid WebSocket endpoint");
|
|
35
|
+
|
|
36
|
+
return { matched: false };
|
|
37
|
+
},
|
|
38
|
+
invalidRoom: () => {
|
|
39
|
+
ws.close(1011, "Invalid room name");
|
|
40
|
+
|
|
41
|
+
return { matched: true };
|
|
42
|
+
},
|
|
43
|
+
invalidToken: () => {
|
|
44
|
+
ws.close(1011, "Invalid authentication token");
|
|
45
|
+
|
|
46
|
+
return { matched: true };
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
export type RequestHandler = (
|
|
51
|
+
req: Http.IncomingMessage,
|
|
52
|
+
res: Http.ServerResponse,
|
|
53
|
+
next?: () => void
|
|
54
|
+
) => void;
|
|
55
|
+
|
|
56
|
+
export interface WebSocketHandlerResult {
|
|
57
|
+
matched: boolean;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export type WebSocketHandler = (
|
|
61
|
+
ws: WS.WebSocket,
|
|
62
|
+
req: Http.IncomingMessage
|
|
63
|
+
) => Promise<WebSocketHandlerResult>;
|
|
64
|
+
|
|
65
|
+
export interface CreatePluvHandlerResult {
|
|
66
|
+
createWsServer: () => WS.Server<WS.WebSocket>;
|
|
67
|
+
handler: RequestHandler;
|
|
68
|
+
wsHandler: WebSocketHandler;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export const createPluvHandler = <
|
|
72
|
+
TPluv extends PluvIO<NodePlatform, any, any, any, any, any, any>
|
|
73
|
+
>(
|
|
74
|
+
config: CreatePluvHandlerConfig<TPluv>
|
|
75
|
+
): CreatePluvHandlerResult => {
|
|
76
|
+
const { authorize, endpoint = "/api/pluv", io, server } = config;
|
|
77
|
+
|
|
78
|
+
const wsServer = new WS.Server({ server });
|
|
79
|
+
|
|
80
|
+
const wsHandler = async (
|
|
81
|
+
ws: WS.WebSocket,
|
|
82
|
+
req: Http.IncomingMessage
|
|
83
|
+
): Promise<WebSocketHandlerResult> => {
|
|
84
|
+
const url = req.url;
|
|
85
|
+
const onError = handle(ws);
|
|
86
|
+
|
|
87
|
+
if (!url) {
|
|
88
|
+
onError.invalidEndpoint();
|
|
89
|
+
|
|
90
|
+
return { matched: false };
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const parsed = Url.parse(url, true);
|
|
94
|
+
const { pathname } = parsed;
|
|
95
|
+
|
|
96
|
+
if (!pathname) return onError.invalidEndpoint();
|
|
97
|
+
|
|
98
|
+
const matcher = match<{ roomId: string }>(`${endpoint}/room/:roomId`);
|
|
99
|
+
const matched = matcher(pathname);
|
|
100
|
+
|
|
101
|
+
if (!matched) return onError.invalidEndpoint();
|
|
102
|
+
|
|
103
|
+
const { roomId } = matched.params;
|
|
104
|
+
|
|
105
|
+
if (!roomId) return onError.invalidRoom();
|
|
106
|
+
|
|
107
|
+
const token = parsed.query?.token || undefined;
|
|
108
|
+
|
|
109
|
+
if (Array.isArray(token)) return onError.invalidToken();
|
|
110
|
+
|
|
111
|
+
const room = io.getRoom(roomId);
|
|
112
|
+
|
|
113
|
+
await room.register(ws, { token });
|
|
114
|
+
|
|
115
|
+
return { matched: true };
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
const createWsServer = () => {
|
|
119
|
+
return wsServer.on("connection", async (ws, req) => {
|
|
120
|
+
await wsHandler(ws, req);
|
|
121
|
+
});
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
const handler = async (
|
|
125
|
+
req: Http.IncomingMessage,
|
|
126
|
+
res: Http.ServerResponse,
|
|
127
|
+
next?: () => void
|
|
128
|
+
): Promise<Http.ServerResponse | null> => {
|
|
129
|
+
if (!authorize) return next?.() ?? null;
|
|
130
|
+
if (req.method !== "GET") return next?.() ?? null;
|
|
131
|
+
|
|
132
|
+
const url = req.url;
|
|
133
|
+
|
|
134
|
+
if (!url) return next?.() ?? null;
|
|
135
|
+
|
|
136
|
+
const parsed = Url.parse(url, true);
|
|
137
|
+
const { pathname } = parsed;
|
|
138
|
+
|
|
139
|
+
if (!pathname) return next?.() ?? null;
|
|
140
|
+
|
|
141
|
+
const matcher = match<{}>(`${endpoint}/authorize`);
|
|
142
|
+
const matched = matcher(pathname);
|
|
143
|
+
|
|
144
|
+
if (!matched) return next?.() ?? null;
|
|
145
|
+
|
|
146
|
+
const roomId = parsed.query?.room || undefined;
|
|
147
|
+
|
|
148
|
+
if (typeof roomId !== "string" || !roomId) {
|
|
149
|
+
res.writeHead(404, { "Content-Type": "text/plain" });
|
|
150
|
+
return res.end("Room does not exist");
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
try {
|
|
154
|
+
const user = await authorize({ res, req, roomId });
|
|
155
|
+
|
|
156
|
+
if (!user) throw new Error();
|
|
157
|
+
|
|
158
|
+
const token = await io.createToken({ room: roomId, user });
|
|
159
|
+
|
|
160
|
+
res.writeHead(200, { "Content-Type": "text/plain" });
|
|
161
|
+
return res.end(token);
|
|
162
|
+
} catch (err) {
|
|
163
|
+
res.writeHead(403, { "Content-Type": "text/plain" });
|
|
164
|
+
return res.end(err instanceof Error ? err.message : "Unauthorized");
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
return {
|
|
169
|
+
createWsServer,
|
|
170
|
+
handler,
|
|
171
|
+
wsHandler,
|
|
172
|
+
};
|
|
173
|
+
};
|
package/src/index.ts
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
export type {
|
|
2
|
+
AuthorizeFunction,
|
|
3
|
+
AuthorizeFunctionContext,
|
|
4
|
+
CreatePluvHandlerConfig,
|
|
5
|
+
} from "./createPluvHandler";
|
|
6
|
+
export { createPluvHandler } from "./createPluvHandler";
|
|
1
7
|
export type { WebSocket } from "ws";
|
|
2
8
|
export type { NodePlatformOptions } from "./NodePlatform";
|
|
3
9
|
export { platformNode } from "./platformNode";
|