@celerity-sdk/core 0.4.0 → 0.6.0

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/dist/index.js CHANGED
@@ -378,10 +378,10 @@ __name(EventType, "EventType");
378
378
 
379
379
  // src/decorators/consumer.ts
380
380
  import "reflect-metadata";
381
- function Consumer(sourceId) {
381
+ function Consumer(source) {
382
382
  return (target) => {
383
383
  const meta = {};
384
- if (sourceId !== void 0) meta.sourceId = sourceId;
384
+ if (source !== void 0) meta.source = source;
385
385
  Reflect.defineMetadata(CONSUMER_METADATA, meta, target);
386
386
  Reflect.defineMetadata(INJECTABLE_METADATA, true, target);
387
387
  };
@@ -440,11 +440,11 @@ function parseScheduleArg(arg) {
440
440
  return isScheduleExpression(arg) ? {
441
441
  schedule: arg
442
442
  } : {
443
- scheduleId: arg
443
+ source: arg
444
444
  };
445
445
  }
446
446
  const meta = {};
447
- if (arg.scheduleId !== void 0) meta.scheduleId = arg.scheduleId;
447
+ if (arg.source !== void 0) meta.source = arg.source;
448
448
  if (arg.schedule !== void 0) meta.schedule = arg.schedule;
449
449
  return meta;
450
450
  }
@@ -835,6 +835,10 @@ __name(runLayerPipeline, "runLayerPipeline");
835
835
  // src/layers/system.ts
836
836
  import { ConfigLayer, captureResourceLinks, getResourceTypes } from "@celerity-sdk/config";
837
837
  var RESOURCE_LAYER_MAP = {
838
+ datastore: {
839
+ pkg: "@celerity-sdk/datastore",
840
+ className: "DatastoreLayer"
841
+ },
838
842
  bucket: {
839
843
  pkg: "@celerity-sdk/bucket",
840
844
  className: "ObjectStorageLayer"
@@ -843,9 +847,17 @@ var RESOURCE_LAYER_MAP = {
843
847
  pkg: "@celerity-sdk/queue",
844
848
  className: "QueueLayer"
845
849
  },
850
+ topic: {
851
+ pkg: "@celerity-sdk/topic",
852
+ className: "TopicLayer"
853
+ },
846
854
  cache: {
847
855
  pkg: "@celerity-sdk/cache",
848
856
  className: "CacheLayer"
857
+ },
858
+ sqlDatabase: {
859
+ pkg: "@celerity-sdk/sql-database",
860
+ className: "SqlDatabaseLayer"
849
861
  }
850
862
  };
851
863
  async function createDefaultSystemLayers() {
@@ -1323,6 +1335,7 @@ __name(matchRoute, "matchRoute");
1323
1335
  // src/bootstrap/module-graph.ts
1324
1336
  import "reflect-metadata";
1325
1337
  import createDebug4 from "debug";
1338
+ import { isResourceLayerToken } from "@celerity-sdk/common";
1326
1339
  var debug4 = createDebug4("celerity:core:bootstrap");
1327
1340
  function buildModuleGraph(rootModule) {
1328
1341
  const graph = /* @__PURE__ */ new Map();
@@ -1351,7 +1364,8 @@ function buildModuleGraph(rootModule) {
1351
1364
  controllers: [],
1352
1365
  functionHandlers: [],
1353
1366
  guards: [],
1354
- providers: []
1367
+ providers: [],
1368
+ layers: []
1355
1369
  });
1356
1370
  return;
1357
1371
  }
@@ -1393,7 +1407,8 @@ function buildModuleGraph(rootModule) {
1393
1407
  controllers,
1394
1408
  functionHandlers: metadata.functionHandlers ?? [],
1395
1409
  guards,
1396
- providers
1410
+ providers,
1411
+ layers: metadata.layers ?? []
1397
1412
  });
1398
1413
  }
1399
1414
  __name(walk, "walk");
@@ -1511,6 +1526,7 @@ function checkDependencies(consumer, depTokens, visibleTokens, moduleClass, grap
1511
1526
  checkDependencies(dep, adoptedDeps, visibleTokens, moduleClass, graph, container, diagnostics);
1512
1527
  continue;
1513
1528
  }
