fetch-har 9.0.0 → 10.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/src/index.ts CHANGED
@@ -6,34 +6,21 @@ import { Readable } from 'readable-stream';
6
6
 
7
7
  if (!globalThis.Blob) {
8
8
  try {
9
- // eslint-disable-next-line @typescript-eslint/no-var-requires, import/no-extraneous-dependencies
10
- globalThis.Blob = require('formdata-node').Blob;
9
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
10
+ globalThis.Blob = require('node:buffer').Blob;
11
11
  } catch (e) {
12
- throw new Error(
13
- 'Since you do not have the Blob API available in this environment you must install the optional `formdata-node` dependency.'
14
- );
12
+ throw new Error('The Blob API is required for this library. https://developer.mozilla.org/en-US/docs/Web/API/Blob');
15
13
  }
16
14
  }
17
15
 
18
16
  if (!globalThis.File) {
19
17
  try {
20
- // eslint-disable-next-line @typescript-eslint/no-var-requires, import/no-extraneous-dependencies
21
- globalThis.File = require('formdata-node').File;
18
+ // Node's native `fetch` implementation unfortunately does not make this API global so we need
19
+ // to pull it in if we don't have it.
20
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
21
+ globalThis.File = require('undici').File;
22
22
  } catch (e) {
23
- throw new Error(
24
- 'Since you do not have the File API available in this environment you must install the optional `formdata-node` dependency.'
25
- );
26
- }
27
- }
28
-
29
- if (!globalThis.FormData) {
30
- try {
31
- // eslint-disable-next-line @typescript-eslint/no-var-requires, import/no-extraneous-dependencies
32
- globalThis.FormData = require('formdata-node').FormData;
33
- } catch (e) {
34
- throw new Error(
35
- 'Since you do not have the FormData API available in this environment you must install the optional `formdata-node` dependency.'
36
- );
23
+ throw new Error('The File API is required for this library. https://developer.mozilla.org/en-US/docs/Web/API/File');
37
24
  }
38
25
  }
39
26
 
@@ -51,10 +38,9 @@ interface RequestInitWithDuplex extends RequestInit {
51
38
  }
52
39
 
53
40
  export interface FetchHAROptions {
54
- userAgent?: string;
55
41
  files?: Record<string, Blob | Buffer>;
56
- multipartEncoder?: any; // form-data-encoder
57
42
  init?: RequestInitWithDuplex;
43
+ userAgent?: string;
58
44
  }
59
45
 
