instrumentation.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. "use strict";
  2. var __defProp = Object.defineProperty;
  3. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  4. var __getOwnPropNames = Object.getOwnPropertyNames;
  5. var __hasOwnProp = Object.prototype.hasOwnProperty;
  6. var __export = (target, all) => {
  7. for (var name in all)
  8. __defProp(target, name, { get: all[name], enumerable: true });
  9. };
  10. var __copyProps = (to, from, except, desc) => {
  11. if (from && typeof from === "object" || typeof from === "function") {
  12. for (let key of __getOwnPropNames(from))
  13. if (!__hasOwnProp.call(to, key) && key !== except)
  14. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  15. }
  16. return to;
  17. };
  18. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  19. var instrumentation_exports = {};
  20. __export(instrumentation_exports, {
  21. SdkObject: () => SdkObject,
  22. createInstrumentation: () => createInstrumentation,
  23. createRootSdkObject: () => createRootSdkObject
  24. });
  25. module.exports = __toCommonJS(instrumentation_exports);
  26. var import_events = require("events");
  27. var import_crypto = require("./utils/crypto");
  28. class SdkObject extends import_events.EventEmitter {
  29. constructor(parent, guidPrefix, guid) {
  30. super();
  31. this.guid = guid || `${guidPrefix || ""}@${(0, import_crypto.createGuid)()}`;
  32. this.setMaxListeners(0);
  33. this.attribution = { ...parent.attribution };
  34. this.instrumentation = parent.instrumentation;
  35. }
  36. closeReason() {
  37. return this.attribution.page?._closeReason || this.attribution.context?._closeReason || this.attribution.browser?._closeReason;
  38. }
  39. }
  40. function createRootSdkObject() {
  41. const fakeParent = { attribution: {}, instrumentation: createInstrumentation() };
  42. const root = new SdkObject(fakeParent);
  43. root.guid = "";
  44. return root;
  45. }
  46. function createInstrumentation() {
  47. const listeners = /* @__PURE__ */ new Map();
  48. return new Proxy({}, {
  49. get: (obj, prop) => {
  50. if (typeof prop !== "string")
  51. return obj[prop];
  52. if (prop === "addListener")
  53. return (listener, context) => listeners.set(listener, context);
  54. if (prop === "removeListener")
  55. return (listener) => listeners.delete(listener);
  56. if (!prop.startsWith("on"))
  57. return obj[prop];
  58. return async (sdkObject, ...params) => {
  59. for (const [listener, context] of listeners) {
  60. if (!context || sdkObject.attribution.context === context)
  61. await listener[prop]?.(sdkObject, ...params);
  62. }
  63. };
  64. }
  65. });
  66. }
  67. // Annotate the CommonJS export names for ESM import in node:
  68. 0 && (module.exports = {
  69. SdkObject,
  70. createInstrumentation,
  71. createRootSdkObject
  72. });