summaryrefslogtreecommitdiff
path: root/shared/logger/node_modules/@amp-metrics/sentrykit/dist/index.mjs
blob: 5755983b7d2478a1b4e2b221f5bb7664cd97f4b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
  for (var prop in b || (b = {}))
    if (__hasOwnProp.call(b, prop))
      __defNormalProp(a, prop, b[prop]);
  if (__getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(b)) {
      if (__propIsEnum.call(b, prop))
        __defNormalProp(a, prop, b[prop]);
    }
  return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));

// src/config.ts
import { SDK_VERSION as SDK_VERSION2 } from "@sentry/browser";

// src/transports/fetch.ts
import { rejectedSyncPromise } from "@sentry/utils";

// src/transports/base.ts
import {
  createEnvelope,
  envelopeItemTypeToDataCategory,
  forEachEnvelopeItem,
  isRateLimited,
  logger,
  makePromiseBuffer,
  resolvedSyncPromise,
  SentryError,
  serializeEnvelope,
  updateRateLimits
} from "@sentry/utils";
var DEFAULT_TRANSPORT_BUFFER_SIZE = 30;
function createTransport(options, makeRequest, buffer = makePromiseBuffer(
  options.bufferSize || DEFAULT_TRANSPORT_BUFFER_SIZE
)) {
  let rateLimits = {};
  const flush = (timeout) => buffer.drain(timeout);
  function send(envelope) {
    const filteredEnvelopeItems = [];
    forEachEnvelopeItem(envelope, (item, type) => {
      const envelopeItemDataCategory = envelopeItemTypeToDataCategory(type);
      if (isRateLimited(rateLimits, envelopeItemDataCategory)) {
        const event = getEventForEnvelopeItem(item, type);
        options.recordDroppedEvent("ratelimit_backoff", envelopeItemDataCategory, event);
      } else {
        filteredEnvelopeItems.push(item);
      }
    });
    if (filteredEnvelopeItems.length === 0) {
      return resolvedSyncPromise();
    }
    const filteredEnvelope = createEnvelope(envelope[0], filteredEnvelopeItems);
    const recordEnvelopeLoss = (reason) => {
      forEachEnvelopeItem(filteredEnvelope, (item, type) => {
        const event = getEventForEnvelopeItem(item, type);
        options.recordDroppedEvent(reason, envelopeItemTypeToDataCategory(type), event);
      });
    };
    const getRequest = options.getRequest || ((envelope2) => ({
      body: serializeEnvelope(envelope2, options.textEncoder)
    }));
    const request = getRequest(filteredEnvelope);
    if (!request) {
      return resolvedSyncPromise();
    }
    const requestTask = () => makeRequest(request).then(
      (response) => {
        if (response.statusCode !== void 0 && (response.statusCode < 200 || response.statusCode >= 300)) {
          ;
          (typeof __SENTRY_DEBUG__ === "undefined" || __SENTRY_DEBUG__) && logger.warn(`Sentry responded with status code ${response.statusCode} to sent event.`);
        }
        rateLimits = updateRateLimits(rateLimits, response);
        return response;
      },
      (error) => {
        recordEnvelopeLoss("network_error");
        throw error;
      }
    );
    return buffer.add(requestTask).then(
      (result) => result,
      (error) => {
        if (error instanceof SentryError) {
          ;
          (typeof __SENTRY_DEBUG__ === "undefined" || __SENTRY_DEBUG__) && logger.error("Skipped sending event because buffer is full.");
          recordEnvelopeLoss("queue_overflow");
          return resolvedSyncPromise();
        } else {
          throw error;
        }
      }
    );
  }
  send.__sentry__baseTransport__ = true;
  return {
    send,
    flush
  };
}
function getEventForEnvelopeItem(item, type) {
  if (type !== "event" && type !== "transaction") {
    return void 0;
  }
  return Array.isArray(item) ? item[1] : void 0;
}

