zone-patch-fetch.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. 'use strict';
  2. /**
  3. * @license Angular v<unknown>
  4. * (c) 2010-2025 Google LLC. https://angular.io/
  5. * License: MIT
  6. */
  7. /**
  8. * @fileoverview
  9. * @suppress {missingRequire}
  10. */
  11. function patchFetch(Zone) {
  12. Zone.__load_patch('fetch', (global, Zone, api) => {
  13. let fetch = global['fetch'];
  14. if (typeof fetch !== 'function') {
  15. return;
  16. }
  17. const originalFetch = global[api.symbol('fetch')];
  18. if (originalFetch) {
  19. // restore unpatched fetch first
  20. fetch = originalFetch;
  21. }
  22. const ZoneAwarePromise = global.Promise;
  23. const symbolThenPatched = api.symbol('thenPatched');
  24. const fetchTaskScheduling = api.symbol('fetchTaskScheduling');
  25. const OriginalResponse = global.Response;
  26. const placeholder = function () { };
  27. const createFetchTask = (source, data, originalImpl, self, args, ac) => new Promise((resolve, reject) => {
  28. const task = Zone.current.scheduleMacroTask(source, placeholder, data, () => {
  29. // The promise object returned by the original implementation passed into the
  30. // function. This might be a `fetch` promise, `Response.prototype.json` promise,
  31. // etc.
  32. let implPromise;
  33. let zone = Zone.current;
  34. try {
  35. zone[fetchTaskScheduling] = true;
  36. implPromise = originalImpl.apply(self, args);
  37. }
  38. catch (error) {
  39. reject(error);
  40. return;
  41. }
  42. finally {
  43. zone[fetchTaskScheduling] = false;
  44. }
  45. if (!(implPromise instanceof ZoneAwarePromise)) {
  46. let ctor = implPromise.constructor;
  47. if (!ctor[symbolThenPatched]) {
  48. api.patchThen(ctor);
  49. }
  50. }
  51. implPromise.then((resource) => {
  52. if (task.state !== 'notScheduled') {
  53. task.invoke();
  54. }
  55. resolve(resource);
  56. }, (error) => {
  57. if (task.state !== 'notScheduled') {
  58. task.invoke();
  59. }
  60. reject(error);
  61. });
  62. }, () => {
  63. ac?.abort();
  64. });
  65. });
  66. global['fetch'] = function () {
  67. const args = Array.prototype.slice.call(arguments);
  68. const options = args.length > 1 ? args[1] : {};
  69. const signal = options?.signal;
  70. const ac = new AbortController();
  71. const fetchSignal = ac.signal;
  72. options.signal = fetchSignal;
  73. args[1] = options;
  74. let onAbort;
  75. if (signal) {
  76. const nativeAddEventListener = signal[Zone.__symbol__('addEventListener')] ||
  77. signal.addEventListener;
  78. onAbort = () => ac.abort();
  79. nativeAddEventListener.call(signal, 'abort', onAbort, { once: true });
  80. }
  81. return createFetchTask('fetch', { fetchArgs: args }, fetch, this, args, ac).finally(() => {
  82. // We need to be good citizens and remove the `abort` listener once
  83. // the fetch is settled. The `abort` listener may not be called at all,
  84. // which means the event listener closure would retain a reference to
  85. // the `ac` object even if it goes out of scope. Since browser's garbage
  86. // collectors work differently, some may not be smart enough to collect a signal.
  87. signal?.removeEventListener('abort', onAbort);
  88. });
  89. };
  90. if (OriginalResponse?.prototype) {
  91. // https://fetch.spec.whatwg.org/#body-mixin
  92. ['arrayBuffer', 'blob', 'formData', 'json', 'text']
  93. // Safely check whether the method exists on the `Response` prototype before patching.
  94. .filter((method) => typeof OriginalResponse.prototype[method] === 'function')
  95. .forEach((method) => {
  96. api.patchMethod(OriginalResponse.prototype, method, (delegate) => (self, args) => createFetchTask(`Response.${method}`, undefined, delegate, self, args, undefined));
  97. });
  98. }
  99. });
  100. }
  101. patchFetch(Zone);