@pluv/platform-pluv 0.32.9 → 0.34.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/dist/index.mjs CHANGED
@@ -41,9 +41,9 @@ var __async = (__this, __arguments, generator) => {
41
41
  });
42
42
  };
43
43
 
44
- // src/PluvIO.ts
44
+ // src/PluvPlatform.ts
45
+ import { AbstractPlatform } from "@pluv/io";
45
46
  import { Hono } from "hono";
46
- import { handle } from "hono/vercel";
47
47
 
48
48
  // src/constants.ts
49
49
  var SIGNATURE_HEADER = "x-pluv-signature-256";
@@ -142,11 +142,50 @@ var verifyWebhook = (params) => __async(void 0, null, function* () {
142
142
  return timingSafeEqual(verificationBytes, signatureBytes);
143
143
  });
144
144
 
145
- // src/PluvIO.ts
146
- var PluvIO = class {
147
- constructor(options) {
148
- this._webhooks = new Hono().basePath("/").post("/event", (c) => __async(this, null, function* () {
149
- var _a, _b;
145
+ // src/PluvPlatform.ts
146
+ var PluvPlatform = class extends AbstractPlatform {
147
+ constructor(params) {
148
+ super();
149
+ this._config = {
150
+ authorize: {
151
+ required: false,
152
+ secret: false
153
+ },
154
+ handleMode: "fetch",
155
+ registrationMode: "attached",
156
+ requireAuth: true,
157
+ listeners: {
158
+ onRoomDeleted: true,
159
+ onRoomMessage: false,
160
+ onStorageUpdated: false,
161
+ onUserConnected: true,
162
+ onUserDisconnected: true
163
+ }
164
+ };
165
+ this._name = "platformPluv";
166
+ this._createToken = (params) => __async(this, null, function* () {
167
+ var _a;
168
+ const parsed = this._authorize.user.parse(params.user);
169
+ const res = yield fetch(this._endpoints.createToken, {
170
+ headers: { "content-type": "application/json" },
171
+ method: "post",
172
+ body: JSON.stringify({
173
+ maxAge: (_a = params.maxAge) != null ? _a : null,
174
+ publicKey: this._publicKey,
175
+ room: params.room,
176
+ secretKey: this._secretKey,
177
+ user: parsed
178
+ })
179
+ }).catch(() => null);
180
+ if (!res || !res.ok || res.status !== 200) {
181
+ throw new Error("Authorization failed");
182
+ }
183
+ const token = yield res.text().catch(() => null);
184
+ if (typeof token !== "string") throw new Error("Authorization failed");
185
+ return token;
186
+ });
187
+ this._webhooks = new Hono().basePath("/").post("/", (c) => __async(this, null, function* () {
188
+ var _a, _b, _c, _d, _e;
150
189
  const signature = c.req.header(SIGNATURE_HEADER);
151
190
  if (!this._webhookSecret || !signature) return c.json({ error: "Unauthorized" }, 401);
152
191
  const payload = yield c.req.json();
@@ -168,109 +207,87 @@ var PluvIO = class {
168
207
  case "room-deleted": {
169
208
  const room = data.room;
170
209
  const encodedState = data.storage;
171
- yield Promise.resolve(this._listeners.onRoomDeleted({ encodedState, room }));
210
+ yield Promise.resolve((_c = this._listeners) == null ? void 0 : _c.onRoomDeleted({ encodedState, room }));
172
211
  return c.json({ data: { room } }, 200);
173
212
  }
174
213
  case "user-connected": {
175
214
  const room = data.room;
176
215
  const encodedState = data.storage;
177
216
  const user = data.user;
178
- yield Promise.resolve(this._listeners.onUserConnected({ encodedState, room, user }));
217
+ yield Promise.resolve((_d = this._listeners) == null ? void 0 : _d.onUserConnected({ encodedState, room, user }));
179
218
  }
180
219
  case "user-disconnected": {
181
220
  const room = data.room;
182
221
  const encodedState = data.storage;
183
222
  const user = data.user;
184
- yield Promise.resolve(this._listeners.onUserDisconnected({ encodedState, room, user }));
223
+ yield Promise.resolve((_e = this._listeners) == null ? void 0 : _e.onUserDisconnected({ encodedState, room, user }));
185
224
  }
186
225
  default:
187
226
  return c.json({ data: { ok: true } }, 200);
188
227
  }
189
228
  }));
190
- const {
191
- _defs,
192
- authorize,
193
- basePath,
194
- getInitialStorage,
195
- onRoomDeleted,
196
- onUserConnected,
197
- onUserDisconnected,
198
- publicKey,
199
- secretKey,
200
- webhookSecret
201
- } = options;
202
- this._authorize = {
203
- required: true,
204
- secret: "",
205
- user: authorize.user
206
- };
229
+ const { _defs, basePath, publicKey, secretKey, webhookSecret } = params;
207
230
  this._basePath = basePath;
208
231
  this._endpoints = __spreadValues({
209
232
  createToken: "https://pluv.io/api/room/token"
210
233
  }, _defs == null ? void 0 : _defs.endpoints);
211
- this._getInitialStorage = getInitialStorage;
212
- this._listeners = {
213
- onRoomDeleted: (event) => onRoomDeleted == null ? void 0 : onRoomDeleted(event),
214
- onUserConnected: (event) => onUserConnected == null ? void 0 : onUserConnected(event),
215
- onUserDisconnected: (event) => onUserDisconnected == null ? void 0 : onUserDisconnected(event)
216
- };
217
234
  this._publicKey = publicKey;
218
235
  this._secretKey = secretKey;
219
236
  this._webhookSecret = webhookSecret;
237
+ this._fetch = new Hono().basePath(this._basePath).route("/", this._webhooks).fetch;
220
238
  }
221
- /**
222
- * @ignore
223
- * @readonly
224
- * @deprecated Internal use only. Changes to this will never be marked as breaking.
225
- */
226
- get _defs() {
227
- return {
228
- authorize: this._authorize,
229
- context: {},
230
- events: {},
231
- get platform() {
232
- throw new Error("Invalid platform reference");
233
- }
234
- };
239
+ acceptWebSocket(webSocket) {
240
+ throw new Error("Not implemented");
235
241
  }
236
- get fetch() {
237
- const app = new Hono().basePath(this._basePath).route("/", this._webhooks);
238
- return app.fetch;
242
+ convertWebSocket(webSocket, config) {
243
+ throw new Error("Not implemented");
239
244
  }
240
- get handler() {
241
- const app = new Hono().basePath(this._basePath).route("/", this._webhooks);
242
- return handle(app);
245
+ getLastPing(webSocket) {
246
+ throw new Error("Not implemented");
243
247
  }
244
- createToken(params) {
245
- return __async(this, null, function* () {
246
- var _a;
247
- const parsed = this._authorize.user.parse(params.user);
248
- const res = yield fetch(this._endpoints.createToken, {
249
- headers: { "content-type": "application/json" },
250
- method: "post",
251
- body: JSON.stringify({
252
- maxAge: (_a = params.maxAge) != null ? _a : null,
253
- publicKey: this._publicKey,
254
- room: params.room,
255
- secretKey: this._secretKey,
256
- user: parsed
257
- })
258
- }).catch(() => null);
259
- if (!res || !res.ok || res.status !== 200) {
260
- throw new Error("Authorization failed");
261
- }
262
- const token = yield res.text().catch(() => null);
263
- if (typeof token !== "string") throw new Error("Authorization failed");
264
- return token;
265
- });
248
+ getSerializedState(webSocket) {
249
+ throw new Error("Not implemented");
250
+ }
251
+ getSessionId(webSocket) {
252
+ throw new Error("Not implemented");
253
+ }
254
+ getWebSockets() {
255
+ throw new Error("Not implemented");
256
+ }
257
+ initialize(config) {
258
+ throw new Error("Not implemented");
259
+ }
260
+ parseData(data) {
261
+ throw new Error("Not implemented");
262
+ }
263
+ randomUUID() {
264
+ throw new Error("Not implemented");
265
+ }
266
+ setSerializedState(webSocket, state) {
267
+ throw new Error("Not implemented");
268
+ }
269
+ validateConfig(config) {
270
+ if (!config.authorize) throw new Error("Config `authorize` must be provided to `platformPluv`");
271
+ if (!!config.onRoomMessage) throw new Error("Config `onRoomMessage` is not supported on `platformPluv`");
272
+ if (!!config.onStorageUpdated) throw new Error("Config `onStorageUpdated` is not supported on `platformPluv`");
273
+ if (!!config.authorize.required) {
274
+ throw new Error("Config `authorize.required` is not allowed to be false on `platformPluv`");
275
+ }
276
+ this._authorize = config.authorize;
277
+ this._getInitialStorage = config.getInitialStorage;
278
+ this._listeners = {
279
+ onRoomDeleted: config.onRoomDeleted,
280
+ onUserConnected: config.onUserConnected,
281
+ onUserDisconnected: config.onUserDisconnected
282
+ };
266
283
  }
267
284
  };
268
285
 
269
- // src/createIO.ts
270
- var createIO = (config) => {
271
- return new PluvIO(config);
286
+ // src/platformPluv.ts
287
+ var platformPluv = (config) => {
288
+ return new PluvPlatform(config);
272
289
  };
273
290
  export {
274
- ZodEvent,
275
- createIO
291
+ PluvPlatform,
292
+ platformPluv
276
293
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pluv/platform-pluv",
3
- "version": "0.32.9",
3
+ "version": "0.34.0",
4
4
  "description": "@pluv/io adapter for pluv.io",
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.8.4",
21
- "hono": "^4.6.8",
22
- "zod": "^3.23.8",
23
- "@pluv/crdt": "^0.32.9",
24
- "@pluv/io": "^0.32.9",
25
- "@pluv/types": "^0.32.9"
20
+ "@types/node": "^22.10.2",
21
+ "hono": "^4.6.14",
22
+ "zod": "^3.24.1",
23
+ "@pluv/crdt": "^0.34.0",
24
+ "@pluv/io": "^0.34.0",
25
+ "@pluv/types": "^0.34.0"
26
26
  },
27
27
  "devDependencies": {
28
- "eslint": "^8.57.0",
28
+ "eslint": "^8.57.1",
29
29
  "tsup": "^8.3.5",
30
- "typescript": "^5.6.3",
31
- "@pluv/tsconfig": "^0.32.9",
32
- "eslint-config-pluv": "^0.32.9"
30
+ "typescript": "^5.7.2",
31
+ "@pluv/tsconfig": "^0.34.0",
32
+ "eslint-config-pluv": "^0.34.0"
33
33
  },
34
34
  "scripts": {
35
35
  "build": "tsup src/index.ts --format esm,cjs --dts",
@@ -1,14 +1,125 @@
1
- import {
2
- AbstractPlatform,
1
+ import type {
3
2
  AbstractPlatformConfig,
4
3
  AbstractWebSocket,
5
4
  ConvertWebSocketConfig,
6
- WebSocketRegistrationMode,
5
+ GetInitialStorageFn,
6
+ JWTEncodeParams,
7
7
  WebSocketSerializedState,
8
8
  } from "@pluv/io";
9
+ import { AbstractPlatform, authorize } from "@pluv/io";
10
+ import { Hono } from "hono";
11
+ import { SIGNATURE_HEADER } from "./constants";
12
+ import { ZodEvent } from "./schemas";
13
+ import { verifyWebhook } from "./shared";
14
+ import type { PluvIOEndpoints, PluvIOListeners } from "./types";
9
15
 
10
- export class PluvPlatform extends AbstractPlatform {
11
- _registrationMode: WebSocketRegistrationMode = "attached";
16
+ export interface PluvPlatformConfig {
17
+ /**
18
+ * @ignore
19
+ * @readonly
20
+ * @deprecated Internal use only. Changes to this will never be marked as breaking.
21
+ */
22
+ _defs?: {
23
+ endpoints?: PluvIOEndpoints;
24
+ };
25
+ basePath: string;
26
+ publicKey: string;
27
+ secretKey: string;
28
+ webhookSecret?: string;
29
+ }
30
+
31
+ export class PluvPlatform extends AbstractPlatform<
32
+ any,
33
+ {},
34
+ {},
35
+ {
36
+ authorize: {
37
+ required: false;
38
+ secret: false;
39
+ };
40
+ handleMode: "fetch";
41
+ registrationMode: "attached";
42
+ requireAuth: true;
43
+ listeners: {
44
+ onRoomDeleted: true;
45
+ onRoomMessage: false;
46
+ onStorageUpdated: false;
47
+ onUserConnected: true;
48
+ onUserDisconnected: true;
49
+ };
50
+ }
51
+ > {
52
+ public readonly _config = {
53
+ authorize: {
54
+ required: false as const,
55
+ secret: false as const,
56
+ },
57
+ handleMode: "fetch" as const,
58
+ registrationMode: "attached" as const,
59
+ requireAuth: true as const,
60
+ listeners: {
61
+ onRoomDeleted: true as const,
62
+ onRoomMessage: false as const,
63
+ onStorageUpdated: false as const,
64
+ onUserConnected: true as const,
65
+ onUserDisconnected: true as const,
66
+ },
67
+ };
68
+ public readonly _name = "platformPluv";
69
+
70
+ private _authorize: any;
71
+ private readonly _basePath: string;
72
+ private readonly _endpoints: PluvIOEndpoints;
73
+ private _getInitialStorage?: GetInitialStorageFn<{}>;
74
+ private _listeners?: PluvIOListeners;
75
+ private readonly _publicKey: string;
76
+ private readonly _secretKey: string;
77
+ private readonly _webhookSecret?: string;
78
+
79
+ public _createToken = async (params: JWTEncodeParams<any, any>): Promise<string> => {
80
+ const parsed = this._authorize.user.parse(params.user);
81
+
82
+ const res = await fetch(this._endpoints.createToken, {
83
+ headers: { "content-type": "application/json" },
84
+ method: "post",
85
+ body: JSON.stringify({
86
+ maxAge: params.maxAge ?? null,
87
+ publicKey: this._publicKey,
88
+ room: params.room,
89
+ secretKey: this._secretKey,
90
+ user: parsed,
91
+ }),
92
+ }).catch(() => null);
93
+
94
+ if (!res || !res.ok || res.status !== 200) {
95
+ throw new Error("Authorization failed");
96
+ }
97
+
98
+ const token = await res.text().catch(() => null);
99
+
100
+ if (typeof token !== "string") throw new Error("Authorization failed");
101
+
102
+ return token;
103
+ };
104
+
105
+ constructor(params: PluvPlatformConfig) {
106
+ super();
107
+
108
+ const { _defs, basePath, publicKey, secretKey, webhookSecret } = params;
109
+
110
+ this._basePath = basePath;
111
+ this._endpoints = {
112
+ createToken: "https://pluv.io/api/room/token",
113
+ ..._defs?.endpoints,
114
+ };
115
+ this._publicKey = publicKey;
116
+ this._secretKey = secretKey;
117
+ this._webhookSecret = webhookSecret;
118
+
119
+ this._fetch = new Hono().basePath(this._basePath).route("/", this._webhooks).fetch as (
120
+ req: any,
121
+ ) => Promise<any>;
122
+ }
12
123
 
13
124
  public acceptWebSocket(webSocket: AbstractWebSocket): Promise<void> {
14
125
  throw new Error("Not implemented");
@@ -49,4 +160,85 @@ export class PluvPlatform extends AbstractPlatform {
49
160
  public setSerializedState(webSocket: AbstractWebSocket, state: WebSocketSerializedState): void {
50
161
  throw new Error("Not implemented");
51
162
  }
163
+
164
+ public validateConfig(config: any): void {
165
+ if (!config.authorize) throw new Error("Config `authorize` must be provided to `platformPluv`");
166
+ if (!!config.onRoomMessage) throw new Error("Config `onRoomMessage` is not supported on `platformPluv`");
167
+ if (!!config.onStorageUpdated) throw new Error("Config `onStorageUpdated` is not supported on `platformPluv`");
168
+ if (!!config.authorize.required) {
169
+ throw new Error("Config `authorize.required` is not allowed to be false on `platformPluv`");
170
+ }
171
+
172
+ /**
173
+ * !HACK
174
+ * @description Assign these on the validation step, because we know this will happen on the
175
+ * server constructor internally.
176
+ * @date December 16, 2024
177
+ */
178
+ this._authorize = config.authorize;
179
+ this._getInitialStorage = config.getInitialStorage;
180
+ this._listeners = {
181
+ onRoomDeleted: config.onRoomDeleted,
182
+ onUserConnected: config.onUserConnected,
183
+ onUserDisconnected: config.onUserDisconnected,
184
+ };
185
+ }
186
+
187
+ private _webhooks = new Hono().basePath("/").post("/", async (c) => {
188
+ const signature = c.req.header(SIGNATURE_HEADER);
189
+
190
+ if (!this._webhookSecret || !signature) return c.json({ error: "Unauthorized" }, 401);
191
+
192
+ const payload = await c.req.json();
193
+
194
+ const verified = await verifyWebhook({
195
+ payload,
196
+ signature,
197
+ secret: this._webhookSecret,
198
+ });
199
+
200
+ if (!verified) return c.json({ error: "Unauthorized" }, 401);
201
+
202
+ const parsed = ZodEvent.safeParse(payload);
203
+
204
+ if (!parsed.success) return c.json({ data: { ok: true } }, 200);
205
+
206
+ const { event, data } = parsed.data;
207
+
208
+ switch (event) {
209
+ case "initial-storage": {
210
+ const room = data.room;
211
+ const storage =
212
+ typeof room === "string"
213
+ ? ((await this._getInitialStorage?.({ context: {}, room })) ?? null)
214
+ : null;
215
+
216
+ return c.json({ data: { storage } }, 200);
217
+ }
218
+ case "room-deleted": {
219
+ const room = data.room;
220
+ const encodedState = data.storage;
221
+
222
+ await Promise.resolve(this._listeners?.onRoomDeleted({ encodedState, room }));
223
+
224
+ return c.json({ data: { room } }, 200);
225
+ }
226
+ case "user-connected": {
227
+ const room = data.room;
228
+ const encodedState = data.storage;
229
+ const user = data.user as any;
230
+
231
+ await Promise.resolve(this._listeners?.onUserConnected({ encodedState, room, user }));
232
+ }
233
+ case "user-disconnected": {
234
+ const room = data.room;
235
+ const encodedState = data.storage;
236
+ const user = data.user as any;
237
+
238
+ await Promise.resolve(this._listeners?.onUserDisconnected({ encodedState, room, user }));
239
+ }
240
+ default:
241
+ return c.json({ data: { ok: true } }, 200);
242
+ }
243
+ });
52
244
  }
package/src/index.ts CHANGED
@@ -1,4 +1,8 @@
1
- export { createIO } from "./createIO";
2
- export type { PluvIO, PluvIOConfig } from "./PluvIO";
3
- export type { PluvPlatform } from "./PluvPlatform";
4
- export { ZodEvent } from "./schemas";
1
+ export { platformPluv } from "./platformPluv";
2
+ export { PluvPlatform } from "./PluvPlatform";
3
+ export type {
4
+ PluvIOEndpoints,
5
+ RoomDeletedMessageEventData,
6
+ UserConnectedEventData,
7
+ UserDisconnectedEventData,
8
+ } from "./types";
@@ -0,0 +1,6 @@
1
+ import type { PluvPlatformConfig } from "./PluvPlatform";
2
+ import { PluvPlatform } from "./PluvPlatform";
3
+
4
+ export const platformPluv = (config: PluvPlatformConfig) => {
5
+ return new PluvPlatform(config);
6
+ };
package/src/types.ts ADDED
@@ -0,0 +1,29 @@
1
+ import type { GetInitialStorageFn } from "@pluv/io";
2
+
3
+ export interface PluvIOEndpoints {
4
+ createToken: string;
5
+ }
6
+
7
+ export type RoomDeletedMessageEventData = {
8
+ encodedState: string | null;
9
+ room: string;
10
+ };
11
+
12
+ export type UserConnectedEventData = {
13
+ encodedState: string | null;
14
+ room: string;
15
+ user: any;
16
+ };
17
+
18
+ export type UserDisconnectedEventData = {
19
+ encodedState: string | null;
20
+ room: string;
21
+ user: any;
22
+ };
23
+
24
+ export type PluvIOListeners = {
25
+ getInitialStorage?: GetInitialStorageFn<{}>;
26
+ onRoomDeleted: (event: RoomDeletedMessageEventData) => void;
27
+ onUserConnected: (event: UserConnectedEventData) => void;
28
+ onUserDisconnected: (event: UserDisconnectedEventData) => void;
29
+ };