// src/transports/utils.ts
import { isNativeFetch, logger as logger2 } from "@sentry/utils";
import { GLOBAL_OBJ } from "@sentry/utils";
var WINDOW = GLOBAL_OBJ;
var cachedFetchImpl = void 0;
function getNativeFetchImplementation() {
  if (cachedFetchImpl) {
    return cachedFetchImpl;
  }
  if (isNativeFetch(WINDOW.fetch)) {
    return cachedFetchImpl = WINDOW.fetch.bind(WINDOW);
  }
  const document = WINDOW.document;
  let fetchImpl = WINDOW.fetch;
  if (document && typeof document.createElement === "function") {
    try {
      const sandbox = document.createElement("iframe");
      sandbox.hidden = true;
      document.head.appendChild(sandbox);
      const contentWindow = sandbox.contentWindow;
      if (contentWindow && contentWindow.fetch) {
        fetchImpl = contentWindow.fetch;
      }
      document.head.removeChild(sandbox);
    } catch (e) {
      ;
      (typeof __SENTRY_DEBUG__ === "undefined" || __SENTRY_DEBUG__) && logger2.warn("Could not create sandbox iframe for pure fetch check, bailing to window.fetch: ", e);
    }
  }
  return cachedFetchImpl = fetchImpl.bind(WINDOW);
}
function clearCachedFetchImplementation() {
  cachedFetchImpl = void 0;
}

// src/transports/fetch.ts
function makeFetchTransport(options, nativeFetch = getNativeFetchImplementation()) {
  let pendingBodySize = 0;
  let pendingCount = 0;
  function makeRequest(request) {
    const requestSize = request.body.length;
    pendingBodySize += requestSize;
    pendingCount++;
    const requestOptions = __spreadValues({
      body: request.body,
      method: "POST",
      referrerPolicy: "origin",
      headers: request.headers,
      keepalive: pendingBodySize <= 6e4 && pendingCount < 15
    }, options.fetchOptions);
    try {
      return nativeFetch(request.url, requestOptions).then((response) => {
        pendingBodySize -= requestSize;
        pendingCount--;
        return {
          statusCode: response.status,
          headers: {
            "x-sentry-rate-limits": response.headers.get("X-Sentry-Rate-Limits"),
            "retry-after": response.headers.get("Retry-After")
          }
        };
      });
    } catch (e) {
      clearCachedFetchImplementation();
      pendingBodySize -= requestSize;
      pendingCount--;
      return rejectedSyncPromise(e);
    }
  }
  return createTransport(options, makeRequest);
}

// src/transports/xhr.ts
import { SyncPromise } from "@sentry/utils";
var XHR_READYSTATE_DONE = 4;
function makeXHRTransport(options) {
  function makeRequest(request) {
    return new SyncPromise((resolve, reject) => {
      const xhr = new XMLHttpRequest();
      xhr.onerror = reject;
      xhr.onreadystatechange = () => {
        if (xhr.readyState === XHR_READYSTATE_DONE) {
          resolve({
            statusCode: xhr.status,
            headers: {
              "x-sentry-rate-limits": xhr.getResponseHeader("X-Sentry-Rate-Limits"),
              "retry-after": xhr.getResponseHeader("Retry-After")
            }
          });
        }
      };
      xhr.open("POST", request.url);
      for (const header in request.headers) {
        if (Object.prototype.hasOwnProperty.call(request.headers, header)) {
          xhr.setRequestHeader(header, request.headers[header]);
        }
      }
      xhr.send(request.body);
    });
  }
  return createTransport(options, makeRequest);
}

// src/transport.ts
import { supportsFetch } from "@sentry/utils";

// src/ingestion-event.ts
function envelopeToIngestionEvents(envelope, options) {
  const [_envelopeHeader, items] = envelope;
  delete _envelopeHeader.dsn;
  const sharedFields = {
    _envelopeHeader,
    project: options.project,
    v: 4
  };
  return items.map((item) => {
    const itemType = item[0].type;
    if (itemType !== "event" && itemType !== "transaction") {
      return __spreadProps(__spreadValues({}, sharedFields), {
        _itemHeader: item[0],
        _debugLogs: [
          `Items of type "${itemType}" are not supported yet. Dropped the item.`
        ]
      });
    }
    const [_itemHeader, payload] = item;
    return __spreadValues(__spreadProps(__spreadValues({}, sharedFields), {
      _itemHeader
    }), payload);
  });
}

