@pluv/platform-node 4.0.2 → 5.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 -11
- package/CHANGELOG.md +38 -0
- package/dist/index.d.mts +20 -6
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +50 -6
- package/dist/index.mjs.map +1 -1
- package/package.json +9 -9
- package/src/NodePlatform.ts +9 -4
- package/src/index.ts +3 -0
- package/src/platformNode.ts +13 -4
- package/src/types.ts +5 -0
- package/src/utils/index.ts +2 -0
- package/src/utils/toRequest.ts +69 -0
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,18 +1,15 @@
|
|
|
1
1
|
$ tsdown
|
|
2
|
-
[34mℹ[39m [34mtsdown v0.22.
|
|
2
|
+
[34mℹ[39m [34mtsdown v0.22.3[39m powered by [38;2;255;126;23mrolldown v1.1.2[39m
|
|
3
3
|
[34mℹ[39m config file: [4m/home/runner/work/pluv/pluv/packages/platform-node/tsdown.config.ts[24m
|
|
4
4
|
[34mℹ[39m entry: [34msrc/index.ts[39m
|
|
5
5
|
[34mℹ[39m target: [34mesnext[39m
|
|
6
6
|
[34mℹ[39m tsconfig: [34mtsconfig.json[39m
|
|
7
7
|
[34mℹ[39m Build start
|
|
8
|
-
[34mℹ[39m [2mdist/[22m[1mindex.mjs[22m [2m
|
|
9
|
-
[34mℹ[39m [2mdist/[22mindex.mjs.map [
|
|
10
|
-
[34mℹ[39m [2mdist/[22mindex.d.mts.map [2m 2.
|
|
11
|
-
[34mℹ[39m [2mdist/[22m[32m[1mindex.d.mts[22m[39m [2m 5.
|
|
12
|
-
[34mℹ[39m 4 files, total:
|
|
13
|
-
[33m[PLUGIN_TIMINGS] [0mYour build spent significant time in
|
|
14
|
-
- rolldown-plugin-dts:generate (68%)
|
|
15
|
-
- tsdown:deps (21%)
|
|
16
|
-
See https://rolldown.rs/options/checks#plugintimings for more details.
|
|
8
|
+
[34mℹ[39m [2mdist/[22m[1mindex.mjs[22m [2m 5.68 kB[22m [2m│ gzip: 1.94 kB[22m
|
|
9
|
+
[34mℹ[39m [2mdist/[22mindex.mjs.map [2m17.59 kB[22m [2m│ gzip: 4.72 kB[22m
|
|
10
|
+
[34mℹ[39m [2mdist/[22mindex.d.mts.map [2m 2.99 kB[22m [2m│ gzip: 1.03 kB[22m
|
|
11
|
+
[34mℹ[39m [2mdist/[22m[32m[1mindex.d.mts[22m[39m [2m 5.91 kB[22m [2m│ gzip: 1.59 kB[22m
|
|
12
|
+
[34mℹ[39m 4 files, total: 32.18 kB
|
|
13
|
+
[33m[PLUGIN_TIMINGS] [0mYour build spent significant time in plugin `rolldown-plugin-dts:generate`. See https://rolldown.rs/reference/InputOptions.checks#plugintimings for more details.
|
|
17
14
|
|
|
18
|
-
[32m✔[39m Build complete in [
|
|
15
|
+
[32m✔[39m Build complete in [32m3295ms[39m
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,43 @@
|
|
|
1
1
|
# @pluv/platform-node
|
|
2
2
|
|
|
3
|
+
## 5.0.0
|
|
4
|
+
|
|
5
|
+
### Major Changes
|
|
6
|
+
|
|
7
|
+
- 8f9efe9: **BREAKING**: `@pluv/platform-node` init context now uses `request` instead of `req`, aligned with `@pluv/platform-cloudflare`.
|
|
8
|
+
|
|
9
|
+
- `room.register` and `io.createToken` now take `{ request: Request | IncomingMessage }` instead of `{ req: IncomingMessage }`.
|
|
10
|
+
- Function-form `authorize` receives `{ request: Request }` — `IncomingMessage` values are normalized automatically.
|
|
11
|
+
- Optional `platformNode({ origin })` sets the base URL when converting `IncomingMessage` to `Request`.
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
// Before
|
|
15
|
+
await room.register(ws, { req, token });
|
|
16
|
+
await io.createToken({ req, room, user });
|
|
17
|
+
authorize: ({ req }) => ({ ... });
|
|
18
|
+
|
|
19
|
+
// After
|
|
20
|
+
await room.register(ws, { request: req, token });
|
|
21
|
+
await io.createToken({ request: c.req.raw, room, user });
|
|
22
|
+
authorize: ({ request }) => ({ ... });
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
See the v5 migration guide for details.
|
|
26
|
+
|
|
27
|
+
### Patch Changes
|
|
28
|
+
|
|
29
|
+
- @pluv/crdt@5.0.0
|
|
30
|
+
- @pluv/io@5.0.0
|
|
31
|
+
- @pluv/types@5.0.0
|
|
32
|
+
|
|
33
|
+
## 4.1.0
|
|
34
|
+
|
|
35
|
+
### Patch Changes
|
|
36
|
+
|
|
37
|
+
- @pluv/crdt@4.1.0
|
|
38
|
+
- @pluv/io@4.1.0
|
|
39
|
+
- @pluv/types@4.1.0
|
|
40
|
+
|
|
3
41
|
## 4.0.2
|
|
4
42
|
|
|
5
43
|
### Patch Changes
|
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AbstractEventMap, AbstractListener, AbstractPersistence, AbstractPlatform, AbstractPlatformConfig, AbstractPubSub, AbstractWebSocket, AbstractWebSocketConfig, ConvertWebSocketConfig, CreateIOParams,
|
|
1
|
+
import { AbstractEventMap, AbstractListener, AbstractPersistence, AbstractPlatform, AbstractPlatformConfig, AbstractPubSub, AbstractWebSocket, AbstractWebSocketConfig, ConvertWebSocketConfig, CreateIOParams, PluvContext, PluvIOAuthorize, WebSocketRegistrationMode, WebSocketSerializedState, WebSocketSession } from "@pluv/io";
|
|
2
2
|
import { WebSocket, WebSocket as WebSocket$1 } from "ws";
|
|
3
3
|
import { BaseUser, IOAuthorize, Id, InferIOAuthorizeUser, Json, JsonObject } from "@pluv/types";
|
|
4
4
|
import { IncomingMessage } from "node:http";
|
|
@@ -7,6 +7,12 @@ import { CrdtLibraryType, NoopCrdtDocFactory } from "@pluv/crdt";
|
|
|
7
7
|
//#region src/utils/identity.d.ts
|
|
8
8
|
declare const identity: <T extends unknown>(x: T) => T;
|
|
9
9
|
//#endregion
|
|
10
|
+
//#region src/utils/toRequest.d.ts
|
|
11
|
+
type ToRequestOptions = {
|
|
12
|
+
origin?: string;
|
|
13
|
+
};
|
|
14
|
+
declare const toRequest: (request: Request | IncomingMessage, options?: ToRequestOptions) => Request;
|
|
15
|
+
//#endregion
|
|
10
16
|
//#region src/infer.d.ts
|
|
11
17
|
type InferCallback<TMeta extends Record<string, Json> = {}> = (i: typeof identity) => {
|
|
12
18
|
meta?: (io: TMeta) => TMeta;
|
|
@@ -33,6 +39,14 @@ declare class NodeWebSocket<TAuthorize extends IOAuthorize<any, any> | null = nu
|
|
|
33
39
|
terminate(): void;
|
|
34
40
|
}
|
|
35
41
|
//#endregion
|
|
42
|
+
//#region src/types.d.ts
|
|
43
|
+
type NodeRegisterInput = {
|
|
44
|
+
request: Request | IncomingMessage;
|
|
45
|
+
};
|
|
46
|
+
type NodeAuthorizeContext = {
|
|
47
|
+
request: Request;
|
|
48
|
+
};
|
|
49
|
+
//#endregion
|
|
36
50
|
//#region src/NodePlatform.d.ts
|
|
37
51
|
type NodePlatformRoomContext<TMeta extends Record<string, Json>> = keyof TMeta extends never ? {
|
|
38
52
|
meta?: undefined;
|
|
@@ -41,6 +55,7 @@ type NodePlatformRoomContext<TMeta extends Record<string, Json>> = keyof TMeta e
|
|
|
41
55
|
};
|
|
42
56
|
type NodePlatformConfig<TMeta extends Record<string, Json>> = {
|
|
43
57
|
mode?: WebSocketRegistrationMode;
|
|
58
|
+
origin?: string;
|
|
44
59
|
} & ({
|
|
45
60
|
persistence?: undefined;
|
|
46
61
|
pubSub?: undefined;
|
|
@@ -50,9 +65,7 @@ type NodePlatformConfig<TMeta extends Record<string, Json>> = {
|
|
|
50
65
|
}) & {
|
|
51
66
|
roomContext?: NodePlatformRoomContext<TMeta>;
|
|
52
67
|
};
|
|
53
|
-
declare class NodePlatform<TAuthorize extends IOAuthorize<any, any> | null = null, TMeta extends Record<string, Json> = {}> extends AbstractPlatform<NodeWebSocket<TAuthorize>, {
|
|
54
|
-
req: IncomingMessage;
|
|
55
|
-
}, NodePlatformRoomContext<TMeta>, {
|
|
68
|
+
declare class NodePlatform<TAuthorize extends IOAuthorize<any, any> | null = null, TMeta extends Record<string, Json> = {}> extends AbstractPlatform<NodeWebSocket<TAuthorize>, NodeRegisterInput, NodePlatformRoomContext<TMeta>, {
|
|
56
69
|
authorize: {
|
|
57
70
|
secret: true;
|
|
58
71
|
};
|
|
@@ -88,6 +101,7 @@ declare class NodePlatform<TAuthorize extends IOAuthorize<any, any> | null = nul
|
|
|
88
101
|
router: true;
|
|
89
102
|
};
|
|
90
103
|
readonly _name = "platformNode";
|
|
104
|
+
readonly origin: string | undefined;
|
|
91
105
|
constructor(config?: NodePlatformConfig<TMeta>);
|
|
92
106
|
acceptWebSocket(webSocket: NodeWebSocket<TAuthorize>): Promise<void>;
|
|
93
107
|
convertWebSocket(webSocket: WebSocket$1, config: ConvertWebSocketConfig): NodeWebSocket<TAuthorize>;
|
|
@@ -103,11 +117,11 @@ declare class NodePlatform<TAuthorize extends IOAuthorize<any, any> | null = nul
|
|
|
103
117
|
//#endregion
|
|
104
118
|
//#region src/platformNode.d.ts
|
|
105
119
|
type PlatformNodeCreateIOParams<TMeta extends Record<string, Json> = {}, TContext extends Record<string, any> = {}, TUser extends BaseUser | null = null, TCrdt extends CrdtLibraryType<any> = CrdtLibraryType<NoopCrdtDocFactory>> = Id<NodePlatformConfig<TMeta> & Omit<CreateIOParams<NodePlatform<IOAuthorize<TUser, TContext>, TMeta>, TContext, TUser, TCrdt>, "authorize" | "context" | "platform"> & {
|
|
106
|
-
authorize?: PluvIOAuthorize<NodePlatform<IOAuthorize<TUser, TContext>, TMeta>, TUser,
|
|
120
|
+
authorize?: PluvIOAuthorize<NodePlatform<IOAuthorize<TUser, TContext>, TMeta>, TUser, NodeAuthorizeContext>;
|
|
107
121
|
context?: PluvContext<NodePlatform<IOAuthorize<TUser, any>, TMeta>, TContext>;
|
|
108
122
|
types?: InferCallback<TMeta>;
|
|
109
123
|
}>;
|
|
110
124
|
declare const platformNode: <TMeta extends Record<string, Json> = {}, TContext extends Record<string, any> = {}, TUser extends BaseUser | null = null, TCrdt extends CrdtLibraryType<any> = CrdtLibraryType<NoopCrdtDocFactory>>(config?: PlatformNodeCreateIOParams<TMeta, TContext, TUser, TCrdt>) => CreateIOParams<NodePlatform<IOAuthorize<TUser, TContext>, TMeta>, TContext, TUser, TCrdt>;
|
|
111
125
|
//#endregion
|
|
112
|
-
export { type InferCallback, type NodePlatform, type NodePlatformConfig, type PlatformNodeCreateIOParams, type WebSocket, infer, platformNode };
|
|
126
|
+
export { type InferCallback, type NodeAuthorizeContext, type NodePlatform, type NodePlatformConfig, type NodeRegisterInput, type PlatformNodeCreateIOParams, type ToRequestOptions, type WebSocket, infer, platformNode, toRequest };
|
|
113
127
|
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/utils/identity.ts","../src/infer.ts","../src/NodeWebSocket.ts","../src/NodePlatform.ts","../src/platformNode.ts"],"mappings":";;;;;;;cAAa,QAAA,sBAA+B,CAAA,EAAG,CAAA,KAAI,CAAM;;;
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/utils/identity.ts","../src/utils/toRequest.ts","../src/infer.ts","../src/NodeWebSocket.ts","../src/types.ts","../src/NodePlatform.ts","../src/platformNode.ts"],"mappings":";;;;;;;cAAa,QAAA,sBAA+B,CAAA,EAAG,CAAA,KAAI,CAAM;;;KCwC7C,gBAAA;EACR,MAAM;AAAA;AAAA,cAGG,SAAA,GACT,OAAA,EAAS,OAAA,GAAU,eAAA,EACnB,OAAA,GAAS,gBAAA,KACV,OAAA;;;KC5CS,aAAA,eAA4B,MAAA,SAAe,IAAA,WAAe,CAAA,SAAU,QAAA;EAC5E,IAAA,IAAQ,EAAA,EAAI,KAAA,KAAU,KAAA;AAAA;AAAA,cAGb,KAAA,iBAAuB,MAAA,SAAe,IAAA,QAAY,QAAA,EAAU,aAAA,CAAc,KA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AA,GAAU,WAAA,CAAY,YAAA,CAAa,WAAA,CAAY,KAAA,QAAa,KAAA,GAAQ,QAAA;EACpE,KAAA,GAAQ,aAAA,CAAc,KAAA;AAAA;AAAA,cAIrB,YAAA,iBACK,MAAA,SAAe,IAAA,yBACZ,MAAA,kCACH,QAAA,8BACA,eAAA,QAAuB,eAAA,CAAgB,kBAAA,GAErD,MAAA,GAAQ,0BAAA,CAA2B,KAAA,EAAO,QAAA,EAAU,KAAA,EAAO,KAAA,MAC5D,cAAA,CAAe,YAAA,CAAa,WAAA,CAAY,KAAA,EAAO,QAAA,GAAW,KAAA,GAAQ,QAAA,EAAU,KAAA,EAAO,KAAA"}
|
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { AbstractPlatform, AbstractWebSocket } from "@pluv/io";
|
|
2
2
|
import crypto from "node:crypto";
|
|
3
3
|
import { TextDecoder } from "node:util";
|
|
4
|
+
import { Readable } from "node:stream";
|
|
4
5
|
//#region src/infer.ts
|
|
5
6
|
const infer = (callback) => callback;
|
|
6
7
|
//#endregion
|
|
@@ -102,8 +103,9 @@ var NodePlatform = class NodePlatform extends AbstractPlatform {
|
|
|
102
103
|
id = crypto.randomUUID();
|
|
103
104
|
_config;
|
|
104
105
|
_name = "platformNode";
|
|
106
|
+
origin;
|
|
105
107
|
constructor(config = {}) {
|
|
106
|
-
const { roomContext, mode = "attached", persistence, pubSub } = config;
|
|
108
|
+
const { origin, roomContext, mode = "attached", persistence, pubSub } = config;
|
|
107
109
|
super({
|
|
108
110
|
roomContext,
|
|
109
111
|
...persistence && pubSub ? {
|
|
@@ -111,6 +113,7 @@ var NodePlatform = class NodePlatform extends AbstractPlatform {
|
|
|
111
113
|
pubSub
|
|
112
114
|
} : {}
|
|
113
115
|
});
|
|
116
|
+
this.origin = origin;
|
|
114
117
|
this._config = {
|
|
115
118
|
authorize: { secret: true },
|
|
116
119
|
handleMode: "io",
|
|
@@ -152,10 +155,11 @@ var NodePlatform = class NodePlatform extends AbstractPlatform {
|
|
|
152
155
|
}
|
|
153
156
|
initialize(config) {
|
|
154
157
|
return new NodePlatform({
|
|
155
|
-
roomContext: config.roomContext,
|
|
156
158
|
mode: this._config.registrationMode,
|
|
159
|
+
origin: this.origin,
|
|
157
160
|
persistence: this.persistence.initialize(config.roomContext),
|
|
158
|
-
pubSub: this.pubSub
|
|
161
|
+
pubSub: this.pubSub,
|
|
162
|
+
roomContext: config.roomContext
|
|
159
163
|
})._initialize();
|
|
160
164
|
}
|
|
161
165
|
parseData(data) {
|
|
@@ -172,11 +176,51 @@ var NodePlatform = class NodePlatform extends AbstractPlatform {
|
|
|
172
176
|
}
|
|
173
177
|
};
|
|
174
178
|
//#endregion
|
|
179
|
+
//#region src/utils/toRequest.ts
|
|
180
|
+
const getHeader = (incoming, name) => {
|
|
181
|
+
const value = incoming.headers[name];
|
|
182
|
+
if (Array.isArray(value)) return value[0];
|
|
183
|
+
return value;
|
|
184
|
+
};
|
|
185
|
+
const inferOrigin = (incoming) => {
|
|
186
|
+
const host = getHeader(incoming, "x-forwarded-host") ?? getHeader(incoming, "host") ?? "localhost";
|
|
187
|
+
return `${getHeader(incoming, "x-forwarded-proto") ?? (incoming.socket.encrypted ? "https" : "http")}://${host}`;
|
|
188
|
+
};
|
|
189
|
+
const toHeaders = (incoming) => {
|
|
190
|
+
const headers = new Headers();
|
|
191
|
+
for (const [key, value] of Object.entries(incoming.headers)) {
|
|
192
|
+
if (value == null) continue;
|
|
193
|
+
if (Array.isArray(value)) {
|
|
194
|
+
for (const item of value) headers.append(key, item);
|
|
195
|
+
continue;
|
|
196
|
+
}
|
|
197
|
+
headers.append(key, value);
|
|
198
|
+
}
|
|
199
|
+
return headers;
|
|
200
|
+
};
|
|
201
|
+
const toRequest = (request, options = {}) => {
|
|
202
|
+
if (request instanceof Request) return request;
|
|
203
|
+
const origin = options.origin ?? inferOrigin(request);
|
|
204
|
+
const url = new URL(request.url ?? "/", origin);
|
|
205
|
+
const method = request.method ?? "GET";
|
|
206
|
+
const headers = toHeaders(request);
|
|
207
|
+
if (method === "GET" || method === "HEAD") return new Request(url, {
|
|
208
|
+
headers,
|
|
209
|
+
method
|
|
210
|
+
});
|
|
211
|
+
return new Request(url, {
|
|
212
|
+
body: Readable.toWeb(request),
|
|
213
|
+
duplex: "half",
|
|
214
|
+
headers,
|
|
215
|
+
method
|
|
216
|
+
});
|
|
217
|
+
};
|
|
218
|
+
//#endregion
|
|
175
219
|
//#region src/platformNode.ts
|
|
176
220
|
const platformNode = (config = {}) => {
|
|
177
|
-
const { authorize, context, crdt, debug, limits } = config;
|
|
221
|
+
const { authorize, context, crdt, debug, limits, origin } = config;
|
|
178
222
|
return {
|
|
179
|
-
authorize,
|
|
223
|
+
authorize: typeof authorize === "function" ? (input) => authorize({ request: toRequest(input.request, { origin }) }) : authorize,
|
|
180
224
|
context,
|
|
181
225
|
crdt,
|
|
182
226
|
debug,
|
|
@@ -185,6 +229,6 @@ const platformNode = (config = {}) => {
|
|
|
185
229
|
};
|
|
186
230
|
};
|
|
187
231
|
//#endregion
|
|
188
|
-
export { infer, platformNode };
|
|
232
|
+
export { infer, platformNode, toRequest };
|
|
189
233
|
|
|
190
234
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":[],"sources":["../src/infer.ts","../src/NodeWebSocket.ts","../src/NodePlatform.ts","../src/platformNode.ts"],"sourcesContent":["import type { Json } from \"@pluv/types\";\nimport type { identity } from \"./utils\";\n\nexport type InferCallback<TMeta extends Record<string, Json> = {}> = (i: typeof identity) => {\n meta?: (io: TMeta) => TMeta;\n};\n\nexport const infer = <TMeta extends Record<string, Json> = {}>(callback: InferCallback<TMeta>) =>\n callback;\n","import type {\n AbstractCloseEvent,\n AbstractErrorEvent,\n AbstractEventMap,\n AbstractListener,\n AbstractMessageEvent,\n AbstractWebSocketConfig,\n WebSocketSerializedState,\n WebSocketSession,\n} from \"@pluv/io\";\nimport { AbstractWebSocket } from \"@pluv/io\";\nimport type { InferIOAuthorizeUser, IOAuthorize, JsonObject } from \"@pluv/types\";\nimport crypto from \"node:crypto\";\nimport type { WebSocket } from \"ws\";\n\nexport interface NodeWebSocketEventMap {\n close: AbstractCloseEvent;\n message: AbstractMessageEvent;\n error: AbstractErrorEvent;\n}\n\nexport type NodeWebSocketConfig = AbstractWebSocketConfig;\n\nexport class NodeWebSocket<\n TAuthorize extends IOAuthorize<any, any> | null = null,\n> extends AbstractWebSocket<WebSocket> {\n private _sessionId: string | null = null;\n private _state: WebSocketSerializedState;\n private _user: InferIOAuthorizeUser<TAuthorize> = null as InferIOAuthorizeUser<TAuthorize>;\n\n public set presence(presence: JsonObject | null) {\n this._state.presence = presence;\n }\n\n public get readyState(): 0 | 1 | 2 | 3 {\n return this.webSocket.readyState;\n }\n\n public get session(): WebSocketSession<TAuthorize> {\n const sessionId = this.sessionId;\n const state = this._state;\n const user = this._user;\n\n return {\n ...state,\n id: sessionId,\n user,\n webSocket: this,\n };\n }\n\n public get sessionId(): string {\n if (this._sessionId) return this._sessionId;\n\n const sessionId = this._sessionId ?? `p_${crypto.randomUUID()}`;\n\n this._sessionId = sessionId;\n\n return sessionId;\n }\n\n public get state(): WebSocketSerializedState {\n return this._state;\n }\n\n public set state(state: WebSocketSerializedState) {\n this._state = state;\n }\n\n public set user(user: InferIOAuthorizeUser<TAuthorize>) {\n this._user = user;\n }\n\n constructor(webSocket: WebSocket, config: NodeWebSocketConfig) {\n const { room } = config;\n\n super(webSocket, config);\n\n this._state = {\n presence: null,\n quit: false,\n room,\n timers: {\n ping: new Date().getTime(),\n presence: null,\n },\n };\n }\n\n public addEventListener<TType extends keyof AbstractEventMap>(\n type: TType,\n handler: AbstractListener<TType>,\n ): void {\n switch (type) {\n case \"close\":\n this.webSocket.on(\"close\", async (code, buffer) => {\n const reason = buffer.toString(\"utf-8\");\n\n await Promise.resolve((handler as AbstractListener<\"close\">)({ code, reason }));\n });\n\n return;\n case \"error\":\n this.webSocket.on(\"error\", async (error) => {\n const message = error.message;\n\n await Promise.resolve(\n (handler as AbstractListener<\"error\">)({ error, message }),\n );\n });\n\n return;\n case \"message\":\n this.webSocket.on(\"message\", async (data) => {\n await Promise.resolve((handler as AbstractListener<\"message\">)({ data }));\n });\n\n return;\n default:\n }\n }\n\n public close(code?: number | undefined, reason?: string | undefined): void {\n const canClose = [this.CONNECTING, this.OPEN].some(\n (readyState) => readyState === this.readyState,\n );\n\n if (!canClose) return;\n\n this.webSocket.close(code, reason);\n }\n\n public send(message: string | ArrayBuffer | ArrayBufferView): void {\n if (this.readyState !== this.OPEN) return;\n\n this.webSocket.send(message, (error) => {\n if (error) throw error;\n });\n }\n\n public terminate(): void {\n this.webSocket.terminate();\n }\n}\n","import type {\n AbstractPersistence,\n AbstractPlatformConfig,\n AbstractPubSub,\n ConvertWebSocketConfig,\n WebSocketRegistrationMode,\n WebSocketSerializedState,\n} from \"@pluv/io\";\nimport { AbstractPlatform } from \"@pluv/io\";\nimport type { IOAuthorize, Json } from \"@pluv/types\";\nimport crypto from \"node:crypto\";\nimport type { IncomingMessage } from \"node:http\";\nimport { TextDecoder } from \"node:util\";\nimport type { WebSocket } from \"ws\";\nimport { NodeWebSocket } from \"./NodeWebSocket\";\n\nexport type NodePlatformRoomContext<TMeta extends Record<string, Json>> = keyof TMeta extends never\n ? { meta?: undefined }\n : { meta: TMeta };\n\nexport type NodePlatformConfig<TMeta extends Record<string, Json>> = {\n mode?: WebSocketRegistrationMode;\n} & (\n | { persistence?: undefined; pubSub?: undefined }\n | { persistence: AbstractPersistence; pubSub: AbstractPubSub }\n) & { roomContext?: NodePlatformRoomContext<TMeta> };\n\nexport class NodePlatform<\n TAuthorize extends IOAuthorize<any, any> | null = null,\n TMeta extends Record<string, Json> = {},\n> extends AbstractPlatform<\n NodeWebSocket<TAuthorize>,\n { req: IncomingMessage },\n NodePlatformRoomContext<TMeta>,\n {\n authorize: {\n secret: true;\n };\n handleMode: \"io\";\n requireAuth: false;\n registrationMode: WebSocketRegistrationMode;\n listeners: {\n onRoomDestroyed: true;\n onRoomMessage: true;\n onStorageDestroyed: true;\n onStorageUpdated: true;\n onUserConnected: true;\n onUserDisconnected: true;\n };\n router: true;\n }\n> {\n public readonly id = crypto.randomUUID();\n public readonly _config;\n public readonly _name = \"platformNode\";\n\n constructor(config: NodePlatformConfig<TMeta> = {}) {\n const { roomContext, mode = \"attached\", persistence, pubSub } = config;\n\n super({\n roomContext,\n ...(persistence && pubSub ? { persistence, pubSub } : {}),\n });\n\n this._config = {\n authorize: {\n secret: true as const,\n },\n handleMode: \"io\" as const,\n registrationMode: mode,\n requireAuth: false as const,\n listeners: {\n onRoomDestroyed: true as const,\n onRoomMessage: true as const,\n onStorageDestroyed: true as const,\n onStorageUpdated: true as const,\n onUserConnected: true as const,\n onUserDisconnected: true as const,\n },\n router: true as const,\n };\n }\n\n public acceptWebSocket(webSocket: NodeWebSocket<TAuthorize>): Promise<void> {\n return Promise.resolve(undefined);\n }\n\n public convertWebSocket(\n webSocket: WebSocket,\n config: ConvertWebSocketConfig,\n ): NodeWebSocket<TAuthorize> {\n const { room } = config;\n\n return new NodeWebSocket<TAuthorize>(webSocket, {\n persistence: this.persistence,\n platform: this,\n room,\n });\n }\n\n public getLastPing(webSocket: NodeWebSocket<TAuthorize>): number | null {\n return null;\n }\n\n public getSerializedState(webSocket: WebSocket): WebSocketSerializedState | null {\n return null;\n }\n\n public getSessionId(webSocket: WebSocket): string | null {\n return null;\n }\n\n public getWebSockets(): readonly WebSocket[] {\n return [];\n }\n\n public initialize(config: AbstractPlatformConfig<NodePlatformRoomContext<TMeta>>): this {\n return new NodePlatform({\n roomContext: config.roomContext,\n mode: this._config.registrationMode,\n persistence: this.persistence.initialize(config.roomContext),\n pubSub: this.pubSub,\n } as NodePlatformConfig<TMeta>)._initialize() as this;\n }\n\n public parseData(data: string | ArrayBuffer): Record<string, any> {\n if (typeof data === \"string\") return JSON.parse(data);\n\n const decoder = new TextDecoder(\"utf8\");\n\n return JSON.parse(decoder.decode(data));\n }\n\n public randomUUID(): string {\n return crypto.randomUUID();\n }\n\n public setSerializedState(\n webSocket: NodeWebSocket<TAuthorize>,\n state: WebSocketSerializedState,\n ): WebSocketSerializedState {\n webSocket.state = state;\n\n return webSocket.state;\n }\n}\n","import type { CrdtLibraryType, NoopCrdtDocFactory } from \"@pluv/crdt\";\nimport type { CreateIOParams, InferInitContextType, PluvContext, PluvIOAuthorize } from \"@pluv/io\";\nimport type { BaseUser, Id, IOAuthorize, Json } from \"@pluv/types\";\nimport type { NodePlatformConfig } from \"./NodePlatform\";\nimport { NodePlatform } from \"./NodePlatform\";\nimport type { InferCallback } from \"./infer\";\n\nexport type PlatformNodeCreateIOParams<\n TMeta extends Record<string, Json> = {},\n TContext extends Record<string, any> = {},\n TUser extends BaseUser | null = null,\n TCrdt extends CrdtLibraryType<any> = CrdtLibraryType<NoopCrdtDocFactory>,\n> = Id<\n NodePlatformConfig<TMeta> &\n Omit<\n CreateIOParams<\n NodePlatform<IOAuthorize<TUser, TContext>, TMeta>,\n TContext,\n TUser,\n TCrdt\n >,\n \"authorize\" | \"context\" | \"platform\"\n > & {\n authorize?: PluvIOAuthorize<\n NodePlatform<IOAuthorize<TUser, TContext>, TMeta>,\n TUser,\n InferInitContextType<NodePlatform<IOAuthorize<TUser, TContext>, TMeta>>\n >;\n context?: PluvContext<NodePlatform<IOAuthorize<TUser, any>, TMeta>, TContext>;\n types?: InferCallback<TMeta>;\n }\n>;\n\nexport const platformNode = <\n TMeta extends Record<string, Json> = {},\n TContext extends Record<string, any> = {},\n TUser extends BaseUser | null = null,\n TCrdt extends CrdtLibraryType<any> = CrdtLibraryType<NoopCrdtDocFactory>,\n>(\n config: PlatformNodeCreateIOParams<TMeta, TContext, TUser, TCrdt> = {},\n): CreateIOParams<NodePlatform<IOAuthorize<TUser, TContext>, TMeta>, TContext, TUser, TCrdt> => {\n const { authorize, context, crdt, debug, limits } = config;\n\n return {\n authorize,\n context,\n crdt,\n debug,\n limits,\n platform: () => new NodePlatform<IOAuthorize<TUser, TContext>, TMeta>(config),\n };\n};\n"],"mappings":";;;;AAOA,MAAa,SAAkD,aAC3D;;;ACeJ,IAAa,gBAAb,cAEU,kBAA6B;CACnC,aAAoC;CACpC;CACA,QAAkD;CAElD,IAAW,SAAS,UAA6B;EAC7C,KAAK,OAAO,WAAW;CAC3B;CAEA,IAAW,aAA4B;EACnC,OAAO,KAAK,UAAU;CAC1B;CAEA,IAAW,UAAwC;EAC/C,MAAM,YAAY,KAAK;EACvB,MAAM,QAAQ,KAAK;EACnB,MAAM,OAAO,KAAK;EAElB,OAAO;GACH,GAAG;GACH,IAAI;GACJ;GACA,WAAW;EACf;CACJ;CAEA,IAAW,YAAoB;EAC3B,IAAI,KAAK,YAAY,OAAO,KAAK;EAEjC,MAAM,YAAY,KAAK,cAAc,KAAK,OAAO,WAAW;EAE5D,KAAK,aAAa;EAElB,OAAO;CACX;CAEA,IAAW,QAAkC;EACzC,OAAO,KAAK;CAChB;CAEA,IAAW,MAAM,OAAiC;EAC9C,KAAK,SAAS;CAClB;CAEA,IAAW,KAAK,MAAwC;EACpD,KAAK,QAAQ;CACjB;CAEA,YAAY,WAAsB,QAA6B;EAC3D,MAAM,EAAE,SAAS;EAEjB,MAAM,WAAW,MAAM;EAEvB,KAAK,SAAS;GACV,UAAU;GACV,MAAM;GACN;GACA,QAAQ;IACJ,uBAAM,IAAI,KAAK,EAAA,CAAE,QAAQ;IACzB,UAAU;GACd;EACJ;CACJ;CAEA,iBACI,MACA,SACI;EACJ,QAAQ,MAAR;GACI,KAAK;IACD,KAAK,UAAU,GAAG,SAAS,OAAO,MAAM,WAAW;KAC/C,MAAM,SAAS,OAAO,SAAS,OAAO;KAEtC,MAAM,QAAQ,QAAS,QAAsC;MAAE;MAAM;KAAO,CAAC,CAAC;IAClF,CAAC;IAED;GACJ,KAAK;IACD,KAAK,UAAU,GAAG,SAAS,OAAO,UAAU;K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U;GACV;EACJ,CAAC;CACL;CAEA,YAAmB,WAAqD;EACpE,OAAO;CACX;CAEA,mBAA0B,WAAuD;EAC7E,OAAO;CACX;CAEA,aAAoB,WAAqC;EACrD,OAAO;CACX;CAEA,gBAA6C;EACzC,OAAO,CAAC;CACZ;CAEA,WAAkB,QAAsE;EACpF,OAAO,IAAI,aAAa;GACpB,aAAa,OAAO;GACpB,MAAM,KAAK,QAAQ;GACnB,aAAa,KAAK,YAAY,WAAW,OAAO,WAAW;GAC3D,QAAQ,KAAK;EACjB,CAA8B,CAAC,CAAC,YAAY;CAChD;CAEA,UAAiB,MAAiD;EAC9D,IAAI,OAAO,SAAS,UAAU,OAAO,KAAK,MAAM,IAAI;EAEpD,MAAM,UAAU,IAAI,YAAY,MAAM;EAEtC,OAAO,KAAK,MAAM,QAAQ,OAAO,IAAI,CAAC;CAC1C;CAEA,aAA4B;EACxB,OAAO,OAAO,WAAW;CAC7B;CAEA,mBACI,WACA,OACwB;EACxB,UAAU,QAAQ;EAElB,OAAO,UAAU;CACrB;AACJ;;;AChHA,MAAa,gBAMT,SAAoE,CAAC,MACuB;CAC5F,MAAM,EAAE,WAAW,SAAS,MAAM,OAAO,WAAW;CAEpD,OAAO;EACH;EACA;EACA;EACA;EACA;EACA,gBAAgB,IAAI,aAAkD,MAAM;CAChF;AACJ"}
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../src/infer.ts","../src/NodeWebSocket.ts","../src/NodePlatform.ts","../src/utils/toRequest.ts","../src/platformNode.ts"],"sourcesContent":["import type { Json } from \"@pluv/types\";\nimport type { identity } from \"./utils\";\n\nexport type InferCallback<TMeta extends Record<string, Json> = {}> = (i: typeof identity) => {\n meta?: (io: TMeta) => TMeta;\n};\n\nexport const infer = <TMeta extends Record<string, Json> = {}>(callback: InferCallback<TMeta>) =>\n callback;\n","import type {\n AbstractCloseEvent,\n AbstractErrorEvent,\n AbstractEventMap,\n AbstractListener,\n AbstractMessageEvent,\n AbstractWebSocketConfig,\n WebSocketSerializedState,\n WebSocketSession,\n} from \"@pluv/io\";\nimport { AbstractWebSocket } from \"@pluv/io\";\nimport type { InferIOAuthorizeUser, IOAuthorize, JsonObject } from \"@pluv/types\";\nimport crypto from \"node:crypto\";\nimport type { WebSocket } from \"ws\";\n\nexport interface NodeWebSocketEventMap {\n close: AbstractCloseEvent;\n message: AbstractMessageEvent;\n error: AbstractErrorEvent;\n}\n\nexport type NodeWebSocketConfig = AbstractWebSocketConfig;\n\nexport class NodeWebSocket<\n TAuthorize extends IOAuthorize<any, any> | null = null,\n> extends AbstractWebSocket<WebSocket> {\n private _sessionId: string | null = null;\n private _state: WebSocketSerializedState;\n private _user: InferIOAuthorizeUser<TAuthorize> = null as InferIOAuthorizeUser<TAuthorize>;\n\n public set presence(presence: JsonObject | null) {\n this._state.presence = presence;\n }\n\n public get readyState(): 0 | 1 | 2 | 3 {\n return this.webSocket.readyState;\n }\n\n public get session(): WebSocketSession<TAuthorize> {\n const sessionId = this.sessionId;\n const state = this._state;\n const user = this._user;\n\n return {\n ...state,\n id: sessionId,\n user,\n webSocket: this,\n };\n }\n\n public get sessionId(): string {\n if (this._sessionId) return this._sessionId;\n\n const sessionId = this._sessionId ?? `p_${crypto.randomUUID()}`;\n\n this._sessionId = sessionId;\n\n return sessionId;\n }\n\n public get state(): WebSocketSerializedState {\n return this._state;\n }\n\n public set state(state: WebSocketSerializedState) {\n this._state = state;\n }\n\n public set user(user: InferIOAuthorizeUser<TAuthorize>) {\n this._user = user;\n }\n\n constructor(webSocket: WebSocket, config: NodeWebSocketConfig) {\n const { room } = config;\n\n super(webSocket, config);\n\n this._state = {\n presence: null,\n quit: false,\n room,\n timers: {\n ping: new Date().getTime(),\n presence: null,\n },\n };\n }\n\n public addEventListener<TType extends keyof AbstractEventMap>(\n type: TType,\n handler: AbstractListener<TType>,\n ): void {\n switch (type) {\n case \"close\":\n this.webSocket.on(\"close\", async (code, buffer) => {\n const reason = buffer.toString(\"utf-8\");\n\n await Promise.resolve((handler as AbstractListener<\"close\">)({ code, reason }));\n });\n\n return;\n case \"error\":\n this.webSocket.on(\"error\", async (error) => {\n const message = error.message;\n\n await Promise.resolve(\n (handler as AbstractListener<\"error\">)({ error, message }),\n );\n });\n\n return;\n case \"message\":\n this.webSocket.on(\"message\", async (data) => {\n await Promise.resolve((handler as AbstractListener<\"message\">)({ data }));\n });\n\n return;\n default:\n }\n }\n\n public close(code?: number | undefined, reason?: string | undefined): void {\n const canClose = [this.CONNECTING, this.OPEN].some(\n (readyState) => readyState === this.readyState,\n );\n\n if (!canClose) return;\n\n this.webSocket.close(code, reason);\n }\n\n public send(message: string | ArrayBuffer | ArrayBufferView): void {\n if (this.readyState !== this.OPEN) return;\n\n this.webSocket.send(message, (error) => {\n if (error) throw error;\n });\n }\n\n public terminate(): void {\n this.webSocket.terminate();\n }\n}\n","import type {\n AbstractPersistence,\n AbstractPlatformConfig,\n AbstractPubSub,\n ConvertWebSocketConfig,\n WebSocketRegistrationMode,\n WebSocketSerializedState,\n} from \"@pluv/io\";\nimport { AbstractPlatform } from \"@pluv/io\";\nimport type { IOAuthorize, Json } from \"@pluv/types\";\nimport crypto from \"node:crypto\";\nimport { TextDecoder } from \"node:util\";\nimport type { WebSocket } from \"ws\";\nimport { NodeWebSocket } from \"./NodeWebSocket\";\nimport type { NodeRegisterInput } from \"./types\";\n\nexport type NodePlatformRoomContext<TMeta extends Record<string, Json>> = keyof TMeta extends never\n ? { meta?: undefined }\n : { meta: TMeta };\n\nexport type NodePlatformConfig<TMeta extends Record<string, Json>> = {\n mode?: WebSocketRegistrationMode;\n origin?: string;\n} & (\n | { persistence?: undefined; pubSub?: undefined }\n | { persistence: AbstractPersistence; pubSub: AbstractPubSub }\n) & { roomContext?: NodePlatformRoomContext<TMeta> };\n\nexport class NodePlatform<\n TAuthorize extends IOAuthorize<any, any> | null = null,\n TMeta extends Record<string, Json> = {},\n> extends AbstractPlatform<\n NodeWebSocket<TAuthorize>,\n NodeRegisterInput,\n NodePlatformRoomContext<TMeta>,\n {\n authorize: {\n secret: true;\n };\n handleMode: \"io\";\n requireAuth: false;\n registrationMode: WebSocketRegistrationMode;\n listeners: {\n onRoomDestroyed: true;\n onRoomMessage: true;\n onStorageDestroyed: true;\n onStorageUpdated: true;\n onUserConnected: true;\n onUserDisconnected: true;\n };\n router: true;\n }\n> {\n public readonly id = crypto.randomUUID();\n public readonly _config;\n public readonly _name = \"platformNode\";\n public readonly origin: string | undefined;\n\n constructor(config: NodePlatformConfig<TMeta> = {}) {\n const { origin, roomContext, mode = \"attached\", persistence, pubSub } = config;\n\n super({\n roomContext,\n ...(persistence && pubSub ? { persistence, pubSub } : {}),\n });\n\n this.origin = origin;\n\n this._config = {\n authorize: {\n secret: true as const,\n },\n handleMode: \"io\" as const,\n registrationMode: mode,\n requireAuth: false as const,\n listeners: {\n onRoomDestroyed: true as const,\n onRoomMessage: true as const,\n onStorageDestroyed: true as const,\n onStorageUpdated: true as const,\n onUserConnected: true as const,\n onUserDisconnected: true as const,\n },\n router: true as const,\n };\n }\n\n public acceptWebSocket(webSocket: NodeWebSocket<TAuthorize>): Promise<void> {\n return Promise.resolve(undefined);\n }\n\n public convertWebSocket(\n webSocket: WebSocket,\n config: ConvertWebSocketConfig,\n ): NodeWebSocket<TAuthorize> {\n const { room } = config;\n\n return new NodeWebSocket<TAuthorize>(webSocket, {\n persistence: this.persistence,\n platform: this,\n room,\n });\n }\n\n public getLastPing(webSocket: NodeWebSocket<TAuthorize>): number | null {\n return null;\n }\n\n public getSerializedState(webSocket: WebSocket): WebSocketSerializedState | null {\n return null;\n }\n\n public getSessionId(webSocket: WebSocket): string | null {\n return null;\n }\n\n public getWebSockets(): readonly WebSocket[] {\n return [];\n }\n\n public initialize(config: AbstractPlatformConfig<NodePlatformRoomContext<TMeta>>): this {\n return new NodePlatform({\n mode: this._config.registrationMode,\n origin: this.origin,\n persistence: this.persistence.initialize(config.roomContext),\n pubSub: this.pubSub,\n roomContext: config.roomContext,\n } as NodePlatformConfig<TMeta>)._initialize() as this;\n }\n\n public parseData(data: string | ArrayBuffer): Record<string, any> {\n if (typeof data === \"string\") return JSON.parse(data);\n\n const decoder = new TextDecoder(\"utf8\");\n\n return JSON.parse(decoder.decode(data));\n }\n\n public randomUUID(): string {\n return crypto.randomUUID();\n }\n\n public setSerializedState(\n webSocket: NodeWebSocket<TAuthorize>,\n state: WebSocketSerializedState,\n ): WebSocketSerializedState {\n webSocket.state = state;\n\n return webSocket.state;\n }\n}\n","import type { IncomingMessage } from \"node:http\";\nimport { Readable } from \"node:stream\";\n\nconst getHeader = (incoming: IncomingMessage, name: string): string | undefined => {\n const value = incoming.headers[name];\n\n if (Array.isArray(value)) return value[0];\n\n return value;\n};\n\nconst inferOrigin = (incoming: IncomingMessage): string => {\n const host =\n getHeader(incoming, \"x-forwarded-host\") ?? getHeader(incoming, \"host\") ?? \"localhost\";\n const forwardedProto = getHeader(incoming, \"x-forwarded-proto\");\n const protocol =\n forwardedProto ??\n ((incoming.socket as { encrypted?: boolean }).encrypted ? \"https\" : \"http\");\n\n return `${protocol}://${host}`;\n};\n\nconst toHeaders = (incoming: IncomingMessage): Headers => {\n const headers = new Headers();\n\n for (const [key, value] of Object.entries(incoming.headers)) {\n if (value == null) continue;\n\n if (Array.isArray(value)) {\n for (const item of value) headers.append(key, item);\n\n continue;\n }\n\n headers.append(key, value);\n }\n\n return headers;\n};\n\nexport type ToRequestOptions = {\n origin?: string;\n};\n\nexport const toRequest = (\n request: Request | IncomingMessage,\n options: ToRequestOptions = {},\n): Request => {\n if (request instanceof Request) return request;\n\n const origin = options.origin ?? inferOrigin(request);\n const url = new URL(request.url ?? \"/\", origin);\n const method = request.method ?? \"GET\";\n const headers = toHeaders(request);\n\n if (method === \"GET\" || method === \"HEAD\") {\n return new Request(url, {\n headers,\n method,\n });\n }\n\n return new Request(url, {\n body: Readable.toWeb(request) as ReadableStream,\n duplex: \"half\",\n headers,\n method,\n });\n};\n","import type { CrdtLibraryType, NoopCrdtDocFactory } from \"@pluv/crdt\";\nimport type { CreateIOParams, PluvContext, PluvIOAuthorize } from \"@pluv/io\";\nimport type { BaseUser, Id, IOAuthorize, Json } from \"@pluv/types\";\nimport type { IncomingMessage } from \"node:http\";\nimport type { NodePlatformConfig } from \"./NodePlatform\";\nimport { NodePlatform } from \"./NodePlatform\";\nimport type { InferCallback } from \"./infer\";\nimport type { NodeAuthorizeContext } from \"./types\";\nimport { toRequest } from \"./utils/toRequest\";\n\nexport type PlatformNodeCreateIOParams<\n TMeta extends Record<string, Json> = {},\n TContext extends Record<string, any> = {},\n TUser extends BaseUser | null = null,\n TCrdt extends CrdtLibraryType<any> = CrdtLibraryType<NoopCrdtDocFactory>,\n> = Id<\n NodePlatformConfig<TMeta> &\n Omit<\n CreateIOParams<\n NodePlatform<IOAuthorize<TUser, TContext>, TMeta>,\n TContext,\n TUser,\n TCrdt\n >,\n \"authorize\" | \"context\" | \"platform\"\n > & {\n authorize?: PluvIOAuthorize<\n NodePlatform<IOAuthorize<TUser, TContext>, TMeta>,\n TUser,\n NodeAuthorizeContext\n >;\n context?: PluvContext<NodePlatform<IOAuthorize<TUser, any>, TMeta>, TContext>;\n types?: InferCallback<TMeta>;\n }\n>;\n\nexport const platformNode = <\n TMeta extends Record<string, Json> = {},\n TContext extends Record<string, any> = {},\n TUser extends BaseUser | null = null,\n TCrdt extends CrdtLibraryType<any> = CrdtLibraryType<NoopCrdtDocFactory>,\n>(\n config: PlatformNodeCreateIOParams<TMeta, TContext, TUser, TCrdt> = {},\n): CreateIOParams<NodePlatform<IOAuthorize<TUser, TContext>, TMeta>, TContext, TUser, TCrdt> => {\n const { authorize, context, crdt, debug, limits, origin } = config;\n\n const resolvedAuthorize =\n typeof authorize === \"function\"\n ? (input: { request: Request | IncomingMessage }) =>\n authorize({ request: toRequest(input.request, { origin }) })\n : authorize;\n\n return {\n authorize: resolvedAuthorize,\n context,\n crdt,\n debug,\n limits,\n platform: () => new NodePlatform<IOAuthorize<TUser, TContext>, TMeta>(config),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EA,OAAO;AACX;AAMA,MAAa,aACT,SACA,UAA4B,CAAC,MACnB;CACV,IAAI,mBAAmB,SAAS,OAAO;CAEvC,MAAM,SAAS,QAAQ,UAAU,YAAY,OAAO;CACpD,MAAM,MAAM,IAAI,IAAI,QAAQ,OAAO,KAAK,MAAM;CAC9C,MAAM,SAAS,QAAQ,UAAU;CACjC,MAAM,UAAU,UAAU,OAAO;CAEjC,IAAI,WAAW,SAAS,WAAW,QAC/B,OAAO,IAAI,QAAQ,KAAK;EACpB;EACA;CACJ,CAAC;CAGL,OAAO,IAAI,QAAQ,KAAK;EACpB,MAAM,SAAS,MAAM,OAAO;EAC5B,QAAQ;EACR;EACA;CACJ,CAAC;AACL;;;AChCA,MAAa,gBAMT,SAAoE,CAAC,MACuB;CAC5F,MAAM,EAAE,WAAW,SAAS,MAAM,OAAO,QAAQ,WAAW;CAQ5D,OAAO;EACH,WANA,OAAO,cAAc,cACd,UACG,UAAU,EAAE,SAAS,UAAU,MAAM,SAAS,EAAE,OAAO,CAAC,EAAE,CAAC,IAC/D;EAIN;EACA;EACA;EACA;EACA,gBAAgB,IAAI,aAAkD,MAAM;CAChF;AACJ"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pluv/platform-node",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.0.0",
|
|
4
4
|
"description": "@pluv/io adapter for node.js",
|
|
5
5
|
"author": {
|
|
6
6
|
"email": "david@pluv.io",
|
|
@@ -20,20 +20,20 @@
|
|
|
20
20
|
"access": "public"
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@types/node": "^25.9.
|
|
23
|
+
"@types/node": "^25.9.3",
|
|
24
24
|
"@types/ws": "^8.18.1",
|
|
25
25
|
"path-to-regexp": "^8.4.2",
|
|
26
|
-
"@pluv/
|
|
27
|
-
"@pluv/
|
|
28
|
-
"@pluv/
|
|
26
|
+
"@pluv/crdt": "^5.0.0",
|
|
27
|
+
"@pluv/io": "^5.0.0",
|
|
28
|
+
"@pluv/types": "^5.0.0"
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|
|
31
|
-
"eslint": "^10.
|
|
32
|
-
"tsdown": "0.22.
|
|
31
|
+
"eslint": "^10.5.0",
|
|
32
|
+
"tsdown": "0.22.3",
|
|
33
33
|
"typescript": "^5.9.3",
|
|
34
34
|
"ws": "^8.21.0",
|
|
35
|
-
"@pluv/tsconfig": "^
|
|
36
|
-
"eslint-config-pluv": "^
|
|
35
|
+
"@pluv/tsconfig": "^5.0.0",
|
|
36
|
+
"eslint-config-pluv": "^5.0.0"
|
|
37
37
|
},
|
|
38
38
|
"peerDependencies": {
|
|
39
39
|
"ws": "^8.0.0"
|
package/src/NodePlatform.ts
CHANGED
|
@@ -9,10 +9,10 @@ import type {
|
|
|
9
9
|
import { AbstractPlatform } from "@pluv/io";
|
|
10
10
|
import type { IOAuthorize, Json } from "@pluv/types";
|
|
11
11
|
import crypto from "node:crypto";
|
|
12
|
-
import type { IncomingMessage } from "node:http";
|
|
13
12
|
import { TextDecoder } from "node:util";
|
|
14
13
|
import type { WebSocket } from "ws";
|
|
15
14
|
import { NodeWebSocket } from "./NodeWebSocket";
|
|
15
|
+
import type { NodeRegisterInput } from "./types";
|
|
16
16
|
|
|
17
17
|
export type NodePlatformRoomContext<TMeta extends Record<string, Json>> = keyof TMeta extends never
|
|
18
18
|
? { meta?: undefined }
|
|
@@ -20,6 +20,7 @@ export type NodePlatformRoomContext<TMeta extends Record<string, Json>> = keyof
|
|
|
20
20
|
|
|
21
21
|
export type NodePlatformConfig<TMeta extends Record<string, Json>> = {
|
|
22
22
|
mode?: WebSocketRegistrationMode;
|
|
23
|
+
origin?: string;
|
|
23
24
|
} & (
|
|
24
25
|
| { persistence?: undefined; pubSub?: undefined }
|
|
25
26
|
| { persistence: AbstractPersistence; pubSub: AbstractPubSub }
|
|
@@ -30,7 +31,7 @@ export class NodePlatform<
|
|
|
30
31
|
TMeta extends Record<string, Json> = {},
|
|
31
32
|
> extends AbstractPlatform<
|
|
32
33
|
NodeWebSocket<TAuthorize>,
|
|
33
|
-
|
|
34
|
+
NodeRegisterInput,
|
|
34
35
|
NodePlatformRoomContext<TMeta>,
|
|
35
36
|
{
|
|
36
37
|
authorize: {
|
|
@@ -53,15 +54,18 @@ export class NodePlatform<
|
|
|
53
54
|
public readonly id = crypto.randomUUID();
|
|
54
55
|
public readonly _config;
|
|
55
56
|
public readonly _name = "platformNode";
|
|
57
|
+
public readonly origin: string | undefined;
|
|
56
58
|
|
|
57
59
|
constructor(config: NodePlatformConfig<TMeta> = {}) {
|
|
58
|
-
const { roomContext, mode = "attached", persistence, pubSub } = config;
|
|
60
|
+
const { origin, roomContext, mode = "attached", persistence, pubSub } = config;
|
|
59
61
|
|
|
60
62
|
super({
|
|
61
63
|
roomContext,
|
|
62
64
|
...(persistence && pubSub ? { persistence, pubSub } : {}),
|
|
63
65
|
});
|
|
64
66
|
|
|
67
|
+
this.origin = origin;
|
|
68
|
+
|
|
65
69
|
this._config = {
|
|
66
70
|
authorize: {
|
|
67
71
|
secret: true as const,
|
|
@@ -116,10 +120,11 @@ export class NodePlatform<
|
|
|
116
120
|
|
|
117
121
|
public initialize(config: AbstractPlatformConfig<NodePlatformRoomContext<TMeta>>): this {
|
|
118
122
|
return new NodePlatform({
|
|
119
|
-
roomContext: config.roomContext,
|
|
120
123
|
mode: this._config.registrationMode,
|
|
124
|
+
origin: this.origin,
|
|
121
125
|
persistence: this.persistence.initialize(config.roomContext),
|
|
122
126
|
pubSub: this.pubSub,
|
|
127
|
+
roomContext: config.roomContext,
|
|
123
128
|
} as NodePlatformConfig<TMeta>)._initialize() as this;
|
|
124
129
|
}
|
|
125
130
|
|
package/src/index.ts
CHANGED
|
@@ -4,3 +4,6 @@ export type { InferCallback } from "./infer";
|
|
|
4
4
|
export type { NodePlatform, NodePlatformConfig } from "./NodePlatform";
|
|
5
5
|
export { platformNode } from "./platformNode";
|
|
6
6
|
export type { PlatformNodeCreateIOParams } from "./platformNode";
|
|
7
|
+
export type { NodeAuthorizeContext, NodeRegisterInput } from "./types";
|
|
8
|
+
export { toRequest } from "./utils/toRequest";
|
|
9
|
+
export type { ToRequestOptions } from "./utils/toRequest";
|
package/src/platformNode.ts
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import type { CrdtLibraryType, NoopCrdtDocFactory } from "@pluv/crdt";
|
|
2
|
-
import type { CreateIOParams,
|
|
2
|
+
import type { CreateIOParams, PluvContext, PluvIOAuthorize } from "@pluv/io";
|
|
3
3
|
import type { BaseUser, Id, IOAuthorize, Json } from "@pluv/types";
|
|
4
|
+
import type { IncomingMessage } from "node:http";
|
|
4
5
|
import type { NodePlatformConfig } from "./NodePlatform";
|
|
5
6
|
import { NodePlatform } from "./NodePlatform";
|
|
6
7
|
import type { InferCallback } from "./infer";
|
|
8
|
+
import type { NodeAuthorizeContext } from "./types";
|
|
9
|
+
import { toRequest } from "./utils/toRequest";
|
|
7
10
|
|
|
8
11
|
export type PlatformNodeCreateIOParams<
|
|
9
12
|
TMeta extends Record<string, Json> = {},
|
|
@@ -24,7 +27,7 @@ export type PlatformNodeCreateIOParams<
|
|
|
24
27
|
authorize?: PluvIOAuthorize<
|
|
25
28
|
NodePlatform<IOAuthorize<TUser, TContext>, TMeta>,
|
|
26
29
|
TUser,
|
|
27
|
-
|
|
30
|
+
NodeAuthorizeContext
|
|
28
31
|
>;
|
|
29
32
|
context?: PluvContext<NodePlatform<IOAuthorize<TUser, any>, TMeta>, TContext>;
|
|
30
33
|
types?: InferCallback<TMeta>;
|
|
@@ -39,10 +42,16 @@ export const platformNode = <
|
|
|
39
42
|
>(
|
|
40
43
|
config: PlatformNodeCreateIOParams<TMeta, TContext, TUser, TCrdt> = {},
|
|
41
44
|
): CreateIOParams<NodePlatform<IOAuthorize<TUser, TContext>, TMeta>, TContext, TUser, TCrdt> => {
|
|
42
|
-
const { authorize, context, crdt, debug, limits } = config;
|
|
45
|
+
const { authorize, context, crdt, debug, limits, origin } = config;
|
|
46
|
+
|
|
47
|
+
const resolvedAuthorize =
|
|
48
|
+
typeof authorize === "function"
|
|
49
|
+
? (input: { request: Request | IncomingMessage }) =>
|
|
50
|
+
authorize({ request: toRequest(input.request, { origin }) })
|
|
51
|
+
: authorize;
|
|
43
52
|
|
|
44
53
|
return {
|
|
45
|
-
authorize,
|
|
54
|
+
authorize: resolvedAuthorize,
|
|
46
55
|
context,
|
|
47
56
|
crdt,
|
|
48
57
|
debug,
|
package/src/types.ts
ADDED
package/src/utils/index.ts
CHANGED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import type { IncomingMessage } from "node:http";
|
|
2
|
+
import { Readable } from "node:stream";
|
|
3
|
+
|
|
4
|
+
const getHeader = (incoming: IncomingMessage, name: string): string | undefined => {
|
|
5
|
+
const value = incoming.headers[name];
|
|
6
|
+
|
|
7
|
+
if (Array.isArray(value)) return value[0];
|
|
8
|
+
|
|
9
|
+
return value;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
const inferOrigin = (incoming: IncomingMessage): string => {
|
|
13
|
+
const host =
|
|
14
|
+
getHeader(incoming, "x-forwarded-host") ?? getHeader(incoming, "host") ?? "localhost";
|
|
15
|
+
const forwardedProto = getHeader(incoming, "x-forwarded-proto");
|
|
16
|
+
const protocol =
|
|
17
|
+
forwardedProto ??
|
|
18
|
+
((incoming.socket as { encrypted?: boolean }).encrypted ? "https" : "http");
|
|
19
|
+
|
|
20
|
+
return `${protocol}://${host}`;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const toHeaders = (incoming: IncomingMessage): Headers => {
|
|
24
|
+
const headers = new Headers();
|
|
25
|
+
|
|
26
|
+
for (const [key, value] of Object.entries(incoming.headers)) {
|
|
27
|
+
if (value == null) continue;
|
|
28
|
+
|
|
29
|
+
if (Array.isArray(value)) {
|
|
30
|
+
for (const item of value) headers.append(key, item);
|
|
31
|
+
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
headers.append(key, value);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return headers;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export type ToRequestOptions = {
|
|
42
|
+
origin?: string;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export const toRequest = (
|
|
46
|
+
request: Request | IncomingMessage,
|
|
47
|
+
options: ToRequestOptions = {},
|
|
48
|
+
): Request => {
|
|
49
|
+
if (request instanceof Request) return request;
|
|
50
|
+
|
|
51
|
+
const origin = options.origin ?? inferOrigin(request);
|
|
52
|
+
const url = new URL(request.url ?? "/", origin);
|
|
53
|
+
const method = request.method ?? "GET";
|
|
54
|
+
const headers = toHeaders(request);
|
|
55
|
+
|
|
56
|
+
if (method === "GET" || method === "HEAD") {
|
|
57
|
+
return new Request(url, {
|
|
58
|
+
headers,
|
|
59
|
+
method,
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return new Request(url, {
|
|
64
|
+
body: Readable.toWeb(request) as ReadableStream,
|
|
65
|
+
duplex: "half",
|
|
66
|
+
headers,
|
|
67
|
+
method,
|
|
68
|
+
});
|
|
69
|
+
};
|