@pluv/io 2.1.0 → 2.2.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/.turbo/turbo-build.log +9 -9
- package/CHANGELOG.md +8 -0
- package/dist/index.d.mts +19 -16
- package/dist/index.d.ts +19 -16
- package/dist/index.js +124 -91
- package/dist/index.mjs +124 -91
- package/package.json +5 -5
- package/src/IORoom.ts +11 -5
- package/src/PluvIO.ts +7 -5
- package/src/PluvServer.ts +238 -214
- package/src/createIO.ts +1 -1
- package/src/types.ts +13 -8
- package/src/version.generated.ts +1 -1
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,74 @@ var pickBy = (obj, by) => {
|
|
|
1156
1162
|
};
|
|
1157
1163
|
|
|
1158
1164
|
// src/version.generated.ts
|
|
1159
|
-
var version = "2.
|
|
1165
|
+
var version = "2.2.0";
|
|
1160
1166
|
|
|
1161
1167
|
// src/PluvServer.ts
|
|
1162
1168
|
var PluvServer = class {
|
|
1163
1169
|
constructor(options) {
|
|
1164
1170
|
this.version = version;
|
|
1165
1171
|
this._authorize = null;
|
|
1166
|
-
this.
|
|
1172
|
+
this._context = {};
|
|
1173
|
+
this._getInitialStorage = null;
|
|
1174
|
+
const {
|
|
1175
|
+
authorize: authorize2,
|
|
1176
|
+
context,
|
|
1177
|
+
crdt = import_crdt2.noop,
|
|
1178
|
+
debug = false,
|
|
1179
|
+
getInitialStorage,
|
|
1180
|
+
io,
|
|
1181
|
+
limits,
|
|
1182
|
+
platform,
|
|
1183
|
+
router = new PluvRouter({})
|
|
1184
|
+
} = options;
|
|
1185
|
+
const {
|
|
1186
|
+
onRoomDeleted,
|
|
1187
|
+
onRoomMessage,
|
|
1188
|
+
onStorageUpdated,
|
|
1189
|
+
onUserConnected,
|
|
1190
|
+
onUserDisconnected
|
|
1191
|
+
} = options;
|
|
1192
|
+
platform().validateConfig(options);
|
|
1193
|
+
this._crdt = crdt;
|
|
1194
|
+
this._debug = debug;
|
|
1195
|
+
this._inputRouter = router;
|
|
1196
|
+
this._io = io;
|
|
1197
|
+
this._limits = limits;
|
|
1198
|
+
this._platform = platform;
|
|
1199
|
+
if (authorize2) this._authorize = authorize2;
|
|
1200
|
+
if (context) this._context = context;
|
|
1201
|
+
if (getInitialStorage) this._getInitialStorage = getInitialStorage;
|
|
1202
|
+
this._listeners = {
|
|
1203
|
+
onRoomDeleted: (event) => onRoomDeleted == null ? void 0 : onRoomDeleted(event),
|
|
1204
|
+
onRoomMessage: (event) => onRoomMessage == null ? void 0 : onRoomMessage(event),
|
|
1205
|
+
onStorageUpdated: (event) => onStorageUpdated == null ? void 0 : onStorageUpdated(event),
|
|
1206
|
+
onUserConnected: (event) => onUserConnected == null ? void 0 : onUserConnected(event),
|
|
1207
|
+
onUserDisconnected: (event) => onUserDisconnected == null ? void 0 : onUserDisconnected(event)
|
|
1208
|
+
};
|
|
1209
|
+
}
|
|
1210
|
+
get fetch() {
|
|
1211
|
+
const platform = this._platform();
|
|
1212
|
+
return (...args) => {
|
|
1213
|
+
if (!platform._fetch)
|
|
1214
|
+
throw new Error(`\`${platform._name}\` does not support \`fetch\``);
|
|
1215
|
+
return platform._fetch(...args);
|
|
1216
|
+
};
|
|
1217
|
+
}
|
|
1218
|
+
/**
|
|
1219
|
+
* @ignore
|
|
1220
|
+
* @readonly
|
|
1221
|
+
* @deprecated Internal use only. Changes to this will never be marked as breaking.
|
|
1222
|
+
*/
|
|
1223
|
+
get _defs() {
|
|
1224
|
+
return {
|
|
1225
|
+
authorize: this._authorize,
|
|
1226
|
+
context: this._context,
|
|
1227
|
+
events: this._router._defs.events,
|
|
1228
|
+
platform: this._platform()
|
|
1229
|
+
};
|
|
1230
|
+
}
|
|
1231
|
+
get _baseRouter() {
|
|
1232
|
+
return new PluvRouter({
|
|
1167
1233
|
$getOthers: this._procedure.sync((data, { room, session, sessions }) => {
|
|
1168
1234
|
const currentTime = Date.now();
|
|
1169
1235
|
const others = sessions.filter((wsSession) => {
|
|
@@ -1198,9 +1264,9 @@ var PluvServer = class {
|
|
|
1198
1264
|
timers: { presence: timers.presence }
|
|
1199
1265
|
}
|
|
1200
1266
|
};
|
|
1201
|
-
}).self((_0, _1) => __async(this, [_0, _1], function* (data, { context, doc, room, session }) {
|
|
1267
|
+
}).self((_0, _1) => __async(this, [_0, _1], function* (data, { context, doc, platform, room, session }) {
|
|
1202
1268
|
var _a;
|
|
1203
|
-
const oldState = yield
|
|
1269
|
+
const oldState = yield platform.persistence.getStorageState(room);
|
|
1204
1270
|
const update = data == null ? void 0 : data.update;
|
|
1205
1271
|
if (!oldState) {
|
|
1206
1272
|
const loadedState = yield (_a = this._getInitialStorage) == null ? void 0 : _a.call(this, { context, room });
|
|
@@ -1210,7 +1276,9 @@ var PluvServer = class {
|
|
|
1210
1276
|
}
|
|
1211
1277
|
if (!doc.isEmpty()) {
|
|
1212
1278
|
const encodedState2 = doc.getEncodedState();
|
|
1213
|
-
return {
|
|
1279
|
+
return {
|
|
1280
|
+
$storageReceived: { changeKind: "unchanged", state: encodedState2 }
|
|
1281
|
+
};
|
|
1214
1282
|
}
|
|
1215
1283
|
if (!!update) {
|
|
1216
1284
|
const encodedState2 = doc.applyEncodedState({ update }).getEncodedState();
|
|
@@ -1218,26 +1286,29 @@ var PluvServer = class {
|
|
|
1218
1286
|
if (!!this._limits.storageMaxSize && storageSize > this._limits.storageMaxSize) {
|
|
1219
1287
|
throw new Error("Storage has exceeded the size limit");
|
|
1220
1288
|
}
|
|
1221
|
-
yield
|
|
1289
|
+
yield platform.persistence.setStorageState(room, encodedState2).catch((error) => {
|
|
1222
1290
|
this._logDebug(error);
|
|
1223
1291
|
});
|
|
1224
1292
|
this._getListeners().onStorageUpdated({
|
|
1225
1293
|
context,
|
|
1226
1294
|
encodedState: encodedState2,
|
|
1295
|
+
platform,
|
|
1227
1296
|
room,
|
|
1228
1297
|
user: session == null ? void 0 : session.user,
|
|
1229
1298
|
webSocket: session == null ? void 0 : session.webSocket.webSocket
|
|
1230
1299
|
});
|
|
1231
|
-
return {
|
|
1300
|
+
return {
|
|
1301
|
+
$storageReceived: { changeKind: "initialized", state: encodedState2 }
|
|
1302
|
+
};
|
|
1232
1303
|
}
|
|
1233
1304
|
const encodedState = doc.getEncodedState();
|
|
1234
1305
|
return { $storageReceived: { changeKind: "empty", state: encodedState } };
|
|
1235
1306
|
})),
|
|
1236
|
-
$ping: this._procedure.self((data, { session }) => {
|
|
1307
|
+
$ping: this._procedure.self((data, { platform, session }) => {
|
|
1237
1308
|
if (!session) return {};
|
|
1238
1309
|
const currentTime = (/* @__PURE__ */ new Date()).getTime();
|
|
1239
1310
|
const prevState = session.webSocket.state;
|
|
1240
|
-
|
|
1311
|
+
platform.setSerializedState(session.webSocket, __spreadProps(__spreadValues({}, prevState), {
|
|
1241
1312
|
timers: __spreadProps(__spreadValues({}, prevState.timers), {
|
|
1242
1313
|
ping: currentTime
|
|
1243
1314
|
})
|
|
@@ -1248,7 +1319,10 @@ var PluvServer = class {
|
|
|
1248
1319
|
const presence = data == null ? void 0 : data.presence;
|
|
1249
1320
|
const { session } = context;
|
|
1250
1321
|
if (!session) return {};
|
|
1251
|
-
const cleanedPatch = pickBy(
|
|
1322
|
+
const cleanedPatch = pickBy(
|
|
1323
|
+
presence != null ? presence : {},
|
|
1324
|
+
(value) => typeof value !== "undefined"
|
|
1325
|
+
);
|
|
1252
1326
|
const updated = Object.assign(/* @__PURE__ */ Object.create(null), session.presence, cleanedPatch);
|
|
1253
1327
|
const bytes = new TextEncoder().encode(JSON.stringify(updated)).length;
|
|
1254
1328
|
if (!!this._limits.presenceMaxSize && bytes > this._limits.presenceMaxSize) {
|
|
@@ -1264,7 +1338,7 @@ var PluvServer = class {
|
|
|
1264
1338
|
}
|
|
1265
1339
|
};
|
|
1266
1340
|
}),
|
|
1267
|
-
$updateStorage: this._procedure.broadcast((data, { context, doc, room }) => {
|
|
1341
|
+
$updateStorage: this._procedure.broadcast((data, { context, doc, platform, room }) => {
|
|
1268
1342
|
var _a;
|
|
1269
1343
|
const origin = data == null ? void 0 : data.origin;
|
|
1270
1344
|
const update = (_a = data == null ? void 0 : data.update) != null ? _a : null;
|
|
@@ -1275,110 +1349,68 @@ var PluvServer = class {
|
|
|
1275
1349
|
if (!!this._limits.storageMaxSize && storageSize > this._limits.storageMaxSize) {
|
|
1276
1350
|
throw new Error("Storage has exceeded the size limit");
|
|
1277
1351
|
}
|
|
1278
|
-
|
|
1352
|
+
platform.persistence.setStorageState(room, encodedState).then(() => {
|
|
1279
1353
|
this._getListeners().onStorageUpdated({
|
|
1280
1354
|
context,
|
|
1281
1355
|
encodedState,
|
|
1356
|
+
platform,
|
|
1282
1357
|
room
|
|
1283
1358
|
});
|
|
1284
1359
|
});
|
|
1285
1360
|
return { $storageUpdated: { state: encodedState } };
|
|
1286
1361
|
})
|
|
1287
1362
|
});
|
|
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
1363
|
}
|
|
1346
1364
|
get _procedure() {
|
|
1347
1365
|
return new PluvProcedure();
|
|
1348
1366
|
}
|
|
1367
|
+
get _router() {
|
|
1368
|
+
return this._inputRouter ? PluvRouter.merge(this._baseRouter, this._inputRouter) : this._baseRouter;
|
|
1369
|
+
}
|
|
1349
1370
|
createRoom(room, ...options) {
|
|
1350
1371
|
var _a, _c;
|
|
1351
1372
|
const _b = (_a = options[0]) != null ? _a : {}, { _meta, debug, onDestroy, onMessage } = _b, platformRoomContext = __objRest(_b, ["_meta", "debug", "onDestroy", "onMessage"]);
|
|
1352
|
-
|
|
1353
|
-
|
|
1373
|
+
const platform = this._platform();
|
|
1374
|
+
if (platform._config.handleMode !== "io") {
|
|
1375
|
+
throw new Error(`\`createRoom\` is unsupported for \`${platform._name}\``);
|
|
1354
1376
|
}
|
|
1355
1377
|
if (!/^[a-z0-9](?:[a-z0-9-_]*[a-z0-9])?$/i.test(room))
|
|
1356
1378
|
throw new Error("Unsupported room name");
|
|
1357
1379
|
const roomContext = platformRoomContext;
|
|
1358
1380
|
const context = typeof this._context === "function" ? this._context(roomContext) : this._context;
|
|
1381
|
+
const listeners = this._getListeners();
|
|
1382
|
+
const logDebug = this._logDebug.bind(this);
|
|
1359
1383
|
const newRoom = new IORoom(room, __spreadProps(__spreadValues({}, !!_meta ? { _meta } : {}), {
|
|
1360
1384
|
authorize: (_c = this._authorize) != null ? _c : void 0,
|
|
1361
1385
|
context,
|
|
1362
1386
|
crdt: this._crdt,
|
|
1363
1387
|
debug: debug != null ? debug : this._debug,
|
|
1364
|
-
onDestroy
|
|
1365
|
-
this
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1388
|
+
onDestroy(event) {
|
|
1389
|
+
return __async(this, null, function* () {
|
|
1390
|
+
logDebug(`${import_kleur2.default.blue("Deleting empty room:")} ${room}`);
|
|
1391
|
+
yield Promise.resolve(onDestroy == null ? void 0 : onDestroy(event));
|
|
1392
|
+
yield Promise.resolve(listeners.onRoomDeleted(event));
|
|
1393
|
+
yield event.platform.persistence.deleteStorageState(room);
|
|
1394
|
+
logDebug(`${import_kleur2.default.blue("Deleted room:")} ${room}`);
|
|
1395
|
+
});
|
|
1396
|
+
},
|
|
1397
|
+
onMessage(event) {
|
|
1398
|
+
return __async(this, null, function* () {
|
|
1399
|
+
yield Promise.resolve(onMessage == null ? void 0 : onMessage(event));
|
|
1400
|
+
yield Promise.resolve(listeners.onRoomMessage(event));
|
|
1401
|
+
});
|
|
1402
|
+
},
|
|
1403
|
+
onUserConnected(event) {
|
|
1404
|
+
return __async(this, null, function* () {
|
|
1405
|
+
yield Promise.resolve(listeners.onUserConnected(event));
|
|
1406
|
+
});
|
|
1407
|
+
},
|
|
1408
|
+
onUserDisconnected(event) {
|
|
1409
|
+
return __async(this, null, function* () {
|
|
1410
|
+
yield Promise.resolve(listeners.onUserDisconnected(event));
|
|
1411
|
+
});
|
|
1412
|
+
},
|
|
1413
|
+
platform,
|
|
1382
1414
|
roomContext,
|
|
1383
1415
|
router: this._router
|
|
1384
1416
|
}));
|
|
@@ -1439,10 +1471,11 @@ var PluvIO = class {
|
|
|
1439
1471
|
`createToken called with large payload. User must be at most 512 bytes. Current size: ${bytes.toLocaleString()}`
|
|
1440
1472
|
);
|
|
1441
1473
|
}
|
|
1442
|
-
|
|
1474
|
+
const platform = this._platform();
|
|
1475
|
+
if (platform._createToken) return yield platform._createToken(params);
|
|
1443
1476
|
const secret = (_a = ioAuthorize == null ? void 0 : ioAuthorize.secret) != null ? _a : null;
|
|
1444
1477
|
if (!secret) throw new Error("`authorize` was specified without a valid secret");
|
|
1445
|
-
return yield authorize({ platform
|
|
1478
|
+
return yield authorize({ platform, secret }).encode(
|
|
1446
1479
|
params
|
|
1447
1480
|
);
|
|
1448
1481
|
});
|