@jsenv/fetch 1.1.41 → 1.1.43

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jsenv/fetch",
3
- "version": "1.1.41",
3
+ "version": "1.1.43",
4
4
  "description": "Unified url fetcher (http, http, file, data)",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -37,7 +37,7 @@
37
37
  },
38
38
  "dependencies": {
39
39
  "@jsenv/urls": "2.2.1",
40
- "@jsenv/server": "15.1.3",
40
+ "@jsenv/server": "15.1.4",
41
41
  "node-fetch": "2.6.7"
42
42
  }
43
43
  }
@@ -10,8 +10,7 @@ export const fetchUsingXHR = async (
10
10
  ) => {
11
11
  const headersPromise = createPromiseAndHooks();
12
12
  const bodyPromise = createPromiseAndHooks();
13
-
14
- const xhr = new XMLHttpRequest();
13
+ let xhr = new XMLHttpRequest();
15
14
 
16
15
  const failure = (error) => {
17
16
  // if it was already resolved, we must reject the body promise
@@ -25,45 +24,40 @@ export const fetchUsingXHR = async (
25
24
  const cleanup = () => {
26
25
  xhr.ontimeout = null;
27
26
  xhr.onerror = null;
28
- xhr.onload = null;
29
27
  xhr.onreadystatechange = null;
28
+ xhr.onload = null;
29
+ xhr = null;
30
30
  };
31
31
 
32
+ signal.addEventListener("abort", () => {
33
+ if (xhr) {
34
+ xhr.abort();
35
+ const abortError = new Error("aborted");
36
+ abortError.name = "AbortError";
37
+ failure(abortError);
38
+ }
39
+ });
32
40
  xhr.ontimeout = () => {
33
41
  cleanup();
34
42
  failure(new Error(`xhr request timeout on ${url}.`));
35
43
  };
36
-
37
44
  xhr.onerror = (error) => {
38
45
  cleanup();
39
46
  // unfortunately with have no clue why it fails
40
47
  // might be cors for instance
41
48
  failure(createRequestError(error, { url }));
42
49
  };
43
-
44
- xhr.onload = () => {
45
- cleanup();
46
- bodyPromise.resolve();
47
- };
48
-
49
- signal.addEventListener("abort", () => {
50
- xhr.abort();
51
- const abortError = new Error("aborted");
52
- abortError.name = "AbortError";
53
- failure(abortError);
54
- });
55
-
56
50
  xhr.onreadystatechange = () => {
57
51
  // https://developer.mozilla.org/fr/docs/Web/API/XMLHttpRequest/readyState
58
- const { readyState } = xhr;
59
-
60
- if (readyState === 2) {
52
+ if (xhr.readyState === 2) {
61
53
  headersPromise.resolve();
62
- } else if (readyState === 4) {
63
- cleanup();
64
- bodyPromise.resolve();
65
54
  }
66
55
  };
56
+ xhr.onload = () => {
57
+ const body = "response" in xhr ? xhr.response : xhr.responseText;
58
+ bodyPromise.resolve(body);
59
+ cleanup();
60
+ };
67
61
 
68
62
  xhr.open(method, url, true);
69
63
  Object.keys(headers).forEach((key) => {
@@ -85,16 +79,11 @@ export const fetchUsingXHR = async (
85
79
  const responseHeaders = getHeadersFromXHR(xhr);
86
80
 
87
81
  const readBody = async () => {
88
- await bodyPromise;
89
-
90
- const { status } = xhr;
82
+ const { body } = await bodyPromise;
91
83
  // in Chrome on file:/// URLs, status is 0
92
- if (status === 0) {
84
+ if (responseStatus === 0) {
93
85
  responseStatus = 200;
94
86
  }
95
-
96
- const body = "response" in xhr ? xhr.response : xhr.responseText;
97
-
98
87
  return {
99
88
  responseBody: body,
100
89
  responseBodyType: detectBodyType(body),