// src/utils.ts
import { getCurrentHub, SDK_VERSION } from "@sentry/browser";
function isHeadlessBrowser() {
  return navigator.userAgent === void 0 || navigator.appVersion === void 0 || navigator.plugins === void 0 || navigator.languages === void 0 || navigator.languages.length === 0 || navigator.language === "" || navigator.webdriver || navigator.plugins.length === 0 || /HeadlessChrome/.test(navigator.userAgent) || /headless/i.test(navigator.appVersion);
}
function versionMismatchErrorMessage(sdkVersion) {
  sdkVersion || (sdkVersion = "unknown");
  return `Version mismatch between the installed Sentry SDK version (${sdkVersion}) and supported SDK version (${SUPPORTED_SENTRY_VERSION}) by SentryKit. Make sure to use supported version of the Sentry SDK (${SUPPORTED_SENTRY_VERSION}).`;
}
function checkSentryKitIsCompatibleWith(sdkVersion) {
  if (sdkVersion !== SUPPORTED_SENTRY_VERSION) {
    console.error(
      `[SentryKit Versioning Error] ${versionMismatchErrorMessage(
        sdkVersion
      )} All sent data will be discarded.`
    );
    return false;
  }
  return true;
}
function checkSentrySDKCompatibility() {
  return checkSentryKitIsCompatibleWith(SDK_VERSION);
}
function checkEnvelopeSentrySDKCompatibility(envelope) {
  var _a;
  const sdkVersion = (_a = envelope[0].sdk) == null ? void 0 : _a.version;
  return checkSentryKitIsCompatibleWith(sdkVersion);
}
var correctSetupGuide = `The correct way to use SentryKit is as follows:

import {createSentryConfig} from '@amp-metrics/sentrykit'

Sentry.init(createSentryConfig({
  // all your configs
}))`;
function monitorSentryConfig(config) {
  ;
  config.__sentrykit_original_config = __spreadValues({}, config);
  return config;
}
function monitorSentryHubBindClient() {
  const hub = getCurrentHub();
  const originalBindClient = hub.bindClient;
  hub.bindClient = (client) => {
    assertCorrectSentryKitConfiguration(client.getOptions());
    originalBindClient.call(hub, client);
  };
}
function assertCorrectSentryKitConfiguration(config) {
  var _a, _b;
  const sdkVersion = (_b = (_a = config._metadata) == null ? void 0 : _a.sdk) == null ? void 0 : _b.version;
  if (sdkVersion !== SUPPORTED_SENTRY_VERSION) {
    throw new Error(
      `[SentryKit Initialization Error] ${versionMismatchErrorMessage(
        sdkVersion
      )}`
    );
  }
  const originalConfig = config.__sentrykit_original_config;
  if (!originalConfig) {
    throw new Error(
      `[SentryKit Initialization Error] Configuration has to be generated through \`createSentryConfig\` function. ${correctSetupGuide}`
    );
  }
  for (const key in originalConfig) {
    if (key === "integrations") {
      continue;
    }
    if (originalConfig[key] !== config[key]) {
      throw new Error(
        `[SentryKit Initialization Error] Configuration generated through \`createSentryConfig\` function has been changed. ${correctSetupGuide}`
      );
    }
  }
}

// src/logger.ts
var originalLog = console.log;
var enabled = false;
var enableLogger = () => {
  enabled = true;
};
var createLogger = (prefix) => (...args) => {
  ;
  (typeof __SENTRY_DEBUG__ === "undefined" || __SENTRY_DEBUG__) && enabled && originalLog(prefix, ...args);
};

