Skip to content

Commit

Permalink
fix: invalid character in header
Browse files Browse the repository at this point in the history
  • Loading branch information
szymonrybczak committed Aug 30, 2024
1 parent a99b27e commit 57c2180
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
19 changes: 19 additions & 0 deletions packages/cli-server-api/src/__tests__/statusPageMiddleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
5 changes: 4 additions & 1 deletion packages/cli-server-api/src/statusPageMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
5 changes: 4 additions & 1 deletion packages/cli-tools/src/getNextPort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ const getNextPort = async (port: number, root: string): Promise<Result> => {

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') {
Expand Down

0 comments on commit 57c2180

Please sign in to comment.