1529
+ if (isResourceLayerToken(dep)) continue;
1514
1530
  diagnostics.push({
1515
1531
  type: "missing_dependency",
1516
1532
  message: `${tokenToString(consumer)} in ${moduleClass.name} requires ${tokenToString(dep)} \u2014 no provider registered. Ensure the module providing it is included in your module's "imports" array, or register a provider for it directly.`
@@ -1536,10 +1552,10 @@ var debug5 = createDebug5("celerity:core:scanner:http");
1536
1552
  async function scanHttpHandlers(graph, container, registry) {
1537
1553
  for (const [, node] of graph) {
1538
1554
  for (const controllerClass of node.controllers) {
1539
- await scanClassHandler(controllerClass, container, registry);
1555
+ await scanClassHandler(controllerClass, container, registry, node.layers);
1540
1556
  }
1541
1557
  for (const fnHandler of node.functionHandlers) {
1542
- scanFunctionHandler(fnHandler, registry);
1558
+ scanFunctionHandler(fnHandler, registry, node.layers);
1543
1559
  }
1544
1560
  }
1545
1561
  }
@@ -1563,11 +1579,10 @@ async function scanModule(moduleClass, container, registry) {
1563
1579
  await scanHttpGuards(graph, container, registry);
1564
1580
  }
1565
1581
  __name(scanModule, "scanModule");
1566
- async function scanClassHandler(controllerClass, container, registry) {
1582
+ async function scanClassHandler(controllerClass, container, registry, moduleLayers) {
1567
1583
  const controllerMeta = Reflect.getOwnMetadata(CONTROLLER_METADATA, controllerClass);
1568
1584
  if (!controllerMeta) return;
1569
- const instance = await container.resolve(controllerClass);
1570
- const prototype = Object.getPrototypeOf(instance);
1585
+ const prototype = controllerClass.prototype;
1571
1586
  const methods = Object.getOwnPropertyNames(prototype).filter((name) => name !== "constructor");
1572
1587
  const classProtectedBy = Reflect.getOwnMetadata(GUARD_PROTECTEDBY_METADATA, controllerClass) ?? [];
1573
1588
  const classLayers = Reflect.getOwnMetadata(LAYER_METADATA, controllerClass) ?? [];
@@ -1585,6 +1600,7 @@ async function scanClassHandler(controllerClass, container, registry) {
1585
1600
  const descriptor = Object.getOwnPropertyDescriptor(prototype, methodName);
1586
1601
  if (!descriptor?.value || typeof descriptor.value !== "function") continue;
1587
1602
  const layers = [
1603
+ ...moduleLayers,
1588
1604
  ...classLayers,
1589
1605
  ...methodLayers
1590
1606
  ];
@@ -1609,15 +1625,16 @@ async function scanClassHandler(controllerClass, container, registry) {
1609
1625
  ...methodCustomMetadata
1610
1626
  },
1611
1627
  handlerFn: descriptor.value,
1612
- handlerInstance: instance
1628
+ controllerClass
1613
1629
  });
1614
1630
  }
1615
1631
  }
1616
1632
  __name(scanClassHandler, "scanClassHandler");
1617
- function scanFunctionHandler(definition, registry) {
1633
+ function scanFunctionHandler(definition, registry, moduleLayers) {
1618
1634
  if (definition.type !== "http") return;
1619
1635
  const meta = definition.metadata;
1620
1636
  const layers = [
1637
+ ...moduleLayers,
1621
1638
  ...meta.layers ?? []
1622
1639
  ];
1623
1640
  if (meta.schema) {
@@ -1650,8 +1667,7 @@ __name(scanFunctionHandler, "scanFunctionHandler");
1650
1667
  async function scanClassGuard(guardClass, container, registry) {
1651
1668
  const guardName = Reflect.getOwnMetadata(GUARD_CUSTOM_METADATA, guardClass);
1652
1669
  if (!guardName) return;
1653
- const instance = await container.resolve(guardClass);
1654
- const prototype = Object.getPrototypeOf(instance);
1670
+ const prototype = guardClass.prototype;
1655
1671
  const descriptor = Object.getOwnPropertyDescriptor(prototype, "check");
1656
1672
  if (!descriptor?.value || typeof descriptor.value !== "function") {
1657
1673
  debug5("scanClassGuard: %s has no check() method, skipping", guardClass.name);
@@ -1663,7 +1679,7 @@ async function scanClassGuard(guardClass, container, registry) {
1663
1679
  registry.registerGuard({
1664
1680
  name: guardName,
1665
1681
  handlerFn: descriptor.value,
1666
- handlerInstance: instance,
1682
+ guardClass,
1667
1683
  paramMetadata,
1668
1684
  customMetadata
1669
1685
  });
@@ -1755,19 +1771,18 @@ var debug6 = createDebug6("celerity:core:scanner:websocket");
1755
1771
  async function scanWebSocketHandlers(graph, container, registry) {
1756
1772
  for (const [, node] of graph) {
1757
1773
  for (const controllerClass of node.controllers) {
1758
- await scanClassHandler2(controllerClass, container, registry);
1774
+ await scanClassHandler2(controllerClass, container, registry, node.layers);
1759
1775
  }
1760
1776
  for (const fnHandler of node.functionHandlers) {
1761
- scanFunctionHandler2(fnHandler, registry);
1777
+ scanFunctionHandler2(fnHandler, registry, node.layers);
1762
1778
  }
1763
1779
  }
1764
1780
  }
1765
1781
  __name(scanWebSocketHandlers, "scanWebSocketHandlers");
1766
- async function scanClassHandler2(controllerClass, container, registry) {
1782
+ async function scanClassHandler2(controllerClass, container, registry, moduleLayers) {
1767
1783
  const isWsController = Reflect.getOwnMetadata(WEBSOCKET_CONTROLLER_METADATA, controllerClass);
1768
1784
  if (!isWsController) return;
1769
- const instance = await container.resolve(controllerClass);
1770
- const prototype = Object.getPrototypeOf(instance);
1785
+ const prototype = controllerClass.prototype;
1771
1786
  const methods = Object.getOwnPropertyNames(prototype).filter((name) => name !== "constructor");
1772
1787
  const classProtectedBy = Reflect.getOwnMetadata(GUARD_PROTECTEDBY_METADATA, controllerClass) ?? [];
1773
1788
  const classLayers = Reflect.getOwnMetadata(LAYER_METADATA, controllerClass) ?? [];
@@ -1783,6 +1798,7 @@ async function scanClassHandler2(controllerClass, container, registry) {
1783
1798
  if (!descriptor?.value || typeof descriptor.value !== "function") continue;
1784
1799
  debug6("scanClassHandler: %s %s (%s.%s)", eventMeta.eventType, eventMeta.route, controllerClass.name, methodName);
1785
1800
  const layers = [
1801
+ ...moduleLayers,
1786
1802
  ...classLayers,
1787
1803
  ...methodLayers
1788
1804
  ];
@@ -1804,15 +1820,16 @@ async function scanClassHandler2(controllerClass, container, registry) {
1804
1820
  ...methodCustomMetadata
1805
1821
  },
1806
1822
  handlerFn: descriptor.value,
1807
- handlerInstance: instance
1823
+ controllerClass
1808
1824
  });
1809
1825
  }
1810
1826
  }
1811
1827
  __name(scanClassHandler2, "scanClassHandler");
1812
- function scanFunctionHandler2(definition, registry) {
1828
+ function scanFunctionHandler2(definition, registry, moduleLayers) {
1813
1829
  if (definition.type !== "websocket") return;
1814
1830
  const meta = definition.metadata;
1815
1831
  const layers = [
1832
+ ...moduleLayers,
1816
1833
  ...meta.layers ?? []
1817
1834
  ];
1818
1835
  if (meta.schema) {
@@ -1844,19 +1861,18 @@ var debug7 = createDebug7("celerity:core:scanner:consumer");
1844
1861
  async function scanConsumerHandlers(graph, container, registry) {
1845
1862
  for (const [, node] of graph) {
1846
1863
  for (const controllerClass of node.controllers) {
1847
- await scanClassHandler3(controllerClass, container, registry);
1864
+ await scanClassHandler3(controllerClass, container, registry, node.layers);
1848
1865
  }
1849
1866
  for (const fnHandler of node.functionHandlers) {
1850
- scanFunctionHandler3(fnHandler, registry);
1867
+ scanFunctionHandler3(fnHandler, registry, node.layers);
1851
1868
  }
1852
1869
  }
1853
1870
  }
1854
1871
  __name(scanConsumerHandlers, "scanConsumerHandlers");
1855
- async function scanClassHandler3(controllerClass, container, registry) {
1872
+ async function scanClassHandler3(controllerClass, container, registry, moduleLayers) {
1856
1873
  const consumerMeta = Reflect.getOwnMetadata(CONSUMER_METADATA, controllerClass);
1857
1874
  if (!consumerMeta) return;
1858
- const instance = await container.resolve(controllerClass);
1859
- const prototype = Object.getPrototypeOf(instance);
1875
+ const prototype = controllerClass.prototype;
1860
1876
  const methods = Object.getOwnPropertyNames(prototype).filter((name) => name !== "constructor");
1861
1877
  const classLayers = Reflect.getOwnMetadata(LAYER_METADATA, controllerClass) ?? [];
1862
1878
  const classCustomMetadata = Reflect.getOwnMetadata(CUSTOM_METADATA, controllerClass) ?? {};
@@ -1868,8 +1884,9 @@ async function scanClassHandler3(controllerClass, container, registry) {
1868
1884
  const methodCustomMetadata = Reflect.getOwnMetadata(CUSTOM_METADATA, prototype, methodName) ?? {};
1869
1885
  const descriptor = Object.getOwnPropertyDescriptor(prototype, methodName);
1870
1886
  if (!descriptor?.value || typeof descriptor.value !== "function") continue;
1871
- const handlerTag = handlerMeta.route ?? methodName;
1887
+ const handlerTag = consumerMeta.source ? `${consumerMeta.source}::${methodName}` : methodName;
1872
1888
  const layers = [
1889
+ ...moduleLayers,
1873
1890
  ...classLayers,
1874
1891
  ...methodLayers
1875
1892
  ];
@@ -1890,16 +1907,17 @@ async function scanClassHandler3(controllerClass, container, registry) {
1890
1907
  ...methodCustomMetadata
1891
1908
  },
1892
1909
  handlerFn: descriptor.value,
1893
- handlerInstance: instance
1910
+ controllerClass
1894
1911
  });
1895
1912
  }
1896
1913
  }
1897
1914
  __name(scanClassHandler3, "scanClassHandler");
1898
- function scanFunctionHandler3(definition, registry) {
1915
+ function scanFunctionHandler3(definition, registry, moduleLayers) {
1899
1916
  if (definition.type !== "consumer") return;
1900
1917
  const meta = definition.metadata;
1901
- const handlerTag = meta.route ?? definition.id ?? "default";
1918
+ const handlerTag = definition.id ?? "default";
1902
1919
  const layers = [
1920
+ ...moduleLayers,
1903
1921
  ...meta.layers ?? []
1904
1922
  ];
1905
1923
  if (meta.messageSchema) {
@@ -1929,17 +1947,16 @@ var debug8 = createDebug8("celerity:core:scanner:schedule");
1929
1947
  async function scanScheduleHandlers(graph, container, registry) {
1930
1948
  for (const [, node] of graph) {
1931
1949
  for (const controllerClass of node.controllers) {
1932
- await scanClassHandler4(controllerClass, container, registry);
1950
+ await scanClassHandler4(controllerClass, container, registry, node.layers);
1933
1951
  }
1934
1952
  for (const fnHandler of node.functionHandlers) {
1935
- scanFunctionHandler4(fnHandler, registry);
1953
+ scanFunctionHandler4(fnHandler, registry, node.layers);
1936
1954
  }
1937
1955
  }
1938
1956
  }
1939
1957
  __name(scanScheduleHandlers, "scanScheduleHandlers");
1940
- async function scanClassHandler4(controllerClass, container, registry) {
1941
- const instance = await container.resolve(controllerClass);
1942
- const prototype = Object.getPrototypeOf(instance);
1958
+ async function scanClassHandler4(controllerClass, container, registry, moduleLayers) {
1959
+ const prototype = controllerClass.prototype;
1943
1960
  const methods = Object.getOwnPropertyNames(prototype).filter((name) => name !== "constructor");
1944
1961
  const classLayers = Reflect.getOwnMetadata(LAYER_METADATA, controllerClass) ?? [];
1945
1962
  const classCustomMetadata = Reflect.getOwnMetadata(CUSTOM_METADATA, controllerClass) ?? {};
@@ -1951,8 +1968,9 @@ async function scanClassHandler4(controllerClass, container, registry) {
1951
1968
  const methodCustomMetadata = Reflect.getOwnMetadata(CUSTOM_METADATA, prototype, methodName) ?? {};
1952
1969
  const descriptor = Object.getOwnPropertyDescriptor(prototype, methodName);
1953
1970
  if (!descriptor?.value || typeof descriptor.value !== "function") continue;
1954
- const handlerTag = handlerMeta.scheduleId ?? methodName;
1971
+ const handlerTag = handlerMeta.source ? `${handlerMeta.source}::${methodName}` : methodName;
1955
1972
  const layers = [
1973
+ ...moduleLayers,
1956
1974
  ...classLayers,
1957
1975
  ...methodLayers
1958
1976
  ];
@@ -1974,21 +1992,22 @@ async function scanClassHandler4(controllerClass, container, registry) {
1974
1992
  ...handlerMeta.schedule ? {
1975
1993
  schedule: handlerMeta.schedule
1976
1994
  } : {},
1977
- ...handlerMeta.scheduleId ? {
1978
- scheduleId: handlerMeta.scheduleId
1995
+ ...handlerMeta.source ? {
1996
+ source: handlerMeta.source
1979
1997
  } : {}
1980
1998
  },
1981
1999
  handlerFn: descriptor.value,
1982
- handlerInstance: instance
2000
+ controllerClass
1983
2001
  });
1984
2002
  }
1985
2003
  }
1986
2004
  __name(scanClassHandler4, "scanClassHandler");
1987
- function scanFunctionHandler4(definition, registry) {
2005
+ function scanFunctionHandler4(definition, registry, moduleLayers) {
1988
2006
  if (definition.type !== "schedule") return;
1989
2007
  const meta = definition.metadata;
1990
- const handlerTag = meta.scheduleId ?? definition.id ?? "default";
2008
+ const handlerTag = meta.source ?? definition.id ?? "default";
1991
2009
  const layers = [
2010
+ ...moduleLayers,
1992
2011
  ...meta.layers ?? []
1993
2012
  ];
1994
2013
  if (meta.schema) {
@@ -2000,7 +2019,7 @@ function scanFunctionHandler4(definition, registry) {
2000
2019
  ...meta.customMetadata ?? {}
2001
2020
  };
2002
2021
  if (meta.schedule) customMetadata.schedule = meta.schedule;
2003
- if (meta.scheduleId) customMetadata.scheduleId = meta.scheduleId;
2022
+ if (meta.source) customMetadata.source = meta.source;
2004
2023
  debug8("scanFunctionHandler: tag=%s", handlerTag);
2005
2024
  registry.register({
2006
2025
  type: "schedule",
@@ -2023,17 +2042,16 @@ var debug9 = createDebug9("celerity:core:scanner:custom");
2023
2042
  async function scanCustomHandlers(graph, container, registry) {
2024
2043
  for (const [, node] of graph) {
2025
2044
  for (const controllerClass of node.controllers) {
2026
- await scanClassHandler5(controllerClass, container, registry);
2045
+ await scanClassHandler5(controllerClass, container, registry, node.layers);
2027
2046
  }
2028
2047
  for (const fnHandler of node.functionHandlers) {
2029
- scanFunctionHandler5(fnHandler, registry);
2048
+ scanFunctionHandler5(fnHandler, registry, node.layers);
2030
2049
  }
2031
2050
  }
2032
2051
  }
2033
2052
  __name(scanCustomHandlers, "scanCustomHandlers");
2034
- async function scanClassHandler5(controllerClass, container, registry) {
2035
- const instance = await container.resolve(controllerClass);
2036
- const prototype = Object.getPrototypeOf(instance);
2053
+ async function scanClassHandler5(controllerClass, container, registry, moduleLayers) {
2054
+ const prototype = controllerClass.prototype;
2037
2055
  const methods = Object.getOwnPropertyNames(prototype).filter((name) => name !== "constructor");
2038
2056
  const classLayers = Reflect.getOwnMetadata(LAYER_METADATA, controllerClass) ?? [];
2039
2057
  const classCustomMetadata = Reflect.getOwnMetadata(CUSTOM_METADATA, controllerClass) ?? {};
@@ -2046,6 +2064,7 @@ async function scanClassHandler5(controllerClass, container, registry) {
2046
2064
  const descriptor = Object.getOwnPropertyDescriptor(prototype, methodName);
2047
2065
  if (!descriptor?.value || typeof descriptor.value !== "function") continue;
2048
2066
  const layers = [
2067
+ ...moduleLayers,
2049
2068
  ...classLayers,
2050
2069
  ...methodLayers
2051
2070
  ];
@@ -2066,16 +2085,17 @@ async function scanClassHandler5(controllerClass, container, registry) {
2066
2085
  ...methodCustomMetadata
2067
2086
  },
2068
2087
  handlerFn: descriptor.value,
2069
- handlerInstance: instance
2088
+ controllerClass
2070
2089
  });
2071
2090
  }
2072
2091
  }
2073
2092
  __name(scanClassHandler5, "scanClassHandler");
2074
- function scanFunctionHandler5(definition, registry) {
2093
+ function scanFunctionHandler5(definition, registry, moduleLayers) {
2075
2094
  if (definition.type !== "custom") return;
2076
2095
  const meta = definition.metadata;
2077
2096
  const name = meta.name ?? definition.id ?? "default";
2078
2097
  const layers = [
2098
+ ...moduleLayers,
2079
2099
  ...meta.layers ?? []
2080
2100
  ];
2081
2101
  if (meta.schema) {
@@ -2272,6 +2292,28 @@ function buildHttpContext(request, metadata, container, logger) {
2272
2292
  }
2273
2293
  __name(buildHttpContext, "buildHttpContext");
2274
2294
 
2295
+ // src/handlers/types.ts
2296
+ async function resolveHandlerInstance(handler, container) {
2297
+ if (handler.handlerInstance) return handler.handlerInstance;
2298
+ if (!handler.controllerClass) {
2299
+ throw new Error("Handler has no controllerClass for deferred resolution");
2300
+ }
2301
+ const instance = await container.resolve(handler.controllerClass);
2302
+ handler.handlerInstance = instance;
2303
+ return instance;
2304
+ }
2305
+ __name(resolveHandlerInstance, "resolveHandlerInstance");
2306
+ async function resolveGuardInstance(guard, container) {
2307
+ if (guard.handlerInstance) return guard.handlerInstance;
2308
+ if (!guard.guardClass) {
2309
+ throw new Error("Guard has no guardClass for deferred resolution");
2310
+ }
2311
+ const instance = await container.resolve(guard.guardClass);
2312
+ guard.handlerInstance = instance;
2313
+ return instance;
2314
+ }
2315
+ __name(resolveGuardInstance, "resolveGuardInstance");
2316
+
2275
2317
  // src/handlers/http-pipeline.ts
2276
2318
  var debug12 = createDebug12("celerity:core:pipeline");
2277
2319
  async function executeHttpPipeline(handler, request, options) {
@@ -2322,6 +2364,9 @@ async function executeHttpPipeline(handler, request, options) {
2322
2364
  error: message,
2323
2365
  ...error instanceof Error && error.stack ? {
2324
2366
  stack: error.stack
2367
+ } : {},
2368
+ ...error instanceof Error && error.cause ? {
2369
+ cause: String(error.cause)
2325
2370
  } : {}
2326
2371
  });
2327
2372
  } else {
@@ -2347,7 +2392,8 @@ async function invokeClassHandler(handler, context) {
2347
2392
  for (const meta of sorted) {
2348
2393
  args[meta.index] = extractValidatedParam(meta.type, meta.key, context.request, context.metadata);
2349
2394
  }
2350
- return handler.handlerFn.apply(handler.handlerInstance, args);
2395
+ const instance = await resolveHandlerInstance(handler, context.container);
2396
+ return handler.handlerFn.apply(instance, args);
2351
2397
  }
2352
2398
  __name(invokeClassHandler, "invokeClassHandler");
2353
2399
  var VALIDATED_METADATA_KEYS = {
@@ -2446,6 +2492,9 @@ async function executeWebSocketPipeline(handler, message, options) {
2446
2492
  eventType: message.eventType,
2447
2493
  ...error instanceof Error && error.stack ? {
2448
2494
  stack: error.stack
2495
+ } : {},
2496
+ ...error instanceof Error && error.cause ? {
2497
+ cause: String(error.cause)
2449
2498
  } : {}
2450
2499
  });
2451
2500
  } else {
@@ -2462,7 +2511,8 @@ async function invokeClassHandler2(handler, context) {
2462
2511
  for (const meta of sorted) {
2463
2512
  args[meta.index] = extractWebSocketParam(meta, context);
2464
2513
  }
2465
- await handler.handlerFn.apply(handler.handlerInstance, args);
2514
+ const instance = await resolveHandlerInstance(handler, context.container);
2515
+ await handler.handlerFn.apply(instance, args);
2466
2516
  }
2467
2517
  __name(invokeClassHandler2, "invokeClassHandler");
2468
2518
  async function invokeFunctionHandler2(handler, context) {
@@ -2560,6 +2610,9 @@ async function executeConsumerPipeline(handler, event, options) {
2560
2610
  messageCount: event.messages.length,
2561
2611
  ...error instanceof Error && error.stack ? {
2562
2612
  stack: error.stack
2613
+ } : {},
2614
+ ...error instanceof Error && error.cause ? {
2615
+ cause: String(error.cause)
2563
2616
  } : {}
2564
2617
  });
2565
2618
  } else {
@@ -2581,7 +2634,8 @@ async function invokeClassHandler3(handler, context, validatedMessages, rawMessa
2581
2634
  for (const meta of sorted) {
2582
2635
  args[meta.index] = extractConsumerParam(meta, context, validatedMessages, rawMessages);
2583
2636
  }
2584
- return await handler.handlerFn.apply(handler.handlerInstance, args);
2637
+ const instance = await resolveHandlerInstance(handler, context.container);
2638
+ return await handler.handlerFn.apply(instance, args);
2585
2639
  }
2586
2640
  __name(invokeClassHandler3, "invokeClassHandler");
2587
2641
  async function invokeFunctionHandler3(handler, context, validatedMessages, _rawMessages) {
@@ -2654,6 +2708,9 @@ async function executeSchedulePipeline(handler, event, options) {
2654
2708
  scheduleId: event.scheduleId,
2655
2709
  ...error instanceof Error && error.stack ? {
2656
2710
  stack: error.stack
2711
+ } : {},
2712
+ ...error instanceof Error && error.cause ? {
2713
+ cause: String(error.cause)
2657
2714
  } : {}
2658
2715
  });
2659
2716
  } else {
@@ -2675,7 +2732,8 @@ async function invokeClassHandler4(handler, context, validatedInput) {
2675
2732
  for (const meta of sorted) {
2676
2733
  args[meta.index] = extractScheduleParam(meta, context, validatedInput);
2677
2734
  }
2678
- return await handler.handlerFn.apply(handler.handlerInstance, args);
2735
+ const instance = await resolveHandlerInstance(handler, context.container);
2736
+ return await handler.handlerFn.apply(instance, args);
2679
2737
  }
2680
2738
  __name(invokeClassHandler4, "invokeClassHandler");
2681
2739
  async function invokeFunctionHandler4(handler, context, _validatedInput) {
@@ -2746,7 +2804,8 @@ async function invokeClassHandler5(handler, context, validatedPayload) {
2746
2804
  for (const meta of sorted) {
2747
2805
  args[meta.index] = extractCustomParam(meta, context, validatedPayload);
2748
2806
  }
2749
- return handler.handlerFn.apply(handler.handlerInstance, args);
2807
+ const instance = await resolveHandlerInstance(handler, context.container);
2808
+ return handler.handlerFn.apply(instance, args);
2750
2809
  }
2751
2810
  __name(invokeClassHandler5, "invokeClassHandler");
2752
2811
  async function invokeFunctionHandler5(handler, context, validatedPayload) {
@@ -3103,7 +3162,7 @@ function createScheduleHandler(configOrString, configOrHandler, maybeHandler) {
3103
3162
  if (isScheduleExpression2(configOrString)) {
3104
3163
  config.schedule = configOrString;
3105
3164
  } else {
3106
- config.scheduleId = configOrString;
3165
+ config.source = configOrString;
3107
3166
  }
3108
3167
  } else {
3109
3168
  config = configOrString;
@@ -3114,7 +3173,7 @@ function createScheduleHandler(configOrString, configOrHandler, maybeHandler) {
3114
3173
  inject: config.inject ?? [],
3115
3174
  customMetadata: config.metadata ?? {}
3116
3175
  };
3117
- if (config.scheduleId !== void 0) metadata.scheduleId = config.scheduleId;
3176
+ if (config.source !== void 0) metadata.source = config.source;
3118
3177
  if (config.schedule !== void 0) metadata.schedule = config.schedule;
3119
3178
  if (config.schema !== void 0) metadata.schema = config.schema;
3120
3179
  return {
@@ -3237,7 +3296,7 @@ function buildResolvedFromExport(handlerId, handlerType, handlerFn, fnDef) {
3237
3296
  return {
3238
3297
  ...base,
3239
3298
  type: "schedule",
3240
- handlerTag: meta?.scheduleId ?? handlerId
3299
+ handlerTag: meta?.source ?? handlerId
3241
3300
  };
3242
3301
  case "custom":
3243
3302
  return {
@@ -3356,7 +3415,8 @@ async function invokeClassGuard(guard, input, metadata, options, logger) {
3356
3415
  args[i] = guardContext;
3357
3416
  }
3358
3417
  }
3359
- return guard.handlerFn.apply(guard.handlerInstance, args);
3418
+ const instance = await resolveGuardInstance(guard, options.container);
3419
+ return guard.handlerFn.apply(instance, args);
3360
3420
  }
3361
3421
  __name(invokeClassGuard, "invokeClassGuard");
3362
3422
  async function invokeFunctionGuard(guard, input, metadata, options, logger) {
@@ -3748,22 +3808,37 @@ __name(registerWebSocketHandlers, "registerWebSocketHandlers");
3748
3808
  async function registerConsumerHandlers(app, consumers, result) {
3749
3809
  for (const consumer of consumers?.consumers ?? []) {
3750
3810
  for (const def of consumer.handlers) {
3751
- const tag = `source::${consumer.sourceId}::${def.name}`;
3752
- const callback = result.createConsumerCallback(tag, def.name) ?? await result.createConsumerCallbackById(def.handler, def.location, def.name);
3811
+ const methodName = def.handler.split(".").pop() ?? def.name;
3812
+ const lookupKey = `${consumer.consumerName}::${methodName}`;
3813
+ const callback = result.createConsumerCallback(lookupKey, def.name) ?? await result.createConsumerCallbackById(def.handler, def.location, def.name);
3753
3814
  if (callback) {
3754
- app.registerConsumerHandler(tag, def.timeout, callback);
3815
+ app.registerConsumerHandler(def.name, def.timeout, callback);
3755
3816
  }
3756
3817
  }
3757
3818
  }
3758
3819
  }
3759
3820
  __name(registerConsumerHandlers, "registerConsumerHandlers");
3821
+ async function registerEventHandlers(app, events, result) {
3822
+ for (const event of events?.events ?? []) {
3823
+ for (const def of event.handlers) {
3824
+ const methodName = def.handler.split(".").pop() ?? def.name;
3825
+ const lookupKey = `${event.consumerName}::${methodName}`;
3826
+ const callback = result.createConsumerCallback(lookupKey, def.name) ?? await result.createConsumerCallbackById(def.handler, def.location, def.name);
3827
+ if (callback) {
3828
+ app.registerConsumerHandler(def.name, def.timeout, callback);
3829
+ }
3830
+ }
3831
+ }
3832
+ }
3833
+ __name(registerEventHandlers, "registerEventHandlers");
3760
3834
  async function registerScheduleHandlers(app, schedules, result) {
3761
3835
  for (const schedule of schedules?.schedules ?? []) {
3762
3836
  for (const def of schedule.handlers) {
3763
- const tag = `source::${schedule.scheduleId}::${def.name}`;
3764
- const callback = result.createScheduleCallback(tag, def.name) ?? await result.createScheduleCallbackById(def.handler, def.location, def.name);
3837
+ const methodName = def.handler.split(".").pop() ?? def.name;
3838
+ const lookupKey = `${schedule.scheduleId}::${methodName}`;
3839
+ const callback = result.createScheduleCallback(lookupKey, def.name) ?? await result.createScheduleCallbackById(def.handler, def.location, def.name);
3765
3840
  if (callback) {
3766
- app.registerScheduleHandler(tag, def.timeout, callback);
3841
+ app.registerScheduleHandler(def.name, def.timeout, callback);
3767
3842
  }
3768
3843
  }
3769
3844
  }
@@ -3785,6 +3860,7 @@ async function startRuntime(options) {
3785
3860
  await registerGuardHandlers(app, appConfig.api?.guards, result);
3786
3861
  await registerWebSocketHandlers(app, appConfig.api?.websocket, result);
3787
3862
  await registerConsumerHandlers(app, appConfig.consumers, result);
3863
+ await registerEventHandlers(app, appConfig.events, result);
3788
3864
  await registerScheduleHandlers(app, appConfig.schedules, result);
3789
3865
  await registerCustomHandlers(app, appConfig.customHandlers, result);
3790
3866
  if (appConfig.api?.websocket) {