tevrocsdk 0.1.43 → 0.1.45
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 -20
- package/tevroc-sdk.esm.js.map +7 -0
- package/tevroc-sdk.min.js +20 -20
- package/tevroc-sdk.umd.js +72 -20
package/tevroc-sdk.umd.js
CHANGED
|
@@ -41278,6 +41278,18 @@ var Tevroc = (() => {
|
|
|
41278
41278
|
this.response = options.response;
|
|
41279
41279
|
}
|
|
41280
41280
|
};
|
|
41281
|
+
function buildRealtimeRoomName(value, appId = "") {
|
|
41282
|
+
if (!value) return "";
|
|
41283
|
+
const appKey = String(appId || "tevrocdev");
|
|
41284
|
+
const text2 = String(value);
|
|
41285
|
+
let encoded = "";
|
|
41286
|
+
for (let index2 = 0; index2 < text2.length; index2 += 1) {
|
|
41287
|
+
const charCode = text2.charCodeAt(index2);
|
|
41288
|
+
const keyCode = appKey.charCodeAt(index2 % appKey.length);
|
|
41289
|
+
encoded += (charCode ^ keyCode + index2 + 1).toString(16).padStart(2, "0");
|
|
41290
|
+
}
|
|
41291
|
+
return `chat:${encoded}`;
|
|
41292
|
+
}
|
|
41281
41293
|
function createClient(options = {}) {
|
|
41282
41294
|
return new TevrocClient(options);
|
|
41283
41295
|
}
|
|
@@ -41362,7 +41374,6 @@ var Tevroc = (() => {
|
|
|
41362
41374
|
if (value !== void 0 && value !== null) headers.set(key, String(value));
|
|
41363
41375
|
}
|
|
41364
41376
|
}
|
|
41365
|
-
console.error(headers, url);
|
|
41366
41377
|
try {
|
|
41367
41378
|
const response = await this.fetch(url, {
|
|
41368
41379
|
method,
|
|
@@ -41397,30 +41408,71 @@ var Tevroc = (() => {
|
|
|
41397
41408
|
}
|
|
41398
41409
|
function createRealtimeApi(client) {
|
|
41399
41410
|
const toWebSocketUrl = (baseUrl) => baseUrl.replace(/^http:/, "ws:").replace(/^https:/, "wss:");
|
|
41400
|
-
const
|
|
41401
|
-
|
|
41402
|
-
const
|
|
41403
|
-
|
|
41404
|
-
|
|
41405
|
-
|
|
41406
|
-
|
|
41407
|
-
|
|
41408
|
-
|
|
41409
|
-
|
|
41410
|
-
|
|
41411
|
-
|
|
41412
|
-
|
|
41413
|
-
|
|
41411
|
+
const encryptRoomName = (room) => buildRealtimeRoomName(room, client.appId);
|
|
41412
|
+
const connect = (room, handlers = {}, options = {}) => {
|
|
41413
|
+
const roomName = encryptRoomName(room || options.room || "default");
|
|
41414
|
+
const url = `${toWebSocketUrl(client.baseUrl)}/realtime/${encodeURIComponent(roomName || "default")}`;
|
|
41415
|
+
const reconnectMs = options.reconnectMs ?? 3e3;
|
|
41416
|
+
const maxReconnects = options.maxReconnects ?? 5;
|
|
41417
|
+
let socket = null;
|
|
41418
|
+
let reconnectTimer = null;
|
|
41419
|
+
let reconnectCount = 0;
|
|
41420
|
+
let closedByUser = false;
|
|
41421
|
+
const cleanup = () => {
|
|
41422
|
+
if (reconnectTimer) {
|
|
41423
|
+
clearTimeout(reconnectTimer);
|
|
41424
|
+
reconnectTimer = null;
|
|
41425
|
+
}
|
|
41426
|
+
};
|
|
41427
|
+
const openSocket = () => {
|
|
41428
|
+
cleanup();
|
|
41429
|
+
if (closedByUser) return;
|
|
41430
|
+
socket = new WebSocket(url);
|
|
41431
|
+
socket.addEventListener("open", (event) => {
|
|
41432
|
+
reconnectCount = 0;
|
|
41433
|
+
handlers.open?.(event);
|
|
41434
|
+
});
|
|
41435
|
+
socket.addEventListener("message", (event) => {
|
|
41436
|
+
let data = event.data;
|
|
41437
|
+
try {
|
|
41438
|
+
data = JSON.parse(event.data);
|
|
41439
|
+
} catch {
|
|
41440
|
+
}
|
|
41441
|
+
handlers.message?.(data, event);
|
|
41442
|
+
});
|
|
41443
|
+
socket.addEventListener("close", (event) => {
|
|
41444
|
+
handlers.close?.(event);
|
|
41445
|
+
if (closedByUser || reconnectCount >= maxReconnects) return;
|
|
41446
|
+
reconnectTimer = setTimeout(() => {
|
|
41447
|
+
reconnectCount += 1;
|
|
41448
|
+
openSocket();
|
|
41449
|
+
}, reconnectMs);
|
|
41450
|
+
});
|
|
41451
|
+
socket.addEventListener("error", (event) => {
|
|
41452
|
+
handlers.error?.(event);
|
|
41453
|
+
});
|
|
41454
|
+
};
|
|
41455
|
+
openSocket();
|
|
41414
41456
|
return {
|
|
41415
|
-
socket,
|
|
41416
|
-
send: (payload) => socket
|
|
41417
|
-
close: () =>
|
|
41457
|
+
socket: () => socket,
|
|
41458
|
+
send: (payload) => socket?.readyState === WebSocket.OPEN && socket.send(typeof payload === "string" ? payload : JSON.stringify(payload)),
|
|
41459
|
+
close: () => {
|
|
41460
|
+
closedByUser = true;
|
|
41461
|
+
cleanup();
|
|
41462
|
+
socket?.close();
|
|
41463
|
+
},
|
|
41464
|
+
reconnect: () => {
|
|
41465
|
+
closedByUser = false;
|
|
41466
|
+
openSocket();
|
|
41467
|
+
}
|
|
41418
41468
|
};
|
|
41419
41469
|
};
|
|
41420
41470
|
return {
|
|
41471
|
+
encryptRoomName,
|
|
41472
|
+
roomName: encryptRoomName,
|
|
41421
41473
|
connect,
|
|
41422
|
-
room: (room, handlers = {}) => {
|
|
41423
|
-
const connection = connect(room, handlers);
|
|
41474
|
+
room: (room, handlers = {}, options = {}) => {
|
|
41475
|
+
const connection = connect(room, handlers, options);
|
|
41424
41476
|
return {
|
|
41425
41477
|
...connection,
|
|
41426
41478
|
publish: (payload) => connection.send(payload)
|