From 71be5f45d8d117e3ca2b983f79b4529cd7dc79df Mon Sep 17 00:00:00 2001
From: Dan Jones <danjon@noc.ac.uk>
Date: Fri, 14 Feb 2025 16:14:15 +0000
Subject: [PATCH] fix: wait for authentication for fast polling rates

---
 CHANGELOG.md         | 4 ++++
 dist/adapter.esm.js  | 9 +++++++++
 dist/adapter.js      | 9 +++++++++
 src/adapter/index.js | 9 +++++++++
 4 files changed, 31 insertions(+)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index f37fea3..3ca620e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
 
 ## [Unreleased]
 
+### Fixed
+
+- Wait for authentication to complete for fast polling rates
+
 ### Changed
 
 - Add hyphen to regex for schema version to enable issue branches
diff --git a/dist/adapter.esm.js b/dist/adapter.esm.js
index 8625521..22e9c0a 100644
--- a/dist/adapter.esm.js
+++ b/dist/adapter.esm.js
@@ -8,6 +8,7 @@ class Adapter {
     this.protocol = protocol;
     this.config = config;
     this.axios = axios;
+    this.authenticating = false;
   }
 
   /**
@@ -44,6 +45,10 @@ class Adapter {
    * @returns {object}
    */
   getAuthorizationHeader() {
+    if (this.authenticating) {
+      // return an pseudo response with an ignored status
+      return Promise.reject({ response: { status: 405 } });
+    }
     if (!this.tokenValid())
       return this.auth().then((response) => {
         return {
@@ -62,6 +67,7 @@ class Adapter {
    * @returns {object}
    */
   auth() {
+    this.authenticating = true;
     let adapterConfig = this.config;
     return this.axios
       .get(`${adapterConfig.api}/token`, {
@@ -72,9 +78,11 @@ class Adapter {
       })
       .then((response) => {
         this.credentials = response.data;
+        this.authenticating = false;
         return response;
       })
       .catch((error) => {
+        this.authenticating = false;
         return Promise.reject(error);
       });
   }
@@ -116,6 +124,7 @@ class Adapter {
               retry = true;
             }
             break;
+          // ignore 405 from auth in progress
           case 503: {
             retry = true;
           }
diff --git a/dist/adapter.js b/dist/adapter.js
index c1f0e28..868a47c 100644
--- a/dist/adapter.js
+++ b/dist/adapter.js
@@ -10,6 +10,7 @@ class Adapter {
     this.protocol = protocol;
     this.config = config;
     this.axios = axios;
+    this.authenticating = false;
   }
 
   /**
@@ -46,6 +47,10 @@ class Adapter {
    * @returns {object}
    */
   getAuthorizationHeader() {
+    if (this.authenticating) {
+      // return an pseudo response with an ignored status
+      return Promise.reject({ response: { status: 405 } });
+    }
     if (!this.tokenValid())
       return this.auth().then((response) => {
         return {
@@ -64,6 +69,7 @@ class Adapter {
    * @returns {object}
    */
   auth() {
+    this.authenticating = true;
     let adapterConfig = this.config;
     return this.axios
       .get(`${adapterConfig.api}/token`, {
@@ -74,9 +80,11 @@ class Adapter {
       })
       .then((response) => {
         this.credentials = response.data;
+        this.authenticating = false;
         return response;
       })
       .catch((error) => {
+        this.authenticating = false;
         return Promise.reject(error);
       });
   }
@@ -118,6 +126,7 @@ class Adapter {
               retry = true;
             }
             break;
+          // ignore 405 from auth in progress
           case 503: {
             retry = true;
           }
diff --git a/src/adapter/index.js b/src/adapter/index.js
index ef71cc3..fba5231 100644
--- a/src/adapter/index.js
+++ b/src/adapter/index.js
@@ -8,6 +8,7 @@ export class Adapter {
     this.protocol = protocol;
     this.config = config;
     this.axios = axios;
+    this.authenticating = false;
   }
 
   /**
@@ -44,6 +45,10 @@ export class Adapter {
    * @returns {object}
    */
   getAuthorizationHeader() {
+    if (this.authenticating) {
+      // return an pseudo response with an ignored status
+      return Promise.reject({ response: { status: 405 } });
+    }
     if (!this.tokenValid())
       return this.auth().then((response) => {
         return {
@@ -62,6 +67,7 @@ export class Adapter {
    * @returns {object}
    */
   auth() {
+    this.authenticating = true;
     let adapterConfig = this.config;
     return this.axios
       .get(`${adapterConfig.api}/token`, {
@@ -72,9 +78,11 @@ export class Adapter {
       })
       .then((response) => {
         this.credentials = response.data;
+        this.authenticating = false;
         return response;
       })
       .catch((error) => {
+        this.authenticating = false;
         return Promise.reject(error);
       });
   }
@@ -116,6 +124,7 @@ export class Adapter {
               retry = true;
             }
             break;
+          // ignore 405 from auth in progress
           case 503: {
             retry = true;
           }
-- 
GitLab