whatwg-url

中文文档
17.3.0发布于 2026/10/14MITCommonJS构建来源已验证近 30 天下载 10.1亿

An implementation of the WHATWG URL Standard's URL API and parsing machinery

原始使用文档
文档目录7 节
  1. Specification conformance
  2. API
  3. The URL and URLSearchParams classes
  4. Low-level URL Standard API
  5. whatwg-url/webidl2js-wrapper module
  6. Development instructions
  7. Supporting whatwg-url

whatwg-url

whatwg-url is a full implementation of the WHATWG URL Standard. It can be used standalone, but it also exposes a lot of the internal algorithms that are useful for integrating a URL parser into a project like jsdom.

Specification conformance

whatwg-url is currently up to date with the URL spec up to commit fde3f74.

For file: URLs, whose origin is left unspecified, whatwg-url chooses to use a new opaque origin (which serializes to "null").

API

The URL and URLSearchParams classes

The main API is provided by the URL and URLSearchParams exports, which follows the spec's behavior in all ways (including e.g. USVString conversion). Most consumers of this library will want to use these.

Low-level URL Standard API

The following methods are exported for use by places like jsdom that need to implement things like HTMLHyperlinkElementUtils. They mostly operate on or return an "internal URL" or "URL record" type.

The stateOverride parameter is one of the following strings:

The URL record type has the following API:

These properties should be treated with care, as in general changing them will cause the URL record to be in an inconsistent state until the appropriate invocation of basicURLParse is used to fix it up. You can see examples of this in the URL Standard, where there are many step sequences like "4. Set context object’s url’s fragment to the empty string. 5. Basic URL parse input with context object’s url as url and fragment state as state override." In between those two steps, a URL record is in an unusable state.

The return value of "failure" in the spec is represented by null. That is, functions like parseURL and basicURLParse can return either a URL record or null.

The parseURLWithValidationErrors function returns an object with a url property and a validationErrors property. The url property is a URL record or null, and the validationErrors property is an array of validation error names reported while parsing.

const result = parseURLWithValidationErrors("https://example.org/%s");

console.log(result.url === null); // false
console.log(result.validationErrors); // [ "invalid-URL-unit" ]

The isValidURLString function implements the grammar-based validity checker from the URL Standard's URL writing section. This is separate from parser validation errors, so it can disagree with parseURLWithValidationErrors() in some cases. Pass a baseURL URL record to check relative-URL strings.

whatwg-url/webidl2js-wrapper module

This module exports the URL and URLSearchParams interface wrappers API generated by webidl2js.

Development instructions

First, install Node.js. Then, fetch the dependencies of whatwg-url, by running from this directory:

npm install

To run tests:

npm test

To generate a coverage report:

npm run coverage

To build and run the live viewer:

npm run prepare
npm run build-live-viewer

Serve the contents of the live-viewer directory using any web server.

Supporting whatwg-url

The jsdom project (including whatwg-url) is a community-driven project maintained by a team of volunteers. You could support us by:

  • Getting professional support for whatwg-url as part of a Tidelift subscription. Tidelift helps making open source sustainable for us while giving teams assurances for maintenance, licensing, and security.
  • Contributing directly to the project.

版本与发布记录

whatwg-url 共发布 77 个版本,首次发布于 2015/06/04;过去 12 个月发布了 8 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
17.3.0latest2026/10/1423 天前
17.2.02026/10/041 个月前
17.1.22026/09/211 个月前
17.1.12026/09/101 个月前
17.1.02026/07/074 个月前
17.0.02026/06/294 个月前

关于 whatwg-url 的常见问题

whatwg-url 是什么?

An implementation of the WHATWG URL Standard's URL API and parsing machinery

如何安装 whatwg-url?

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

whatwg-url 支持 TypeScript 吗?

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

whatwg-url 支持 ESM 还是 CommonJS?

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

whatwg-url 需要什么版本的 Node.js?

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

whatwg-url 可以商用吗?

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

whatwg-url 的下载量有多少?

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

whatwg-url 还在维护吗?

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