@meridianjs/framework 0.1.5 → 0.1.7

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.d.ts CHANGED
@@ -191,4 +191,24 @@ declare const apiRateLimit: express_rate_limit.RateLimitRequestHandler;
191
191
  */
192
192
  declare function validate(schema: ZodSchema): (req: Request, res: Response, next: NextFunction) => void;
193
193
 
194
- export { type BootstrapOptions, ConsoleLogger, type MeridianApp, type MiddlewareRoute, type MiddlewaresConfig, apiRateLimit, authRateLimit, bootstrap, createMeridianContainer, createServer, defineConfig, defineMiddlewares, loadConfig, loadJobs, loadLinks, loadModules, loadPlugins, loadRoutes, loadSubscribers, resolveModuleDefinition, validate };
194
+ /**
195
+ * Manages Server-Sent Event (SSE) connections grouped by workspace.
196
+ * Subscribers call broadcast() after mutations; the frontend receives live updates
197
+ * and invalidates the relevant TanStack Query cache entries.
198
+ */
199
+ declare class SseManager {
200
+ private clients;
201
+ /**
202
+ * Registers a response as an SSE client for the given workspace.
203
+ * Returns an unsubscribe function to call on connection close.
204
+ */
205
+ subscribe(workspaceId: string, res: Response): () => void;
206
+ /**
207
+ * Sends an SSE event to all connected clients in the given workspace.
208
+ */
209
+ broadcast(workspaceId: string, event: string, data: unknown): void;
210
+ }
211
+ /** Singleton shared across all routes and subscribers. */
212
+ declare const sseManager: SseManager;
213
+
214
+ export { type BootstrapOptions, ConsoleLogger, type MeridianApp, type MiddlewareRoute, type MiddlewaresConfig, SseManager, apiRateLimit, authRateLimit, bootstrap, createMeridianContainer, createServer, defineConfig, defineMiddlewares, loadConfig, loadJobs, loadLinks, loadModules, loadPlugins, loadRoutes, loadSubscribers, resolveModuleDefinition, sseManager, validate };
package/dist/index.js CHANGED
@@ -32,6 +32,9 @@ function wrapContainer(raw) {
32
32
  },
33
33
  createScope() {
34
34
  return wrapContainer(raw.createScope());
35
+ },
36
+ async dispose() {
37
+ await raw.dispose();
35
38
  }
36
39
  };
37
40
  return container;
@@ -506,6 +509,7 @@ import cors from "cors";
506
509
  import helmet from "helmet";
507
510
  function createServer(container, config) {
508
511
  const app = express();
512
+ app.set("trust proxy", 1);
509
513
  const logger = container.resolve("logger");
510
514
  app.use(express.json({ limit: "10mb" }));
511
515
  app.use(express.urlencoded({ extended: true, limit: "10mb" }));
@@ -536,8 +540,10 @@ function createServer(container, config) {
536
540
  exposedHeaders: ["X-RateLimit-Limit", "X-RateLimit-Remaining", "X-RateLimit-Reset"]
537
541
  })
538
542
  );
