fetch-har 8.1.0 → 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 +19 -0
- package/dist/index.d.ts +1 -2
- package/dist/index.js +46 -36
- package/package.json +2 -3
- package/src/index.ts +64 -48
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,22 @@
|
|
|
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
|
+
|
|
8
|
+
## <small>8.1.2 (2022-07-27)</small>
|
|
9
|
+
|
|
10
|
+
* fix: updating the data-urls package to fix a problem with file case sensititivity ([42cadff](https://github.com/readmeio/fetch-har/commit/42cadff))
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
## <small>8.1.1 (2022-07-27)</small>
|
|
15
|
+
|
|
16
|
+
* fix: swapping parse-data-url for @readme/data-urls (#307) ([a2306bd](https://github.com/readmeio/fetch-har/commit/a2306bd)), closes [#307](https://github.com/readmeio/fetch-har/issues/307)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
1
20
|
## 8.1.0 (2022-07-26)
|
|
2
21
|
|
|
3
22
|
* chore: code comment cleanup ([bd344f1](https://github.com/readmeio/fetch-har/commit/bd344f1))
|
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
|
@@ -10,12 +10,9 @@ var __assign = (this && this.__assign) || function () {
|
|
|
10
10
|
};
|
|
11
11
|
return __assign.apply(this, arguments);
|
|
12
12
|
};
|
|
13
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
14
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
15
|
-
};
|
|
16
13
|
exports.__esModule = true;
|
|
17
14
|
var readable_stream_1 = require("readable-stream");
|
|
18
|
-
var
|
|
15
|
+
var data_urls_1 = require("@readme/data-urls");
|
|
19
16
|
if (!globalThis.Blob) {
|
|
20
17
|
try {
|
|
21
18
|
// eslint-disable-next-line @typescript-eslint/no-var-requires, import/no-extraneous-dependencies
|
|
@@ -85,6 +82,15 @@ function isFormData(value) {
|
|
|
85
82
|
isFunction(value.entries) &&
|
|
86
83
|
isFunction(value[Symbol.iterator]));
|
|
87
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
|
+
}
|
|
88
94
|
function fetchHAR(har, opts) {
|
|
89
95
|
var _a;
|
|
90
96
|
if (opts === void 0) { opts = {}; }
|
|
@@ -202,25 +208,27 @@ function fetchHAR(har, opts) {
|
|
|
202
208
|
}
|
|
203
209
|
request.postData.params.forEach(function (param) {
|
|
204
210
|
if ('fileName' in param) {
|
|
205
|
-
if (opts.files
|
|
206
|
-
var fileContents = opts.files
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
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.');
|
|
214
227
|
}
|
|
215
|
-
else if (isFile(fileContents)) {
|
|
216
|
-
form_1.append(param.name, fileContents, param.fileName);
|
|
217
|
-
return;
|
|
218
|
-
}
|
|
219
|
-
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.');
|
|
220
228
|
}
|
|
221
|
-
|
|
229
|
+
if ('value' in param) {
|
|
222
230
|
var paramBlob = void 0;
|
|
223
|
-
var parsed = (0,
|
|
231
|
+
var parsed = (0, data_urls_1.parse)(param.value);
|
|
224
232
|
if (parsed) {
|
|
225
233
|
// If we were able to parse out this data URL we don't need to transform its data
|
|
226
234
|
// into a buffer for `Blob` because that supports data URLs already.
|
|
@@ -275,26 +283,28 @@ function fetchHAR(har, opts) {
|
|
|
275
283
|
// If we've got `files` map content present, and this post data content contains a valid data
|
|
276
284
|
// URL then we can substitute the payload with that file instead of the using data URL.
|
|
277
285
|
if (opts.files) {
|
|
278
|
-
var parsed = (0,
|
|
286
|
+
var parsed = (0, data_urls_1.parse)(request.postData.text);
|
|
279
287
|
if (parsed) {
|
|
280
288
|
if ((parsed === null || parsed === void 0 ? void 0 : parsed.name) && parsed.name in opts.files) {
|
|
281
|
-
var fileContents = opts.files
|
|
282
|
-
if (
|
|
283
|
-
|
|
284
|
-
}
|
|
285
|
-
else if (isFile(fileContents)) {
|
|
286
|
-
// `Readable.from` isn't available in browsers but the browser `Request` object can
|
|
287
|
-
// handle `File` objects just fine without us having to mold it into shape.
|
|
288
|
-
if (isBrowser()) {
|
|
289
|
+
var fileContents = getFileFromSuppliedFiles(parsed.name, opts.files);
|
|
290
|
+
if (fileContents) {
|
|
291
|
+
if (isBuffer(fileContents)) {
|
|
289
292
|
options.body = fileContents;
|
|
290
293
|
}
|
|
291
|
-
else {
|
|
292
|
-
//
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
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
|
+
}
|
|
298
308
|
}
|
|
299
309
|
}
|
|
300
310
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "fetch-har",
|
|
3
|
-
"version": "8.1.
|
|
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",
|
|
@@ -30,8 +30,8 @@
|
|
|
30
30
|
},
|
|
31
31
|
"homepage": "https://github.com/readmeio/fetch-har#readme",
|
|
32
32
|
"dependencies": {
|
|
33
|
+
"@readme/data-urls": "^1.0.1",
|
|
33
34
|
"@types/har-format": "^1.2.8",
|
|
34
|
-
"parse-data-url": "^4.0.1",
|
|
35
35
|
"readable-stream": "^3.6.0"
|
|
36
36
|
},
|
|
37
37
|
"optionalDependencies": {
|
|
@@ -46,7 +46,6 @@
|
|
|
46
46
|
"@types/mocha": "^9.1.1",
|
|
47
47
|
"@types/multer": "^1.4.7",
|
|
48
48
|
"@types/node": "^18.0.0",
|
|
49
|
-
"@types/parse-data-url": "^3.0.0",
|
|
50
49
|
"@types/readable-stream": "^2.3.13",
|
|
51
50
|
"chai": "^4.3.4",
|
|
52
51
|
"datauri": "^4.1.0",
|
package/src/index.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { Har } from 'har-format';
|
|
2
|
+
import type { DataURL as npmDataURL } from '@readme/data-urls';
|
|
2
3
|
import { Readable } from 'readable-stream';
|
|
3
|
-
import parseDataUrl from '
|
|
4
|
+
import { parse as parseDataUrl } from '@readme/data-urls';
|
|
4
5
|
|
|
5
6
|
if (!globalThis.Blob) {
|
|
6
7
|
try {
|
|
@@ -35,6 +36,19 @@ if (!globalThis.FormData) {
|
|
|
35
36
|
}
|
|
36
37
|
}
|
|
37
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
|
+
|
|
38
52
|
function isBrowser() {
|
|
39
53
|
return typeof window !== 'undefined' && typeof document !== 'undefined';
|
|
40
54
|
}
|
|
@@ -85,18 +99,15 @@ function isFormData(value: any) {
|
|
|
85
99
|
);
|
|
86
100
|
}
|
|
87
101
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
}
|
|
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
|
+
}
|
|
94
108
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
// returned back to us.
|
|
98
|
-
name?: string;
|
|
99
|
-
};
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
100
111
|
|
|
101
112
|
export default function fetchHAR(har: Har, opts: FetchHAROptions = {}) {
|
|
102
113
|
if (!har) throw new Error('Missing HAR definition');
|
|
@@ -231,30 +242,33 @@ export default function fetchHAR(har: Har, opts: FetchHAROptions = {}) {
|
|
|
231
242
|
|
|
232
243
|
request.postData.params.forEach(param => {
|
|
233
244
|
if ('fileName' in param) {
|
|
234
|
-
if (opts.files
|
|
235
|
-
const fileContents = opts.files
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
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.'
|
|
246
267
|
);
|
|
247
|
-
|
|
248
|
-
return;
|
|
249
|
-
} else if (isFile(fileContents)) {
|
|
250
|
-
form.append(param.name, fileContents as Blob, param.fileName);
|
|
251
|
-
return;
|
|
252
268
|
}
|
|
269
|
+
}
|
|
253
270
|
|
|
254
|
-
|
|
255
|
-
'An unknown object has been supplied into the `files` config for use. We only support instances of the File API and Node Buffer objects.'
|
|
256
|
-
);
|
|
257
|
-
} else if ('value' in param) {
|
|
271
|
+
if ('value' in param) {
|
|
258
272
|
let paramBlob;
|
|
259
273
|
const parsed = parseDataUrl(param.value);
|
|
260
274
|
if (parsed) {
|
|
@@ -320,22 +334,24 @@ export default function fetchHAR(har: Har, opts: FetchHAROptions = {}) {
|
|
|
320
334
|
const parsed = parseDataUrl(request.postData.text) as DataURL;
|
|
321
335
|
if (parsed) {
|
|
322
336
|
if (parsed?.name && parsed.name in opts.files) {
|
|
323
|
-
const fileContents = opts.files
|
|
324
|
-
if (
|
|
325
|
-
|
|
326
|
-
} else if (isFile(fileContents)) {
|
|
327
|
-
// `Readable.from` isn't available in browsers but the browser `Request` object can
|
|
328
|
-
// handle `File` objects just fine without us having to mold it into shape.
|
|
329
|
-
if (isBrowser()) {
|
|
337
|
+
const fileContents = getFileFromSuppliedFiles(parsed.name, opts.files);
|
|
338
|
+
if (fileContents) {
|
|
339
|
+
if (isBuffer(fileContents)) {
|
|
330
340
|
options.body = fileContents;
|
|
331
|
-
} else {
|
|
332
|
-
//
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
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
|
+
}
|
|
339
355
|
}
|
|
340
356
|
}
|
|
341
357
|
}
|