@swrve/smarttv-sdk
Version:
Swrve marketing engagement platform SDK for SmartTV OTT devices
98 lines (97 loc) • 3.77 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.StorageManager = void 0;
const PAL_1 = __importDefault(require("../utils/PAL"));
const md5_1 = require("../utils/md5");
const SwrveConstants_1 = require("../utils/SwrveConstants");
const SwrveConstants = __importStar(require("../utils/SwrveConstants"));
class StorageManager {
static saveData(key, data) {
this.getStorage().setItem(this.getKey(key), data);
}
static getData(key) {
return this.getStorage().getItem(this.getKey(key));
}
static getExternalIdForSwrveId(swrveUserId) {
const store = this.getStorage();
const key = this.getKey("ext-");
for (let i = 0; i < store.length; i++) {
const rawKey = store.key(i);
if (!rawKey || !rawKey.startsWith(key)) {
continue;
}
const externalId = rawKey.substring(key.length);
const mappedSwrveId = store.getItem(rawKey);
if (mappedSwrveId === swrveUserId) {
return externalId;
}
}
return "";
}
static clearData(key) {
this.getStorage().removeItem(this.getKey(key));
}
static saveDataWithMD5Hash(key, data) {
const store = this.getStorage();
return md5_1.md5Async(key + data)
.then(md5 => {
store.setItem(this.getKey(key), data);
store.setItem(this.getHashKey(key), md5);
});
}
static getDataWithMD5Hash(key) {
const store = this.getStorage();
const data = store.getItem(this.getKey(key));
const hash = store.getItem(this.getHashKey(key));
return md5_1.md5Async(key + data)
.then(rehash => hash === rehash ? data : null);
}
static clearUserData(userId, externalId) {
StorageManager.clearData(userId);
StorageManager.clearData("verified-" + userId);
StorageManager.clearData(SwrveConstants.CAMPAIGNS + userId);
StorageManager.clearData(SwrveConstants.CAMPAIGN_STATE + userId);
StorageManager.clearData(SwrveConstants.REAL_TIME_USER_PROPERTIES + userId);
StorageManager.clearData("resources" + userId);
StorageManager.clearData("resources" + userId + ".hash");
if (externalId) {
StorageManager.clearData("ext-" + externalId);
}
}
static getKey(key) {
return "swrve." + key;
}
static getHashKey(key) {
return "swrve." + key + ".hash";
}
static getStorage() {
const store = PAL_1.default.getPlatform().synchronousStorage;
if (!store) {
throw new Error(SwrveConstants_1.NO_SYNCHRONOUS_STORAGE);
}
return store;
}
}
exports.StorageManager = StorageManager;