UNPKG

honeypot-run

Version:

Patented threat analytics and fingerprinting library. Requires a honeypot.run account

90 lines (89 loc) 2.65 kB
class Honeypot { constructor() { this.honey = null; this.id = null; this.q = []; this.didInit = false; this.callbacks = {}; } async identify(id, props, type) { if (window.honeypot.identify) { window.honeypot.identify(id, props, type); } else { window.honeypot.id = { id, ...(props ? { properties: props } : {}), ...(type ? { type } : {}), }; } } on(eventName, fn) { var _a; if (window.honeypot.on) { window.honeypot.on(eventName, fn); } else { ((_a = window.honeypot.callbacks)[eventName] ?? (_a[eventName] = [])).push(fn); } } async flow(flowId, instanceId) { 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(); }); } async track(eventType, eventProperties = {}) { if (window.honeypot.track) { window.honeypot.track(eventType, eventProperties); } else { window.honeypot.q.push([eventType, eventProperties]); } } async init() { } load(url) { return new Promise((resolve) => { const script = document.createElement('script'); script.async = true; script.src = url; document.head.appendChild(script); script.onload = () => { this.didInit = true; resolve(window.honeypot); }; }); } async get() { return new Promise((resolve, reject) => { let t = Date.now(); const e = () => { if (window.honeypot && window.honeypot.honey) { resolve(window.honeypot.honey); } else if (Date.now() - t >= 5000) { reject(new Error("Honey not found")); } else { setTimeout(e, 100); } }; e(); }); } } const honeypot = new Honeypot(); export { honeypot }; export default honeypot;