60
46
  type DataURL = npmDataURL & {
@@ -85,34 +71,6 @@ function isFile(value: any) {
85
71
  return false;
86
72
  }
87
73
 
88
- /**
89
- * @license MIT
90
- * @see {@link https://github.com/octet-stream/form-data-encoder/blob/master/lib/util/isFunction.ts}
91
- */
92
- function isFunction(value: any) {
93
- return typeof value === 'function';
94
- }
95
-
96
- /**
97
- * We're using this library in here instead of loading it from `form-data-encoder` because that
98
- * uses lookbehind regex in its main encoder that Safari doesn't support so it throws a fatal page
99
- * exception.
100
- *
101
- * @license MIT
102
- * @see {@link https://github.com/octet-stream/form-data-encoder/blob/master/lib/util/isFormData.ts}
103
- */
104
- function isFormData(value: any) {
105
- return (
106
- value &&
107
- isFunction(value.constructor) &&
108
- value[Symbol.toStringTag] === 'FormData' &&
109
- isFunction(value.append) &&
110
- isFunction(value.getAll) &&
111
- isFunction(value.entries) &&
112
- isFunction(value[Symbol.iterator])
113
- );
114
- }
115
-
116
74
  function getFileFromSuppliedFiles(filename: string, files: FetchHAROptions['files']) {
117
75
  if (filename in files) {
118
76
  return files[filename];
@@ -180,7 +138,7 @@ export default function fetchHAR(har: Har, opts: FetchHAROptions = {}) {
180
138
  'cookie',
181
139
  request.cookies
182
140
  .map(cookie => `${encodeURIComponent(cookie.name)}=${encodeURIComponent(cookie.value)}`)
183
- .join('; ')
141
+ .join('; '),
184
142
  );
185
143
  }
186
144
  }
@@ -227,33 +185,6 @@ export default function fetchHAR(har: Har, opts: FetchHAROptions = {}) {
227
185
  }
228
186
 
229
187
  const form = new FormData();
230
- if (!isFormData(form)) {
231
- /**
232
- * The `form-data` NPM module returns one of two things: a native `FormData` API or its
233
- * own polyfill. Unfortunately this polyfill does not support the full API of the native
234
- * FormData object so when you load `form-data` within a browser environment you'll
235
- * have two major differences in API:
236
- *
237
- * - The `.append()` API in `form-data` requires that the third argument is an object
238
- * containing various, undocumented, options. In the browser, `.append()`'s third
239
- * argument should only be present when the second is a `Blob` or `USVString`, and
240
- * when it is present, it should be a filename string.
241
- * - `form-data` does not expose an `.entries()` API, so the only way to retrieve data
242
- * out of it for construction of boundary-separated payload content is to use its
243
- * `.pipe()` API. Since the browser doesn't have this API, you'll be unable to
244
- * retrieve data out of it.
245
- *
246
- * Now since the native `FormData` API is iterable, and has the `.entries()` iterator,
247
- * we can easily detect if we have a native copy of the FormData API. It's for all of
248
- * these reasons that we're opting to hard crash here because supporting this
249
- * non-compliant API is more trouble than its worth.
250
- *
251
- * @see {@link https://github.com/form-data/form-data/issues/124}
252
- */
253
- throw new Error(
254
- "We've detected you're using a non-spec compliant FormData library. We recommend polyfilling FormData with https://npm.im/formdata-node"
255
- );
256
- }
257
188
 
258
189
  request.postData.params.forEach(param => {
259
190
  if ('fileName' in param) {
@@ -268,7 +199,7 @@ export default function fetchHAR(har: Har, opts: FetchHAROptions = {}) {
268
199
  new File([fileContents], param.fileName, {
269
200
  type: param.contentType || null,
270
201
  }),
271
- param.fileName
202
+ param.fileName,
272
203
  );
273
204
 
274
205
  return;
@@ -278,7 +209,7 @@ export default function fetchHAR(har: Har, opts: FetchHAROptions = {}) {
278
209
  }
279
210
 
280
211
  throw new TypeError(
281
- 'An unknown object has been supplied into the `files` config for use. We only support instances of the File API and Node Buffer objects.'
212
+ 'An unknown object has been supplied into the `files` config for use. We only support instances of the File API and Node Buffer objects.',
282
213
  );
283
214
  }
284
215
  }
@@ -299,34 +230,14 @@ export default function fetchHAR(har: Har, opts: FetchHAROptions = {}) {
299
230
  }
300
231
 
301
232
  throw new Error(
302
- "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."
233
+ "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.",
303
234
  );
304
235
  }
305
236
 
306
237
  form.append(param.name, param.value);
307
238
  });
308
239
 
309
- /**
310
- * If a the `fetch` polyfill that's being used here doesn't have spec-compliant handling
311
- * for the `FormData` API (like `node-fetch@2`), then you should pass in a handler (like
312
- * the `form-data-encoder` library) to transform its contents into something that can be
313
- * used with the `Request` object.
314
- *
315
- * @see {@link https://www.npmjs.com/package/formdata-node}
316
- */
317
- if (opts.multipartEncoder) {
318
- // eslint-disable-next-line new-cap
319
- const encoder = new opts.multipartEncoder(form);
320
- Object.keys(encoder.headers).forEach(header => {
321
- headers.set(header, encoder.headers[header]);
322
- });
323
-
324
- // @ts-expect-error "Property 'from' does not exist on type 'typeof Readable'." but it does!
325
- options.body = Readable.from(encoder);
326
- shouldSetDuplex = true;
327
- } else {
328
- options.body = form;
329
- }
240
+ options.body = form;
330
241
  break;
331
242
 
332
243
  default: