URI.js
js是一个 RFC 3986 兼容,方案可扩展的URI解析/验证/解析库,适用于所有JavaScript环境(浏览器,Node.js等)。 它也符合国际信息交换条例(RFC 3987), idna (RFC 5890)、IPv6地址(RFC 5952)、IPv6区域标识符(RFC 6874)规范。
URI.js有一个广泛的测试套件,可以在所有(Node.js, web)环境中工作。它的重量为6.4kb(压缩,压缩后为17kb)。
API
解析
URI.parse("uri://user:[email protected]:123/one/two.three?q1=a1&q2=a2#body");
//returns:
//{
// scheme : "uri",
// userinfo : "user:pass",
// host : "example.com",
// port : 123,
// path : "/one/two.three",
// query : "q1=a1&q2=a2",
// fragment : "body"
//}
序列化
URI.serialize({scheme : "http", host : "example.com", fragment : "footer"}) === "http://example.com/#footer"
解决
URI.resolve("uri://a/b/c/d?q", "../../g") === "uri://a/g"
正常化
URI.normalize("HTTP://ABC.com:80/%7Esmith/home.html") === "http://abc.com/~smith/home.html"
比较
URI.equal("example://a/b/c/%7Bfoo%7D", "eXAMPLE://a/./b/../b/%63/%7bfoo%7d") === true
IP支持
//IPv4 normalization
URI.normalize("//192.068.001.000") === "//192.68.1.0"
//IPv6 normalization
URI.normalize("//[2001:0:0DB8::0:0001]") === "//[2001:0:db8::1]"
//IPv6 zone identifier support
URI.parse("//[2001:db8::7%25en1]");
//returns:
//{
// host : "2001:db8::7%en1"
//}
IRI的支持
//convert IRI to URI
URI.serialize(URI.parse("http://examplé.org/rosé")) === "http://xn--exampl-gva.org/ros%C3%A9"
//convert URI to IRI
URI.serialize(URI.parse("http://xn--exampl-gva.org/ros%C3%A9"), {iri:true}) === "http://examplé.org/rosé"
选项
以上所有函数都可以接受一个额外的options参数,该参数是一个对象,可以包含以下一个或多个属性:
-
scheme(字符串)指示URI应被视为的模式,覆盖URI的正常模式解析行为。
-
reference(字符串)如果设置为
"suffix",它表明URI是后缀格式,验证器将使用该选项scheme属性来确定URI的模式。 -
tolerant(布尔,假)如果设置为
true,解析器将放松URI解析规则。 -
absolutePath(布尔,假)如果设置为
true时,序列化程序将不解析相对类型path组件。 -
iri(布尔,假)如果设置为
true,序列化器将按照要求对非ascii字符进行转义 RFC 3987。 -
unicodeSupport(布尔,假)如果设置为
true,解析器将按顺序对解析输出中的非ascii字符进行转义 RFC 3987。 -
domainHost(布尔,假)如果设置为
true,图书馆会招待的host组件作为域名,并转换idn(国际域名) RFC 5891。
方案可扩展
js支持插入自定义 计划 依赖的处理规则。目前,URI.js内置了对以下方案的支持:
- http [RFC 2616]
- https (RFC 2818]
- ws (RFC 6455]
- wss (RFC 6455]
- mailto [RFC 6068]
- 缸(RFC 2141]
- urn: uuid (RFC 4122]
HTTP / HTTPS支持
URI.equal("HTTP://ABC.COM:80", "http://abc.com/") === true
URI.equal("https://abc.com", "HTTPS://ABC.COM:443/") === true
WS / WSS支持
URI.parse("wss://example.com/foo?bar=baz");
//returns:
//{
// scheme : "wss",
// host: "example.com",
// resourceName: "/foo?bar=baz",
// secure: true,
//}
URI.equal("WS://ABC.COM:80/chat#one", "ws://abc.com/chat") === true
Mailto支持
URI.parse("mailto:[email protected],[email protected]?subject=SUBSCRIBE&body=Sign%20me%20up!");
//returns:
//{
// scheme : "mailto",
// to : ["[email protected]", "[email protected]"],
// subject : "SUBSCRIBE",
// body : "Sign me up!"
//}
URI.serialize({
scheme : "mailto",
to : ["[email protected]"],
subject : "REMOVE",
body : "Please remove me",
headers : {
cc : "[email protected]"
}
}) === "mailto:[email protected][email protected]&subject=REMOVE&body=Please%20remove%20me"
URN的支持
URI.parse("urn:example:foo");
//returns:
//{
// scheme : "urn",
// nid : "example",
// nss : "foo",
//}
URN UUID支持
URI.parse("urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6");
//returns:
//{
// scheme : "urn",
// nid : "uuid",
// uuid : "f81d4fae-7dec-11d0-a765-00a0c91e6bf6",
//}
用法
要在浏览器中加载,请使用以下标签:
<script type="text/javascript" src="uri-js/dist/es5/uri.all.min.js"></script>
要在CommonJS/Module环境中加载,首先使用npm/yarn在命令行上运行:
npm install uri-js
# OR
yarn add uri-js
然后,在你的代码中,使用:
const URI = require("uri-js");
如果你用ES6+ (ESNEXT)或TypeScript编写代码,你可以使用:
import * as URI from "uri-js";
或者你可以使用named exports加载你需要的东西:
import { parse, serialize, resolve, resolveComponents, normalize, equal, removeDotSegments, pctEncChar, pctDecChars, escapeComponent, unescapeComponent } from "uri-js";
打破变化
打破3.x的变化
URN解析已经完全改变,以更好地与规范保持一致。计划现在总是 urn,但有两个新属性: nid 其中包含命名空间标识符,以及 nss 其中包含命名空间特定字符串。这个 nss 属性将被更高阶的方案处理程序(如UUID URN方案处理程序)删除。
URN的UUID现在可以在 uuid 财产。
打破2.x的变化
URI验证被删除了,因为它速度慢,暴露了一个漏洞,而且通常没有用处。
打破1.x的变化
这个 errors 已解析组件上的数组现在是 error 字符串。