Exports
[<Class>]
type Exports =
static member URL (input: U2<string,Input>, ?base: U2<string,URL>) : URL
static member URLSearchParams (?init: U5<string,Iterable<(string * string),obj,obj>,(string * string) array,Record<string,U2<string,string array>>,URLSearchParams>) : URLSearchParams
static member domainToASCII (domain: string) : string
static member domainToUnicode (domain: string) : string
static member fileURLToPath (url: U2<string,URL>, ?options: FileUrlToPathOptions) : string
static member fileURLToPathBuffer (url: U2<string,URL>, ?options: FileUrlToPathOptions) : NonSharedBuffer
static member format (urlObject: U2<string,UrlObject>) : string
static member format (urlObject: URL, ?options: URLFormatOptions) : string
static member parse (urlString: string, ?parseQueryString: bool, ?slashesDenoteHost: bool) : UrlWithStringQuery
static member parse (urlString: string) : UrlWithStringQuery
static member parse_Overload2 (urlString: string, parseQueryString: bool, ?slashesDenoteHost: bool) : U2<UrlWithParsedQuery,Url>
static member pathToFileURL (path: string, ?options: PathToFileUrlOptions) : URL
static member resolve (from: string, to: string) : string
static member urlToHttpOptions (url: URL) : ClientRequestArgs| Assembly | Xantham.Fable.Node |
| Attributes | [<Erase>] |
The package's value exports, each bound to its import.
Methods
static member URL (input: U2<string,Input>, ?base: U2<string,URL>) : URL
static member URL (input: U2<string,Input>, ?base: U2<string,URL>) : URLBrowser-compatible URL class, implemented by following the WHATWG URL Standard. Examples of parsed URLs may be found in the Standard itself. The URL class is also available on the global object.
In accordance with browser conventions, all properties of URL objects are implemented as getters and setters on the class prototype, rather than as data properties on the object itself. Thus, unlike legacy urlObjects, using the delete keyword on any properties of URL objects (e.g. delete myURL.protocol, delete myURL.pathname, etc) has no effect but will still return true.
@since v7.0.0, v6.13.0
static member URLSearchParams (?init: U5<string,Iterable<(string * string),obj,obj>,(string * string) array,Record<string,U2<string,string array>>,URLSearchParams>) : URLSearchParams
static member URLSearchParams (?init: U5<string,Iterable<(string * string),obj,obj>,(string * string) array,Record<string,U2<string,string array>>,URLSearchParams>) : URLSearchParamsThe URLSearchParams API provides read and write access to the query of a URL. The URLSearchParams class can also be used standalone with one of the four following constructors. The URLSearchParams class is also available on the global object.
The WHATWG URLSearchParams interface and the querystring module have similar purpose, but the purpose of the querystring module is more general, as it allows the customization of delimiter characters (& and =). On the other hand, this API is designed purely for URL query strings.
const myURL = new URL('https://example.org/?abc=123');
console.log(myURL.searchParams.get('abc'));
// Prints 123
myURL.searchParams.append('abc', 'xyz');
console.log(myURL.href);
// Prints https://example.org/?abc=123&abc=xyz
myURL.searchParams.delete('abc');
myURL.searchParams.set('a', 'b');
console.log(myURL.href);
// Prints https://example.org/?a=b
const newSearchParams = new URLSearchParams(myURL.searchParams);
// The above is equivalent to
// const newSearchParams = new URLSearchParams(myURL.search);
newSearchParams.append('a', 'c');
console.log(myURL.href);
// Prints https://example.org/?a=b
console.log(newSearchParams.toString());
// Prints a=b&a=c
// newSearchParams.toString() is implicitly called
myURL.search = newSearchParams;
console.log(myURL.href);
// Prints https://example.org/?a=b&a=c
newSearchParams.delete('a');
console.log(myURL.href);
// Prints https://example.org/?a=b&a=c
@since v7.5.0, v6.13.0
static member domainToASCII (domain: string) : string
static member domainToASCII (domain: string) : stringReturns the Punycode ASCII serialization of the domain. If domain is an invalid domain, the empty string is returned.
It performs the inverse operation to domainToUnicode.
import url from 'node:url';
console.log(url.domainToASCII('español.com'));
// Prints xn--espaol-zwa.com
console.log(url.domainToASCII('中文.com'));
// Prints xn--fiq228c.com
console.log(url.domainToASCII('xn--iñvalid.com'));
// Prints an empty string
@since v7.4.0, v6.13.0
static member domainToUnicode (domain: string) : string
static member domainToUnicode (domain: string) : stringReturns the Unicode serialization of the domain. If domain is an invalid domain, the empty string is returned.
It performs the inverse operation to domainToASCII.
import url from 'node:url';
console.log(url.domainToUnicode('xn--espaol-zwa.com'));
// Prints español.com
console.log(url.domainToUnicode('xn--fiq228c.com'));
// Prints 中文.com
console.log(url.domainToUnicode('xn--iñvalid.com'));
// Prints an empty string
@since v7.4.0, v6.13.0
static member fileURLToPath (url: U2<string,URL>, ?options: FileUrlToPathOptions) : string
static member fileURLToPath (url: U2<string,URL>, ?options: FileUrlToPathOptions) : stringThis function ensures the correct decodings of percent-encoded characters as well as ensuring a cross-platform valid absolute path string.
import { fileURLToPath } from 'node:url';
const __filename = fileURLToPath(import.meta.url);
new URL('file:///C:/path/').pathname; // Incorrect: /C:/path/
fileURLToPath('file:///C:/path/'); // Correct: C:\path\ (Windows)
new URL('file://nas/foo.txt').pathname; // Incorrect: /foo.txt
fileURLToPath('file://nas/foo.txt'); // Correct: \\nas\foo.txt (Windows)
new URL('file:///你好.txt').pathname; // Incorrect: /%E4%BD%A0%E5%A5%BD.txt
fileURLToPath('file:///你好.txt'); // Correct: /你好.txt (POSIX)
new URL('file:///hello world').pathname; // Incorrect: /hello%20world
fileURLToPath('file:///hello world'); // Correct: /hello world (POSIX)
@since v10.12.0
| Parameter | |
|---|---|
url | The file URL string or URL object to convert to a path. |
Returns. The fully-resolved platform-specific Node.js file path.
static member fileURLToPathBuffer (url: U2<string,URL>, ?options: FileUrlToPathOptions) : NonSharedBuffer
static member fileURLToPathBuffer (url: U2<string,URL>, ?options: FileUrlToPathOptions) : NonSharedBufferLike url.fileURLToPath(...) except that instead of returning a string representation of the path, a Buffer is returned. This conversion is helpful when the input URL contains percent-encoded segments that are not valid UTF-8 / Unicode sequences.
@since v22.18.0
| Parameter | |
|---|---|
url | The file URL string or URL object to convert to a path. |
Returns. The fully-resolved platform-specific Node.js file path as a Buffer.
static member format (urlObject: U2<string,UrlObject>) : string
static member format (urlObject: U2<string,UrlObject>) : stringThe url.format() method returns a formatted URL string derived from urlObject.
import url from 'node:url';
url.format({
protocol: 'https',
hostname: 'example.com',
pathname: '/some/path',
query: {
page: 1,
format: 'json',
},
});
// => 'https://example.com/some/path?page=1&format=json'
If urlObject is not an object or a string, url.format() will throw a TypeError.
The formatting process operates as follows:
- A new empty string
resultis created. - If
urlObject.protocolis a string, it is appended as-is toresult. - Otherwise, if
urlObject.protocolis notundefinedand is not a string, anErroris thrown. - For all string values of
urlObject.protocolthat do not end with an ASCII colon (:) character, the literal string:will be appended toresult. - If either of the following conditions is true, then the literal string
//will be appended toresult: urlObject.slashesproperty is true;urlObject.protocolbegins withhttp,https,ftp,gopher, orfile;- If the value of the
urlObject.authproperty is truthy, and eitherurlObject.hostorurlObject.hostnameare notundefined, the value ofurlObject.authwill be coerced into a string and appended toresultfollowed by the literal string@. - If the
urlObject.hostproperty isundefinedthen: - If the
urlObject.hostnameis a string, it is appended toresult. - Otherwise, if
urlObject.hostnameis notundefinedand is not a string, anErroris thrown. - If the
urlObject.portproperty value is truthy, andurlObject.hostnameis notundefined: - The literal string
:is appended toresult, and - The value of
urlObject.portis coerced to a string and appended toresult. - Otherwise, if the
urlObject.hostproperty value is truthy, the value ofurlObject.hostis coerced to a string and appended toresult. - If the
urlObject.pathnameproperty is a string that is not an empty string: - If the
urlObject.pathnamedoes not start with an ASCII forward slash (/), then the literal string'/'is appended toresult. - The value of
urlObject.pathnameis appended toresult. - Otherwise, if
urlObject.pathnameis notundefinedand is not a string, anErroris thrown. - If the
urlObject.searchproperty isundefinedand if theurlObject.queryproperty is anObject, the literal string?is appended toresultfollowed by the output of calling thequerystringmodule'sstringify()method passing the value ofurlObject.query. - Otherwise, if
urlObject.searchis a string: - If the value of
urlObject.searchdoes not start with the ASCII question mark (?) character, the literal string?is appended toresult. - The value of
urlObject.searchis appended toresult. - Otherwise, if
urlObject.searchis notundefinedand is not a string, anErroris thrown. - If the
urlObject.hashproperty is a string: - If the value of
urlObject.hashdoes not start with the ASCII hash (#) character, the literal string#is appended toresult. - The value of
urlObject.hashis appended toresult. - Otherwise, if the
urlObject.hashproperty is notundefinedand is not a string, anErroris thrown. resultis returned.
@since v0.1.25
| Parameter | |
|---|---|
urlObject | A URL object (as returned by url.parse() or constructed otherwise). If a string, it is converted to an object by passing it to url.parse(). |
urlObject | A URL object (as returned by url.parse() or constructed otherwise). If a string, it is converted to an object by passing it to url.parse(). |
static member format (urlObject: URL, ?options: URLFormatOptions) : string
static member format (urlObject: URL, ?options: URLFormatOptions) : stringThe url.format() method returns a formatted URL string derived from urlObject.
import url from 'node:url';
url.format({
protocol: 'https',
hostname: 'example.com',
pathname: '/some/path',
query: {
page: 1,
format: 'json',
},
});
// => 'https://example.com/some/path?page=1&format=json'
If urlObject is not an object or a string, url.format() will throw a TypeError.
The formatting process operates as follows:
- A new empty string
resultis created. - If
urlObject.protocolis a string, it is appended as-is toresult. - Otherwise, if
urlObject.protocolis notundefinedand is not a string, anErroris thrown. - For all string values of
urlObject.protocolthat do not end with an ASCII colon (:) character, the literal string:will be appended toresult. - If either of the following conditions is true, then the literal string
//will be appended toresult: urlObject.slashesproperty is true;urlObject.protocolbegins withhttp,https,ftp,gopher, orfile;- If the value of the
urlObject.authproperty is truthy, and eitherurlObject.hostorurlObject.hostnameare notundefined, the value ofurlObject.authwill be coerced into a string and appended toresultfollowed by the literal string@. - If the
urlObject.hostproperty isundefinedthen: - If the
urlObject.hostnameis a string, it is appended toresult. - Otherwise, if
urlObject.hostnameis notundefinedand is not a string, anErroris thrown. - If the
urlObject.portproperty value is truthy, andurlObject.hostnameis notundefined: - The literal string
:is appended toresult, and - The value of
urlObject.portis coerced to a string and appended toresult. - Otherwise, if the
urlObject.hostproperty value is truthy, the value ofurlObject.hostis coerced to a string and appended toresult. - If the
urlObject.pathnameproperty is a string that is not an empty string: - If the
urlObject.pathnamedoes not start with an ASCII forward slash (/), then the literal string'/'is appended toresult. - The value of
urlObject.pathnameis appended toresult. - Otherwise, if
urlObject.pathnameis notundefinedand is not a string, anErroris thrown. - If the
urlObject.searchproperty isundefinedand if theurlObject.queryproperty is anObject, the literal string?is appended toresultfollowed by the output of calling thequerystringmodule'sstringify()method passing the value ofurlObject.query. - Otherwise, if
urlObject.searchis a string: - If the value of
urlObject.searchdoes not start with the ASCII question mark (?) character, the literal string?is appended toresult. - The value of
urlObject.searchis appended toresult. - Otherwise, if
urlObject.searchis notundefinedand is not a string, anErroris thrown. - If the
urlObject.hashproperty is a string: - If the value of
urlObject.hashdoes not start with the ASCII hash (#) character, the literal string#is appended toresult. - The value of
urlObject.hashis appended toresult. - Otherwise, if the
urlObject.hashproperty is notundefinedand is not a string, anErroris thrown. resultis returned.
@since v0.1.25
| Parameter | |
|---|---|
urlObject | A URL object (as returned by url.parse() or constructed otherwise). If a string, it is converted to an object by passing it to url.parse(). |
urlObject | A URL object (as returned by url.parse() or constructed otherwise). If a string, it is converted to an object by passing it to url.parse(). |
static member parse (urlString: string, ?parseQueryString: bool, ?slashesDenoteHost: bool) : UrlWithStringQuery
static member parse (urlString: string, ?parseQueryString: bool, ?slashesDenoteHost: bool) : UrlWithStringQueryThe url.parse() method takes a URL string, parses it, and returns a URL object.
A TypeError is thrown if urlString is not a string.
A URIError is thrown if the auth property is present but cannot be decoded.
url.parse() uses a lenient, non-standard algorithm for parsing URL strings. It is prone to security issues such as host name spoofing and incorrect handling of usernames and passwords. Do not use with untrusted input. CVEs are not issued for url.parse() vulnerabilities. Use the WHATWG URL API instead.
@since v0.1.25
| Parameter | |
|---|---|
urlString | The URL string to parse. |
parseQueryString | If true, the query property will always be set to an object returned by the {@link querystring} module's parse() method. If false, the query property on the returned URL object will be an unparsed, undecoded string. |
slashesDenoteHost | If true, the first token after the literal string // and preceding the next / will be interpreted as the host. For instance, given //foo/bar, the result would be {host: 'foo', pathname: '/bar'} rather than {pathname: '//foo/bar'}. |
static member parse (urlString: string) : UrlWithStringQuery
static member parse (urlString: string) : UrlWithStringQueryThe url.parse() method takes a URL string, parses it, and returns a URL object.
A TypeError is thrown if urlString is not a string.
A URIError is thrown if the auth property is present but cannot be decoded.
url.parse() uses a lenient, non-standard algorithm for parsing URL strings. It is prone to security issues such as host name spoofing and incorrect handling of usernames and passwords. Do not use with untrusted input. CVEs are not issued for url.parse() vulnerabilities. Use the WHATWG URL API instead.
@since v0.1.25
| Parameter | |
|---|---|
urlString | The URL string to parse. |
parseQueryString | If true, the query property will always be set to an object returned by the {@link querystring} module's parse() method. If false, the query property on the returned URL object will be an unparsed, undecoded string. |
slashesDenoteHost | If true, the first token after the literal string // and preceding the next / will be interpreted as the host. For instance, given //foo/bar, the result would be {host: 'foo', pathname: '/bar'} rather than {pathname: '//foo/bar'}. |
static member parse_Overload2 (urlString: string, parseQueryString: bool, ?slashesDenoteHost: bool) : U2<UrlWithParsedQuery,Url>
static member parse_Overload2 (urlString: string, parseQueryString: bool, ?slashesDenoteHost: bool) : U2<UrlWithParsedQuery,Url>The url.parse() method takes a URL string, parses it, and returns a URL object.
A TypeError is thrown if urlString is not a string.
A URIError is thrown if the auth property is present but cannot be decoded.
url.parse() uses a lenient, non-standard algorithm for parsing URL strings. It is prone to security issues such as host name spoofing and incorrect handling of usernames and passwords. Do not use with untrusted input. CVEs are not issued for url.parse() vulnerabilities. Use the WHATWG URL API instead.
@since v0.1.25
| Parameter | |
|---|---|
urlString | The URL string to parse. |
parseQueryString | If true, the query property will always be set to an object returned by the {@link querystring} module's parse() method. If false, the query property on the returned URL object will be an unparsed, undecoded string. |
slashesDenoteHost | If true, the first token after the literal string // and preceding the next / will be interpreted as the host. For instance, given //foo/bar, the result would be {host: 'foo', pathname: '/bar'} rather than {pathname: '//foo/bar'}. |
static member pathToFileURL (path: string, ?options: PathToFileUrlOptions) : URL
static member pathToFileURL (path: string, ?options: PathToFileUrlOptions) : URLThis function ensures that path is resolved absolutely, and that the URL control characters are correctly encoded when converting into a File URL.
import { pathToFileURL } from 'node:url';
new URL('/foo#1', 'file:'); // Incorrect: file:///foo#1
pathToFileURL('/foo#1'); // Correct: file:///foo%231 (POSIX)
new URL('/some/path%.c', 'file:'); // Incorrect: file:///some/path%.c
pathToFileURL('/some/path%.c'); // Correct: file:///some/path%25.c (POSIX)
@since v10.12.0
| Parameter | |
|---|---|
path | The path to convert to a File URL. |
Returns. The file URL object.
static member resolve (from: string, to: string) : string
static member resolve (from: string, to: string) : stringThe url.resolve() method resolves a target URL relative to a base URL in a manner similar to that of a web browser resolving an anchor tag.
import url from 'node:url';
url.resolve('/one/two/three', 'four'); // '/one/two/four'
url.resolve('http://example.com/', '/one'); // 'http://example.com/one'
url.resolve('http://example.com/one', '/two'); // 'http://example.com/two'
To achieve the same result using the WHATWG URL API:
function resolve(from, to) {
const resolvedUrl = new URL(to, new URL(from, 'resolve://'));
if (resolvedUrl.protocol === 'resolve:') {
// `from` is a relative URL.
const { pathname, search, hash } = resolvedUrl;
return pathname + search + hash;
}
return resolvedUrl.toString();
}
resolve('/one/two/three', 'four'); // '/one/two/four'
resolve('http://example.com/', '/one'); // 'http://example.com/one'
resolve('http://example.com/one', '/two'); // 'http://example.com/two'
@since v0.1.25
| Parameter | |
|---|---|
from | The base URL to use if to is a relative URL. |
to | The target URL to resolve. |
static member urlToHttpOptions (url: URL) : ClientRequestArgs
static member urlToHttpOptions (url: URL) : ClientRequestArgsThis utility function converts a URL object into an ordinary options object as expected by the http.request() and https.request() APIs.
import { urlToHttpOptions } from 'node:url';
const myURL = new URL('https://a:b@測試?abc#foo');
console.log(urlToHttpOptions(myURL));
/*
{
protocol: 'https:',
hostname: 'xn--g6w251d',
hash: '#foo',
search: '?abc',
pathname: '/',
path: '/?abc',
href: 'https://a:b@xn--g6w251d/?abc#foo',
auth: 'a:b'
}
@since v15.7.0, v14.18.0
| Parameter | |
|---|---|
url | The WHATWG URL object to convert to an options object. |
Returns. Options object