mirror of
https://github.com/tenrok/axios.git
synced 2026-06-17 19:21:29 +03:00
73a7c55282
* Clean up error handling, fix a proto-pollution gap, and seal a few loose ends.
Been poking around the codebase and found a handful of things that needed tidying up:
- resolveConfig.js - config.params and config.paramsSerializer were being accessed directly off user input instead of going through the own() guard. If someone crafted a config with inherited params from the prototype, you'd get unexpected behavior. Swapped to own('params') / own('paramsSerializer') like the rest of the module does.
- http.js - there was a stray console.warn('emit error', err) in an abort-event catch block. Debug leftover, shouldn't reach production. Replaced with a quiet catch.
- AxiosHeaders.js - three places were throwing raw Error or TypeError instead of AxiosError. Swapped them over with ERR_BAD_OPTION_VALUE. Also added the missing AxiosError import (creates a circular dep with AxiosError.js which imports AxiosHeaders, but it works fine at runtime since the throws are inside method bodies, not at module eval time).
- toFormData.js - the circular reference detection was throwing a bare Error. Changed to AxiosError without a code, so it stays distinguishable from the depth-exceeded error that uses ERR_FORM_DATA_DEPTH_EXCEEDED. (There's a test that explicitly checks this distinction.)
- formDataToStream.js - two more raw throws (TypeError and Error) → AxiosError.
- buildURL.js - import self-path ../helpers/AxiosURLSearchParams.js when it lives in the same directory as the importer. Changed to ./AxiosURLSearchParams.js.
- index.d.cts - CanceledError was missing readonly name: 'CanceledError' that index.d.ts already has. Added it to keep the CJS declarations in sync.
Lint passes clean, all 770 unit tests green. Nothing breaking - all changes are either internal (no consumer-facing API change) or type-only.
* Update AxiosHeaders.js
* Update AxiosHeaders.js
* fix: revert breaking error-type changes per review feedback
Reverts AxiosError throws back to native Error/TypeError in AxiosHeaders,
formDataToStream, and toFormData to avoid breaking existing consumers
who catch by constructor name or check isAxiosError().
Adds regression tests for resolveConfig own('params')/own('paramsSerializer')
guard as requested in review.
Removes unused AxiosError imports from AxiosHeaders and formDataToStream.
* docs: add pre-release notes for config hardening
---------
Co-authored-by: Jason Saayman <jasonsaayman@gmail.com>
120 lines
3.0 KiB
JavaScript
120 lines
3.0 KiB
JavaScript
import util from 'util';
|
|
import { Readable } from 'stream';
|
|
import utils from '../utils.js';
|
|
import readBlob from './readBlob.js';
|
|
import platform from '../platform/index.js';
|
|
|
|
const BOUNDARY_ALPHABET = platform.ALPHABET.ALPHA_DIGIT + '-_';
|
|
|
|
const textEncoder = typeof TextEncoder === 'function' ? new TextEncoder() : new util.TextEncoder();
|
|
|
|
const CRLF = '\r\n';
|
|
const CRLF_BYTES = textEncoder.encode(CRLF);
|
|
const CRLF_BYTES_COUNT = 2;
|
|
|
|
class FormDataPart {
|
|
constructor(name, value) {
|
|
const { escapeName } = this.constructor;
|
|
const isStringValue = utils.isString(value);
|
|
|
|
let headers = `Content-Disposition: form-data; name="${escapeName(name)}"${
|
|
!isStringValue && value.name ? `; filename="${escapeName(value.name)}"` : ''
|
|
}${CRLF}`;
|
|
|
|
if (isStringValue) {
|
|
value = textEncoder.encode(String(value).replace(/\r?\n|\r\n?/g, CRLF));
|
|
} else {
|
|
const safeType = String(value.type || 'application/octet-stream').replace(/[\r\n]/g, '');
|
|
headers += `Content-Type: ${safeType}${CRLF}`;
|
|
}
|
|
|
|
this.headers = textEncoder.encode(headers + CRLF);
|
|
|
|
this.contentLength = isStringValue ? value.byteLength : value.size;
|
|
|
|
this.size = this.headers.byteLength + this.contentLength + CRLF_BYTES_COUNT;
|
|
|
|
this.name = name;
|
|
this.value = value;
|
|
}
|
|
|
|
async *encode() {
|
|
yield this.headers;
|
|
|
|
const { value } = this;
|
|
|
|
if (utils.isTypedArray(value)) {
|
|
yield value;
|
|
} else {
|
|
yield* readBlob(value);
|
|
}
|
|
|
|
yield CRLF_BYTES;
|
|
}
|
|
|
|
static escapeName(name) {
|
|
return String(name).replace(
|
|
/[\r\n"]/g,
|
|
(match) =>
|
|
({
|
|
'\r': '%0D',
|
|
'\n': '%0A',
|
|
'"': '%22',
|
|
})[match]
|
|
);
|
|
}
|
|
}
|
|
|
|
const formDataToStream = (form, headersHandler, options) => {
|
|
const {
|
|
tag = 'form-data-boundary',
|
|
size = 25,
|
|
boundary = tag + '-' + platform.generateString(size, BOUNDARY_ALPHABET),
|
|
} = options || {};
|
|
|
|
if (!utils.isFormData(form)) {
|
|
throw new TypeError('FormData instance required');
|
|
}
|
|
|
|
if (boundary.length < 1 || boundary.length > 70) {
|
|
throw new Error('boundary must be 1-70 characters long');
|
|
}
|
|
|
|
const boundaryBytes = textEncoder.encode('--' + boundary + CRLF);
|
|
const footerBytes = textEncoder.encode('--' + boundary + '--' + CRLF);
|
|
let contentLength = footerBytes.byteLength;
|
|
|
|
const parts = Array.from(form.entries()).map(([name, value]) => {
|
|
const part = new FormDataPart(name, value);
|
|
contentLength += part.size;
|
|
return part;
|
|
});
|
|
|
|
contentLength += boundaryBytes.byteLength * parts.length;
|
|
|
|
contentLength = utils.toFiniteNumber(contentLength);
|
|
|
|
const computedHeaders = {
|
|
'Content-Type': `multipart/form-data; boundary=${boundary}`,
|
|
};
|
|
|
|
if (Number.isFinite(contentLength)) {
|
|
computedHeaders['Content-Length'] = contentLength;
|
|
}
|
|
|
|
headersHandler && headersHandler(computedHeaders);
|
|
|
|
return Readable.from(
|
|
(async function* () {
|
|
for (const part of parts) {
|
|
yield boundaryBytes;
|
|
yield* part.encode();
|
|
}
|
|
|
|
yield footerBytes;
|
|
})()
|
|
);
|
|
};
|
|
|
|
export default formDataToStream;
|