1 | import {polyfillNeeded} from './utils.js';
|
2 | /**
|
3 | * Note: the "fetch.Request" default value is available for fetch imported from
|
4 | * the "node-fetch" package and not in browsers. This is OK since browsers
|
5 | * will be importing umd-polyfill.js from that path "self" is passed the
|
6 | * decorator so the default value will not be used (because browsers that define
|
7 | * fetch also has Request). One quirky setup where self.fetch exists but
|
8 | * self.Request does not is when the "unfetch" minimal fetch polyfill is used
|
9 | * on top of IE11; for this case the browser will try to use the fetch.Request
|
10 | * default value which in turn will be undefined but then then "if (Request)"
|
11 | * will ensure that you get a patched fetch but still no Request (as expected).
|
12 | * @param {fetch, Request = fetch.Request}
|
13 | * @returns {fetch: abortableFetch, Request: AbortableRequest}
|
14 | */
|
15 | export default function abortableFetchDecorator(patchTargets) {
|
16 | if ('function' === typeof patchTargets) {
|
17 | patchTargets = {fetch: patchTargets};
|
18 | }
|
19 | const {
|
20 | fetch,
|
21 | Request: NativeRequest = fetch.Request,
|
22 | AbortController: NativeAbortController,
|
23 | __FORCE_INSTALL_ABORTCONTROLLER_POLYFILL = false,
|
24 | } = patchTargets;
|
25 |
|
26 | if (!polyfillNeeded({fetch, Request: NativeRequest, AbortController: NativeAbortController, __FORCE_INSTALL_ABORTCONTROLLER_POLYFILL})) {
|
27 | return {fetch, Request};
|
28 | }
|
29 |
|
30 | let Request = NativeRequest;
|
31 | // Note that the "unfetch" minimal fetch polyfill defines fetch() without
|
32 | // defining window.Request, and this polyfill need to work on top of unfetch
|
33 | // hence we only patch it if it's available. Also we don't patch it if signal
|
34 | // is already available on the Request prototype because in this case support
|
35 | // is present and the patching below can cause a crash since it assigns to
|
36 | // request.signal which is technically a read-only property. This latter error
|
37 | // happens when you run the main5.js node-fetch example in the repo
|
38 | // "abortcontroller-polyfill-examples". The exact error is:
|
39 | // request.signal = init.signal;
|
40 | // ^
|
41 | // TypeError: Cannot set property signal of #<Request> which has only a getter
|
42 | if ((Request && !Request.prototype.hasOwnProperty('signal')) || __FORCE_INSTALL_ABORTCONTROLLER_POLYFILL) {
|
43 | Request = function Request(input, init) {
|
44 | let signal;
|
45 | if (init && init.signal) {
|
46 | signal = init.signal;
|
47 | // Never pass init.signal to the native Request implementation when the polyfill has
|
48 | // been installed because if we're running on top of a browser with a
|
49 | // working native AbortController (i.e. the polyfill was installed due to
|
50 | // __FORCE_INSTALL_ABORTCONTROLLER_POLYFILL being set), then passing our
|
51 | // fake AbortSignal to the native fetch will trigger:
|
52 | // TypeError: Failed to construct 'Request': member signal is not of type AbortSignal.
|
53 | delete init.signal;
|
54 | }
|
55 | const request = new NativeRequest(input, init);
|
56 | if (signal) {
|
57 | Object.defineProperty(request, 'signal', {
|
58 | writable: false,
|
59 | enumerable: false,
|
60 | configurable: true,
|
61 | value: signal
|
62 | });
|
63 | }
|
64 | return request;
|
65 | };
|
66 | Request.prototype = NativeRequest.prototype;
|
67 | }
|
68 |
|
69 | const realFetch = fetch;
|
70 | const abortableFetch = (input, init) => {
|
71 | const signal = (Request && Request.prototype.isPrototypeOf(input)) ? input.signal : init ? init.signal : undefined;
|
72 |
|
73 | if (signal) {
|
74 | let abortError;
|
75 | try {
|
76 | abortError = new DOMException('Aborted', 'AbortError');
|
77 | } catch (err) {
|
78 | // IE 11 does not support calling the DOMException constructor, use a
|
79 | // regular error object on it instead.
|
80 | abortError = new Error('Aborted');
|
81 | abortError.name = 'AbortError';
|
82 | }
|
83 |
|
84 | // Return early if already aborted, thus avoiding making an HTTP request
|
85 | if (signal.aborted) {
|
86 | return Promise.reject(abortError);
|
87 | }
|
88 |
|
89 | // Turn an event into a promise, reject it once `abort` is dispatched
|
90 | const cancellation = new Promise((_, reject) => {
|
91 | signal.addEventListener('abort', () => reject(abortError), {once: true});
|
92 | });
|
93 |
|
94 | if (init && init.signal) {
|
95 | // Never pass .signal to the native implementation when the polyfill has
|
96 | // been installed because if we're running on top of a browser with a
|
97 | // working native AbortController (i.e. the polyfill was installed due to
|
98 | // __FORCE_INSTALL_ABORTCONTROLLER_POLYFILL being set), then passing our
|
99 | // fake AbortSignal to the native fetch will trigger:
|
100 | // TypeError: Failed to execute 'fetch' on 'Window': member signal is not of type AbortSignal.
|
101 | delete init.signal;
|
102 | }
|
103 | // Return the fastest promise (don't need to wait for request to finish)
|
104 | return Promise.race([cancellation, realFetch(input, init)]);
|
105 | }
|
106 |
|
107 | return realFetch(input, init);
|
108 | };
|
109 |
|
110 | return {fetch: abortableFetch, Request};
|
111 | }
|