From 57c2180cd1593eedca058b407dfc493b908770ec Mon Sep 17 00:00:00 2001 From: szymonrybczak Date: Fri, 30 Aug 2024 13:46:47 +0200 Subject: [PATCH] fix: invalid character in header --- .../__tests__/statusPageMiddleware.test.ts | 19 +++++++++++++++++++ .../src/statusPageMiddleware.ts | 5 ++++- packages/cli-tools/src/getNextPort.ts | 5 ++++- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/packages/cli-server-api/src/__tests__/statusPageMiddleware.test.ts b/packages/cli-server-api/src/__tests__/statusPageMiddleware.test.ts index 3c783a948..3a785b256 100644 --- a/packages/cli-server-api/src/__tests__/statusPageMiddleware.test.ts +++ b/packages/cli-server-api/src/__tests__/statusPageMiddleware.test.ts @@ -20,4 +20,23 @@ describe('statusPageMiddleware', () => { ); expect(res.end).toHaveBeenCalledWith('packager-status:running'); }); + + it('should set headers and end the response with invalid characters', () => { + process.cwd = () => '/привіт/path'; + + const res: http.ServerResponse = { + setHeader: jest.fn(), + end: jest.fn(), + } as any; + + const mockReq: http.IncomingMessage = {} as any; + statusPageMiddleware(mockReq, res); + + // We're strictly checking response here, because React Native is strongly depending on this response. Changing the response might be a breaking change. + expect(res.setHeader).toHaveBeenCalledWith( + 'X-React-Native-Project-Root', + new URL(`file:///${process.cwd()}`).pathname.slice(1), + ); + expect(res.end).toHaveBeenCalledWith('packager-status:running'); + }); }); diff --git a/packages/cli-server-api/src/statusPageMiddleware.ts b/packages/cli-server-api/src/statusPageMiddleware.ts index 3e98f2b2b..b7d690752 100644 --- a/packages/cli-server-api/src/statusPageMiddleware.ts +++ b/packages/cli-server-api/src/statusPageMiddleware.ts @@ -14,6 +14,9 @@ export default function statusPageMiddleware( _req: http.IncomingMessage, res: http.ServerResponse, ) { - res.setHeader('X-React-Native-Project-Root', process.cwd()); + res.setHeader( + 'X-React-Native-Project-Root', + new URL(`file:///${process.cwd()}`).pathname.slice(1), + ); res.end('packager-status:running'); } diff --git a/packages/cli-tools/src/getNextPort.ts b/packages/cli-tools/src/getNextPort.ts index 374fe13c7..be4a5a4d0 100644 --- a/packages/cli-tools/src/getNextPort.ts +++ b/packages/cli-tools/src/getNextPort.ts @@ -19,7 +19,10 @@ const getNextPort = async (port: number, root: string): Promise => { const isRunning = typeof result === 'object' && result.status === 'running'; - if (isRunning && result.root === root) { + if ( + isRunning && + result.root === new URL(`file:///${root}`).pathname.slice(1) + ) { // Found running bundler for this project, so we do not need to start packager! start = false; } else if (isRunning || result === 'unrecognized') {