Repository navigation
Move /status into community-cli-plugin #59662
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
+91
−5
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
packages/community-cli-plugin/src/dev-server/__tests__/statusMiddleware-test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| /** | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| * | ||
| * @flow strict-local | ||
| * @format | ||
| */ | ||
|
|
||
| import statusMiddleware from '../statusMiddleware'; | ||
| import connect from 'connect'; | ||
| import http from 'node:http'; | ||
|
|
||
| jest.useRealTimers(); | ||
|
|
||
| async function fetchStatus(projectRoot: string): Promise<Response> { | ||
| const app = connect().use('/status', statusMiddleware({projectRoot})); | ||
| const server = http.createServer(app); | ||
| await new Promise(resolve => server.listen(0, 'localhost', resolve)); | ||
| try { | ||
| const address = server.address(); | ||
| if (address == null || typeof address === 'string') { | ||
| throw new Error('Expected the server to listen on a TCP port'); | ||
| } | ||
| return await fetch(`http://localhost:${address.port}/status`); | ||
| } finally { | ||
| server.closeAllConnections(); | ||
| await new Promise(resolve => server.close(() => resolve())); | ||
| } | ||
| } | ||
|
|
||
| describe('/status', () => { | ||
| // React Native apps and `isDevServerRunning` depend on this exact response. | ||
| test('reports that the packager is running for the project root', async () => { | ||
| const resp = await fetchStatus('/mocked/path'); | ||
|
|
||
| expect(resp.status).toBe(200); | ||
| expect(await resp.text()).toBe('packager-status:running'); | ||
| expect(resp.headers.get('X-React-Native-Project-Root')).toBe( | ||
| '/mocked/path', | ||
| ); | ||
| expect(resp.headers.get('Cache-Control')).toBe('no-store'); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
packages/community-cli-plugin/src/dev-server/statusMiddleware.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| /** | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| * | ||
| * @flow strict-local | ||
| * @format | ||
| */ | ||
|
|
||
| import type {NextHandleFunction} from 'connect'; | ||
|
|
||
| type Options = Readonly<{ | ||
| projectRoot: string, | ||
| }>; | ||
|
|
||
| /** | ||
| * Report that this dev server is running for `projectRoot`, so that the app | ||
| * and `isDevServerRunning` can tell it from another process on the port. | ||
| */ | ||
| export default function statusMiddleware({ | ||
| projectRoot, | ||
| }: Options): NextHandleFunction { | ||
| const projectRootHeader = new URL(`file:///${projectRoot}`).pathname.slice(1); | ||
|
|
||
| return (req, res) => { | ||
| res.setHeader('X-React-Native-Project-Root', projectRootHeader); | ||
| res.setHeader('Cache-Control', 'no-store'); | ||
| res.end('packager-status:running'); | ||
| }; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if this is a new bug but this PR at least moves it
This correctly URL-encodes the header value:
Whereas
isDevServerRunningL40 comparesprojectRootto the header without decoding it:https://github.com/react/react-native/pull/59662/changes#diff-03f42a7eeca678002da6426eba661a1fee15e67e45200ed4debad1498530c26aR40
So
isDevServerRunning()will return false for anything with a space (etc) in it. Worth a bit of test coverage for non-URL-safeprojectRoots