|
1 | 1 | import { HttpLibrary, RequestContext, ResponseContext, ZstdCompressorCallback } from "./http";
|
2 |
| -import fetch from "cross-fetch"; |
| 2 | +import { fetch as crossFetch } from "cross-fetch"; |
3 | 3 | import pako from "pako";
|
4 | 4 | import bufferFrom from "buffer-from";
|
5 |
| -import { isBrowser } from "../util"; |
| 5 | +import { isBrowser, isNode } from "../util"; |
6 | 6 |
|
7 | 7 | export class IsomorphicFetchHttpLibrary implements HttpLibrary {
|
8 | 8 | public debug = false;
|
@@ -47,27 +47,57 @@ export class IsomorphicFetchHttpLibrary implements HttpLibrary {
|
47 | 47 | }
|
48 | 48 | }
|
49 | 49 |
|
50 |
| - const resultPromise = fetch(request.getUrl(), { |
51 |
| - method: method, |
52 |
| - body: body as any, |
53 |
| - headers: headers, |
54 |
| - signal: request.getHttpConfig().signal, |
55 |
| - }).then((resp: any) => { |
56 |
| - const headers: { [name: string]: string } = {}; |
57 |
| - resp.headers.forEach((value: string, name: string) => { |
58 |
| - headers[name] = value; |
| 50 | + let resultPromise: Promise<ResponseContext>; |
| 51 | + |
| 52 | + // On non-node environments, use native fetch if available. |
| 53 | + // `cross-fetch` incorrectly assumes all browsers have XHR available. |
| 54 | + // See https://github.com/lquixada/cross-fetch/issues/78 |
| 55 | + // TODO: Remove once once above issue is resolved. |
| 56 | + if (!isNode && typeof fetch === "function") { |
| 57 | + resultPromise = fetch(request.getUrl(), { |
| 58 | + method: method, |
| 59 | + body: body as any, |
| 60 | + headers: headers, |
| 61 | + signal: request.getHttpConfig().signal, |
| 62 | + }).then((resp: any) => { |
| 63 | + const headers: { [name: string]: string } = {}; |
| 64 | + resp.headers.forEach((value: string, name: string) => { |
| 65 | + headers[name] = value; |
| 66 | + }); |
| 67 | + |
| 68 | + const body = { |
| 69 | + text: () => resp.text(), |
| 70 | + binary: () => resp.buffer(), |
| 71 | + }; |
| 72 | + const response = new ResponseContext(resp.status, headers, body); |
| 73 | + if (this.debug) { |
| 74 | + this.logResponse(response); |
| 75 | + } |
| 76 | + return response; |
59 | 77 | });
|
| 78 | + } else { |
| 79 | + resultPromise = crossFetch(request.getUrl(), { |
| 80 | + method: method, |
| 81 | + body: body as any, |
| 82 | + headers: headers, |
| 83 | + signal: request.getHttpConfig().signal, |
| 84 | + }).then((resp: any) => { |
| 85 | + const headers: { [name: string]: string } = {}; |
| 86 | + resp.headers.forEach((value: string, name: string) => { |
| 87 | + headers[name] = value; |
| 88 | + }); |
60 | 89 |
|
61 |
| - const body = { |
62 |
| - text: () => resp.text(), |
63 |
| - binary: () => resp.buffer(), |
64 |
| - }; |
65 |
| - const response = new ResponseContext(resp.status, headers, body); |
66 |
| - if (this.debug) { |
67 |
| - this.logResponse(response); |
68 |
| - } |
69 |
| - return response; |
70 |
| - }); |
| 90 | + const body = { |
| 91 | + text: () => resp.text(), |
| 92 | + binary: () => resp.buffer(), |
| 93 | + }; |
| 94 | + const response = new ResponseContext(resp.status, headers, body); |
| 95 | + if (this.debug) { |
| 96 | + this.logResponse(response); |
| 97 | + } |
| 98 | + return response; |
| 99 | + }); |
| 100 | + } |
71 | 101 |
|
72 | 102 | return resultPromise;
|
73 | 103 | }
|
|
0 commit comments