zone-patch-fetch.js 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. 'use strict';
  2. /**
  3. * @license Angular v<unknown>
  4. * (c) 2010-2024 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. if (signal) {
  75. const nativeAddEventListener = signal[Zone.__symbol__('addEventListener')] ||
  76. signal.addEventListener;
  77. nativeAddEventListener.call(signal, 'abort', function () {
  78. ac.abort();
  79. }, { once: true });
  80. }
  81. return createFetchTask('fetch', { fetchArgs: args }, fetch, this, args, ac);
  82. };
  83. if (OriginalResponse?.prototype) {
  84. // https://fetch.spec.whatwg.org/#body-mixin
  85. ['arrayBuffer', 'blob', 'formData', 'json', 'text']
  86. // Safely check whether the method exists on the `Response` prototype before patching.
  87. .filter((method) => typeof OriginalResponse.prototype[method] === 'function')
  88. .forEach((method) => {
  89. api.patchMethod(OriginalResponse.prototype, method, (delegate) => (self, args) => createFetchTask(`Response.${method}`, undefined, delegate, self, args, undefined));
  90. });
  91. }
  92. });
  93. }
  94. patchFetch(Zone);