mirror of
https://github.com/axios/axios.git
synced 2026-04-12 02:31:57 +08:00
* Fixed bug #4727; Added node 18.x to the CI; Added hotfix for `ERR_OSSL_EVP_UNSUPPORTED` issue with karma running on node >=17.x; Added `cross-env` to allow running build and test scripts on Windows platforms; * Added conditional setting of `--openssl-legacy-provider` option for node versions >=17.x; * Refactored ssl-hotfix & test script; * Fixed and refactored default max body length test due to ECONNRESET failure; * Added test for converting the data uri to a Blob; Fixed bug with parsing mime type for Blob; Co-authored-by: Jay <jasonsaayman@gmail.com>
52 lines
1.3 KiB
JavaScript
52 lines
1.3 KiB
JavaScript
'use strict';
|
|
|
|
var AxiosError = require('../core/AxiosError');
|
|
var parseProtocol = require('./parseProtocol');
|
|
var platform = require('../platform');
|
|
|
|
var 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}
|
|
*/
|
|
module.exports = function fromDataURI(uri, asBlob, options) {
|
|
var _Blob = options && options.Blob || platform.classes.Blob;
|
|
var protocol = parseProtocol(uri);
|
|
|
|
if (asBlob === undefined && _Blob) {
|
|
asBlob = true;
|
|
}
|
|
|
|
if (protocol === 'data') {
|
|
uri = protocol.length ? uri.slice(protocol.length + 1) : uri;
|
|
|
|
var match = DATA_URL_PATTERN.exec(uri);
|
|
|
|
if (!match) {
|
|
throw new AxiosError('Invalid URL', AxiosError.ERR_INVALID_URL);
|
|
}
|
|
|
|
var mime = match[1];
|
|
var isBase64 = match[2];
|
|
var body = match[3];
|
|
var buffer = Buffer.from(decodeURIComponent(body), isBase64 ? 'base64' : 'utf8');
|
|
|
|
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);
|
|
};
|