tevrocsdk 0.1.43 → 0.1.44
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/package.json +1 -1
- package/tevroc-sdk.cjs.js +48602 -0
- package/tevroc-sdk.cjs.js.map +7 -0
- package/tevroc-sdk.esm.js +72 -19
- package/tevroc-sdk.esm.js.map +7 -0
- package/tevroc-sdk.min.js +20 -20
- package/tevroc-sdk.umd.js +72 -19
package/tevroc-sdk.esm.js
CHANGED
|
@@ -41268,6 +41268,18 @@ var TevrocError = class extends Error {
|
|
|
41268
41268
|
this.response = options.response;
|
|
41269
41269
|
}
|
|
41270
41270
|
};
|
|
41271
|
+
function buildRealtimeRoomName(value, appId = "") {
|
|
41272
|
+
if (!value) return "";
|
|
41273
|
+
const appKey = String(appId || "tevrocdev");
|
|
41274
|
+
const text2 = String(value);
|
|
41275
|
+
let encoded = "";
|
|
41276
|
+
for (let index2 = 0; index2 < text2.length; index2 += 1) {
|
|
41277
|
+
const charCode = text2.charCodeAt(index2);
|
|
41278
|
+
const keyCode = appKey.charCodeAt(index2 % appKey.length);
|
|
41279
|
+
encoded += (charCode ^ keyCode + index2 + 1).toString(16).padStart(2, "0");
|
|
41280
|
+
}
|
|
41281
|
+
return `chat:${encoded}`;
|
|
41282
|
+
}
|
|
41271
41283
|
function createClient(options = {}) {
|
|
41272
41284
|
return new TevrocClient(options);
|
|
41273
41285
|
}
|
|
@@ -41387,30 +41399,71 @@ function createShiplogicApi(client) {
|
|
|
41387
41399
|
}
|
|
41388
41400
|
function createRealtimeApi(client) {
|
|
41389
41401
|
const toWebSocketUrl = (baseUrl) => baseUrl.replace(/^http:/, "ws:").replace(/^https:/, "wss:");
|
|
41390
|
-
const
|
|
41391
|
-
|
|
41392
|
-
const
|
|
41393
|
-
|
|
41394
|
-
|
|
41395
|
-
|
|
41396
|
-
|
|
41397
|
-
|
|
41398
|
-
|
|
41399
|
-
|
|
41400
|
-
|
|
41401
|
-
|
|
41402
|
-
|
|
41403
|
-
|
|
41402
|
+
const encryptRoomName = (room) => buildRealtimeRoomName(room, client.appId);
|
|
41403
|
+
const connect = (room, handlers = {}, options = {}) => {
|
|
41404
|
+
const roomName = encryptRoomName(room || options.room || "default");
|
|
41405
|
+
const url = `${toWebSocketUrl(client.baseUrl)}/realtime/${encodeURIComponent(roomName || "default")}`;
|
|
41406
|
+
const reconnectMs = options.reconnectMs ?? 3e3;
|
|
41407
|
+
const maxReconnects = options.maxReconnects ?? 5;
|
|
41408
|
+
let socket = null;
|
|
41409
|
+
let reconnectTimer = null;
|
|
41410
|
+
let reconnectCount = 0;
|
|
41411
|
+
let closedByUser = false;
|
|
41412
|
+
const cleanup = () => {
|
|
41413
|
+
if (reconnectTimer) {
|
|
41414
|
+
clearTimeout(reconnectTimer);
|
|
41415
|
+
reconnectTimer = null;
|
|
41416
|
+
}
|
|
41417
|
+
};
|
|
41418
|
+
const openSocket = () => {
|
|
41419
|
+
cleanup();
|
|
41420
|
+
if (closedByUser) return;
|
|
41421
|
+
socket = new WebSocket(url);
|
|
41422
|
+
socket.addEventListener("open", (event) => {
|
|
41423
|
+
reconnectCount = 0;
|
|
41424
|
+
handlers.open?.(event);
|
|
41425
|
+
});
|
|
41426
|
+
socket.addEventListener("message", (event) => {
|
|
41427
|
+
let data = event.data;
|
|
41428
|
+
try {
|
|
41429
|
+
data = JSON.parse(event.data);
|
|
41430
|
+
} catch {
|
|
41431
|
+
}
|
|
41432
|
+
handlers.message?.(data, event);
|
|
41433
|
+
});
|
|
41434
|
+
socket.addEventListener("close", (event) => {
|
|
41435
|
+
handlers.close?.(event);
|
|
41436
|
+
if (closedByUser || reconnectCount >= maxReconnects) return;
|
|
41437
|
+
reconnectTimer = setTimeout(() => {
|
|
41438
|
+
reconnectCount += 1;
|
|
41439
|
+
openSocket();
|
|
41440
|
+
}, reconnectMs);
|
|
41441
|
+
});
|
|
41442
|
+
socket.addEventListener("error", (event) => {
|
|
41443
|
+
handlers.error?.(event);
|
|
41444
|
+
});
|
|
41445
|
+
};
|
|
41446
|
+
openSocket();
|
|
41404
41447
|
return {
|
|
41405
|
-
socket,
|
|
41406
|
-
send: (payload) => socket
|
|
41407
|
-
close: () =>
|
|
41448
|
+
socket: () => socket,
|
|
41449
|
+
send: (payload) => socket?.readyState === WebSocket.OPEN && socket.send(typeof payload === "string" ? payload : JSON.stringify(payload)),
|
|
41450
|
+
close: () => {
|
|
41451
|
+
closedByUser = true;
|
|
41452
|
+
cleanup();
|
|
41453
|
+
socket?.close();
|
|
41454
|
+
},
|
|
41455
|
+
reconnect: () => {
|
|
41456
|
+
closedByUser = false;
|
|
41457
|
+
openSocket();
|
|
41458
|
+
}
|
|
41408
41459
|
};
|
|
41409
41460
|
};
|
|
41410
41461
|
return {
|
|
41462
|
+
encryptRoomName,
|
|
41463
|
+
roomName: encryptRoomName,
|
|
41411
41464
|
connect,
|
|
41412
|
-
room: (room, handlers = {}) => {
|
|
41413
|
-
const connection = connect(room, handlers);
|
|
41465
|
+
room: (room, handlers = {}, options = {}) => {
|
|
41466
|
+
const connection = connect(room, handlers, options);
|
|
41414
41467
|
return {
|
|
41415
41468
|
...connection,
|
|
41416
41469
|
publish: (payload) => connection.send(payload)
|