// src/privacy/rules.ts
var debug = /* @__PURE__ */ createLogger("[SentryKit Privacy rules]");
function visit(obj, callback, parentContext) {
  const parentPath = (parentContext == null ? void 0 : parentContext.path) ? parentContext.path + "." : "";
  const _visit = (key) => {
    const path = parentPath + key;
    const value = obj[key];
    const action = callback({
      obj,
      key,
      value,
      path,
      parentContext
    });
    if (action === "remove") {
      return action;
    }
    if (value !== null && typeof value === "object") {
      visit(value, callback, {
        obj,
        key,
        value,
        path,
        parentContext
      });
    }
  };
  if (Array.isArray(obj)) {
    for (let i = 0; i < obj.length; i++) {
      const action = _visit(i.toString());
      if (action === "remove") {
        obj.splice(i, 1);
        i--;
      }
    }
  } else {
    for (const key in obj) {
      const action = _visit(key);
      if (action === "remove") {
        delete obj[key];
      }
    }
  }
}
var matchesPath = (rule, eventType, context) => {
  if ((rule.type === "url" || rule.type === "url-query") && typeof context.obj[context.key] !== "string") {
    return false;
  }
  if (rule.type === "timestamp" && typeof context.obj[context.key] !== "number") {
    return false;
  }
  if (!rule.matchPath) {
    if (rule.type === "url" || rule.type === "url-query") {
      return isURLField(context);
    }
    if (rule.type === "timestamp") {
      return isTimestampField(context);
    }
    return true;
  }
  return typeof rule.matchPath === "function" ? rule.matchPath({
    eventType,
    path: context.path,
    key: context.key,
    value: context.value
  }) : rule.matchPath.test(context.path);
};
var matchesQuery = (rule, eventType, context, queryName, queryValue) => {
  if (rule.type !== "url-query") {
    return false;
  }
  if (!rule.matchQueryName) {
    return true;
  }
  return typeof rule.matchQueryName === "function" ? rule.matchQueryName({
    eventType,
    path: context.path,
    key: context.key,
    url: context.value,
    queryName,
    queryValue
  }) : rule.matchQueryName.test(queryName);
};
var KnownURLFields = [
  (context) => /^spans\.\d+\.data\.url$/.test(context.path) && context.parentContext.obj.op === "http.client" && (Object.defineProperty(context.obj, "_url", {
    enumerable: false,
    value: context.value
  }) || true),
  (context) => /^spans\.\d+\.description$/.test(context.path) && context.obj.op === "http.client" && context.obj.data && context.obj.description === `${context.obj.data.method} ${context.obj.data._url || context.obj.data.url}`,
  (context) => /^breadcrumbs\.\d+\.data\.url$/.test(context.path) && (context.parentContext.obj.category === "fetch" || context.parentContext.obj.category === "xhr"),
  (context) => /^breadcrumbs\.\d+\.data\.from$/.test(context.path) && context.parentContext.obj.category === "navigation",
  (context) => /^breadcrumbs\.\d+\.data\.to$/.test(context.path) && context.parentContext.obj.category === "navigation",
  (context) => /^request\.url$/.test(context.path),
  (context) => /^request\.headers\.Referer$/.test(context.path)
];
var isURLField = (context) => KnownURLFields.some((test) => test(context));
var isTimestampField = (context) => {
  return typeof context.value === "number" && context.path.toLowerCase().endsWith("timestamp");
};
var processForPrivacy = (event, rules) => {
  const eventType = event._itemHeader.type === "transaction" ? "transaction" : "error";
  visit(event, (context) => {
    const { path, key, obj } = context;
    for (const [i, rule] of rules.entries()) {
      if (!matchesPath(rule, eventType, context)) {
        continue;
      }
      if ("action" in rule && rule.action === "keep") {
        continue;
      }
      if (rule.type === "timestamp") {
        const timestamp = obj[key];
        const timestampOrPrecision = typeof rule.precision === "function" ? rule.precision({
          eventType,
          path,
          key,
          timestamp
        }) : rule.precision;
        if (typeof timestampOrPrecision === "number") {
          ;
          (typeof __SENTRY_DEBUG__ === "undefined" || __SENTRY_DEBUG__) && timestamp !== timestampOrPrecision && debug(
            `Setting timestamp to a new value of "${timestampOrPrecision}" for path: "${path}"`
          );
          obj[key] = timestampOrPrecision;
        } else if (timestampOrPrecision === "seconds") {
          ;
          (typeof __SENTRY_DEBUG__ === "undefined" || __SENTRY_DEBUG__) && debug(
            `Reducing timestamp to "${timestampOrPrecision}" for path: "${path}"`
          );
          obj[key] = Math.round(timestamp);
        } else if (timestampOrPrecision === "minutes") {
          ;
          (typeof __SENTRY_DEBUG__ === "undefined" || __SENTRY_DEBUG__) && debug(
            `Reducing timestamp to "${timestampOrPrecision}" for path: "${path}"`
          );
          obj[key] = Math.round(timestamp / 60) * 60;
        }
      } else if (rule.type === "url-query") {
        const url = obj[key];
        const [base, oldQuery = ""] = url.split("?");
        const queryParams = new URLSearchParams(oldQuery);
        const entries = [...queryParams.entries()];
        entries.forEach(([queryName, queryValue]) => {
          if (matchesQuery(rule, eventType, context, queryName, queryValue)) {
            const hasActionAfterwards = rules.slice(i + 1).some(
              (r) => matchesPath(r, eventType, context) && matchesQuery(r, eventType, context, queryName, queryValue)
            );
            if (hasActionAfterwards) {
              ;
              (typeof __SENTRY_DEBUG__ === "undefined" || __SENTRY_DEBUG__) && debug(
                `Skipping the active rule as there is an overriding rule for the query "${queryName}" in the url "${url}" in path "${path}".`
              );
              return;
            }
            if (rule.action === "remove") {
              ;
              (typeof __SENTRY_DEBUG__ === "undefined" || __SENTRY_DEBUG__) && debug(
                `Removing query "${queryName}" from the url "${url}" in the path: "${path}"  (value was: "${queryValue}")`
              );
              queryParams.delete(queryName);
            } else if (rule.action === "replace") {
              const newValue = typeof rule.replace === "function" ? rule.replace({
                key,
                path,
                url,
                queryName,
                queryValue,
                eventType
              }) : rule.replace;
              (typeof __SENTRY_DEBUG__ === "undefined" || __SENTRY_DEBUG__) && newValue !== queryValue && debug(
                `Setting query "${queryName}" in the url "${url}" to a new value of "${newValue}" for path: "${path}" (value was: "${queryValue}")`
              );
              queryParams.set(queryName, newValue);
            }
          }
        });
        const query = queryParams.toString();
        obj[key] = base + (query ? `?${query}` : "");
      } else if (rule.type === "url") {
        const hasActionAfterwards = rules.slice(i + 1).some((r) => matchesPath(r, eventType, context) && r.type === "url");
        if (hasActionAfterwards) {
          ;
          (typeof __SENTRY_DEBUG__ === "undefined" || __SENTRY_DEBUG__) && debug(
            `Skipping the active rule as there is an overriding rule for the url "${obj[key]}" in path "${path}".`
          );
          continue;
        }
        if (rule.action === "remove") {
          ;
          (typeof __SENTRY_DEBUG__ === "undefined" || __SENTRY_DEBUG__) && debug(`Removing the url "${obj[key]}" in the path: "${path}"`);
          return "remove";
        } else if (rule.action === "replace") {
          const newValue = typeof rule.replace === "function" ? rule.replace({
            key,
            path,
            url: obj[key],
            eventType
          }) : rule.replace;
          (typeof __SENTRY_DEBUG__ === "undefined" || __SENTRY_DEBUG__) && newValue !== obj[key] && debug(
            `Setting the url"${obj[key]}" to a new value of "${newValue}" for path: "${path}" (value was: "${obj[key]}")`
          );
          obj[key] = newValue;
        }
      } else if (rule.type === "any" && "action" in rule) {
        const hasActionAfterwards = rules.slice(i + 1).some((r) => matchesPath(r, eventType, context) && r.type === "any");
        if (hasActionAfterwards) {
          ;
          (typeof __SENTRY_DEBUG__ === "undefined" || __SENTRY_DEBUG__) && debug(
            `Skipping the active rule as there is an overriding rule for the path "${path}".`
          );
          continue;
        }
        if (rule.action === "remove") {
          ;
          (typeof __SENTRY_DEBUG__ === "undefined" || __SENTRY_DEBUG__) && debug(`Removing the path "${path}" (value was: "${obj[key]}")`);
          return "remove";
        } else if (rule.action === "replace") {
          const newValue = typeof rule.replace === "function" ? rule.replace({
            key,
            path,
            value: obj[key],
            eventType
          }) : rule.replace;
          (typeof __SENTRY_DEBUG__ === "undefined" || __SENTRY_DEBUG__) && newValue !== obj[key] && debug(
            `Setting a new value of "${newValue}" for path: "${path}" (value was: "${obj[key]}")`
          );
          obj[key] = newValue;
        }
      }
    }
  });
  return event;
};

