diff --git a/readme.md b/readme.md index c97c4d422..856689986 100644 --- a/readme.md +++ b/readme.md @@ -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 diff --git a/source/core/index.ts b/source/core/index.ts index ae1015fa6..3557daec8 100644 --- a/source/core/index.ts +++ b/source/core/index.ts @@ -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; diff --git a/source/core/utils/get-body-size.ts b/source/core/utils/get-body-size.ts index d7cf8f0c6..88453daa6 100644 --- a/source/core/utils/get-body-size.ts +++ b/source/core/utils/get-body-size.ts @@ -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 => { if (headers && 'content-length' in headers) { return Number(headers['content-length']); @@ -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; }; diff --git a/test/headers.ts b/test/headers.ts index 20107a6fa..e7830de45 100644 --- a/test/headers.ts +++ b/test/headers.ts @@ -1,5 +1,4 @@ import fs = require('fs'); -import {promisify} from 'util'; import path = require('path'); import test from 'ava'; import {Handler} from 'express'; @@ -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) => { diff --git a/test/progress.ts b/test/progress.ts index 39b3bb954..67f5fa9bf 100644 --- a/test/progress.ts +++ b/test/progress.ts @@ -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);