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