@pluv/io 2.1.0 → 2.2.1

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.js CHANGED
@@ -655,6 +655,7 @@ var IORoom = class {
655
655
  this._listeners.onUserDisconnected({
656
656
  context: this._context,
657
657
  encodedState,
658
+ platform: this._platform,
658
659
  room: this.id,
659
660
  user
660
661
  })
@@ -712,6 +713,7 @@ var IORoom = class {
712
713
  this._listeners.onUserConnected({
713
714
  context: this._context,
714
715
  encodedState,
716
+ platform: this._platform,
715
717
  room: this.id,
716
718
  user,
717
719
  webSocket: pluvWs.webSocket
@@ -875,6 +877,7 @@ var IORoom = class {
875
877
  this._listeners.onDestroy(__spreadProps(__spreadValues({}, "_meta" in this._platform && !!this._platform._meta ? { _meta: this._platform._meta } : {}), {
876
878
  context: this._context,
877
879
  encodedState,
880
+ platform: this._platform,
878
881
  room: this.id
879
882
  }))
880
883
  );
@@ -913,6 +916,7 @@ var IORoom = class {
913
916
  garbageCollect: () => __async(this, null, function* () {
914
917
  yield this.garbageCollect();
915
918
  }),
919
+ platform: this._platform,
916
920
  get presence() {
917
921
  return session.presence;
918
922
  },
@@ -939,6 +943,7 @@ var IORoom = class {
939
943
  context: this._context,
940
944
  encodedState: doc.getEncodedState(),
941
945
  message,
946
+ platform: this._platform,
942
947
  room: this.id,
943
948
  user: session.user,
944
949
  webSocket: session.webSocket.webSocket
@@ -1105,6 +1110,7 @@ var IORoom = class {
1105
1110
  garbageCollect: () => __async(this, null, function* () {
1106
1111
  yield this.garbageCollect();
1107
1112
  }),
1113
+ platform: this._platform,
1108
1114
  get presence() {
1109
1115
  return null;
1110
1116
  },
@@ -1156,14 +1162,56 @@ var pickBy = (obj, by) => {
1156
1162
  };
1157
1163
 
1158
1164
  // src/version.generated.ts
1159
- var version = "2.1.0";
1165
+ var version = "2.2.1";
1160
1166
 
1161
1167
  // src/PluvServer.ts
1162
1168
  var PluvServer = class {
1163
1169
  constructor(options) {
1164
1170
  this.version = version;
1165
- this._authorize = null;
1166
- this._baseRouter = new PluvRouter({
1171
+ this._config = __spreadValues({
1172
+ crdt: import_crdt2.noop,
1173
+ debug: false,
1174
+ router: new PluvRouter({})
1175
+ }, options);
1176
+ const {
1177
+ onRoomDeleted,
1178
+ onRoomMessage,
1179
+ onStorageUpdated,
1180
+ onUserConnected,
1181
+ onUserDisconnected
1182
+ } = options;
1183
+ this._listeners = {
1184
+ onRoomDeleted: (event) => onRoomDeleted == null ? void 0 : onRoomDeleted(event),
1185
+ onRoomMessage: (event) => onRoomMessage == null ? void 0 : onRoomMessage(event),
1186
+ onStorageUpdated: (event) => onStorageUpdated == null ? void 0 : onStorageUpdated(event),
1187
+ onUserConnected: (event) => onUserConnected == null ? void 0 : onUserConnected(event),
1188
+ onUserDisconnected: (event) => onUserDisconnected == null ? void 0 : onUserDisconnected(event)
1189
+ };
1190
+ }
1191
+ get fetch() {
1192
+ const platform = this._config.platform();
1193
+ return (...args) => {
1194
+ if (!platform._fetch)
1195
+ throw new Error(`\`${platform._name}\` does not support \`fetch\``);
1196
+ return platform._fetch(...args);
1197
+ };
1198
+ }
1199
+ /**
1200
+ * @ignore
1201
+ * @readonly
1202
+ * @deprecated Internal use only. Changes to this will never be marked as breaking.
1203
+ */
1204
+ get _defs() {
1205
+ return {
1206
+ authorize: this._config.authorize,
1207
+ context: this._config.context,
1208
+ events: this._router._defs.events,
1209
+ platform: this._config.platform()
1210
+ };
1211
+ }
1212
+ get _baseRouter() {
1213
+ const listeners = this._getListeners();
1214
+ return new PluvRouter({
1167
1215
  $getOthers: this._procedure.sync((data, { room, session, sessions }) => {
1168
1216
  const currentTime = Date.now();
1169
1217
  const others = sessions.filter((wsSession) => {
@@ -1198,46 +1246,54 @@ var PluvServer = class {
1198
1246
  timers: { presence: timers.presence }
1199
1247
  }
1200
1248
  };
1201
- }).self((_0, _1) => __async(this, [_0, _1], function* (data, { context, doc, room, session }) {
1202
- var _a;
1203
- const oldState = yield this._platform.persistence.getStorageState(room);
1249
+ }).self((_0, _1) => __async(this, [_0, _1], function* (data, { context, doc, platform, room, session }) {
1250
+ var _a, _b;
1251
+ const oldState = yield platform.persistence.getStorageState(room);
1204
1252
  const update = data == null ? void 0 : data.update;
1205
1253
  if (!oldState) {
1206
- const loadedState = yield (_a = this._getInitialStorage) == null ? void 0 : _a.call(this, { context, room });
1254
+ const loadedState = yield (_b = (_a = this._config).getInitialStorage) == null ? void 0 : _b.call(_a, {
1255
+ context,
1256
+ room
1257
+ });
1207
1258
  if (!!loadedState) {
1208
1259
  doc.applyEncodedState({ update: loadedState }).getEncodedState();
1209
1260
  }
1210
1261
  }
1211
1262
  if (!doc.isEmpty()) {
1212
1263
  const encodedState2 = doc.getEncodedState();
1213
- return { $storageReceived: { changeKind: "unchanged", state: encodedState2 } };
1264
+ return {
1265
+ $storageReceived: { changeKind: "unchanged", state: encodedState2 }
1266
+ };
1214
1267
  }
1215
1268
  if (!!update) {
1216
1269
  const encodedState2 = doc.applyEncodedState({ update }).getEncodedState();
1217
1270
  const storageSize = new TextEncoder().encode(encodedState2).length;
1218
- if (!!this._limits.storageMaxSize && storageSize > this._limits.storageMaxSize) {
1271
+ if (!!this._config.limits.storageMaxSize && storageSize > this._config.limits.storageMaxSize) {
1219
1272
  throw new Error("Storage has exceeded the size limit");
1220
1273
  }
1221
- yield this._platform.persistence.setStorageState(room, encodedState2).catch((error) => {
1274
+ yield platform.persistence.setStorageState(room, encodedState2).catch((error) => {
1222
1275
  this._logDebug(error);
1223
1276
  });
1224
- this._getListeners().onStorageUpdated({
1277
+ listeners.onStorageUpdated({
1225
1278
  context,
1226
1279
  encodedState: encodedState2,
1280
+ platform,
1227
1281
  room,
1228
1282
  user: session == null ? void 0 : session.user,
1229
1283
  webSocket: session == null ? void 0 : session.webSocket.webSocket
1230
1284
  });
1231
- return { $storageReceived: { changeKind: "initialized", state: encodedState2 } };
1285
+ return {
1286
+ $storageReceived: { changeKind: "initialized", state: encodedState2 }
1287
+ };
1232
1288
  }
1233
1289
  const encodedState = doc.getEncodedState();
1234
1290
  return { $storageReceived: { changeKind: "empty", state: encodedState } };
1235
1291
  })),
1236
- $ping: this._procedure.self((data, { session }) => {
1292
+ $ping: this._procedure.self((data, { platform, session }) => {
1237
1293
  if (!session) return {};
1238
1294
  const currentTime = (/* @__PURE__ */ new Date()).getTime();
1239
1295
  const prevState = session.webSocket.state;
1240
- this._platform.setSerializedState(session.webSocket, __spreadProps(__spreadValues({}, prevState), {
1296
+ platform.setSerializedState(session.webSocket, __spreadProps(__spreadValues({}, prevState), {
1241
1297
  timers: __spreadProps(__spreadValues({}, prevState.timers), {
1242
1298
  ping: currentTime
1243
1299
  })
@@ -1248,10 +1304,13 @@ var PluvServer = class {
1248
1304
  const presence = data == null ? void 0 : data.presence;
1249
1305
  const { session } = context;
1250
1306
  if (!session) return {};
1251
- const cleanedPatch = pickBy(presence != null ? presence : {}, (value) => typeof value !== "undefined");
1307
+ const cleanedPatch = pickBy(
1308
+ presence != null ? presence : {},
1309
+ (value) => typeof value !== "undefined"
1310
+ );
1252
1311
  const updated = Object.assign(/* @__PURE__ */ Object.create(null), session.presence, cleanedPatch);
1253
1312
  const bytes = new TextEncoder().encode(JSON.stringify(updated)).length;
1254
- if (!!this._limits.presenceMaxSize && bytes > this._limits.presenceMaxSize) {
1313
+ if (!!this._config.limits.presenceMaxSize && bytes > this._config.limits.presenceMaxSize) {
1255
1314
  throw new Error(
1256
1315
  `Large presence. Presence must be at most 512 bytes. Current size: ${bytes.toLocaleString()}`
1257
1316
  );
@@ -1264,7 +1323,7 @@ var PluvServer = class {
1264
1323
  }
1265
1324
  };
1266
1325
  }),
1267
- $updateStorage: this._procedure.broadcast((data, { context, doc, room }) => {
1326
+ $updateStorage: this._procedure.broadcast((data, { context, doc, platform, room }) => {
1268
1327
  var _a;
1269
1328
  const origin = data == null ? void 0 : data.origin;
1270
1329
  const update = (_a = data == null ? void 0 : data.update) != null ? _a : null;
@@ -1272,113 +1331,72 @@ var PluvServer = class {
1272
1331
  const updated = update === null ? doc : doc.applyEncodedState({ update });
1273
1332
  const encodedState = updated.getEncodedState();
1274
1333
  const storageSize = new TextEncoder().encode(encodedState).length;
1275
- if (!!this._limits.storageMaxSize && storageSize > this._limits.storageMaxSize) {
1334
+ if (!!this._config.limits.storageMaxSize && storageSize > this._config.limits.storageMaxSize) {
1276
1335
  throw new Error("Storage has exceeded the size limit");
1277
1336
  }
1278
- this._platform.persistence.setStorageState(room, encodedState).then(() => {
1279
- this._getListeners().onStorageUpdated({
1337
+ platform.persistence.setStorageState(room, encodedState).then(() => {
1338
+ listeners.onStorageUpdated({
1280
1339
  context,
1281
1340
  encodedState,
1341
+ platform,
1282
1342
  room
1283
1343
  });
1284
1344
  });
1285
1345
  return { $storageUpdated: { state: encodedState } };
1286
1346
  })
1287
1347
  });
1288
- this._context = {};
1289
- this._getInitialStorage = null;
1290
- const {
1291
- authorize: authorize2,
1292
- context,
1293
- crdt = import_crdt2.noop,
1294
- debug = false,
1295
- getInitialStorage,
1296
- io,
1297
- limits,
1298
- platform,
1299
- router = new PluvRouter({})
1300
- } = options;
1301
- const {
1302
- onRoomDeleted,
1303
- onRoomMessage,
1304
- onStorageUpdated,
1305
- onUserConnected,
1306
- onUserDisconnected
1307
- } = options;
1308
- platform.validateConfig(options);
1309
- this._crdt = crdt;
1310
- this._debug = debug;
1311
- this._io = io;
1312
- this._limits = limits;
1313
- this._platform = platform;
1314
- this._router = router ? PluvRouter.merge(this._baseRouter, router) : this._baseRouter;
1315
- if (authorize2) this._authorize = authorize2;
1316
- if (context) this._context = context;
1317
- if (getInitialStorage) this._getInitialStorage = getInitialStorage;
1318
- this._listeners = {
1319
- onRoomDeleted: (event) => onRoomDeleted == null ? void 0 : onRoomDeleted(event),
1320
- onRoomMessage: (event) => onRoomMessage == null ? void 0 : onRoomMessage(event),
1321
- onStorageUpdated: (event) => onStorageUpdated == null ? void 0 : onStorageUpdated(event),
1322
- onUserConnected: (event) => onUserConnected == null ? void 0 : onUserConnected(event),
1323
- onUserDisconnected: (event) => onUserDisconnected == null ? void 0 : onUserDisconnected(event)
1324
- };
1325
- }
1326
- get fetch() {
1327
- return (...args) => {
1328
- if (!this._platform._fetch)
1329
- throw new Error(`\`${this._platform._name}\` does not support \`fetch\``);
1330
- return this._platform._fetch(...args);
1331
- };
1332
- }
1333
- /**
1334
- * @ignore
1335
- * @readonly
1336
- * @deprecated Internal use only. Changes to this will never be marked as breaking.
1337
- */
1338
- get _defs() {
1339
- return {
1340
- authorize: this._authorize,
1341
- context: this._context,
1342
- events: this._router._defs.events,
1343
- platform: this._platform
1344
- };
1345
1348
  }
1346
1349
  get _procedure() {
1347
1350
  return new PluvProcedure();
1348
1351
  }
1352
+ get _router() {
1353
+ return this._config.router ? PluvRouter.merge(this._baseRouter, this._config.router) : this._baseRouter;
1354
+ }
1349
1355
  createRoom(room, ...options) {
1350
1356
  var _a, _c;
1351
1357
  const _b = (_a = options[0]) != null ? _a : {}, { _meta, debug, onDestroy, onMessage } = _b, platformRoomContext = __objRest(_b, ["_meta", "debug", "onDestroy", "onMessage"]);
1352
- if (this._platform._config.handleMode !== "io") {
1353
- throw new Error(`\`createRoom\` is unsupported for \`${this._platform._name}\``);
1358
+ const platform = this._config.platform();
1359
+ platform.validateConfig(this._config);
1360
+ if (platform._config.handleMode !== "io") {
1361
+ throw new Error(`\`createRoom\` is unsupported for \`${platform._name}\``);
1354
1362
  }
1355
1363
  if (!/^[a-z0-9](?:[a-z0-9-_]*[a-z0-9])?$/i.test(room))
1356
1364
  throw new Error("Unsupported room name");
1357
1365
  const roomContext = platformRoomContext;
1358
- const context = typeof this._context === "function" ? this._context(roomContext) : this._context;
1366
+ const context = typeof this._config.context === "function" ? this._config.context(roomContext) : this._config.context;
1367
+ const listeners = this._getListeners();
1368
+ const logDebug = this._logDebug.bind(this);
1359
1369
  const newRoom = new IORoom(room, __spreadProps(__spreadValues({}, !!_meta ? { _meta } : {}), {
1360
- authorize: (_c = this._authorize) != null ? _c : void 0,
1370
+ authorize: (_c = this._config.authorize) != null ? _c : void 0,
1361
1371
  context,
1362
- crdt: this._crdt,
1363
- debug: debug != null ? debug : this._debug,
1364
- onDestroy: (event) => __async(this, null, function* () {
1365
- this._logDebug(`${import_kleur2.default.blue("Deleting empty room:")} ${room}`);
1366
- yield Promise.resolve(onDestroy == null ? void 0 : onDestroy(event));
1367
- yield Promise.resolve(this._getListeners().onRoomDeleted(event));
1368
- yield this._platform.persistence.deleteStorageState(room);
1369
- this._logDebug(`${import_kleur2.default.blue("Deleted room:")} ${room}`);
1370
- }),
1371
- onMessage: (event) => __async(this, null, function* () {
1372
- yield Promise.resolve(onMessage == null ? void 0 : onMessage(event));
1373
- yield Promise.resolve(this._getListeners().onRoomMessage(event));
1374
- }),
1375
- onUserConnected: (event) => __async(this, null, function* () {
1376
- yield Promise.resolve(this._getListeners().onUserConnected(event));
1377
- }),
1378
- onUserDisconnected: (event) => __async(this, null, function* () {
1379
- yield Promise.resolve(this._getListeners().onUserDisconnected(event));
1380
- }),
1381
- platform: this._platform,
1372
+ crdt: this._config.crdt,
1373
+ debug: debug != null ? debug : this._config.debug,
1374
+ onDestroy(event) {
1375
+ return __async(this, null, function* () {
1376
+ logDebug(`${import_kleur2.default.blue("Deleting empty room:")} ${room}`);
1377
+ yield Promise.resolve(onDestroy == null ? void 0 : onDestroy(event));
1378
+ yield Promise.resolve(listeners.onRoomDeleted(event));
1379
+ yield event.platform.persistence.deleteStorageState(room);
1380
+ logDebug(`${import_kleur2.default.blue("Deleted room:")} ${room}`);
1381
+ });
1382
+ },
1383
+ onMessage(event) {
1384
+ return __async(this, null, function* () {
1385
+ yield Promise.resolve(onMessage == null ? void 0 : onMessage(event));
1386
+ yield Promise.resolve(listeners.onRoomMessage(event));
1387
+ });
1388
+ },
1389
+ onUserConnected(event) {
1390
+ return __async(this, null, function* () {
1391
+ yield Promise.resolve(listeners.onUserConnected(event));
1392
+ });
1393
+ },
1394
+ onUserDisconnected(event) {
1395
+ return __async(this, null, function* () {
1396
+ yield Promise.resolve(listeners.onUserDisconnected(event));
1397
+ });
1398
+ },
1399
+ platform,
1382
1400
  roomContext,
1383
1401
  router: this._router
1384
1402
  }));
@@ -1387,14 +1405,14 @@ var PluvServer = class {
1387
1405
  }
1388
1406
  createToken(params) {
1389
1407
  return __async(this, null, function* () {
1390
- return yield this._io.createToken(params);
1408
+ return yield this._config.io.createToken(params);
1391
1409
  });
1392
1410
  }
1393
1411
  _getListeners() {
1394
1412
  return this._listeners;
1395
1413
  }
1396
1414
  _logDebug(...data) {
1397
- if (this._debug) console.log(...data);
1415
+ if (this._config.debug) console.log(...data);
1398
1416
  }
1399
1417
  };
1400
1418
 
@@ -1439,10 +1457,13 @@ var PluvIO = class {
1439
1457
  `createToken called with large payload. User must be at most 512 bytes. Current size: ${bytes.toLocaleString()}`
1440
1458
  );
1441
1459
  }
1442
- if (this._platform._createToken) return yield this._platform._createToken(params);
1460
+ const platform = this._platform();
1461
+ if (platform._createToken) {
1462
+ return yield platform._createToken(__spreadProps(__spreadValues({}, params), { authorize: ioAuthorize }));
1463
+ }
1443
1464
  const secret = (_a = ioAuthorize == null ? void 0 : ioAuthorize.secret) != null ? _a : null;
1444
1465
  if (!secret) throw new Error("`authorize` was specified without a valid secret");
1445
- return yield authorize({ platform: this._platform, secret }).encode(
1466
+ return yield authorize({ platform, secret }).encode(
1446
1467
  params
1447
1468
  );
1448
1469
  });