// src/transport.ts
function createTransportUrl(ingestUrl, topic) {
  const url = new URL(ingestUrl);
  url.pathname = "/report/2/" + topic;
  return url.toString();
}
function makeTransport(options) {
  options.getRequest = (envelope) => {
    if (!checkEnvelopeSentrySDKCompatibility(envelope)) {
      return false;
    }
    const events = envelopeToIngestionEvents(
      envelope,
      options.sentryKitConfig
    ).map(
      (event) => processForPrivacy(
        JSON.parse(JSON.stringify(event)),
        options.sentryKitConfig.privacyRules
      )
    );
    const topic = events[0]._itemHeader.type === "transaction" ? "traces" : "error";
    const url = createTransportUrl(
      options.sentryKitConfig.ingestUrl,
      options.sentryKitConfig.topic[topic]
    );
    return {
      url,
      body: JSON.stringify({ events }),
      headers: {
        "Content-type": "application/json"
      }
    };
  };
  return supportsFetch() ? makeFetchTransport(options) : makeXHRTransport(options);
}

// src/privacy/settings.ts
var debug2 = /* @__PURE__ */ createLogger("[SentryKit Privacy settings]");
function createPrivacyRulesFrom({
  allowQueryParams,
  allowExtra,
  allowTags,
  timestampPrecision
}) {
  const privacyRules = [];
  if (allowTags) {
    privacyRules.push({
      type: "any",
      matchPath: ({ path, key }) => {
        const shouldKeep = path === `tags.${key}` && allowTags.includes(key);
        (typeof __SENTRY_DEBUG__ === "undefined" || __SENTRY_DEBUG__) && shouldKeep && debug2(`Keeping the tag "${key}" for path: "${path}"`);
        return shouldKeep;
      },
      action: "keep"
    });
  }
  if (allowExtra) {
    privacyRules.push({
      type: "any",
      matchPath: ({ path, key }) => {
        const shouldKeep = path === `extra.${key}` && allowExtra.includes(key);
        (typeof __SENTRY_DEBUG__ === "undefined" || __SENTRY_DEBUG__) && shouldKeep && debug2(`Keeping the extra "${key}" for path: "${path}"`);
        return shouldKeep;
      },
      action: "keep"
    });
  }
  if (allowQueryParams) {
    privacyRules.push({
      type: "url-query",
      matchQueryName: ({ url, queryName, path }) => {
        const params = typeof allowQueryParams === "function" ? allowQueryParams(url) : allowQueryParams;
        const shouldKeep = params.includes(queryName);
        (typeof __SENTRY_DEBUG__ === "undefined" || __SENTRY_DEBUG__) && shouldKeep && debug2(`Keeping the query name "${queryName}" for path: "${path}"`);
        return shouldKeep;
      },
      action: "keep"
    });
  }
  if (timestampPrecision) {
    privacyRules.push({
      type: "timestamp",
      precision: ({ eventType, timestamp, path }) => {
        if (eventType === "error") {
          ;
          (typeof __SENTRY_DEBUG__ === "undefined" || __SENTRY_DEBUG__) && debug2(
            `Reducing timestamp to "${timestampPrecision}" for path: "${path}"`
          );
          return timestampPrecision;
        }
        return timestamp;
      }
    });
  }
  return privacyRules;
}

