Skip to content

Commit 835c70b

Browse files
committedApr 22, 2020
Do not alter options.body
Fixes #1165
1 parent 8501c69 commit 835c70b

File tree

2 files changed

+16
-17
lines changed

2 files changed

+16
-17
lines changed
 

‎source/core/index.ts

+15-9
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ const kCancelTimeouts = Symbol('cancelTimeouts');
4141
const kStartedReading = Symbol('startedReading');
4242
const kStopReading = Symbol('stopReading');
4343
const kTriggerRead = Symbol('triggerRead');
44+
const kBody = Symbol('body');
4445
export const kIsNormalizedAlready = Symbol('isNormalizedAlready');
4546

4647
const supportsBrotli = is.string((process.versions as any).brotli);
@@ -452,6 +453,7 @@ export default class Request extends Duplex implements RequestEvents<Request> {
452453
[kUploadedSize]: number;
453454
[kStopReading]: boolean;
454455
[kTriggerRead]: boolean;
456+
[kBody]: Options['body'];
455457
[kBodySize]?: number;
456458
[kServerResponsesPiped]: Set<ServerResponse>;
457459
[kIsFromCache]?: boolean;
@@ -889,21 +891,23 @@ export default class Request extends Duplex implements RequestEvents<Request> {
889891
if (isFormData(options.body) && noContentType) {
890892
headers['content-type'] = `multipart/form-data; boundary=${options.body.getBoundary()}`;
891893
}
894+
895+
this[kBody] = options.body;
892896
} else if (isForm) {
893897
if (noContentType) {
894898
headers['content-type'] = 'application/x-www-form-urlencoded';
895899
}
896900

897-
options.body = (new URLSearchParams(options.form as Record<string, string>)).toString();
901+
this[kBody] = (new URLSearchParams(options.form as Record<string, string>)).toString();
898902
} else {
899903
if (noContentType) {
900904
headers['content-type'] = 'application/json';
901905
}
902906

903-
options.body = JSON.stringify(options.json);
907+
this[kBody] = JSON.stringify(options.json);
904908
}
905909

906-
const uploadBodySize = await getBodySize(options);
910+
const uploadBodySize = await getBodySize(this[kBody], options.headers);
907911

908912
// See https://tools.ietf.org/html/rfc7230#section-3.3.2
909913
// A user agent SHOULD send a Content-Length in a request message when
@@ -1149,21 +1153,23 @@ export default class Request extends Duplex implements RequestEvents<Request> {
11491153
this.emit('uploadProgress', this.uploadProgress);
11501154

11511155
// Send body
1156+
const body = this[kBody];
11521157
const currentRequest = this.redirects.length === 0 ? this : request;
1153-
if (is.nodeStream(options.body)) {
1154-
options.body.pipe(currentRequest);
1155-
options.body.once('error', (error: NodeJS.ErrnoException) => {
1158+
1159+
if (is.nodeStream(body)) {
1160+
body.pipe(currentRequest);
1161+
body.once('error', (error: NodeJS.ErrnoException) => {
11561162
this._beforeError(new UploadError(error, this));
11571163
});
11581164

1159-
options.body.once('end', () => {
1165+
body.once('end', () => {
11601166
delete options.body;
11611167
});
11621168
} else {
11631169
this._unlockWrite();
11641170

1165-
if (!is.undefined(options.body)) {
1166-
this._writeRequest(options.body, null as unknown as string, () => {});
1171+
if (!is.undefined(body)) {
1172+
this._writeRequest(body, null as unknown as string, () => {});
11671173
currentRequest.end();
11681174

11691175
this._lockWrite();

‎source/core/utils/get-body-size.ts

+1-8
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,9 @@ import {ClientRequestArgs} from 'http';
44
import is from '@sindresorhus/is';
55
import isFormData from './is-form-data';
66

7-
interface Options {
8-
body?: unknown;
9-
headers: ClientRequestArgs['headers'];
10-
}
11-
127
const statAsync = promisify(stat);
138

14-
export default async (options: Options): Promise<number | undefined> => {
15-
const {body, headers} = options;
16-
9+
export default async (body: unknown, headers: ClientRequestArgs['headers']): Promise<number | undefined> => {
1710
if (headers && 'content-length' in headers) {
1811
return Number(headers['content-length']);
1912
}

0 commit comments

Comments
 (0)
Please sign in to comment.