@onebun/core 0.1.23 → 0.1.24
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
|
@@ -145,7 +145,7 @@ describe('OneBunApplication', () => {
|
|
|
145
145
|
expect(config).toBeDefined();
|
|
146
146
|
});
|
|
147
147
|
|
|
148
|
-
test('should create config service when envSchema provided', () => {
|
|
148
|
+
test('should create config service when envSchema provided (duplicate check)', () => {
|
|
149
149
|
@Module({})
|
|
150
150
|
class TestModule {}
|
|
151
151
|
|
|
@@ -159,12 +159,9 @@ describe('OneBunApplication', () => {
|
|
|
159
159
|
|
|
160
160
|
const app = createTestApp(TestModule, { envSchema });
|
|
161
161
|
|
|
162
|
-
//
|
|
162
|
+
// Config service is created eagerly in the constructor
|
|
163
163
|
const config = app.getConfig();
|
|
164
164
|
expect(config).toBeDefined();
|
|
165
|
-
|
|
166
|
-
// The actual value access might need the config to be fully initialized
|
|
167
|
-
// which happens during runtime, not during construction
|
|
168
165
|
});
|
|
169
166
|
|
|
170
167
|
test('should provide typed access to config values via getConfig()', () => {
|
|
@@ -261,17 +258,34 @@ describe('OneBunApplication', () => {
|
|
|
261
258
|
});
|
|
262
259
|
|
|
263
260
|
describe('Layer methods', () => {
|
|
264
|
-
|
|
261
|
+
let originalServe: typeof Bun.serve;
|
|
262
|
+
|
|
263
|
+
beforeEach(() => {
|
|
264
|
+
originalServe = Bun.serve;
|
|
265
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
266
|
+
(Bun as any).serve = mock(() => ({
|
|
267
|
+
stop: mock(),
|
|
268
|
+
port: 3000,
|
|
269
|
+
}));
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
afterEach(() => {
|
|
273
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
274
|
+
(Bun as any).serve = originalServe;
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
test('should return layer from root module', async () => {
|
|
265
278
|
@Module({})
|
|
266
279
|
class TestModule {}
|
|
267
280
|
|
|
268
281
|
const app = createTestApp(TestModule);
|
|
282
|
+
await app.start();
|
|
269
283
|
|
|
270
284
|
const layer = app.getLayer();
|
|
271
285
|
expect(layer).toBeDefined();
|
|
272
286
|
});
|
|
273
287
|
|
|
274
|
-
test('should return layer for complex module structure', () => {
|
|
288
|
+
test('should return layer for complex module structure', async () => {
|
|
275
289
|
class TestController {}
|
|
276
290
|
class TestService {}
|
|
277
291
|
|
|
@@ -282,6 +296,7 @@ describe('OneBunApplication', () => {
|
|
|
282
296
|
class TestModule {}
|
|
283
297
|
|
|
284
298
|
const app = createTestApp(TestModule);
|
|
299
|
+
await app.start();
|
|
285
300
|
|
|
286
301
|
const layer = app.getLayer();
|
|
287
302
|
expect(layer).toBeDefined();
|
|
@@ -394,14 +409,13 @@ describe('OneBunApplication', () => {
|
|
|
394
409
|
});
|
|
395
410
|
|
|
396
411
|
describe('Module class handling', () => {
|
|
397
|
-
test('should throw error for plain class without decorator', () => {
|
|
412
|
+
test('should throw error for plain class without decorator', async () => {
|
|
398
413
|
class PlainModule {}
|
|
399
414
|
|
|
400
|
-
//
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
}).toThrow('Module PlainModule does not have @Module decorator');
|
|
415
|
+
// Module creation now happens in start(), so the error is thrown there
|
|
416
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
417
|
+
const app = createTestApp(PlainModule as any);
|
|
418
|
+
await expect(app.start()).rejects.toThrow('Module PlainModule does not have @Module decorator');
|
|
405
419
|
});
|
|
406
420
|
|
|
407
421
|
test('should handle class with constructor parameters', () => {
|
|
@@ -152,12 +152,14 @@ function resolveHost(explicitHost: string | undefined): string {
|
|
|
152
152
|
* OneBun Application
|
|
153
153
|
*/
|
|
154
154
|
export class OneBunApplication {
|
|
155
|
-
private rootModule: ModuleInstance;
|
|
155
|
+
private rootModule: ModuleInstance | null = null;
|
|
156
156
|
private server: ReturnType<typeof Bun.serve> | null = null;
|
|
157
157
|
private options: ApplicationOptions;
|
|
158
158
|
private logger: SyncLogger;
|
|
159
159
|
private config: IConfig<OneBunAppConfig>;
|
|
160
160
|
private configService: ConfigServiceImpl | null = null;
|
|
161
|
+
private moduleClass: new (...args: unknown[]) => object;
|
|
162
|
+
private loggerLayer: Layer.Layer<never, never, unknown>;
|
|
161
163
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
162
164
|
private metricsService: any = null;
|
|
163
165
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -176,6 +178,8 @@ export class OneBunApplication {
|
|
|
176
178
|
moduleClass: new (...args: unknown[]) => object,
|
|
177
179
|
options?: Partial<ApplicationOptions>,
|
|
178
180
|
) {
|
|
181
|
+
this.moduleClass = moduleClass;
|
|
182
|
+
|
|
179
183
|
// Resolve port and host with priority: explicit > env > default
|
|
180
184
|
this.options = {
|
|
181
185
|
port: resolvePort(options?.port),
|
|
@@ -194,7 +198,7 @@ export class OneBunApplication {
|
|
|
194
198
|
|
|
195
199
|
// Use provided logger layer, or create from options, or use default
|
|
196
200
|
// Priority: loggerLayer > loggerOptions > env variables > NODE_ENV defaults
|
|
197
|
-
|
|
201
|
+
this.loggerLayer = this.options.loggerLayer
|
|
198
202
|
?? (this.options.loggerOptions
|
|
199
203
|
? makeLoggerFromOptions(this.options.loggerOptions)
|
|
200
204
|
: makeLogger());
|
|
@@ -205,13 +209,14 @@ export class OneBunApplication {
|
|
|
205
209
|
Effect.map(LoggerService, (logger: Logger) =>
|
|
206
210
|
logger.child({ className: 'OneBunApplication' }),
|
|
207
211
|
),
|
|
208
|
-
loggerLayer,
|
|
209
|
-
),
|
|
212
|
+
this.loggerLayer,
|
|
213
|
+
) as Effect.Effect<Logger, never, never>,
|
|
210
214
|
) as Logger;
|
|
211
215
|
this.logger = createSyncLogger(effectLogger);
|
|
212
216
|
|
|
213
|
-
// Create configuration service if config
|
|
214
|
-
|
|
217
|
+
// Create configuration service eagerly if config exists (it stores a reference,
|
|
218
|
+
// doesn't call config.get(), so safe before initialization)
|
|
219
|
+
if (!(this.config instanceof NotInitializedConfig)) {
|
|
215
220
|
this.configService = new ConfigServiceImpl(this.logger, this.config);
|
|
216
221
|
}
|
|
217
222
|
|
|
@@ -274,8 +279,8 @@ export class OneBunApplication {
|
|
|
274
279
|
}
|
|
275
280
|
}
|
|
276
281
|
|
|
277
|
-
//
|
|
278
|
-
|
|
282
|
+
// Note: root module creation is deferred to start() to ensure
|
|
283
|
+
// config is fully initialized before services are created.
|
|
279
284
|
}
|
|
280
285
|
|
|
281
286
|
/**
|
|
@@ -323,11 +328,23 @@ export class OneBunApplication {
|
|
|
323
328
|
return this.getConfig().get(path);
|
|
324
329
|
}
|
|
325
330
|
|
|
331
|
+
/**
|
|
332
|
+
* Ensure root module is created (i.e., start() has been called).
|
|
333
|
+
* Throws if called before start().
|
|
334
|
+
*/
|
|
335
|
+
private ensureModule(): ModuleInstance {
|
|
336
|
+
if (!this.rootModule) {
|
|
337
|
+
throw new Error('Application not started. Call start() before accessing the module.');
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
return this.rootModule;
|
|
341
|
+
}
|
|
342
|
+
|
|
326
343
|
/**
|
|
327
344
|
* Get root module layer
|
|
328
345
|
*/
|
|
329
346
|
getLayer(): Layer.Layer<never, never, unknown> {
|
|
330
|
-
return this.
|
|
347
|
+
return this.ensureModule().getLayer();
|
|
331
348
|
}
|
|
332
349
|
|
|
333
350
|
/**
|
|
@@ -377,6 +394,10 @@ export class OneBunApplication {
|
|
|
377
394
|
this.logger.info('Application configuration initialized');
|
|
378
395
|
}
|
|
379
396
|
|
|
397
|
+
// Create the root module AFTER config is initialized,
|
|
398
|
+
// so services can safely use this.config.get() in their constructors
|
|
399
|
+
this.rootModule = OneBunModule.create(this.moduleClass, this.loggerLayer, this.config);
|
|
400
|
+
|
|
380
401
|
// Start metrics collection if enabled
|
|
381
402
|
if (this.metricsService && this.metricsService.startSystemMetricsCollection) {
|
|
382
403
|
this.metricsService.startSystemMetricsCollection();
|
|
@@ -384,10 +405,10 @@ export class OneBunApplication {
|
|
|
384
405
|
}
|
|
385
406
|
|
|
386
407
|
// Setup the module and create controller instances
|
|
387
|
-
await Effect.runPromise(this.
|
|
408
|
+
await Effect.runPromise(this.ensureModule().setup() as Effect.Effect<unknown, never, never>);
|
|
388
409
|
|
|
389
410
|
// Get all controllers from the root module
|
|
390
|
-
const controllers = this.
|
|
411
|
+
const controllers = this.ensureModule().getControllers();
|
|
391
412
|
this.logger.debug(`Loaded ${controllers.length} controllers`);
|
|
392
413
|
|
|
393
414
|
// Initialize WebSocket handler and detect gateways
|
|
@@ -396,7 +417,7 @@ export class OneBunApplication {
|
|
|
396
417
|
// Register WebSocket gateways (they are in controllers array but decorated with @WebSocketGateway)
|
|
397
418
|
for (const controllerClass of controllers) {
|
|
398
419
|
if (isWebSocketGateway(controllerClass)) {
|
|
399
|
-
const instance = this.
|
|
420
|
+
const instance = this.ensureModule().getControllerInstance?.(controllerClass);
|
|
400
421
|
if (instance) {
|
|
401
422
|
this.wsHandler.registerGateway(controllerClass, instance as import('../websocket/ws-base-gateway').BaseWebSocketGateway);
|
|
402
423
|
this.logger.info(`Registered WebSocket gateway: ${controllerClass.name}`);
|
|
@@ -438,14 +459,14 @@ export class OneBunApplication {
|
|
|
438
459
|
}
|
|
439
460
|
|
|
440
461
|
// Get controller instance from module
|
|
441
|
-
if (!this.
|
|
462
|
+
if (!this.ensureModule().getControllerInstance) {
|
|
442
463
|
this.logger.warn(
|
|
443
464
|
`Module does not support getControllerInstance for ${controllerClass.name}`,
|
|
444
465
|
);
|
|
445
466
|
continue;
|
|
446
467
|
}
|
|
447
468
|
|
|
448
|
-
const controller = this.
|
|
469
|
+
const controller = this.ensureModule().getControllerInstance!(controllerClass) as Controller;
|
|
449
470
|
if (!controller) {
|
|
450
471
|
this.logger.warn(`Controller instance not found for ${controllerClass.name}`);
|
|
451
472
|
continue;
|
|
@@ -508,8 +529,8 @@ export class OneBunApplication {
|
|
|
508
529
|
}
|
|
509
530
|
|
|
510
531
|
// Call onApplicationInit lifecycle hook for all services and controllers
|
|
511
|
-
if (this.
|
|
512
|
-
await this.
|
|
532
|
+
if (this.ensureModule().callOnApplicationInit) {
|
|
533
|
+
await this.ensureModule().callOnApplicationInit!();
|
|
513
534
|
this.logger.debug('Application initialization hooks completed');
|
|
514
535
|
}
|
|
515
536
|
|
|
@@ -1242,7 +1263,7 @@ export class OneBunApplication {
|
|
|
1242
1263
|
this.logger.info('Stopping OneBun application...');
|
|
1243
1264
|
|
|
1244
1265
|
// Call beforeApplicationDestroy lifecycle hook
|
|
1245
|
-
if (this.rootModule
|
|
1266
|
+
if (this.rootModule?.callBeforeApplicationDestroy) {
|
|
1246
1267
|
this.logger.debug('Calling beforeApplicationDestroy hooks');
|
|
1247
1268
|
await this.rootModule.callBeforeApplicationDestroy(signal);
|
|
1248
1269
|
}
|
|
@@ -1276,7 +1297,7 @@ export class OneBunApplication {
|
|
|
1276
1297
|
}
|
|
1277
1298
|
|
|
1278
1299
|
// Call onModuleDestroy lifecycle hook
|
|
1279
|
-
if (this.rootModule
|
|
1300
|
+
if (this.rootModule?.callOnModuleDestroy) {
|
|
1280
1301
|
this.logger.debug('Calling onModuleDestroy hooks');
|
|
1281
1302
|
await this.rootModule.callOnModuleDestroy();
|
|
1282
1303
|
}
|
|
@@ -1288,7 +1309,7 @@ export class OneBunApplication {
|
|
|
1288
1309
|
}
|
|
1289
1310
|
|
|
1290
1311
|
// Call onApplicationDestroy lifecycle hook
|
|
1291
|
-
if (this.rootModule
|
|
1312
|
+
if (this.rootModule?.callOnApplicationDestroy) {
|
|
1292
1313
|
this.logger.debug('Calling onApplicationDestroy hooks');
|
|
1293
1314
|
await this.rootModule.callOnApplicationDestroy(signal);
|
|
1294
1315
|
}
|
|
@@ -1304,7 +1325,7 @@ export class OneBunApplication {
|
|
|
1304
1325
|
|
|
1305
1326
|
// Check if any controller has queue-related decorators
|
|
1306
1327
|
const hasQueueHandlers = controllers.some(controller => {
|
|
1307
|
-
const instance = this.
|
|
1328
|
+
const instance = this.ensureModule().getControllerInstance?.(controller);
|
|
1308
1329
|
if (!instance) {
|
|
1309
1330
|
return false;
|
|
1310
1331
|
}
|
|
@@ -1364,7 +1385,7 @@ export class OneBunApplication {
|
|
|
1364
1385
|
|
|
1365
1386
|
// Register handlers from controllers using registerService
|
|
1366
1387
|
for (const controllerClass of controllers) {
|
|
1367
|
-
const instance = this.
|
|
1388
|
+
const instance = this.ensureModule().getControllerInstance?.(controllerClass);
|
|
1368
1389
|
if (!instance) {
|
|
1369
1390
|
continue;
|
|
1370
1391
|
}
|
|
@@ -1528,11 +1549,11 @@ export class OneBunApplication {
|
|
|
1528
1549
|
* ```
|
|
1529
1550
|
*/
|
|
1530
1551
|
getService<T>(serviceClass: new (...args: unknown[]) => T): T {
|
|
1531
|
-
if (!this.
|
|
1552
|
+
if (!this.ensureModule().getServiceByClass) {
|
|
1532
1553
|
throw new Error('Module does not support getServiceByClass');
|
|
1533
1554
|
}
|
|
1534
1555
|
|
|
1535
|
-
const service = this.
|
|
1556
|
+
const service = this.ensureModule().getServiceByClass!(serviceClass);
|
|
1536
1557
|
if (!service) {
|
|
1537
1558
|
throw new Error(
|
|
1538
1559
|
`Service ${serviceClass.name} not found. Make sure it's registered in the module's providers.`,
|
|
@@ -11,6 +11,7 @@ import {
|
|
|
11
11
|
expect,
|
|
12
12
|
beforeEach,
|
|
13
13
|
afterEach,
|
|
14
|
+
mock,
|
|
14
15
|
} from 'bun:test';
|
|
15
16
|
|
|
16
17
|
import { OneBunApplication } from '../application';
|
|
@@ -817,50 +818,63 @@ describe('decorators', () => {
|
|
|
817
818
|
});
|
|
818
819
|
|
|
819
820
|
test('should properly inject module service into controller without @Inject', async () => {
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
821
|
+
// Mock Bun.serve to avoid starting a real server
|
|
822
|
+
const originalServe = Bun.serve;
|
|
823
|
+
|
|
824
|
+
(Bun as any).serve = mock(() => ({
|
|
825
|
+
stop: mock(),
|
|
826
|
+
port: 3000,
|
|
827
|
+
}));
|
|
828
|
+
|
|
829
|
+
try {
|
|
830
|
+
@Service()
|
|
831
|
+
class TestService extends BaseService {
|
|
832
|
+
getValue() {
|
|
833
|
+
return 'injected-value';
|
|
834
|
+
}
|
|
824
835
|
}
|
|
825
|
-
}
|
|
826
836
|
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
837
|
+
// No @Inject needed - automatic DI via emitDecoratorMetadata
|
|
838
|
+
@Controller('')
|
|
839
|
+
class TestController extends BaseController {
|
|
840
|
+
constructor(private service: TestService) {
|
|
841
|
+
super();
|
|
842
|
+
}
|
|
833
843
|
|
|
834
|
-
|
|
835
|
-
|
|
844
|
+
getServiceValue() {
|
|
845
|
+
return this.service.getValue();
|
|
846
|
+
}
|
|
836
847
|
}
|
|
837
|
-
}
|
|
838
848
|
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
849
|
+
@Module({
|
|
850
|
+
controllers: [TestController],
|
|
851
|
+
providers: [TestService],
|
|
852
|
+
})
|
|
853
|
+
class TestModule {}
|
|
844
854
|
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
855
|
+
const app = new OneBunApplication(TestModule, {
|
|
856
|
+
loggerLayer: makeMockLoggerLayer(),
|
|
857
|
+
});
|
|
848
858
|
|
|
849
|
-
|
|
850
|
-
|
|
859
|
+
// start() creates rootModule and calls setup, triggering dependency injection
|
|
860
|
+
await app.start();
|
|
851
861
|
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
862
|
+
// Access rootModule after start to verify DI
|
|
863
|
+
|
|
864
|
+
const rootModule = (app as any).rootModule;
|
|
855
865
|
|
|
856
|
-
|
|
857
|
-
|
|
866
|
+
// Get controller instance and verify service was injected
|
|
867
|
+
const controllerInstance = rootModule.getControllerInstance(TestController) as TestController;
|
|
858
868
|
|
|
859
|
-
|
|
860
|
-
|
|
869
|
+
expect(controllerInstance).toBeDefined();
|
|
870
|
+
expect(controllerInstance).toBeInstanceOf(TestController);
|
|
861
871
|
|
|
862
|
-
|
|
863
|
-
|
|
872
|
+
// Verify the injected service works correctly
|
|
873
|
+
expect(controllerInstance.getServiceValue()).toBe('injected-value');
|
|
874
|
+
} finally {
|
|
875
|
+
|
|
876
|
+
(Bun as any).serve = originalServe;
|
|
877
|
+
}
|
|
864
878
|
});
|
|
865
879
|
});
|
|
866
880
|
|