-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Description
Bug: colors is not defined when using --websocket flag
When attempting to use the --websocket flag without the --proxy flag, the server crashes with a ReferenceError: colors is not defined error.
Environment Versions
- OS Type: Windows 11
- Node version:
v22.17.0(or runnode --version) - http-server version:
v14.1.2(master branch, unreleased)
Steps to reproduce
-
Clone the repository and install dependencies:
git clone https://github.com/http-party/http-server.git cd http-server npm install -
Run http-server with the
--websocketflag but without--proxy:node bin/http-server --websocket
-
Observe the crash
Expected result
The server should start and display a warning message:
WebSocket proxy will not be enabled because proxy is not enabled
Then continue running normally.
Actual result
The server crashes immediately with:
C:\Users\Harsh\V0 Builds\express\http-server\bin\http-server:233
logger.warning(colors.yellow('WebSocket proxy will not be enabled because proxy is not enabled'));
^
ReferenceError: colors is not defined
at listen (C:\Users\Harsh\V0 Builds\express\http-server\bin\http-server:233:22)
at C:\Users\Harsh\V0 Builds\express\http-server\bin\http-server:185:5
...
also, warning is not defined as well for logger:
Root Cause
There are two bugs on line 233 in bin/http-server:
- Undefined variable: The code references
colorswhich is never imported. The file importschalkinstead (line 5). - Undefined method: The code calls
logger.warning()which doesn't exist. The logger object only hasinfoandrequestmethods (defined at lines 138-171).
Current code (line 233):
logger.warning(
colors.yellow(
"WebSocket proxy will not be enabled because proxy is not enabled",
),
);Should be:
logger.info(
chalk.yellow(
"WebSocket proxy will not be enabled because proxy is not enabled",
),
);Additional Bug: Same issue in error handler
There's also a similar bug in the nopanic error handler (lines 115-118) where colors is used instead of chalk:
console.log(colors.green(etime));
console.log(`${colors.red("Fatal error: ")}${e.code}: ${e.message}`);
console.log(colors.bold(`Check ${filename} file in this folder.`));All three instances should use chalk instead of colors.
Other information
- The bug is currently only in the
masterbranch and has not been released to npm yet - This affects the unreleased websocket proxy feature
- The fix is simple: replace
colorswithchalkandlogger.warningwithlogger.info
i was going through the codebase- just curios on how it works and found it!