Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/minimal-package.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default { name: '@mondaycom/apps-sdk', version: '3.2.1' };
export default { name: '@mondaycom/apps-sdk', version: '3.2.2-beta.1' };
26 changes: 23 additions & 3 deletions lib/secure-storage/secure-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,26 @@ const authenticate = async (connectionData: ConnectionData): Promise<ConnectionD
};

let connectionData: ConnectionData;
let authenticationPromise: Promise<ConnectionData> | null = null;

const authenticateWithMutex = async (currentConnectionData: ConnectionData): Promise<ConnectionData> => {
// If there's already an authentication in progress, wait for it
if (authenticationPromise) {
return authenticationPromise;
}

// Start a new authentication and cache the promise
authenticationPromise = authenticate(currentConnectionData);

try {
const result = await authenticationPromise;
connectionData = result; // Update the global connection data
return result;
} finally {
// Clear the promise when done (success or failure)
authenticationPromise = null;
}
};

export class SecureStorage implements ISecureStorageInstance {

Expand All @@ -170,14 +190,14 @@ export class SecureStorage implements ISecureStorageInstance {
}

async delete(key: string) {
connectionData = await authenticate(connectionData);
connectionData = await authenticateWithMutex(connectionData);
const fullPath = generateCrudPath(key, connectionData.id);
await secureStorageFetch<VaultBaseResponse>(fullPath, connectionData, { method: 'DELETE' });
return true;
}

async get<T>(key: string) {
connectionData = await authenticate(connectionData);
connectionData = await authenticateWithMutex(connectionData);
const fullPath = generateCrudPath(key, connectionData.id);
const result = await secureStorageFetch<VaultBaseResponse>(fullPath, connectionData, { method: 'GET' });
if (!isDefined(result) || !isDefined(result?.data)) {
Expand All @@ -192,7 +212,7 @@ export class SecureStorage implements ISecureStorageInstance {
}

async set<T extends JsonValue>(key: string, value: T) {
connectionData = await authenticate(connectionData);
connectionData = await authenticateWithMutex(connectionData);
const fullPath = generateCrudPath(key, connectionData.id);
const formalizedValue = isObject(value) ? value : { [MONDAY_CODE_RESERVED_PRIMITIVES_KEY]: value };
await secureStorageFetch<VaultBaseResponse>(fullPath, connectionData, {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mondaycom/apps-sdk",
"version": "3.2.1",
"version": "3.2.2-beta.1",
"description": "monday apps SDK for NodeJS",
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js",
Expand Down
Loading