539
- app.use((req, _res, next) => {
543
+ app.use((req, res, next) => {
540
544
  req.scope = container.createScope();
545
+ res.on("finish", () => req.scope.dispose?.());
546
+ res.on("close", () => req.scope.dispose?.());
541
547
  next();
542
548
  });
543
549
  app.get("/health", (_req, res) => {
@@ -657,18 +663,34 @@ async function bootstrap(opts) {
657
663
  await bus.close?.();
658
664
  } catch {
659
665
  }
666
+ try {
667
+ const scheduler = container.resolve("scheduler");
668
+ await scheduler.close();
669
+ } catch {
670
+ }
660
671
  logger.info("Meridian server stopped.");
661
672
  }
662
673
  };
663
674
  }
664
675
  async function loadMiddlewares(server, container, apiDir) {
665
676
  const logger = container.resolve("logger");
666
- const middlewaresPath = path8.join(apiDir, "middlewares.ts");
667
- let mod;
668
- try {
669
- const { pathToFileURL: pathToFileURL8 } = await import("url");
670
- mod = await import(pathToFileURL8(middlewaresPath).href);
671
- } catch {
677
+ const candidates = [
678
+ path8.join(apiDir, "middlewares.ts"),
679
+ path8.join(apiDir, "middlewares.mts"),
680
+ path8.join(apiDir, "middlewares.js"),
681
+ path8.join(apiDir, "middlewares.mjs")
682
+ ];
683
+ let mod = null;
684
+ const { pathToFileURL: pathToFileURL8 } = await import("url");
685
+ for (const candidate of candidates) {
686
+ try {
687
+ mod = await import(pathToFileURL8(candidate).href);
688
+ break;
689
+ } catch (err) {
690
+ if (err.code !== "ERR_MODULE_NOT_FOUND" && err.code !== "MODULE_NOT_FOUND") throw err;
691
+ }
692
+ }
693
+ if (!mod) {
672
694
  return;
673
695
  }
674
696
  const config = mod.default;
@@ -724,8 +746,56 @@ function validate(schema) {
724
746
  next();
725
747
  };
726
748
  }
749
+
750
+ // src/sse-manager.ts
751
+ var SseManager = class {
752
+ clients = /* @__PURE__ */ new Map();
753
+ /**
754
+ * Registers a response as an SSE client for the given workspace.
755
+ * Returns an unsubscribe function to call on connection close.
756
+ */
757
+ subscribe(workspaceId, res) {
758
+ if (!this.clients.has(workspaceId)) {
759
+ this.clients.set(workspaceId, /* @__PURE__ */ new Set());
760
+ }
761
+ this.clients.get(workspaceId).add(res);
762
+ return () => {
763
+ const set = this.clients.get(workspaceId);
764
+ if (set) {
765
+ set.delete(res);
766
+ if (set.size === 0) this.clients.delete(workspaceId);
767
+ }
768
+ };
769
+ }
770
+ /**
771
+ * Sends an SSE event to all connected clients in the given workspace.
772
+ */
773
+ broadcast(workspaceId, event, data) {
774
+ const set = this.clients.get(workspaceId);
775
+ if (!set?.size) return;
776
+ const safeEvent = event.replace(/[\r\n]/g, "");
777
+ const payload = `event: ${safeEvent}
778
+ data: ${JSON.stringify(data)}
779
+
780
+ `;
781
+ const dead = [];
782
+ for (const res of set) {
783
+ try {
784
+ res.write(payload);
785
+ } catch {
786
+ dead.push(res);
787
+ }
788
+ }
789
+ for (const res of dead) {
790
+ set.delete(res);
791
+ }
792
+ if (set.size === 0) this.clients.delete(workspaceId);
793
+ }
794
+ };
795
+ var sseManager = new SseManager();
727
796
  export {
728
797
  ConsoleLogger,
798
+ SseManager,
729
799
  apiRateLimit,
730
800
  asClass,
731
801
  asFunction,
@@ -744,5 +814,6 @@ export {
744
814
  loadRoutes,
745
815
  loadSubscribers,
746
816
  resolveModuleDefinition,
817
+ sseManager,
747
818
  validate
748
819
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@meridianjs/framework",
3
- "version": "0.1.5",
3
+ "version": "0.1.7",
4
4
  "description": "Core Meridian framework: bootstrap, DI container, module/route/subscriber/job loaders",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",
@@ -8,8 +8,10 @@
8
8
  "type": "module",
9
9
  "exports": {
10
10
  ".": {
11
- "types": "./dist/index.d.ts",
12
- "import": "./dist/index.js"
11
+ "import": {
12
+ "types": "./dist/index.d.ts",
13
+ "default": "./dist/index.js"
14
+ }
13
15
  }
14
16
  },
15
17
  "scripts": {