Skip to content
Open
Show file tree
Hide file tree
Changes from 52 commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
cdcdffe
fea(`node-url-to-whatwg-url`): scaffold codemod
AugustinMauroy Aug 3, 2025
3addfb3
url-parse(`node-url-to-whatwg-url`): setup `url-parse`
AugustinMauroy Aug 3, 2025
04174b8
WIP
AugustinMauroy Aug 3, 2025
4249d8e
url-parse(`node-url-to-whatwg-url`): introduce
AugustinMauroy Aug 12, 2025
873775a
clean
AugustinMauroy Aug 12, 2025
4cf1c36
WIP
AugustinMauroy Aug 12, 2025
ab18017
fix: ts
AugustinMauroy Aug 12, 2025
4b60d03
fix: windows
AugustinMauroy Aug 12, 2025
b289b2d
use resolve utility
AugustinMauroy Aug 12, 2025
50193e0
add more supported case for `url-format`
AugustinMauroy Aug 12, 2025
264a1f2
test: add more case
AugustinMauroy Aug 12, 2025
c60c073
add support of semicolon
AugustinMauroy Aug 12, 2025
c5ce549
Update package.json
AugustinMauroy Aug 12, 2025
cedf9bc
Revert "Update package.json"
AugustinMauroy Aug 12, 2025
0adc023
update
AugustinMauroy Aug 12, 2025
599fff4
fea(`node-url-to-whatwg-url`): scaffold codemod
AugustinMauroy Aug 3, 2025
a4ff1db
url-parse(`node-url-to-whatwg-url`): setup `url-parse`
AugustinMauroy Aug 3, 2025
4aeffdd
WIP
AugustinMauroy Aug 3, 2025
0b1f00a
url-parse(`node-url-to-whatwg-url`): introduce
AugustinMauroy Aug 12, 2025
729f5f8
clean
AugustinMauroy Aug 12, 2025
0914a53
WIP
AugustinMauroy Aug 12, 2025
d8debdf
fix: ts
AugustinMauroy Aug 12, 2025
b431aa4
fix: windows
AugustinMauroy Aug 12, 2025
25f7df2
use resolve utility
AugustinMauroy Aug 12, 2025
36f3b79
add more supported case for `url-format`
AugustinMauroy Aug 12, 2025
821e3ee
test: add more case
AugustinMauroy Aug 12, 2025
0713b1e
add support of semicolon
AugustinMauroy Aug 12, 2025
4b06973
Update package.json
AugustinMauroy Aug 12, 2025
9eb2c0d
Revert "Update package.json"
AugustinMauroy Aug 12, 2025
b893428
update
AugustinMauroy Aug 12, 2025
7c45240
Merge branch 'feat(`node-url-to-whatwg-url`)' of https://github.com/n…
AugustinMauroy Aug 12, 2025
3694442
update logic
AugustinMauroy Aug 12, 2025
e571ce7
Update url-format.ts
AugustinMauroy Aug 12, 2025
d240d5a
Update url-parse.ts
AugustinMauroy Aug 12, 2025
c128063
chore: remove `@next` and clean (#176)
AugustinMauroy Aug 12, 2025
9467f6c
fea(`node-url-to-whatwg-url`): scaffold codemod
AugustinMauroy Aug 3, 2025
4430f7f
url-parse(`node-url-to-whatwg-url`): setup `url-parse`
AugustinMauroy Aug 3, 2025
01247a7
WIP
AugustinMauroy Aug 3, 2025
e1b3415
url-parse(`node-url-to-whatwg-url`): introduce
AugustinMauroy Aug 12, 2025
68fe603
clean
AugustinMauroy Aug 12, 2025
1c6a884
WIP
AugustinMauroy Aug 12, 2025
ac7b540
use resolve utility
AugustinMauroy Aug 12, 2025
4bccf85
update
AugustinMauroy Aug 12, 2025
0e4799e
update logic
AugustinMauroy Aug 12, 2025
587e97d
Update url-format.ts
AugustinMauroy Aug 12, 2025
02fb501
Update url-parse.ts
AugustinMauroy Aug 12, 2025
245ca03
chore: remove `@next` and clean (#176)
AugustinMauroy Aug 12, 2025
6fdb711
Merge branch 'feat(`node-url-to-whatwg-url`)' of https://github.com/n…
AugustinMauroy Aug 13, 2025
81f421f
Update package-lock.json
AugustinMauroy Aug 13, 2025
841d576
Update url-format.ts
AugustinMauroy Aug 13, 2025
44ca92d
Merge branch 'main' into feat(`node-url-to-whatwg-url`)
AugustinMauroy Aug 17, 2025
c8ff01a
improve
AugustinMauroy Aug 17, 2025
ca7dadf
update from feedback
AugustinMauroy Aug 20, 2025
6601add
Update url-format.ts
AugustinMauroy Aug 20, 2025
328fadd
simplify
AugustinMauroy Aug 20, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion biome.jsonc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "./node_modules/@biomejs/biome/configuration_schema.json",
"files": {
"includes": ["**", "!**/*.snap.cjs", "!**/fixtures/**", "!**/expected/**", "!**/input/**"]
"includes": ["**", "!**/*.snap.cjs", "!**/fixtures/**", "!**/tests/**"]
},
"assist": { "actions": { "source": { "organizeImports": "off" } } },
// Rules for the linter
Expand Down
78 changes: 45 additions & 33 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

84 changes: 84 additions & 0 deletions recipes/node-url-to-whatwg-url/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# Node.js URL to WHATWG URL

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.

See [DEP0116](https://nodejs.org/api/deprecations.html#DEP0116).

## Example

### `url.parse` to `new URL()`

**Before:**
```js
const url = require('node:url');

const myURL = url.parse('https://example.com/path?query=string#hash');

const { auth } = myURL;
const urlAuth = myURL.auth;

const { path } = myURL;
const urlPath = myURL.path;

const { hostname } = myURL;
const urlHostname = myURL.hostname;
```

**After:**
```js
const myURL = new URL('https://example.com/path?query=string#hash');

const auth = `${myURL.username}:${myURL.password}`;
const urlAuth = `${myURL.username}:${myURL.password}`;

const path = `${myURL.pathname}${myURL.search}`;
const urlPath = `${myURL.pathname}${myURL.search}`;

const hostname = myURL.hostname.replace(/^\[|\]$/, '');
const urlHostname = myURL.hostname.replace(/^\[|\]$/, '');
```

### `url.format` to `myUrl.toString()

**Before:**
```js
const url = require('node:url');

url.format({
protocol: 'https',
hostname: 'example.com',
pathname: '/some/path',
query: {
page: 1,
format: 'json',
},
});
```

**After:**
```js
const myUrl = new URL('https://example.com/some/path?page=1&format=json').toString();
```

## Caveats

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.

```js
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'
```

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.

21 changes: 21 additions & 0 deletions recipes/node-url-to-whatwg-url/codemod.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
schema_version: "1.0"
name: "@nodejs/node-url-to-whatwg-url"
version: 1.0.0
description: Handle DEP0116 via transforming `url.parse` to `new URL()`
author: Augustin Mauroy
license: MIT
workflow: workflow.yaml
category: migration

targets:
languages:
- javascript
- typescript

keywords:
- transformation
- migration

registry:
access: public
visibility: public
24 changes: 24 additions & 0 deletions recipes/node-url-to-whatwg-url/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "@nodejs/node-url-to-whatwg-url",
"version": "1.0.0",
"description": "Handle DEP0116 via transforming `url.parse` to `new URL()`",
"type": "module",
"scripts": {
"test": "node --run test:import-process && node --run test:url-format && node --run test:url-parse",
"test:import-process": "npx codemod jssg test -l typescript ./src/import-process.ts ./tests/ --filter import-process",
"test:url-format": "npx codemod jssg test -l typescript ./src/url-format.ts ./tests/ --filter url-format",
"test:url-parse": "npx codemod jssg test -l typescript ./src/url-parse.ts ./tests/ --filter url-parse"
},
"repository": {
"type": "git",
"url": "git+https://github.com/nodejs/userland-migrations.git",
"directory": "recipes/node-url-to-whatwg-url",
"bugs": "https://github.com/nodejs/userland-migrations/issues"
},
"author": "Augustin Mauroy",
"license": "MIT",
"homepage": "https://github.com/nodejs/userland-migrations/tree/main/node-url-to-whatwg-url#readme",
"devDependencies": {
"@codemod.com/jssg-types": "^1.0.3"
}
}
Loading
Loading