@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/es5/global.js +3174 -3031
- package/es5/global.js.map +1 -1
- package/es5/global.min.js +1 -1
- package/es5/global.min.js.map +1 -1
- package/es5/identity.js +3155 -3012
- package/es5/identity.js.map +1 -1
- package/es5/identity.min.js +1 -1
- package/es5/identity.min.js.map +1 -1
- package/es5/index.js +3178 -3035
- package/es5/index.js.map +1 -1
- package/es5/index.min.js +1 -1
- package/es5/index.min.js.map +1 -1
- package/es5/monetization.js +1 -1
- package/es5/monetization.js.map +1 -1
- package/es5/monetization.min.js +1 -1
- package/es5/monetization.min.js.map +1 -1
- package/package.json +1 -1
- package/src/identity.js +87 -34
- package/src/version.js +1 -1
package/package.json
CHANGED
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
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
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
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
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
|
-
|
|
594
|
-
|
|
595
|
-
|
|
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
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
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
|
-
|
|
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
|
-
|
|
615
|
-
this.sessionStorageCache.
|
|
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
|
-
|
|
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