From 8cb8b570adf97351efd000f6700a86bc2242d8b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ali=20R=C4=B1za=20Kat?= Date: Wed, 14 Aug 2024 12:54:42 +0300 Subject: [PATCH] Requested Changes --- lib/countly-storage.js | 41 ++++++++++++++++++++++------------------- lib/countly.js | 8 ++------ 2 files changed, 24 insertions(+), 25 deletions(-) diff --git a/lib/countly-storage.js b/lib/countly-storage.js index 5edfdf8..e28e084 100644 --- a/lib/countly-storage.js +++ b/lib/countly-storage.js @@ -1,34 +1,37 @@ const fs = require('fs'); const path = require('path'); var cc = require("./countly-common"); + var storagePath; var __data = {}; +var defaultPath = "../data/"; // Default path +var defaultBulkPath = "../bulk_data/"; // Default path -var setStoragePath = function (path) { - defaultPath = "../data/"; // Default path - storagePath = path || defaultPath; -} +var setStoragePath = function(userPath) { + storagePath = userPath || defaultPath; +}; -var setBulkDataPath = function (path) { - defaultPath = "../bulk_data/"; // Default path - storagePath = path || defaultPath; -} +var setBulkDataPath = function(userPath) { + storagePath = userPath || defaultBulkPath; +}; -var getStoragePath = function () { +var getStoragePath = function() { return storagePath; -} +}; -var clearStoragePath = function () { +var resetStorage = function() { storagePath = undefined; - return storagePath; -} + __data = {}; + asyncWriteLock = false; + asyncWriteQueue = []; +}; /** * Read value from file * @param {String} key - key for file * @returns {varies} value in file */ -var readFile = function (key) { +var readFile = function(key) { var dir = path.resolve(__dirname, `${getStoragePath()}__${key}.json`); // try reading data file @@ -59,7 +62,7 @@ var readFile = function (key) { /** * Force store data synchronously on unrecoverable errors to preserve it for next launch */ -var forceStore = function () { +var forceStore = function() { for (var i in __data) { var dir = path.resolve(__dirname, `${getStoragePath()}__${i}.json`); var ob = {}; @@ -83,7 +86,7 @@ var asyncWriteQueue = []; * @param {varies} value - value to store * @param {Function} callback - callback to call when done storing */ -var writeFile = function (key, value, callback) { +var writeFile = function(key, value, callback) { var ob = {}; ob[key] = value; var dir = path.resolve(__dirname, `${getStoragePath()}__${key}.json`); @@ -112,7 +115,7 @@ var writeFile = function (key, value, callback) { * @param {varies} value - value to store * @param {Function} callback - callback to call when done storing */ -var storeSet = function (key, value, callback) { +var storeSet = function(key, value, callback) { __data[key] = value; if (!asyncWriteLock) { asyncWriteLock = true; @@ -129,7 +132,7 @@ var storeSet = function (key, value, callback) { * @param {varies} def - default value to use if not set * @returns {varies} value for the key */ -var storeGet = function (key, def) { +var storeGet = function(key, def) { cc.log(cc.logLevelEnums.DEBUG, `storeGet, Fetching item from storage with key: [${key}].`); if (typeof __data[key] === "undefined") { var ob = readFile(key); @@ -163,6 +166,6 @@ module.exports = { getStoragePath, setStoragePath, setBulkDataPath, - clearStoragePath, + resetStorage, readFile, }; \ No newline at end of file diff --git a/lib/countly.js b/lib/countly.js index c475174..dffb611 100644 --- a/lib/countly.js +++ b/lib/countly.js @@ -220,7 +220,7 @@ Countly.Bulk = Bulk; cc.log(cc.logLevelEnums.DEBUG, `init, IP address: [${Countly.ip_address}].`); } cc.log(cc.logLevelEnums.DEBUG, `init, Force POST requests: [${Countly.force_post}].`); - cc.log(cc.logLevelEnums.DEBUG, `init, Storage path: [${Countly.storage_path}].`); + cc.log(cc.logLevelEnums.DEBUG, `init, Storage path: [${CountlyStorage.getStoragePath()}].`); cc.log(cc.logLevelEnums.DEBUG, `init, Require consent: [${Countly.require_consent}].`); if (Countly.remote_config) { cc.log(cc.logLevelEnums.DEBUG, `init, Automatic Remote Configuration is on.`); @@ -328,7 +328,6 @@ Countly.Bulk = Bulk; maxBreadcrumbCount = 100; maxStackTraceLinesPerThread = 30; maxStackTraceLineLength = 200; - __data = {}; deviceIdType = null; // cc DEBUG @@ -354,12 +353,9 @@ Countly.Bulk = Bulk; Countly.city = undefined; Countly.ip_address = undefined; Countly.force_post = undefined; - Countly.storage_path = CountlyStorage.clearStoragePath(); Countly.require_consent = undefined; Countly.http_options = undefined; - - asyncWriteLock = false; - asyncWriteQueue = []; + CountlyStorage.resetStorage(); }; /**