From afe0c37184fd752f3808af6fba45f5f4acbe1c5c Mon Sep 17 00:00:00 2001 From: Giovanni Minotti Date: Wed, 21 Oct 2020 19:10:32 +0200 Subject: [PATCH 1/7] Remove automatic `content-length` on ReadStream --- readme.md | 2 +- source/core/utils/get-body-size.ts | 10 ---------- 2 files changed, 1 insertion(+), 11 deletions(-) diff --git a/readme.md b/readme.md index a09c2af41..45ae23c6e 100644 --- a/readme.md +++ b/readme.md @@ -236,7 +236,7 @@ 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`. ###### json diff --git a/source/core/utils/get-body-size.ts b/source/core/utils/get-body-size.ts index d7cf8f0c6..8a0939cc5 100644 --- a/source/core/utils/get-body-size.ts +++ b/source/core/utils/get-body-size.ts @@ -27,15 +27,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; }; From f2da0ee80c491bbca3595206155fd63449faa0c7 Mon Sep 17 00:00:00 2001 From: Giovanni Minotti Date: Wed, 21 Oct 2020 19:17:06 +0200 Subject: [PATCH 2/7] Tests --- source/core/utils/get-body-size.ts | 3 --- test/headers.ts | 6 ++---- test/progress.ts | 9 ++++++++- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/source/core/utils/get-body-size.ts b/source/core/utils/get-body-size.ts index 8a0939cc5..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']); 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); From eef4f9e067a9ffc1f69e5b370941bedc9b11b7f8 Mon Sep 17 00:00:00 2001 From: Giovanni Minotti Date: Fri, 20 Nov 2020 10:24:37 +0100 Subject: [PATCH 3/7] Added small note --- readme.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/readme.md b/readme.md index 5e6f5c56e..fcdfdb03f 100644 --- a/readme.md +++ b/readme.md @@ -238,6 +238,8 @@ Type: `string | Buffer | stream.Readable` or [`form-data` instance](https://gith 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`. +The `content-length` is no more automatically set when `body` is a `fs.createReadStream`. + ###### json Type: `object | Array | number | string | boolean | null` *(JSON-serializable values)* From 959595422c52c550933681b300c52d25b1ac6b91 Mon Sep 17 00:00:00 2001 From: Giovanni Minotti Date: Fri, 20 Nov 2020 12:15:55 +0100 Subject: [PATCH 4/7] Updated doc comments --- source/core/index.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/source/core/index.ts b/source/core/index.ts index ae1015fa6..d483cee1b 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`. + + The `content-length` is no more automatically set when `body` is a `fs.createReadStream`. */ body?: string | Buffer | Readable; From 8b63415b08c9e73412e01502fe4dd2435240838f Mon Sep 17 00:00:00 2001 From: Giovanni Minotti Date: Fri, 20 Nov 2020 11:54:49 +0000 Subject: [PATCH 5/7] Added "Since Got 12" --- readme.md | 2 +- source/core/index.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/readme.md b/readme.md index fcdfdb03f..3b8e2a957 100644 --- a/readme.md +++ b/readme.md @@ -238,7 +238,7 @@ Type: `string | Buffer | stream.Readable` or [`form-data` instance](https://gith 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`. -The `content-length` is no more automatically set when `body` is a `fs.createReadStream`. +Since Got 12 the `content-length` is no more automatically set when `body` is a `fs.createReadStream`. ###### json diff --git a/source/core/index.ts b/source/core/index.ts index d483cee1b..6ebf7ce0c 100644 --- a/source/core/index.ts +++ b/source/core/index.ts @@ -425,7 +425,7 @@ interface PlainOptions extends URLOptions { 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`. - The `content-length` is no more automatically set when `body` is a `fs.createReadStream`. + Since Got 12 the `content-length` is no more automatically set when `body` is a `fs.createReadStream`. */ body?: string | Buffer | Readable; From 04802ee0c3f7fce0884a136c0454efbc76904fc7 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Fri, 20 Nov 2020 22:22:45 +0700 Subject: [PATCH 6/7] Update readme.md --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 3b8e2a957..856689986 100644 --- a/readme.md +++ b/readme.md @@ -238,7 +238,7 @@ Type: `string | Buffer | stream.Readable` or [`form-data` instance](https://gith 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`. +Since Got 12, the `content-length` is not automatically set when `body` is a `fs.createReadStream`. ###### json From 409e807c3512937ce4ae8026e0a3f40616f49754 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Fri, 20 Nov 2020 22:23:13 +0700 Subject: [PATCH 7/7] Update index.ts --- source/core/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/core/index.ts b/source/core/index.ts index 6ebf7ce0c..3557daec8 100644 --- a/source/core/index.ts +++ b/source/core/index.ts @@ -425,7 +425,7 @@ interface PlainOptions extends URLOptions { 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`. + Since Got 12, the `content-length` is not automatically set when `body` is a `fs.createReadStream`. */ body?: string | Buffer | Readable;