xhr.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. Object.defineProperty(exports, '__esModule', { value: true });
  2. const core = require('@sentry/core');
  3. const utils = require('@sentry/utils');
  4. /**
  5. * The DONE ready state for XmlHttpRequest
  6. *
  7. * Defining it here as a constant b/c XMLHttpRequest.DONE is not always defined
  8. * (e.g. during testing, it is `undefined`)
  9. *
  10. * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/readyState}
  11. */
  12. const XHR_READYSTATE_DONE = 4;
  13. /**
  14. * Creates a Transport that uses the XMLHttpRequest API to send events to Sentry.
  15. */
  16. function makeXHRTransport(options) {
  17. function makeRequest(request) {
  18. return new utils.SyncPromise((resolve, reject) => {
  19. const xhr = new XMLHttpRequest();
  20. xhr.onerror = reject;
  21. xhr.onreadystatechange = () => {
  22. if (xhr.readyState === XHR_READYSTATE_DONE) {
  23. resolve({
  24. statusCode: xhr.status,
  25. headers: {
  26. 'x-sentry-rate-limits': xhr.getResponseHeader('X-Sentry-Rate-Limits'),
  27. 'retry-after': xhr.getResponseHeader('Retry-After'),
  28. },
  29. });
  30. }
  31. };
  32. xhr.open('POST', options.url);
  33. for (const header in options.headers) {
  34. if (Object.prototype.hasOwnProperty.call(options.headers, header)) {
  35. xhr.setRequestHeader(header, options.headers[header]);
  36. }
  37. }
  38. xhr.send(request.body);
  39. });
  40. }
  41. return core.createTransport(options, makeRequest);
  42. }
  43. exports.makeXHRTransport = makeXHRTransport;
  44. //# sourceMappingURL=xhr.js.map