Skip to content

Commit fc8c3d2

Browse files
Feat(node url to whatwg url) (#172)
1 parent 4a7a7f7 commit fc8c3d2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+1330
-33
lines changed

package-lock.json

Lines changed: 45 additions & 33 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Node.js URL to WHATWG URL
2+
3+
This recipe converts Node.js `url` module usage to the WHATWG URL API. It modifies code that uses the legacy `url` module to use the modern `URL` class instead.
4+
5+
See [DEP0116](https://nodejs.org/api/deprecations.html#DEP0116).
6+
7+
## Example
8+
9+
### `url.parse` to `new URL()`
10+
11+
**Before:**
12+
```js
13+
const url = require('node:url');
14+
15+
const myURL = url.parse('https://example.com/path?query=string#hash');
16+
17+
const { auth } = myURL;
18+
const urlAuth = myURL.auth;
19+
20+
const { path } = myURL;
21+
const urlPath = myURL.path;
22+
23+
const { hostname } = myURL;
24+
const urlHostname = myURL.hostname;
25+
```
26+
27+
**After:**
28+
```js
29+
const myURL = new URL('https://example.com/path?query=string#hash');
30+
31+
const auth = `${myURL.username}:${myURL.password}`;
32+
const urlAuth = `${myURL.username}:${myURL.password}`;
33+
34+
const path = `${myURL.pathname}${myURL.search}`;
35+
const urlPath = `${myURL.pathname}${myURL.search}`;
36+
37+
const hostname = myURL.hostname.replace(/^\[|\]$/, '');
38+
const urlHostname = myURL.hostname.replace(/^\[|\]$/, '');
39+
```
40+
41+
### `url.format` to `myUrl.toString()
42+
43+
**Before:**
44+
```js
45+
const url = require('node:url');
46+
47+
url.format({
48+
protocol: 'https',
49+
hostname: 'example.com',
50+
pathname: '/some/path',
51+
query: {
52+
page: 1,
53+
format: 'json',
54+
},
55+
});
56+
```
57+
58+
**After:**
59+
```js
60+
const myUrl = new URL('https://example.com/some/path?page=1&format=json').toString();
61+
```
62+
63+
> **Note:** The migration of `url.format` can also be done as `` `${new URL('https://example.com/some/path?page=1&format=json')}` `` which is little bit more efficient. But it may be less readable for some users.
64+
65+
## Caveats
66+
67+
The [`url.resolve`](https://nodejs.org/api/url.html#urlresolvefrom-to) method is not directly translatable to the WHATWG URL API. You may need to implement custom logic to handle URL resolution in your application.
68+
69+
```js
70+
function resolve(from, to) {
71+
const resolvedUrl = new URL(to, new URL(from, 'resolve://'));
72+
if (resolvedUrl.protocol === 'resolve:') {
73+
// `from` is a relative URL.
74+
const { pathname, search, hash } = resolvedUrl;
75+
return pathname + search + hash;
76+
}
77+
return resolvedUrl.toString();
78+
}
79+
80+
resolve('/one/two/three', 'four'); // '/one/two/four'
81+
resolve('http://example.com/', '/one'); // 'http://example.com/one'
82+
resolve('http://example.com/one', '/two'); // 'http://example.com/two'
83+
```
84+
85+
If you are using `url.parse().auth`, `url.parse().username`, or `url.parse().password`. Will transform to `new URL().auth` which is not valid WHATWG url property. So you have to manually construct the `auth`, `path`, and `hostname` properties as shown in the examples above.
86+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
schema_version: "1.0"
2+
name: "@nodejs/node-url-to-whatwg-url"
3+
version: 1.0.0
4+
description: Handle DEP0116 via transforming `url.parse` to `new URL()`
5+
author: Augustin Mauroy
6+
license: MIT
7+
workflow: workflow.yaml
8+
category: migration
9+
10+
targets:
11+
languages:
12+
- javascript
13+
- typescript
14+
15+
keywords:
16+
- transformation
17+
- migration
18+
19+
registry:
20+
access: public
21+
visibility: public
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "@nodejs/node-url-to-whatwg-url",
3+
"version": "1.0.0",
4+
"description": "Handle DEP0116 via transforming `url.parse` to `new URL()`",
5+
"type": "module",
6+
"scripts": {
7+
"test": "node --run test:import-process && node --run test:url-format && node --run test:url-parse",
8+
"test:import-process": "npx codemod jssg test -l typescript ./src/import-process.ts ./tests/ --filter import-process",
9+
"test:url-format": "npx codemod jssg test -l typescript ./src/url-format.ts ./tests/ --filter url-format",
10+
"test:url-parse": "npx codemod jssg test -l typescript ./src/url-parse.ts ./tests/ --filter url-parse"
11+
},
12+
"repository": {
13+
"type": "git",
14+
"url": "git+https://github.com/nodejs/userland-migrations.git",
15+
"directory": "recipes/node-url-to-whatwg-url",
16+
"bugs": "https://github.com/nodejs/userland-migrations/issues"
17+
},
18+
"author": "Augustin Mauroy",
19+
"license": "MIT",
20+
"homepage": "https://github.com/nodejs/userland-migrations/tree/main/node-url-to-whatwg-url#readme",
21+
"devDependencies": {
22+
"@codemod.com/jssg-types": "^1.0.3"
23+
}
24+
}

0 commit comments

Comments
 (0)