Skip to content

replaced deprecated url.parse with URL in parseOptionsFromUrl#1548

Closed
vzaidman wants to merge 3 commits into
react:mainfrom
vzaidman:export-D78890065
Closed

replaced deprecated url.parse with URL in parseOptionsFromUrl#1548
vzaidman wants to merge 3 commits into
react:mainfrom
vzaidman:export-D78890065

Conversation

@vzaidman

@vzaidman vzaidman commented Aug 7, 2025

Copy link
Copy Markdown
Contributor

Summary:
Gets rid of the deprecated url.parse in favor of URL in parseOptionsFromUrl.

Changelog: [Fixed] Replaced the deprecated url.parse with URL and correctly encode and decode source urls and source map urls

Differential Revision: D78890065

vzaidman and others added 2 commits August 7, 2025 07:40
Summary:
In a codebase with non-ascii characters in file paths (e.g. Japanese or Chinese characters), metro has an error when serving bundles:

```
TypeError: Invalid character in header content
```

This was reported in the Expo project expo/expo#27397 but it's not Expo code and was later closed.

The problem is fairly simple—HTTP headers need to use ASCII characters only, but filesystems are not limited to ASCII so trying to put a filesystem path directly into a HTTP header value is going to cause a problem.

The solution also seems relatively simple—URL encode the path so that it uses percent encoding of non-ASCII characters.

As suggested in https://velog.io/demian/RN-%EC%98%A4%EB%A5%98-%ED%95%B4%EA%B2%B0-TypeError-Invalid-character-in-header-content-X-React-Native-Project-Root, some people were  adding the following middleware to fix that issue:
```
function createMetroStatusMiddleware(metroConfig) {
    return (_req, res)=>{
        res.setHeader("X-React-Native-Project-Root", new URL(metroConfig.projectRoot));
        res.end("packager-status:running");
    };
}
```
Instead, I went to the root cause, and improved the parsing of the URL for both `sourceUrl` and `sourceMapUrl` fixing both the issue with serving bundles and the issue with serving source maps.

> **_NOTE:_**  this problem stems from using the [deprecated `url.parse` instead of the newer URL](https://nodejs.org/api/url.html#urlparseurlstring-parsequerystring-slashesdenotehost).

Changelog: [General][Fixed] Metro supporting non-ASCII bundle/sourcemaps paths

Pull Request resolved: react#1538

Test Plan:
Using a test diff D78806803 I managed to reproduce Metro not launching RNTester before this diff.

1. `buck2 install -r rntester-android` (with the modified bundle path)
2. `DEV=1 js1 run`
3. launch `RNTester`

Before- app crashes:
 https://pxl.cl/7LFkb

After- app launches, debugging works:
{F1980565661}

can even add the bundle to ignore-list and then it gets ignored:
{F1980565660}

Differential Revision: D78799358
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Aug 7, 2025
@facebook-github-bot

Copy link
Copy Markdown
Contributor

This pull request was exported from Phabricator. Differential Revision: D78890065

…eact#1548)

Summary:
Pull Request resolved: react#1548

Gets rid of the deprecated [`url.parse`](https://nodejs.org/api/url.html#urlparseurlstring-parsequerystring-slashesdenotehost) in favor of `URL` in `parseOptionsFromUrl`.

Changelog: [Fixed] Replaced the deprecated `url.parse` with `URL` and correctly encode and decode source urls and source map urls

Differential Revision: D78890065
@facebook-github-bot

Copy link
Copy Markdown
Contributor

This pull request was exported from Phabricator. Differential Revision: D78890065

@facebook-github-bot

Copy link
Copy Markdown
Contributor

This pull request has been merged in 71e158a.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. fb-exported Merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants