@pluv/io 3.1.0 → 3.1.2

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.0 build /home/runner/work/pluv/pluv/packages/io
2
+ > @pluv/io@3.1.2 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.0 version /home/runner/work/pluv/pluv/packages/io
6
+ > @pluv/io@3.1.2 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.48 KB
16
- ESM ⚡️ Build success in 177ms
17
- CJS dist/index.js 54.65 KB
18
- CJS ⚡️ Build success in 177ms
15
+ ESM dist/index.mjs 52.78 KB
16
+ ESM ⚡️ Build success in 109ms
17
+ CJS dist/index.js 54.96 KB
18
+ CJS ⚡️ Build success in 109ms
19
19
  DTS Build start
20
- DTS ⚡️ Build success in 4363ms
21
- DTS dist/index.d.mts 33.31 KB
22
- DTS dist/index.d.ts 33.31 KB
20
+ DTS ⚡️ Build success in 4267ms
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.2
4
+
5
+ ### Patch Changes
6
+
7
+ - 8f138aa: Fixed `getInitialStorage` not being applied.
8
+ - @pluv/crdt@3.1.2
9
+ - @pluv/types@3.1.2
10
+
11
+ ## 3.1.1
12
+
13
+ ### Patch Changes
14
+
15
+ - 7b719bf: Fixed `PluvIO.server` not requiring input parameters (i.e. was optional) when `createIO` specified `crdt`.
16
+ - @pluv/crdt@3.1.1
17
+ - @pluv/types@3.1.1
18
+
3
19
  ## 3.1.0
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
@@ -1,6 +1,6 @@
1
1
  import { AbstractCrdtDocFactory, CrdtLibraryType as CrdtLibraryType$1, NoopCrdtDocFactory } from '@pluv/crdt';
2
2
  export { CrdtLibraryType } from '@pluv/crdt';
3
- import { JsonObject, IOAuthorize, InferIOAuthorizeUser, MaybePromise, IOEventMessage, EventRecord, ProcedureLike, InputZodLike, IORouterLike, BaseUser, UndefinedProps, CrdtDocLike, Id, InferEventMessage, InferEventsOutput, Maybe, IOLike, InferIOOutput, CrdtLibraryType, InferIOInput, InferIOAuthorize } from '@pluv/types';
3
+ import { JsonObject, IOAuthorize, InferIOAuthorizeUser, MaybePromise, IOEventMessage, EventRecord, ProcedureLike, InputZodLike, IORouterLike, BaseUser, UndefinedProps, CrdtDocLike, Id, InferEventMessage, InferEventsOutput, Maybe, IOLike, InferIOOutput, CrdtLibraryType, InferIOInput, InferIOAuthorize, HasRequiredProperty } from '@pluv/types';
4
4
  export { BaseUser, EventMessage, EventRecord, IOEventMessage } from '@pluv/types';
5
5
 
6
6
  declare abstract class AbstractPersistence {
@@ -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
  }
@@ -494,9 +495,10 @@ type ResolvedServerConfig<TPlatform extends AbstractPlatform<any> = AbstractPlat
494
495
  } : {
495
496
  getInitialStorage: GetInitialStorageFn<TContext>;
496
497
  });
497
- type ServerConfig<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> | null = any, TContext extends Record<string, any> = {}, TCrdt extends CrdtLibraryType$1<any> = CrdtLibraryType$1<NoopCrdtDocFactory>, TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext> = {}> = {
498
+ type BaseServerConfig<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> | null = any, TContext extends Record<string, any> = {}, TCrdt extends CrdtLibraryType$1<any> = CrdtLibraryType$1<NoopCrdtDocFactory>, TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext> = {}> = {
498
499
  [P in keyof ResolvedServerConfig<TPlatform, TAuthorize, TContext, TCrdt, TEvents> as ResolvedServerConfig<TPlatform, TAuthorize, TContext, TCrdt, TEvents>[P] extends undefined ? never : P]: ResolvedServerConfig<TPlatform, TAuthorize, TContext, TCrdt, TEvents>[P];
499
500
  };
501
+ type ServerConfig<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> | null = any, TContext extends Record<string, any> = {}, TCrdt extends CrdtLibraryType$1<any> = CrdtLibraryType$1<NoopCrdtDocFactory>, TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext> = {}> = HasRequiredProperty<BaseServerConfig<TPlatform, TAuthorize, TContext, TCrdt, TEvents>> extends true ? [BaseServerConfig<TPlatform, TAuthorize, TContext, TCrdt, TEvents>] : [BaseServerConfig<TPlatform, TAuthorize, TContext, TCrdt, TEvents>?];
500
502
  declare class PluvIO<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> | null = any, TContext extends Record<string, any> = {}, TCrdt extends CrdtLibraryType$1<any> = CrdtLibraryType$1<any>> {
501
503
  readonly version: string;
502
504
  private readonly _authorize;
@@ -510,7 +512,7 @@ declare class PluvIO<TPlatform extends AbstractPlatform<any> = AbstractPlatform<
510
512
  createToken(params: JWTEncodeParams<InferIOAuthorizeUser<TAuthorize>, TPlatform>): Promise<string>;
511
513
  mergeRouters<TRouters extends PluvRouter<TPlatform, TAuthorize, TContext, any>[]>(...routers: TRouters): MergedRouter<TRouters>;
512
514
  router<TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext>>(events: TEvents): PluvRouter<TPlatform, TAuthorize, TContext, TEvents>;
513
- server<TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext> = {}>(config?: ServerConfig<TPlatform, TAuthorize, TContext, TCrdt, TEvents>): PluvServer<TPlatform, TAuthorize, TContext, TCrdt, TEvents>;
515
+ server<TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext> = {}>(...config: ServerConfig<TPlatform, TAuthorize, TContext, TCrdt, TEvents>): PluvServer<TPlatform, TAuthorize, TContext, TCrdt, TEvents>;
514
516
  private _getIOAuthorize;
515
517
  }
516
518
 
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { AbstractCrdtDocFactory, CrdtLibraryType as CrdtLibraryType$1, NoopCrdtDocFactory } from '@pluv/crdt';
2
2
  export { CrdtLibraryType } from '@pluv/crdt';
3
- import { JsonObject, IOAuthorize, InferIOAuthorizeUser, MaybePromise, IOEventMessage, EventRecord, ProcedureLike, InputZodLike, IORouterLike, BaseUser, UndefinedProps, CrdtDocLike, Id, InferEventMessage, InferEventsOutput, Maybe, IOLike, InferIOOutput, CrdtLibraryType, InferIOInput, InferIOAuthorize } from '@pluv/types';
3
+ import { JsonObject, IOAuthorize, InferIOAuthorizeUser, MaybePromise, IOEventMessage, EventRecord, ProcedureLike, InputZodLike, IORouterLike, BaseUser, UndefinedProps, CrdtDocLike, Id, InferEventMessage, InferEventsOutput, Maybe, IOLike, InferIOOutput, CrdtLibraryType, InferIOInput, InferIOAuthorize, HasRequiredProperty } from '@pluv/types';
4
4
  export { BaseUser, EventMessage, EventRecord, IOEventMessage } from '@pluv/types';
5
5
 
6
6
  declare abstract class AbstractPersistence {
@@ -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
  }
@@ -494,9 +495,10 @@ type ResolvedServerConfig<TPlatform extends AbstractPlatform<any> = AbstractPlat
494
495
  } : {
495
496
  getInitialStorage: GetInitialStorageFn<TContext>;
496
497
  });
497
- type ServerConfig<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> | null = any, TContext extends Record<string, any> = {}, TCrdt extends CrdtLibraryType$1<any> = CrdtLibraryType$1<NoopCrdtDocFactory>, TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext> = {}> = {
498
+ type BaseServerConfig<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> | null = any, TContext extends Record<string, any> = {}, TCrdt extends CrdtLibraryType$1<any> = CrdtLibraryType$1<NoopCrdtDocFactory>, TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext> = {}> = {
498
499
  [P in keyof ResolvedServerConfig<TPlatform, TAuthorize, TContext, TCrdt, TEvents> as ResolvedServerConfig<TPlatform, TAuthorize, TContext, TCrdt, TEvents>[P] extends undefined ? never : P]: ResolvedServerConfig<TPlatform, TAuthorize, TContext, TCrdt, TEvents>[P];
499
500
  };
501
+ type ServerConfig<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> | null = any, TContext extends Record<string, any> = {}, TCrdt extends CrdtLibraryType$1<any> = CrdtLibraryType$1<NoopCrdtDocFactory>, TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext> = {}> = HasRequiredProperty<BaseServerConfig<TPlatform, TAuthorize, TContext, TCrdt, TEvents>> extends true ? [BaseServerConfig<TPlatform, TAuthorize, TContext, TCrdt, TEvents>] : [BaseServerConfig<TPlatform, TAuthorize, TContext, TCrdt, TEvents>?];
500
502
  declare class PluvIO<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends PluvIOAuthorize<TPlatform, any, InferInitContextType<TPlatform>> | null = any, TContext extends Record<string, any> = {}, TCrdt extends CrdtLibraryType$1<any> = CrdtLibraryType$1<any>> {
501
503
  readonly version: string;
502
504
  private readonly _authorize;
@@ -510,7 +512,7 @@ declare class PluvIO<TPlatform extends AbstractPlatform<any> = AbstractPlatform<
510
512
  createToken(params: JWTEncodeParams<InferIOAuthorizeUser<TAuthorize>, TPlatform>): Promise<string>;
511
513
  mergeRouters<TRouters extends PluvRouter<TPlatform, TAuthorize, TContext, any>[]>(...routers: TRouters): MergedRouter<TRouters>;
512
514
  router<TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext>>(events: TEvents): PluvRouter<TPlatform, TAuthorize, TContext, TEvents>;
513
- server<TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext> = {}>(config?: ServerConfig<TPlatform, TAuthorize, TContext, TCrdt, TEvents>): PluvServer<TPlatform, TAuthorize, TContext, TCrdt, TEvents>;
515
+ server<TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext> = {}>(...config: ServerConfig<TPlatform, TAuthorize, TContext, TCrdt, TEvents>): PluvServer<TPlatform, TAuthorize, TContext, TCrdt, TEvents>;
514
516
  private _getIOAuthorize;
515
517
  }
516
518
 
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.0";
1173
+ var version = "3.1.2";
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,8 +1266,7 @@ 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 });
1269
+ const loadedState = yield this._getInitialStorage({ context, room });
1266
1270
  if (!!loadedState) {
1267
1271
  doc.applyEncodedState({ update: loadedState }).getEncodedState();
1268
1272
  }
@@ -1462,15 +1466,17 @@ var PluvIO = class {
1462
1466
  const user = params.user;
1463
1467
  const parsed = !!ioAuthorize ? ioAuthorize.user.parse(user) : user;
1464
1468
  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
- );
1469
+ throw new Error(oneLine`
1470
+ createToken was called with a long user id. User ID must be at
1471
+ most 128 characters. Current length: ${user.id.length.toLocaleString()}
1472
+ `);
1468
1473
  }
1469
1474
  const bytes = new TextEncoder().encode(JSON.stringify(parsed)).length;
1470
1475
  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
- );
1476
+ throw new Error(oneLine`
1477
+ createToken called with large payload. User must be at most 512
1478
+ bytes. Current size: ${bytes.toLocaleString()}
1479
+ `);
1474
1480
  }
1475
1481
  const platform = this._platform();
