@devanachat/sdk 0.12.0 → 0.13.0
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/CHANGELOG.md +54 -12
- package/README.md +13 -5
- package/build/index.cjs.js +74 -29
- package/build/index.cjs.js.map +1 -1
- package/build/index.mjs +74 -29
- package/build/index.mjs.map +1 -1
- package/build/index.umd.js +1 -1
- package/build/index.umd.js.map +1 -1
- package/build/types/WebSocketChatClient.d.ts +29 -2
- package/build/types/protocol/schemes/commands/CreateMessage.d.ts +2 -0
- package/build/types/protocol/schemes/events/Bye.d.ts +1 -0
- package/package.json +1 -1
package/build/index.mjs
CHANGED
|
@@ -4187,6 +4187,12 @@ class ChatStateTracker {
|
|
|
4187
4187
|
;// ./src/WebSocketChatClient.ts
|
|
4188
4188
|
|
|
4189
4189
|
|
|
4190
|
+
const MAX_RETRY_AFTER_MS = 60000;
|
|
4191
|
+
// Going Away, Service Restart, Try Again Later
|
|
4192
|
+
const SERVER_CLOSE_CODES = [1001, 1012, 1013];
|
|
4193
|
+
function normalizeRetryAfter(retryAfterMs) {
|
|
4194
|
+
return typeof retryAfterMs === 'number' && Number.isFinite(retryAfterMs) && retryAfterMs > 0 ? Math.min(retryAfterMs, MAX_RETRY_AFTER_MS) : undefined;
|
|
4195
|
+
}
|
|
4190
4196
|
var WebSocketChatClientEvent = /*#__PURE__*/function (WebSocketChatClientEvent) {
|
|
4191
4197
|
WebSocketChatClientEvent["connect"] = "connect";
|
|
4192
4198
|
WebSocketChatClientEvent["disconnect"] = "disconnect";
|
|
@@ -4209,6 +4215,10 @@ class WebSocketChatClient extends AbstractChatClient {
|
|
|
4209
4215
|
reconnectEnabled = false;
|
|
4210
4216
|
reconnectTimeoutId;
|
|
4211
4217
|
reconnectAttempts = 0;
|
|
4218
|
+
serverClosedSinceStable = false;
|
|
4219
|
+
byeReceived = false;
|
|
4220
|
+
byeRetryAfterMs;
|
|
4221
|
+
stableConnectionTimeoutId;
|
|
4212
4222
|
constructor(options) {
|
|
4213
4223
|
super();
|
|
4214
4224
|
this.options = options;
|
|
@@ -4245,7 +4255,7 @@ class WebSocketChatClient extends AbstractChatClient {
|
|
|
4245
4255
|
disconnect() {
|
|
4246
4256
|
const wasActive = this.ws !== null || this.reconnectTimeoutId !== undefined;
|
|
4247
4257
|
this.reconnectEnabled = false;
|
|
4248
|
-
this.
|
|
4258
|
+
this.resetBackoff();
|
|
4249
4259
|
this.cancelScheduledReconnect();
|
|
4250
4260
|
this.releaseSocket(1000); // Normal closure
|
|
4251
4261
|
this.failPendingCommands(new Error('Client disconnected before the command was answered'));
|
|
@@ -4287,9 +4297,16 @@ class WebSocketChatClient extends AbstractChatClient {
|
|
|
4287
4297
|
// Not every implementation follows a failed handshake with a close
|
|
4288
4298
|
// event (e.g. Node.js), so an error alone means the connection is lost.
|
|
4289
4299
|
ws.onerror = () => ws === this.ws && this.handleConnectionLoss(true);
|
|
4300
|
+
ws.onopen = () => ws === this.ws && this.startConnectingTimeout(this.options.sessionTimeoutMs ?? 60000);
|
|
4290
4301
|
this.ws = ws;
|
|
4291
4302
|
this.authenticated = false;
|
|
4292
|
-
this.
|
|
4303
|
+
this.byeReceived = false;
|
|
4304
|
+
this.byeRetryAfterMs = undefined;
|
|
4305
|
+
this.startConnectingTimeout(this.options.connectingTimeoutMs ?? 10000);
|
|
4306
|
+
}
|
|
4307
|
+
startConnectingTimeout(timeoutMs) {
|
|
4308
|
+
clearTimeout(this.connectingTimeoutId);
|
|
4309
|
+
this.connectingTimeoutId = setTimeout(() => this.triggerConnectionTimeout(), timeoutMs);
|
|
4293
4310
|
}
|
|
4294
4311
|
sendEnvelope(envelope) {
|
|
4295
4312
|
if (this.isReady) {
|
|
@@ -4301,34 +4318,52 @@ class WebSocketChatClient extends AbstractChatClient {
|
|
|
4301
4318
|
onMessage(event) {
|
|
4302
4319
|
this.lastReceivedMessageAt = Date.now();
|
|
4303
4320
|
const envelope = JSON.parse(event.data);
|
|
4321
|
+
const ws = this.ws;
|
|
4304
4322
|
this.handleIncomingEnvelope(envelope);
|
|
4305
4323
|
this.emit(envelope.type, envelope.data);
|
|
4306
4324
|
this.emit(this.Event.message, envelope);
|
|
4307
4325
|
|
|
4326
|
+
// A listener has called disconnect() or connect(), which settled this socket already.
|
|
4327
|
+
if (ws !== this.ws) {
|
|
4328
|
+
return;
|
|
4329
|
+
}
|
|
4330
|
+
if (envelope.type === 'Bye') {
|
|
4331
|
+
this.onBye(envelope.data);
|
|
4332
|
+
return;
|
|
4333
|
+
}
|
|
4334
|
+
|
|
4308
4335
|
// Login successfully
|
|
4309
4336
|
if (!this.authenticated) {
|
|
4310
|
-
|
|
4311
|
-
this.
|
|
4312
|
-
|
|
4313
|
-
|
|
4314
|
-
|
|
4315
|
-
|
|
4316
|
-
|
|
4317
|
-
|
|
4318
|
-
|
|
4319
|
-
|
|
4320
|
-
|
|
4321
|
-
|
|
4322
|
-
|
|
4323
|
-
|
|
4324
|
-
|
|
4325
|
-
|
|
4326
|
-
|
|
4337
|
+
this.authenticated = true;
|
|
4338
|
+
this.stableConnectionTimeoutId = setTimeout(() => this.resetBackoff(), this.options.reconnect?.stableConnectionMs ?? 30000);
|
|
4339
|
+
this.startConnectionMonitor();
|
|
4340
|
+
this.settleConnect();
|
|
4341
|
+
this.emit(this.Event.connect);
|
|
4342
|
+
this.sendFromQueue();
|
|
4343
|
+
}
|
|
4344
|
+
}
|
|
4345
|
+
onBye(bye) {
|
|
4346
|
+
this.byeReceived = true;
|
|
4347
|
+
this.byeRetryAfterMs = normalizeRetryAfter(bye?.retryAfterMs);
|
|
4348
|
+
if (bye?.reason?.type === 'Service' || bye?.reason?.error?.code === 'ServiceUnavailableException') {
|
|
4349
|
+
// Not left to the close code: the server may close with 1000 and some platforms rewrite the code.
|
|
4350
|
+
this.handleConnectionLoss(true, true);
|
|
4351
|
+
return;
|
|
4352
|
+
}
|
|
4353
|
+
if (this.authenticated) {
|
|
4354
|
+
return;
|
|
4355
|
+
}
|
|
4356
|
+
this.settleConnect(bye);
|
|
4357
|
+
const error = bye?.reason?.error;
|
|
4358
|
+
if (error?.code === 'AuthenticationException') {
|
|
4359
|
+
// Invalid token - retrying would be rejected the same way.
|
|
4360
|
+
this.handleConnectionLoss(false);
|
|
4361
|
+
this.emit(this.Event.error, new Error(`Authentication rejected: ${error.message}`));
|
|
4327
4362
|
}
|
|
4328
4363
|
}
|
|
4329
4364
|
onClose(event) {
|
|
4330
4365
|
// Connection was closed because of error
|
|
4331
|
-
this.handleConnectionLoss(event.code !== 1000);
|
|
4366
|
+
this.handleConnectionLoss(event.code !== 1000, this.byeReceived || SERVER_CLOSE_CODES.includes(event.code));
|
|
4332
4367
|
}
|
|
4333
4368
|
|
|
4334
4369
|
/**
|
|
@@ -4336,7 +4371,7 @@ class WebSocketChatClient extends AbstractChatClient {
|
|
|
4336
4371
|
* may never come, or take minutes on a dead TCP connection), settle
|
|
4337
4372
|
* everything that depended on it and schedule a retry when requested.
|
|
4338
4373
|
*/
|
|
4339
|
-
handleConnectionLoss(reconnect) {
|
|
4374
|
+
handleConnectionLoss(reconnect, closedByServer = false) {
|
|
4340
4375
|
this.releaseSocket(reconnect ? 3000 : 1000);
|
|
4341
4376
|
|
|
4342
4377
|
// The server can no longer answer anything that was queued or in
|
|
@@ -4346,7 +4381,7 @@ class WebSocketChatClient extends AbstractChatClient {
|
|
|
4346
4381
|
if (reconnect) {
|
|
4347
4382
|
// Keep a pending connect() promise unsettled - the retry below is
|
|
4348
4383
|
// expected to authenticate and will resolve it.
|
|
4349
|
-
this.scheduleReconnect();
|
|
4384
|
+
this.scheduleReconnect(closedByServer);
|
|
4350
4385
|
} else {
|
|
4351
4386
|
this.reconnectEnabled = false;
|
|
4352
4387
|
this.settleConnect(new Error('Connection closed before authentication'));
|
|
@@ -4362,13 +4397,15 @@ class WebSocketChatClient extends AbstractChatClient {
|
|
|
4362
4397
|
this.stopConnectionMonitor();
|
|
4363
4398
|
clearTimeout(this.connectingTimeoutId);
|
|
4364
4399
|
this.connectingTimeoutId = undefined;
|
|
4400
|
+
clearTimeout(this.stableConnectionTimeoutId);
|
|
4401
|
+
this.stableConnectionTimeoutId = undefined;
|
|
4365
4402
|
this.authenticated = false;
|
|
4366
4403
|
const ws = this.ws;
|
|
4367
4404
|
this.ws = null;
|
|
4368
4405
|
if (!ws) {
|
|
4369
4406
|
return;
|
|
4370
4407
|
}
|
|
4371
|
-
ws.onmessage = ws.onclose = ws.onerror = null;
|
|
4408
|
+
ws.onmessage = ws.onclose = ws.onerror = ws.onopen = null;
|
|
4372
4409
|
if (ws.readyState === ws.CONNECTING || ws.readyState === ws.OPEN) {
|
|
4373
4410
|
try {
|
|
4374
4411
|
ws.close(closeCode);
|
|
@@ -4377,19 +4414,27 @@ class WebSocketChatClient extends AbstractChatClient {
|
|
|
4377
4414
|
}
|
|
4378
4415
|
}
|
|
4379
4416
|
}
|
|
4380
|
-
scheduleReconnect() {
|
|
4417
|
+
scheduleReconnect(closedByServer) {
|
|
4381
4418
|
this.cancelScheduledReconnect();
|
|
4382
|
-
|
|
4383
|
-
|
|
4419
|
+
|
|
4420
|
+
// Once the server has closed a connection, the attempts that fail while it is still down
|
|
4421
|
+
// keep the longer backoff.
|
|
4422
|
+
this.serverClosedSinceStable ||= closedByServer;
|
|
4423
|
+
const reconnectOptions = this.options.reconnect;
|
|
4424
|
+
const minDelay = Math.max(0, this.serverClosedSinceStable ? reconnectOptions?.serverCloseMinDelayMs ?? 5000 : reconnectOptions?.minDelayMs ?? 1000);
|
|
4425
|
+
const maxDelay = Math.max(minDelay, reconnectOptions?.maxDelayMs ?? 30000);
|
|
4384
4426
|
const delay = Math.min(maxDelay, minDelay * 2 ** Math.min(this.reconnectAttempts, 30));
|
|
4385
4427
|
this.reconnectAttempts++;
|
|
4386
4428
|
|
|
4387
|
-
//
|
|
4388
|
-
// in lockstep after a server restart.
|
|
4429
|
+
// Full jitter spreads the clients dropped together over the whole delay; the server jitters its own hint.
|
|
4389
4430
|
this.reconnectTimeoutId = setTimeout(() => {
|
|
4390
4431
|
this.reconnectTimeoutId = undefined;
|
|
4391
4432
|
this.connect().catch(() => undefined);
|
|
4392
|
-
},
|
|
4433
|
+
}, this.byeRetryAfterMs ?? Math.random() * delay);
|
|
4434
|
+
}
|
|
4435
|
+
resetBackoff() {
|
|
4436
|
+
this.reconnectAttempts = 0;
|
|
4437
|
+
this.serverClosedSinceStable = false;
|
|
4393
4438
|
}
|
|
4394
4439
|
cancelScheduledReconnect() {
|
|
4395
4440
|
clearTimeout(this.reconnectTimeoutId);
|