@pluv/platform-node 0.17.3 → 0.19.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 +15 -15
- package/CHANGELOG.md +111 -0
- package/README.md +3 -4
- package/dist/index.d.mts +16 -15
- package/dist/index.d.ts +16 -15
- package/dist/index.js +20 -36
- package/dist/index.mjs +18 -34
- package/package.json +9 -9
- package/src/NodePlatform.ts +2 -8
- package/src/NodeWebSocket.ts +2 -7
- package/src/createPluvHandler.ts +26 -37
- package/src/index.ts +1 -5
- package/src/platformNode.ts +1 -3
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
|
|
2
|
-
> @pluv/platform-node@0.
|
|
2
|
+
> @pluv/platform-node@0.19.0 build /home/runner/work/pluv/pluv/packages/platform-node
|
|
3
3
|
> tsup src/index.ts --format esm,cjs --dts
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
5
|
+
CLI Building entry: src/index.ts
|
|
6
|
+
CLI Using tsconfig: tsconfig.json
|
|
7
|
+
CLI tsup v8.1.0
|
|
8
|
+
CLI Target: es6
|
|
9
|
+
ESM Build start
|
|
10
|
+
CJS Build start
|
|
11
|
+
ESM dist/index.mjs 6.12 KB
|
|
12
|
+
ESM ⚡️ Build success in 82ms
|
|
13
|
+
CJS dist/index.js 7.93 KB
|
|
14
|
+
CJS ⚡️ Build success in 86ms
|
|
15
|
+
DTS Build start
|
|
16
|
+
DTS ⚡️ Build success in 4070ms
|
|
17
|
+
DTS dist/index.d.mts 2.92 KB
|
|
18
|
+
DTS dist/index.d.ts 2.92 KB
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,116 @@
|
|
|
1
1
|
# @pluv/platform-node
|
|
2
2
|
|
|
3
|
+
## 0.19.0
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- @pluv/io@0.19.0
|
|
8
|
+
- @pluv/types@0.19.0
|
|
9
|
+
|
|
10
|
+
## 0.18.0
|
|
11
|
+
|
|
12
|
+
### Minor Changes
|
|
13
|
+
|
|
14
|
+
- 99b5ca9: ## Breaking Changes
|
|
15
|
+
|
|
16
|
+
- `@pluv/io` has been updated to introduce `PluvProcedure`, `PluvRouter` and `PluvServer`. This change is intended to improve the ergonomics of declaring events and simplifying inferences of event types.
|
|
17
|
+
|
|
18
|
+
### Before:
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
// backend/io.ts
|
|
22
|
+
|
|
23
|
+
import { createIO } from "@pluv/io";
|
|
24
|
+
import { createPluvHandler, platformNode } from "@pluv/platform-node";
|
|
25
|
+
import { z } from "zod";
|
|
26
|
+
|
|
27
|
+
export const io = createIO({
|
|
28
|
+
platform: platformNode(),
|
|
29
|
+
})
|
|
30
|
+
.event("SEND_MESSAGE", {
|
|
31
|
+
input: z.object({ message: z.string() }),
|
|
32
|
+
resolver: ({ message }) => ({ RECEIVE_MESSAGE: { message } }),
|
|
33
|
+
})
|
|
34
|
+
.event("DOUBLE_VALUE", {
|
|
35
|
+
input: z.object({ value: z.number() }),
|
|
36
|
+
resolver: ({ value }) => ({ VALUE_DOUBLED: { value: value * 2 } }),
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
const Pluv = createPluvHandler({
|
|
40
|
+
io,
|
|
41
|
+
/* ... */
|
|
42
|
+
});
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
```ts
|
|
46
|
+
// frontend/pluv.ts
|
|
47
|
+
|
|
48
|
+
import { createClient } from "@pluv/react";
|
|
49
|
+
import type { io } from "../backend/io";
|
|
50
|
+
|
|
51
|
+
const client = createClient<typeof io>({
|
|
52
|
+
/* ... */
|
|
53
|
+
});
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### Now:
|
|
57
|
+
|
|
58
|
+
```ts
|
|
59
|
+
import { createIO } from "@pluv/io";
|
|
60
|
+
import { createPluvHandler, platformNode } from "@pluv/platform-node";
|
|
61
|
+
import { z } from "zod";
|
|
62
|
+
|
|
63
|
+
const io = createIO({
|
|
64
|
+
platform: platformNode(),
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
const router = io.router({
|
|
68
|
+
SEND_MESSAGE: io.procedure
|
|
69
|
+
.input(z.object({ message: z.string() }))
|
|
70
|
+
.broadcast(({ message }) => ({
|
|
71
|
+
RECEIVE_MESSAGE: { message },
|
|
72
|
+
})),
|
|
73
|
+
DOUBLE_VALUE: io.procedure
|
|
74
|
+
.input(z.object({ value: z.number() }))
|
|
75
|
+
.broadcast(({ value }) => ({
|
|
76
|
+
VALUE_DOUBLED: { value: value * 2 },
|
|
77
|
+
})),
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
export const ioServer = io.server({ router });
|
|
81
|
+
|
|
82
|
+
const Pluv = createPluvHandler({
|
|
83
|
+
io: ioServer, // <- This uses the PluvServer now
|
|
84
|
+
/* ... */
|
|
85
|
+
});
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
```ts
|
|
89
|
+
// frontend/pluv.ts
|
|
90
|
+
|
|
91
|
+
import { createClient } from "@pluv/react";
|
|
92
|
+
import type { ioServer } from "../backend/io";
|
|
93
|
+
|
|
94
|
+
// This users the PluvServer type now
|
|
95
|
+
const client = createClient<typeof ioServer>({
|
|
96
|
+
/* ... */
|
|
97
|
+
});
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
- `PluvRouter` instances can also be merged via the `mergeRouters` method, which effectively performs an `Object.assign` of the events object and returns a new `PluvRouter` with the correct types:
|
|
101
|
+
|
|
102
|
+
```ts
|
|
103
|
+
const router = io.mergeRouters(router1, router2);
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### Patch Changes
|
|
107
|
+
|
|
108
|
+
- Updated dependencies [428c21c]
|
|
109
|
+
- Updated dependencies [329dbcd]
|
|
110
|
+
- Updated dependencies [99b5ca9]
|
|
111
|
+
- @pluv/io@0.18.0
|
|
112
|
+
- @pluv/types@0.18.0
|
|
113
|
+
|
|
3
114
|
## 0.17.3
|
|
4
115
|
|
|
5
116
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -52,9 +52,8 @@ import { platformNode } from "@pluv/platform-node";
|
|
|
52
52
|
import Http from "http";
|
|
53
53
|
import WebSocket from "ws";
|
|
54
54
|
|
|
55
|
-
export const io = createIO({
|
|
56
|
-
|
|
57
|
-
});
|
|
55
|
+
export const io = createIO({ platform: platformNode() });
|
|
56
|
+
export const ioServer = io.server();
|
|
58
57
|
|
|
59
58
|
const server = Http.createServer();
|
|
60
59
|
const wsServer = new WebSocket.Server({ server });
|
|
@@ -65,7 +64,7 @@ const parseRoomId = (url: string): string => {
|
|
|
65
64
|
|
|
66
65
|
wsServer.on("connection", async (ws, req) => {
|
|
67
66
|
const roomId = parseRoomId(req.url);
|
|
68
|
-
const room =
|
|
67
|
+
const room = ioServer.getRoom(roomId);
|
|
69
68
|
|
|
70
69
|
await room.register(ws);
|
|
71
70
|
});
|
package/dist/index.d.mts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import { AbstractWebSocket, AbstractEventMap, AbstractListener, AbstractWebSocketConfig, AbstractPersistance, AbstractPubSub, AbstractPlatform, ConvertWebSocketConfig,
|
|
1
|
+
import { AbstractWebSocket, AbstractEventMap, AbstractListener, AbstractWebSocketConfig, AbstractPersistance, AbstractPubSub, AbstractPlatform, ConvertWebSocketConfig, PluvServer } from '@pluv/io';
|
|
2
2
|
import { MaybePromise, Maybe, InferIOAuthorizeUser, InferIOAuthorize, InferIOAuthorizeRequired } from '@pluv/types';
|
|
3
|
-
import
|
|
4
|
-
import
|
|
3
|
+
import { IncomingMessage as IncomingMessage$1, ServerResponse, Server } from 'node:http';
|
|
4
|
+
import { WebSocket, Server as Server$1 } from 'ws';
|
|
5
5
|
export { WebSocket } from 'ws';
|
|
6
|
+
import { IncomingMessage } from 'http';
|
|
6
7
|
|
|
7
8
|
type NodeWebSocketConfig = AbstractWebSocketConfig;
|
|
8
9
|
declare class NodeWebSocket extends AbstractWebSocket {
|
|
@@ -33,31 +34,31 @@ declare class NodePlatform extends AbstractPlatform<WebSocket, {
|
|
|
33
34
|
}
|
|
34
35
|
|
|
35
36
|
interface AuthorizeFunctionContext {
|
|
36
|
-
req:
|
|
37
|
-
res:
|
|
37
|
+
req: IncomingMessage$1;
|
|
38
|
+
res: ServerResponse;
|
|
38
39
|
roomId: string;
|
|
39
40
|
}
|
|
40
|
-
type AuthorizeFunction<
|
|
41
|
-
type CreatePluvHandlerConfig<
|
|
41
|
+
type AuthorizeFunction<TPluvServer extends PluvServer<NodePlatform, any, any, any>> = (ctx: AuthorizeFunctionContext) => MaybePromise<Maybe<InferIOAuthorizeUser<InferIOAuthorize<TPluvServer>>>>;
|
|
42
|
+
type CreatePluvHandlerConfig<TPluvServer extends PluvServer<NodePlatform, any, any, any>> = {
|
|
42
43
|
endpoint?: string;
|
|
43
|
-
io:
|
|
44
|
-
server:
|
|
45
|
-
} & (InferIOAuthorizeRequired<InferIOAuthorize<
|
|
46
|
-
authorize: AuthorizeFunction<
|
|
44
|
+
io: TPluvServer;
|
|
45
|
+
server: Server;
|
|
46
|
+
} & (InferIOAuthorizeRequired<InferIOAuthorize<TPluvServer>> extends true ? {
|
|
47
|
+
authorize: AuthorizeFunction<TPluvServer>;
|
|
47
48
|
} : {
|
|
48
49
|
authorize?: undefined;
|
|
49
50
|
});
|
|
50
|
-
type RequestHandler = (req:
|
|
51
|
+
type RequestHandler = (req: IncomingMessage$1, res: ServerResponse, next?: () => void) => void;
|
|
51
52
|
interface WebSocketHandlerResult {
|
|
52
53
|
matched: boolean;
|
|
53
54
|
}
|
|
54
|
-
type WebSocketHandler = (ws:
|
|
55
|
+
type WebSocketHandler = (ws: WebSocket, req: IncomingMessage$1) => Promise<WebSocketHandlerResult>;
|
|
55
56
|
interface CreatePluvHandlerResult {
|
|
56
|
-
createWsServer: () =>
|
|
57
|
+
createWsServer: () => Server$1;
|
|
57
58
|
handler: RequestHandler;
|
|
58
59
|
wsHandler: WebSocketHandler;
|
|
59
60
|
}
|
|
60
|
-
declare const createPluvHandler: <
|
|
61
|
+
declare const createPluvHandler: <TPluvServer extends PluvServer<NodePlatform, any, any, any>>(config: CreatePluvHandlerConfig<TPluvServer>) => CreatePluvHandlerResult;
|
|
61
62
|
|
|
62
63
|
declare const platformNode: (options?: NodePlatformOptions) => NodePlatform;
|
|
63
64
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import { AbstractWebSocket, AbstractEventMap, AbstractListener, AbstractWebSocketConfig, AbstractPersistance, AbstractPubSub, AbstractPlatform, ConvertWebSocketConfig,
|
|
1
|
+
import { AbstractWebSocket, AbstractEventMap, AbstractListener, AbstractWebSocketConfig, AbstractPersistance, AbstractPubSub, AbstractPlatform, ConvertWebSocketConfig, PluvServer } from '@pluv/io';
|
|
2
2
|
import { MaybePromise, Maybe, InferIOAuthorizeUser, InferIOAuthorize, InferIOAuthorizeRequired } from '@pluv/types';
|
|
3
|
-
import
|
|
4
|
-
import
|
|
3
|
+
import { IncomingMessage as IncomingMessage$1, ServerResponse, Server } from 'node:http';
|
|
4
|
+
import { WebSocket, Server as Server$1 } from 'ws';
|
|
5
5
|
export { WebSocket } from 'ws';
|
|
6
|
+
import { IncomingMessage } from 'http';
|
|
6
7
|
|
|
7
8
|
type NodeWebSocketConfig = AbstractWebSocketConfig;
|
|
8
9
|
declare class NodeWebSocket extends AbstractWebSocket {
|
|
@@ -33,31 +34,31 @@ declare class NodePlatform extends AbstractPlatform<WebSocket, {
|
|
|
33
34
|
}
|
|
34
35
|
|
|
35
36
|
interface AuthorizeFunctionContext {
|
|
36
|
-
req:
|
|
37
|
-
res:
|
|
37
|
+
req: IncomingMessage$1;
|
|
38
|
+
res: ServerResponse;
|
|
38
39
|
roomId: string;
|
|
39
40
|
}
|
|
40
|
-
type AuthorizeFunction<
|
|
41
|
-
type CreatePluvHandlerConfig<
|
|
41
|
+
type AuthorizeFunction<TPluvServer extends PluvServer<NodePlatform, any, any, any>> = (ctx: AuthorizeFunctionContext) => MaybePromise<Maybe<InferIOAuthorizeUser<InferIOAuthorize<TPluvServer>>>>;
|
|
42
|
+
type CreatePluvHandlerConfig<TPluvServer extends PluvServer<NodePlatform, any, any, any>> = {
|
|
42
43
|
endpoint?: string;
|
|
43
|
-
io:
|
|
44
|
-
server:
|
|
45
|
-
} & (InferIOAuthorizeRequired<InferIOAuthorize<
|
|
46
|
-
authorize: AuthorizeFunction<
|
|
44
|
+
io: TPluvServer;
|
|
45
|
+
server: Server;
|
|
46
|
+
} & (InferIOAuthorizeRequired<InferIOAuthorize<TPluvServer>> extends true ? {
|
|
47
|
+
authorize: AuthorizeFunction<TPluvServer>;
|
|
47
48
|
} : {
|
|
48
49
|
authorize?: undefined;
|
|
49
50
|
});
|
|
50
|
-
type RequestHandler = (req:
|
|
51
|
+
type RequestHandler = (req: IncomingMessage$1, res: ServerResponse, next?: () => void) => void;
|
|
51
52
|
interface WebSocketHandlerResult {
|
|
52
53
|
matched: boolean;
|
|
53
54
|
}
|
|
54
|
-
type WebSocketHandler = (ws:
|
|
55
|
+
type WebSocketHandler = (ws: WebSocket, req: IncomingMessage$1) => Promise<WebSocketHandlerResult>;
|
|
55
56
|
interface CreatePluvHandlerResult {
|
|
56
|
-
createWsServer: () =>
|
|
57
|
+
createWsServer: () => Server$1;
|
|
57
58
|
handler: RequestHandler;
|
|
58
59
|
wsHandler: WebSocketHandler;
|
|
59
60
|
}
|
|
60
|
-
declare const createPluvHandler: <
|
|
61
|
+
declare const createPluvHandler: <TPluvServer extends PluvServer<NodePlatform, any, any, any>>(config: CreatePluvHandlerConfig<TPluvServer>) => CreatePluvHandlerResult;
|
|
61
62
|
|
|
62
63
|
declare const platformNode: (options?: NodePlatformOptions) => NodePlatform;
|
|
63
64
|
|
package/dist/index.js
CHANGED
|
@@ -56,9 +56,9 @@ __export(src_exports, {
|
|
|
56
56
|
module.exports = __toCommonJS(src_exports);
|
|
57
57
|
|
|
58
58
|
// src/createPluvHandler.ts
|
|
59
|
+
var import_node_url = __toESM(require("url"));
|
|
59
60
|
var import_path_to_regexp = require("path-to-regexp");
|
|
60
|
-
var
|
|
61
|
-
var import_ws = __toESM(require("ws"));
|
|
61
|
+
var import_ws = require("ws");
|
|
62
62
|
var handle = (ws) => ({
|
|
63
63
|
invalidEndpoint: () => {
|
|
64
64
|
ws.close(1011, "Invalid WebSocket endpoint");
|
|
@@ -75,7 +75,7 @@ var handle = (ws) => ({
|
|
|
75
75
|
});
|
|
76
76
|
var createPluvHandler = (config) => {
|
|
77
77
|
const { authorize, endpoint = "/api/pluv", io, server } = config;
|
|
78
|
-
const wsServer = new import_ws.
|
|
78
|
+
const wsServer = new import_ws.Server({ server });
|
|
79
79
|
const wsHandler = (ws, req) => __async(void 0, null, function* () {
|
|
80
80
|
var _a;
|
|
81
81
|
const url = req.url;
|
|
@@ -84,20 +84,16 @@ var createPluvHandler = (config) => {
|
|
|
84
84
|
onError.invalidEndpoint();
|
|
85
85
|
return { matched: false };
|
|
86
86
|
}
|
|
87
|
-
const parsed =
|
|
87
|
+
const parsed = import_node_url.default.parse(url, true);
|
|
88
88
|
const { pathname } = parsed;
|
|
89
|
-
if (!pathname)
|
|
90
|
-
return onError.invalidEndpoint();
|
|
89
|
+
if (!pathname) return onError.invalidEndpoint();
|
|
91
90
|
const matcher = (0, import_path_to_regexp.match)(`${endpoint}/room/:roomId`);
|
|
92
91
|
const matched = matcher(pathname);
|
|
93
|
-
if (!matched)
|
|
94
|
-
return onError.invalidEndpoint();
|
|
92
|
+
if (!matched) return onError.invalidEndpoint();
|
|
95
93
|
const { roomId } = matched.params;
|
|
96
|
-
if (!roomId)
|
|
97
|
-
return onError.invalidRoom();
|
|
94
|
+
if (!roomId) return onError.invalidRoom();
|
|
98
95
|
const token = ((_a = parsed.query) == null ? void 0 : _a.token) || void 0;
|
|
99
|
-
if (Array.isArray(token))
|
|
100
|
-
return onError.invalidToken();
|
|
96
|
+
if (Array.isArray(token)) return onError.invalidToken();
|
|
101
97
|
const room = io.getRoom(roomId, { req });
|
|
102
98
|
yield room.register(ws, { req, token });
|
|
103
99
|
return { matched: true };
|
|
@@ -109,21 +105,16 @@ var createPluvHandler = (config) => {
|
|
|
109
105
|
};
|
|
110
106
|
const handler = (req, res, next) => __async(void 0, null, function* () {
|
|
111
107
|
var _a, _b, _c, _d, _e, _f;
|
|
112
|
-
if (!authorize)
|
|
113
|
-
|
|
114
|
-
if (req.method !== "GET")
|
|
115
|
-
return (_b = next == null ? void 0 : next()) != null ? _b : null;
|
|
108
|
+
if (!authorize) return (_a = next == null ? void 0 : next()) != null ? _a : null;
|
|
109
|
+
if (req.method !== "GET") return (_b = next == null ? void 0 : next()) != null ? _b : null;
|
|
116
110
|
const url = req.url;
|
|
117
|
-
if (!url)
|
|
118
|
-
|
|
119
|
-
const parsed = import_url.default.parse(url, true);
|
|
111
|
+
if (!url) return (_c = next == null ? void 0 : next()) != null ? _c : null;
|
|
112
|
+
const parsed = import_node_url.default.parse(url, true);
|
|
120
113
|
const { pathname } = parsed;
|
|
121
|
-
if (!pathname)
|
|
122
|
-
return (_d = next == null ? void 0 : next()) != null ? _d : null;
|
|
114
|
+
if (!pathname) return (_d = next == null ? void 0 : next()) != null ? _d : null;
|
|
123
115
|
const matcher = (0, import_path_to_regexp.match)(`${endpoint}/authorize`);
|
|
124
116
|
const matched = matcher(pathname);
|
|
125
|
-
if (!matched)
|
|
126
|
-
return (_e = next == null ? void 0 : next()) != null ? _e : null;
|
|
117
|
+
if (!matched) return (_e = next == null ? void 0 : next()) != null ? _e : null;
|
|
127
118
|
const roomId = ((_f = parsed.query) == null ? void 0 : _f.room) || void 0;
|
|
128
119
|
if (typeof roomId !== "string" || !roomId) {
|
|
129
120
|
res.writeHead(404, { "Content-Type": "text/plain" });
|
|
@@ -131,8 +122,7 @@ var createPluvHandler = (config) => {
|
|
|
131
122
|
}
|
|
132
123
|
try {
|
|
133
124
|
const user = yield authorize({ res, req, roomId });
|
|
134
|
-
if (!user)
|
|
135
|
-
throw new Error();
|
|
125
|
+
if (!user) throw new Error();
|
|
136
126
|
const token = yield io.createToken({
|
|
137
127
|
req,
|
|
138
128
|
room: roomId,
|
|
@@ -203,11 +193,8 @@ var NodeWebSocket = class extends import_io.AbstractWebSocket {
|
|
|
203
193
|
}
|
|
204
194
|
}
|
|
205
195
|
close(code, reason) {
|
|
206
|
-
const canClose = [this.CONNECTING, this.OPEN].some(
|
|
207
|
-
|
|
208
|
-
);
|
|
209
|
-
if (!canClose)
|
|
210
|
-
return;
|
|
196
|
+
const canClose = [this.CONNECTING, this.OPEN].some((readyState) => readyState === this.readyState);
|
|
197
|
+
if (!canClose) return;
|
|
211
198
|
this.webSocket.close(code, reason);
|
|
212
199
|
}
|
|
213
200
|
initialize() {
|
|
@@ -216,11 +203,9 @@ var NodeWebSocket = class extends import_io.AbstractWebSocket {
|
|
|
216
203
|
});
|
|
217
204
|
}
|
|
218
205
|
send(message) {
|
|
219
|
-
if (this.readyState !== this.OPEN)
|
|
220
|
-
return;
|
|
206
|
+
if (this.readyState !== this.OPEN) return;
|
|
221
207
|
this.webSocket.send(message, (error) => {
|
|
222
|
-
if (error)
|
|
223
|
-
throw error;
|
|
208
|
+
if (error) throw error;
|
|
224
209
|
});
|
|
225
210
|
}
|
|
226
211
|
terminate() {
|
|
@@ -238,8 +223,7 @@ var NodePlatform = class extends import_io2.AbstractPlatform {
|
|
|
238
223
|
return new NodeWebSocket(webSocket, config);
|
|
239
224
|
}
|
|
240
225
|
parseData(data) {
|
|
241
|
-
if (typeof data === "string")
|
|
242
|
-
return JSON.parse(data);
|
|
226
|
+
if (typeof data === "string") return JSON.parse(data);
|
|
243
227
|
const decoder = new import_util.TextDecoder("utf8");
|
|
244
228
|
return JSON.parse(decoder.decode(data));
|
|
245
229
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -20,9 +20,9 @@ var __async = (__this, __arguments, generator) => {
|
|
|
20
20
|
};
|
|
21
21
|
|
|
22
22
|
// src/createPluvHandler.ts
|
|
23
|
-
import { match } from "path-to-regexp";
|
|
24
23
|
import Url from "url";
|
|
25
|
-
import
|
|
24
|
+
import { match } from "path-to-regexp";
|
|
25
|
+
import { Server as WsServer } from "ws";
|
|
26
26
|
var handle = (ws) => ({
|
|
27
27
|
invalidEndpoint: () => {
|
|
28
28
|
ws.close(1011, "Invalid WebSocket endpoint");
|
|
@@ -39,7 +39,7 @@ var handle = (ws) => ({
|
|
|
39
39
|
});
|
|
40
40
|
var createPluvHandler = (config) => {
|
|
41
41
|
const { authorize, endpoint = "/api/pluv", io, server } = config;
|
|
42
|
-
const wsServer = new
|
|
42
|
+
const wsServer = new WsServer({ server });
|
|
43
43
|
const wsHandler = (ws, req) => __async(void 0, null, function* () {
|
|
44
44
|
var _a;
|
|
45
45
|
const url = req.url;
|
|
@@ -50,18 +50,14 @@ var createPluvHandler = (config) => {
|
|
|
50
50
|
}
|
|
51
51
|
const parsed = Url.parse(url, true);
|
|
52
52
|
const { pathname } = parsed;
|
|
53
|
-
if (!pathname)
|
|
54
|
-
return onError.invalidEndpoint();
|
|
53
|
+
if (!pathname) return onError.invalidEndpoint();
|
|
55
54
|
const matcher = match(`${endpoint}/room/:roomId`);
|
|
56
55
|
const matched = matcher(pathname);
|
|
57
|
-
if (!matched)
|
|
58
|
-
return onError.invalidEndpoint();
|
|
56
|
+
if (!matched) return onError.invalidEndpoint();
|
|
59
57
|
const { roomId } = matched.params;
|
|
60
|
-
if (!roomId)
|
|
61
|
-
return onError.invalidRoom();
|
|
58
|
+
if (!roomId) return onError.invalidRoom();
|
|
62
59
|
const token = ((_a = parsed.query) == null ? void 0 : _a.token) || void 0;
|
|
63
|
-
if (Array.isArray(token))
|
|
64
|
-
return onError.invalidToken();
|
|
60
|
+
if (Array.isArray(token)) return onError.invalidToken();
|
|
65
61
|
const room = io.getRoom(roomId, { req });
|
|
66
62
|
yield room.register(ws, { req, token });
|
|
67
63
|
return { matched: true };
|
|
@@ -73,21 +69,16 @@ var createPluvHandler = (config) => {
|
|
|
73
69
|
};
|
|
74
70
|
const handler = (req, res, next) => __async(void 0, null, function* () {
|
|
75
71
|
var _a, _b, _c, _d, _e, _f;
|
|
76
|
-
if (!authorize)
|
|
77
|
-
|
|
78
|
-
if (req.method !== "GET")
|
|
79
|
-
return (_b = next == null ? void 0 : next()) != null ? _b : null;
|
|
72
|
+
if (!authorize) return (_a = next == null ? void 0 : next()) != null ? _a : null;
|
|
73
|
+
if (req.method !== "GET") return (_b = next == null ? void 0 : next()) != null ? _b : null;
|
|
80
74
|
const url = req.url;
|
|
81
|
-
if (!url)
|
|
82
|
-
return (_c = next == null ? void 0 : next()) != null ? _c : null;
|
|
75
|
+
if (!url) return (_c = next == null ? void 0 : next()) != null ? _c : null;
|
|
83
76
|
const parsed = Url.parse(url, true);
|
|
84
77
|
const { pathname } = parsed;
|
|
85
|
-
if (!pathname)
|
|
86
|
-
return (_d = next == null ? void 0 : next()) != null ? _d : null;
|
|
78
|
+
if (!pathname) return (_d = next == null ? void 0 : next()) != null ? _d : null;
|
|
87
79
|
const matcher = match(`${endpoint}/authorize`);
|
|
88
80
|
const matched = matcher(pathname);
|
|
89
|
-
if (!matched)
|
|
90
|
-
return (_e = next == null ? void 0 : next()) != null ? _e : null;
|
|
81
|
+
if (!matched) return (_e = next == null ? void 0 : next()) != null ? _e : null;
|
|
91
82
|
const roomId = ((_f = parsed.query) == null ? void 0 : _f.room) || void 0;
|
|
92
83
|
if (typeof roomId !== "string" || !roomId) {
|
|
93
84
|
res.writeHead(404, { "Content-Type": "text/plain" });
|
|
@@ -95,8 +86,7 @@ var createPluvHandler = (config) => {
|
|
|
95
86
|
}
|
|
96
87
|
try {
|
|
97
88
|
const user = yield authorize({ res, req, roomId });
|
|
98
|
-
if (!user)
|
|
99
|
-
throw new Error();
|
|
89
|
+
if (!user) throw new Error();
|
|
100
90
|
const token = yield io.createToken({
|
|
101
91
|
req,
|
|
102
92
|
room: roomId,
|
|
@@ -167,11 +157,8 @@ var NodeWebSocket = class extends AbstractWebSocket {
|
|
|
167
157
|
}
|
|
168
158
|
}
|
|
169
159
|
close(code, reason) {
|
|
170
|
-
const canClose = [this.CONNECTING, this.OPEN].some(
|
|
171
|
-
|
|
172
|
-
);
|
|
173
|
-
if (!canClose)
|
|
174
|
-
return;
|
|
160
|
+
const canClose = [this.CONNECTING, this.OPEN].some((readyState) => readyState === this.readyState);
|
|
161
|
+
if (!canClose) return;
|
|
175
162
|
this.webSocket.close(code, reason);
|
|
176
163
|
}
|
|
177
164
|
initialize() {
|
|
@@ -180,11 +167,9 @@ var NodeWebSocket = class extends AbstractWebSocket {
|
|
|
180
167
|
});
|
|
181
168
|
}
|
|
182
169
|
send(message) {
|
|
183
|
-
if (this.readyState !== this.OPEN)
|
|
184
|
-
return;
|
|
170
|
+
if (this.readyState !== this.OPEN) return;
|
|
185
171
|
this.webSocket.send(message, (error) => {
|
|
186
|
-
if (error)
|
|
187
|
-
throw error;
|
|
172
|
+
if (error) throw error;
|
|
188
173
|
});
|
|
189
174
|
}
|
|
190
175
|
terminate() {
|
|
@@ -202,8 +187,7 @@ var NodePlatform = class extends AbstractPlatform {
|
|
|
202
187
|
return new NodeWebSocket(webSocket, config);
|
|
203
188
|
}
|
|
204
189
|
parseData(data) {
|
|
205
|
-
if (typeof data === "string")
|
|
206
|
-
return JSON.parse(data);
|
|
190
|
+
if (typeof data === "string") return JSON.parse(data);
|
|
207
191
|
const decoder = new TextDecoder("utf8");
|
|
208
192
|
return JSON.parse(decoder.decode(data));
|
|
209
193
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pluv/platform-node",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.19.0",
|
|
4
4
|
"description": "@pluv/io adapter for node.js",
|
|
5
5
|
"author": "leedavidcs",
|
|
6
6
|
"license": "MIT",
|
|
@@ -17,19 +17,19 @@
|
|
|
17
17
|
"access": "public"
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@types/node": "^20.
|
|
20
|
+
"@types/node": "^20.14.9",
|
|
21
21
|
"@types/ws": "^8.5.10",
|
|
22
|
-
"path-to-regexp": "^
|
|
23
|
-
"@pluv/io": "^0.
|
|
24
|
-
"@pluv/types": "^0.
|
|
22
|
+
"path-to-regexp": "^7.0.0",
|
|
23
|
+
"@pluv/io": "^0.19.0",
|
|
24
|
+
"@pluv/types": "^0.19.0"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
27
|
"eslint": "^8.57.0",
|
|
28
|
-
"tsup": "^8.0
|
|
28
|
+
"tsup": "^8.1.0",
|
|
29
29
|
"typescript": "^5.4.5",
|
|
30
|
-
"ws": "^8.
|
|
31
|
-
"@pluv/tsconfig": "^0.
|
|
32
|
-
"eslint-config-pluv": "^0.
|
|
30
|
+
"ws": "^8.17.0",
|
|
31
|
+
"@pluv/tsconfig": "^0.19.0",
|
|
32
|
+
"eslint-config-pluv": "^0.19.0"
|
|
33
33
|
},
|
|
34
34
|
"peerDependencies": {
|
|
35
35
|
"ws": "^8.0.0"
|
package/src/NodePlatform.ts
CHANGED
|
@@ -13,20 +13,14 @@ export type NodePlatformOptions =
|
|
|
13
13
|
pubSub: AbstractPubSub;
|
|
14
14
|
};
|
|
15
15
|
|
|
16
|
-
export class NodePlatform extends AbstractPlatform<
|
|
17
|
-
WebSocket,
|
|
18
|
-
{ req: IncomingMessage }
|
|
19
|
-
> {
|
|
16
|
+
export class NodePlatform extends AbstractPlatform<WebSocket, { req: IncomingMessage }> {
|
|
20
17
|
constructor(options: NodePlatformOptions = {}) {
|
|
21
18
|
const { persistance, pubSub } = options;
|
|
22
19
|
|
|
23
20
|
super(persistance && pubSub ? { persistance, pubSub } : {});
|
|
24
21
|
}
|
|
25
22
|
|
|
26
|
-
public convertWebSocket(
|
|
27
|
-
webSocket: WebSocket,
|
|
28
|
-
config: ConvertWebSocketConfig,
|
|
29
|
-
): NodeWebSocket {
|
|
23
|
+
public convertWebSocket(webSocket: WebSocket, config: ConvertWebSocketConfig): NodeWebSocket {
|
|
30
24
|
return new NodeWebSocket(webSocket, config);
|
|
31
25
|
}
|
|
32
26
|
|
package/src/NodeWebSocket.ts
CHANGED
|
@@ -32,10 +32,7 @@ export class NodeWebSocket extends AbstractWebSocket {
|
|
|
32
32
|
this.webSocket = webSocket;
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
-
public addEventListener<TType extends keyof AbstractEventMap>(
|
|
36
|
-
type: TType,
|
|
37
|
-
handler: AbstractListener<TType>,
|
|
38
|
-
): void {
|
|
35
|
+
public addEventListener<TType extends keyof AbstractEventMap>(type: TType, handler: AbstractListener<TType>): void {
|
|
39
36
|
switch (type) {
|
|
40
37
|
case "close":
|
|
41
38
|
this.webSocket.on("close", async (code, reason) => {
|
|
@@ -74,9 +71,7 @@ export class NodeWebSocket extends AbstractWebSocket {
|
|
|
74
71
|
}
|
|
75
72
|
|
|
76
73
|
public close(code?: number | undefined, reason?: string | undefined): void {
|
|
77
|
-
const canClose = [this.CONNECTING, this.OPEN].some(
|
|
78
|
-
(readyState) => readyState === this.readyState,
|
|
79
|
-
);
|
|
74
|
+
const canClose = [this.CONNECTING, this.OPEN].some((readyState) => readyState === this.readyState);
|
|
80
75
|
|
|
81
76
|
if (!canClose) return;
|
|
82
77
|
|
package/src/createPluvHandler.ts
CHANGED
|
@@ -1,35 +1,36 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import {
|
|
1
|
+
import type { PluvServer } from "@pluv/io";
|
|
2
|
+
import type {
|
|
3
3
|
InferIOAuthorize,
|
|
4
4
|
InferIOAuthorizeRequired,
|
|
5
5
|
InferIOAuthorizeUser,
|
|
6
6
|
Maybe,
|
|
7
7
|
MaybePromise,
|
|
8
8
|
} from "@pluv/types";
|
|
9
|
-
import
|
|
9
|
+
import type { Server as HttpServer, IncomingMessage, ServerResponse } from "node:http";
|
|
10
|
+
import Url from "node:url";
|
|
10
11
|
import { match } from "path-to-regexp";
|
|
11
|
-
import
|
|
12
|
-
import
|
|
12
|
+
import type { WebSocket } from "ws";
|
|
13
|
+
import { Server as WsServer } from "ws";
|
|
13
14
|
import { NodePlatform } from "./NodePlatform";
|
|
14
15
|
|
|
15
16
|
export interface AuthorizeFunctionContext {
|
|
16
|
-
req:
|
|
17
|
-
res:
|
|
17
|
+
req: IncomingMessage;
|
|
18
|
+
res: ServerResponse;
|
|
18
19
|
roomId: string;
|
|
19
20
|
}
|
|
20
|
-
export type AuthorizeFunction<
|
|
21
|
+
export type AuthorizeFunction<TPluvServer extends PluvServer<NodePlatform, any, any, any>> = (
|
|
21
22
|
ctx: AuthorizeFunctionContext,
|
|
22
|
-
) => MaybePromise<Maybe<InferIOAuthorizeUser<InferIOAuthorize<
|
|
23
|
+
) => MaybePromise<Maybe<InferIOAuthorizeUser<InferIOAuthorize<TPluvServer>>>>;
|
|
23
24
|
|
|
24
|
-
export type CreatePluvHandlerConfig<
|
|
25
|
+
export type CreatePluvHandlerConfig<TPluvServer extends PluvServer<NodePlatform, any, any, any>> = {
|
|
25
26
|
endpoint?: string;
|
|
26
|
-
io:
|
|
27
|
-
server:
|
|
28
|
-
} & (InferIOAuthorizeRequired<InferIOAuthorize<
|
|
29
|
-
? { authorize: AuthorizeFunction<
|
|
27
|
+
io: TPluvServer;
|
|
28
|
+
server: HttpServer;
|
|
29
|
+
} & (InferIOAuthorizeRequired<InferIOAuthorize<TPluvServer>> extends true
|
|
30
|
+
? { authorize: AuthorizeFunction<TPluvServer> }
|
|
30
31
|
: { authorize?: undefined });
|
|
31
32
|
|
|
32
|
-
const handle = (ws:
|
|
33
|
+
const handle = (ws: WebSocket) => ({
|
|
33
34
|
invalidEndpoint: () => {
|
|
34
35
|
ws.close(1011, "Invalid WebSocket endpoint");
|
|
35
36
|
|
|
@@ -47,40 +48,28 @@ const handle = (ws: WS.WebSocket) => ({
|
|
|
47
48
|
},
|
|
48
49
|
});
|
|
49
50
|
|
|
50
|
-
export type RequestHandler = (
|
|
51
|
-
req: Http.IncomingMessage,
|
|
52
|
-
res: Http.ServerResponse,
|
|
53
|
-
next?: () => void,
|
|
54
|
-
) => void;
|
|
51
|
+
export type RequestHandler = (req: IncomingMessage, res: ServerResponse, next?: () => void) => void;
|
|
55
52
|
|
|
56
53
|
export interface WebSocketHandlerResult {
|
|
57
54
|
matched: boolean;
|
|
58
55
|
}
|
|
59
56
|
|
|
60
|
-
export type WebSocketHandler = (
|
|
61
|
-
ws: WS.WebSocket,
|
|
62
|
-
req: Http.IncomingMessage,
|
|
63
|
-
) => Promise<WebSocketHandlerResult>;
|
|
57
|
+
export type WebSocketHandler = (ws: WebSocket, req: IncomingMessage) => Promise<WebSocketHandlerResult>;
|
|
64
58
|
|
|
65
59
|
export interface CreatePluvHandlerResult {
|
|
66
|
-
createWsServer: () =>
|
|
60
|
+
createWsServer: () => WsServer;
|
|
67
61
|
handler: RequestHandler;
|
|
68
62
|
wsHandler: WebSocketHandler;
|
|
69
63
|
}
|
|
70
64
|
|
|
71
|
-
export const createPluvHandler = <
|
|
72
|
-
|
|
73
|
-
>(
|
|
74
|
-
config: CreatePluvHandlerConfig<TPluv>,
|
|
65
|
+
export const createPluvHandler = <TPluvServer extends PluvServer<NodePlatform, any, any, any>>(
|
|
66
|
+
config: CreatePluvHandlerConfig<TPluvServer>,
|
|
75
67
|
): CreatePluvHandlerResult => {
|
|
76
68
|
const { authorize, endpoint = "/api/pluv", io, server } = config;
|
|
77
69
|
|
|
78
|
-
const wsServer = new
|
|
70
|
+
const wsServer = new WsServer({ server });
|
|
79
71
|
|
|
80
|
-
const wsHandler = async (
|
|
81
|
-
ws: WS.WebSocket,
|
|
82
|
-
req: Http.IncomingMessage,
|
|
83
|
-
): Promise<WebSocketHandlerResult> => {
|
|
72
|
+
const wsHandler = async (ws: WebSocket, req: IncomingMessage): Promise<WebSocketHandlerResult> => {
|
|
84
73
|
const url = req.url;
|
|
85
74
|
const onError = handle(ws);
|
|
86
75
|
|
|
@@ -122,10 +111,10 @@ export const createPluvHandler = <
|
|
|
122
111
|
};
|
|
123
112
|
|
|
124
113
|
const handler = async (
|
|
125
|
-
req:
|
|
126
|
-
res:
|
|
114
|
+
req: IncomingMessage,
|
|
115
|
+
res: ServerResponse,
|
|
127
116
|
next?: () => void,
|
|
128
|
-
): Promise<
|
|
117
|
+
): Promise<ServerResponse | null> => {
|
|
129
118
|
if (!authorize) return next?.() ?? null;
|
|
130
119
|
if (req.method !== "GET") return next?.() ?? null;
|
|
131
120
|
|
package/src/index.ts
CHANGED
|
@@ -1,8 +1,4 @@
|
|
|
1
|
-
export type {
|
|
2
|
-
AuthorizeFunction,
|
|
3
|
-
AuthorizeFunctionContext,
|
|
4
|
-
CreatePluvHandlerConfig,
|
|
5
|
-
} from "./createPluvHandler";
|
|
1
|
+
export type { AuthorizeFunction, AuthorizeFunctionContext, CreatePluvHandlerConfig } from "./createPluvHandler";
|
|
6
2
|
export { createPluvHandler } from "./createPluvHandler";
|
|
7
3
|
export type { WebSocket } from "ws";
|
|
8
4
|
export type { NodePlatformOptions } from "./NodePlatform";
|
package/src/platformNode.ts
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import type { NodePlatformOptions } from "./NodePlatform";
|
|
2
2
|
import { NodePlatform } from "./NodePlatform";
|
|
3
3
|
|
|
4
|
-
export const platformNode = (
|
|
5
|
-
options: NodePlatformOptions = {},
|
|
6
|
-
): NodePlatform => {
|
|
4
|
+
export const platformNode = (options: NodePlatformOptions = {}): NodePlatform => {
|
|
7
5
|
return new NodePlatform(options);
|
|
8
6
|
};
|