@schibsted/account-sdk-browser 5.0.1-beta.2 → 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.2",
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
@@ -151,6 +151,9 @@ const SESSION_CALL_BLOCKED_TTL = 1000 * 30; //set to 30s, the default period for
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
 
@@ -568,71 +571,121 @@ export class Identity extends EventEmitter {
568
571
  return sessionData;
569
572
  };
570
573
 
571
- const _checkRedirectionNeed = (sessionData={})=>{
574
+ const _checkRedirectionNeed = (sessionData= {}) => {
572
575
  const sessionDataKeys = Object.keys(sessionData);
573
576
 
574
577
  return sessionDataKeys.length === 1 &&
575
578
  sessionDataKeys[0] === 'redirectURL';
576
- }
579
+ };
577
580
 
578
581
  const _getSession = async () => {
579
- if (this._enableSessionCaching) {
580
- // Try to resolve from cache (it has a TTL)
581
- let cachedSession = this.sessionStorageCache.get(HAS_SESSION_CACHE_KEY);
582
- if (cachedSession) {
583
- return _postProcess(cachedSession);
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();
587
+
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;
584
596
  }
585
- }
597
+ };
586
598
 
587
- // Prevent concurrent calls to session-service
588
- if (this._isSessionCallBlocked()) {
589
- // Returning data directly, without _postProcess since the data returned is the local copy
590
- return this._session;
591
- }
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();
592
604
 
593
- let sessionData = null;
594
- try {
595
- this._blockSessionCall();
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
+ }
596
608
 
597
- sessionData = await this._sessionService.get('/v2/session', {tabId: this._tabId});
598
- } catch (err) {
599
- if (err && err.code === 400 && this._enableSessionCaching) {
600
- const expiresIn = 1000 * (err.expiresIn || 300);
601
- this.sessionStorageCache.set(HAS_SESSION_CACHE_KEY, { error: err }, expiresIn);
602
- }
603
- throw err;
604
- }
609
+ if (this._enableSessionCaching) {
610
+ const expiresIn = 1000 * (sessionData.expiresIn || 300);
611
+ this.sessionStorageCache.set(HAS_SESSION_CACHE_KEY, sessionData, expiresIn);
612
+ }
605
613
 
606
- if (sessionData){
607
- // For expiring session and Safari browser do full page redirect to get new session
608
- if(_checkRedirectionNeed(sessionData)){
609
- await this.callbackBeforeRedirect();
610
- this.window.location.href = 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;
676
+ this._unblockSessionCallByTab();
625
677
 
626
678
  return sessionData;
627
679
  },
628
680
  err => {
629
681
  this.emit('error', err);
630
682
  this._hasSessionInProgress = false;
683
+ this._unblockSessionCallByTab();
684
+
631
685
  throw new SDKError('HasSession failed', err);
632
686
  }
633
687
  );
634
688
 
635
- this._unblockSessionCallByTab();
636
689
  return this._hasSessionInProgress;
637
690
  }
638
691
 
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.2';
4
+ const version = '5.0.1-beta.3';
5
5
  export default version;