fetch.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. Object.defineProperty(exports, '__esModule', { value: true });
  2. const core = require('@sentry/core');
  3. const utils$1 = require('@sentry/utils');
  4. const utils = require('./utils.js');
  5. /**
  6. * Creates a Transport that uses the Fetch API to send events to Sentry.
  7. */
  8. function makeFetchTransport(
  9. options,
  10. nativeFetch = utils.getNativeFetchImplementation(),
  11. ) {
  12. let pendingBodySize = 0;
  13. let pendingCount = 0;
  14. function makeRequest(request) {
  15. const requestSize = request.body.length;
  16. pendingBodySize += requestSize;
  17. pendingCount++;
  18. const requestOptions = {
  19. body: request.body,
  20. method: 'POST',
  21. referrerPolicy: 'origin',
  22. headers: options.headers,
  23. // Outgoing requests are usually cancelled when navigating to a different page, causing a "TypeError: Failed to
  24. // fetch" error and sending a "network_error" client-outcome - in Chrome, the request status shows "(cancelled)".
  25. // The `keepalive` flag keeps outgoing requests alive, even when switching pages. We want this since we're
  26. // frequently sending events right before the user is switching pages (eg. whenfinishing navigation transactions).
  27. // Gotchas:
  28. // - `keepalive` isn't supported by Firefox
  29. // - As per spec (https://fetch.spec.whatwg.org/#http-network-or-cache-fetch):
  30. // If the sum of contentLength and inflightKeepaliveBytes is greater than 64 kibibytes, then return a network error.
  31. // We will therefore only activate the flag when we're below that limit.
  32. // There is also a limit of requests that can be open at the same time, so we also limit this to 15
  33. // See https://github.com/getsentry/sentry-javascript/pull/7553 for details
  34. keepalive: pendingBodySize <= 60000 && pendingCount < 15,
  35. ...options.fetchOptions,
  36. };
  37. try {
  38. return nativeFetch(options.url, requestOptions).then(response => {
  39. pendingBodySize -= requestSize;
  40. pendingCount--;
  41. return {
  42. statusCode: response.status,
  43. headers: {
  44. 'x-sentry-rate-limits': response.headers.get('X-Sentry-Rate-Limits'),
  45. 'retry-after': response.headers.get('Retry-After'),
  46. },
  47. };
  48. });
  49. } catch (e) {
  50. utils.clearCachedFetchImplementation();
  51. pendingBodySize -= requestSize;
  52. pendingCount--;
  53. return utils$1.rejectedSyncPromise(e);
  54. }
  55. }
  56. return core.createTransport(options, makeRequest);
  57. }
  58. exports.makeFetchTransport = makeFetchTransport;
  59. //# sourceMappingURL=fetch.js.map