kuzzle-sdk
Advanced tools
Comparing version 7.7.2 to 7.7.3
@@ -1,1 +0,1 @@ | ||
/*! Kuzzle Javascript SDK version 7.7.2 */ | ||
/*! Kuzzle Javascript SDK version 7.7.3 */ |
{ | ||
"name": "kuzzle-sdk", | ||
"version": "7.7.2", | ||
"version": "7.7.3", | ||
"description": "Official Javascript SDK for Kuzzle", | ||
@@ -5,0 +5,0 @@ "author": "The Kuzzle Team <support@kuzzle.io>", |
@@ -324,3 +324,5 @@ "use strict"; | ||
if (response.result.jwt) { | ||
throw new Error('Kuzzle support for cookie authentication is disabled or not supported'); | ||
const err = new Error('Kuzzle support for cookie authentication is disabled or not supported'); | ||
this.kuzzle.emit('loginAttempt', { success: false, error: err.message }); | ||
throw err; | ||
} | ||
@@ -351,2 +353,3 @@ this.kuzzle.emit('loginAttempt', { success: true }); | ||
this._authenticationToken = null; | ||
this.kuzzle.emit('logoutAttempt', { success: true }); | ||
}); | ||
@@ -353,0 +356,0 @@ } |
@@ -62,2 +62,6 @@ "use strict"; | ||
this.kuzzle.on('reconnected', () => this.resubscribe()); | ||
this.kuzzle.on('reAuthenticated', () => { | ||
this.saveSubscriptions(); | ||
this.resubscribe(); | ||
}); | ||
} | ||
@@ -64,0 +68,0 @@ /** |
@@ -70,2 +70,4 @@ import { KuzzleEventEmitter } from './core/KuzzleEventEmitter'; | ||
private _cookieAuthentication; | ||
private _reconnectInProgress; | ||
private _loggedIn; | ||
private __proxy__; | ||
@@ -206,2 +208,3 @@ /** | ||
connect(): Promise<void>; | ||
_reconnect(): Promise<void>; | ||
/** | ||
@@ -272,3 +275,3 @@ * Try to re-authenticate the SDK if the current token is invalid. | ||
*/ | ||
tokenExpired(): void; | ||
tokenExpired(): Promise<void>; | ||
/** | ||
@@ -275,0 +278,0 @@ * Adds a new controller and make it available in the SDK. |
@@ -23,2 +23,3 @@ "use strict"; | ||
'loginAttempt', | ||
'logoutAttempt', | ||
'networkError', | ||
@@ -28,2 +29,3 @@ 'offlineQueuePush', | ||
'queryError', | ||
'reAuthenticated', | ||
'reconnected', | ||
@@ -159,2 +161,40 @@ 'reconnectionError', | ||
this._lastTokenExpired = null; | ||
this._reconnectInProgress = false; | ||
this._loggedIn = false; | ||
this.on('loginAttempt', async (status) => { | ||
if (status.success) { | ||
this._loggedIn = true; | ||
return; | ||
} | ||
/** | ||
* In case of login failure we need to be sure that the stored token is still valid | ||
*/ | ||
try { | ||
const response = await this.auth.checkToken(); | ||
this._loggedIn = response.valid; | ||
} | ||
catch { | ||
this._loggedIn = false; | ||
} | ||
}); | ||
/** | ||
* When successfuly logged out | ||
*/ | ||
this.on('logoutAttempt', status => { | ||
if (status.success) { | ||
this._loggedIn = false; | ||
} | ||
}); | ||
/** | ||
* On connection we need to verify if the token is still valid to know if we are still "logged in" | ||
*/ | ||
this.on('connected', async () => { | ||
try { | ||
const { valid } = await this.auth.checkToken(); | ||
this._loggedIn = valid; | ||
} | ||
catch { | ||
this._loggedIn = false; | ||
} | ||
}); | ||
return proxify_1.proxify(this, { | ||
@@ -319,20 +359,29 @@ seal: true, | ||
}); | ||
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) { | ||
this.playQueue(); | ||
} | ||
this.emit('reconnected'); | ||
}); | ||
this.protocol.addListener('reconnect', this._reconnect.bind(this)); | ||
this.protocol.addListener('discarded', data => this.emit('discarded', data)); | ||
this.protocol.addListener('websocketRenewalStart', () => { this._reconnectInProgress = true; }); | ||
this.protocol.addListener('websocketRenewalDone', () => { this._reconnectInProgress = false; }); | ||
return this.protocol.connect(); | ||
} | ||
async _reconnect() { | ||
if (this._reconnectInProgress) { | ||
return; | ||
} | ||
if (this.autoQueue) { | ||
this.stopQueuing(); | ||
} | ||
// If an authenticator was set, check if a user was logged in and if the token is still valid and try | ||
// to re-authenticate if needed. Otherwise the SDK is in disconnected state. | ||
if (this._loggedIn | ||
&& this.authenticator | ||
&& !await this.tryReAuthenticate()) { | ||
this._loggedIn = false; | ||
this.disconnect(); | ||
return; | ||
} | ||
if (this.autoReplay) { | ||
this.playQueue(); | ||
} | ||
this.emit('reconnected'); | ||
} | ||
/** | ||
@@ -349,2 +398,3 @@ * Try to re-authenticate the SDK if the current token is invalid. | ||
async tryReAuthenticate() { | ||
this._reconnectInProgress = true; | ||
try { | ||
@@ -364,2 +414,5 @@ const { valid } = await this.auth.checkToken(); | ||
} | ||
finally { | ||
this._reconnectInProgress = false; | ||
} | ||
} | ||
@@ -377,2 +430,3 @@ /** | ||
const { valid } = await this.auth.checkToken(); | ||
this._loggedIn = valid; | ||
if (!valid) { | ||
@@ -411,2 +465,3 @@ throw new Error('The "authenticator" function failed to authenticate the SDK.'); | ||
disconnect() { | ||
this._loggedIn = false; | ||
this.protocol.close(); | ||
@@ -517,3 +572,10 @@ } | ||
*/ | ||
tokenExpired() { | ||
async tokenExpired() { | ||
if (this._reconnectInProgress) { | ||
return; | ||
} | ||
if (this._loggedIn && this.authenticator && await this.tryReAuthenticate()) { | ||
this.emit('reAuthenticated'); | ||
return; | ||
} | ||
const now = Date.now(); | ||
@@ -520,0 +582,0 @@ if ((now - this._lastTokenExpired) < this.tokenExpiredInterval) { |
@@ -71,2 +71,6 @@ 'use strict'; | ||
clientNetworkError(error) { | ||
// Only emit disconnect once, if the connection was ready before | ||
if (this.isReady()) { | ||
this.emit('disconnect', { origin: DisconnectionOrigin.NETWORK_ERROR }); | ||
} | ||
this.state = 'offline'; | ||
@@ -92,5 +96,2 @@ this.clear(); | ||
} | ||
else { | ||
this.emit('disconnect', { origin: DisconnectionOrigin.NETWORK_ERROR }); | ||
} | ||
} | ||
@@ -97,0 +98,0 @@ isReady() { |
@@ -65,2 +65,3 @@ import { BaseProtocolRealtime } from './abstract/Realtime'; | ||
close(): void; | ||
private setupPingPong; | ||
} |
@@ -101,29 +101,3 @@ 'use strict'; | ||
this.clientConnected(); | ||
/** | ||
* Send pings to the server | ||
*/ | ||
clearInterval(this.pingIntervalId); | ||
this.waitForPong = false; // Reset when connection is established | ||
this.pingIntervalId = setInterval(() => { | ||
// 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; | ||
} | ||
// 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._pingInterval); | ||
this.setupPingPong(); | ||
return resolve(); | ||
@@ -177,3 +151,6 @@ }; | ||
// for responses, data.room == requestId | ||
if (data.room) { | ||
if (data.type === 'TokenExpired') { | ||
this.emit('tokenExpired'); | ||
} | ||
else if (data.room) { | ||
this.emit(data.room, data); | ||
@@ -238,3 +215,4 @@ } | ||
if (this.client) { | ||
this.client.close(); | ||
this.client.onclose = undefined; // Remove the listener that will emit disconnected / networkError event before closing | ||
this.client.close(1000); | ||
} | ||
@@ -252,3 +230,6 @@ this.client = null; | ||
}) | ||
.catch(error => this.emit(formattedRequest.payload.requestId, { error })); | ||
.catch(error => { | ||
this.emit(formattedRequest.payload.requestId, { error }); | ||
this.emit('websocketRenewalDone'); // Notify that the websocket has finished renewing his connection with Kuzzle | ||
}); | ||
} | ||
@@ -278,7 +259,8 @@ /** | ||
this.state = 'offline'; | ||
this.removeAllListeners(); | ||
this.wasConnected = false; | ||
if (this.client) { | ||
this.client.close(); | ||
this.client.close(1000); // Close with 1000 will trigger the `disconnect` | ||
} | ||
// Remove all listerner after closing the connection, this way the `disconnect` can be emitted when calling close. | ||
this.removeAllListeners(); | ||
this.client = null; | ||
@@ -290,4 +272,34 @@ this.stopRetryingToConnect = true; | ||
} | ||
setupPingPong() { | ||
clearInterval(this.pingIntervalId); | ||
// Reset when connection is established | ||
this.waitForPong = false; | ||
this.pingIntervalId = setInterval(() => { | ||
// 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; | ||
} | ||
// 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. | ||
*/ | ||
clearInterval(this.pingIntervalId); | ||
this.client.onclose = undefined; // Remove the listener that will emit disconnected / networkError event before closing | ||
this.client.close(1000); | ||
this.waitForPong = false; | ||
this.clientNetworkError(error); | ||
} | ||
}, this._pingInterval); | ||
} | ||
} | ||
exports.default = WebSocketProtocol; | ||
//# sourceMappingURL=WebSocket.js.map |
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
442818
10459