kuzzle-sdk
Advanced tools
Comparing version 7.7.1 to 7.7.2
@@ -1,1 +0,1 @@ | ||
/*! Kuzzle Javascript SDK version 7.7.1 */ | ||
/*! Kuzzle Javascript SDK version 7.7.2 */ |
{ | ||
"name": "kuzzle-sdk", | ||
"version": "7.7.1", | ||
"version": "7.7.2", | ||
"description": "Official Javascript SDK for Kuzzle", | ||
@@ -5,0 +5,0 @@ "author": "The Kuzzle Team <support@kuzzle.io>", |
@@ -52,3 +52,3 @@ 'use strict'; | ||
get expired () { | ||
return Date.now() > this.expiresAt; | ||
return Math.round(Date.now() / 1000) > this.expiresAt; | ||
} | ||
@@ -55,0 +55,0 @@ |
@@ -46,3 +46,3 @@ "use strict"; | ||
scrollId: this._result.scrollId | ||
}, this._options) | ||
}) | ||
.then(({ result }) => this._buildNextSearchResult(result)); | ||
@@ -49,0 +49,0 @@ } |
@@ -42,2 +42,7 @@ import { KuzzleEventEmitter } from './core/KuzzleEventEmitter'; | ||
deprecationHandler: Deprecation; | ||
/** | ||
* Authenticator function called after a reconnection if the SDK is no longer | ||
* authenticated. | ||
*/ | ||
authenticator: () => Promise<void>; | ||
auth: AuthController; | ||
@@ -202,2 +207,19 @@ bulk: any; | ||
/** | ||
* Try to re-authenticate the SDK if the current token is invalid. | ||
* | ||
* If the token is invalid, this method will return false and emit a | ||
* "reconnectionError" event when: | ||
* - the SDK cannot re-authenticate using the authenticator function | ||
* - the authenticator function is not set | ||
* | ||
* This method never returns a rejected promise. | ||
*/ | ||
private tryReAuthenticate; | ||
/** | ||
* Use the "authenticator" function to authenticate the SDK. | ||
* | ||
* @returns The authentication token | ||
*/ | ||
authenticate(): Promise<void>; | ||
/** | ||
* Adds a listener to a Kuzzle global event. When an event is fired, listeners are called in the order of their | ||
@@ -204,0 +226,0 @@ * insertion. |
@@ -28,2 +28,3 @@ "use strict"; | ||
'reconnected', | ||
'reconnectionError', | ||
'tokenExpired' | ||
@@ -49,2 +50,7 @@ ]; | ||
super(); | ||
/** | ||
* Authenticator function called after a reconnection if the SDK is no longer | ||
* authenticated. | ||
*/ | ||
this.authenticator = null; | ||
if (protocol === undefined || protocol === null) { | ||
@@ -312,6 +318,12 @@ throw new Error('"protocol" argument missing'); | ||
}); | ||
this.protocol.addListener('reconnect', () => { | ||
this.protocol.addListener('reconnect', async () => { | ||
if (this.autoQueue) { | ||
this.stopQueuing(); | ||
} | ||
// If an authenticator was set, check if the token is still valid and try | ||
// to re-authenticate if needed. Otherwise the SDK is in disconnected state. | ||
if (this.authenticator && !await this.tryReAuthenticate()) { | ||
this.disconnect(); | ||
return; | ||
} | ||
if (this.autoReplay) { | ||
@@ -326,2 +338,43 @@ this.playQueue(); | ||
/** | ||
* Try to re-authenticate the SDK if the current token is invalid. | ||
* | ||
* If the token is invalid, this method will return false and emit a | ||
* "reconnectionError" event when: | ||
* - the SDK cannot re-authenticate using the authenticator function | ||
* - the authenticator function is not set | ||
* | ||
* This method never returns a rejected promise. | ||
*/ | ||
async tryReAuthenticate() { | ||
try { | ||
const { valid } = await this.auth.checkToken(); | ||
if (valid) { | ||
return true; | ||
} | ||
await this.authenticate(); | ||
return true; | ||
} | ||
catch (err) { | ||
this.emit('reconnectionError', { | ||
error: new Error(`Failed to authenticate the SDK after reconnection: ${err}`) | ||
}); | ||
return false; | ||
} | ||
} | ||
/** | ||
* Use the "authenticator" function to authenticate the SDK. | ||
* | ||
* @returns The authentication token | ||
*/ | ||
async authenticate() { | ||
if (typeof this.authenticator !== 'function') { | ||
throw new Error('The "authenticator" property must be a function.'); | ||
} | ||
await this.authenticator(); | ||
const { valid } = await this.auth.checkToken(); | ||
if (!valid) { | ||
throw new Error('The "authenticator" function failed to authenticate the SDK.'); | ||
} | ||
} | ||
/** | ||
* Adds a listener to a Kuzzle global event. When an event is fired, listeners are called in the order of their | ||
@@ -517,3 +570,4 @@ * insertion. | ||
.forEach(droppedRequest => { | ||
this.emit('offlineQueuePop', droppedRequest.query); | ||
this.emit('offlineQueuePop', droppedRequest.request); | ||
droppedRequest.reject(new Error('Query aborted: queued time exceeded the queueTTL option value')); | ||
}); | ||
@@ -526,3 +580,4 @@ } | ||
.forEach(droppedRequest => { | ||
this.emit('offlineQueuePop', droppedRequest.query); | ||
this.emit('offlineQueuePop', droppedRequest.request); | ||
droppedRequest.reject(new Error('Query aborted: too many queued requests (see the queueMaxSize option)')); | ||
}); | ||
@@ -540,3 +595,3 @@ } | ||
.catch(this.offlineQueue[0].reject); | ||
this.emit('offlineQueuePop', this.offlineQueue.shift()); | ||
this.emit('offlineQueuePop', this.offlineQueue.shift().request); | ||
setTimeout(() => { | ||
@@ -543,0 +598,0 @@ dequeuingProcess(); |
@@ -13,6 +13,5 @@ import { BaseProtocolRealtime } from './abstract/Realtime'; | ||
private ping; | ||
private pongTimeoutId; | ||
private waitForPong; | ||
private pingIntervalId; | ||
private _pingInterval; | ||
private _pongTimeout; | ||
private _httpProtocol; | ||
@@ -26,3 +25,3 @@ /** | ||
* - `reconnectionDelay` Number of milliseconds between reconnection attempts (default: `1000`) | ||
* - `pingInterval` Number of milliseconds between two pings (default: `10000`) | ||
* - `pingInterval` Number of milliseconds between two pings (default: `2000`) | ||
* - `ssl` Use SSL to connect to Kuzzle server. Default `false` unless port is 443 or 7443. | ||
@@ -29,0 +28,0 @@ */ |
@@ -40,3 +40,3 @@ 'use strict'; | ||
* - `reconnectionDelay` Number of milliseconds between reconnection attempts (default: `1000`) | ||
* - `pingInterval` Number of milliseconds between two pings (default: `10000`) | ||
* - `pingInterval` Number of milliseconds between two pings (default: `2000`) | ||
* - `ssl` Use SSL to connect to Kuzzle server. Default `false` unless port is 443 or 7443. | ||
@@ -68,3 +68,2 @@ */ | ||
this._pingInterval = typeof options.pingInterval === 'number' ? options.pingInterval : 2000; | ||
this._pongTimeout = this._pingInterval; | ||
this.client = null; | ||
@@ -99,3 +98,3 @@ this.lasturl = null; | ||
this.client.on('pong', () => { | ||
clearTimeout(this.pongTimeoutId); | ||
this.waitForPong = false; | ||
}); | ||
@@ -108,11 +107,25 @@ } | ||
*/ | ||
clearInterval(this.pingIntervalId); | ||
this.waitForPong = false; // Reset when connection is established | ||
this.pingIntervalId = setInterval(() => { | ||
if (this.client && this.client.readyState === 1) { | ||
// If the connection is established and we are not waiting for a pong we ping Kuzzle | ||
if (this.client | ||
&& this.client.readyState === this.client.OPEN | ||
&& !this.waitForPong) { | ||
this.ping(); | ||
this.waitForPong = true; | ||
return; | ||
} | ||
this.pongTimeoutId = setTimeout(() => { | ||
const error = new Error('Connection lost.'); | ||
// If we were waiting for a pong that never occured before the next ping cycle we throw an error | ||
if (this.waitForPong) { | ||
const error = new Error('Kuzzle does\'nt respond to ping. Connection lost.'); | ||
error.status = 503; | ||
/** | ||
* Ensure that the websocket connection is closed because if the connection was fine but Kuzzle could not respond in time | ||
* a new connection will be created if `autoReconnect=true` and there would 2 opened websocket connection. | ||
*/ | ||
this.client.close(); | ||
this.waitForPong = false; | ||
this.clientNetworkError(error); | ||
}, this._pongTimeout); | ||
} | ||
}, this._pingInterval); | ||
@@ -163,3 +176,3 @@ return resolve(); | ||
if (data && data.p && data.p === 2 && Object.keys(data).length === 1) { | ||
clearTimeout(this.pongTimeoutId); | ||
this.waitForPong = false; | ||
return; | ||
@@ -183,3 +196,3 @@ } | ||
*/ | ||
clearTimeout(this.pongTimeoutId); | ||
this.waitForPong = false; | ||
}; | ||
@@ -249,3 +262,3 @@ }); | ||
clearInterval(this.pingIntervalId); | ||
clearTimeout(this.pongTimeoutId); | ||
this.pingIntervalId = null; | ||
super.clientDisconnected(origin); | ||
@@ -260,3 +273,3 @@ } | ||
clearInterval(this.pingIntervalId); | ||
clearTimeout(this.pongTimeoutId); | ||
this.pingIntervalId = null; | ||
super.clientNetworkError(error); | ||
@@ -276,2 +289,4 @@ } | ||
this.stopRetryingToConnect = true; | ||
clearInterval(this.pingIntervalId); | ||
this.pingIntervalId = null; | ||
super.close(); | ||
@@ -278,0 +293,0 @@ } |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
438229
10367