Skip to content

Commit

Permalink
Unify calculating content-length (#544)
Browse files Browse the repository at this point in the history
  • Loading branch information
szmarczak authored and sindresorhus committed Aug 3, 2018
1 parent 5c3adba commit ba0cb0d
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 9 deletions.
4 changes: 4 additions & 0 deletions source/get-body-size.js
Expand Up @@ -19,6 +19,10 @@ module.exports = async options => {
return Buffer.byteLength(body);
}

if (is.buffer(body)) {
return body.length;
}

if (isFormData(body)) {
return util.promisify(body.getLength.bind(body))();
}
Expand Down
5 changes: 0 additions & 5 deletions source/normalize-arguments.js
Expand Up @@ -108,11 +108,6 @@ module.exports = (url, options, defaults) => {
options.body = JSON.stringify(body);
}

if (is.undefined(headers['content-length']) && is.undefined(headers['transfer-encoding']) && !is.nodeStream(body)) {
const length = is.string(options.body) ? Buffer.byteLength(options.body) : options.body.length;
headers['content-length'] = length;
}

// Convert buffer to stream to receive upload progress events (#322)
if (is.buffer(body)) {
options.body = toReadableStream(body);
Expand Down
4 changes: 0 additions & 4 deletions source/request-as-event-emitter.js
Expand Up @@ -162,10 +162,6 @@ module.exports = options => {
try {
uploadBodySize = await getBodySize(options);

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

0 comments on commit ba0cb0d

Please sign in to comment.