Skip to content

Commit 472b8ef

Browse files
Giotinosindresorhus
andauthoredNov 20, 2020
Remove automatic content-length on ReadStream (#1510)
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
1 parent 3f707e6 commit 472b8ef

File tree

5 files changed

+16
-20
lines changed

5 files changed

+16
-20
lines changed
 

‎readme.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,9 @@ Type: `string | Buffer | stream.Readable` or [`form-data` instance](https://gith
236236

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

239-
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`.
239+
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`.
240+
241+
Since Got 12, the `content-length` is not automatically set when `body` is a `fs.createReadStream`.
240242

241243
###### json
242244

‎source/core/index.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,9 @@ interface PlainOptions extends URLOptions {
423423
424424
__Note #4__: This option is not enumerable and will not be merged with the instance defaults.
425425
426-
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`.
426+
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`.
427+
428+
Since Got 12, the `content-length` is not automatically set when `body` is a `fs.createReadStream`.
427429
*/
428430
body?: string | Buffer | Readable;
429431

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

-13
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
import {ReadStream, stat} from 'fs';
21
import {promisify} from 'util';
32
import {ClientRequestArgs} from 'http';
43
import is from '@sindresorhus/is';
54
import isFormData from './is-form-data';
65

7-
const statAsync = promisify(stat);
8-
96
export default async (body: unknown, headers: ClientRequestArgs['headers']): Promise<number | undefined> => {
107
if (headers && 'content-length' in headers) {
118
return Number(headers['content-length']);
@@ -27,15 +24,5 @@ export default async (body: unknown, headers: ClientRequestArgs['headers']): Pro
2724
return promisify(body.getLength.bind(body))();
2825
}
2926

30-
if (body instanceof ReadStream) {
31-
const {size} = await statAsync(body.path);
32-
33-
if (size === 0) {
34-
return undefined;
35-
}
36-
37-
return size;
38-
}
39-
4027
return undefined;
4128
};

‎test/headers.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import fs = require('fs');
2-
import {promisify} from 'util';
32
import path = require('path');
43
import test from 'ava';
54
import {Handler} from 'express';
@@ -174,16 +173,15 @@ test('form-data sets `content-length` header', withServer, async (t, server, got
174173
t.is(headers['content-length'], '157');
175174
});
176175

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

180179
const fixture = path.resolve('test/fixtures/stream-content-length');
181-
const {size} = await promisify(fs.stat)(fixture);
182180
const {body} = await got.post({
183181
body: fs.createReadStream(fixture)
184182
});
185183
const headers = JSON.parse(body);
186-
t.is(Number(headers['content-length']), size);
184+
t.is(headers['content-length'], undefined);
187185
});
188186

189187
test('buffer as `options.body` sets `content-length` header', withServer, async (t, server, got) => {

‎test/progress.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,16 @@ test('upload progress - file stream', withServer, async (t, server, got) => {
122122
const path = tempy.file();
123123
fs.writeFileSync(path, file);
124124

125+
const {size} = await promisify(fs.stat)(path);
126+
125127
const events: Progress[] = [];
126128

127-
await got.post({body: fs.createReadStream(path)})
129+
await got.post({
130+
body: fs.createReadStream(path),
131+
headers: {
132+
'content-length': size.toString()
133+
}
134+
})
128135
.on('uploadProgress', (event: Progress) => events.push(event));
129136

130137
checkEvents(t, events, file.length);

0 commit comments

Comments
 (0)
Please sign in to comment.