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.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
|
}
|
|
@@ -41352,7 +41364,6 @@ var TevrocClient = class {
|
|
|
41352
41364
|
if (value !== void 0 && value !== null) headers.set(key, String(value));
|
|
41353
41365
|
}
|
|
41354
41366
|
}
|
|
41355
|
-
console.error(headers, url);
|
|
41356
41367
|
try {
|
|
41357
41368
|
const response = await this.fetch(url, {
|
|
41358
41369
|
method,
|
|
@@ -41387,30 +41398,71 @@ function createShiplogicApi(client) {
|
|
|
41387
41398
|
}
|
|
41388
41399
|
function createRealtimeApi(client) {
|
|
41389
41400
|
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
|
-
|
|
41401
|
+
const encryptRoomName = (room) => buildRealtimeRoomName(room, client.appId);
|
|
41402
|
+
const connect = (room, handlers = {}, options = {}) => {
|
|
41403
|
+
const roomName = encryptRoomName(room || options.room || "default");
|
|
41404
|
+
const url = `${toWebSocketUrl(client.baseUrl)}/realtime/${encodeURIComponent(roomName || "default")}`;
|
|
41405
|
+
const reconnectMs = options.reconnectMs ?? 3e3;
|
|
41406
|
+
const maxReconnects = options.maxReconnects ?? 5;
|
|
41407
|
+
let socket = null;
|
|
41408
|
+
let reconnectTimer = null;
|
|
41409
|
+
let reconnectCount = 0;
|
|
41410
|
+
let closedByUser = false;
|
|
41411
|
+
const cleanup = () => {
|
|
41412
|
+
if (reconnectTimer) {
|
|
41413
|
+
clearTimeout(reconnectTimer);
|
|
41414
|
+
reconnectTimer = null;
|
|
41415
|
+
}
|
|
41416
|
+
};
|
|
41417
|
+
const openSocket = () => {
|
|
41418
|
+
cleanup();
|
|
41419
|
+
if (closedByUser) return;
|
|
41420
|
+
socket = new WebSocket(url);
|
|
41421
|
+
socket.addEventListener("open", (event) => {
|
|
41422
|
+
reconnectCount = 0;
|
|
41423
|
+
handlers.open?.(event);
|
|
41424
|
+
});
|
|
41425
|
+
socket.addEventListener("message", (event) => {
|
|
41426
|
+
let data = event.data;
|
|
41427
|
+
try {
|
|
41428
|
+
data = JSON.parse(event.data);
|
|
41429
|
+
} catch {
|
|
41430
|
+
}
|
|
41431
|
+
handlers.message?.(data, event);
|
|
41432
|
+
});
|
|
41433
|
+
socket.addEventListener("close", (event) => {
|
|
41434
|
+
handlers.close?.(event);
|
|
41435
|
+
if (closedByUser || reconnectCount >= maxReconnects) return;
|
|
41436
|
+
reconnectTimer = setTimeout(() => {
|
|
41437
|
+
reconnectCount += 1;
|
|
41438
|
+
openSocket();
|
|
41439
|
+
}, reconnectMs);
|
|
41440
|
+
});
|
|
41441
|
+
socket.addEventListener("error", (event) => {
|
|
41442
|
+
handlers.error?.(event);
|
|
41443
|
+
});
|
|
41444
|
+
};
|
|
41445
|
+
openSocket();
|
|
41404
41446
|
return {
|
|
41405
|
-
socket,
|
|
41406
|
-
send: (payload) => socket
|
|
41407
|
-
close: () =>
|
|
41447
|
+
socket: () => socket,
|
|
41448
|
+
send: (payload) => socket?.readyState === WebSocket.OPEN && socket.send(typeof payload === "string" ? payload : JSON.stringify(payload)),
|
|
41449
|
+
close: () => {
|
|
41450
|
+
closedByUser = true;
|
|
41451
|
+
cleanup();
|
|
41452
|
+
socket?.close();
|
|
41453
|
+
},
|
|
41454
|
+
reconnect: () => {
|
|
41455
|
+
closedByUser = false;
|
|
41456
|
+
openSocket();
|
|
41457
|
+
}
|
|
41408
41458
|
};
|
|
41409
41459
|
};
|
|
41410
41460
|
return {
|
|
41461
|
+
encryptRoomName,
|
|
41462
|
+
roomName: encryptRoomName,
|
|
41411
41463
|
connect,
|
|
41412
|
-
room: (room, handlers = {}) => {
|
|
41413
|
-
const connection = connect(room, handlers);
|
|
41464
|
+
room: (room, handlers = {}, options = {}) => {
|
|
41465
|
+
const connection = connect(room, handlers, options);
|
|
41414
41466
|
return {
|
|
41415
41467
|
...connection,
|
|
41416
41468
|
publish: (payload) => connection.send(payload)
|