@koi-design/callkit 2.0.2 → 2.0.3
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.d.ts +7 -1
- package/dist/index.global.js +289 -219
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +108 -38
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +105 -35
- package/dist/index.mjs.map +1 -1
- package/package.json +47 -48
package/dist/index.mjs
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
|
-
//
|
|
1
|
+
// core/index.ts
|
|
2
2
|
import md5 from "blueimp-md5";
|
|
3
3
|
|
|
4
|
-
//
|
|
4
|
+
// core/axios.ts
|
|
5
5
|
import axios from "axios";
|
|
6
6
|
var instance = axios.create({
|
|
7
7
|
headers: {
|
|
8
8
|
"Content-Type": "application/x-www-form-urlencoded"
|
|
9
|
-
}
|
|
9
|
+
},
|
|
10
|
+
timeout: 6e4
|
|
10
11
|
});
|
|
11
12
|
instance.interceptors.request.use((config) => config);
|
|
12
13
|
instance.interceptors.response.use(
|
|
@@ -16,25 +17,49 @@ instance.interceptors.response.use(
|
|
|
16
17
|
var request = (config) => instance.request(config);
|
|
17
18
|
var axios_default = request;
|
|
18
19
|
|
|
19
|
-
//
|
|
20
|
+
// core/api.ts
|
|
20
21
|
var Api = class {
|
|
21
22
|
callKit;
|
|
22
23
|
constructor(callKit) {
|
|
23
24
|
this.callKit = callKit;
|
|
24
25
|
}
|
|
26
|
+
isLogining = false;
|
|
27
|
+
isLoginOuting = false;
|
|
25
28
|
async login(params) {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
29
|
+
if (this.isLogining) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
this.isLogining = true;
|
|
33
|
+
try {
|
|
34
|
+
const res = await this.post({
|
|
35
|
+
url: "/auth/agentUser/login",
|
|
36
|
+
method: "post",
|
|
37
|
+
data: params
|
|
38
|
+
});
|
|
39
|
+
this.isLogining = false;
|
|
40
|
+
return res;
|
|
41
|
+
} catch (error) {
|
|
42
|
+
this.isLogining = false;
|
|
43
|
+
throw error;
|
|
44
|
+
}
|
|
31
45
|
}
|
|
32
46
|
async loginOut(params) {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
47
|
+
if (this.isLoginOuting) {
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
this.isLoginOuting = true;
|
|
51
|
+
try {
|
|
52
|
+
const res = await this.post({
|
|
53
|
+
url: "/auth/agentUser/loginOut",
|
|
54
|
+
method: "post",
|
|
55
|
+
data: params
|
|
56
|
+
});
|
|
57
|
+
this.isLoginOuting = false;
|
|
58
|
+
return res;
|
|
59
|
+
} catch (error) {
|
|
60
|
+
this.isLoginOuting = false;
|
|
61
|
+
throw error;
|
|
62
|
+
}
|
|
38
63
|
}
|
|
39
64
|
async trackLogs(log) {
|
|
40
65
|
return this.post(
|
|
@@ -97,7 +122,7 @@ var Api = class {
|
|
|
97
122
|
}
|
|
98
123
|
};
|
|
99
124
|
|
|
100
|
-
//
|
|
125
|
+
// core/const.ts
|
|
101
126
|
var CallStatus = {
|
|
102
127
|
/**
|
|
103
128
|
* Initial state/Hang up
|
|
@@ -446,7 +471,7 @@ var trackLogsDefaultConfig = {
|
|
|
446
471
|
maxSize: 8192
|
|
447
472
|
};
|
|
448
473
|
|
|
449
|
-
//
|
|
474
|
+
// core/call.ts
|
|
450
475
|
var Call = class {
|
|
451
476
|
callKit;
|
|
452
477
|
constructor(callKit) {
|
|
@@ -587,20 +612,19 @@ var Call = class {
|
|
|
587
612
|
// package.json
|
|
588
613
|
var package_default = {
|
|
589
614
|
name: "@koi-design/callkit",
|
|
590
|
-
version: "2.0.
|
|
615
|
+
version: "2.0.3",
|
|
591
616
|
description: "callkit",
|
|
592
617
|
author: "koi",
|
|
593
618
|
license: "ISC",
|
|
594
619
|
scripts: {
|
|
595
620
|
build: "tsup",
|
|
596
|
-
start: "tsup --watch",
|
|
597
621
|
dev: "vite",
|
|
598
|
-
lint: "eslint -c
|
|
599
|
-
prepare: "husky install",
|
|
622
|
+
lint: "eslint -c ../../.eslintrc.js --ext .jsx,.js,.tsx,.ts ./package --fix",
|
|
600
623
|
release: "tsup && node scripts/pkg.js"
|
|
601
624
|
},
|
|
602
625
|
exports: {
|
|
603
626
|
".": {
|
|
627
|
+
types: "./dist/index.d.ts",
|
|
604
628
|
require: "./dist/index.js",
|
|
605
629
|
import: "./dist/index.mjs"
|
|
606
630
|
}
|
|
@@ -637,7 +661,7 @@ var package_default = {
|
|
|
637
661
|
}
|
|
638
662
|
};
|
|
639
663
|
|
|
640
|
-
//
|
|
664
|
+
// core/config.ts
|
|
641
665
|
var Config = class {
|
|
642
666
|
callKit;
|
|
643
667
|
constructor(callKit) {
|
|
@@ -653,6 +677,7 @@ var Config = class {
|
|
|
653
677
|
socket: "",
|
|
654
678
|
userInfo: {
|
|
655
679
|
wsUrl: "",
|
|
680
|
+
logGather: false,
|
|
656
681
|
sessionId: "",
|
|
657
682
|
// User
|
|
658
683
|
username: "",
|
|
@@ -707,7 +732,8 @@ var Config = class {
|
|
|
707
732
|
fsPort: "",
|
|
708
733
|
iceInfo: [],
|
|
709
734
|
iceGatheringTimeout: this.config.userInfo.iceGatheringTimeout,
|
|
710
|
-
encryptionMethod: EncryptionMethod.INTERNAL
|
|
735
|
+
encryptionMethod: EncryptionMethod.INTERNAL,
|
|
736
|
+
logGather: false
|
|
711
737
|
};
|
|
712
738
|
this.callKit.trigger(KitEvent.KIT_LOGIN_CHANGE, false);
|
|
713
739
|
}
|
|
@@ -740,7 +766,7 @@ var Config = class {
|
|
|
740
766
|
}
|
|
741
767
|
};
|
|
742
768
|
|
|
743
|
-
//
|
|
769
|
+
// core/logger.ts
|
|
744
770
|
import stringify from "json-stringify-safe";
|
|
745
771
|
function getLevel(level) {
|
|
746
772
|
return LoggerLevelMap[level];
|
|
@@ -877,7 +903,7 @@ var Logger = class {
|
|
|
877
903
|
}
|
|
878
904
|
};
|
|
879
905
|
|
|
880
|
-
//
|
|
906
|
+
// core/connect.ts
|
|
881
907
|
import {
|
|
882
908
|
UserAgent,
|
|
883
909
|
Web,
|
|
@@ -886,7 +912,7 @@ import {
|
|
|
886
912
|
RegistererState
|
|
887
913
|
} from "sip.js";
|
|
888
914
|
|
|
889
|
-
//
|
|
915
|
+
// core/utils.ts
|
|
890
916
|
var formatGetInviteData = (inviteData) => {
|
|
891
917
|
const extendInfoStr = inviteData["x-antaios-manual-extend_info"] || inviteData["x-antaios-manual-extend-info"];
|
|
892
918
|
let extendInfoObj = null;
|
|
@@ -901,7 +927,7 @@ var formatGetInviteData = (inviteData) => {
|
|
|
901
927
|
}
|
|
902
928
|
};
|
|
903
929
|
|
|
904
|
-
//
|
|
930
|
+
// core/connect.ts
|
|
905
931
|
var DEFAULT_RECONNECT_CONFIG = {
|
|
906
932
|
maxAttempts: 3,
|
|
907
933
|
delay: 500
|
|
@@ -1431,7 +1457,7 @@ var Connect = class {
|
|
|
1431
1457
|
this.setCallId(inviteData.callUuid);
|
|
1432
1458
|
} else {
|
|
1433
1459
|
this.setCallId(null);
|
|
1434
|
-
this.callKit.logger.
|
|
1460
|
+
this.callKit.logger.warn("call id is not exist", {
|
|
1435
1461
|
caller: "Connect.register.onInvite",
|
|
1436
1462
|
content: {
|
|
1437
1463
|
inviteData
|
|
@@ -1495,9 +1521,7 @@ var Connect = class {
|
|
|
1495
1521
|
onDisconnect: (error) => {
|
|
1496
1522
|
console.log("onDisconnect", error);
|
|
1497
1523
|
if (error) {
|
|
1498
|
-
this.
|
|
1499
|
-
} else {
|
|
1500
|
-
this.callKit.logger.info("SIP User Agent Disconnected", {
|
|
1524
|
+
this.callKit.logger.info("SIP User Agent Disconnected with error", {
|
|
1501
1525
|
caller: "Connect.register",
|
|
1502
1526
|
type: "SIP",
|
|
1503
1527
|
content: {
|
|
@@ -1505,6 +1529,13 @@ var Connect = class {
|
|
|
1505
1529
|
errCode: ErrorCode.WEBRTC_USER_AGENT_ERROR
|
|
1506
1530
|
}
|
|
1507
1531
|
});
|
|
1532
|
+
this.startReconnectTimer();
|
|
1533
|
+
} else {
|
|
1534
|
+
this.callKit.logger.info("SIP User Agent Disconnected", {
|
|
1535
|
+
caller: "Connect.register",
|
|
1536
|
+
type: "SIP",
|
|
1537
|
+
content: {}
|
|
1538
|
+
});
|
|
1508
1539
|
}
|
|
1509
1540
|
},
|
|
1510
1541
|
onRegister: () => {
|
|
@@ -1613,6 +1644,15 @@ var Connect = class {
|
|
|
1613
1644
|
}
|
|
1614
1645
|
});
|
|
1615
1646
|
});
|
|
1647
|
+
await this.userAgent?.stop().catch((err) => {
|
|
1648
|
+
this.callKit.logger.warn(err, {
|
|
1649
|
+
caller: "Connect.unregister",
|
|
1650
|
+
type: "SIP",
|
|
1651
|
+
content: {
|
|
1652
|
+
errCode: ErrorCode.WEBRTC_CANCEL_REGISTER_ERROR
|
|
1653
|
+
}
|
|
1654
|
+
});
|
|
1655
|
+
});
|
|
1616
1656
|
}
|
|
1617
1657
|
async call(callback) {
|
|
1618
1658
|
this.callKit.logger.info("connect call", {
|
|
@@ -1847,14 +1887,17 @@ var Connect = class {
|
|
|
1847
1887
|
}
|
|
1848
1888
|
};
|
|
1849
1889
|
|
|
1850
|
-
//
|
|
1890
|
+
// core/socket.ts
|
|
1851
1891
|
var RECONNECT_CONFIG = {
|
|
1852
1892
|
enabled: true,
|
|
1853
1893
|
maxAttempts: 3,
|
|
1854
1894
|
delay: 500,
|
|
1855
1895
|
backoffMultiplier: 1.5,
|
|
1856
1896
|
pingInterval: 3e4,
|
|
1857
|
-
pingTimeout: 5e3
|
|
1897
|
+
pingTimeout: 5e3,
|
|
1898
|
+
maxConnectsPerWindow: 5,
|
|
1899
|
+
timeWindow: 6e4
|
|
1900
|
+
// 60 seconds
|
|
1858
1901
|
};
|
|
1859
1902
|
var Socket = class {
|
|
1860
1903
|
callKit;
|
|
@@ -1869,6 +1912,8 @@ var Socket = class {
|
|
|
1869
1912
|
isReconnecting = false;
|
|
1870
1913
|
reconnectAttempts = 0;
|
|
1871
1914
|
socketError = false;
|
|
1915
|
+
// Track connection attempts for rate limiting
|
|
1916
|
+
connectAttemptTimes = [];
|
|
1872
1917
|
constructor(callKit) {
|
|
1873
1918
|
this.callKit = callKit;
|
|
1874
1919
|
const { reconnect } = this.callKit.config.getConfig();
|
|
@@ -1909,8 +1954,8 @@ var Socket = class {
|
|
|
1909
1954
|
this.ws.onerror = null;
|
|
1910
1955
|
this.ws.onmessage = null;
|
|
1911
1956
|
}
|
|
1912
|
-
if (this.ws
|
|
1913
|
-
this.ws
|
|
1957
|
+
if (this.ws?.readyState === WebSocket.CONNECTING || this.ws?.readyState === WebSocket.OPEN) {
|
|
1958
|
+
this.ws?.close(1e3, "socket close");
|
|
1914
1959
|
this.callKit.logger.info("socket clear", {
|
|
1915
1960
|
caller: "Socket.clearWebSocket",
|
|
1916
1961
|
type: "INCALL",
|
|
@@ -1924,6 +1969,7 @@ var Socket = class {
|
|
|
1924
1969
|
if (this.ws) {
|
|
1925
1970
|
this.clearWebSocket();
|
|
1926
1971
|
}
|
|
1972
|
+
this.connectAttemptTimes.push(Date.now());
|
|
1927
1973
|
this.ws = new WebSocket(socketUrl);
|
|
1928
1974
|
this.ws.onopen = (ev) => this.onOpen(ev);
|
|
1929
1975
|
this.ws.onclose = (ev) => this.onClose(ev);
|
|
@@ -2301,6 +2347,7 @@ var Socket = class {
|
|
|
2301
2347
|
this.lastPingTime = void 0;
|
|
2302
2348
|
this.satrtConfirm = false;
|
|
2303
2349
|
this.socketError = false;
|
|
2350
|
+
this.connectAttemptTimes = [];
|
|
2304
2351
|
this.clearWebSocket();
|
|
2305
2352
|
}
|
|
2306
2353
|
attemptReconnect() {
|
|
@@ -2308,6 +2355,29 @@ var Socket = class {
|
|
|
2308
2355
|
clearTimeout(this.reconnectTimer);
|
|
2309
2356
|
this.reconnectTimer = void 0;
|
|
2310
2357
|
}
|
|
2358
|
+
const { maxConnectsPerWindow, timeWindow } = this.socketConfig;
|
|
2359
|
+
if (maxConnectsPerWindow && timeWindow) {
|
|
2360
|
+
const now = Date.now();
|
|
2361
|
+
this.connectAttemptTimes = this.connectAttemptTimes.filter(
|
|
2362
|
+
(time) => now - time < timeWindow
|
|
2363
|
+
);
|
|
2364
|
+
if (this.connectAttemptTimes.length >= maxConnectsPerWindow) {
|
|
2365
|
+
this.callKit.trigger(KitEvent.INCALL_CONNECT_EVENT, {
|
|
2366
|
+
event: "INCALL_RECONNECT_ERROR"
|
|
2367
|
+
});
|
|
2368
|
+
this.callKit.logger.warn("Connection rate limit exceeded", {
|
|
2369
|
+
caller: "Socket.attemptReconnect",
|
|
2370
|
+
type: "INCALL",
|
|
2371
|
+
content: {
|
|
2372
|
+
errCode: ErrorCode.SOCKET_RECONNECT_FAILED,
|
|
2373
|
+
attempts: this.connectAttemptTimes.length,
|
|
2374
|
+
maxConnectsPerWindow,
|
|
2375
|
+
timeWindow
|
|
2376
|
+
}
|
|
2377
|
+
});
|
|
2378
|
+
return;
|
|
2379
|
+
}
|
|
2380
|
+
}
|
|
2311
2381
|
if (this.reconnectAttempts >= this.socketConfig.maxAttempts) {
|
|
2312
2382
|
this.callKit.trigger(KitEvent.INCALL_CONNECT_EVENT, {
|
|
2313
2383
|
event: "INCALL_RECONNECT_ERROR"
|
|
@@ -2350,7 +2420,7 @@ var Socket = class {
|
|
|
2350
2420
|
}
|
|
2351
2421
|
};
|
|
2352
2422
|
|
|
2353
|
-
//
|
|
2423
|
+
// core/index.ts
|
|
2354
2424
|
var CallKit = class {
|
|
2355
2425
|
api;
|
|
2356
2426
|
config;
|