mirror of
https://github.com/tenrok/axios.git
synced 2026-06-17 19:21:29 +03:00
5061879649
* fix: update fromDataURI regex to match RFC 2397 Update the DATA_URL_PATTERN regex to correctly match all valid RFC 2397 data URIs. The previous regex required a semicolon-terminated media type segment, which rejected valid data URIs like `data:;base64,MTIz` and `data:application/octet-stream,123`. Fixes #10808 * fix: normalize omitted mediatype to text/plain per RFC 2397 When a data URI has parameters but no mediatype (e.g. data:;charset=UTF-8,...), prepend text/plain as the default per RFC 2397 section 3. * fix: use stricter RFC 2397 regex and expand test matrix - Switch to type/subtype-aware regex from #10808 - Require name=value parameters, separate ;base64 group - Add tests: charset param, URL-encoded body, Blob type preservation, datax: rejection, missing comma rejection - Normalize omitted mediatype to text/plain per RFC 2397 §3 * chore: apply small nits --------- Co-authored-by: Abhishek Chauhan <abhishek-chauhan@outlook.com> Co-authored-by: Jay <jasonsaayman@gmail.com>
67 lines
1.8 KiB
JavaScript
67 lines
1.8 KiB
JavaScript
'use strict';
|
|
|
|
import AxiosError from '../core/AxiosError.js';
|
|
import parseProtocol from './parseProtocol.js';
|
|
import platform from '../platform/index.js';
|
|
|
|
// RFC 2397: data:[<mediatype>][;base64],<data>
|
|
// mediatype = type/subtype followed by optional ;name=value parameters
|
|
const DATA_URL_PATTERN = /^([^,;]+\/[^,;]+)?((?:;[^,;=]+=[^,;]+)*)(;base64)?,([\s\S]*)$/;
|
|
|
|
/**
|
|
* Parse data uri to a Buffer or Blob
|
|
*
|
|
* @param {String} uri
|
|
* @param {?Boolean} asBlob
|
|
* @param {?Object} options
|
|
* @param {?Function} options.Blob
|
|
*
|
|
* @returns {Buffer|Blob}
|
|
*/
|
|
export default function fromDataURI(uri, asBlob, options) {
|
|
const _Blob = (options && options.Blob) || platform.classes.Blob;
|
|
const protocol = parseProtocol(uri);
|
|
|
|
if (asBlob === undefined && _Blob) {
|
|
asBlob = true;
|
|
}
|
|
|
|
if (protocol === 'data') {
|
|
uri = protocol.length ? uri.slice(protocol.length + 1) : uri;
|
|
|
|
const match = DATA_URL_PATTERN.exec(uri);
|
|
|
|
if (!match) {
|
|
throw new AxiosError('Invalid URL', AxiosError.ERR_INVALID_URL);
|
|
}
|
|
|
|
const type = match[1];
|
|
const params = match[2];
|
|
const encoding = match[3] ? 'base64' : 'utf8';
|
|
const body = match[4];
|
|
|
|
// RFC 2397 section 3: default mediatype is text/plain;charset=US-ASCII
|
|
// Bare `data:,` leaves mime undefined; Blob normalises that to "" per spec.
|
|
let mime;
|
|
if (type) {
|
|
mime = params ? type + params : type;
|
|
} else if (params) {
|
|
mime = 'text/plain' + params;
|
|
}
|
|
|
|
const buffer = Buffer.from(decodeURIComponent(body), encoding);
|
|
|
|
if (asBlob) {
|
|
if (!_Blob) {
|
|
throw new AxiosError('Blob is not supported', AxiosError.ERR_NOT_SUPPORT);
|
|
}
|
|
|
|
return new _Blob([buffer], { type: mime });
|
|
}
|
|
|
|
return buffer;
|
|
}
|
|
|
|
throw new AxiosError('Unsupported protocol ' + protocol, AxiosError.ERR_NOT_SUPPORT);
|
|
}
|