// src/config.ts
var debug3 = /* @__PURE__ */ createLogger("[SentryKit Config]");
var SUPPORTED_SENTRY_VERSION = "7.57.0";
var ENVIRONMENTS = {
  prod: "prod",
  qa: "qa"
};
var INGEST_URLS = {
  prod: "https://xp.apple.com",
  qa: "https://xp-qa.apple.com"
};
var defaultPrivacyRules = [
  {
    type: "any",
    matchPath: /tags\..+$/,
    action: "remove"
  },
  {
    type: "any",
    matchPath: /tags\.(http\.status_code|visibilitychange|effectiveConnectionType|connectionType|deviceMemory|hardwareConcurrency|lcp\..+)$/,
    action: "keep"
  },
  {
    type: "any",
    matchPath: /^extra\.[^.]+$/,
    action: "remove"
  },
  {
    type: "any",
    matchPath: /^extra\.arguments$/,
    action: "keep"
  },
  {
    type: "any",
    matchPath: /^user\.[^.]+$/,
    action: "remove"
  },
  {
    type: "url-query",
    action: "replace",
    replace: "REMOVED"
  }
];
function baseConfig(userOptions) {
  const privacyRules = typeof userOptions.privacyRules === "function" ? userOptions.privacyRules(defaultPrivacyRules) : userOptions.privacyRules === false ? [] : [...defaultPrivacyRules, ...userOptions.privacyRules || []];
  if (userOptions.privacySettings) {
    privacyRules.push(...createPrivacyRulesFrom(userOptions.privacySettings));
  }
  const config = __spreadProps(__spreadValues({
    transport: makeTransport,
    ingestUrl: "",
    topic: "xp_amp_web_error_log",
    environment: "qa",
    filterHeadless: true,
    maxBreadcrumbs: 0,
    release: void 0,
    redactKeys: void 0,
    sampleRate: void 0,
    tracesSampleRate: void 0,
    tracesSampler: void 0
  }, userOptions), {
    privacyRules,
    dsn: "https://dsn@bypass/1",
    autoSessionTracking: false,
    sendClientReports: false,
    replaysSessionSampleRate: void 0,
    replaysOnErrorSampleRate: void 0
  });
  if (typeof config.topic === "string") {
    config.topic = {
      error: config.topic,
      traces: ""
    };
  }
  const $config = config;
  $config.transportOptions = __spreadProps(__spreadValues({}, $config.transportOptions), {
    sentryKitConfig: $config
  });
  if (!$config.ingestUrl) {
    $config.ingestUrl = $config.environment === ENVIRONMENTS.prod ? INGEST_URLS.prod : INGEST_URLS.qa;
  }
  ;
  (typeof __SENTRY_DEBUG__ === "undefined" || __SENTRY_DEBUG__) && debug3(
    `Initialized with environment "${$config.environment}" and ingestUrl "${$config.ingestUrl}".`
  );
  (typeof __SENTRY_DEBUG__ === "undefined" || __SENTRY_DEBUG__) && debug3(
    $config.topic.traces ? `Tracing topic is set to "${$config.topic.traces}".` : `No tracing topic is set.`
  );
  return $config;
}
function beforeHooksOptions(config) {
  const IS_SUPPORTED_SDK = SDK_VERSION2 === SUPPORTED_SENTRY_VERSION;
  const shouldSkipEvent = !IS_SUPPORTED_SDK || config.environment === ENVIRONMENTS.prod && config.filterHeadless && isHeadlessBrowser();
  (typeof __SENTRY_DEBUG__ === "undefined" || __SENTRY_DEBUG__) && shouldSkipEvent && debug3("Events and transactions will not be sent to sentry.");
  return {
    beforeSend: (event, hint) => {
      if (shouldSkipEvent) {
        return null;
      }
      return config.beforeSend ? config.beforeSend(event, hint) : event;
    },
    beforeSendTransaction(event, hint) {
      if (shouldSkipEvent) {
        return null;
      }
      return config.beforeSendTransaction ? config.beforeSendTransaction(event, hint) : event;
    },
    beforeBreadcrumb(breadcrumb, hint) {
      if (breadcrumb.category === "console") {
        return null;
      }
      return config.beforeBreadcrumb ? config.beforeBreadcrumb(breadcrumb, hint) : breadcrumb;
    }
  };
}

