@pluv/io 3.1.1 → 3.1.3

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,9 +1,9 @@
1
1
 
2
- > @pluv/io@3.1.1 build /home/runner/work/pluv/pluv/packages/io
2
+ > @pluv/io@3.1.3 build /home/runner/work/pluv/pluv/packages/io
3
3
  > pnpm run version && tsup src/index.ts --format esm,cjs --dts
4
4
 
5
5
 
6
- > @pluv/io@3.1.1 version /home/runner/work/pluv/pluv/packages/io
6
+ > @pluv/io@3.1.3 version /home/runner/work/pluv/pluv/packages/io
7
7
  > genversion ./src/version.generated.ts --esm
8
8
 
9
9
  CLI Building entry: src/index.ts
@@ -12,11 +12,11 @@
12
12
  CLI Target: es6
13
13
  ESM Build start
14
14
  CJS Build start
15
- ESM dist/index.mjs 52.51 KB
16
- ESM ⚡️ Build success in 116ms
17
- CJS dist/index.js 54.68 KB
18
- CJS ⚡️ Build success in 119ms
15
+ CJS dist/index.js 55.16 KB
16
+ CJS ⚡️ Build success in 168ms
17
+ ESM dist/index.mjs 52.98 KB
18
+ ESM ⚡️ Build success in 169ms
19
19
  DTS Build start
20
- DTS ⚡️ Build success in 4316ms
21
- DTS dist/index.d.mts 33.94 KB
22
- DTS dist/index.d.ts 33.94 KB
20
+ DTS ⚡️ Build success in 4308ms
21
+ DTS dist/index.d.mts 33.98 KB
22
+ DTS dist/index.d.ts 33.98 KB
package/CHANGELOG.md CHANGED
@@ -1,5 +1,21 @@
1
1
  # @pluv/io
2
2
 
3
+ ## 3.1.3
4
+
5
+ ### Patch Changes
6
+
7
+ - 651a9f8: Fixed applying `getInitialStorage` if the initial storage state is empty.
8
+ - @pluv/crdt@3.1.3
9
+ - @pluv/types@3.1.3
10
+
11
+ ## 3.1.2
12
+
13
+ ### Patch Changes
14
+
15
+ - 8f138aa: Fixed `getInitialStorage` not being applied.
16
+ - @pluv/crdt@3.1.2
17
+ - @pluv/types@3.1.2
18
+
3
19
  ## 3.1.1
4
20
 
5
21
  ### Patch Changes
package/README.md CHANGED
@@ -38,9 +38,19 @@ Create your pluv.io backend
38
38
 
39
39
  ```ts
40
40
  // backend
41
- const io = createIO(platformNode({ crdt: yjs }));
41
+ const io = createIO(
42
+ platformNode({
43
+ context: () => ({ db }),
44
+ crdt: yjs,
45
+ })
46
+ );
42
47
 
43
48
  export const ioServer = io.server({
49
+ getInitialStorage: async ({ context: { db }, room }) => {
50
+ return await db.room
51
+ .findUnique({ where: { id: room } })
52
+ .then((result) => result?.encodedState ?? null);
53
+ },
44
54
  router: io.router({
45
55
  sendGreeting: io.procedure
46
56
  .input(z.object({ message: z.string() }))
package/dist/index.d.mts CHANGED
@@ -477,6 +477,7 @@ declare class PluvServer<TPlatform extends AbstractPlatform<any, any> = Abstract
477
477
  constructor(options: PluvServerConfig<TPlatform, TAuthorize, TContext, TCrdt, TEvents>);
478
478
  createRoom(room: string, ...options: CreateRoomOptions<TPlatform, TAuthorize, TContext, TEvents>): IORoom<TPlatform, TAuthorize, TContext, TCrdt, TEvents>;
479
479
  createToken(params: JWTEncodeParams<InferIOAuthorizeUser<InferIOAuthorize<this>>, TPlatform>): Promise<string>;
480
+ private _getInitialStorage;
480
481
  private _getListeners;
481
482
  private _logDebug;
482
483
  }
package/dist/index.d.ts CHANGED
@@ -477,6 +477,7 @@ declare class PluvServer<TPlatform extends AbstractPlatform<any, any> = Abstract
477
477
  constructor(options: PluvServerConfig<TPlatform, TAuthorize, TContext, TCrdt, TEvents>);
478
478
  createRoom(room: string, ...options: CreateRoomOptions<TPlatform, TAuthorize, TContext, TEvents>): IORoom<TPlatform, TAuthorize, TContext, TCrdt, TEvents>;
479
479
  createToken(params: JWTEncodeParams<InferIOAuthorizeUser<InferIOAuthorize<this>>, TPlatform>): Promise<string>;
480
+ private _getInitialStorage;
480
481
  private _getListeners;
481
482
  private _logDebug;
482
483
  }
package/dist/index.js CHANGED
@@ -1170,12 +1170,17 @@ var IORoom = class {
1170
1170
  };
1171
1171
 
1172
1172
  // src/version.generated.ts
1173
- var version = "3.1.1";
1173
+ var version = "3.1.3";
1174
1174
 
1175
1175
  // src/PluvServer.ts
1176
1176
  var PluvServer = class {
1177
1177
  constructor(options) {
1178
1178
  this.version = version;
1179
+ this._getInitialStorage = (...args) => {
1180
+ var _a;
1181
+ const getInitialStorage = typeof this._config.getInitialStorage !== "string" ? (_a = this._config.getInitialStorage) != null ? _a : () => null : () => null;
1182
+ return getInitialStorage(...args);
1183
+ };
1179
1184
  this._config = __spreadValues({
1180
1185
  crdt: import_crdt2.noop,
1181
1186
  debug: false,
@@ -1261,9 +1266,11 @@ var PluvServer = class {
1261
1266
  const oldState = yield platform.persistence.getStorageState(room);
1262
1267
  const update = data == null ? void 0 : data.update;
1263
1268
  if (!oldState) {
1264
- const getInitialStorage = typeof this._config.getInitialStorage === "function" ? this._config.getInitialStorage : () => null;
1265
- const loadedState = yield getInitialStorage({ context, room });
1266
- if (!!loadedState) {
1269
+ const loadedState = yield this._getInitialStorage({ context, room });
1270
+ const checkDoc = this._config.crdt.doc(() => ({})).getEmpty().applyEncodedState({ update: loadedState });
1271
+ const isEmpty = checkDoc.isEmpty();
1272
+ checkDoc.destroy();
1273
+ if (!!loadedState && !isEmpty) {
1267
1274
  doc.applyEncodedState({ update: loadedState }).getEncodedState();
1268
1275
  }
1269
1276
  }
@@ -1462,15 +1469,17 @@ var PluvIO = class {
1462
1469
  const user = params.user;
1463
1470
  const parsed = !!ioAuthorize ? ioAuthorize.user.parse(user) : user;
1464
1471
  if (!!this._limits.userIdMaxLength && user.id.length > this._limits.userIdMaxLength) {
1465
- throw new Error(
1466
- `createToken was called with a long user id. User ID must be at most 128 characters. Current length: ${user.id.length.toLocaleString()}`
1467
- );
1472
+ throw new Error(oneLine`
1473
+ createToken was called with a long user id. User ID must be at
1474
+ most 128 characters. Current length: ${user.id.length.toLocaleString()}
1475
+ `);
1468
1476
  }
1469
1477
  const bytes = new TextEncoder().encode(JSON.stringify(parsed)).length;
1470
1478
  if (!!this._limits.userMaxSize && bytes > this._limits.userMaxSize) {
1471
- throw new Error(
1472
- `createToken called with large payload. User must be at most 512 bytes. Current size: ${bytes.toLocaleString()}`
1473
- );
1479
+ throw new Error(oneLine`
1480
+ createToken called with large payload. User must be at most 512
1481
+ bytes. Current size: ${bytes.toLocaleString()}
1482
+ `);
1474
1483
  }
1475
1484
  const platform = this._platform();
1476
1485
  if (platform._createToken) {
@@ -1489,13 +1498,16 @@ var PluvIO = class {
1489
1498
  router(events) {
1490
1499
  const invalidName = Object.keys(events).find((name) => name.includes("$"));
1491
1500
  if (typeof invalidName === "string") {
1492
- throw new Error(`Invalid event name. Event names must not contain $: "${invalidName}"`);
1501
+ throw new Error(oneLine`
1502
+ Invalid event name. Event names must not contain $: "${invalidName}"
1503
+ `);
1493
1504
  }
1494
1505
  return new PluvRouter(events);
1495
1506
  }
1496
1507
  server(...config) {
1497
1508
  var _a, _b;
1498
- return new PluvServer(__spreadProps(__spreadValues({}, (_a = config[0]) != null ? _a : {}), {
1509
+ const serverConfig = (_a = config[0]) != null ? _a : {};
1510
+ return new PluvServer(__spreadProps(__spreadValues({}, serverConfig), {
1499
1511
  authorize: (_b = this._authorize) != null ? _b : void 0,
1500
1512
  context: this._context,
1501
1513
  crdt: this._crdt,
package/dist/index.mjs CHANGED
@@ -1128,12 +1128,17 @@ var IORoom = class {
1128
1128
  };
1129
1129
 
1130
1130
  // src/version.generated.ts
1131
- var version = "3.1.1";
1131
+ var version = "3.1.3";
1132
1132
 
1133
1133
  // src/PluvServer.ts
1134
1134
  var PluvServer = class {
1135
1135
  constructor(options) {
1136
1136
  this.version = version;
1137
+ this._getInitialStorage = (...args) => {
1138
+ var _a;
1139
+ const getInitialStorage = typeof this._config.getInitialStorage !== "string" ? (_a = this._config.getInitialStorage) != null ? _a : () => null : () => null;
1140
+ return getInitialStorage(...args);
1141
+ };
1137
1142
  this._config = __spreadValues({
1138
1143
  crdt: noop2,
1139
1144
  debug: false,
@@ -1219,9 +1224,11 @@ var PluvServer = class {
1219
1224
  const oldState = yield platform.persistence.getStorageState(room);
1220
1225
  const update = data == null ? void 0 : data.update;
1221
1226
  if (!oldState) {
1222
- const getInitialStorage = typeof this._config.getInitialStorage === "function" ? this._config.getInitialStorage : () => null;
1223
- const loadedState = yield getInitialStorage({ context, room });
1224
- if (!!loadedState) {
1227
+ const loadedState = yield this._getInitialStorage({ context, room });
1228
+ const checkDoc = this._config.crdt.doc(() => ({})).getEmpty().applyEncodedState({ update: loadedState });
1229
+ const isEmpty = checkDoc.isEmpty();
1230
+ checkDoc.destroy();
1231
+ if (!!loadedState && !isEmpty) {
1225
1232
  doc.applyEncodedState({ update: loadedState }).getEncodedState();
1226
1233
  }
1227
1234
  }
@@ -1420,15 +1427,17 @@ var PluvIO = class {
1420
1427
  const user = params.user;
1421
1428
  const parsed = !!ioAuthorize ? ioAuthorize.user.parse(user) : user;
1422
1429
  if (!!this._limits.userIdMaxLength && user.id.length > this._limits.userIdMaxLength) {
1423
- throw new Error(
1424
- `createToken was called with a long user id. User ID must be at most 128 characters. Current length: ${user.id.length.toLocaleString()}`
1425
- );
1430
+ throw new Error(oneLine`
1431
+ createToken was called with a long user id. User ID must be at
1432
+ most 128 characters. Current length: ${user.id.length.toLocaleString()}
1433
+ `);
1426
1434
  }
1427
1435
  const bytes = new TextEncoder().encode(JSON.stringify(parsed)).length;
1428
1436
  if (!!this._limits.userMaxSize && bytes > this._limits.userMaxSize) {
1429
- throw new Error(
1430
- `createToken called with large payload. User must be at most 512 bytes. Current size: ${bytes.toLocaleString()}`
1431
- );
1437
+ throw new Error(oneLine`
1438
+ createToken called with large payload. User must be at most 512
1439
+ bytes. Current size: ${bytes.toLocaleString()}
1440
+ `);
1432
1441
  }
1433
1442
  const platform = this._platform();
1434
1443
  if (platform._createToken) {
@@ -1447,13 +1456,16 @@ var PluvIO = class {
1447
1456
  router(events) {
1448
1457
  const invalidName = Object.keys(events).find((name) => name.includes("$"));
1449
1458
  if (typeof invalidName === "string") {
1450
- throw new Error(`Invalid event name. Event names must not contain $: "${invalidName}"`);
1459
+ throw new Error(oneLine`
1460
+ Invalid event name. Event names must not contain $: "${invalidName}"
1461
+ `);
1451
1462
  }
1452
1463
  return new PluvRouter(events);
1453
1464
  }
1454
1465
  server(...config) {
1455
1466
  var _a, _b;
1456
- return new PluvServer(__spreadProps(__spreadValues({}, (_a = config[0]) != null ? _a : {}), {
1467
+ const serverConfig = (_a = config[0]) != null ? _a : {};
1468
+ return new PluvServer(__spreadProps(__spreadValues({}, serverConfig), {
1457
1469
  authorize: (_b = this._authorize) != null ? _b : void 0,
1458
1470
  context: this._context,
1459
1471
  crdt: this._crdt,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pluv/io",
3
- "version": "3.1.1",
3
+ "version": "3.1.3",
4
4
  "description": "@pluv/io server-side websocket manager",
5
5
  "author": "leedavidcs",
6
6
  "license": "MIT",
@@ -21,16 +21,16 @@
21
21
  "jose": "^5.10.0",
22
22
  "kleur": "^4.1.5",
23
23
  "wonka": "^6.3.5",
24
- "@pluv/crdt": "^3.1.1",
25
- "@pluv/types": "^3.1.1"
24
+ "@pluv/crdt": "^3.1.3",
25
+ "@pluv/types": "^3.1.3"
26
26
  },
27
27
  "devDependencies": {
28
28
  "eslint": "^9.27.0",
29
29
  "genversion": "^3.2.0",
30
30
  "tsup": "^8.5.0",
31
31
  "typescript": "^5.8.3",
32
- "@pluv/tsconfig": "^3.1.1",
33
- "eslint-config-pluv": "^3.1.1"
32
+ "@pluv/tsconfig": "^3.1.3",
33
+ "eslint-config-pluv": "^3.1.3"
34
34
  },
35
35
  "scripts": {
36
36
  "build": "pnpm run version && tsup src/index.ts --format esm,cjs --dts",
package/src/PluvIO.ts CHANGED
@@ -24,6 +24,7 @@ import type {
24
24
  PluvIORouter,
25
25
  ResolvedPluvIOAuthorize,
26
26
  } from "./types";
27
+ import { oneLine } from "./utils";
27
28
  import { __PLUV_VERSION } from "./version";
28
29
 
29
30
  export type PluvIOConfig<
@@ -151,17 +152,19 @@ export class PluvIO<
151
152
  const parsed = !!ioAuthorize ? ioAuthorize.user.parse(user) : user;
152
153
 
153
154
  if (!!this._limits.userIdMaxLength && user.id.length > this._limits.userIdMaxLength) {
154
- throw new Error(
155
- `createToken was called with a long user id. User ID must be at most 128 characters. Current length: ${user.id.length.toLocaleString()}`,
156
- );
155
+ throw new Error(oneLine`
156
+ createToken was called with a long user id. User ID must be at
157
+ most 128 characters. Current length: ${user.id.length.toLocaleString()}
158
+ `);
157
159
  }
158
160
 
159
161
  const bytes = new TextEncoder().encode(JSON.stringify(parsed)).length;
160
162
 
161
163
  if (!!this._limits.userMaxSize && bytes > this._limits.userMaxSize) {
162
- throw new Error(
163
- `createToken called with large payload. User must be at most 512 bytes. Current size: ${bytes.toLocaleString()}`,
164
- );
164
+ throw new Error(oneLine`
165
+ createToken called with large payload. User must be at most 512
166
+ bytes. Current size: ${bytes.toLocaleString()}
167
+ `);
165
168
  }
166
169
 
167
170
  const platform = this._platform();
@@ -197,7 +200,9 @@ export class PluvIO<
197
200
  const invalidName = Object.keys(events).find((name) => name.includes("$"));
198
201
 
199
202
  if (typeof invalidName === "string") {
200
- throw new Error(`Invalid event name. Event names must not contain $: "${invalidName}"`);
203
+ throw new Error(oneLine`
204
+ Invalid event name. Event names must not contain $: "${invalidName}"
205
+ `);
201
206
  }
202
207
 
203
208
  return new PluvRouter<TPlatform, TAuthorize, TContext, TEvents>(events);
@@ -206,14 +211,16 @@ export class PluvIO<
206
211
  public server<TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext> = {}>(
207
212
  ...config: ServerConfig<TPlatform, TAuthorize, TContext, TCrdt, TEvents>
208
213
  ): PluvServer<TPlatform, TAuthorize, TContext, TCrdt, TEvents> {
214
+ const serverConfig = (config[0] ?? {}) as PluvServerConfig<
215
+ TPlatform,
216
+ TAuthorize,
217
+ TContext,
218
+ TCrdt,
219
+ TEvents
220
+ >;
221
+
209
222
  return new PluvServer<TPlatform, TAuthorize, TContext, TCrdt, TEvents>({
210
- ...((config[0] ?? {}) as PluvServerConfig<
211
- TPlatform,
212
- TAuthorize,
213
- TContext,
214
- TCrdt,
215
- TEvents
216
- >),
223
+ ...serverConfig,
217
224
  authorize: this._authorize ?? undefined,
218
225
  context: this._context,
219
226
  crdt: this._crdt,
package/src/PluvServer.ts CHANGED
@@ -209,13 +209,16 @@ export class PluvServer<
209
209
  const update = (data as any)?.update as Maybe<string>;
210
210
 
211
211
  if (!oldState) {
212
- const getInitialStorage: GetInitialStorageFn<TContext> =
213
- typeof this._config.getInitialStorage === "function"
214
- ? this._config.getInitialStorage
215
- : () => null;
216
- const loadedState = await getInitialStorage({ context, room });
212
+ const loadedState = await this._getInitialStorage({ context, room });
217
213
 
218
- if (!!loadedState) {
214
+ const checkDoc = this._config.crdt
215
+ .doc(() => ({}))
216
+ .getEmpty()
217
+ .applyEncodedState({ update: loadedState });
218
+ const isEmpty = checkDoc.isEmpty();
219
+ checkDoc.destroy();
220
+
221
+ if (!!loadedState && !isEmpty) {
219
222
  doc.applyEncodedState({ update: loadedState }).getEncodedState();
220
223
  }
221
224
  }
@@ -463,6 +466,16 @@ export class PluvServer<
463
466
  return await this._config.io.createToken(params);
464
467
  }
465
468
 
469
+ private _getInitialStorage: GetInitialStorageFn<TContext> = (...args) => {
470
+ const getInitialStorage =
471
+ typeof this._config.getInitialStorage !== "string"
472
+ ? (this._config.getInitialStorage ??
473
+ ((() => null) as GetInitialStorageFn<TContext>))
474
+ : ((() => null) as GetInitialStorageFn<TContext>);
475
+
476
+ return getInitialStorage(...args);
477
+ };
478
+
466
479
  private _getListeners(): BasePluvIOListeners<TPlatform, TAuthorize, TContext, TEvents> {
467
480
  return (this as any)._listeners;
468
481
  }
@@ -1,2 +1,2 @@
1
1
  // Generated by genversion.
2
- export const version = '3.1.1'
2
+ export const version = '3.1.3'