fetch-har 8.1.2 → 8.1.3

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/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ ## <small>8.1.3 (2022-07-29)</small>
2
+
3
+ * chore: clenaing up some lengthy test comments ([7f797af](https://github.com/readmeio/fetch-har/commit/7f797af))
4
+ * fix: postData filenames may be encoded so we should look for them in `opts.files` too (#308) ([f938ee3](https://github.com/readmeio/fetch-har/commit/f938ee3)), closes [#308](https://github.com/readmeio/fetch-har/issues/308)
5
+
6
+
7
+
1
8
  ## <small>8.1.2 (2022-07-27)</small>
2
9
 
3
10
  * fix: updating the data-urls package to fix a problem with file case sensititivity ([42cadff](https://github.com/readmeio/fetch-har/commit/42cadff))
package/dist/index.d.ts CHANGED
@@ -1,10 +1,9 @@
1
1
  /// <reference types="node" />
2
2
  import type { Har } from 'har-format';
3
- declare type FetchHAROptions = {
3
+ export declare type FetchHAROptions = {
4
4
  userAgent?: string;
5
5
  files?: Record<string, Blob | Buffer>;
6
6
  multipartEncoder?: any;
7
7
  init?: RequestInit;
8
8
  };
9
9
  export default function fetchHAR(har: Har, opts?: FetchHAROptions): Promise<Response>;
10
- export {};
package/dist/index.js CHANGED
@@ -82,6 +82,15 @@ function isFormData(value) {
82
82
  isFunction(value.entries) &&
83
83
  isFunction(value[Symbol.iterator]));
84
84
  }
85
+ function getFileFromSuppliedFiles(filename, files) {
86
+ if (filename in files) {
87
+ return files[filename];
88
+ }
89
+ else if (decodeURIComponent(filename) in files) {
90
+ return files[decodeURIComponent(filename)];
91
+ }
92
+ return false;
93
+ }
85
94
  function fetchHAR(har, opts) {
86
95
  var _a;
87
96
  if (opts === void 0) { opts = {}; }
@@ -199,23 +208,25 @@ function fetchHAR(har, opts) {
199
208
  }
200
209
  request.postData.params.forEach(function (param) {
201
210
  if ('fileName' in param) {
202
- if (opts.files && param.fileName in opts.files) {
203
- var fileContents = opts.files[param.fileName];
204
- // If the file we've got available to us is a Buffer then we need to convert it so
205
- // that the FormData API can use it.
206
- if (isBuffer(fileContents)) {
207
- form_1.append(param.name, new File([fileContents], param.fileName, {
208
- type: param.contentType || null
209
- }), param.fileName);
210
- return;
211
+ if (opts.files) {
212
+ var fileContents = getFileFromSuppliedFiles(param.fileName, opts.files);
213
+ if (fileContents) {
214
+ // If the file we've got available to us is a Buffer then we need to convert it so
215
+ // that the FormData API can use it.
216
+ if (isBuffer(fileContents)) {
217
+ form_1.append(param.name, new File([fileContents], param.fileName, {
218
+ type: param.contentType || null
219
+ }), param.fileName);
220
+ return;
221
+ }
222
+ else if (isFile(fileContents)) {
223
+ form_1.append(param.name, fileContents, param.fileName);
224
+ return;
225
+ }
226
+ 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.');
211
227
  }
212
- else if (isFile(fileContents)) {
213
- form_1.append(param.name, fileContents, param.fileName);
214
- return;
215
- }
216
- 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.');
217
228
  }
218
- else if ('value' in param) {
229
+ if ('value' in param) {
219
230
  var paramBlob = void 0;
220
231
  var parsed = (0, data_urls_1.parse)(param.value);
221
232
  if (parsed) {
@@ -275,23 +286,25 @@ function fetchHAR(har, opts) {
275
286
  var parsed = (0, data_urls_1.parse)(request.postData.text);
276
287
  if (parsed) {
277
288
  if ((parsed === null || parsed === void 0 ? void 0 : parsed.name) && parsed.name in opts.files) {
278
- var fileContents = opts.files[parsed.name];
279
- if (isBuffer(fileContents)) {
280
- options.body = fileContents;
281
- }
282
- else if (isFile(fileContents)) {
283
- // `Readable.from` isn't available in browsers but the browser `Request` object can
284
- // handle `File` objects just fine without us having to mold it into shape.
285
- if (isBrowser()) {
289
+ var fileContents = getFileFromSuppliedFiles(parsed.name, opts.files);
290
+ if (fileContents) {
291
+ if (isBuffer(fileContents)) {
286
292
  options.body = fileContents;
287
293
  }
288
- else {
289
- // @ts-expect-error "Property 'from' does not exist on type 'typeof Readable'." but it does!
290
- options.body = readable_stream_1.Readable.from(fileContents.stream());
291
- // Supplying a polyfilled `File` stream into `Request.body` doesn't automatically
292
- // add `Content-Length`.
293
- if (!headers.has('content-length')) {
294
- headers.set('content-length', String(fileContents.size));
294
+ else if (isFile(fileContents)) {
295
+ // `Readable.from` isn't available in browsers but the browser `Request` object can
296
+ // handle `File` objects just fine without us having to mold it into shape.
297
+ if (isBrowser()) {
298
+ options.body = fileContents;
299
+ }
300
+ else {
301
+ // @ts-expect-error "Property 'from' does not exist on type 'typeof Readable'." but it does!
302
+ options.body = readable_stream_1.Readable.from(fileContents.stream());
303
+ // Supplying a polyfilled `File` stream into `Request.body` doesn't automatically
304
+ // add `Content-Length`.
305
+ if (!headers.has('content-length')) {
306
+ headers.set('content-length', String(fileContents.size));
307
+ }
295
308
  }
296
309
  }
297
310
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fetch-har",
3
- "version": "8.1.2",
3
+ "version": "8.1.3",
4
4
  "description": "Make a fetch request from a HAR definition",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/src/index.ts CHANGED
@@ -36,6 +36,19 @@ if (!globalThis.FormData) {
36
36
  }
37
37
  }
38
38
 
39
+ export type FetchHAROptions = {
40
+ userAgent?: string;
41
+ files?: Record<string, Blob | Buffer>;
42
+ multipartEncoder?: any; // form-data-encoder
43
+ init?: RequestInit;
44
+ };
45
+
46
+ type DataURL = npmDataURL & {
47
+ // `parse-data-url` doesn't explicitly support `name` in data URLs but if it's there it'll be
48
+ // returned back to us.
49
+ name?: string;
50
+ };
51
+
39
52
  function isBrowser() {
40
53
  return typeof window !== 'undefined' && typeof document !== 'undefined';
41
54
  }
@@ -86,18 +99,15 @@ function isFormData(value: any) {
86
99
  );
87
100
  }
88
101
 
89
- type FetchHAROptions = {
90
- userAgent?: string;
91
- files?: Record<string, Blob | Buffer>;
92
- multipartEncoder?: any; // form-data-encoder
93
- init?: RequestInit;
94
- };
102
+ function getFileFromSuppliedFiles(filename: string, files: FetchHAROptions['files']) {
103
+ if (filename in files) {
104
+ return files[filename];
105
+ } else if (decodeURIComponent(filename) in files) {
106
+ return files[decodeURIComponent(filename)];
107
+ }
95
108
 
96
- type DataURL = npmDataURL & {
97
- // `parse-data-url` doesn't explicitly support `name` in data URLs but if it's there it'll be
98
- // returned back to us.
99
- name?: string;
100
- };
109
+ return false;
110
+ }
101
111
 
102
112
  export default function fetchHAR(har: Har, opts: FetchHAROptions = {}) {
103
113
  if (!har) throw new Error('Missing HAR definition');
@@ -232,30 +242,33 @@ export default function fetchHAR(har: Har, opts: FetchHAROptions = {}) {
232
242
 
233
243
  request.postData.params.forEach(param => {
234
244
  if ('fileName' in param) {
235
- if (opts.files && param.fileName in opts.files) {
236
- const fileContents = opts.files[param.fileName];
237
-
238
- // If the file we've got available to us is a Buffer then we need to convert it so
239
- // that the FormData API can use it.
240
- if (isBuffer(fileContents)) {
241
- form.append(
242
- param.name,
243
- new File([fileContents], param.fileName, {
244
- type: param.contentType || null,
245
- }),
246
- param.fileName
245
+ if (opts.files) {
246
+ const fileContents = getFileFromSuppliedFiles(param.fileName, opts.files);
247
+ if (fileContents) {
248
+ // If the file we've got available to us is a Buffer then we need to convert it so
249
+ // that the FormData API can use it.
250
+ if (isBuffer(fileContents)) {
251
+ form.append(
252
+ param.name,
253
+ new File([fileContents], param.fileName, {
254
+ type: param.contentType || null,
255
+ }),
256
+ param.fileName
257
+ );
258
+
259
+ return;
260
+ } else if (isFile(fileContents)) {
261
+ form.append(param.name, fileContents as Blob, param.fileName);
262
+ return;
263
+ }
264
+
265
+ throw new TypeError(
266
+ 'An unknown object has been supplied into the `files` config for use. We only support instances of the File API and Node Buffer objects.'
247
267
  );
248
-
249
- return;
250
- } else if (isFile(fileContents)) {
251
- form.append(param.name, fileContents as Blob, param.fileName);
252
- return;
253
268
  }
269
+ }
254
270
 
255
- throw new TypeError(
256
- 'An unknown object has been supplied into the `files` config for use. We only support instances of the File API and Node Buffer objects.'
257
- );
258
- } else if ('value' in param) {
271
+ if ('value' in param) {
259
272
  let paramBlob;
260
273
  const parsed = parseDataUrl(param.value);
261
274
  if (parsed) {
@@ -321,22 +334,24 @@ export default function fetchHAR(har: Har, opts: FetchHAROptions = {}) {
321
334
  const parsed = parseDataUrl(request.postData.text) as DataURL;
322
335
  if (parsed) {
323
336
  if (parsed?.name && parsed.name in opts.files) {
324
- const fileContents = opts.files[parsed.name];
325
- if (isBuffer(fileContents)) {
326
- options.body = fileContents;
327
- } else if (isFile(fileContents)) {
328
- // `Readable.from` isn't available in browsers but the browser `Request` object can
329
- // handle `File` objects just fine without us having to mold it into shape.
330
- if (isBrowser()) {
337
+ const fileContents = getFileFromSuppliedFiles(parsed.name, opts.files);
338
+ if (fileContents) {
339
+ if (isBuffer(fileContents)) {
331
340
  options.body = fileContents;
332
- } else {
333
- // @ts-expect-error "Property 'from' does not exist on type 'typeof Readable'." but it does!
334
- options.body = Readable.from((fileContents as File).stream());
335
-
336
- // Supplying a polyfilled `File` stream into `Request.body` doesn't automatically
337
- // add `Content-Length`.
338
- if (!headers.has('content-length')) {
339
- headers.set('content-length', String((fileContents as File).size));
341
+ } else if (isFile(fileContents)) {
342
+ // `Readable.from` isn't available in browsers but the browser `Request` object can
343
+ // handle `File` objects just fine without us having to mold it into shape.
344
+ if (isBrowser()) {
345
+ options.body = fileContents;
346
+ } else {
347
+ // @ts-expect-error "Property 'from' does not exist on type 'typeof Readable'." but it does!
348
+ options.body = Readable.from((fileContents as File).stream());
349
+
350
+ // Supplying a polyfilled `File` stream into `Request.body` doesn't automatically
351
+ // add `Content-Length`.
352
+ if (!headers.has('content-length')) {
353
+ headers.set('content-length', String((fileContents as File).size));
354
+ }
340
355
  }
341
356
  }
342
357
  }