facebook / metro

🚇 The JavaScript bundler for React Native
https://metrobundler.dev
MIT License
5.24k stars 626 forks source link

Add server_listening reporter event with actually-bound port and address #1274

Closed robhogan closed 6 months ago

robhogan commented 6 months ago

Summary

Add a server_listening reporter event. This serves two purposes:

Expose actually-bound port and address when input configuration is more ambiguous

Metro may already be configured to start on an ephemeral (OS-assigned) port, if configured with --port 0, however Metro doesn't report or expose which port was actually assigned - the initialize_done reporter event repeats the configured port.

(h/t @kmagiera who let us know about this at React Conf ⚛️)

Similarly, when Metro is configured to start on localhost, it doesn't expose whether it binds to IPv6 (potentially dual stack) or (exclusively) IPv4.

Report when Metro is first ready to accept HTTP requests

Metro may be listening before the transformer is fully initialised or the file crawl is complete - but this is safe, because request handlers wait for bundler readiness. We may in future start listening earlier in the startup process so that Metro is available to at least start queueing requests as soon as possible.

Integrators may use server_listening as an explicit signal that Metro is ready to accept http requests, even though it may not be fully warm.

Changelog

 - **[Feature]**: Add `server_listening` reporter event, exposing bound port, address and family.

Test plan

With console.log(event) added to TerminalReporter for server_listening:

yarn start serve --port 0
yarn run v1.22.19
$ node packages/metro/src/cli serve --port 0

                        ▒▒▓▓▓▓▒▒
                     ▒▓▓▓▒▒░░▒▒▓▓▓▒
                  ▒▓▓▓▓░░░▒▒▒▒░░░▓▓▓▓▒
                 ▓▓▒▒▒▓▓▓▓▓▓▓▓▓▓▓▓▒▒▒▓▓
                 ▓▓░░░░░▒▓▓▓▓▓▓▒░░░░░▓▓
                 ▓▓░░▓▓▒░░░▒▒░░░▒▓▒░░▓▓
                 ▓▓░░▓▓▓▓▓▒▒▒▒▓▓▓▓▒░░▓▓
                 ▓▓░░▓▓▓▓▓▓▓▓▓▓▓▓▓▒░░▓▓
                 ▓▓▒░░▒▒▓▓▓▓▓▓▓▓▒░░░▒▓▓
                  ▒▓▓▓▒░░░▒▓▓▒░░░▒▓▓▓▒
                     ▒▓▓▓▒░░░░▒▓▓▓▒
                        ▒▒▓▓▓▓▒▒

{
  type: 'server_listening',
  address: '::1',
  port: 57182,
  family: 'IPv6'
}
                Welcome to Metro v0.80.9
              Fast - Scalable - Integrated
facebook-github-bot commented 6 months ago

@robhogan has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

codecov-commenter commented 6 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 83.83%. Comparing base (57607bf) to head (6bd5d2b).

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #1274 +/- ## ======================================= Coverage 83.83% 83.83% ======================================= Files 207 207 Lines 10819 10821 +2 Branches 2702 2702 ======================================= + Hits 9070 9072 +2 Misses 1749 1749 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

facebook-github-bot commented 6 months ago

@robhogan merged this pull request in facebook/metro@6856d00cfdddc1dd5ed9ab35249fe7ca1610ca75.