1476
1482
  if (platform._createToken) {
@@ -1489,14 +1495,17 @@ var PluvIO = class {
1489
1495
  router(events) {
1490
1496
  const invalidName = Object.keys(events).find((name) => name.includes("$"));
1491
1497
  if (typeof invalidName === "string") {
1492
- throw new Error(`Invalid event name. Event names must not contain $: "${invalidName}"`);
1498
+ throw new Error(oneLine`
1499
+ Invalid event name. Event names must not contain $: "${invalidName}"
1500
+ `);
1493
1501
  }
1494
1502
  return new PluvRouter(events);
1495
1503
  }
1496
- server(config = {}) {
1497
- var _a;
1498
- return new PluvServer(__spreadProps(__spreadValues({}, config), {
1499
- authorize: (_a = this._authorize) != null ? _a : void 0,
1504
+ server(...config) {
1505
+ var _a, _b;
1506
+ const serverConfig = (_a = config[0]) != null ? _a : {};
1507
+ return new PluvServer(__spreadProps(__spreadValues({}, serverConfig), {
1508
+ authorize: (_b = this._authorize) != null ? _b : void 0,
1500
1509
  context: this._context,
1501
1510
  crdt: this._crdt,
1502
1511
  debug: this._debug,
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.0";
1131
+ var version = "3.1.2";
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,8 +1224,7 @@ 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 });
1227
+ const loadedState = yield this._getInitialStorage({ context, room });
1224
1228
  if (!!loadedState) {
1225
1229
  doc.applyEncodedState({ update: loadedState }).getEncodedState();
1226
1230
  }
@@ -1420,15 +1424,17 @@ var PluvIO = class {
1420
1424
  const user = params.user;
1421
1425
  const parsed = !!ioAuthorize ? ioAuthorize.user.parse(user) : user;
1422
1426
  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
- );
1427
+ throw new Error(oneLine`
1428
+ createToken was called with a long user id. User ID must be at
1429
+ most 128 characters. Current length: ${user.id.length.toLocaleString()}
1430
+ `);
1426
1431
  }
1427
1432
  const bytes = new TextEncoder().encode(JSON.stringify(parsed)).length;
1428
1433
  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
- );
1434
+ throw new Error(oneLine`
1435
+ createToken called with large payload. User must be at most 512
1436
+ bytes. Current size: ${bytes.toLocaleString()}
1437
+ `);
1432
1438
  }
1433
1439
  const platform = this._platform();
1434
1440
  if (platform._createToken) {
@@ -1447,14 +1453,17 @@ var PluvIO = class {
1447
1453
  router(events) {
1448
1454
  const invalidName = Object.keys(events).find((name) => name.includes("$"));
1449
1455
  if (typeof invalidName === "string") {
1450
- throw new Error(`Invalid event name. Event names must not contain $: "${invalidName}"`);
1456
+ throw new Error(oneLine`
1457
+ Invalid event name. Event names must not contain $: "${invalidName}"
1458
+ `);
1451
1459
  }
1452
1460
  return new PluvRouter(events);
1453
1461
  }
1454
- server(config = {}) {
1455
- var _a;
1456
- return new PluvServer(__spreadProps(__spreadValues({}, config), {
1457
- authorize: (_a = this._authorize) != null ? _a : void 0,
1462
+ server(...config) {
1463
+ var _a, _b;
1464
+ const serverConfig = (_a = config[0]) != null ? _a : {};
1465
+ return new PluvServer(__spreadProps(__spreadValues({}, serverConfig), {
1466
+ authorize: (_b = this._authorize) != null ? _b : void 0,
1458
1467
  context: this._context,
1459
1468
  crdt: this._crdt,
1460
1469
  debug: this._debug,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pluv/io",
3
- "version": "3.1.0",
3
+ "version": "3.1.2",
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.0",
25
- "@pluv/types": "^3.1.0"
24
+ "@pluv/crdt": "^3.1.2",
25
+ "@pluv/types": "^3.1.2"
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.0",
33
- "eslint-config-pluv": "^3.1.0"
32
+ "@pluv/tsconfig": "^3.1.2",
33
+ "eslint-config-pluv": "^3.1.2"
34
34
  },
35
35
  "scripts": {
36
36
  "build": "pnpm run version && tsup src/index.ts --format esm,cjs --dts",
package/src/PluvIO.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type { AbstractCrdtDocFactory, CrdtLibraryType, NoopCrdtDocFactory } from "@pluv/crdt";
2
2
  import { noop } from "@pluv/crdt";
3
- import type { BaseUser, IOAuthorize, InferIOAuthorizeUser } from "@pluv/types";
3
+ import type { BaseUser, HasRequiredProperty, IOAuthorize, InferIOAuthorizeUser } from "@pluv/types";
4
4
  import type { AbstractPlatform, InferInitContextType } from "./AbstractPlatform";
5
5
  import type { WebSocketRegisterConfig } from "./IORoom";
6
6
  import { PluvProcedure } from "./PluvProcedure";
@@ -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<
@@ -56,7 +57,7 @@ type ResolvedServerConfig<
56
57
  ? { getInitialStorage?: "[ERROR]: Must specify crdt to use getInitialStorage" }
57
58
  : { getInitialStorage: GetInitialStorageFn<TContext> });
58
59
 
59
- export type ServerConfig<
60
+ export type BaseServerConfig<
60
61
  TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>,
61
62
  TAuthorize extends PluvIOAuthorize<
62
63
  TPlatform,
@@ -78,6 +79,23 @@ export type ServerConfig<
78
79
  : P]: ResolvedServerConfig<TPlatform, TAuthorize, TContext, TCrdt, TEvents>[P];
79
80
  };
80
81
 
82
+ export type ServerConfig<
83
+ TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>,
84
+ TAuthorize extends PluvIOAuthorize<
85
+ TPlatform,
86
+ any,
87
+ InferInitContextType<TPlatform>
88
+ > | null = any,
89
+ TContext extends Record<string, any> = {},
90
+ TCrdt extends CrdtLibraryType<any> = CrdtLibraryType<NoopCrdtDocFactory>,
91
+ TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext> = {},
92
+ > =
93
+ HasRequiredProperty<
94
+ BaseServerConfig<TPlatform, TAuthorize, TContext, TCrdt, TEvents>
95
+ > extends true
96
+ ? [BaseServerConfig<TPlatform, TAuthorize, TContext, TCrdt, TEvents>]
97
+ : [BaseServerConfig<TPlatform, TAuthorize, TContext, TCrdt, TEvents>?];
98
+
81
99
  export class PluvIO<
82
100
  TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>,
83
101
  TAuthorize extends PluvIOAuthorize<
@@ -134,17 +152,19 @@ export class PluvIO<
134
152
  const parsed = !!ioAuthorize ? ioAuthorize.user.parse(user) : user;
135
153
 
136
154
  if (!!this._limits.userIdMaxLength && user.id.length > this._limits.userIdMaxLength) {
137
- throw new Error(
138
- `createToken was called with a long user id. User ID must be at most 128 characters. Current length: ${user.id.length.toLocaleString()}`,
139
- );
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
+ `);
140
159
  }
141
160
 
142
161
  const bytes = new TextEncoder().encode(JSON.stringify(parsed)).length;
143
162
 
144
163
  if (!!this._limits.userMaxSize && bytes > this._limits.userMaxSize) {
145
- throw new Error(
146
- `createToken called with large payload. User must be at most 512 bytes. Current size: ${bytes.toLocaleString()}`,
147
- );
164
+ throw new Error(oneLine`
165
+ createToken called with large payload. User must be at most 512
166
+ bytes. Current size: ${bytes.toLocaleString()}
167
+ `);
148
168
  }
149
169
 
150
170
  const platform = this._platform();
@@ -180,31 +200,35 @@ export class PluvIO<
180
200
  const invalidName = Object.keys(events).find((name) => name.includes("$"));
181
201
 
182
202
  if (typeof invalidName === "string") {
183
- 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
+ `);
184
206
  }
185
207
 
186
208
  return new PluvRouter<TPlatform, TAuthorize, TContext, TEvents>(events);
187
209
  }
188
210
 
189
211
  public server<TEvents extends PluvRouterEventConfig<TPlatform, TAuthorize, TContext> = {}>(
190
- config: ServerConfig<TPlatform, TAuthorize, TContext, TCrdt, TEvents> = {} as ServerConfig<
212
+ ...config: ServerConfig<TPlatform, TAuthorize, TContext, TCrdt, TEvents>
213
+ ): PluvServer<TPlatform, TAuthorize, TContext, TCrdt, TEvents> {
214
+ const serverConfig = (config[0] ?? {}) as PluvServerConfig<
191
215
  TPlatform,
192
216
  TAuthorize,
193
217
  TContext,
194
218
  TCrdt,
195
219
  TEvents
196
- >,
197
- ): PluvServer<TPlatform, TAuthorize, TContext, TCrdt, TEvents> {
220
+ >;
221
+
198
222
  return new PluvServer<TPlatform, TAuthorize, TContext, TCrdt, TEvents>({
199
- ...config,
223
+ ...serverConfig,
200
224
  authorize: this._authorize ?? undefined,
201
225
  context: this._context,
202
226
  crdt: this._crdt,
203
227
  debug: this._debug,
204
- io: this as PluvIO<TPlatform, TAuthorize, TContext>,
228
+ io: this as PluvIO<TPlatform, TAuthorize, TContext, TCrdt>,
205
229
  limits: this._limits,
206
230
  platform: this._platform,
207
- } as PluvServerConfig<TPlatform, TAuthorize, TContext, TCrdt, TEvents>);
231
+ });
208
232
  }
209
233
 
210
234
  private _getIOAuthorize(
package/src/PluvServer.ts CHANGED
@@ -209,11 +209,7 @@ 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
214
  if (!!loadedState) {
219
215
  doc.applyEncodedState({ update: loadedState }).getEncodedState();
@@ -463,6 +459,16 @@ export class PluvServer<
463
459
  return await this._config.io.createToken(params);
464
460
  }
465
461
 
462
+ private _getInitialStorage: GetInitialStorageFn<TContext> = (...args) => {
463
+ const getInitialStorage =
464
+ typeof this._config.getInitialStorage !== "string"
465
+ ? (this._config.getInitialStorage ??
466
+ ((() => null) as GetInitialStorageFn<TContext>))
467
+ : ((() => null) as GetInitialStorageFn<TContext>);
468
+
469
+ return getInitialStorage(...args);
470
+ };
471
+
466
472
  private _getListeners(): BasePluvIOListeners<TPlatform, TAuthorize, TContext, TEvents> {
467
473
  return (this as any)._listeners;
468
474
  }
@@ -1,2 +1,2 @@
1
1
  // Generated by genversion.
2
- export const version = '3.1.0'
2
+ export const version = '3.1.2'