cubedev-tracking
Advanced tools
Comparing version 1.0.7 to 1.1.0
127
index.js
@@ -10,6 +10,6 @@ "use strict"; | ||
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); | ||
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator")); | ||
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); | ||
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator")); | ||
@@ -65,3 +65,48 @@ | ||
var COOKIE_ID = "cubedev_anonymous"; | ||
var getCookieId = /*#__PURE__*/function () { | ||
var _ref = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee() { | ||
var r; | ||
return _regenerator["default"].wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
_context.prev = 0; | ||
_context.next = 3; | ||
return fetch('https://identity.cube.dev', { | ||
credentials: 'include' | ||
}); | ||
case 3: | ||
r = _context.sent; | ||
if (!(r.status >= 400)) { | ||
_context.next = 6; | ||
break; | ||
} | ||
throw new Error('failed request to identity service'); | ||
case 6: | ||
return _context.abrupt("return", r.text()); | ||
case 9: | ||
_context.prev = 9; | ||
_context.t0 = _context["catch"](0); | ||
console.error(_context.t0); | ||
return _context.abrupt("return", (0, _v["default"])()); | ||
case 13: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee, null, [[0, 9]]); | ||
})); | ||
return function getCookieId() { | ||
return _ref.apply(this, arguments); | ||
}; | ||
}(); | ||
var COOKIE_ID = 'cubedev_anonymous'; | ||
var topDomainValue = topDomain(window.location.hostname); | ||
@@ -72,15 +117,27 @@ var COOKIE_DOMAIN = topDomainValue ? '.' + topDomainValue : window.location.hostname; | ||
var track = /*#__PURE__*/function () { | ||
var _ref = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2(event) { | ||
var _ref2 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee3(event) { | ||
var flush, currentPromise; | ||
return _regenerator["default"].wrap(function _callee2$(_context2) { | ||
return _regenerator["default"].wrap(function _callee3$(_context3) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
switch (_context3.prev = _context3.next) { | ||
case 0: | ||
if (!(0, _componentCookie["default"])(COOKIE_ID)) { | ||
(0, _componentCookie["default"])(COOKIE_ID, (0, _v["default"])(), { | ||
domain: COOKIE_DOMAIN, | ||
maxage: MAX_AGE | ||
}); | ||
if ((0, _componentCookie["default"])(COOKIE_ID)) { | ||
_context3.next = 8; | ||
break; | ||
} | ||
_context3.t0 = _componentCookie["default"]; | ||
_context3.t1 = COOKIE_ID; | ||
_context3.next = 5; | ||
return getCookieId(); | ||
case 5: | ||
_context3.t2 = _context3.sent; | ||
_context3.t3 = { | ||
domain: COOKIE_DOMAIN, | ||
maxage: MAX_AGE | ||
}; | ||
(0, _context3.t0)(_context3.t1, _context3.t2, _context3.t3); | ||
case 8: | ||
trackEvents.push(_objectSpread(_objectSpread(_objectSpread(_objectSpread({}, baseProps), event), {}, { | ||
@@ -95,7 +152,7 @@ referrer: document.referrer | ||
flush = /*#__PURE__*/function () { | ||
var _ref2 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(toFlush, retries) { | ||
var _ref3 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2(toFlush, retries) { | ||
var sentAt, result; | ||
return _regenerator["default"].wrap(function _callee$(_context) { | ||
return _regenerator["default"].wrap(function _callee2$(_context2) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
@@ -108,7 +165,7 @@ if (!toFlush) { | ||
if (toFlush.length) { | ||
_context.next = 3; | ||
_context2.next = 3; | ||
break; | ||
} | ||
return _context.abrupt("return", null); | ||
return _context2.abrupt("return", null); | ||
@@ -120,5 +177,5 @@ case 3: | ||
_context.prev = 4; | ||
_context2.prev = 4; | ||
sentAt = new Date().toJSON(); | ||
_context.next = 8; | ||
_context2.next = 8; | ||
return fetch('https://track.cube.dev/track', { | ||
@@ -137,39 +194,39 @@ method: 'post', | ||
case 8: | ||
result = _context.sent; | ||
result = _context2.sent; | ||
if (!(result.status !== 200 && retries > 0)) { | ||
_context.next = 11; | ||
_context2.next = 11; | ||
break; | ||
} | ||
return _context.abrupt("return", flush(toFlush, retries - 1)); | ||
return _context2.abrupt("return", flush(toFlush, retries - 1)); | ||
case 11: | ||
_context.next = 17; | ||
_context2.next = 17; | ||
break; | ||
case 13: | ||
_context.prev = 13; | ||
_context.t0 = _context["catch"](4); | ||
_context2.prev = 13; | ||
_context2.t0 = _context2["catch"](4); | ||
if (!(retries > 0)) { | ||
_context.next = 17; | ||
_context2.next = 17; | ||
break; | ||
} | ||
return _context.abrupt("return", flush(toFlush, retries - 1)); | ||
return _context2.abrupt("return", flush(toFlush, retries - 1)); | ||
case 17: | ||
return _context.abrupt("return", null); | ||
return _context2.abrupt("return", null); | ||
case 18: | ||
case "end": | ||
return _context.stop(); | ||
return _context2.stop(); | ||
} | ||
} | ||
}, _callee, null, [[4, 13]]); | ||
}, _callee2, null, [[4, 13]]); | ||
})); | ||
return function flush(_x2, _x3) { | ||
return _ref2.apply(this, arguments); | ||
return _ref3.apply(this, arguments); | ||
}; | ||
@@ -186,14 +243,14 @@ }(); | ||
flushPromise = currentPromise; | ||
return _context2.abrupt("return", flushPromise); | ||
return _context3.abrupt("return", flushPromise); | ||
case 6: | ||
case 13: | ||
case "end": | ||
return _context2.stop(); | ||
return _context3.stop(); | ||
} | ||
} | ||
}, _callee2); | ||
}, _callee3); | ||
})); | ||
return function track(_x) { | ||
return _ref.apply(this, arguments); | ||
return _ref2.apply(this, arguments); | ||
}; | ||
@@ -200,0 +257,0 @@ }(); |
{ | ||
"name": "cubedev-tracking", | ||
"version": "1.0.7", | ||
"version": "1.1.0", | ||
"description": "Tracker of anonymized for cube.dev and related websites", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -8,3 +8,3 @@ import cookie from 'component-cookie'; | ||
const getLevels = hostname => { | ||
const getLevels = (hostname) => { | ||
const parts = hostname.split('.'); | ||
@@ -26,3 +26,3 @@ const last = parts[parts.length - 1]; | ||
const topDomain = hostname => { | ||
const topDomain = (hostname) => { | ||
const levels = getLevels(hostname); | ||
@@ -45,5 +45,22 @@ // Lookup the real top level one. | ||
const COOKIE_ID = "cubedev_anonymous"; | ||
const getCookieId = async () => { | ||
try { | ||
const r = await fetch('https://identity.cube.dev', { | ||
credentials: 'include', | ||
}); | ||
if (r.status >= 400) { | ||
throw new Error('failed request to identity service'); | ||
} | ||
return r.text(); | ||
} catch (ex) { | ||
console.error(ex); | ||
return uuidv4(); | ||
} | ||
}; | ||
const COOKIE_ID = 'cubedev_anonymous'; | ||
const topDomainValue = topDomain(window.location.hostname); | ||
const COOKIE_DOMAIN = topDomainValue ? '.' + topDomainValue : window.location.hostname; | ||
const COOKIE_DOMAIN = topDomainValue | ||
? '.' + topDomainValue | ||
: window.location.hostname; | ||
const MAX_AGE = 365 * 24 * 60 * 60 * 1000; // 1 year | ||
@@ -53,3 +70,6 @@ | ||
if (!cookie(COOKIE_ID)) { | ||
cookie(COOKIE_ID, uuidv4(), { domain: COOKIE_DOMAIN, maxage: MAX_AGE }); | ||
cookie(COOKIE_ID, await getCookieId(), { | ||
domain: COOKIE_DOMAIN, | ||
maxage: MAX_AGE, | ||
}); | ||
} | ||
@@ -63,3 +83,3 @@ trackEvents.push({ | ||
clientAnonymousId: cookie(COOKIE_ID), | ||
clientTimestamp: new Date().toJSON() | ||
clientTimestamp: new Date().toJSON(), | ||
}); | ||
@@ -81,3 +101,3 @@ const flush = async (toFlush, retries) => { | ||
method: 'post', | ||
body: JSON.stringify(toFlush.map(r => ({ ...r, sentAt }))), | ||
body: JSON.stringify(toFlush.map((r) => ({ ...r, sentAt }))), | ||
headers: { 'Content-Type': 'application/json' }, | ||
@@ -97,7 +117,9 @@ }); | ||
}; | ||
const currentPromise = (flushPromise || Promise.resolve()).then(() => flush()).then(() => { | ||
if (currentPromise === flushPromise) { | ||
flushPromise = null; | ||
} | ||
}); | ||
const currentPromise = (flushPromise || Promise.resolve()) | ||
.then(() => flush()) | ||
.then(() => { | ||
if (currentPromise === flushPromise) { | ||
flushPromise = null; | ||
} | ||
}); | ||
flushPromise = currentPromise; | ||
@@ -104,0 +126,0 @@ return flushPromise; |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
13535
6
355
5