Interface: UrlAPI
Defined in: packages/core/src/runtime.ts:267
URL operations interface Subset of Node.js url module
Properties
parse()
parse: {(
urlString):UrlWithStringQuery; (urlString,parseQueryString,slashesDenoteHost?):UrlWithStringQuery; (urlString,parseQueryString,slashesDenoteHost?):UrlWithParsedQuery; (urlString,parseQueryString,slashesDenoteHost?):Url; }
Defined in: packages/core/src/runtime.ts:268
Call Signature
(
urlString):UrlWithStringQuery
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.
Parameters
urlString
string
The URL string to parse.
Returns
UrlWithStringQuery
Since
v0.1.25
Deprecated
Use the WHATWG URL API instead.
Call Signature
(
urlString,parseQueryString,slashesDenoteHost?):UrlWithStringQuery
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.
Parameters
urlString
string
The URL string to parse.
parseQueryString
If true, the query property will always be set to an object returned by the querystring module's parse() method. If false, the query property
on the returned URL object will be an unparsed, undecoded string.
false | undefined
slashesDenoteHost?
boolean
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'}.
Returns
UrlWithStringQuery
Since
v0.1.25
Deprecated
Use the WHATWG URL API instead.
Call Signature
(
urlString,parseQueryString,slashesDenoteHost?):UrlWithParsedQuery
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.
Parameters
urlString
string
The URL string to parse.
parseQueryString
true
If true, the query property will always be set to an object returned by the querystring module's parse() method. If false, the query property
on the returned URL object will be an unparsed, undecoded string.
slashesDenoteHost?
boolean
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'}.
Returns
UrlWithParsedQuery
Since
v0.1.25
Deprecated
Use the WHATWG URL API instead.
Call Signature
(
urlString,parseQueryString,slashesDenoteHost?):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.
Parameters
urlString
string
The URL string to parse.
parseQueryString
boolean
If true, the query property will always be set to an object returned by the querystring module's parse() method. If false, the query property
on the returned URL object will be an unparsed, undecoded string.
slashesDenoteHost?
boolean
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'}.
Returns
Url
Since
v0.1.25
Deprecated
Use the WHATWG URL API instead.
format()
format: {(
urlObject,options?):string; (urlObject):string; }
Defined in: packages/core/src/runtime.ts:269
Call Signature
(
urlObject,options?):string
The 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.
- The literal string
- If the
- 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.
- If the
- 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.
- If the value of
- 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.
- If the value of
- Otherwise, if the
urlObject.hashproperty is notundefinedand is not a string, anErroris thrown. resultis returned.
Parameters
urlObject
URL
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().
options?
URLFormatOptions
Returns
string
Since
v0.1.25
Legacy
Use the WHATWG URL API instead.
Call Signature
(
urlObject):string
The 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.
- The literal string
- If the
- 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.
- If the
- 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.
- If the value of
- 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.
- If the value of
- Otherwise, if the
urlObject.hashproperty is notundefinedand is not a string, anErroris thrown. resultis returned.
Parameters
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().
string | UrlObject
Returns
string
Since
v0.1.25
Legacy
Use the WHATWG URL API instead.
fileURLToPath()
fileURLToPath: (
url,options?) =>string
Defined in: packages/core/src/runtime.ts:270
This 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)
Parameters
url
The file URL string or URL object to convert to a path.
string | URL
options?
FileUrlToPathOptions
Returns
string
The fully-resolved platform-specific Node.js file path.
Since
v10.12.0