From 83eef031152cc954ae4bb692bf6ddb73b5b170c8 Mon Sep 17 00:00:00 2001 From: Sai Kumar Battinoju <88789928+saikumarrs@users.noreply.github.com> Date: Fri, 22 Mar 2024 13:58:16 +0530 Subject: [PATCH] fix(analytics-js-plugins): add backward compatibility for storage api (#1669) fix: add backward compatibility for storage api --- .../src/utilities/retryQueue/RetryQueue.ts | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/packages/analytics-js-plugins/src/utilities/retryQueue/RetryQueue.ts b/packages/analytics-js-plugins/src/utilities/retryQueue/RetryQueue.ts index e9b227c4c..0bf2bf17c 100644 --- a/packages/analytics-js-plugins/src/utilities/retryQueue/RetryQueue.ts +++ b/packages/analytics-js-plugins/src/utilities/retryQueue/RetryQueue.ts @@ -5,7 +5,7 @@ import type { StorageType } from '@rudderstack/analytics-js-common/types/Storage import type { Nullable } from '@rudderstack/analytics-js-common/types/Nullable'; import type { ILogger } from '@rudderstack/analytics-js-common/types/Logger'; import type { BatchOpts, QueueOpts } from '@rudderstack/analytics-js-common/types/LoadOptions'; -import { isDefined, isNullOrUndefined } from '@rudderstack/analytics-js-common/utilities/checks'; +import { isDefined, isFunction, isNullOrUndefined } from '@rudderstack/analytics-js-common/utilities/checks'; import { LOCAL_STORAGE } from '@rudderstack/analytics-js-common/constants/storages'; import { generateUUID } from '@rudderstack/analytics-js-common/utilities/uuId'; import type { @@ -705,7 +705,21 @@ class RetryQueue implements IQueue { }; const findOtherQueues = (name: string): IStore[] => { const res: IStore[] = []; - const storageKeys = this.store.getOriginalEngine().keys(); + const storageEngine = this.store.getOriginalEngine(); + let storageKeys = []; + // 'keys' API is not supported by all the core SDK versions + // Hence, we need this backward compatibility check + if (isFunction(storageEngine.keys)) { + storageKeys = storageEngine.keys(); + } else { + for (let i = 0; i < storageEngine.length; i++) { + const key = storageEngine.key(i); + if (key) { + storageKeys.push(key); + } + } + } + storageKeys.forEach((k: string) => { const keyParts: string[] = k ? k.split('.') : [];