Skip to content

Commit bdb4a9f

Browse files
committed
Remove the event-stream dependency
1 parent 4288f9d commit bdb4a9f

3 files changed

Lines changed: 8 additions & 146 deletions

File tree

package-lock.json

Lines changed: 1 addition & 102 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,15 @@
7272
"eslint-plugin-jest": "^26.1.3",
7373
"eslint-plugin-jest-formatting": "^3.1.0",
7474
"eslint-plugin-simple-import-sort": "^7.0.0",
75-
"markdownlint-cli2": "^0.5.1",
7675
"jest": "^27.5.1",
76+
"markdownlint-cli2": "^0.5.1",
7777
"prettier": "^2.6.2",
7878
"ts-jest": "^27.1.4",
7979
"type-fest": "^2.12.2",
8080
"typescript": "^4.6.3"
8181
},
8282
"dependencies": {
8383
"chalk": "^4.1.2",
84-
"event-stream": "4.0.1",
8584
"form-data": "4.0.0",
8685
"har-schema": "^2.0.0",
8786
"stringify-object": "3.3.0",

src/httpsnippet.ts

Lines changed: 6 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { map as eventStreamMap } from 'event-stream';
21
import FormData from 'form-data';
32
import { Param, PostDataCommon, Request as NpmHarRequest } from 'har-format';
43
import { stringify as queryStringify } from 'querystring';
@@ -176,61 +175,26 @@ export class HTTPSnippet {
176175
if (request.postData?.params) {
177176
const form = new FormData();
178177

179-
// The `form-data` module returns one of two things: a native FormData object, or its own polyfill
180-
// Since the polyfill does not support the full API of the native FormData object, when this library is running in a browser environment it'll fail on two things:
181-
//
182-
// 1. The API for `form.append()` has three arguments and the third should only be present when the second is a
183-
// Blob or USVString.
184-
// 1. `FormData.pipe()` isn't a function.
185-
//
186-
// Since the native FormData object is iterable, we easily detect what version of `form-data` we're working with here to allow `multipart/form-data` requests to be compiled under both browser and Node environments.
187-
//
188-
// This hack is pretty awful but it's the only way we can use this library in the browser as if we code this against just the native FormData object, we can't polyfill that back into Node because Blob and File objects, which something like `formdata-polyfill` requires, don't exist there.
189-
// @ts-expect-error TODO
190-
const isNativeFormData = typeof form[Symbol.iterator] === 'function';
191-
192178
// TODO: THIS ABSOLUTELY MUST BE REMOVED.
193179
// IT BREAKS SOME USE-CASES FOR MULTIPART FORMS THAT DEPEND ON BEING ABLE TO SET THE BOUNDARY.
194180
// easter egg
195181
const boundary = '---011000010111000001101001'; // this is binary for "api". yep.
196-
if (!isNativeFormData) {
197-
// @ts-expect-error THIS IS WRONG. VERY WRONG.
198-
form._boundary = boundary;
199-
}
200182

201183
request.postData?.params.forEach(param => {
202184
const name = param.name;
203185
const value = param.value || '';
204186
const filename = param.fileName || null;
205187

206-
if (isNativeFormData) {
207-
if (isBlob(value)) {
208-
// @ts-expect-error TODO
209-
form.append(name, value, filename);
210-
} else {
211-
form.append(name, value);
212-
}
188+
if (isBlob(value)) {
189+
// @ts-expect-error TODO
190+
form.append(name, value, filename);
213191
} else {
214-
form.append(name, value, {
215-
// @ts-expect-error TODO
216-
filename,
217-
// @ts-expect-error TODO
218-
contentType: param.contentType || null,
219-
});
192+
form.append(name, value);
220193
}
221194
});
222195

223-
if (isNativeFormData) {
224-
for (const data of formDataIterator(form, boundary)) {
225-
request.postData.text += data;
226-
}
227-
} else {
228-
form.pipe(
229-
// @ts-expect-error TODO
230-
eventStreamMap(data => {
231-
request.postData.text += data;
232-
}),
233-
);
196+
for (const data of formDataIterator(form, boundary)) {
197+
request.postData.text += data;
234198
}
235199

236200
request.postData.boundary = boundary;

0 commit comments

Comments
 (0)