<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Parse &#96;data:&#96; URLs——模具行业从业者必备知识手册]]></title><description><![CDATA[<p dir="auto">&lt;h2&gt;Parse <code>data:</code> URLs&lt;/h2&gt;</p>
<p dir="auto">&lt;p&gt;This package helps you parse &lt;code&gt;data:&lt;/code&gt; URLs &lt;a href="<a href="https://fetch.spec.whatwg.org/#data-urls" rel="nofollow ugc">https://fetch.spec.whatwg.org/#data-urls</a>"&gt;according to the WHATWG Fetch Standard&lt;/a&gt;:&lt;/p&gt;</p>
<p dir="auto">&lt;pre&gt;&lt;code&gt;<br />
const parseDataURL = require("data-urls");</p>
<p dir="auto">const textExample = parseDataURL("data:,Hello%2C%20World!");<br />
console.log(textExample.mimeType.toString()); // "text/plain;charset=US-ASCII"<br />
console.log(textExample.body.toString());     // "Hello, World!"</p>
<p dir="auto">const htmlExample = dataURL("data:text/html,%3Ch1%3EHello%2C%20World!%3C%2Fh1%3E");<br />
console.log(htmlExample.mimeType.toString()); // "text/html"<br />
console.log(htmlExample.body.toString());     // &lt;h1&gt;Hello, World!&lt;/h1&gt;</p>
<p dir="auto">const pngExample = parseDataURL("data:image/png;base64,iVBORw0KGgoAAA" +<br />
"ANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4" +<br />
"//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU" +<br />
"5ErkJggg==");<br />
console.log(pngExample.mimeType.toString()); // "image/png"<br />
console.log(pngExample.body);                // &lt;Buffer 89 50 4e 47 0d ... &gt;<br />
&lt;/code&gt;&lt;/pre&gt;</p>
<p dir="auto">&lt;h3&gt;API&lt;/h3&gt;</p>
<p dir="auto">&lt;p&gt;This package's main module's default export is a function that accepts a string and returns a &lt;code&gt;{ mimeType, body }&lt;/code&gt; object, or &lt;code&gt;null&lt;/code&gt; if the result cannot be parsed as a &lt;code&gt;data:&lt;/code&gt; URL.&lt;/p&gt;</p>
<p dir="auto">&lt;ul&gt;<br />
&lt;li&gt;The &lt;code&gt;mimeType&lt;/code&gt; property is an instance of &lt;a href="<a href="https://www.npmjs.com/package/whatwg-mimetype" rel="nofollow ugc">https://www.npmjs.com/package/whatwg-mimetype</a>"&gt;whatwg-mimetype&lt;/a&gt;'s &lt;code&gt;MIMEType&lt;/code&gt; class.&lt;/li&gt;<br />
&lt;li&gt;The &lt;code&gt;body&lt;/code&gt; property is a Node.js &lt;a href="<a href="https://nodejs.org/docs/latest/api/buffer.html" rel="nofollow ugc">https://nodejs.org/docs/latest/api/buffer.html</a>"&gt;&lt;code&gt;Buffer&lt;/code&gt;&lt;/a&gt; instance.&lt;/li&gt;<br />
&lt;/ul&gt;</p>
<p dir="auto">&lt;p&gt;As shown in the examples above, both of these have useful &lt;code&gt;toString()&lt;/code&gt; methods for manipulating them as string values. However…&lt;/p&gt;</p>
<p dir="auto">&lt;h4&gt;A word of caution on string decoding&lt;/h4&gt;</p>
<p dir="auto">&lt;p&gt;Because Node.js's &lt;code&gt;Buffer.prototype.toString()&lt;/code&gt; assumes a UTF-8 encoding, simply doing &lt;code&gt;dataURL.body.toString()&lt;/code&gt; may not work correctly if the &lt;code&gt;data:&lt;/code&gt; URL's contents were not originally written in UTF-8. This includes if the encoding is "US-ASCII", &lt;a href="<a href="https://encoding.spec.whatwg.org/#names-and-labels" rel="nofollow ugc">https://encoding.spec.whatwg.org/#names-and-labels</a>"&gt;aka windows-1252&lt;/a&gt;, which is notable for being the default in many cases.&lt;/p&gt;</p>
<p dir="auto">&lt;p&gt;A more complete decoding example would use the &lt;a href="<a href="https://www.npmjs.com/package/whatwg-encoding" rel="nofollow ugc">https://www.npmjs.com/package/whatwg-encoding</a>"&gt;whatwg-encoding&lt;/a&gt; package as follows:&lt;/p&gt;</p>
<p dir="auto">&lt;pre&gt;&lt;code&gt;<br />
const parseDataURL = require("data-urls");<br />
const { labelToName, decode } = require("whatwg-encoding");</p>
<p dir="auto">const dataURL = parseDataURL(arbitraryString);<br />
const encodingName = labelToName(dataURL.mimeType.parameters.get("charset"));<br />
const bodyDecoded = decode(dataURL.body, encodingName);<br />
&lt;/code&gt;&lt;/pre&gt;</p>
<p dir="auto">&lt;p&gt;For example, given an &lt;code&gt;arbitraryString&lt;/code&gt; of &lt;code&gt;data:,Hello!&lt;/code&gt;, this will produce a &lt;code&gt;bodyDecoded&lt;/code&gt; of &lt;code&gt;"Hello!"&lt;/code&gt;, as expected. But given an &lt;code&gt;arbitraryString&lt;/code&gt; of &lt;code&gt;"data:,Héllo!"&lt;/code&gt;, this will correctly produce a &lt;code&gt;bodyDecoded&lt;/code&gt; of &lt;code&gt;"Héllo!"&lt;/code&gt;, whereas just doing &lt;code&gt;dataURL.body.toString()&lt;/code&gt; will give back &lt;code&gt;"HÃ<img src="https://forum.mj7.cn/assets/plugins/nodebb-plugin-emoji/emoji/android/00a9.png?v=k0o2a4s5i30" class="not-responsive emoji emoji-android emoji--copyright" title=":copyright:" alt="©" />llo!"&lt;/code&gt;.&lt;/p&gt;</p>
<p dir="auto">&lt;p&gt;In summary, only use &lt;code&gt;dataURL.body.toString()&lt;/code&gt; when you are very certain your data is inside the ASCII range (i.e. code points within the range U+0000 to U+007F).&lt;/p&gt;</p>
<p dir="auto">&lt;h4&gt;Advanced functionality: parsing from a URL record&lt;/h4&gt;</p>
<p dir="auto">&lt;p&gt;If you are using the &lt;a href="<a href="https://github.com/jsdom/whatwg-url" rel="nofollow ugc">https://github.com/jsdom/whatwg-url</a>"&gt;whatwg-url&lt;/a&gt; package, you may already have a "URL record" object on hand, as produced by that package's &lt;code&gt;parseURL&lt;/code&gt; export. In that case, you can use this package's &lt;code&gt;fromURLRecord&lt;/code&gt; export to save a bit of work:&lt;/p&gt;</p>
<p dir="auto">&lt;pre&gt;&lt;code&gt;<br />
const { parseURL } = require("whatwg-url");<br />
const dataURLFromURLRecord = require("data-urls").fromURLRecord;</p>
<p dir="auto">const urlRecord = parseURL("data:,Hello%2C%20World!");<br />
const dataURL = dataURLFromURLRecord(urlRecord);<br />
&lt;/code&gt;&lt;/pre&gt;</p>
<p dir="auto">&lt;p&gt;In practice, we expect this f&lt;/p&gt;</p>
<p dir="auto">&lt;hr&gt;<br />
&lt;p&gt;有没有同行遇到过同样的问题？分享一下经验。&lt;/p&gt;</p>
]]></description><link>https://forum.mj7.cn/topic/7194/parse-data-urls-模具行业从业者必备知识手册</link><generator>RSS for Node</generator><lastBuildDate>Fri, 02 Oct 2026 10:19:45 GMT</lastBuildDate><atom:link href="https://forum.mj7.cn/topic/7194.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 19 Apr 2026 21:52:09 GMT</pubDate><ttl>60</ttl></channel></rss>