mirror of
https://github.com/tenrok/axios.git
synced 2026-06-20 20:00:40 +03:00
fix(formdata): add hotfix to use the asynchronous API to compute the content-length header value; (#5521)
This commit is contained in:
+12
-5
@@ -7,6 +7,7 @@ import buildURL from './../helpers/buildURL.js';
|
|||||||
import {getProxyForUrl} from 'proxy-from-env';
|
import {getProxyForUrl} from 'proxy-from-env';
|
||||||
import http from 'http';
|
import http from 'http';
|
||||||
import https from 'https';
|
import https from 'https';
|
||||||
|
import util from 'util';
|
||||||
import followRedirects from 'follow-redirects';
|
import followRedirects from 'follow-redirects';
|
||||||
import zlib from 'zlib';
|
import zlib from 'zlib';
|
||||||
import {VERSION} from '../env/data.js';
|
import {VERSION} from '../env/data.js';
|
||||||
@@ -117,7 +118,8 @@ const isHttpAdapterSupported = typeof process !== 'undefined' && utils.kindOf(pr
|
|||||||
|
|
||||||
/*eslint consistent-return:0*/
|
/*eslint consistent-return:0*/
|
||||||
export default isHttpAdapterSupported && function httpAdapter(config) {
|
export default isHttpAdapterSupported && function httpAdapter(config) {
|
||||||
return new Promise(function dispatchHttpRequest(resolvePromise, rejectPromise) {
|
/*eslint no-async-promise-executor:0*/
|
||||||
|
return new Promise(async function dispatchHttpRequest(resolvePromise, rejectPromise) {
|
||||||
let data = config.data;
|
let data = config.data;
|
||||||
const responseType = config.responseType;
|
const responseType = config.responseType;
|
||||||
const responseEncoding = config.responseEncoding;
|
const responseEncoding = config.responseEncoding;
|
||||||
@@ -208,7 +210,7 @@ export default isHttpAdapterSupported && function httpAdapter(config) {
|
|||||||
convertedData = convertedData.toString(responseEncoding);
|
convertedData = convertedData.toString(responseEncoding);
|
||||||
|
|
||||||
if (!responseEncoding || responseEncoding === 'utf8') {
|
if (!responseEncoding || responseEncoding === 'utf8') {
|
||||||
data = utils.stripBOM(convertedData);
|
convertedData = utils.stripBOM(convertedData);
|
||||||
}
|
}
|
||||||
} else if (responseType === 'stream') {
|
} else if (responseType === 'stream') {
|
||||||
convertedData = stream.Readable.from(convertedData);
|
convertedData = stream.Readable.from(convertedData);
|
||||||
@@ -258,9 +260,14 @@ export default isHttpAdapterSupported && function httpAdapter(config) {
|
|||||||
// support for https://www.npmjs.com/package/form-data api
|
// support for https://www.npmjs.com/package/form-data api
|
||||||
} else if (utils.isFormData(data) && utils.isFunction(data.getHeaders)) {
|
} else if (utils.isFormData(data) && utils.isFunction(data.getHeaders)) {
|
||||||
headers.set(data.getHeaders());
|
headers.set(data.getHeaders());
|
||||||
if (utils.isFunction(data.getLengthSync)) { // check if the undocumented API exists
|
|
||||||
const knownLength = data.getLengthSync();
|
if (!headers.hasContentLength()) {
|
||||||
!utils.isUndefined(knownLength) && headers.setContentLength(knownLength, false);
|
try {
|
||||||
|
const knownLength = await util.promisify(data.getLength).call(data);
|
||||||
|
headers.setContentLength(knownLength);
|
||||||
|
/*eslint no-empty:0*/
|
||||||
|
} catch (e) {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else if (utils.isBlob(data)) {
|
} else if (utils.isBlob(data)) {
|
||||||
data.size && headers.setContentType(data.type || 'application/octet-stream');
|
data.size && headers.setContentType(data.type || 'application/octet-stream');
|
||||||
|
|||||||
@@ -141,7 +141,7 @@ class AxiosHeaders {
|
|||||||
if (header) {
|
if (header) {
|
||||||
const key = utils.findKey(this, header);
|
const key = utils.findKey(this, header);
|
||||||
|
|
||||||
return !!(key && (!matcher || matchHeaderValue(this, this[key], key, matcher)));
|
return !!(key && this[key] !== undefined && (!matcher || matchHeaderValue(this, this[key], key, matcher)));
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
+1
-1
@@ -204,4 +204,4 @@
|
|||||||
"@commitlint/config-conventional"
|
"@commitlint/config-conventional"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Binary file not shown.
|
After Width: | Height: | Size: 1.7 KiB |
@@ -1576,6 +1576,10 @@ describe('supports http with nodejs', function () {
|
|||||||
it('should allow passing FormData', function (done) {
|
it('should allow passing FormData', function (done) {
|
||||||
var form = new FormDataLegacy();
|
var form = new FormDataLegacy();
|
||||||
var file1 = Buffer.from('foo', 'utf8');
|
var file1 = Buffer.from('foo', 'utf8');
|
||||||
|
const image = path.resolve(__dirname, './axios.png');
|
||||||
|
const fileStream = fs.createReadStream(image);
|
||||||
|
|
||||||
|
const stat = fs.statSync(image);
|
||||||
|
|
||||||
form.append('foo', "bar");
|
form.append('foo', "bar");
|
||||||
form.append('file1', file1, {
|
form.append('file1', file1, {
|
||||||
@@ -1584,9 +1588,13 @@ describe('supports http with nodejs', function () {
|
|||||||
contentType: 'image/jpeg'
|
contentType: 'image/jpeg'
|
||||||
});
|
});
|
||||||
|
|
||||||
|
form.append('fileStream', fileStream);
|
||||||
|
|
||||||
server = http.createServer(function (req, res) {
|
server = http.createServer(function (req, res) {
|
||||||
var receivedForm = new formidable.IncomingForm();
|
var receivedForm = new formidable.IncomingForm();
|
||||||
|
|
||||||
|
assert.ok(req.rawHeaders.find(header => header.toLowerCase() === 'content-length'));
|
||||||
|
|
||||||
receivedForm.parse(req, function (err, fields, files) {
|
receivedForm.parse(req, function (err, fields, files) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return done(err);
|
return done(err);
|
||||||
@@ -1609,6 +1617,10 @@ describe('supports http with nodejs', function () {
|
|||||||
assert.strictEqual(res.data.files.file1.originalFilename, 'temp/bar.jpg');
|
assert.strictEqual(res.data.files.file1.originalFilename, 'temp/bar.jpg');
|
||||||
assert.strictEqual(res.data.files.file1.size, 3);
|
assert.strictEqual(res.data.files.file1.size, 3);
|
||||||
|
|
||||||
|
assert.strictEqual(res.data.files.fileStream.mimetype, 'image/png');
|
||||||
|
assert.strictEqual(res.data.files.fileStream.originalFilename, 'axios.png');
|
||||||
|
assert.strictEqual(res.data.files.fileStream.size, stat.size);
|
||||||
|
|
||||||
done();
|
done();
|
||||||
}).catch(done);
|
}).catch(done);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user