@schibsted/account-sdk-browser 5.0.1-beta → 5.0.1-beta.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@schibsted/account-sdk-browser",
3
- "version": "5.0.1-beta",
3
+ "version": "5.0.1-beta.3",
4
4
  "description": "Schibsted account SDK for browsers",
5
5
  "main": "index.js",
6
6
  "type": "module",
package/src/identity.js CHANGED
@@ -146,11 +146,14 @@ import version from './version.js';
146
146
 
147
147
  const HAS_SESSION_CACHE_KEY = 'hasSession-cache';
148
148
  const SESSION_CALL_BLOCKED_CACHE_KEY = 'sessionCallBlocked-cache';
149
- const SESSION_CALL_BLOCKED_TTL = 1000 * 30;
149
+ const SESSION_CALL_BLOCKED_TTL = 1000 * 30; //set to 30s, the default period for a request timeout
150
150
 
151
151
  const TAB_ID_KEY = 'tab-id-cache';
152
152
  const TAB_ID = Math.floor(Math.random() * 100000)
153
153
  const TAB_ID_TTL = 1000 * 60 * 60 * 24 * 30;
154
+ const MAX_SESSION_CALL_RETRIES = 10;
155
+ const MIN_SESSION_CALL_WAIT_TIME = 100;
156
+
154
157
 
155
158
  const globalWindow = () => window;
156
159
 
