data-urls

中文文档
8.0.0发布于 2026/10/04MITCommonJS构建来源已验证近 30 天下载 4.4亿

Parses data: URLs

原始使用文档
文档目录3 节
  1. API
  2. Decoding the body
  3. Advanced functionality: parsing from a URL record

Parse data: URLs

This package helps you parse data: URLs according to the WHATWG Fetch Standard:

const parseDataURL = require("data-urls");

const textExample = parseDataURL("data:,Hello%2C%20World!");
console.log(textExample.mimeType.toString()); // "text/plain;charset=US-ASCII"
console.log(textExample.body);                // Uint8Array(13) [ 72, 101, 108, 108, 111, 44, … ]

const htmlExample = parseDataURL("data:text/html,%3Ch1%3EHello%2C%20World!%3C%2Fh1%3E");
console.log(htmlExample.mimeType.toString()); // "text/html"
console.log(htmlExample.body);                // Uint8Array(22) [ 60, 104, 49, 62, 72, 101, … ]

const pngExample = parseDataURL("data:image/png;base64,iVBORw0KGgoAAA" +
                                "ANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4" +
                                "//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU" +
                                "5ErkJggg==");
console.log(pngExample.mimeType.toString()); // "image/png"
console.log(pngExample.body);                // Uint8Array(85) [ 137, 80, 78, 71, 13, 10, … ]

API

This package's main module's default export is a function that accepts a string and returns a { mimeType, body } object, or null if the result cannot be parsed as a data: URL.

  • The mimeType property is an instance of whatwg-mimetype's MIMEType class.
  • The body property is a Uint8Array instance.

As shown in the examples above, you can easily get a stringified version of the MIME type using its toString() method. Read on for more on getting the stringified version of the body.

Decoding the body

To decode the body bytes of a parsed data URL, you'll need to use the charset parameter of the MIME type, if any. This contains an encoding label; there are various possible labels for a given encoding. You can use the TextDecoder API for this:

const parseDataURL = require("data-urls");

const dataURL = parseDataURL(arbitraryString);

// If there's no charset parameter, e.g. if `arbitraryString` is `"data:text/plain,H%C3%A9llo!"`,
// then let's guess UTF-8.
const encodingLabel = dataURL.mimeType.parameters.get("charset") ?? "utf-8";
const decoder = new TextDecoder(encodingLabel);

const bodyDecoded = decoder.decode(dataURL.body);

(Note that as of the time of this writing in 2026-01, Node.js's built-in TextDecoder has many correctness bugs, so we suggest using the polyfill from the @exodus/bytes package until they are fixed.)

Using the parsed charset is quite important, since the spec requires that if no parseable MIME type is given, the default is "US-ASCII", aka windows-1252—not UTF-8, like you might asume. So for example, given an arbitraryString of "data:,H%E9llo!", the above code snippet will correctly produce a bodyDecoded of "Héllo!" by using the windows-1252 decoder, whereas if you used a UTF-8 decoder you'd get back "H�llo!".

Advanced functionality: parsing from a URL record

If you are using the whatwg-url package, you may already have a "URL record" object on hand, as produced by that package's parseURL export. In that case, you can use this package's fromURLRecord export to save a bit of work:

const { parseURL } = require("whatwg-url");
const dataURLFromURLRecord = require("data-urls").fromURLRecord;

const urlRecord = parseURL("data:,Hello%2C%20World!");
const dataURL = dataURLFromURLRecord(urlRecord);

In practice, we expect this functionality only to be used by consumers like jsdom, which are using these packages at a very low level.

版本与发布记录

data-urls 共发布 13 个版本,首次发布于 2018/02/01;过去 12 个月发布了 3 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
8.0.0latest2026/10/041 个月前
7.0.02026/02/029 个月前
6.0.12026/01/209 个月前
6.0.02025/09/131.1 年前
5.0.02023/11/112 年前
4.0.02023/01/033 年前

关于 data-urls 的常见问题

data-urls 是什么?

Parses data: URLs

如何安装 data-urls?

在项目目录运行 npm install data-urls,或使用 pnpm add / yarn add / bun add。

data-urls 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/data-urls 是否存在。

data-urls 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

data-urls 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js ^22.14.0 || >=24.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

data-urls 可以商用吗?

data-urls 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

data-urls 的下载量有多少?

根据 npm 官方下载统计,data-urls 在 2026/10/06 – 2026/11/04 这 30 天内被下载 436,854,494 次,日均约 14,561,816 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

data-urls 还在维护吗?

最新版本 8.0.0 发布于 2026/10/04(1 个月前);至今共发布 13 个版本,其中过去 12 个月发布了 3 个;首次发布于 2018/02/01。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。