Skip to content

Commit ba0cb0d

Browse files
szmarczaksindresorhus
authored andcommittedAug 3, 2018
Unify calculating content-length (#544)
1 parent 5c3adba commit ba0cb0d

File tree

3 files changed

+4
-9
lines changed

3 files changed

+4
-9
lines changed
 

‎source/get-body-size.js

+4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ module.exports = async options => {
1919
return Buffer.byteLength(body);
2020
}
2121

22+
if (is.buffer(body)) {
23+
return body.length;
24+
}
25+
2226
if (isFormData(body)) {
2327
return util.promisify(body.getLength.bind(body))();
2428
}

‎source/normalize-arguments.js

-5
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,6 @@ module.exports = (url, options, defaults) => {
108108
options.body = JSON.stringify(body);
109109
}
110110

111-
if (is.undefined(headers['content-length']) && is.undefined(headers['transfer-encoding']) && !is.nodeStream(body)) {
112-
const length = is.string(options.body) ? Buffer.byteLength(options.body) : options.body.length;
113-
headers['content-length'] = length;
114-
}
115-
116111
// Convert buffer to stream to receive upload progress events (#322)
117112
if (is.buffer(body)) {
118113
options.body = toReadableStream(body);

‎source/request-as-event-emitter.js

-4
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,6 @@ module.exports = options => {
162162
try {
163163
uploadBodySize = await getBodySize(options);
164164

165-
// This is the second try at setting a `content-length` header.
166-
// This supports getting the size async, in contrast to
167-
// https://github.com/sindresorhus/got/blob/82763c8089596dcee5eaa7f57f5dbf8194842fe6/index.js#L579-L582
168-
// TODO: We should unify these two at some point
169165
if (
170166
uploadBodySize > 0 &&
171167
is.undefined(options.headers['content-length']) &&

0 commit comments

Comments
 (0)
Please sign in to comment.