Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: eslint fixes #578

Merged
merged 14 commits into from
Aug 26, 2022
2 changes: 1 addition & 1 deletion integrations/Drip/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Drip {
window._dcs.account = this.accountId;

(function () {
var dc = document.createElement('script');
const dc = document.createElement('script');
dc.type = 'text/javascript';
dc.setAttribute('data-loader', LOAD_ORIGIN);
dc.async = true;
Expand Down
4 changes: 2 additions & 2 deletions integrations/Heap/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ class Heap {
(r.type = 'text/javascript'),
(r.async = !0),
r.setAttribute('data-loader', LOAD_ORIGIN),
(r.src = 'https://cdn.heapanalytics.com/js/heap-' + e + '.js');
var a = document.getElementsByTagName('script')[0];
(r.src = `https://cdn.heapanalytics.com/js/heap-${e}.js`);
const a = document.getElementsByTagName('script')[0];
a.parentNode.insertBefore(r, a);
for (
let n = function (e) {
Expand Down
6 changes: 3 additions & 3 deletions integrations/PinterestTag/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ export default class PinterestTag {
window.pintrk = function () {
window.pintrk.queue.push(Array.prototype.slice.call(arguments));
};
var n = window.pintrk;
const n = window.pintrk;
(n.queue = []), (n.version = '3.0');
var t = document.createElement('script');
const t = document.createElement('script');
(t.async = !0), (t.src = e), t.setAttribute('data-loader', LOAD_ORIGIN);
var r = document.getElementsByTagName('script')[0];
const r = document.getElementsByTagName('script')[0];
r.parentNode.insertBefore(t, r);
}
})('https://s.pinimg.com/ct/core.js');
Expand Down
4 changes: 2 additions & 2 deletions integrations/Posthog/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ class Posthog {
((p = t.createElement('script')).type = 'text/javascript'),
(p.async = !0),
p.setAttribute('data-loader', LOAD_ORIGIN),
(p.src = s.api_host + '/static/array.js'),
(p.src = `${s.api_host}/static/array.js`),
(r = t.getElementsByTagName('script')[0]).parentNode.insertBefore(p, r);
var u = e;
let u = e;
for (
void 0 !== a ? (u = e[a] = []) : (a = 'posthog'),
u.people = u.people || [],
Expand Down
2 changes: 1 addition & 1 deletion integrations/ProfitWell/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class ProfitWell {
m = s.getElementsByTagName(g)[0];
a.async = 1;
a.setAttribute('data-loader', LOAD_ORIGIN);
a.src = r + '?auth=' + window.publicApiKey;
a.src = `${r}?auth=${window.publicApiKey}`;
m.parentNode.insertBefore(a, m);
})(window, document, 'profitwell', 'script', 'https://public.profitwell.com/js/profitwell.js');
}
Expand Down
2 changes: 1 addition & 1 deletion integrations/Rockerbox/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Rockerbox {
RB.initialize = function (s) {
RB.source = s;
};
var a = d.createElement('script');
const a = d.createElement('script');
a.type = 'text/javascript';
a.async = !0;
a.src = `https://${host}/assets/${library}.js`;
Expand Down
2 changes: 1 addition & 1 deletion integrations/SnapPixel/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class SnapPixel {
r.async = !0;
r.src = n;
r.setAttribute('data-loader', LOAD_ORIGIN);
var u = t.getElementsByTagName(s)[0];
const u = t.getElementsByTagName(s)[0];
u.parentNode.insertBefore(r, u);
})(window, document, 'https://sc-static.net/scevent.min.js');

Expand Down
11 changes: 5 additions & 6 deletions utils/beaconQueue.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/* eslint-disable no-param-reassign */
/* eslint-disable class-methods-use-this */
// import logger from "../logUtil";
import { Store } from './storage/store';
import { replacer } from './utils';
import { replacer, handleError } from './utils';

const defaults = {
queue: 'queue',
Expand Down Expand Up @@ -77,16 +76,16 @@ class BeaconQueue {
}

flushQueue(batch) {
batch.map((event) => {
batch.forEach((event) => {
event.sentAt = new Date().toISOString();
});
const data = { batch };
const payload = JSON.stringify(data, replacer);
const blob = new Blob([payload], { type: 'text/plain' });
const isPushed = navigator.sendBeacon(`${this.url}?writeKey=${this.writekey}`, blob);
// if (!isPushed) {
// logger.debug("Unable to send data");
// }
if (!isPushed) {
handleError(new Error("Unable to queue data to browser's beacon queue"));
}
this.setQueue([]);
this.clearTimer();
}
Expand Down
5 changes: 3 additions & 2 deletions utils/linker/crc32.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-bitwise */
/**
* @description This is utility function for crc32 algorithm
* @version v1.0.0
Expand All @@ -6,7 +7,7 @@
/**
* @description generate crc table
* @params none
* @returns arrray of CRC table
* @returns array of CRC table
*/

const makeCRCTable = function () {
Expand All @@ -25,7 +26,7 @@ const makeCRCTable = function () {
/**
*
* @param {string} str
* @returns {Bystream} crc32
* @returns {Bytestream} crc32
*/
const crc32 = function (str) {
const crcTable = makeCRCTable();
Expand Down
109 changes: 54 additions & 55 deletions utils/linker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,14 @@ const CHECKSUM_OFFSET_MAX_MIN = 1;
const VALID_VERSION = 1;
const DELIMITER = '*';

/**
* Return the key value pairs
* @param {string} value
* @return {?Object<string, string>}
*/
function parseLinker(value) {
const linkerObj = parseLinkerParamValue(value);
if (!linkerObj) {
return null;
}
const { checksum, serializedIds } = linkerObj;
if (!isCheckSumValid(serializedIds, checksum)) {
return null;
}
return deserialize(serializedIds);
}

/**
* Parse the linker param value to version checksum and serializedParams
* @param {string} value
* @return {?Object}
*/
function parseLinkerParamValue(value) {
const parts = value.split(DELIMITER);
const isEven = parts.length % 2 == 0;
const isEven = parts.length % 2 === 0;

if (parts.length < 4 || !isEven) {
// Format <version>*<checksum>*<key1>*<value1>
Expand All @@ -59,24 +42,6 @@ function parseLinkerParamValue(value) {
};
}

/**
* Check if the checksum is valid with time offset tolerance.
* @param {string} serializedIds
* @param {string} checksum
* @return {boolean}
*/
function isCheckSumValid(serializedIds, checksum) {
const userAgent = USER_INTERFACE.getUserAgent();
const language = USER_INTERFACE.getUserLanguage();
for (let i = 0; i <= CHECKSUM_OFFSET_MAX_MIN; i++) {
const calculateCheckSum = getCheckSum(serializedIds, i, userAgent, language);
if (calculateCheckSum == checksum) {
return true;
}
}
return false;
}

/**
* Deserialize the serializedIds and return keyValue pairs.
* @param {string} serializedIds
Expand All @@ -88,48 +53,82 @@ function deserialize(serializedIds) {
for (let i = 0; i < params.length; i += 2) {
const key = params[i];
const valid = KEY_VALIDATOR.test(key);
if (!valid) {
continue;
if (valid) {
const value = decode(params[i + 1]);
// const value = params[i + 1];
keyValuePairs[key] = value;
}
const value = decode(params[i + 1]);
// const value = params[i + 1];
keyValuePairs[key] = value;
}
return keyValuePairs;
}

/**
* Generates a semi-unique value for page visitor.
* @return {string}
*/
function getFingerprint(userAgent, language) {
const date = new Date();
const timezone = date.getTimezoneOffset();
return [userAgent, timezone, language].join(DELIMITER);
}

/**
* Rounded time used to check if t2 - t1 is within our time tolerance.
* @return {number}
*/
function getMinSinceEpoch() {
// Timestamp in minutes, floored.
return Math.floor(Date.now() / 60000);
}

/**
* Create a unique checksum hashing the fingerprint and a few other values.
* @param {string} serializedIds
* @param {number=} opt_offsetMin
* @param {number=} optOffsetMin
* @return {string}
*/
function getCheckSum(serializedIds, opt_offsetMin, userAgent, language) {
function getCheckSum(serializedIds, optOffsetMin, userAgent, language) {
const fingerprint = getFingerprint(userAgent, language);
const offset = opt_offsetMin || 0;
const offset = optOffsetMin || 0;
const timestamp = getMinSinceEpoch() - offset;
const crc = crc32([fingerprint, timestamp, serializedIds].join(DELIMITER));
// Encoded to base36 for less bytes.
return crc.toString(36);
}

/**
* Generates a semi-unique value for page visitor.
* @return {string}
* Check if the checksum is valid with time offset tolerance.
* @param {string} serializedIds
* @param {string} checksum
* @return {boolean}
*/
function getFingerprint(userAgent, language) {
const date = new Date();
const timezone = date.getTimezoneOffset();
return [userAgent, timezone, language].join(DELIMITER);
function isCheckSumValid(serializedIds, checksum) {
const userAgent = USER_INTERFACE.getUserAgent();
const language = USER_INTERFACE.getUserLanguage();
for (let i = 0; i <= CHECKSUM_OFFSET_MAX_MIN; i += 1) {
const calculateCheckSum = getCheckSum(serializedIds, i, userAgent, language);
if (calculateCheckSum === checksum) {
return true;
}
}
return false;
}

/**
* Rounded time used to check if t2 - t1 is within our time tolerance.
* @return {number}
* Return the key value pairs
* @param {string} value
* @return {?Object<string, string>}
*/
function getMinSinceEpoch() {
// Timestamp in minutes, floored.
return Math.floor(Date.now() / 60000);
function parseLinker(value) {
const linkerObj = parseLinkerParamValue(value);
if (!linkerObj) {
return null;
}
const { checksum, serializedIds } = linkerObj;
if (!isCheckSumValid(serializedIds, checksum)) {
return null;
}
return deserialize(serializedIds);
}

export default parseLinker;
28 changes: 17 additions & 11 deletions utils/logUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,50 @@ const LOG_LEVEL_INFO = 1;
const LOG_LEVEL_DEBUG = 2;
const LOG_LEVEL_WARN = 3;
const LOG_LEVEL_ERROR = 4;
let LOG_LEVEL = LOG_LEVEL_ERROR;
const DEF_LOG_LEVEL = LOG_LEVEL_ERROR;
let LOG_LEVEL = DEF_LOG_LEVEL;

const logger = {
setLogLevel(logLevel) {
switch (logLevel.toUpperCase()) {
case 'INFO':
LOG_LEVEL = LOG_LEVEL_INFO;
return;
break;
case 'DEBUG':
LOG_LEVEL = LOG_LEVEL_DEBUG;
return;
break;
case 'WARN':
LOG_LEVEL = LOG_LEVEL_WARN;
break;
default:
LOG_LEVEL = DEF_LOG_LEVEL;
break;
}
},

info() {
info(...args) {
if (LOG_LEVEL <= LOG_LEVEL_INFO) {
console.info(...arguments);
console.info(...args);
}
},

debug() {
debug(...args) {
if (LOG_LEVEL <= LOG_LEVEL_DEBUG) {
console.log(...arguments);
console.log(...args);
}
},

warn() {
warn(...args) {
if (LOG_LEVEL <= LOG_LEVEL_WARN) {
console.warn(...arguments);
console.warn(...args);
}
},

error() {
error(...args) {
if (LOG_LEVEL <= LOG_LEVEL_ERROR) {
console.error(...arguments);
console.error(...args);
}
},
};

export default logger;
7 changes: 4 additions & 3 deletions utils/storage/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import store from 'storejs';
*/
class StoreLocal {
constructor(options) {
this._options = {};
this.sOpts = {};
this.enabled = this.checkSupportAvailability();
this.options(options);
}
Expand All @@ -16,12 +16,13 @@ class StoreLocal {
* @param {*} options
*/
options(options = {}) {
if (arguments.length === 0) return this._options;
if (arguments.length === 0) return this.sOpts;

defaults(options, { enabled: true });

this.enabled = options.enabled && this.enabled;
this._options = options;
this.sOpts = options;
return this.sOpts;
}

/**
Expand Down
3 changes: 0 additions & 3 deletions utils/xhrModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,6 @@ class XHRQueue {
),
);
} else {
// logger.debug(
MoumitaM marked this conversation as resolved.
Show resolved Hide resolved
// `====== request processed successfully: ${xhr.status}`
// );
queueFn(null, xhr.status);
}
}
Expand Down