@cloudbase/database 1.4.1 → 1.4.2
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/.trunk/configs/.markdownlint.yaml +10 -0
- package/.trunk/trunk.yaml +26 -0
- package/dist/commonjs/ObjectId/index.d.ts +11 -11
- package/dist/commonjs/ObjectId/index.js +21 -21
- package/dist/commonjs/aggregate.d.ts +29 -29
- package/dist/commonjs/aggregate.js +124 -124
- package/dist/commonjs/collection.d.ts +17 -17
- package/dist/commonjs/collection.js +72 -72
- package/dist/commonjs/command.d.ts +175 -175
- package/dist/commonjs/command.js +285 -285
- package/dist/commonjs/commands/logic.d.ts +24 -24
- package/dist/commonjs/commands/logic.js +79 -79
- package/dist/commonjs/commands/query.d.ts +73 -73
- package/dist/commonjs/commands/query.js +147 -147
- package/dist/commonjs/commands/update.d.ts +29 -29
- package/dist/commonjs/commands/update.js +49 -49
- package/dist/commonjs/config/app.config.d.ts +6 -6
- package/dist/commonjs/config/app.config.js +7 -7
- package/dist/commonjs/config/error.config.d.ts +3 -3
- package/dist/commonjs/config/error.config.js +15 -15
- package/dist/commonjs/const/code.d.ts +34 -34
- package/dist/commonjs/const/code.js +36 -36
- package/dist/commonjs/constant.d.ts +54 -54
- package/dist/commonjs/constant.js +76 -76
- package/dist/commonjs/document.d.ts +14 -14
- package/dist/commonjs/document.js +217 -219
- package/dist/commonjs/geo/index.d.ts +6 -6
- package/dist/commonjs/geo/index.js +11 -11
- package/dist/commonjs/geo/interface.d.ts +24 -24
- package/dist/commonjs/geo/interface.js +2 -2
- package/dist/commonjs/geo/lineString.d.ts +19 -19
- package/dist/commonjs/geo/lineString.js +57 -57
- package/dist/commonjs/geo/multiLineString.d.ts +18 -18
- package/dist/commonjs/geo/multiLineString.js +56 -56
- package/dist/commonjs/geo/multiPoint.d.ts +18 -18
- package/dist/commonjs/geo/multiPoint.js +50 -50
- package/dist/commonjs/geo/multiPolygon.d.ts +18 -18
- package/dist/commonjs/geo/multiPolygon.js +62 -62
- package/dist/commonjs/geo/point.d.ts +19 -19
- package/dist/commonjs/geo/point.js +43 -43
- package/dist/commonjs/geo/polygon.d.ts +19 -19
- package/dist/commonjs/geo/polygon.js +70 -70
- package/dist/commonjs/helper/symbol.d.ts +15 -15
- package/dist/commonjs/helper/symbol.js +20 -20
- package/dist/commonjs/index.d.ts +50 -50
- package/dist/commonjs/index.js +41 -41
- package/dist/commonjs/operator-map.d.ts +4 -4
- package/dist/commonjs/operator-map.js +23 -23
- package/dist/commonjs/query.d.ts +38 -38
- package/dist/commonjs/query.js +249 -247
- package/dist/commonjs/realtime/error.d.ts +7 -7
- package/dist/commonjs/realtime/error.js +11 -11
- package/dist/commonjs/realtime/listener.d.ts +14 -14
- package/dist/commonjs/realtime/listener.js +17 -17
- package/dist/commonjs/realtime/message.d.ts +3 -3
- package/dist/commonjs/realtime/message.js +10 -10
- package/dist/commonjs/realtime/snapshot.d.ts +16 -16
- package/dist/commonjs/realtime/snapshot.js +42 -42
- package/dist/commonjs/realtime/virtual-websocket-client.d.ts +63 -63
- package/dist/commonjs/realtime/virtual-websocket-client.js +736 -736
- package/dist/commonjs/realtime/websocket-client.d.ts +78 -78
- package/dist/commonjs/realtime/websocket-client.js +567 -567
- package/dist/commonjs/realtime/ws-event.d.ts +126 -126
- package/dist/commonjs/realtime/ws-event.js +139 -139
- package/dist/commonjs/regexp/index.d.ts +18 -18
- package/dist/commonjs/regexp/index.js +30 -30
- package/dist/commonjs/serializer/common.d.ts +11 -11
- package/dist/commonjs/serializer/common.js +90 -90
- package/dist/commonjs/serializer/datatype.d.ts +7 -7
- package/dist/commonjs/serializer/datatype.js +99 -99
- package/dist/commonjs/serializer/query.d.ts +8 -8
- package/dist/commonjs/serializer/query.js +254 -254
- package/dist/commonjs/serializer/update.d.ts +16 -16
- package/dist/commonjs/serializer/update.js +142 -142
- package/dist/commonjs/serverDate/index.d.ts +13 -13
- package/dist/commonjs/serverDate/index.js +23 -23
- package/dist/commonjs/transaction/index.d.ts +30 -30
- package/dist/commonjs/transaction/index.js +105 -105
- package/dist/commonjs/transaction.d.ts +42 -42
- package/dist/commonjs/transaction.js +126 -126
- package/dist/commonjs/typings/index.d.ts +45 -45
- package/dist/commonjs/typings/index.js +2 -2
- package/dist/commonjs/typings/realtime.d.ts +219 -219
- package/dist/commonjs/typings/realtime.js +2 -2
- package/dist/commonjs/util.d.ts +9 -9
- package/dist/commonjs/util.js +101 -101
- package/dist/commonjs/utils/error.d.ts +37 -37
- package/dist/commonjs/utils/error.js +55 -55
- package/dist/commonjs/utils/symbol.d.ts +8 -8
- package/dist/commonjs/utils/symbol.js +39 -39
- package/dist/commonjs/utils/type.d.ts +13 -13
- package/dist/commonjs/utils/type.js +22 -22
- package/dist/commonjs/utils/utils.d.ts +20 -19
- package/dist/commonjs/utils/utils.js +59 -59
- package/dist/commonjs/validate.d.ts +1 -1
- package/dist/commonjs/validate.js +155 -155
- package/package.json +2 -3
- package/src/document.ts +1 -3
- package/src/query.ts +1 -0
- package/dist/esm/ObjectId/index.d.ts +0 -11
- package/dist/esm/ObjectId/index.js +0 -17
- package/dist/esm/aggregate.d.ts +0 -29
- package/dist/esm/aggregate.js +0 -121
- package/dist/esm/collection.d.ts +0 -17
- package/dist/esm/collection.js +0 -69
- package/dist/esm/command.d.ts +0 -175
- package/dist/esm/command.js +0 -281
- package/dist/esm/commands/logic.d.ts +0 -24
- package/dist/esm/commands/logic.js +0 -74
- package/dist/esm/commands/query.d.ts +0 -73
- package/dist/esm/commands/query.js +0 -141
- package/dist/esm/commands/update.d.ts +0 -29
- package/dist/esm/commands/update.js +0 -44
- package/dist/esm/config/app.config.d.ts +0 -6
- package/dist/esm/config/app.config.js +0 -5
- package/dist/esm/config/error.config.d.ts +0 -3
- package/dist/esm/config/error.config.js +0 -13
- package/dist/esm/const/code.d.ts +0 -34
- package/dist/esm/const/code.js +0 -34
- package/dist/esm/constant.d.ts +0 -54
- package/dist/esm/constant.js +0 -67
- package/dist/esm/document.d.ts +0 -14
- package/dist/esm/document.js +0 -216
- package/dist/esm/geo/index.d.ts +0 -6
- package/dist/esm/geo/index.js +0 -6
- package/dist/esm/geo/interface.d.ts +0 -24
- package/dist/esm/geo/interface.js +0 -0
- package/dist/esm/geo/lineString.d.ts +0 -19
- package/dist/esm/geo/lineString.js +0 -54
- package/dist/esm/geo/multiLineString.d.ts +0 -18
- package/dist/esm/geo/multiLineString.js +0 -53
- package/dist/esm/geo/multiPoint.d.ts +0 -18
- package/dist/esm/geo/multiPoint.js +0 -47
- package/dist/esm/geo/multiPolygon.d.ts +0 -18
- package/dist/esm/geo/multiPolygon.js +0 -59
- package/dist/esm/geo/point.d.ts +0 -19
- package/dist/esm/geo/point.js +0 -40
- package/dist/esm/geo/polygon.d.ts +0 -19
- package/dist/esm/geo/polygon.js +0 -67
- package/dist/esm/helper/symbol.d.ts +0 -15
- package/dist/esm/helper/symbol.js +0 -15
- package/dist/esm/index.d.ts +0 -50
- package/dist/esm/index.js +0 -35
- package/dist/esm/operator-map.d.ts +0 -4
- package/dist/esm/operator-map.js +0 -20
- package/dist/esm/query.d.ts +0 -38
- package/dist/esm/query.js +0 -244
- package/dist/esm/realtime/error.d.ts +0 -7
- package/dist/esm/realtime/error.js +0 -8
- package/dist/esm/realtime/listener.d.ts +0 -14
- package/dist/esm/realtime/listener.js +0 -14
- package/dist/esm/realtime/message.d.ts +0 -3
- package/dist/esm/realtime/message.js +0 -6
- package/dist/esm/realtime/snapshot.d.ts +0 -16
- package/dist/esm/realtime/snapshot.js +0 -39
- package/dist/esm/realtime/virtual-websocket-client.d.ts +0 -63
- package/dist/esm/realtime/virtual-websocket-client.js +0 -733
- package/dist/esm/realtime/websocket-client.d.ts +0 -78
- package/dist/esm/realtime/websocket-client.js +0 -564
- package/dist/esm/realtime/ws-event.d.ts +0 -126
- package/dist/esm/realtime/ws-event.js +0 -137
- package/dist/esm/regexp/index.d.ts +0 -18
- package/dist/esm/regexp/index.js +0 -26
- package/dist/esm/serializer/common.d.ts +0 -11
- package/dist/esm/serializer/common.js +0 -82
- package/dist/esm/serializer/datatype.d.ts +0 -7
- package/dist/esm/serializer/datatype.js +0 -95
- package/dist/esm/serializer/query.d.ts +0 -8
- package/dist/esm/serializer/query.js +0 -251
- package/dist/esm/serializer/update.d.ts +0 -16
- package/dist/esm/serializer/update.js +0 -139
- package/dist/esm/serverDate/index.d.ts +0 -13
- package/dist/esm/serverDate/index.js +0 -19
- package/dist/esm/transaction/index.d.ts +0 -30
- package/dist/esm/transaction/index.js +0 -100
- package/dist/esm/transaction.d.ts +0 -42
- package/dist/esm/transaction.js +0 -121
- package/dist/esm/typings/index.d.ts +0 -45
- package/dist/esm/typings/index.js +0 -0
- package/dist/esm/typings/realtime.d.ts +0 -219
- package/dist/esm/typings/realtime.js +0 -0
- package/dist/esm/util.d.ts +0 -9
- package/dist/esm/util.js +0 -98
- package/dist/esm/utils/error.d.ts +0 -37
- package/dist/esm/utils/error.js +0 -49
- package/dist/esm/utils/msg.d.ts +0 -0
- package/dist/esm/utils/msg.js +0 -0
- package/dist/esm/utils/symbol.d.ts +0 -8
- package/dist/esm/utils/symbol.js +0 -36
- package/dist/esm/utils/type.d.ts +0 -13
- package/dist/esm/utils/type.js +0 -20
- package/dist/esm/utils/utils.d.ts +0 -19
- package/dist/esm/utils/utils.js +0 -55
- package/dist/esm/validate.d.ts +0 -1
- package/dist/esm/validate.js +0 -152
|
@@ -1,567 +1,567 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const virtual_websocket_client_1 = require("./virtual-websocket-client");
|
|
4
|
-
const utils_1 = require("../utils/utils");
|
|
5
|
-
const message_1 = require("./message");
|
|
6
|
-
const ws_event_1 = require("./ws-event");
|
|
7
|
-
const error_1 = require("../utils/error");
|
|
8
|
-
const error_2 = require("./error");
|
|
9
|
-
const error_config_1 = require("../config/error.config");
|
|
10
|
-
const __1 = require("../");
|
|
11
|
-
const WS_READY_STATE = {
|
|
12
|
-
CONNECTING: 0,
|
|
13
|
-
OPEN: 1,
|
|
14
|
-
CLOSING: 2,
|
|
15
|
-
CLOSED: 3
|
|
16
|
-
};
|
|
17
|
-
const MAX_RTT_OBSERVED = 3;
|
|
18
|
-
const DEFAULT_EXPECTED_EVENT_WAIT_TIME = 5000;
|
|
19
|
-
const DEFAULT_UNTRUSTED_RTT_THRESHOLD = 10000;
|
|
20
|
-
const DEFAULT_MAX_RECONNECT = 5;
|
|
21
|
-
const DEFAULT_WS_RECONNECT_INTERVAL = 10000;
|
|
22
|
-
const DEFAULT_PING_FAIL_TOLERANCE = 2;
|
|
23
|
-
const DEFAULT_PONG_MISS_TOLERANCE = 2;
|
|
24
|
-
const DEFAULT_LOGIN_TIMEOUT = 5000;
|
|
25
|
-
class RealtimeWebSocketClient {
|
|
26
|
-
constructor(options) {
|
|
27
|
-
this._virtualWSClient = new Set();
|
|
28
|
-
this._queryIdClientMap = new Map();
|
|
29
|
-
this._watchIdClientMap = new Map();
|
|
30
|
-
this._pingFailed = 0;
|
|
31
|
-
this._pongMissed = 0;
|
|
32
|
-
this._logins = new Map();
|
|
33
|
-
this._wsReadySubsribers = [];
|
|
34
|
-
this._wsResponseWait = new Map();
|
|
35
|
-
this._rttObserved = [];
|
|
36
|
-
this.initWebSocketConnection = async (reconnect, availableRetries = this._maxReconnect) => {
|
|
37
|
-
if (reconnect && this._reconnectState) {
|
|
38
|
-
return;
|
|
39
|
-
}
|
|
40
|
-
if (reconnect) {
|
|
41
|
-
this._reconnectState = true;
|
|
42
|
-
}
|
|
43
|
-
if (this._wsInitPromise) {
|
|
44
|
-
return this._wsInitPromise;
|
|
45
|
-
}
|
|
46
|
-
if (reconnect) {
|
|
47
|
-
this.pauseClients();
|
|
48
|
-
}
|
|
49
|
-
this.close(ws_event_1.CLOSE_EVENT_CODE.ReconnectWebSocket);
|
|
50
|
-
this._wsInitPromise = new Promise(async (resolve, reject) => {
|
|
51
|
-
try {
|
|
52
|
-
const wsSign = await this.getWsSign();
|
|
53
|
-
await new Promise(success => {
|
|
54
|
-
const url = wsSign.wsUrl || 'wss://tcb-ws.tencentcloudapi.com';
|
|
55
|
-
this._ws = __1.Db.wsClass ? new __1.Db.wsClass(url) : new WebSocket(url);
|
|
56
|
-
success();
|
|
57
|
-
});
|
|
58
|
-
if (this._ws.connect) {
|
|
59
|
-
await this._ws.connect();
|
|
60
|
-
}
|
|
61
|
-
await this.initWebSocketEvent();
|
|
62
|
-
resolve();
|
|
63
|
-
if (reconnect) {
|
|
64
|
-
this.resumeClients();
|
|
65
|
-
this._reconnectState = false;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
catch (e) {
|
|
69
|
-
console.error('[realtime] initWebSocketConnection connect fail', e);
|
|
70
|
-
if (availableRetries > 0) {
|
|
71
|
-
const isConnected = true;
|
|
72
|
-
this._wsInitPromise = undefined;
|
|
73
|
-
if (isConnected) {
|
|
74
|
-
await utils_1.sleep(this._reconnectInterval);
|
|
75
|
-
if (reconnect) {
|
|
76
|
-
this._reconnectState = false;
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
resolve(this.initWebSocketConnection(reconnect, availableRetries - 1));
|
|
80
|
-
}
|
|
81
|
-
else {
|
|
82
|
-
reject(e);
|
|
83
|
-
if (reconnect) {
|
|
84
|
-
this.closeAllClients(new error_1.CloudSDKError({
|
|
85
|
-
errCode: error_config_1.ERR_CODE.SDK_DATABASE_REALTIME_LISTENER_RECONNECT_WATCH_FAIL,
|
|
86
|
-
errMsg: e
|
|
87
|
-
}));
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
});
|
|
92
|
-
try {
|
|
93
|
-
await this._wsInitPromise;
|
|
94
|
-
this._wsReadySubsribers.forEach(({ resolve }) => resolve());
|
|
95
|
-
}
|
|
96
|
-
catch (e) {
|
|
97
|
-
this._wsReadySubsribers.forEach(({ reject }) => reject());
|
|
98
|
-
}
|
|
99
|
-
finally {
|
|
100
|
-
this._wsInitPromise = undefined;
|
|
101
|
-
this._wsReadySubsribers = [];
|
|
102
|
-
}
|
|
103
|
-
};
|
|
104
|
-
this.initWebSocketEvent = () => new Promise((resolve, reject) => {
|
|
105
|
-
if (!this._ws) {
|
|
106
|
-
throw new Error('can not initWebSocketEvent, ws not exists');
|
|
107
|
-
}
|
|
108
|
-
let wsOpened = false;
|
|
109
|
-
this._ws.onopen = event => {
|
|
110
|
-
console.warn('[realtime] ws event: open', event);
|
|
111
|
-
wsOpened = true;
|
|
112
|
-
resolve();
|
|
113
|
-
};
|
|
114
|
-
this._ws.onerror = event => {
|
|
115
|
-
this._logins = new Map();
|
|
116
|
-
if (!wsOpened) {
|
|
117
|
-
console.error('[realtime] ws open failed with ws event: error', event);
|
|
118
|
-
reject(event);
|
|
119
|
-
}
|
|
120
|
-
else {
|
|
121
|
-
console.error('[realtime] ws event: error', event);
|
|
122
|
-
this.clearHeartbeat();
|
|
123
|
-
this._virtualWSClient.forEach(client => client.closeWithError(new error_1.CloudSDKError({
|
|
124
|
-
errCode: error_config_1.ERR_CODE.SDK_DATABASE_REALTIME_LISTENER_WEBSOCKET_CONNECTION_ERROR,
|
|
125
|
-
errMsg: event
|
|
126
|
-
})));
|
|
127
|
-
}
|
|
128
|
-
};
|
|
129
|
-
this._ws.onclose = closeEvent => {
|
|
130
|
-
console.warn('[realtime] ws event: close', closeEvent);
|
|
131
|
-
this._logins = new Map();
|
|
132
|
-
this.clearHeartbeat();
|
|
133
|
-
switch (closeEvent.code) {
|
|
134
|
-
case ws_event_1.CLOSE_EVENT_CODE.ReconnectWebSocket: {
|
|
135
|
-
break;
|
|
136
|
-
}
|
|
137
|
-
case ws_event_1.CLOSE_EVENT_CODE.NoRealtimeListeners: {
|
|
138
|
-
break;
|
|
139
|
-
}
|
|
140
|
-
case ws_event_1.CLOSE_EVENT_CODE.HeartbeatPingError:
|
|
141
|
-
case ws_event_1.CLOSE_EVENT_CODE.HeartbeatPongTimeoutError:
|
|
142
|
-
case ws_event_1.CLOSE_EVENT_CODE.NormalClosure:
|
|
143
|
-
case ws_event_1.CLOSE_EVENT_CODE.AbnormalClosure: {
|
|
144
|
-
if (this._maxReconnect > 0) {
|
|
145
|
-
this.initWebSocketConnection(true, this._maxReconnect);
|
|
146
|
-
}
|
|
147
|
-
else {
|
|
148
|
-
this.closeAllClients(ws_event_1.getWSCloseError(closeEvent.code));
|
|
149
|
-
}
|
|
150
|
-
break;
|
|
151
|
-
}
|
|
152
|
-
case ws_event_1.CLOSE_EVENT_CODE.NoAuthentication: {
|
|
153
|
-
this.closeAllClients(ws_event_1.getWSCloseError(closeEvent.code, closeEvent.reason));
|
|
154
|
-
break;
|
|
155
|
-
}
|
|
156
|
-
default: {
|
|
157
|
-
if (this._maxReconnect > 0) {
|
|
158
|
-
this.initWebSocketConnection(true, this._maxReconnect);
|
|
159
|
-
}
|
|
160
|
-
else {
|
|
161
|
-
this.closeAllClients(ws_event_1.getWSCloseError(closeEvent.code));
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
};
|
|
166
|
-
this._ws.onmessage = res => {
|
|
167
|
-
const rawMsg = res.data;
|
|
168
|
-
this.heartbeat();
|
|
169
|
-
let msg;
|
|
170
|
-
try {
|
|
171
|
-
msg = JSON.parse(rawMsg);
|
|
172
|
-
}
|
|
173
|
-
catch (e) {
|
|
174
|
-
throw new Error(`[realtime] onMessage parse res.data error: ${e}`);
|
|
175
|
-
}
|
|
176
|
-
if (msg.msgType === 'ERROR') {
|
|
177
|
-
let virtualWatch = null;
|
|
178
|
-
this._virtualWSClient.forEach(item => {
|
|
179
|
-
if (item.watchId === msg.watchId) {
|
|
180
|
-
virtualWatch = item;
|
|
181
|
-
}
|
|
182
|
-
});
|
|
183
|
-
if (virtualWatch) {
|
|
184
|
-
virtualWatch.listener.onError(msg);
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
const responseWaitSpec = this._wsResponseWait.get(msg.requestId);
|
|
188
|
-
if (responseWaitSpec) {
|
|
189
|
-
try {
|
|
190
|
-
if (msg.msgType === 'ERROR') {
|
|
191
|
-
responseWaitSpec.reject(new error_2.RealtimeErrorMessageError(msg));
|
|
192
|
-
}
|
|
193
|
-
else {
|
|
194
|
-
responseWaitSpec.resolve(msg);
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
catch (e) {
|
|
198
|
-
console.error('ws onMessage responseWaitSpec.resolve(msg) errored:', e);
|
|
199
|
-
}
|
|
200
|
-
finally {
|
|
201
|
-
this._wsResponseWait.delete(msg.requestId);
|
|
202
|
-
}
|
|
203
|
-
if (responseWaitSpec.skipOnMessage) {
|
|
204
|
-
return;
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
if (msg.msgType === 'PONG') {
|
|
208
|
-
if (this._lastPingSendTS) {
|
|
209
|
-
const rtt = Date.now() - this._lastPingSendTS;
|
|
210
|
-
if (rtt > DEFAULT_UNTRUSTED_RTT_THRESHOLD) {
|
|
211
|
-
console.warn(`[realtime] untrusted rtt observed: ${rtt}`);
|
|
212
|
-
return;
|
|
213
|
-
}
|
|
214
|
-
if (this._rttObserved.length >= MAX_RTT_OBSERVED) {
|
|
215
|
-
this._rttObserved.splice(0, this._rttObserved.length - MAX_RTT_OBSERVED + 1);
|
|
216
|
-
}
|
|
217
|
-
this._rttObserved.push(rtt);
|
|
218
|
-
}
|
|
219
|
-
return;
|
|
220
|
-
}
|
|
221
|
-
let client = msg.watchId && this._watchIdClientMap.get(msg.watchId);
|
|
222
|
-
if (client) {
|
|
223
|
-
client.onMessage(msg);
|
|
224
|
-
}
|
|
225
|
-
else {
|
|
226
|
-
console.error(`[realtime] no realtime listener found responsible for watchId ${msg.watchId}: `, msg);
|
|
227
|
-
switch (msg.msgType) {
|
|
228
|
-
case 'INIT_EVENT':
|
|
229
|
-
case 'NEXT_EVENT':
|
|
230
|
-
case 'CHECK_EVENT': {
|
|
231
|
-
client = this._queryIdClientMap.get(msg.msgData.queryID);
|
|
232
|
-
if (client) {
|
|
233
|
-
client.onMessage(msg);
|
|
234
|
-
}
|
|
235
|
-
break;
|
|
236
|
-
}
|
|
237
|
-
default: {
|
|
238
|
-
for (const [, client] of this._watchIdClientMap) {
|
|
239
|
-
client.onMessage(msg);
|
|
240
|
-
break;
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
};
|
|
246
|
-
this.heartbeat();
|
|
247
|
-
});
|
|
248
|
-
this.isWSConnected = () => {
|
|
249
|
-
return Boolean(this._ws && this._ws.readyState === WS_READY_STATE.OPEN);
|
|
250
|
-
};
|
|
251
|
-
this.onceWSConnected = async () => {
|
|
252
|
-
if (this.isWSConnected()) {
|
|
253
|
-
return;
|
|
254
|
-
}
|
|
255
|
-
if (this._wsInitPromise) {
|
|
256
|
-
return this._wsInitPromise;
|
|
257
|
-
}
|
|
258
|
-
return new Promise((resolve, reject) => {
|
|
259
|
-
this._wsReadySubsribers.push({
|
|
260
|
-
resolve,
|
|
261
|
-
reject
|
|
262
|
-
});
|
|
263
|
-
});
|
|
264
|
-
};
|
|
265
|
-
this.webLogin = async (envId, refresh) => {
|
|
266
|
-
if (!refresh) {
|
|
267
|
-
if (envId) {
|
|
268
|
-
const loginInfo = this._logins.get(envId);
|
|
269
|
-
if (loginInfo) {
|
|
270
|
-
if (loginInfo.loggedIn && loginInfo.loginResult) {
|
|
271
|
-
return loginInfo.loginResult;
|
|
272
|
-
}
|
|
273
|
-
else if (loginInfo.loggingInPromise) {
|
|
274
|
-
return loginInfo.loggingInPromise;
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
else {
|
|
279
|
-
const emptyEnvLoginInfo = this._logins.get('');
|
|
280
|
-
if (emptyEnvLoginInfo && emptyEnvLoginInfo.loggingInPromise) {
|
|
281
|
-
return emptyEnvLoginInfo.loggingInPromise;
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
const promise = new Promise(async (resolve, reject) => {
|
|
286
|
-
try {
|
|
287
|
-
const wsSign = await this.getWsSign();
|
|
288
|
-
const msgData = {
|
|
289
|
-
envId: wsSign.envId || '',
|
|
290
|
-
accessToken: '',
|
|
291
|
-
referrer: 'web',
|
|
292
|
-
sdkVersion: '',
|
|
293
|
-
dataVersion: __1.Db.dataVersion || ''
|
|
294
|
-
};
|
|
295
|
-
const loginMsg = {
|
|
296
|
-
watchId: undefined,
|
|
297
|
-
requestId: message_1.genRequestId(),
|
|
298
|
-
msgType: 'LOGIN',
|
|
299
|
-
msgData,
|
|
300
|
-
exMsgData: {
|
|
301
|
-
runtime: __1.Db.runtime,
|
|
302
|
-
signStr: wsSign.signStr,
|
|
303
|
-
secretVersion: wsSign.secretVersion
|
|
304
|
-
}
|
|
305
|
-
};
|
|
306
|
-
const loginResMsg = await this.send({
|
|
307
|
-
msg: loginMsg,
|
|
308
|
-
waitResponse: true,
|
|
309
|
-
skipOnMessage: true,
|
|
310
|
-
timeout: DEFAULT_LOGIN_TIMEOUT
|
|
311
|
-
});
|
|
312
|
-
if (!loginResMsg.msgData.code) {
|
|
313
|
-
resolve({
|
|
314
|
-
envId: wsSign.envId
|
|
315
|
-
});
|
|
316
|
-
}
|
|
317
|
-
else {
|
|
318
|
-
reject(new Error(`${loginResMsg.msgData.code} ${loginResMsg.msgData.message}`));
|
|
319
|
-
}
|
|
320
|
-
}
|
|
321
|
-
catch (e) {
|
|
322
|
-
reject(e);
|
|
323
|
-
}
|
|
324
|
-
});
|
|
325
|
-
let loginInfo = envId && this._logins.get(envId);
|
|
326
|
-
const loginStartTS = Date.now();
|
|
327
|
-
if (loginInfo) {
|
|
328
|
-
loginInfo.loggedIn = false;
|
|
329
|
-
loginInfo.loggingInPromise = promise;
|
|
330
|
-
loginInfo.loginStartTS = loginStartTS;
|
|
331
|
-
}
|
|
332
|
-
else {
|
|
333
|
-
loginInfo = {
|
|
334
|
-
loggedIn: false,
|
|
335
|
-
loggingInPromise: promise,
|
|
336
|
-
loginStartTS
|
|
337
|
-
};
|
|
338
|
-
this._logins.set(envId || '', loginInfo);
|
|
339
|
-
}
|
|
340
|
-
try {
|
|
341
|
-
const loginResult = await promise;
|
|
342
|
-
const curLoginInfo = envId && this._logins.get(envId);
|
|
343
|
-
if (curLoginInfo &&
|
|
344
|
-
curLoginInfo === loginInfo &&
|
|
345
|
-
curLoginInfo.loginStartTS === loginStartTS) {
|
|
346
|
-
loginInfo.loggedIn = true;
|
|
347
|
-
loginInfo.loggingInPromise = undefined;
|
|
348
|
-
loginInfo.loginStartTS = undefined;
|
|
349
|
-
loginInfo.loginResult = loginResult;
|
|
350
|
-
return loginResult;
|
|
351
|
-
}
|
|
352
|
-
else if (curLoginInfo) {
|
|
353
|
-
if (curLoginInfo.loggedIn && curLoginInfo.loginResult) {
|
|
354
|
-
return curLoginInfo.loginResult;
|
|
355
|
-
}
|
|
356
|
-
else if (curLoginInfo.loggingInPromise) {
|
|
357
|
-
return curLoginInfo.loggingInPromise;
|
|
358
|
-
}
|
|
359
|
-
else {
|
|
360
|
-
throw new Error('ws unexpected login info');
|
|
361
|
-
}
|
|
362
|
-
}
|
|
363
|
-
else {
|
|
364
|
-
throw new Error('ws login info reset');
|
|
365
|
-
}
|
|
366
|
-
}
|
|
367
|
-
catch (e) {
|
|
368
|
-
loginInfo.loggedIn = false;
|
|
369
|
-
loginInfo.loggingInPromise = undefined;
|
|
370
|
-
loginInfo.loginStartTS = undefined;
|
|
371
|
-
loginInfo.loginResult = undefined;
|
|
372
|
-
throw e;
|
|
373
|
-
}
|
|
374
|
-
};
|
|
375
|
-
this.getWsSign = async () => {
|
|
376
|
-
if (this._wsSign && this._wsSign.expiredTs > Date.now()) {
|
|
377
|
-
return this._wsSign;
|
|
378
|
-
}
|
|
379
|
-
const expiredTs = Date.now() + 60000;
|
|
380
|
-
const res = await this._context.appConfig.request.send('auth.wsWebSign', { runtime: __1.Db.runtime });
|
|
381
|
-
if (res.code) {
|
|
382
|
-
throw new Error(`[tcb-js-sdk] 获取实时数据推送登录票据失败: ${res.code}`);
|
|
383
|
-
}
|
|
384
|
-
if (res.data) {
|
|
385
|
-
const { signStr, wsUrl, secretVersion, envId } = res.data;
|
|
386
|
-
return {
|
|
387
|
-
signStr,
|
|
388
|
-
wsUrl,
|
|
389
|
-
secretVersion,
|
|
390
|
-
envId,
|
|
391
|
-
expiredTs
|
|
392
|
-
};
|
|
393
|
-
}
|
|
394
|
-
else {
|
|
395
|
-
throw new Error('[tcb-js-sdk] 获取实时数据推送登录票据失败');
|
|
396
|
-
}
|
|
397
|
-
};
|
|
398
|
-
this.getWaitExpectedTimeoutLength = () => {
|
|
399
|
-
if (!this._rttObserved.length) {
|
|
400
|
-
return DEFAULT_EXPECTED_EVENT_WAIT_TIME;
|
|
401
|
-
}
|
|
402
|
-
return ((this._rttObserved.reduce((acc, cur) => acc + cur) /
|
|
403
|
-
this._rttObserved.length) *
|
|
404
|
-
1.5);
|
|
405
|
-
};
|
|
406
|
-
this.ping = async () => {
|
|
407
|
-
const msg = {
|
|
408
|
-
watchId: undefined,
|
|
409
|
-
requestId: message_1.genRequestId(),
|
|
410
|
-
msgType: 'PING',
|
|
411
|
-
msgData: null
|
|
412
|
-
};
|
|
413
|
-
await this.send({
|
|
414
|
-
msg
|
|
415
|
-
});
|
|
416
|
-
};
|
|
417
|
-
this.send = async (opts) => new Promise(async (_resolve, _reject) => {
|
|
418
|
-
let timeoutId;
|
|
419
|
-
let _hasResolved = false;
|
|
420
|
-
let _hasRejected = false;
|
|
421
|
-
const resolve = (value) => {
|
|
422
|
-
_hasResolved = true;
|
|
423
|
-
timeoutId && clearTimeout(timeoutId);
|
|
424
|
-
_resolve(value);
|
|
425
|
-
};
|
|
426
|
-
const reject = (error) => {
|
|
427
|
-
_hasRejected = true;
|
|
428
|
-
timeoutId && clearTimeout(timeoutId);
|
|
429
|
-
_reject(error);
|
|
430
|
-
};
|
|
431
|
-
if (opts.timeout) {
|
|
432
|
-
timeoutId = setTimeout(async () => {
|
|
433
|
-
if (!_hasResolved || !_hasRejected) {
|
|
434
|
-
await utils_1.sleep(0);
|
|
435
|
-
if (!_hasResolved || !_hasRejected) {
|
|
436
|
-
reject(new error_1.TimeoutError('wsclient.send timedout'));
|
|
437
|
-
}
|
|
438
|
-
}
|
|
439
|
-
}, opts.timeout);
|
|
440
|
-
}
|
|
441
|
-
try {
|
|
442
|
-
if (this._wsInitPromise) {
|
|
443
|
-
await this._wsInitPromise;
|
|
444
|
-
}
|
|
445
|
-
if (!this._ws) {
|
|
446
|
-
reject(new Error('invalid state: ws connection not exists, can not send message'));
|
|
447
|
-
return;
|
|
448
|
-
}
|
|
449
|
-
if (this._ws.readyState !== WS_READY_STATE.OPEN) {
|
|
450
|
-
reject(new Error(`ws readyState invalid: ${this._ws.readyState}, can not send message`));
|
|
451
|
-
return;
|
|
452
|
-
}
|
|
453
|
-
if (opts.waitResponse) {
|
|
454
|
-
this._wsResponseWait.set(opts.msg.requestId, {
|
|
455
|
-
resolve,
|
|
456
|
-
reject,
|
|
457
|
-
skipOnMessage: opts.skipOnMessage
|
|
458
|
-
});
|
|
459
|
-
}
|
|
460
|
-
try {
|
|
461
|
-
await this._ws.send(JSON.stringify(opts.msg));
|
|
462
|
-
if (!opts.waitResponse) {
|
|
463
|
-
resolve();
|
|
464
|
-
}
|
|
465
|
-
}
|
|
466
|
-
catch (err) {
|
|
467
|
-
if (err) {
|
|
468
|
-
reject(err);
|
|
469
|
-
if (opts.waitResponse) {
|
|
470
|
-
this._wsResponseWait.delete(opts.msg.requestId);
|
|
471
|
-
}
|
|
472
|
-
}
|
|
473
|
-
}
|
|
474
|
-
}
|
|
475
|
-
catch (e) {
|
|
476
|
-
reject(e);
|
|
477
|
-
}
|
|
478
|
-
});
|
|
479
|
-
this.closeAllClients = (error) => {
|
|
480
|
-
this._virtualWSClient.forEach(client => {
|
|
481
|
-
client.closeWithError(error);
|
|
482
|
-
});
|
|
483
|
-
};
|
|
484
|
-
this.pauseClients = (clients) => {
|
|
485
|
-
;
|
|
486
|
-
(clients || this._virtualWSClient).forEach(client => {
|
|
487
|
-
client.pause();
|
|
488
|
-
});
|
|
489
|
-
};
|
|
490
|
-
this.resumeClients = (clients) => {
|
|
491
|
-
;
|
|
492
|
-
(clients || this._virtualWSClient).forEach(client => {
|
|
493
|
-
client.resume();
|
|
494
|
-
});
|
|
495
|
-
};
|
|
496
|
-
this.onWatchStart = (client, queryID) => {
|
|
497
|
-
this._queryIdClientMap.set(queryID, client);
|
|
498
|
-
};
|
|
499
|
-
this.onWatchClose = (client, queryID) => {
|
|
500
|
-
if (queryID) {
|
|
501
|
-
this._queryIdClientMap.delete(queryID);
|
|
502
|
-
}
|
|
503
|
-
this._watchIdClientMap.delete(client.watchId);
|
|
504
|
-
this._virtualWSClient.delete(client);
|
|
505
|
-
if (!this._virtualWSClient.size) {
|
|
506
|
-
this.close(ws_event_1.CLOSE_EVENT_CODE.NoRealtimeListeners);
|
|
507
|
-
}
|
|
508
|
-
};
|
|
509
|
-
this._maxReconnect = options.maxReconnect || DEFAULT_MAX_RECONNECT;
|
|
510
|
-
this._reconnectInterval =
|
|
511
|
-
options.reconnectInterval || DEFAULT_WS_RECONNECT_INTERVAL;
|
|
512
|
-
this._context = options.context;
|
|
513
|
-
}
|
|
514
|
-
heartbeat(immediate) {
|
|
515
|
-
this.clearHeartbeat();
|
|
516
|
-
this._pingTimeoutId = setTimeout(async () => {
|
|
517
|
-
try {
|
|
518
|
-
if (!this._ws || this._ws.readyState !== WS_READY_STATE.OPEN) {
|
|
519
|
-
return;
|
|
520
|
-
}
|
|
521
|
-
this._lastPingSendTS = Date.now();
|
|
522
|
-
await this.ping();
|
|
523
|
-
this._pingFailed = 0;
|
|
524
|
-
this._pongTimeoutId = setTimeout(() => {
|
|
525
|
-
console.error('pong timed out');
|
|
526
|
-
if (this._pongMissed < DEFAULT_PONG_MISS_TOLERANCE) {
|
|
527
|
-
this._pongMissed++;
|
|
528
|
-
this.heartbeat(true);
|
|
529
|
-
}
|
|
530
|
-
else {
|
|
531
|
-
this.initWebSocketConnection(true);
|
|
532
|
-
}
|
|
533
|
-
}, this._context.appConfig.realtimePongWaitTimeout);
|
|
534
|
-
}
|
|
535
|
-
catch (e) {
|
|
536
|
-
if (this._pingFailed < DEFAULT_PING_FAIL_TOLERANCE) {
|
|
537
|
-
this._pingFailed++;
|
|
538
|
-
this.heartbeat();
|
|
539
|
-
}
|
|
540
|
-
else {
|
|
541
|
-
this.close(ws_event_1.CLOSE_EVENT_CODE.HeartbeatPingError);
|
|
542
|
-
}
|
|
543
|
-
}
|
|
544
|
-
}, immediate ? 0 : this._context.appConfig.realtimePingInterval);
|
|
545
|
-
}
|
|
546
|
-
clearHeartbeat() {
|
|
547
|
-
this._pingTimeoutId && clearTimeout(this._pingTimeoutId);
|
|
548
|
-
this._pongTimeoutId && clearTimeout(this._pongTimeoutId);
|
|
549
|
-
}
|
|
550
|
-
close(code) {
|
|
551
|
-
this.clearHeartbeat();
|
|
552
|
-
if (this._ws) {
|
|
553
|
-
this._ws.close(code, ws_event_1.CLOSE_EVENT_CODE_INFO[code].name);
|
|
554
|
-
this._ws = undefined;
|
|
555
|
-
}
|
|
556
|
-
}
|
|
557
|
-
watch(options) {
|
|
558
|
-
if (!this._ws && !this._wsInitPromise) {
|
|
559
|
-
this.initWebSocketConnection(false);
|
|
560
|
-
}
|
|
561
|
-
const virtualClient = new virtual_websocket_client_1.VirtualWebSocketClient(Object.assign(Object.assign({}, options), { send: this.send, login: this.webLogin, isWSConnected: this.isWSConnected, onceWSConnected: this.onceWSConnected, getWaitExpectedTimeoutLength: this.getWaitExpectedTimeoutLength, onWatchStart: this.onWatchStart, onWatchClose: this.onWatchClose, debug: true }));
|
|
562
|
-
this._virtualWSClient.add(virtualClient);
|
|
563
|
-
this._watchIdClientMap.set(virtualClient.watchId, virtualClient);
|
|
564
|
-
return virtualClient.listener;
|
|
565
|
-
}
|
|
566
|
-
}
|
|
567
|
-
exports.RealtimeWebSocketClient = RealtimeWebSocketClient;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const virtual_websocket_client_1 = require("./virtual-websocket-client");
|
|
4
|
+
const utils_1 = require("../utils/utils");
|
|
5
|
+
const message_1 = require("./message");
|
|
6
|
+
const ws_event_1 = require("./ws-event");
|
|
7
|
+
const error_1 = require("../utils/error");
|
|
8
|
+
const error_2 = require("./error");
|
|
9
|
+
const error_config_1 = require("../config/error.config");
|
|
10
|
+
const __1 = require("../");
|
|
11
|
+
const WS_READY_STATE = {
|
|
12
|
+
CONNECTING: 0,
|
|
13
|
+
OPEN: 1,
|
|
14
|
+
CLOSING: 2,
|
|
15
|
+
CLOSED: 3
|
|
16
|
+
};
|
|
17
|
+
const MAX_RTT_OBSERVED = 3;
|
|
18
|
+
const DEFAULT_EXPECTED_EVENT_WAIT_TIME = 5000;
|
|
19
|
+
const DEFAULT_UNTRUSTED_RTT_THRESHOLD = 10000;
|
|
20
|
+
const DEFAULT_MAX_RECONNECT = 5;
|
|
21
|
+
const DEFAULT_WS_RECONNECT_INTERVAL = 10000;
|
|
22
|
+
const DEFAULT_PING_FAIL_TOLERANCE = 2;
|
|
23
|
+
const DEFAULT_PONG_MISS_TOLERANCE = 2;
|
|
24
|
+
const DEFAULT_LOGIN_TIMEOUT = 5000;
|
|
25
|
+
class RealtimeWebSocketClient {
|
|
26
|
+
constructor(options) {
|
|
27
|
+
this._virtualWSClient = new Set();
|
|
28
|
+
this._queryIdClientMap = new Map();
|
|
29
|
+
this._watchIdClientMap = new Map();
|
|
30
|
+
this._pingFailed = 0;
|
|
31
|
+
this._pongMissed = 0;
|
|
32
|
+
this._logins = new Map();
|
|
33
|
+
this._wsReadySubsribers = [];
|
|
34
|
+
this._wsResponseWait = new Map();
|
|
35
|
+
this._rttObserved = [];
|
|
36
|
+
this.initWebSocketConnection = async (reconnect, availableRetries = this._maxReconnect) => {
|
|
37
|
+
if (reconnect && this._reconnectState) {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
if (reconnect) {
|
|
41
|
+
this._reconnectState = true;
|
|
42
|
+
}
|
|
43
|
+
if (this._wsInitPromise) {
|
|
44
|
+
return this._wsInitPromise;
|
|
45
|
+
}
|
|
46
|
+
if (reconnect) {
|
|
47
|
+
this.pauseClients();
|
|
48
|
+
}
|
|
49
|
+
this.close(ws_event_1.CLOSE_EVENT_CODE.ReconnectWebSocket);
|
|
50
|
+
this._wsInitPromise = new Promise(async (resolve, reject) => {
|
|
51
|
+
try {
|
|
52
|
+
const wsSign = await this.getWsSign();
|
|
53
|
+
await new Promise(success => {
|
|
54
|
+
const url = wsSign.wsUrl || 'wss://tcb-ws.tencentcloudapi.com';
|
|
55
|
+
this._ws = __1.Db.wsClass ? new __1.Db.wsClass(url) : new WebSocket(url);
|
|
56
|
+
success();
|
|
57
|
+
});
|
|
58
|
+
if (this._ws.connect) {
|
|
59
|
+
await this._ws.connect();
|
|
60
|
+
}
|
|
61
|
+
await this.initWebSocketEvent();
|
|
62
|
+
resolve();
|
|
63
|
+
if (reconnect) {
|
|
64
|
+
this.resumeClients();
|
|
65
|
+
this._reconnectState = false;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
catch (e) {
|
|
69
|
+
console.error('[realtime] initWebSocketConnection connect fail', e);
|
|
70
|
+
if (availableRetries > 0) {
|
|
71
|
+
const isConnected = true;
|
|
72
|
+
this._wsInitPromise = undefined;
|
|
73
|
+
if (isConnected) {
|
|
74
|
+
await utils_1.sleep(this._reconnectInterval);
|
|
75
|
+
if (reconnect) {
|
|
76
|
+
this._reconnectState = false;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
resolve(this.initWebSocketConnection(reconnect, availableRetries - 1));
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
reject(e);
|
|
83
|
+
if (reconnect) {
|
|
84
|
+
this.closeAllClients(new error_1.CloudSDKError({
|
|
85
|
+
errCode: error_config_1.ERR_CODE.SDK_DATABASE_REALTIME_LISTENER_RECONNECT_WATCH_FAIL,
|
|
86
|
+
errMsg: e
|
|
87
|
+
}));
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
try {
|
|
93
|
+
await this._wsInitPromise;
|
|
94
|
+
this._wsReadySubsribers.forEach(({ resolve }) => resolve());
|
|
95
|
+
}
|
|
96
|
+
catch (e) {
|
|
97
|
+
this._wsReadySubsribers.forEach(({ reject }) => reject());
|
|
98
|
+
}
|
|
99
|
+
finally {
|
|
100
|
+
this._wsInitPromise = undefined;
|
|
101
|
+
this._wsReadySubsribers = [];
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
this.initWebSocketEvent = () => new Promise((resolve, reject) => {
|
|
105
|
+
if (!this._ws) {
|
|
106
|
+
throw new Error('can not initWebSocketEvent, ws not exists');
|
|
107
|
+
}
|
|
108
|
+
let wsOpened = false;
|
|
109
|
+
this._ws.onopen = event => {
|
|
110
|
+
console.warn('[realtime] ws event: open', event);
|
|
111
|
+
wsOpened = true;
|
|
112
|
+
resolve();
|
|
113
|
+
};
|
|
114
|
+
this._ws.onerror = event => {
|
|
115
|
+
this._logins = new Map();
|
|
116
|
+
if (!wsOpened) {
|
|
117
|
+
console.error('[realtime] ws open failed with ws event: error', event);
|
|
118
|
+
reject(event);
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
console.error('[realtime] ws event: error', event);
|
|
122
|
+
this.clearHeartbeat();
|
|
123
|
+
this._virtualWSClient.forEach(client => client.closeWithError(new error_1.CloudSDKError({
|
|
124
|
+
errCode: error_config_1.ERR_CODE.SDK_DATABASE_REALTIME_LISTENER_WEBSOCKET_CONNECTION_ERROR,
|
|
125
|
+
errMsg: event
|
|
126
|
+
})));
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
this._ws.onclose = closeEvent => {
|
|
130
|
+
console.warn('[realtime] ws event: close', closeEvent);
|
|
131
|
+
this._logins = new Map();
|
|
132
|
+
this.clearHeartbeat();
|
|
133
|
+
switch (closeEvent.code) {
|
|
134
|
+
case ws_event_1.CLOSE_EVENT_CODE.ReconnectWebSocket: {
|
|
135
|
+
break;
|
|
136
|
+
}
|
|
137
|
+
case ws_event_1.CLOSE_EVENT_CODE.NoRealtimeListeners: {
|
|
138
|
+
break;
|
|
139
|
+
}
|
|
140
|
+
case ws_event_1.CLOSE_EVENT_CODE.HeartbeatPingError:
|
|
141
|
+
case ws_event_1.CLOSE_EVENT_CODE.HeartbeatPongTimeoutError:
|
|
142
|
+
case ws_event_1.CLOSE_EVENT_CODE.NormalClosure:
|
|
143
|
+
case ws_event_1.CLOSE_EVENT_CODE.AbnormalClosure: {
|
|
144
|
+
if (this._maxReconnect > 0) {
|
|
145
|
+
this.initWebSocketConnection(true, this._maxReconnect);
|
|
146
|
+
}
|
|
147
|
+
else {
|
|
148
|
+
this.closeAllClients(ws_event_1.getWSCloseError(closeEvent.code));
|
|
149
|
+
}
|
|
150
|
+
break;
|
|
151
|
+
}
|
|
152
|
+
case ws_event_1.CLOSE_EVENT_CODE.NoAuthentication: {
|
|
153
|
+
this.closeAllClients(ws_event_1.getWSCloseError(closeEvent.code, closeEvent.reason));
|
|
154
|
+
break;
|
|
155
|
+
}
|
|
156
|
+
default: {
|
|
157
|
+
if (this._maxReconnect > 0) {
|
|
158
|
+
this.initWebSocketConnection(true, this._maxReconnect);
|
|
159
|
+
}
|
|
160
|
+
else {
|
|
161
|
+
this.closeAllClients(ws_event_1.getWSCloseError(closeEvent.code));
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
this._ws.onmessage = res => {
|
|
167
|
+
const rawMsg = res.data;
|
|
168
|
+
this.heartbeat();
|
|
169
|
+
let msg;
|
|
170
|
+
try {
|
|
171
|
+
msg = JSON.parse(rawMsg);
|
|
172
|
+
}
|
|
173
|
+
catch (e) {
|
|
174
|
+
throw new Error(`[realtime] onMessage parse res.data error: ${e}`);
|
|
175
|
+
}
|
|
176
|
+
if (msg.msgType === 'ERROR') {
|
|
177
|
+
let virtualWatch = null;
|
|
178
|
+
this._virtualWSClient.forEach(item => {
|
|
179
|
+
if (item.watchId === msg.watchId) {
|
|
180
|
+
virtualWatch = item;
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
if (virtualWatch) {
|
|
184
|
+
virtualWatch.listener.onError(msg);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
const responseWaitSpec = this._wsResponseWait.get(msg.requestId);
|
|
188
|
+
if (responseWaitSpec) {
|
|
189
|
+
try {
|
|
190
|
+
if (msg.msgType === 'ERROR') {
|
|
191
|
+
responseWaitSpec.reject(new error_2.RealtimeErrorMessageError(msg));
|
|
192
|
+
}
|
|
193
|
+
else {
|
|
194
|
+
responseWaitSpec.resolve(msg);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
catch (e) {
|
|
198
|
+
console.error('ws onMessage responseWaitSpec.resolve(msg) errored:', e);
|
|
199
|
+
}
|
|
200
|
+
finally {
|
|
201
|
+
this._wsResponseWait.delete(msg.requestId);
|
|
202
|
+
}
|
|
203
|
+
if (responseWaitSpec.skipOnMessage) {
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
if (msg.msgType === 'PONG') {
|
|
208
|
+
if (this._lastPingSendTS) {
|
|
209
|
+
const rtt = Date.now() - this._lastPingSendTS;
|
|
210
|
+
if (rtt > DEFAULT_UNTRUSTED_RTT_THRESHOLD) {
|
|
211
|
+
console.warn(`[realtime] untrusted rtt observed: ${rtt}`);
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
if (this._rttObserved.length >= MAX_RTT_OBSERVED) {
|
|
215
|
+
this._rttObserved.splice(0, this._rttObserved.length - MAX_RTT_OBSERVED + 1);
|
|
216
|
+
}
|
|
217
|
+
this._rttObserved.push(rtt);
|
|
218
|
+
}
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
let client = msg.watchId && this._watchIdClientMap.get(msg.watchId);
|
|
222
|
+
if (client) {
|
|
223
|
+
client.onMessage(msg);
|
|
224
|
+
}
|
|
225
|
+
else {
|
|
226
|
+
console.error(`[realtime] no realtime listener found responsible for watchId ${msg.watchId}: `, msg);
|
|
227
|
+
switch (msg.msgType) {
|
|
228
|
+
case 'INIT_EVENT':
|
|
229
|
+
case 'NEXT_EVENT':
|
|
230
|
+
case 'CHECK_EVENT': {
|
|
231
|
+
client = this._queryIdClientMap.get(msg.msgData.queryID);
|
|
232
|
+
if (client) {
|
|
233
|
+
client.onMessage(msg);
|
|
234
|
+
}
|
|
235
|
+
break;
|
|
236
|
+
}
|
|
237
|
+
default: {
|
|
238
|
+
for (const [, client] of this._watchIdClientMap) {
|
|
239
|
+
client.onMessage(msg);
|
|
240
|
+
break;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
};
|
|
246
|
+
this.heartbeat();
|
|
247
|
+
});
|
|
248
|
+
this.isWSConnected = () => {
|
|
249
|
+
return Boolean(this._ws && this._ws.readyState === WS_READY_STATE.OPEN);
|
|
250
|
+
};
|
|
251
|
+
this.onceWSConnected = async () => {
|
|
252
|
+
if (this.isWSConnected()) {
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
if (this._wsInitPromise) {
|
|
256
|
+
return this._wsInitPromise;
|
|
257
|
+
}
|
|
258
|
+
return new Promise((resolve, reject) => {
|
|
259
|
+
this._wsReadySubsribers.push({
|
|
260
|
+
resolve,
|
|
261
|
+
reject
|
|
262
|
+
});
|
|
263
|
+
});
|
|
264
|
+
};
|
|
265
|
+
this.webLogin = async (envId, refresh) => {
|
|
266
|
+
if (!refresh) {
|
|
267
|
+
if (envId) {
|
|
268
|
+
const loginInfo = this._logins.get(envId);
|
|
269
|
+
if (loginInfo) {
|
|
270
|
+
if (loginInfo.loggedIn && loginInfo.loginResult) {
|
|
271
|
+
return loginInfo.loginResult;
|
|
272
|
+
}
|
|
273
|
+
else if (loginInfo.loggingInPromise) {
|
|
274
|
+
return loginInfo.loggingInPromise;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
else {
|
|
279
|
+
const emptyEnvLoginInfo = this._logins.get('');
|
|
280
|
+
if (emptyEnvLoginInfo && emptyEnvLoginInfo.loggingInPromise) {
|
|
281
|
+
return emptyEnvLoginInfo.loggingInPromise;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
const promise = new Promise(async (resolve, reject) => {
|
|
286
|
+
try {
|
|
287
|
+
const wsSign = await this.getWsSign();
|
|
288
|
+
const msgData = {
|
|
289
|
+
envId: wsSign.envId || '',
|
|
290
|
+
accessToken: '',
|
|
291
|
+
referrer: 'web',
|
|
292
|
+
sdkVersion: '',
|
|
293
|
+
dataVersion: __1.Db.dataVersion || ''
|
|
294
|
+
};
|
|
295
|
+
const loginMsg = {
|
|
296
|
+
watchId: undefined,
|
|
297
|
+
requestId: message_1.genRequestId(),
|
|
298
|
+
msgType: 'LOGIN',
|
|
299
|
+
msgData,
|
|
300
|
+
exMsgData: {
|
|
301
|
+
runtime: __1.Db.runtime,
|
|
302
|
+
signStr: wsSign.signStr,
|
|
303
|
+
secretVersion: wsSign.secretVersion
|
|
304
|
+
}
|
|
305
|
+
};
|
|
306
|
+
const loginResMsg = await this.send({
|
|
307
|
+
msg: loginMsg,
|
|
308
|
+
waitResponse: true,
|
|
309
|
+
skipOnMessage: true,
|
|
310
|
+
timeout: DEFAULT_LOGIN_TIMEOUT
|
|
311
|
+
});
|
|
312
|
+
if (!loginResMsg.msgData.code) {
|
|
313
|
+
resolve({
|
|
314
|
+
envId: wsSign.envId
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
else {
|
|
318
|
+
reject(new Error(`${loginResMsg.msgData.code} ${loginResMsg.msgData.message}`));
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
catch (e) {
|
|
322
|
+
reject(e);
|
|
323
|
+
}
|
|
324
|
+
});
|
|
325
|
+
let loginInfo = envId && this._logins.get(envId);
|
|
326
|
+
const loginStartTS = Date.now();
|
|
327
|
+
if (loginInfo) {
|
|
328
|
+
loginInfo.loggedIn = false;
|
|
329
|
+
loginInfo.loggingInPromise = promise;
|
|
330
|
+
loginInfo.loginStartTS = loginStartTS;
|
|
331
|
+
}
|
|
332
|
+
else {
|
|
333
|
+
loginInfo = {
|
|
334
|
+
loggedIn: false,
|
|
335
|
+
loggingInPromise: promise,
|
|
336
|
+
loginStartTS
|
|
337
|
+
};
|
|
338
|
+
this._logins.set(envId || '', loginInfo);
|
|
339
|
+
}
|
|
340
|
+
try {
|
|
341
|
+
const loginResult = await promise;
|
|
342
|
+
const curLoginInfo = envId && this._logins.get(envId);
|
|
343
|
+
if (curLoginInfo &&
|
|
344
|
+
curLoginInfo === loginInfo &&
|
|
345
|
+
curLoginInfo.loginStartTS === loginStartTS) {
|
|
346
|
+
loginInfo.loggedIn = true;
|
|
347
|
+
loginInfo.loggingInPromise = undefined;
|
|
348
|
+
loginInfo.loginStartTS = undefined;
|
|
349
|
+
loginInfo.loginResult = loginResult;
|
|
350
|
+
return loginResult;
|
|
351
|
+
}
|
|
352
|
+
else if (curLoginInfo) {
|
|
353
|
+
if (curLoginInfo.loggedIn && curLoginInfo.loginResult) {
|
|
354
|
+
return curLoginInfo.loginResult;
|
|
355
|
+
}
|
|
356
|
+
else if (curLoginInfo.loggingInPromise) {
|
|
357
|
+
return curLoginInfo.loggingInPromise;
|
|
358
|
+
}
|
|
359
|
+
else {
|
|
360
|
+
throw new Error('ws unexpected login info');
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
else {
|
|
364
|
+
throw new Error('ws login info reset');
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
catch (e) {
|
|
368
|
+
loginInfo.loggedIn = false;
|
|
369
|
+
loginInfo.loggingInPromise = undefined;
|
|
370
|
+
loginInfo.loginStartTS = undefined;
|
|
371
|
+
loginInfo.loginResult = undefined;
|
|
372
|
+
throw e;
|
|
373
|
+
}
|
|
374
|
+
};
|
|
375
|
+
this.getWsSign = async () => {
|
|
376
|
+
if (this._wsSign && this._wsSign.expiredTs > Date.now()) {
|
|
377
|
+
return this._wsSign;
|
|
378
|
+
}
|
|
379
|
+
const expiredTs = Date.now() + 60000;
|
|
380
|
+
const res = await this._context.appConfig.request.send('auth.wsWebSign', { runtime: __1.Db.runtime });
|
|
381
|
+
if (res.code) {
|
|
382
|
+
throw new Error(`[tcb-js-sdk] 获取实时数据推送登录票据失败: ${res.code}`);
|
|
383
|
+
}
|
|
384
|
+
if (res.data) {
|
|
385
|
+
const { signStr, wsUrl, secretVersion, envId } = res.data;
|
|
386
|
+
return {
|
|
387
|
+
signStr,
|
|
388
|
+
wsUrl,
|
|
389
|
+
secretVersion,
|
|
390
|
+
envId,
|
|
391
|
+
expiredTs
|
|
392
|
+
};
|
|
393
|
+
}
|
|
394
|
+
else {
|
|
395
|
+
throw new Error('[tcb-js-sdk] 获取实时数据推送登录票据失败');
|
|
396
|
+
}
|
|
397
|
+
};
|
|
398
|
+
this.getWaitExpectedTimeoutLength = () => {
|
|
399
|
+
if (!this._rttObserved.length) {
|
|
400
|
+
return DEFAULT_EXPECTED_EVENT_WAIT_TIME;
|
|
401
|
+
}
|
|
402
|
+
return ((this._rttObserved.reduce((acc, cur) => acc + cur) /
|
|
403
|
+
this._rttObserved.length) *
|
|
404
|
+
1.5);
|
|
405
|
+
};
|
|
406
|
+
this.ping = async () => {
|
|
407
|
+
const msg = {
|
|
408
|
+
watchId: undefined,
|
|
409
|
+
requestId: message_1.genRequestId(),
|
|
410
|
+
msgType: 'PING',
|
|
411
|
+
msgData: null
|
|
412
|
+
};
|
|
413
|
+
await this.send({
|
|
414
|
+
msg
|
|
415
|
+
});
|
|
416
|
+
};
|
|
417
|
+
this.send = async (opts) => new Promise(async (_resolve, _reject) => {
|
|
418
|
+
let timeoutId;
|
|
419
|
+
let _hasResolved = false;
|
|
420
|
+
let _hasRejected = false;
|
|
421
|
+
const resolve = (value) => {
|
|
422
|
+
_hasResolved = true;
|
|
423
|
+
timeoutId && clearTimeout(timeoutId);
|
|
424
|
+
_resolve(value);
|
|
425
|
+
};
|
|
426
|
+
const reject = (error) => {
|
|
427
|
+
_hasRejected = true;
|
|
428
|
+
timeoutId && clearTimeout(timeoutId);
|
|
429
|
+
_reject(error);
|
|
430
|
+
};
|
|
431
|
+
if (opts.timeout) {
|
|
432
|
+
timeoutId = setTimeout(async () => {
|
|
433
|
+
if (!_hasResolved || !_hasRejected) {
|
|
434
|
+
await utils_1.sleep(0);
|
|
435
|
+
if (!_hasResolved || !_hasRejected) {
|
|
436
|
+
reject(new error_1.TimeoutError('wsclient.send timedout'));
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
}, opts.timeout);
|
|
440
|
+
}
|
|
441
|
+
try {
|
|
442
|
+
if (this._wsInitPromise) {
|
|
443
|
+
await this._wsInitPromise;
|
|
444
|
+
}
|
|
445
|
+
if (!this._ws) {
|
|
446
|
+
reject(new Error('invalid state: ws connection not exists, can not send message'));
|
|
447
|
+
return;
|
|
448
|
+
}
|
|
449
|
+
if (this._ws.readyState !== WS_READY_STATE.OPEN) {
|
|
450
|
+
reject(new Error(`ws readyState invalid: ${this._ws.readyState}, can not send message`));
|
|
451
|
+
return;
|
|
452
|
+
}
|
|
453
|
+
if (opts.waitResponse) {
|
|
454
|
+
this._wsResponseWait.set(opts.msg.requestId, {
|
|
455
|
+
resolve,
|
|
456
|
+
reject,
|
|
457
|
+
skipOnMessage: opts.skipOnMessage
|
|
458
|
+
});
|
|
459
|
+
}
|
|
460
|
+
try {
|
|
461
|
+
await this._ws.send(JSON.stringify(opts.msg));
|
|
462
|
+
if (!opts.waitResponse) {
|
|
463
|
+
resolve();
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
catch (err) {
|
|
467
|
+
if (err) {
|
|
468
|
+
reject(err);
|
|
469
|
+
if (opts.waitResponse) {
|
|
470
|
+
this._wsResponseWait.delete(opts.msg.requestId);
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
catch (e) {
|
|
476
|
+
reject(e);
|
|
477
|
+
}
|
|
478
|
+
});
|
|
479
|
+
this.closeAllClients = (error) => {
|
|
480
|
+
this._virtualWSClient.forEach(client => {
|
|
481
|
+
client.closeWithError(error);
|
|
482
|
+
});
|
|
483
|
+
};
|
|
484
|
+
this.pauseClients = (clients) => {
|
|
485
|
+
;
|
|
486
|
+
(clients || this._virtualWSClient).forEach(client => {
|
|
487
|
+
client.pause();
|
|
488
|
+
});
|
|
489
|
+
};
|
|
490
|
+
this.resumeClients = (clients) => {
|
|
491
|
+
;
|
|
492
|
+
(clients || this._virtualWSClient).forEach(client => {
|
|
493
|
+
client.resume();
|
|
494
|
+
});
|
|
495
|
+
};
|
|
496
|
+
this.onWatchStart = (client, queryID) => {
|
|
497
|
+
this._queryIdClientMap.set(queryID, client);
|
|
498
|
+
};
|
|
499
|
+
this.onWatchClose = (client, queryID) => {
|
|
500
|
+
if (queryID) {
|
|
501
|
+
this._queryIdClientMap.delete(queryID);
|
|
502
|
+
}
|
|
503
|
+
this._watchIdClientMap.delete(client.watchId);
|
|
504
|
+
this._virtualWSClient.delete(client);
|
|
505
|
+
if (!this._virtualWSClient.size) {
|
|
506
|
+
this.close(ws_event_1.CLOSE_EVENT_CODE.NoRealtimeListeners);
|
|
507
|
+
}
|
|
508
|
+
};
|
|
509
|
+
this._maxReconnect = options.maxReconnect || DEFAULT_MAX_RECONNECT;
|
|
510
|
+
this._reconnectInterval =
|
|
511
|
+
options.reconnectInterval || DEFAULT_WS_RECONNECT_INTERVAL;
|
|
512
|
+
this._context = options.context;
|
|
513
|
+
}
|
|
514
|
+
heartbeat(immediate) {
|
|
515
|
+
this.clearHeartbeat();
|
|
516
|
+
this._pingTimeoutId = setTimeout(async () => {
|
|
517
|
+
try {
|
|
518
|
+
if (!this._ws || this._ws.readyState !== WS_READY_STATE.OPEN) {
|
|
519
|
+
return;
|
|
520
|
+
}
|
|
521
|
+
this._lastPingSendTS = Date.now();
|
|
522
|
+
await this.ping();
|
|
523
|
+
this._pingFailed = 0;
|
|
524
|
+
this._pongTimeoutId = setTimeout(() => {
|
|
525
|
+
console.error('pong timed out');
|
|
526
|
+
if (this._pongMissed < DEFAULT_PONG_MISS_TOLERANCE) {
|
|
527
|
+
this._pongMissed++;
|
|
528
|
+
this.heartbeat(true);
|
|
529
|
+
}
|
|
530
|
+
else {
|
|
531
|
+
this.initWebSocketConnection(true);
|
|
532
|
+
}
|
|
533
|
+
}, this._context.appConfig.realtimePongWaitTimeout);
|
|
534
|
+
}
|
|
535
|
+
catch (e) {
|
|
536
|
+
if (this._pingFailed < DEFAULT_PING_FAIL_TOLERANCE) {
|
|
537
|
+
this._pingFailed++;
|
|
538
|
+
this.heartbeat();
|
|
539
|
+
}
|
|
540
|
+
else {
|
|
541
|
+
this.close(ws_event_1.CLOSE_EVENT_CODE.HeartbeatPingError);
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
}, immediate ? 0 : this._context.appConfig.realtimePingInterval);
|
|
545
|
+
}
|
|
546
|
+
clearHeartbeat() {
|
|
547
|
+
this._pingTimeoutId && clearTimeout(this._pingTimeoutId);
|
|
548
|
+
this._pongTimeoutId && clearTimeout(this._pongTimeoutId);
|
|
549
|
+
}
|
|
550
|
+
close(code) {
|
|
551
|
+
this.clearHeartbeat();
|
|
552
|
+
if (this._ws) {
|
|
553
|
+
this._ws.close(code, ws_event_1.CLOSE_EVENT_CODE_INFO[code].name);
|
|
554
|
+
this._ws = undefined;
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
watch(options) {
|
|
558
|
+
if (!this._ws && !this._wsInitPromise) {
|
|
559
|
+
this.initWebSocketConnection(false);
|
|
560
|
+
}
|
|
561
|
+
const virtualClient = new virtual_websocket_client_1.VirtualWebSocketClient(Object.assign(Object.assign({}, options), { send: this.send, login: this.webLogin, isWSConnected: this.isWSConnected, onceWSConnected: this.onceWSConnected, getWaitExpectedTimeoutLength: this.getWaitExpectedTimeoutLength, onWatchStart: this.onWatchStart, onWatchClose: this.onWatchClose, debug: true }));
|
|
562
|
+
this._virtualWSClient.add(virtualClient);
|
|
563
|
+
this._watchIdClientMap.set(virtualClient.watchId, virtualClient);
|
|
564
|
+
return virtualClient.listener;
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
exports.RealtimeWebSocketClient = RealtimeWebSocketClient;
|