fetch.js 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. var __rest = (this && this.__rest) || function (s, e) {
  2. var t = {};
  3. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
  4. t[p] = s[p];
  5. if (s != null && typeof Object.getOwnPropertySymbols === "function")
  6. for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
  7. if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
  8. t[p[i]] = s[p[i]];
  9. }
  10. return t;
  11. };
  12. import { getBytes, getLines, getMessages } from './parse';
  13. export const EventStreamContentType = 'text/event-stream';
  14. const DefaultRetryInterval = 1000;
  15. const LastEventId = 'last-event-id';
  16. export function fetchEventSource(input, _a) {
  17. var { signal: inputSignal, headers: inputHeaders, onopen: inputOnOpen, onmessage, onclose, onerror, openWhenHidden, fetch: inputFetch } = _a, rest = __rest(_a, ["signal", "headers", "onopen", "onmessage", "onclose", "onerror", "openWhenHidden", "fetch"]);
  18. return new Promise((resolve, reject) => {
  19. const headers = Object.assign({}, inputHeaders);
  20. if (!headers.accept) {
  21. headers.accept = EventStreamContentType;
  22. }
  23. let curRequestController;
  24. function onVisibilityChange() {
  25. curRequestController.abort();
  26. if (!document.hidden) {
  27. create();
  28. }
  29. }
  30. if (!openWhenHidden) {
  31. document.addEventListener('visibilitychange', onVisibilityChange);
  32. }
  33. let retryInterval = DefaultRetryInterval;
  34. let retryTimer = 0;
  35. function dispose() {
  36. document.removeEventListener('visibilitychange', onVisibilityChange);
  37. window.clearTimeout(retryTimer);
  38. curRequestController.abort();
  39. }
  40. inputSignal === null || inputSignal === void 0 ? void 0 : inputSignal.addEventListener('abort', () => {
  41. dispose();
  42. resolve();
  43. });
  44. const fetch = inputFetch !== null && inputFetch !== void 0 ? inputFetch : window.fetch;
  45. const onopen = inputOnOpen !== null && inputOnOpen !== void 0 ? inputOnOpen : defaultOnOpen;
  46. async function create() {
  47. var _a;
  48. curRequestController = new AbortController();
  49. try {
  50. const response = await fetch(input, Object.assign(Object.assign({}, rest), { headers, signal: curRequestController.signal }));
  51. await onopen(response);
  52. await getBytes(response.body, getLines(getMessages(id => {
  53. if (id) {
  54. headers[LastEventId] = id;
  55. }
  56. else {
  57. delete headers[LastEventId];
  58. }
  59. }, retry => {
  60. retryInterval = retry;
  61. }, onmessage)));
  62. onclose === null || onclose === void 0 ? void 0 : onclose();
  63. dispose();
  64. resolve();
  65. }
  66. catch (err) {
  67. if (!curRequestController.signal.aborted) {
  68. try {
  69. const interval = (_a = onerror === null || onerror === void 0 ? void 0 : onerror(err)) !== null && _a !== void 0 ? _a : retryInterval;
  70. window.clearTimeout(retryTimer);
  71. retryTimer = window.setTimeout(create, interval);
  72. }
  73. catch (innerErr) {
  74. dispose();
  75. reject(innerErr);
  76. }
  77. }
  78. }
  79. }
  80. create();
  81. });
  82. }
  83. function defaultOnOpen(response) {
  84. const contentType = response.headers.get('content-type');
  85. if (!(contentType === null || contentType === void 0 ? void 0 : contentType.startsWith(EventStreamContentType))) {
  86. throw new Error(`Expected content-type to be ${EventStreamContentType}, Actual: ${contentType}`);
  87. }
  88. }
  89. //# sourceMappingURL=fetch.js.map