Skip to content

Commit

Permalink
Remove automatic content-length on ReadStream (#1510)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
Giotino and sindresorhus authored Nov 20, 2020
1 parent 3f707e6 commit 472b8ef
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 20 deletions.
4 changes: 3 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,9 @@ Type: `string | Buffer | stream.Readable` or [`form-data` instance](https://gith

**Note #4:** This option is not enumerable and will not be merged with the instance defaults.

The `content-length` header will be automatically set if `body` is a `string` / `Buffer` / `fs.createReadStream` instance / [`form-data` instance](https://github.com/form-data/form-data), and `content-length` and `transfer-encoding` are not manually set in `options.headers`.
The `content-length` header will be automatically set if `body` is a `string` / `Buffer` / [`form-data` instance](https://github.com/form-data/form-data), and `content-length` and `transfer-encoding` are not manually set in `options.headers`.

Since Got 12, the `content-length` is not automatically set when `body` is a `fs.createReadStream`.

###### json

Expand Down
4 changes: 3 additions & 1 deletion source/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,9 @@ interface PlainOptions extends URLOptions {
__Note #4__: This option is not enumerable and will not be merged with the instance defaults.
The `content-length` header will be automatically set if `body` is a `string` / `Buffer` / `fs.createReadStream` instance / [`form-data` instance](https://github.com/form-data/form-data), and `content-length` and `transfer-encoding` are not manually set in `options.headers`.
The `content-length` header will be automatically set if `body` is a `string` / `Buffer` / [`form-data` instance](https://github.com/form-data/form-data), and `content-length` and `transfer-encoding` are not manually set in `options.headers`.
Since Got 12, the `content-length` is not automatically set when `body` is a `fs.createReadStream`.
*/
body?: string | Buffer | Readable;

Expand Down
13 changes: 0 additions & 13 deletions source/core/utils/get-body-size.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import {ReadStream, stat} from 'fs';
import {promisify} from 'util';
import {ClientRequestArgs} from 'http';
import is from '@sindresorhus/is';
import isFormData from './is-form-data';

const statAsync = promisify(stat);

export default async (body: unknown, headers: ClientRequestArgs['headers']): Promise<number | undefined> => {
if (headers && 'content-length' in headers) {
return Number(headers['content-length']);
Expand All @@ -27,15 +24,5 @@ export default async (body: unknown, headers: ClientRequestArgs['headers']): Pro
return promisify(body.getLength.bind(body))();
}

if (body instanceof ReadStream) {
const {size} = await statAsync(body.path);

if (size === 0) {
return undefined;
}

return size;
}

return undefined;
};
6 changes: 2 additions & 4 deletions test/headers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import fs = require('fs');
import {promisify} from 'util';
import path = require('path');
import test from 'ava';
import {Handler} from 'express';
Expand Down Expand Up @@ -174,16 +173,15 @@ test('form-data sets `content-length` header', withServer, async (t, server, got
t.is(headers['content-length'], '157');
});

test('stream as `options.body` sets `content-length` header', withServer, async (t, server, got) => {
test('stream as `options.body` does not set `content-length` header', withServer, async (t, server, got) => {
server.post('/', echoHeaders);

const fixture = path.resolve('test/fixtures/stream-content-length');
const {size} = await promisify(fs.stat)(fixture);
const {body} = await got.post({
body: fs.createReadStream(fixture)
});
const headers = JSON.parse(body);
t.is(Number(headers['content-length']), size);
t.is(headers['content-length'], undefined);
});

test('buffer as `options.body` sets `content-length` header', withServer, async (t, server, got) => {
Expand Down
9 changes: 8 additions & 1 deletion test/progress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,16 @@ test('upload progress - file stream', withServer, async (t, server, got) => {
const path = tempy.file();
fs.writeFileSync(path, file);

const {size} = await promisify(fs.stat)(path);

const events: Progress[] = [];

await got.post({body: fs.createReadStream(path)})
await got.post({
body: fs.createReadStream(path),
headers: {
'content-length': size.toString()
}
})
.on('uploadProgress', (event: Progress) => events.push(event));

checkEvents(t, events, file.length);
Expand Down

0 comments on commit 472b8ef

Please sign in to comment.