xhr.js 1.4 KB

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