// src/index.ts
checkSentrySDKCompatibility();
(typeof __SENTRY_DEBUG__ === "undefined" || __SENTRY_DEBUG__) && monitorSentryHubBindClient();
function createSentryConfig(userOptions) {
  ;
  (typeof __SENTRY_DEBUG__ === "undefined" || __SENTRY_DEBUG__) && (userOptions == null ? void 0 : userOptions.debug) && enableLogger();
  const config = baseConfig(userOptions || {});
  if (!config.project) {
    throw new Error(
      "[SentryKit Configuration Error]: The required `project` field is not set."
    );
  }
  const hasTracesConfig = config.tracesSampleRate || config.tracesSampler;
  const hasTracesTopic = config.topic.traces;
  if (hasTracesConfig && !hasTracesTopic) {
    throw new Error(
      "[SentryKit Configuration Error]: The `topic.traces` field is not set while trace sampling is configured."
    );
  }
  if (hasTracesTopic && !hasTracesConfig) {
    throw new Error(
      "[SentryKit Configuration Error]: Trace sampling is configured but `topic.traces` is not set."
    );
  }
  const originalConfig = __spreadValues(__spreadValues({}, config), beforeHooksOptions(config));
  (typeof __SENTRY_DEBUG__ === "undefined" || __SENTRY_DEBUG__) && monitorSentryConfig(originalConfig);
  return originalConfig;
}
var SentryKit = { createSentryConfig };
var src_default = SentryKit;
export {
  SentryKit,
  createSentryConfig,
  src_default as default
};