instrumentation.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. }
  37. function createRootSdkObject() {
  38. const fakeParent = { attribution: {}, instrumentation: createInstrumentation() };
  39. const root = new SdkObject(fakeParent);
  40. root.guid = "";
  41. return root;
  42. }
  43. function createInstrumentation() {
  44. const listeners = /* @__PURE__ */ new Map();
  45. return new Proxy({}, {
  46. get: (obj, prop) => {
  47. if (typeof prop !== "string")
  48. return obj[prop];
  49. if (prop === "addListener")
  50. return (listener, context) => listeners.set(listener, context);
  51. if (prop === "removeListener")
  52. return (listener) => listeners.delete(listener);
  53. if (!prop.startsWith("on"))
  54. return obj[prop];
  55. return async (sdkObject, ...params) => {
  56. for (const [listener, context] of listeners) {
  57. if (!context || sdkObject.attribution.context === context)
  58. await listener[prop]?.(sdkObject, ...params);
  59. }
  60. };
  61. }
  62. });
  63. }
  64. // Annotate the CommonJS export names for ESM import in node:
  65. 0 && (module.exports = {
  66. SdkObject,
  67. createInstrumentation,
  68. createRootSdkObject
  69. });