@@ -193,8 +196,8 @@ export class Identity extends EventEmitter {
193
196
  this._sessionInitiatedSent = false;
194
197
  this.window = window;
195
198
  this.clientId = clientId;
196
- this.sessionStorageCache = new Cache(this.window && this.window.sessionStorage);
197
- this.localStorageCache = new Cache(this.window && this.window.localStorage);
199
+ this.sessionStorageCache = new Cache(() => this.window && this.window.sessionStorage);
200
+ this.localStorageCache = new Cache(() =>this.window && this.window.localStorage);
198
201
  this.redirectUri = redirectUri;
199
202
  this.env = env;
200
203
  this.log = log;
@@ -554,11 +557,6 @@ export class Identity extends EventEmitter {
554
557
  * @return {Promise<HasSessionSuccessResponse|HasSessionFailureResponse>}
555
558
  */
556
559
  hasSession() {
557
- const isSessionCallBlocked = this._isSessionCallBlocked()
558
- if (isSessionCallBlocked) {
559
- return this._session;
560
- }
561
-
562
560
  if (this._hasSessionInProgress) {
563
561
  return this._hasSessionInProgress;
564
562
  }
@@ -573,65 +571,117 @@ export class Identity extends EventEmitter {
573
571
  return sessionData;
574
572
  };
575
573
 
576
- const _checkRedirectionNeed = (sessionData={})=>{
574
+ const _checkRedirectionNeed = (sessionData= {}) => {
577
575
  const sessionDataKeys = Object.keys(sessionData);
578
576
 
579
577
  return sessionDataKeys.length === 1 &&
580
578
  sessionDataKeys[0] === 'redirectURL';
581
- }
579
+ };
582
580
 
583
581
  const _getSession = async () => {
584
- if (this._enableSessionCaching) {
585
- // Try to resolve from cache (it has a TTL)
586
- let cachedSession = this.sessionStorageCache.get(HAS_SESSION_CACHE_KEY);
587
- if (cachedSession) {
588
- return _postProcess(cachedSession);
589
- }
590
- }
582
+ const callSessionEndpoint = async () => {
583
+ try {
584
+ // Blocking future calls to session-service. This lock is removed after the response is processed
585
+ // to account for redirection that can happen towards session-service too
586
+ this._blockSessionCall();
591
587
 
592
- let sessionData = null;
593
- try {
594
- sessionData = await this._sessionService.get('/v2/session', {tabId: this._tabId});
595
- } catch (err) {
596
- if (err && err.code === 400 && this._enableSessionCaching) {
597
- const expiresIn = 1000 * (err.expiresIn || 300);
598
- this.sessionStorageCache.set(HAS_SESSION_CACHE_KEY, { error: err }, expiresIn);
588
+ return await this._sessionService.get('/v2/session', {tabId: this._tabId});
589
+ } catch (err) {
590
+ if (err && err.code === 400 && this._enableSessionCaching) {
591
+ const expiresIn = 1000 * (err.expiresIn || 300);
592
+ this.sessionStorageCache.set(HAS_SESSION_CACHE_KEY, {error: err}, expiresIn);
593
+ }
594
+
595
+ throw err;
599
596
  }
600
- throw err;
601
- }
597
+ };
602
598
 
603
- if (sessionData){
604
- // for expiring session and safari browser do full page redirect to gain new session
605
- if(_checkRedirectionNeed(sessionData)){
606
- this._blockSessionCall();
599
+ const useSessionResponseIfValid = async (sessionData) => {
600
+ if (sessionData) {
601
+ // For expiring session and WebKit browsers do a full page redirect to get a new session
602
+ if (_checkRedirectionNeed(sessionData)) {
603
+ await this.callbackBeforeRedirect();
604
+
605
+ // Doing a return here, to avoid caching the redirect response
606
+ return this.window.location.href = this._sessionService.makeUrl(sessionData.redirectURL, {tabId: this._getTabId()});
607
+ }
607
608
 
608
- await this.callbackBeforeRedirect();
609
+ if (this._enableSessionCaching) {
610
+ const expiresIn = 1000 * (sessionData.expiresIn || 300);
611
+ this.sessionStorageCache.set(HAS_SESSION_CACHE_KEY, sessionData, expiresIn);
612
+ }
609
613
 
610
- return this._sessionService.makeUrl(sessionData.redirectURL, {tabId: this._getTabId()});
614
+ return _postProcess(sessionData)
611
615
  }
616
+ };
612
617
 
618
+ const checkIfSessionCallIsNeededAndSafe = async (blockedAction) => {
613
619
  if (this._enableSessionCaching) {
614
- const expiresIn = 1000 * (sessionData.expiresIn || 300);
615
- this.sessionStorageCache.set(HAS_SESSION_CACHE_KEY, sessionData, expiresIn);
620
+ // Try to resolve from cache (it has a TTL)
621
+ let cachedSession = this.sessionStorageCache.get(HAS_SESSION_CACHE_KEY);
622
+ if (cachedSession) {
623
+ return _postProcess(cachedSession);
624
+ }
616
625
  }
617
- }
618
626
 
619
- return _postProcess(sessionData);
627
+ if (this._isSessionCallBlocked()) {
628
+ if (this._session && this._session.userId) {
629
+ return _postProcess(this._session);
630
+ }
631
+
632
+ if (blockedAction) {
633
+ const blockedResult = await blockedAction();
634
+
635
+ return _postProcess(blockedResult);
636
+ }
637
+
638
+ return null;
639
+ }
640
+ const sessionData = await callSessionEndpoint();
641
+
642
+ return await useSessionResponseIfValid(sessionData);
643
+ };
644
+
645
+ return await checkIfSessionCallIsNeededAndSafe(async ()=> {
646
+ let retryCount = 0;
647
+
648
+ // Try to call session-service MAX_SESSION_CALL_RETRIES times, waiting up to 1 second each time
649
+ while (retryCount < MAX_SESSION_CALL_RETRIES) {
650
+ retryCount++;
651
+ const randomWaitingStep = Math.floor(Math.random() * 9); // ignoring waiting times that are too small to matter
652
+ const randomWaitTime = MIN_SESSION_CALL_WAIT_TIME + (randomWaitingStep * 100);
653
+ await new Promise( resolve => { setTimeout(() =>{
654
+ return resolve();
655
+ }, randomWaitTime)});
656
+
657
+ const result = await checkIfSessionCallIsNeededAndSafe(null);
658
+ if (result) {
659
+ return result;
660
+ }
661
+ }
662
+
663
+ // exceeded number of attempts, returning old session info
664
+ if (this._session && this._session.userId) {
665
+ return this._session;
666
+ }
667
+
668
+ throw new SDKError('HasSession exceeded maximum number of attempts');
669
+ });
620
670
  };
671
+
621
672
  this._hasSessionInProgress = _getSession()
622
673
  .then(
623
674
  sessionData => {
624
675
  this._hasSessionInProgress = false;
625
-
626
- if (isUrl(sessionData)) {
627
- return this.window.location.href = sessionData;
628
- }
676
+ this._unblockSessionCallByTab();
629
677
 
630
678
  return sessionData;
631
679
  },
632
680
  err => {
633
681
  this.emit('error', err);
634
682
  this._hasSessionInProgress = false;
683
+ this._unblockSessionCallByTab();
684
+
635
685
  throw new SDKError('HasSession failed', err);
636
686
  }
637
687
  );
package/src/version.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // Automatically generated in 'npm version' by scripts/genversion.js
2
2
 
3
3
  'use strict'
4
- const version = '5.0.1-beta';
4
+ const version = '5.0.1-beta.3';
5
5
  export default version;