MCPcopy
hub / github.com/axios/axios / fromDataURI

Function fromDataURI

lib/helpers/fromDataURI.js:21–68  ·  view source on GitHub ↗
(uri, asBlob, options)

Source from the content-addressed store, hash-verified

19 * @returns {Buffer|Blob}
20 */
21export default function fromDataURI(uri, asBlob, options) {
22 const _Blob = (options && options.Blob) || platform.classes.Blob;
23 const protocol = parseProtocol(uri);
24
25 if (asBlob === undefined && _Blob) {
26 asBlob = true;
27 }
28
29 if (protocol === 'data') {
30 uri = protocol.length ? uri.slice(protocol.length + 1) : uri;
31
32 const match = DATA_URL_PATTERN.exec(uri);
33
34 if (!match) {
35 throw new AxiosError('Invalid URL', AxiosError.ERR_INVALID_URL);
36 }
37
38 const type = match[1];
39 const params = match[2];
40 const encoding = match[3] ? 'base64' : 'utf8';
41 const body = match[4];
42
43 // RFC 2397 section 3: default mediatype is text/plain;charset=US-ASCII
44 // Bare `data:,` leaves mime undefined; Blob normalises that to "" per spec.
45 let mime = '';
46 if (type) {
47 mime = params ? type + params : type;
48 } else if (params) {
49 mime = 'text/plain' + params;
50 }
51
52 const buffer = encoding === 'base64'
53 ? Buffer.from(body, 'base64')
54 : Buffer.from(decodeURIComponent(body), encoding);
55
56 if (asBlob) {
57 if (!_Blob) {
58 throw new AxiosError('Blob is not supported', AxiosError.ERR_NOT_SUPPORT);
59 }
60
61 return new _Blob([buffer], { type: mime });
62 }
63
64 return buffer;
65 }
66
67 throw new AxiosError('Unsupported protocol ' + protocol, AxiosError.ERR_NOT_SUPPORT);
68}

Callers 2

http.jsFile · 0.85

Calls 2

parseProtocolFunction · 0.85
fromMethod · 0.45

Tested by

no test coverage detected