Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove automatic content-length on ReadStream #1510

Merged
merged 8 commits into from
Nov 20, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 no more 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 no more 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