@@ -15,55 +15,59 @@ export interface IBufferConversions {
1515 *
1616 * @param {String } uri Data URI to turn into a Buffer instance
1717 */
18- export const makeDataUriToBuffer = ( convert : IBufferConversions ) => ( uri : string | URL ) : ParsedDataURI => {
19- uri = String ( uri ) ;
18+ export const makeDataUriToBuffer =
19+ ( convert : IBufferConversions ) =>
20+ ( uri : string | URL ) : ParsedDataURI => {
21+ uri = String ( uri ) ;
2022
21- if ( ! / ^ d a t a : / i. test ( uri ) ) {
22- throw new TypeError (
23- '`uri` does not appear to be a Data URI (must begin with "data:")'
24- ) ;
25- }
23+ if ( ! / ^ d a t a : / i. test ( uri ) ) {
24+ throw new TypeError (
25+ '`uri` does not appear to be a Data URI (must begin with "data:")'
26+ ) ;
27+ }
2628
27- // strip newlines
28- uri = uri . replace ( / \r ? \n / g, '' ) ;
29+ // strip newlines
30+ uri = uri . replace ( / \r ? \n / g, '' ) ;
2931
30- // split the URI up into the "metadata" and the "data" portions
31- const firstComma = uri . indexOf ( ',' ) ;
32- if ( firstComma === - 1 || firstComma <= 4 ) {
33- throw new TypeError ( 'malformed data: URI' ) ;
34- }
32+ // split the URI up into the "metadata" and the "data" portions
33+ const firstComma = uri . indexOf ( ',' ) ;
34+ if ( firstComma === - 1 || firstComma <= 4 ) {
35+ throw new TypeError ( 'malformed data: URI' ) ;
36+ }
3537
36- // remove the "data:" scheme and parse the metadata
37- const meta = uri . substring ( 5 , firstComma ) . split ( ';' ) ;
38+ // remove the "data:" scheme and parse the metadata
39+ const meta = uri . substring ( 5 , firstComma ) . split ( ';' ) ;
3840
39- let charset = '' ;
40- let base64 = false ;
41- const type = meta [ 0 ] || 'text/plain' ;
42- let typeFull = type ;
43- for ( let i = 1 ; i < meta . length ; i ++ ) {
44- if ( meta [ i ] === 'base64' ) {
45- base64 = true ;
46- } else if ( meta [ i ] ) {
47- typeFull += `;${ meta [ i ] } ` ;
48- if ( meta [ i ] . indexOf ( 'charset=' ) === 0 ) {
49- charset = meta [ i ] . substring ( 8 ) ;
41+ let charset = '' ;
42+ let base64 = false ;
43+ const type = meta [ 0 ] || 'text/plain' ;
44+ let typeFull = type ;
45+ for ( let i = 1 ; i < meta . length ; i ++ ) {
46+ if ( meta [ i ] === 'base64' ) {
47+ base64 = true ;
48+ } else if ( meta [ i ] ) {
49+ typeFull += `;${ meta [ i ] } ` ;
50+ if ( meta [ i ] . indexOf ( 'charset=' ) === 0 ) {
51+ charset = meta [ i ] . substring ( 8 ) ;
52+ }
5053 }
5154 }
52- }
53- // defaults to US-ASCII only if type is not provided
54- if ( ! meta [ 0 ] && ! charset . length ) {
55- typeFull += ';charset=US-ASCII' ;
56- charset = 'US-ASCII' ;
57- }
55+ // defaults to US-ASCII only if type is not provided
56+ if ( ! meta [ 0 ] && ! charset . length ) {
57+ typeFull += ';charset=US-ASCII' ;
58+ charset = 'US-ASCII' ;
59+ }
5860
59- // get the encoded data portion and decode URI-encoded chars
60- const data = unescape ( uri . substring ( firstComma + 1 ) ) ;
61- const buffer = base64 ? convert . base64ToArrayBuffer ( data ) : convert . stringToBuffer ( data ) ;
61+ // get the encoded data portion and decode URI-encoded chars
62+ const data = unescape ( uri . substring ( firstComma + 1 ) ) ;
63+ const buffer = base64
64+ ? convert . base64ToArrayBuffer ( data )
65+ : convert . stringToBuffer ( data ) ;
6266
63- return {
64- type,
65- typeFull,
66- charset,
67- buffer,
67+ return {
68+ type,
69+ typeFull,
70+ charset,
71+ buffer,
72+ } ;
6873 } ;
69- }
0 commit comments