feat: make PORT and BASE_URL configurable via environment variables#140
feat: make PORT and BASE_URL configurable via environment variables#140godtail wants to merge 2 commits intohusamql3:mainfrom
Conversation
🤖 Generated with [Pochi](https://getpochi.com) | [Task](https://app.getpochi.com/share/p-126df6bf043248c79c8b4810af9f6662) Co-Authored-By: Pochi <noreply@getpochi.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughThe defaults configuration in Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~5 minutes 🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@packages/shared/src/constants/defaults.ts`:
- Line 7: The BASE_URL constant currently uses the nullish coalescing operator
with process.env.BASE_URL so empty or whitespace-only strings bypass the
fallback; update the logic that sets BASE_URL (the expression using
process.env.BASE_URL and PORT) to treat empty/whitespace values as unset by
trimming the env value first and using a logical OR fallback to the
`http://localhost:${PORT}` URL instead of `??`, i.e. call trim() on
process.env.BASE_URL and fall back when the trimmed result is an empty string.
- Line 1: The PORT constant currently uses parseInt(process.env.PORT, 10) which
can produce NaN for invalid inputs; change the initialization to parse and
validate the environment value: trim and parse process.env.PORT as base 10,
check Number.isInteger(parsed) && parsed >= 1 && parsed <= 65535, and only then
assign it to PORT; otherwise fall back to the default 3333. Update the PORT
declaration (symbol: PORT) so the value is a validated TCP port number to avoid
binding failures at runtime.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: ed72b798-f443-48e0-b655-565ce724a204
📒 Files selected for processing (1)
packages/shared/src/constants/defaults.ts
| @@ -1,8 +1,10 @@ | |||
| const PORT = process.env.PORT ? parseInt(process.env.PORT, 10) : 3333; | |||
There was a problem hiding this comment.
🧩 Analysis chain
🌐 Web query:
In JavaScript, what does parseInt("abc", 10) return, and what is the recommended way to validate a TCP port from an environment variable?
💡 Result:
parseInt("abc", 10) returns NaN. The recommended way to validate a TCP port from an environment variable in Node.js is: const portEnv = process.env.PORT; const port = parseInt(portEnv, 10); if (isNaN(port) || port < 1 || port > 65535) { // Invalid port, use default or throw error throw new Error(Invalid PORT: ${portEnv}); } This checks that the parsed value is a valid number between 1 and 65535 (valid TCP port range).
Citations:
- 1: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt.
- 2: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt
- 3: https://stackoverflow.com/questions/18864677/what-is-process-env-port-in-node-js
- 4: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number
- 5: https://stackoverflow.com/questions/21060645/js-validation-ipport
🏁 Script executed:
cat -n packages/shared/src/constants/defaults.ts | head -20Repository: husamql3/db-studio
Length of output: 492
Harden PORT parsing to avoid NaN and invalid ports.
process.env.PORT may contain non-numeric values (e.g., "abc"), which parse to NaN via parseInt(). This breaks server binding at runtime. Add validation to ensure the port is a valid TCP port (1–65535), otherwise use the default.
Proposed fix
-const PORT = process.env.PORT ? parseInt(process.env.PORT, 10) : 3333;
+const rawPort = process.env.PORT?.trim();
+const parsedPort = rawPort ? Number(rawPort) : NaN;
+const PORT =
+ Number.isInteger(parsedPort) && parsedPort >= 1 && parsedPort <= 65535
+ ? parsedPort
+ : 3333;🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@packages/shared/src/constants/defaults.ts` at line 1, The PORT constant
currently uses parseInt(process.env.PORT, 10) which can produce NaN for invalid
inputs; change the initialization to parse and validate the environment value:
trim and parse process.env.PORT as base 10, check Number.isInteger(parsed) &&
parsed >= 1 && parsed <= 65535, and only then assign it to PORT; otherwise fall
back to the default 3333. Update the PORT declaration (symbol: PORT) so the
value is a validated TCP port number to avoid binding failures at runtime.
🤖 Generated with [Pochi](https://getpochi.com) | [Task](https://app.getpochi.com/share/p-126df6bf043248c79c8b4810af9f6662) Co-Authored-By: Pochi <noreply@getpochi.com>
|
Thanks a lot @godtail for your contribution ❤️ I'm very sick today, I'll review it as soon as I can Appreciated |
|
Hi @godtail I've checked your PR. I'm getting Plus, you have to update when it's updated please re-request me to review it |
Summary
PORTconfigurable viaprocess.env.PORTwith a fallback to3333.BASE_URLconfigurable viaprocess.env.BASE_URLwith a fallback tohttp://localhost:${PORT}.🤖 Generated with Pochi | Task
Summary by CodeRabbit