@carno.js/core 1.2.0 → 1.3.1

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@carno.js/core",
3
- "version": "1.2.0",
3
+ "version": "1.3.1",
4
4
  "description": "Ultra-fast HTTP framework with aggressive AOT/JIT compilation",
5
5
  "main": "src/index.ts",
6
6
  "types": "src/index.ts",
@@ -21,5 +21,5 @@
21
21
  "esbuild-fix-imports-plugin": "^1.0.23",
22
22
  "tsup": "^8.5.1"
23
23
  },
24
- "gitHead": "39c9361e3ffaab51d2d1a40515eafb3e15c12f8c"
24
+ "gitHead": "613c7356a23de92c15fa14640839a514587c64e1"
25
25
  }
package/src/Carno.ts CHANGED
@@ -84,6 +84,10 @@ export class Carno {
84
84
  private validator: ValidatorAdapter | null = null;
85
85
  private server: any;
86
86
 
87
+ // WebSocket support
88
+ _wsHandlerBuilder: ((container: Container) => any) | null = null;
89
+ _wsUpgradePaths: Set<string> = new Set();
90
+
87
91
  // Cached lifecycle event flags - checked once at startup
88
92
  private hasInitHooks = false;
89
93
  private hasBootHooks = false;
@@ -117,7 +121,7 @@ export class Carno {
117
121
 
118
122
  /**
119
123
  * Use a Carno plugin.
120
- * Imports controllers, services, middlewares, and routes from another Carno instance.
124
+ * Imports controllers, services, middlewares, routes, and WebSocket config from another Carno instance.
121
125
  */
122
126
  use(plugin: Carno): this {
123
127
  // Import controllers from plugin
@@ -165,9 +169,36 @@ export class Carno {
165
169
  }
166
170
  }
167
171
 
172
+ // Import WebSocket handler builder and upgrade paths
173
+ if (plugin._wsHandlerBuilder) {
174
+ this._wsHandlerBuilder = plugin._wsHandlerBuilder;
175
+ for (const path of plugin._wsUpgradePaths) {
176
+ this._wsUpgradePaths.add(path);
177
+ }
178
+ }
179
+
180
+ return this;
181
+ }
182
+
183
+ /**
184
+ * Register a WebSocket handler builder and the upgrade paths.
185
+ * Used internally by @carno.js/websocket plugin.
186
+ */
187
+ wsHandler(builder: (container: Container) => any, upgradePaths: string[]): this {
188
+ this._wsHandlerBuilder = builder;
189
+ for (const path of upgradePaths) {
190
+ this._wsUpgradePaths.add(path);
191
+ }
168
192
  return this;
169
193
  }
170
194
 
195
+ /**
196
+ * Returns the underlying Bun server instance (available after listen()).
197
+ */
198
+ getServer(): any {
199
+ return this.server;
200
+ }
201
+
171
202
  private findServiceInPlugin(plugin: Carno, exported: any): any | undefined {
172
203
  return plugin._services.find(
173
204
  s => this.getServiceToken(s) === this.getServiceToken(exported)
@@ -284,6 +315,32 @@ export class Carno {
284
315
  }
285
316
  };
286
317
 
318
+ // Wire in WebSocket support if a handler builder was registered
319
+ if (this._wsHandlerBuilder && this._wsUpgradePaths.size > 0) {
320
+ config.websocket = this._wsHandlerBuilder(this.container);
321
+
322
+ const upgradePaths = this._wsUpgradePaths;
323
+ const fallback = this.handleNotFound.bind(this);
324
+
325
+ config.fetch = (req: Request, server: any) => {
326
+ const pathname = new URL(req.url).pathname;
327
+
328
+ if (upgradePaths.has(pathname)) {
329
+ const upgraded = server.upgrade(req, {
330
+ data: {
331
+ id: crypto.randomUUID(),
332
+ namespace: pathname,
333
+ },
334
+ });
335
+
336
+ if (upgraded) return;
337
+ return new Response('WebSocket upgrade failed', { status: 400 });
338
+ }
339
+
340
+ return fallback(req);
341
+ };
342
+ }
343
+
287
344
  this.server = Bun.serve(config);
288
345
 
289
346
  // Execute BOOT hooks after server is ready
@@ -313,6 +370,12 @@ export class Carno {
313
370
  useValue: this.container
314
371
  });
315
372
 
373
+ // Register this Carno instance so services can inject it (e.g. RoomManager)
374
+ this.container.register({
375
+ token: Carno,
376
+ useValue: this
377
+ });
378
+
316
379
  // Always register CacheService (Memory by default)
317
380
  const cacheConfig = typeof this.config.cache === 'object' ? this.config.cache : {};
318
381
  this.container.register({