This package helps you parse {\ttfamily data\+:} URLs \href{https://fetch.spec.whatwg.org/\#data-urls}{\texttt{ according to the WHATWG Fetch Standard}}\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{const parseDataURL = require("{}data-\/urls"{});}
\DoxyCodeLine{}
\DoxyCodeLine{const textExample = parseDataURL("{}data:,Hello\%2C\%20World!"{});}
\DoxyCodeLine{console.log(textExample.mimeType.toString()); // "{}text/plain;charset=US-\/ASCII"{}}
\DoxyCodeLine{console.log(textExample.body.toString());     // "{}Hello, World!"{}}
\DoxyCodeLine{}
\DoxyCodeLine{const htmlExample = dataURL("{}data:text/html,\%3Ch1\%3EHello\%2C\%20World!\%3C\%2Fh1\%3E"{});}
\DoxyCodeLine{console.log(htmlExample.mimeType.toString()); // "{}text/html"{}}
\DoxyCodeLine{console.log(htmlExample.body.toString());     // <h1>Hello, World!</h1>}
\DoxyCodeLine{}
\DoxyCodeLine{const pngExample = parseDataURL("{}data:image/png;base64,iVBORw0KGgoAAA"{} +}
\DoxyCodeLine{                                "{}ANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4"{} +}
\DoxyCodeLine{                                "{}//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU"{} +}
\DoxyCodeLine{                                "{}5ErkJggg=="{});}
\DoxyCodeLine{console.log(pngExample.mimeType.toString()); // "{}image/png"{}}
\DoxyCodeLine{console.log(pngExample.body);                // <Buffer 89 50 4e 47 0d ... > }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_data_urls_README_autotoc_md7835}{}\doxysection{API}\label{md_sfcode_node_modules_data_urls_README_autotoc_md7835}
This package\textquotesingle{}s main module\textquotesingle{}s default export is a function that accepts a string and returns a {\ttfamily \{ mime\+Type, body \}} object, or {\ttfamily null} if the result cannot be parsed as a {\ttfamily data\+:} URL.


\begin{DoxyItemize}
\item The {\ttfamily mime\+Type} property is an instance of \href{https://www.npmjs.com/package/whatwg-mimetype}{\texttt{ whatwg-\/mimetype}}\textquotesingle{}s {\ttfamily MIMEType} class.
\item The {\ttfamily body} property is a Node.\+js \href{https://nodejs.org/docs/latest/api/buffer.html}{\texttt{ {\ttfamily Buffer}}} instance.
\end{DoxyItemize}

As shown in the examples above, both of these have useful {\ttfamily to\+String()} methods for manipulating them as string values. However…\hypertarget{md_sfcode_node_modules_data_urls_README_autotoc_md7836}{}\doxysubsection{A word of caution on string decoding}\label{md_sfcode_node_modules_data_urls_README_autotoc_md7836}
Because Node.\+js\textquotesingle{}s {\ttfamily Buffer.\+prototype.\+to\+String()} assumes a UTF-\/8 encoding, simply doing {\ttfamily data\+URL.\+body.\+to\+String()} may not work correctly if the {\ttfamily data\+:} URL\textquotesingle{}s contents were not originally written in UTF-\/8. This includes if the encoding is \char`\"{}\+US-\/\+ASCII\char`\"{}, \href{https://encoding.spec.whatwg.org/\#names-and-labels}{\texttt{ aka windows-\/1252}}, which is notable for being the default in many cases.

A more complete decoding example would use the \href{https://www.npmjs.com/package/whatwg-encoding}{\texttt{ whatwg-\/encoding}} package as follows\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{const parseDataURL = require("{}data-\/urls"{});}
\DoxyCodeLine{const \{ labelToName, decode \} = require("{}whatwg-\/encoding"{});}
\DoxyCodeLine{}
\DoxyCodeLine{const dataURL = parseDataURL(arbitraryString);}
\DoxyCodeLine{const encodingName = labelToName(dataURL.mimeType.parameters.get("{}charset"{}));}
\DoxyCodeLine{const bodyDecoded = decode(dataURL.body, encodingName); }

\end{DoxyCode}


For example, given an {\ttfamily arbitrary\+String} of {\ttfamily data\+:,Hello!}, this will produce a {\ttfamily body\+Decoded} of {\ttfamily \char`\"{}\+Hello!\char`\"{}}, as expected. But given an {\ttfamily arbitrary\+String} of {\ttfamily \char`\"{}data\+:,\+Héllo!\char`\"{}}, this will correctly produce a {\ttfamily body\+Decoded} of {\ttfamily \char`\"{}\+Héllo!\char`\"{}}, whereas just doing {\ttfamily data\+URL.\+body.\+to\+String()} will give back {\ttfamily \char`\"{}\+HÃ©llo!\char`\"{}}.

In summary, only use {\ttfamily data\+URL.\+body.\+to\+String()} when you are very certain your data is inside the ASCII range (i.\+e. code points within the range U+0000 to U+007F).\hypertarget{md_sfcode_node_modules_data_urls_README_autotoc_md7837}{}\doxysubsection{Advanced functionality\+: parsing from a URL record}\label{md_sfcode_node_modules_data_urls_README_autotoc_md7837}
If you are using the \href{https://github.com/jsdom/whatwg-url}{\texttt{ whatwg-\/url}} package, you may already have a \char`\"{}\+URL record\char`\"{} object on hand, as produced by that package\textquotesingle{}s {\ttfamily parse\+URL} export. In that case, you can use this package\textquotesingle{}s {\ttfamily from\+URLRecord} export to save a bit of work\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{const \{ parseURL \} = require("{}whatwg-\/url"{});}
\DoxyCodeLine{const dataURLFromURLRecord = require("{}data-\/urls"{}).fromURLRecord;}
\DoxyCodeLine{}
\DoxyCodeLine{const urlRecord = parseURL("{}data:,Hello\%2C\%20World!"{});}
\DoxyCodeLine{const dataURL = dataURLFromURLRecord(urlRecord); }

\end{DoxyCode}


In practice, we expect this functionality only to be used by consumers like \href{https://www.npmjs.com/package/jsdom}{\texttt{ jsdom}}, which are using these packages at a very low level. 