UNPKG

@binance/fingerprint

Version:

binance web fingerprint

95 lines (94 loc) 3.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getCacheDevice = exports.setCacheDevice = exports.getLocalStorage = exports.setLocalStorage = void 0; var _constant = require("../constant"); var _cookie = require("./cookie"); var _json = require("./json"); var _is = require("./is"); var memoryStorage = {}; var isSupportSession = function() { return typeof sessionStorage !== "undefined"; }; var isSupportLocal = function() { return typeof localStorage !== "undefined"; }; function isString(value) { return Object.prototype.toString.call(value) === "[object String]"; } var setLocalStorage = function(key, value) { var options = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : {}; if (!isSupportLocal()) return; if (!options.ttl) { return localStorage.setItem(key, value); } // set ttl return localStorage.setItem(key, JSON.stringify({ value: value, expire: new Date().getTime() + options.ttl })); }; exports.setLocalStorage = setLocalStorage; var getLocalStorage = function(key) { if (!isSupportLocal()) return; var originValue = localStorage.getItem(key); var data = (0, _json).safeJsonParse(originValue) || {}; // expire is null if (!data.expire) return originValue; // has an expiration time and does not expire if (data.expire && data.expire > new Date().getTime()) return data.value; return null; }; exports.getLocalStorage = getLocalStorage; var setCacheDevice = function(value) { if (!_is.isClient) return; var name = _constant.STORE_KEY; var storeVal = JSON.stringify(value); // cookie (0, _cookie).deleteCookie(name); // localStorage if (isSupportLocal()) { setLocalStorage(name, storeVal); } // sessionStorage if (isSupportSession()) { sessionStorage.setItem(name, storeVal); } // memoryStorage memoryStorage[_constant.STORE_KEY] = storeVal; }; exports.setCacheDevice = setCacheDevice; var getCacheDevice = function() { if (!_is.isClient) return {}; var name = _constant.STORE_KEY; var deviceStr = null; // from memoryStorage if (!deviceStr) { deviceStr = memoryStorage[_constant.STORE_KEY]; } // from localStorage if (!deviceStr && isSupportLocal()) { deviceStr = getLocalStorage(name) || null; } // from sessionStorage if (!deviceStr && isSupportSession()) { deviceStr = sessionStorage.getItem(name) || null; } var device = (0, _json).safeJsonParse(deviceStr) || {}; if (isString(device)) { console.log("device need decodeURIComponent"); // eslint-disable-next-line max-len // device maybe like "%7B%22e5ea997c6c44ee66e59b20f1946236c6%22%3A%7B%22date%22%3A1733398786698%2C%22value%22%3A%22%22%7D%7D" // The specific reason has not been found yet try { device = (0, _json).safeJsonParse(decodeURIComponent(device)); } catch (error) { console.log("device need decodeURIComponent error", error); } } // sync to other storage deviceStr && setCacheDevice(device); return device; }; exports.getCacheDevice = getCacheDevice;