fetch-har 10.0.0 → 11.0.0

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/README.md CHANGED
@@ -1,4 +1,5 @@
1
1
  # fetch-har
2
+
2
3
  Make a [fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) request from a HAR definition.
3
4
 
4
5
  [![CI](https://github.com/readmeio/fetch-har/workflows/CI/badge.svg)](https://github.com/readmeio/fetch-har/)
@@ -12,7 +13,7 @@ Make a [fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) reque
12
13
  - Supports Node 18+
13
14
  - Natively works in all browsers that support [fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) without having to use any polyfils.
14
15
  - [Tested](https://github.com/readmeio/fetch-har/actions) across Chrome, Safari, Firefox on Mac, Windows, and Linux.
15
- - Requests can be mocked with [fetch-mock](https://npm.im/fetch-mock) or [msw](https://npm.im/msw).
16
+ - Requests can be mocked with [`msw`](https://npm.im/msw) or [`fetch-mock`](https://npm.im/fetch-mock) (though the latter does not appear to be maintained).
16
17
 
17
18
  ## Installation
18
19
 
@@ -21,9 +22,10 @@ npm install --save fetch-har
21
22
  ```
22
23
 
23
24
  ## Usage
25
+
24
26
  ```js
25
27
  import fetchHAR from 'fetch-har';
26
- // const fetchHAR = require('fetch-har').default;
28
+ // const fetchHAR = require('fetch-har');
27
29
 
28
30
  const har = {
29
31
  log: {
@@ -62,8 +64,11 @@ fetchHAR(har)
62
64
  ```
63
65
 
64
66
  ### API
67
+
65
68
  #### Options
69
+
66
70
  ##### userAgent
71
+
67
72
  A custom `User-Agent` header to apply to your request. Please note that browsers have their own handling for these headers in `fetch()` calls so it may not work everywhere; it will always be sent in Node however.
68
73
 
69
74
  ```js
@@ -71,18 +76,22 @@ await fetchHAR(har, { userAgent: 'my-client/1.0' });
71
76
  ```
72
77
 
73
78
  ##### files
79
+
74
80
  An optional object map you can supply to use for `multipart/form-data` file uploads in leu of relying on if the HAR you have has [data URLs](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs). It supports Node file buffers and the [File](https://developer.mozilla.org/en-US/docs/Web/API/File) API.
75
81
 
76
82
  ```js
77
- await fetchHAR(har, { files: {
78
- 'owlbert.png': await fs.readFile('./owlbert.png'),
79
- 'file.txt': document.querySelector('#some-file-input').files[0],
80
- } });
83
+ await fetchHAR(har, {
84
+ files: {
85
+ 'owlbert.png': await fs.readFile('./owlbert.png'),
86
+ 'file.txt': document.querySelector('#some-file-input').files[0],
87
+ },
88
+ });
81
89
  ```
82
90
 
83
91
  If you don't supply this option `fetch-har` will fallback to the data URL present within the supplied HAR. If no `files` option is present, and no data URL (via `param.value`) is present in the HAR, a fatal exception will be thrown.
84
92
 
85
93
  ##### init
94
+
86
95
  This optional argument lets you supply any option that's available to supply to the [Request constructor](https://developer.mozilla.org/en-US/docs/Web/API/Request/Request).
87
96
 
88
97
  ```js
@@ -92,7 +101,7 @@ await fetchHAR(har, {
92
101
  'x-custom-header': 'buster',
93
102
  }),
94
103
  },
95
- })
104
+ });
96
105
  ```
97
106
 
98
107
  > ❗ Note that if you supply `body` or `credentials` to this option they may be overridden by what your HAR requires.
@@ -0,0 +1,6 @@
1
+ import { FetchHAROptions } from './types.mjs';
2
+ import { Har } from 'har-format';
3
+
4
+ declare function fetchHAR(har: Har, opts?: FetchHAROptions): Promise<Response>;
5
+
6
+ export { fetchHAR as default };
package/dist/index.d.ts CHANGED
@@ -1,21 +1,6 @@
1
- /// <reference types="node" />
2
- import type { Har } from 'har-format';
3
- interface RequestInitWithDuplex extends RequestInit {
4
- /**
5
- * `RequestInit#duplex` does not yet exist in the TS `lib.dom.d.ts` definition yet the native
6
- * fetch implementation in Node 18+, `undici`, requires it for certain POST payloads.
7
- *
8
- * @see {@link https://github.com/microsoft/TypeScript-DOM-lib-generator/issues/1483}
9
- * @see {@link https://github.com/nodejs/node/issues/46221}
10
- * @see {@link https://fetch.spec.whatwg.org/#request-class}
11
- * @see {@link https://github.com/microsoft/TypeScript/blob/main/lib/lib.dom.d.ts}
12
- */
13
- duplex?: 'half';
14
- }
15
- export interface FetchHAROptions {
16
- files?: Record<string, Blob | Buffer>;
17
- init?: RequestInitWithDuplex;
18
- userAgent?: string;
19
- }
20
- export default function fetchHAR(har: Har, opts?: FetchHAROptions): Promise<Response>;
21
- export {};
1
+ import { FetchHAROptions } from './types.js';
2
+ import { Har } from 'har-format';
3
+
4
+ declare function fetchHAR(har: Har, opts?: FetchHAROptions): Promise<Response>;
5
+
6
+ export = fetchHAR;
package/dist/index.js CHANGED
@@ -1,291 +1,240 @@
1
- "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});var __defProp = Object.defineProperty;
2
+ var __defProps = Object.defineProperties;
3
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
4
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
7
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
+ var __spreadValues = (a, b) => {
9
+ for (var prop in b || (b = {}))
10
+ if (__hasOwnProp.call(b, prop))
11
+ __defNormalProp(a, prop, b[prop]);
12
+ if (__getOwnPropSymbols)
13
+ for (var prop of __getOwnPropSymbols(b)) {
14
+ if (__propIsEnum.call(b, prop))
15
+ __defNormalProp(a, prop, b[prop]);
16
+ }
17
+ return a;
12
18
  };
13
- Object.defineProperty(exports, "__esModule", { value: true });
14
- var data_urls_1 = require("@readme/data-urls");
15
- var readable_stream_1 = require("readable-stream");
19
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
+ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
21
+ get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
22
+ }) : x)(function(x) {
23
+ if (typeof require !== "undefined")
24
+ return require.apply(this, arguments);
25
+ throw Error('Dynamic require of "' + x + '" is not supported');
26
+ });
27
+
28
+ // src/index.ts
29
+ var _dataurls = require('@readme/data-urls');
16
30
  if (!globalThis.Blob) {
17
- try {
18
- // eslint-disable-next-line @typescript-eslint/no-var-requires
19
- globalThis.Blob = require('node:buffer').Blob;
20
- }
21
- catch (e) {
22
- throw new Error('The Blob API is required for this library. https://developer.mozilla.org/en-US/docs/Web/API/Blob');
23
- }
31
+ try {
32
+ globalThis.Blob = __require("buffer").Blob;
33
+ } catch (e) {
34
+ throw new Error("The Blob API is required for this library. https://developer.mozilla.org/en-US/docs/Web/API/Blob");
35
+ }
24
36
  }
25
37
  if (!globalThis.File) {
26
- try {
27
- // Node's native `fetch` implementation unfortunately does not make this API global so we need
28
- // to pull it in if we don't have it.
29
- // eslint-disable-next-line @typescript-eslint/no-var-requires
30
- globalThis.File = require('undici').File;
31
- }
32
- catch (e) {
33
- throw new Error('The File API is required for this library. https://developer.mozilla.org/en-US/docs/Web/API/File');
34
- }
38
+ try {
39
+ globalThis.File = __require("undici").File;
40
+ } catch (e) {
41
+ throw new Error("The File API is required for this library. https://developer.mozilla.org/en-US/docs/Web/API/File");
42
+ }
35
43
  }
36
44
  function isBrowser() {
37
- return typeof window !== 'undefined' && typeof document !== 'undefined';
45
+ return typeof window !== "undefined" && typeof document !== "undefined";
38
46
  }
39
47
  function isBuffer(value) {
40
- return typeof Buffer !== 'undefined' && Buffer.isBuffer(value);
48
+ return typeof Buffer !== "undefined" && Buffer.isBuffer(value);
41
49
  }
42
50
  function isFile(value) {
43
- if (value instanceof File) {
44
- /**
45
- * The `Blob` polyfill on Node comes back as being an instanceof `File`. Because passing a Blob
46
- * into a File will end up with a corrupted file we want to prevent this.
47
- *
48
- * This object identity crisis does not happen in the browser.
49
- */
50
- return value.constructor.name === 'File';
51
- }
52
- return false;
51
+ if (value instanceof File) {
52
+ return value.constructor.name === "File";
53
+ }
54
+ return false;
53
55
  }
54
56
  function getFileFromSuppliedFiles(filename, files) {
55
- if (filename in files) {
56
- return files[filename];
57
- }
58
- else if (decodeURIComponent(filename) in files) {
59
- return files[decodeURIComponent(filename)];
60
- }
61
- return false;
57
+ if (files && filename in files) {
58
+ return files[filename];
59
+ } else if (files && decodeURIComponent(filename) in files) {
60
+ return files[decodeURIComponent(filename)];
61
+ }
62
+ return false;
62
63
  }
63
- function fetchHAR(har, opts) {
64
- var _a;
65
- if (opts === void 0) { opts = {}; }
66
- if (!har)
67
- throw new Error('Missing HAR definition');
68
- if (!har.log || !har.log.entries || !har.log.entries.length)
69
- throw new Error('Missing log.entries array');
70
- var request = har.log.entries[0].request;
71
- var url = request.url;
72
- var querystring = '';
73
- var shouldSetDuplex = false;
74
- var options = __assign(__assign({}, (opts.init ? opts.init : {})), { method: request.method });
75
- if (!options.headers) {
76
- options.headers = new Headers();
77
- }
78
- else if (typeof options.headers === 'object' && !(options.headers instanceof Headers) && options.headers !== null) {
79
- options.headers = new Headers(options.headers);
80
- }
81
- var headers = options.headers;
82
- if ('headers' in request && request.headers.length) {
83
- // eslint-disable-next-line consistent-return
84
- request.headers.forEach(function (header) {
85
- try {
86
- return headers.append(header.name, header.value);
87
- }
88
- catch (err) {
89
- /**
90
- * `Headers.append()` will throw errors if the header name is not a legal HTTP header name,
91
- * like `X-API-KEY (Header)`. If that happens instead of tossing the error back out, we
92
- * should silently just ignore
93
- * it.
94
- */
95
- }
96
- });
64
+ function fetchHAR(har, opts = {}) {
65
+ var _a, _b, _c, _d, _e;
66
+ if (!har)
67
+ throw new Error("Missing HAR definition");
68
+ if (!har.log || !har.log.entries || !har.log.entries.length)
69
+ throw new Error("Missing log.entries array");
70
+ const { request } = har.log.entries[0];
71
+ const { url } = request;
72
+ let querystring = "";
73
+ let shouldSetDuplex = false;
74
+ const options = __spreadProps(__spreadValues({}, opts.init ? opts.init : {}), {
75
+ method: request.method
76
+ });
77
+ if (!options.headers) {
78
+ options.headers = new Headers();
79
+ } else if (typeof options.headers === "object" && !(options.headers instanceof Headers) && options.headers !== null) {
80
+ options.headers = new Headers(options.headers);
81
+ }
82
+ const headers = options.headers;
83
+ if ("headers" in request && request.headers.length) {
84
+ request.headers.forEach((header) => {
85
+ try {
86
+ return headers.append(header.name, header.value);
87
+ } catch (err) {
88
+ }
89
+ });
90
+ }
91
+ if ("cookies" in request && request.cookies.length) {
92
+ if (isBrowser()) {
93
+ request.cookies.forEach((cookie) => {
94
+ document.cookie = `${encodeURIComponent(cookie.name)}=${encodeURIComponent(cookie.value)}`;
95
+ });
96
+ options.credentials = "include";
97
+ } else {
98
+ headers.append(
99
+ "cookie",
100
+ request.cookies.map((cookie) => `${encodeURIComponent(cookie.name)}=${encodeURIComponent(cookie.value)}`).join("; ")
101
+ );
97
102
  }
98
- if ('cookies' in request && request.cookies.length) {
99
- /**
100
- * As the browser fetch API can't set custom cookies for requests, they instead need to be
101
- * defined on the document and passed into the request via `credentials: include`. Since this
102
- * is a browser-specific quirk, that should only
103
- * happen in browsers!
104
- */
105
- if (isBrowser()) {
106
- request.cookies.forEach(function (cookie) {
107
- document.cookie = "".concat(encodeURIComponent(cookie.name), "=").concat(encodeURIComponent(cookie.value));
108
- });
109
- options.credentials = 'include';
110
- }
111
- else {
112
- headers.append('cookie', request.cookies
113
- .map(function (cookie) { return "".concat(encodeURIComponent(cookie.name), "=").concat(encodeURIComponent(cookie.value)); })
114
- .join('; '));
115
- }
116
- }
117
- if ('postData' in request) {
118
- if ('params' in request.postData) {
119
- if (!('mimeType' in request.postData)) {
120
- // @ts-expect-error HAR spec requires that `mimeType` is always present but it might not be.
121
- request.postData.mimeType = 'application/octet-stream';
122
- }
123
- switch (request.postData.mimeType) {
124
- case 'application/x-www-form-urlencoded':
125
- /**
126
- * Since the content we're handling here is to be encoded as
127
- * `application/x-www-form-urlencoded`, this should override any other `Content-Type`
128
- * headers that are present in the HAR. This is how Postman handles this case when
129
- * building code snippets!
130
- *
131
- * @see {@link https://github.com/github/fetch/issues/263#issuecomment-209530977}
132
- */
133
- headers.set('Content-Type', request.postData.mimeType);
134
- var encodedParams_1 = new URLSearchParams();
135
- request.postData.params.forEach(function (param) { return encodedParams_1.set(param.name, param.value); });
136
- options.body = encodedParams_1.toString();
137
- break;
138
- case 'multipart/alternative':
139
- case 'multipart/form-data':
140
- case 'multipart/mixed':
141
- case 'multipart/related':
142
- /**
143
- * If there's a `Content-Type` header set we need to remove it. We're doing this because
144
- * when we pass the form data object into `fetch` that'll set a proper `Content-Type`
145
- * header for this request that also includes the boundary used on the content.
146
- *
147
- * If we don't do this, then consumers won't be able to parse out the payload because
148
- * they won't know what the boundary to split on it.
149
- */
150
- if (headers.has('Content-Type')) {
151
- headers.delete('Content-Type');
152
- }
153
- var form_1 = new FormData();
154
- request.postData.params.forEach(function (param) {
155
- if ('fileName' in param) {
156
- if (opts.files) {
157
- var fileContents = getFileFromSuppliedFiles(param.fileName, opts.files);
158
- if (fileContents) {
159
- // If the file we've got available to us is a Buffer then we need to convert it so
160
- // that the FormData API can use it.
161
- if (isBuffer(fileContents)) {
162
- form_1.append(param.name, new File([fileContents], param.fileName, {
163
- type: param.contentType || null,
164
- }), param.fileName);
165
- return;
166
- }
167
- else if (isFile(fileContents)) {
168
- form_1.append(param.name, fileContents, param.fileName);
169
- return;
170
- }
171
- throw new TypeError('An unknown object has been supplied into the `files` config for use. We only support instances of the File API and Node Buffer objects.');
172
- }
173
- }
174
- if ('value' in param) {
175
- var paramBlob = void 0;
176
- var parsed = (0, data_urls_1.parse)(param.value);
177
- if (parsed) {
178
- // If we were able to parse out this data URL we don't need to transform its data
179
- // into a buffer for `Blob` because that supports data URLs already.
180
- paramBlob = new Blob([param.value], { type: parsed.contentType || param.contentType || null });
181
- }
182
- else {
183
- paramBlob = new Blob([param.value], { type: param.contentType || null });
184
- }
185
- form_1.append(param.name, paramBlob, param.fileName);
186
- return;
187
- }
188
- throw new Error("The supplied HAR has a postData parameter with `fileName`, but neither `value` content within the HAR or any file buffers were supplied with the `files` option. Since this library doesn't have access to the filesystem, it can't fetch that file.");
189
- }
190
- form_1.append(param.name, param.value);
191
- });
192
- options.body = form_1;
193
- break;
194
- default:
195
- var formBody_1 = {};
196
- request.postData.params.map(function (param) {
197
- try {
198
- formBody_1[param.name] = JSON.parse(param.value);
199
- }
200
- catch (e) {
201
- formBody_1[param.name] = param.value;
202
- }
203
- return true;
204
- });
205
- options.body = JSON.stringify(formBody_1);
206
- }
207
- }
208
- else if ((_a = request.postData.text) === null || _a === void 0 ? void 0 : _a.length) {
209
- // If we've got `files` map content present, and this post data content contains a valid data
210
- // URL then we can substitute the payload with that file instead of the using data URL.
211
- if (opts.files) {
212
- var parsed = (0, data_urls_1.parse)(request.postData.text);
103
+ }
104
+ if ("postData" in request) {
105
+ if (request.postData && "params" in request.postData) {
106
+ if (!("mimeType" in request.postData)) {
107
+ request.postData.mimeType = "application/octet-stream";
108
+ }
109
+ switch (request.postData.mimeType) {
110
+ case "application/x-www-form-urlencoded":
111
+ headers.set("Content-Type", request.postData.mimeType);
112
+ const encodedParams = new URLSearchParams();
113
+ (_a = request.postData.params) == null ? void 0 : _a.forEach((param) => {
114
+ if (param.value)
115
+ encodedParams.set(param.name, param.value);
116
+ });
117
+ options.body = encodedParams.toString();
118
+ break;
119
+ case "multipart/alternative":
120
+ case "multipart/form-data":
121
+ case "multipart/mixed":
122
+ case "multipart/related":
123
+ if (headers.has("Content-Type")) {
124
+ headers.delete("Content-Type");
125
+ }
126
+ const form = new FormData();
127
+ (_b = request.postData.params) == null ? void 0 : _b.forEach((param) => {
128
+ if ("fileName" in param && param.fileName) {
129
+ if (opts.files) {
130
+ const fileContents = getFileFromSuppliedFiles(param.fileName, opts.files);
131
+ if (fileContents) {
132
+ if (isBuffer(fileContents)) {
133
+ form.append(
134
+ param.name,
135
+ new File([fileContents], param.fileName, {
136
+ type: param.contentType || void 0
137
+ }),
138
+ param.fileName
139
+ );
140
+ return;
141
+ } else if (isFile(fileContents)) {
142
+ form.append(param.name, fileContents, param.fileName);
143
+ return;
144
+ }
145
+ throw new TypeError(
146
+ "An unknown object has been supplied into the `files` config for use. We only support instances of the File API and Node Buffer objects."
147
+ );
148
+ }
149
+ }
150
+ if ("value" in param && param.value) {
151
+ let paramBlob;
152
+ const parsed = _dataurls.parse.call(void 0, param.value);
213
153
  if (parsed) {
214
- if ((parsed === null || parsed === void 0 ? void 0 : parsed.name) && parsed.name in opts.files) {
215
- var fileContents = getFileFromSuppliedFiles(parsed.name, opts.files);
216
- if (fileContents) {
217
- if (isBuffer(fileContents)) {
218
- options.body = fileContents;
219
- }
220
- else if (isFile(fileContents)) {
221
- // `Readable.from` isn't available in browsers but the browser `Request` object can
222
- // handle `File` objects just fine without us having to mold it into shape.
223
- if (isBrowser()) {
224
- options.body = fileContents;
225
- }
226
- else {
227
- // @ts-expect-error "Property 'from' does not exist on type 'typeof Readable'." but it does!
228
- options.body = readable_stream_1.Readable.from(fileContents.stream());
229
- shouldSetDuplex = true;
230
- // Supplying a polyfilled `File` stream into `Request.body` doesn't automatically
231
- // add `Content-Length`.
232
- if (!headers.has('content-length')) {
233
- headers.set('content-length', String(fileContents.size));
234
- }
235
- }
236
- }
237
- }
238
- }
154
+ paramBlob = new Blob([param.value], { type: parsed.contentType || param.contentType || void 0 });
155
+ } else {
156
+ paramBlob = new Blob([param.value], { type: param.contentType || void 0 });
239
157
  }
158
+ form.append(param.name, paramBlob, param.fileName);
159
+ return;
160
+ }
161
+ throw new Error(
162
+ "The supplied HAR has a postData parameter with `fileName`, but neither `value` content within the HAR or any file buffers were supplied with the `files` option. Since this library doesn't have access to the filesystem, it can't fetch that file."
163
+ );
240
164
  }
241
- if (typeof options.body === 'undefined') {
242
- options.body = request.postData.text;
165
+ if (param.value)
166
+ form.append(param.name, param.value);
167
+ });
168
+ options.body = form;
169
+ break;
170
+ default:
171
+ const formBody = {};
172
+ (_c = request.postData.params) == null ? void 0 : _c.map((param) => {
173
+ try {
174
+ formBody[param.name] = JSON.parse(param.value || "");
175
+ } catch (e) {
176
+ formBody[param.name] = param.value;
243
177
  }
178
+ return true;
179
+ });
180
+ options.body = JSON.stringify(formBody);
181
+ }
182
+ } else if ((_e = (_d = request.postData) == null ? void 0 : _d.text) == null ? void 0 : _e.length) {
183
+ if (opts.files) {
184
+ const parsed = _dataurls.parse.call(void 0, request.postData.text);
185
+ if (parsed) {
186
+ if ((parsed == null ? void 0 : parsed.name) && parsed.name in opts.files) {
187
+ const fileContents = getFileFromSuppliedFiles(parsed.name, opts.files);
188
+ if (fileContents) {
189
+ if (isBuffer(fileContents)) {
190
+ options.body = fileContents;
191
+ } else if (isFile(fileContents)) {
192
+ if (isBrowser()) {
193
+ options.body = fileContents;
194
+ } else {
195
+ options.body = fileContents.stream();
196
+ shouldSetDuplex = true;
197
+ if (!headers.has("content-length")) {
198
+ headers.set("content-length", String(fileContents.size));
199
+ }
200
+ }
201
+ }
202
+ }
203
+ }
244
204
  }
245
- /**
246
- * The fetch spec, which Node 18+ strictly abides by, now requires that `duplex` be sent with
247
- * requests that have payloads.
248
- *
249
- * As `RequestInit#duplex` isn't supported by any browsers, or even mentioned on MDN, we aren't
250
- * sending it in browser environments. This work is purely to support Node 18+ and `undici`
251
- * environments.
252
- *
253
- * @see {@link https://github.com/nodejs/node/issues/46221}
254
- * @see {@link https://github.com/whatwg/fetch/pull/1457}
255
- * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Request/Request}
256
- */
257
- if (shouldSetDuplex && !isBrowser()) {
258
- options.duplex = 'half';
259
- }
205
+ }
206
+ if (typeof options.body === "undefined") {
207
+ options.body = request.postData.text;
208
+ }
260
209
  }
261
- // We automaticaly assume that the HAR that we have already has query parameters encoded within
262
- // it so we do **not** use the `URLSearchParams` API here for composing the query string.
263
- var requestURL = url;
264
- if ('queryString' in request && request.queryString.length) {
265
- var urlObj = new URL(requestURL);
266
- var queryParams_1 = Array.from(urlObj.searchParams).map(function (_a) {
267
- var k = _a[0], v = _a[1];
268
- return "".concat(k, "=").concat(v);
269
- });
270
- request.queryString.forEach(function (q) {
271
- queryParams_1.push("".concat(q.name, "=").concat(q.value));
272
- });
273
- querystring = queryParams_1.join('&');
274
- // Because anchor hashes before query strings will prevent query strings from being delivered
275
- // we need to pop them off and re-add them after.
276
- if (urlObj.hash) {
277
- var urlWithoutHashes = requestURL.replace(urlObj.hash, '');
278
- requestURL = "".concat(urlWithoutHashes.split('?')[0]).concat(querystring ? "?".concat(querystring) : '');
279
- requestURL += urlObj.hash;
280
- }
281
- else {
282
- requestURL = "".concat(requestURL.split('?')[0]).concat(querystring ? "?".concat(querystring) : '');
283
- }
210
+ if (shouldSetDuplex && !isBrowser()) {
211
+ options.duplex = "half";
284
212
  }
285
- if (opts.userAgent) {
286
- headers.append('User-Agent', opts.userAgent);
213
+ }
214
+ let requestURL = url;
215
+ if ("queryString" in request && request.queryString.length) {
216
+ const urlObj = new URL(requestURL);
217
+ const queryParams = Array.from(urlObj.searchParams).map(([k, v]) => `${k}=${v}`);
218
+ request.queryString.forEach((q) => {
219
+ queryParams.push(`${q.name}=${q.value}`);
220
+ });
221
+ querystring = queryParams.join("&");
222
+ if (urlObj.hash) {
223
+ const urlWithoutHashes = requestURL.replace(urlObj.hash, "");
224
+ requestURL = `${urlWithoutHashes.split("?")[0]}${querystring ? `?${querystring}` : ""}`;
225
+ requestURL += urlObj.hash;
226
+ } else {
227
+ requestURL = `${requestURL.split("?")[0]}${querystring ? `?${querystring}` : ""}`;
287
228
  }
288
- options.headers = headers;
289
- return fetch(requestURL, options);
229
+ }
230
+ if (opts.userAgent) {
231
+ headers.append("User-Agent", opts.userAgent);
232
+ }
233
+ options.headers = headers;
234
+ return fetch(requestURL, options);
290
235
  }
236
+
237
+
291
238
  exports.default = fetchHAR;
239
+
240
+ module.exports = exports.default//# sourceMappingURL=index.js.map