@honeypot-run/core
Version:
Patented threat analytics and fingerprinting library. Requires a honeypot.run account
288 lines (287 loc) • 10.9 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { createAmplitudePlugin } from './plugins/AmplitudePlugin';
class Honeypot {
constructor() {
this.config = null;
this.honey = null;
this.id = [];
this.q = [];
this.didInit = false;
this.didLoad = false;
this.callbacks = {};
this.geofenced = false;
this._geoCaptcha = false;
}
setup(config) {
this.config = config;
}
amplitudePlugin(pluginConfig) {
return createAmplitudePlugin(this.track.bind(this), this.get.bind(this), this.identify.bind(this), pluginConfig);
}
geofence() {
var _a;
if ((_a = window.honeypot) === null || _a === void 0 ? void 0 : _a.geofence) {
window.honeypot.geofence();
}
this.geofenced = true;
}
geoCaptcha() {
return __awaiter(this, void 0, void 0, function* () {
return new Promise((resolve, reject) => {
const startTime = Date.now();
const checkGeoCaptcha = () => {
var _a;
if ((_a = window.honeypot) === null || _a === void 0 ? void 0 : _a.geoCaptcha) {
resolve(window.honeypot.geoCaptcha());
}
else if (Date.now() - startTime >= 5000) {
reject(new Error("GeoCaptcha not available: Honeypot SDK did not initialize in time."));
}
else {
setTimeout(checkGeoCaptcha, 100);
}
};
checkGeoCaptcha();
});
});
}
ensureHoneypot() {
if (!window.honeypot) {
window.honeypot = { "q": [], "id": [] };
}
}
identify(id, props, type) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
this.ensureHoneypot();
if ((_a = window.honeypot) === null || _a === void 0 ? void 0 : _a.identify) {
window.honeypot.identify(id, props, type);
}
else {
let _id = Object.assign(Object.assign({ id }, (props ? { properties: props } : {})), (type ? { type } : {}));
if (!Array.isArray(window.honeypot.id)) {
window.honeypot.id = [];
}
window.honeypot.id.push(_id);
}
});
}
on(eventName, fn) {
var _a;
var _b;
this.ensureHoneypot();
if (window.honeypot.on) {
window.honeypot.on(eventName, fn);
}
else {
((_a = (_b = window.honeypot.callbacks)[eventName]) !== null && _a !== void 0 ? _a : (_b[eventName] = [])).push(fn);
}
}
flow(flowId, instanceId) {
return __awaiter(this, void 0, void 0, function* () {
this.ensureHoneypot();
return new Promise((resolve, reject) => {
let t = Date.now();
const e = () => {
if (window && window.honeypot && this.didInit) {
resolve(window.honeypot.flow(flowId, instanceId));
}
else if (Date.now() - t >= 5000) {
reject(new Error("Flow could not be created. Did you call the load() method first?"));
}
else {
setTimeout(e, 100);
}
};
e();
});
});
}
track(eventType, eventProperties = {}) {
var _a, _b;
return __awaiter(this, void 0, void 0, function* () {
this.ensureHoneypot();
try {
if ((_a = window.honeypot) === null || _a === void 0 ? void 0 : _a.track) {
return window.honeypot.track(eventType, eventProperties);
}
else {
let q = this.q || [];
q.push([eventType, eventProperties]);
this.q = q;
}
}
catch (error) {
if ((_b = this.config) === null || _b === void 0 ? void 0 : _b.debug) {
console.error("Error tracking event:", error);
}
}
});
}
init() {
return __awaiter(this, void 0, void 0, function* () {
});
}
maybeTime(process_name, end) {
var _a;
if (!((_a = this.config) === null || _a === void 0 ? void 0 : _a.debug)) {
return;
}
if (end) {
console.timeEnd(process_name);
return;
}
console.time(process_name);
}
load(url, identity, type) {
this.didLoad = true;
return new Promise((resolve) => {
var _a, _b, _c;
const script = document.createElement('script');
script.async = true;
const scriptUrl = new URL(url);
let currentURL = null, pageTitle = null;
try {
currentURL = window.location.href;
}
catch (error) { }
try {
pageTitle = document.title;
}
catch (error) { }
scriptUrl.searchParams.append('url', currentURL || 'URL not available');
scriptUrl.searchParams.append('title', pageTitle || 'Title not available');
try {
if (window.honeypot && Array.isArray(window.honeypot.q)) {
const eventNames = window.honeypot.q.map((event) => event[0]);
scriptUrl.searchParams.append('events', JSON.stringify(eventNames));
}
}
catch (error) {
if ((_a = this.config) === null || _a === void 0 ? void 0 : _a.debug) {
console.error("Error appending event names to the script URL:", error);
}
}
try {
const includeParams = [
"hpdebug",
"hpgeotest",
];
const params = new URLSearchParams(window.location.search);
includeParams.forEach((p) => {
if (params.has(p)) {
scriptUrl.searchParams.append(p, params.get(p) || '');
}
});
}
catch (error) {
}
if (identity) {
scriptUrl.searchParams.append('identity', identity);
if (type) {
scriptUrl.searchParams.append('type', type);
}
}
let georef = new URLSearchParams(window.location.search).get('georef');
if (georef) {
scriptUrl.searchParams.append('georef', georef);
}
this.maybeTime('load honeypot');
try {
const id = (_b = window === null || window === void 0 ? void 0 : window.honeypot) === null || _b === void 0 ? void 0 : _b.id;
if (id) {
let _id = null;
if (Array.isArray(id) && id.length > 0) {
_id = (_c = id[0]) === null || _c === void 0 ? void 0 : _c.id;
}
else {
_id = id === null || id === void 0 ? void 0 : id.id;
}
if (_id) {
scriptUrl.searchParams.append('identity', _id);
}
}
}
catch (error) {
}
script.src = scriptUrl.toString();
document.head.appendChild(script);
const config = this.config;
script.onload = () => {
this.didInit = true;
this.maybeTime('load honeypot', true);
try {
if (window.honeypot) {
window.honeypot.config = config;
}
}
catch (error) {
}
resolve(window.honeypot);
};
});
}
setSessionId(sessionId) {
}
setDeviceId(deviceId) {
}
get(force) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
if (!this.didLoad && ((_a = this.config) === null || _a === void 0 ? void 0 : _a.url)) {
try {
yield this.load(this.config.url);
if (window.honeypot) {
return window.honeypot.get();
}
}
catch (error) {
throw new Error(`Failed to load`);
}
}
else if (this.didLoad && !this.didInit) {
return new Promise((resolve, reject) => {
const startTime = Date.now();
const retryLoad = () => {
if (window.honeypot && window.honeypot.get) {
resolve(window.honeypot.get());
}
else if (Date.now() - startTime >= 2000) {
reject(new Error("Timeout: Honeypot SDK did not initialize in time."));
}
else {
setTimeout(retryLoad, 50);
}
};
retryLoad();
});
}
return new Promise((resolve, reject) => {
const e = () => {
var _a;
if (window.honeypot && window.honeypot.honey) {
resolve(window.honeypot.honey);
}
else if ((_a = window.honeypot) === null || _a === void 0 ? void 0 : _a.get) {
return window.honeypot.get();
}
else {
reject(new Error("Honey not found"));
}
};
e();
});
});
}
}
const honeypot = new Honeypot();
export { honeypot };
export default honeypot;