callstack / callstack/haul

Haul server crashes when navigating to root url

Open
#767 0 comments 1 reaction 0 assignees View on GitHub
Dominant language
TypeScript
Stars
3.7k
Forks
185
PR merge metrics
No merged PRs in 30d

Description

## Environment

I am on Windows and node v14.15.1, but it looks like this should apply everywhere

## Description

When you run haul, you get the following message: `done ▶︎ Packager server running on http://localhost:8081`
You can click on the link from the console, which opens localhost at the root
This causes haul to crash

I see three places to change that would each mitigate this

1. Lowest level

```ts
callback({
errors: null,
platform,
file: fs.readFileSync(filePath),
mimeType,
});
```
https://github.com/callstack/haul/blob/dc0b201a0355c0e1c0be897aa9e4586c7e9b2e89/packages/haul-core-legacy/src/compiler/Compiler.js#L149

There needs to be error handling when reading a file. A simple `try/catch` that invokes `callback` with any errors would suffice

2. Intermediate level

```ts
if (fs.existsSync(filePath)) {
send(Events.FILE_RECEIVED, {
taskId: payload.taskId,
filePath,
mimeType: mime.lookup(payload.filename) || 'text/javascript',
});
} else {
send(Events.FILE_NOT_FOUND, {
taskId: payload.taskId,
});
}
```
https://github.com/callstack/haul/blob/dc0b201a0355c0e1c0be897aa9e4586c7e9b2e89/packages/haul-core-legacy/src/compiler/worker/initWorker.js#L82

`existsSync` returns `true` for directories, but there is no reason to send a directory path to the compiler. A check to ensure that the path is a file (e.g. with a call to `statSync`) would prevent this issue

3. Highest level

```ts
server.route({
method: 'GET',
path: '/{any*}',
...
})
```

https://github.com/callstack/haul/blob/dc0b201a0355c0e1c0be897aa9e4586c7e9b2e89/packages/haul-core/src/server/setupCompilerRoutes.ts#L46

This sends every unknown path to the compiler, which is not desirable for the root

Adding a simple redirect like the following to [setupDevtoolRoutes.ts](https://github.com/callstack/haul/blob/dc0b201a0355c0e1c0be897aa9e4586c7e9b2e89/packages/haul-core/src/server/setupDevtoolRoutes.ts#L105) would catch this and help developers get to their expected destination

```ts
server.route({
method: 'GET',
path: '/',
handler: (_, h) => h.redirect('/debugger-ui')
});
```

## Reproducible Demo

This applies to every haul project

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.