@chromahq/core 0.1.12 → 0.1.14

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.
@@ -1,5 +1,11 @@
1
+ import { Container } from '@inversifyjs/container';
2
+
1
3
  const TOKENS = { Store: Symbol.for("Store") };
2
4
 
5
+ const container = new Container({
6
+ defaultScope: "Singleton"
7
+ });
8
+
3
9
  var JobState = /* @__PURE__ */ ((JobState2) => {
4
10
  JobState2["SCHEDULED"] = "scheduled";
5
11
  JobState2["RUNNING"] = "running";
@@ -10,5 +16,5 @@ var JobState = /* @__PURE__ */ ((JobState2) => {
10
16
  return JobState2;
11
17
  })(JobState || {});
12
18
 
13
- export { JobState as J, TOKENS as T };
14
- //# sourceMappingURL=IJob-aWBqI3FC.js.map
19
+ export { JobState as J, TOKENS as T, container as c };
20
+ //# sourceMappingURL=IJob-ByOJx8qA.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"IJob-ByOJx8qA.js","sources":["../src/decorators/tokens.ts","../src/di/Container.ts","../src/scheduler/core/IJob.ts"],"sourcesContent":["export const TOKENS = { Store: Symbol.for('Store') };\n","import { Container as Di } from '@inversifyjs/container';\n\nexport const METADATA_KEY = {\n PARAM_TYPES: 'design:paramtypes',\n TYPE: 'design:type',\n RETURN_TYPE: 'design:returntype',\n};\n\nexport const container = new Di({\n defaultScope: 'Singleton',\n});\n\nexport const bind = <T>(id: symbol | (new (...a: any[]) => T), cls: new (...a: any[]) => T) =>\n container\n .bind<T>(id as any)\n .to(cls)\n .inSingletonScope();\n\nexport const resolve = <T>(id: symbol | (new (...a: any[]) => T)): T => container.get<T>(id as any);\n\nexport function isInjectable(target: any): boolean {\n return !!Reflect.getMetadata(METADATA_KEY.PARAM_TYPES, target);\n}\n","import { JobOptions } from './JobOptions';\n\nexport interface IJob<T = unknown> {\n readonly data?: T;\n handle(context?: JobContext): Promise<void> | void;\n pause?(): Promise<void> | void;\n resume?(): Promise<void> | void;\n stop?(): Promise<void> | void;\n}\n\nexport enum JobState {\n SCHEDULED = 'scheduled',\n RUNNING = 'running',\n PAUSED = 'paused',\n STOPPED = 'stopped',\n COMPLETED = 'completed',\n FAILED = 'failed',\n}\n\nexport interface JobContext {\n id: string;\n options?: JobOptions;\n state: JobState;\n createdAt: Date;\n updatedAt: Date;\n startedAt?: Date;\n pausedAt?: Date;\n stoppedAt?: Date;\n completedAt?: Date;\n error?: Error;\n retryCount?: number;\n\n /**\n * Pause the job execution.\n * @returns Promise<void> | void\n */\n pause: () => Promise<void> | void;\n resume: () => Promise<void> | void;\n stop: () => Promise<void> | void;\n complete: () => Promise<void> | void;\n fail: (error: Error) => Promise<void> | void;\n retry: () => Promise<void> | void;\n\n isRunning: () => boolean;\n isPaused: () => boolean;\n isStopped: () => boolean;\n isCompleted: () => boolean;\n isFailed: () => boolean;\n isRetrying: () => boolean;\n isScheduled: () => boolean;\n isDelayed: () => boolean;\n isRecurring: () => boolean;\n isCron: () => boolean;\n isTimeout: () => boolean;\n isAlarm: () => boolean;\n}\n"],"names":["Di","JobState"],"mappings":";;AAAO,MAAM,SAAS,EAAE,KAAA,EAAO,MAAA,CAAO,GAAA,CAAI,OAAO,CAAA;;ACQ1C,MAAM,SAAA,GAAY,IAAIA,SAAA,CAAG;AAAA,EAC9B,YAAA,EAAc;AAChB,CAAC;;ACAM,IAAK,QAAA,qBAAAC,SAAAA,KAAL;AACL,EAAAA,UAAA,WAAA,CAAA,GAAY,WAAA;AACZ,EAAAA,UAAA,SAAA,CAAA,GAAU,SAAA;AACV,EAAAA,UAAA,QAAA,CAAA,GAAS,QAAA;AACT,EAAAA,UAAA,SAAA,CAAA,GAAU,SAAA;AACV,EAAAA,UAAA,WAAA,CAAA,GAAY,WAAA;AACZ,EAAAA,UAAA,QAAA,CAAA,GAAS,QAAA;AANC,EAAA,OAAAA,SAAAA;AAAA,CAAA,EAAA,QAAA,IAAA,EAAA;;;;"}
@@ -1,7 +1,13 @@
1
1
  'use strict';
2
2
 
3
+ var container$1 = require('@inversifyjs/container');
4
+
3
5
  const TOKENS = { Store: Symbol.for("Store") };
4
6
 
7
+ const container = new container$1.Container({
8
+ defaultScope: "Singleton"
9
+ });
10
+
5
11
  var JobState = /* @__PURE__ */ ((JobState2) => {
6
12
  JobState2["SCHEDULED"] = "scheduled";
7
13
  JobState2["RUNNING"] = "running";
@@ -14,4 +20,5 @@ var JobState = /* @__PURE__ */ ((JobState2) => {
14
20
 
15
21
  exports.JobState = JobState;
16
22
  exports.TOKENS = TOKENS;
17
- //# sourceMappingURL=IJob-Bds0eWrM.js.map
23
+ exports.container = container;
24
+ //# sourceMappingURL=IJob-CoWGJUZM.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"IJob-CoWGJUZM.js","sources":["../src/decorators/tokens.ts","../src/di/Container.ts","../src/scheduler/core/IJob.ts"],"sourcesContent":["export const TOKENS = { Store: Symbol.for('Store') };\n","import { Container as Di } from '@inversifyjs/container';\n\nexport const METADATA_KEY = {\n PARAM_TYPES: 'design:paramtypes',\n TYPE: 'design:type',\n RETURN_TYPE: 'design:returntype',\n};\n\nexport const container = new Di({\n defaultScope: 'Singleton',\n});\n\nexport const bind = <T>(id: symbol | (new (...a: any[]) => T), cls: new (...a: any[]) => T) =>\n container\n .bind<T>(id as any)\n .to(cls)\n .inSingletonScope();\n\nexport const resolve = <T>(id: symbol | (new (...a: any[]) => T)): T => container.get<T>(id as any);\n\nexport function isInjectable(target: any): boolean {\n return !!Reflect.getMetadata(METADATA_KEY.PARAM_TYPES, target);\n}\n","import { JobOptions } from './JobOptions';\n\nexport interface IJob<T = unknown> {\n readonly data?: T;\n handle(context?: JobContext): Promise<void> | void;\n pause?(): Promise<void> | void;\n resume?(): Promise<void> | void;\n stop?(): Promise<void> | void;\n}\n\nexport enum JobState {\n SCHEDULED = 'scheduled',\n RUNNING = 'running',\n PAUSED = 'paused',\n STOPPED = 'stopped',\n COMPLETED = 'completed',\n FAILED = 'failed',\n}\n\nexport interface JobContext {\n id: string;\n options?: JobOptions;\n state: JobState;\n createdAt: Date;\n updatedAt: Date;\n startedAt?: Date;\n pausedAt?: Date;\n stoppedAt?: Date;\n completedAt?: Date;\n error?: Error;\n retryCount?: number;\n\n /**\n * Pause the job execution.\n * @returns Promise<void> | void\n */\n pause: () => Promise<void> | void;\n resume: () => Promise<void> | void;\n stop: () => Promise<void> | void;\n complete: () => Promise<void> | void;\n fail: (error: Error) => Promise<void> | void;\n retry: () => Promise<void> | void;\n\n isRunning: () => boolean;\n isPaused: () => boolean;\n isStopped: () => boolean;\n isCompleted: () => boolean;\n isFailed: () => boolean;\n isRetrying: () => boolean;\n isScheduled: () => boolean;\n isDelayed: () => boolean;\n isRecurring: () => boolean;\n isCron: () => boolean;\n isTimeout: () => boolean;\n isAlarm: () => boolean;\n}\n"],"names":["Di","JobState"],"mappings":";;;;AAAO,MAAM,SAAS,EAAE,KAAA,EAAO,MAAA,CAAO,GAAA,CAAI,OAAO,CAAA;;ACQ1C,MAAM,SAAA,GAAY,IAAIA,qBAAA,CAAG;AAAA,EAC9B,YAAA,EAAc;AAChB,CAAC;;ACAM,IAAK,QAAA,qBAAAC,SAAAA,KAAL;AACL,EAAAA,UAAA,WAAA,CAAA,GAAY,WAAA;AACZ,EAAAA,UAAA,SAAA,CAAA,GAAU,SAAA;AACV,EAAAA,UAAA,QAAA,CAAA,GAAS,QAAA;AACT,EAAAA,UAAA,SAAA,CAAA,GAAU,SAAA;AACV,EAAAA,UAAA,WAAA,CAAA,GAAY,WAAA;AACZ,EAAAA,UAAA,QAAA,CAAA,GAAS,QAAA;AANC,EAAA,OAAAA,SAAAA;AAAA,CAAA,EAAA,QAAA,IAAA,EAAA;;;;;;"}
package/dist/boot.cjs.js CHANGED
@@ -1,11 +1,7 @@
1
1
  'use strict';
2
2
 
3
- var container$1 = require('@inversifyjs/container');
4
- var IJob = require('./IJob-Bds0eWrM.js');
5
-
6
- const container = new container$1.Container({
7
- defaultScope: "Singleton"
8
- });
3
+ var IJob = require('./IJob-CoWGJUZM.js');
4
+ require('@inversifyjs/container');
9
5
 
10
6
  class MiddlewareRegistryClass {
11
7
  constructor() {
@@ -1098,7 +1094,7 @@ class Scheduler {
1098
1094
  try {
1099
1095
  this.registry.updateState(id, IJob.JobState.RUNNING);
1100
1096
  console.log(`Executing job ${id}`);
1101
- const jobInstance = container.get(id);
1097
+ const jobInstance = IJob.container.get(id);
1102
1098
  console.log(jobInstance);
1103
1099
  await jobInstance.handle.bind(jobInstance).call(jobInstance, context);
1104
1100
  if (!context.isStopped() && !context.isPaused()) {
@@ -1146,9 +1142,9 @@ class ApplicationBootstrap {
1146
1142
  * Add a store definition to be initialized
1147
1143
  */
1148
1144
  withStore(storeDefinition) {
1149
- if (storeDefinition && storeDefinition.name) {
1145
+ if (storeDefinition && storeDefinition.def && storeDefinition.store) {
1150
1146
  this.storeDefinitions.push(storeDefinition);
1151
- this.logger.debug(`\u{1F4E6} Added store definition: ${storeDefinition.name}`);
1147
+ this.logger.debug(`\u{1F4E6} Added store definition: ${storeDefinition.def.name}`);
1152
1148
  }
1153
1149
  return this;
1154
1150
  }
@@ -1156,9 +1152,7 @@ class ApplicationBootstrap {
1156
1152
  * Add multiple store definitions to be initialized
1157
1153
  */
1158
1154
  withStores(storeDefinitions) {
1159
- for (const store of storeDefinitions) {
1160
- this.withStore(store);
1161
- }
1155
+ storeDefinitions.forEach((store) => this.withStore(store));
1162
1156
  return this;
1163
1157
  }
1164
1158
  /**
@@ -1166,9 +1160,11 @@ class ApplicationBootstrap {
1166
1160
  */
1167
1161
  async create({
1168
1162
  keepPortAlive = false,
1169
- portName
1163
+ portName,
1164
+ enableLogs = true
1170
1165
  }) {
1171
1166
  try {
1167
+ this.logger = new BootstrapLogger(enableLogs);
1172
1168
  this.logger.info("\u{1F680} Starting Chroma application bootstrap...");
1173
1169
  await this.discoverAndInitializeStores();
1174
1170
  await this.discoverServices();
@@ -1179,7 +1175,7 @@ class ApplicationBootstrap {
1179
1175
  await this.bootMessages();
1180
1176
  await this.bootServices();
1181
1177
  this.logger.success("\u{1F389} Chroma application initialization complete!");
1182
- bootstrap$1({ container, keepAlive: keepPortAlive, portName });
1178
+ bootstrap$1({ container: IJob.container, keepAlive: keepPortAlive, portName });
1183
1179
  } catch (error) {
1184
1180
  this.logger.error("\u{1F4A5} Application bootstrap failed:", error);
1185
1181
  throw error;
@@ -1193,7 +1189,7 @@ class ApplicationBootstrap {
1193
1189
  console.log("services", this.serviceRegistry.entries());
1194
1190
  for (const [serviceName, ServiceClass] of this.serviceRegistry.entries()) {
1195
1191
  try {
1196
- const instance = container.get(ServiceClass);
1192
+ const instance = IJob.container.get(ServiceClass);
1197
1193
  if (typeof instance.onBoot === "function") {
1198
1194
  await instance.onBoot();
1199
1195
  this.logger.success(`Booted service: ${serviceName}`);
@@ -1216,7 +1212,7 @@ class ApplicationBootstrap {
1216
1212
  const ServiceClass = module?.default;
1217
1213
  if (!ServiceClass || typeof ServiceClass !== "function") {
1218
1214
  this.logger.warn(
1219
- `\u26A0\uFE0F Skipping invalid service module - no default export or not a constructor`
1215
+ `\u26A0\uFE0F Skipping invalid service module - no default export or not a constructor` + module
1220
1216
  );
1221
1217
  continue;
1222
1218
  }
@@ -1247,25 +1243,20 @@ class ApplicationBootstrap {
1247
1243
  return;
1248
1244
  }
1249
1245
  this.logger.info(`Initializing ${this.storeDefinitions.length} store(s)...`);
1250
- const chromaGlobal = globalThis.__CHROMA__;
1251
- if (chromaGlobal?.initStores && typeof chromaGlobal.initStores === "function") {
1252
- let isFirstStore = true;
1253
- for (const store of this.storeDefinitions) {
1254
- const { classes, store: storeInstance } = await chromaGlobal.initStores(store);
1255
- const diKey = `CentralStore:${store.name}`;
1256
- container.bind(diKey).toConstantValue(storeInstance);
1257
- if (isFirstStore) {
1258
- container.bind(IJob.TOKENS.Store).toConstantValue(storeInstance);
1259
- isFirstStore = false;
1260
- }
1261
- this.registerMessageClass(classes.GetStoreStateMessage, `store:${store.name}:getState`);
1262
- this.registerMessageClass(classes.SetStoreStateMessage, `store:${store.name}:setState`);
1263
- this.registerMessageClass(
1264
- classes.SubscribeToStoreMessage,
1265
- `store:${store.name}:subscribe`
1266
- );
1267
- this.logger.debug(`\u2705 Initialized store: ${store.name}`);
1246
+ let isFirstStore = true;
1247
+ for (const store of this.storeDefinitions) {
1248
+ const diKey = `CentralStore:${store.name}`;
1249
+ const storeInstance = store.store;
1250
+ const classes = store.classes;
1251
+ IJob.container.bind(diKey).toConstantValue(storeInstance);
1252
+ if (isFirstStore) {
1253
+ IJob.container.bind(IJob.TOKENS.Store).toConstantValue(storeInstance);
1254
+ isFirstStore = false;
1268
1255
  }
1256
+ this.registerMessageClass(classes.GetStoreStateMessage, `store:${store.name}:getState`);
1257
+ this.registerMessageClass(classes.SetStoreStateMessage, `store:${store.name}:setState`);
1258
+ this.registerMessageClass(classes.SubscribeToStoreMessage, `store:${store.name}:subscribe`);
1259
+ this.logger.debug(`\u2705 Initialized store: ${store.name}`);
1269
1260
  }
1270
1261
  this.logger.success(`\u2705 Initialized ${this.storeDefinitions.length} store(s)`);
1271
1262
  } catch (error) {
@@ -1406,7 +1397,7 @@ class ApplicationBootstrap {
1406
1397
  this.logger.error(`\u274C ${errorMessage}`);
1407
1398
  return { success: false, message: errorMessage };
1408
1399
  }
1409
- container.bind(ServiceClass).toSelf().inSingletonScope();
1400
+ IJob.container.bind(ServiceClass).toSelf().inSingletonScope();
1410
1401
  serviceMetadata.registered = true;
1411
1402
  this.logger.success(`\u2705 Registered service: ${ServiceClass.name}`);
1412
1403
  return { success: true, message: `Successfully registered ${ServiceClass.name}` };
@@ -1431,7 +1422,7 @@ class ApplicationBootstrap {
1431
1422
  try {
1432
1423
  const messageMetadata = Reflect.getMetadata("name", MessageClass);
1433
1424
  const messageName = messageMetadata || MessageClass.name;
1434
- container.bind(messageName).to(MessageClass).inSingletonScope();
1425
+ IJob.container.bind(messageName).to(MessageClass).inSingletonScope();
1435
1426
  this.logger.success(`\u2705 Registered message: ${messageName}`);
1436
1427
  } catch (error) {
1437
1428
  this.logger.error(`\u274C Failed to register message ${MessageClass.name}:`, error);
@@ -1439,7 +1430,7 @@ class ApplicationBootstrap {
1439
1430
  }
1440
1431
  }
1441
1432
  async registerMessageClass(MessageClass, name) {
1442
- container.bind(name).to(MessageClass).inSingletonScope();
1433
+ IJob.container.bind(name).to(MessageClass).inSingletonScope();
1443
1434
  this.logger.success(`\u2705 Registered message: ${name}`);
1444
1435
  }
1445
1436
  /**
@@ -1457,7 +1448,7 @@ class ApplicationBootstrap {
1457
1448
  try {
1458
1449
  const messageMetadata = Reflect.getMetadata("name", MessageClass);
1459
1450
  const messageName = messageMetadata || MessageClass.name;
1460
- const messageInstance = container.get(messageName);
1451
+ const messageInstance = IJob.container.get(messageName);
1461
1452
  await messageInstance.boot();
1462
1453
  this.logger.success(`\u2705 Booted message: ${messageName}`);
1463
1454
  } catch (error) {
@@ -1474,21 +1465,21 @@ class ApplicationBootstrap {
1474
1465
  "/src/app/jobs/**/*.job.{ts,js}",
1475
1466
  { eager: true }
1476
1467
  );
1477
- if (!container.isBound(Scheduler)) {
1478
- container.bind(Scheduler).toSelf().inSingletonScope();
1468
+ if (!IJob.container.isBound(Scheduler)) {
1469
+ IJob.container.bind(Scheduler).toSelf().inSingletonScope();
1479
1470
  }
1480
- this.scheduler = container.get(Scheduler);
1471
+ this.scheduler = IJob.container.get(Scheduler);
1481
1472
  for (const module of Object.values(jobModules)) {
1482
1473
  const JobClass = module?.default;
1483
1474
  if (!JobClass) continue;
1484
1475
  try {
1485
1476
  const jobMetadata = Reflect.getMetadata("name", JobClass);
1486
1477
  const jobName = jobMetadata || JobClass.name;
1487
- container.bind(JobClass).toSelf().inSingletonScope();
1478
+ IJob.container.bind(JobClass).toSelf().inSingletonScope();
1488
1479
  const id = `${jobName.toLowerCase()}:${JobClass.name.toLowerCase()} ${Math.random().toString(36).substring(2, 15)}`;
1489
- container.bind(id).to(JobClass).inSingletonScope();
1480
+ IJob.container.bind(id).to(JobClass).inSingletonScope();
1490
1481
  const options = Reflect.getMetadata("job:options", JobClass) || {};
1491
- const instance = container.get(JobClass);
1482
+ const instance = IJob.container.get(JobClass);
1492
1483
  JobRegistry.instance.register(id, instance, options);
1493
1484
  this.scheduler.schedule(id, options);
1494
1485
  this.logger.success(`\u2705 Registered job: ${jobName}`);
@@ -1499,25 +1490,34 @@ class ApplicationBootstrap {
1499
1490
  }
1500
1491
  }
1501
1492
  class BootstrapLogger {
1493
+ constructor(enableLogs = true) {
1494
+ this.enableLogs = enableLogs;
1495
+ }
1502
1496
  info(message, context) {
1497
+ if (!this.enableLogs) return;
1503
1498
  console.log(message);
1504
1499
  if (context) console.log(" ", context);
1505
1500
  }
1506
1501
  success(message) {
1502
+ if (!this.enableLogs) return;
1507
1503
  console.log(message);
1508
1504
  }
1509
1505
  warn(message) {
1506
+ if (!this.enableLogs) return;
1510
1507
  console.warn(message);
1511
1508
  }
1512
1509
  error(message, error) {
1510
+ if (!this.enableLogs) return;
1513
1511
  console.error(message);
1514
1512
  if (error) console.error(" ", error);
1515
1513
  }
1516
1514
  debug(message, context) {
1515
+ if (!this.enableLogs) return;
1517
1516
  console.debug(message);
1518
1517
  if (context) console.debug(" ", context);
1519
1518
  }
1520
1519
  divider() {
1520
+ if (!this.enableLogs) return;
1521
1521
  console.log("=".repeat(50));
1522
1522
  }
1523
1523
  }
@@ -1547,9 +1547,10 @@ class BootstrapBuilder {
1547
1547
  */
1548
1548
  async create({
1549
1549
  keepPortAlive = false,
1550
- portName
1550
+ portName,
1551
+ enableLogs = true
1551
1552
  } = {}) {
1552
- await this.app.create({ keepPortAlive, portName });
1553
+ await this.app.create({ keepPortAlive, portName, enableLogs });
1553
1554
  }
1554
1555
  }
1555
1556