@mantiq/core 0.1.0 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/http/Kernel.ts +28 -8
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mantiq/core",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "Service container, router, middleware, HTTP kernel, config, and exception handler",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -133,17 +133,37 @@ export class HttpKernel {
133
133
  */
134
134
  async start(): Promise<void> {
135
135
  const config = this.container.make(ConfigRepository)
136
- const port = config.get<number>('app.port', 3000)
136
+ const preferredPort = config.get<number>('app.port', 3000)
137
137
  const hostname = config.get<string>('app.host', '0.0.0.0')
138
138
 
139
- Bun.serve({
140
- port,
141
- hostname,
142
- fetch: (req, server) => this.handle(req, server),
143
- websocket: this.wsKernel.getBunHandlers(),
144
- })
139
+ let port = preferredPort
140
+ const maxAttempts = 20
145
141
 
146
- console.log(`Server running at http://${hostname === '0.0.0.0' ? 'localhost' : hostname}:${port}`)
142
+ for (let attempt = 0; attempt < maxAttempts; attempt++) {
143
+ try {
144
+ Bun.serve({
145
+ port,
146
+ hostname,
147
+ fetch: (req, server) => this.handle(req, server),
148
+ websocket: this.wsKernel.getBunHandlers(),
149
+ })
150
+
151
+ const display = hostname === '0.0.0.0' ? 'localhost' : hostname
152
+ if (port !== preferredPort) {
153
+ console.log(`Port ${preferredPort} in use, using ${port} instead`)
154
+ }
155
+ console.log(`Server running at http://${display}:${port}`)
156
+ return
157
+ } catch (err: any) {
158
+ if (err?.code === 'EADDRINUSE') {
159
+ port++
160
+ continue
161
+ }
162
+ throw err
163
+ }
164
+ }
165
+
166
+ throw new Error(`No available port found (tried ${preferredPort}–${port - 1})`)
147
167
  }
148
168
 
149
169
  // ── Private ───────────────────────────────────────────────────────────────