GeezerVison is showing github.com — doc: discuss special protocol handling · nodejs/node@1b41cd4
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: doc/api/url.md +40Lines changed: 40 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -389,6 +389,46 @@ console.log(myURL.href); |
389 | 389 |
|
390 | 390 | Invalid URL protocol values assigned to the `protocol` property are ignored. |
391 | 391 |
|
| 392 | +##### Special Schemes |
| 393 | + |
| 394 | +The [WHATWG URL Standard][] considers a handful of URL protocol schemes to be |
| 395 | +_special_ in terms of how they are parsed and serialized. When a URL is |
| 396 | +parsed using one of these special protocols, the `url.protocol` property |
| 397 | +may be changed to another special protocol but cannot be changed to a |
| 398 | +non-special protocol, and vice versa. |
| 399 | + |
| 400 | +For instance, changing from `http` to `https` works: |
| 401 | + |
| 402 | +```js |
| 403 | +const u = new URL('http://example.org'); |
| 404 | +u.protocol = 'https'; |
| 405 | +console.log(u.href); |
| 406 | +// https://example.org |
| 407 | +``` |
| 408 | + |
| 409 | +However, changing from `http` to a hypothetical `fish` protocol does not |
| 410 | +because the new protocol is not special. |
| 411 | + |
| 412 | +```js |
| 413 | +const u = new URL('http://example.org'); |
| 414 | +u.protocol = 'fish'; |
| 415 | +console.log(u.href); |
| 416 | +// http://example.org |
| 417 | +``` |
| 418 | + |
| 419 | +Likewise, changing from a non-special protocol to a special protocol is also |
| 420 | +not permitted: |
| 421 | + |
| 422 | +```js |
| 423 | +const u = new URL('fish://example.org'); |
| 424 | +u.protocol = 'http'; |
| 425 | +console.log(u.href); |
| 426 | +// fish://example.org |
| 427 | +``` |
| 428 | + |
| 429 | +The protocol schemes considered to be special by the WHATWG URL Standard |
| 430 | +include: `ftp`, `file`, `gopher`, `http`, `https`, `ws`, and `wss`. |
| 431 | + |
392 | 432 | #### url.search |
393 | 433 |
|
394 | 434 | * {string} |
|