raiton 6.8.6-beta.21 → 6.8.9-beta.25

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": "raiton",
3
- "version": "6.8.6-beta.21",
3
+ "version": "6.8.9-beta.25",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "description": "Modular TypeScript Backend Framework with Dependency Injection, Plugins, Controllers, Middleware and Hot Reloading",
@@ -41,6 +41,7 @@
41
41
  "dev:node": "node build/bin/index.mjs dev",
42
42
  "dev:deno": "deno task dev",
43
43
  "build": "bun build source/bin/index.ts --target=node --format=esm --outfile=build/bin/index.mjs",
44
+ "test": "bun test tests",
44
45
  "start": "bun run source/bin/index.ts start",
45
46
  "start:node": "node build/bin/index.mjs start",
46
47
  "start:deno": "deno task start",
@@ -9,6 +9,7 @@ import {RaitonThread} from "../thread.ts";
9
9
  import {Injection} from "../injection/index.ts";
10
10
  import {isControllerArtifact, isSocketArtifact, isMcpArtifact, isSchedulerArtifact} from "../../framework/index.ts";
11
11
  import {Artifacts} from "../../framework/artifacts.ts";
12
+ import {METADATA_KEYS} from "../../framework/constants/index.ts";
12
13
  import path from "node:path";
13
14
  import {ControllerRouteTracker} from "./tracker.ts";
14
15
 
@@ -58,7 +59,7 @@ export class ControllerBuilder {
58
59
  return undefined;
59
60
 
60
61
  const imported = await import(`${filename}?v=${version || 1}&t=${timestamp || Date.now()}`)
61
- const artifact = imported.default || imported || undefined;
62
+ const artifact = resolveArtifact(imported, {isController, isSocket, isMcp, isScheduler});
62
63
 
63
64
  if (!artifact) return undefined;
64
65
  if (!RaitonThread.current?.application) return undefined;
@@ -104,3 +105,43 @@ export class ControllerBuilder {
104
105
  }
105
106
  }
106
107
  }
108
+
109
+ interface ArtifactKinds {
110
+ isController: boolean;
111
+ isSocket: boolean;
112
+ isMcp: boolean;
113
+ isScheduler: boolean;
114
+ }
115
+ /**
116
+ * Sélectionne la classe réellement décorée parmi les exports d'un fichier
117
+ * d'artefact.
118
+ *
119
+ * Les fichiers peuvent exporter leur artefact en `default` **ou** en export
120
+ * nommé (les exemples de la documentation Raiton utilisent `export class
121
+ * MathServer`). L'ancien `imported.default || imported` retombait sur l'objet
122
+ * namespace du module pour un export nommé : `compileMcp` enregistrait alors un
123
+ * serveur fantôme (nom et métadonnées vides) sur le même chemin HTTP.
124
+ */
125
+ export function resolveArtifact(imported: any, kinds: ArtifactKinds): any {
126
+ if (!imported || typeof imported !== 'object') return imported;
127
+
128
+ const keys: symbol[] = [];
129
+ if (kinds.isController) keys.push(METADATA_KEYS.CONTROLLERS);
130
+ if (kinds.isSocket) keys.push(METADATA_KEYS.SOCKETS);
131
+ if (kinds.isMcp) keys.push(METADATA_KEYS.MCP);
132
+ if (kinds.isScheduler) keys.push(METADATA_KEYS.SCHEDULER);
133
+
134
+ const decorated = Object.values(imported)
135
+ .find((candidate) => isDecoratedArtifact(candidate, keys));
136
+
137
+ return decorated ?? imported.default ?? imported;
138
+ }
139
+
140
+ function isDecoratedArtifact(candidate: any, keys: symbol[]): boolean {
141
+ if (typeof candidate !== 'function') return false;
142
+ const prototype = candidate.prototype;
143
+ return keys.some((key) =>
144
+ (prototype ? Boolean(Reflect.getMetadata(key, prototype)) : false)
145
+ || Boolean(Reflect.getMetadata(key, candidate)),
146
+ );
147
+ }
@@ -6,6 +6,47 @@ import {isArtifact} from "./utilities/index.ts";
6
6
  import {compileMcp} from "../core/mcp/builder.ts";
7
7
  import {compileScheduler} from "../core/scheduler/builder.ts";
8
8
 
9
+ /**
10
+ * Un artefact middleware/hook peut être exporté sous deux formes : une classe
11
+ * (instanciée par le conteneur) ou une fonction `(context) => …`. Instancier
12
+ * systématiquement lève « function is not a constructor » dès que la fonction
13
+ * est fléchée ou `async` — et un `function` classique, lui constructible, ne
14
+ * doit pas être instancié non plus. On ne reconnaît donc que la syntaxe classe.
15
+ */
16
+ function isClass(value: unknown): value is ConstructorType {
17
+ if (typeof value !== 'function') return false
18
+ if (!/^\s*class[\s{]/.test(Function.prototype.toString.call(value))) return false
19
+ try {
20
+ Reflect.construct(Object, [], value)
21
+ return true
22
+ } catch {
23
+ return false
24
+ }
25
+ }
26
+
27
+ /**
28
+ * Un artefact de middleware *décorateur* n'est pas enregistré dans le pipeline
29
+ * racine : le remplacer l'y ajouterait et le ferait exécuter sur **toutes** les
30
+ * requêtes. On ne remplace donc que les entrées déjà présentes.
31
+ */
32
+ function isRegistered(registry: any, name: string): boolean {
33
+ const normalized = name.toLowerCase()
34
+ const stack = registry?.stack
35
+ if (Array.isArray(stack)) {
36
+ return stack.some((entry: any) => {
37
+ const entryName = typeof entry === 'function' ? entry.name : entry?.name
38
+ return typeof entryName === 'string' && entryName.toLowerCase() === normalized
39
+ })
40
+ }
41
+ const hooks = registry?.hooks
42
+ if (hooks instanceof Map) {
43
+ return [...hooks.keys()].some(
44
+ (key) => typeof key === 'string' && key.toLowerCase() === normalized
45
+ )
46
+ }
47
+ return false
48
+ }
49
+
9
50
  export type HmrChannel =
10
51
  | 'hmr:di'
11
52
  | 'hmr:controller'
@@ -179,9 +220,9 @@ export class Artifacts {
179
220
  typeof mod === 'function' ? mod.name ?? mod.constructor.name : undefined
180
221
  );
181
222
 
182
- if (typeof name === 'string') {
223
+ if (typeof name === 'string' && isRegistered(rootScope.middleware, name)) {
183
224
  if (typeof mod === 'function') {
184
- const instance = new (mod as ConstructorType)()
225
+ const instance = isClass(mod) ? new (mod as ConstructorType)() : mod
185
226
  rootScope.middleware.replace(name, instance)
186
227
  } else if (typeof mod === 'object' && mod !== null) {
187
228
  rootScope.middleware.replace(name, mod)
@@ -208,9 +249,9 @@ export class Artifacts {
208
249
  typeof mod === 'function' ? mod.name ?? mod.constructor.name : undefined
209
250
  );
210
251
 
211
- if (typeof name === 'string') {
252
+ if (typeof name === 'string' && isRegistered(rootScope.hooks, name)) {
212
253
  if (typeof mod === 'function') {
213
- const instance = new (mod as ConstructorType)()
254
+ const instance = isClass(mod) ? new (mod as ConstructorType)() : mod
214
255
  rootScope.hooks.replace(name, instance)
215
256
  } else if (typeof mod === 'object' && mod !== null) {
216
257
  rootScope.hooks.replace(name, mod)
@@ -0,0 +1,60 @@
1
+ import "reflect-metadata";
2
+ import {describe, expect, it} from "bun:test";
3
+ import {resolveArtifact} from "../source/core/controller/builder.ts";
4
+ import {METADATA_KEYS} from "../source/framework/constants/decorators.constant.ts";
5
+
6
+ const ALL = {isController: false, isSocket: false, isMcp: false, isScheduler: false};
7
+
8
+ function serverMetadata(path = "/mcp") {
9
+ return {
10
+ name: "named-server",
11
+ version: "1.0.0",
12
+ path,
13
+ tools: [],
14
+ prompts: [],
15
+ resources: [],
16
+ resourceTemplates: [],
17
+ };
18
+ }
19
+
20
+ describe("résolution des artefacts exportés", () => {
21
+ it("retrouve un serveur MCP exporté en nommé", () => {
22
+ class MathServer {
23
+ add() {
24
+ return 1;
25
+ }
26
+ }
27
+
28
+ Reflect.defineMetadata(METADATA_KEYS.MCP, serverMetadata(), MathServer.prototype);
29
+
30
+ const artifact = resolveArtifact({MathServer}, {...ALL, isMcp: true});
31
+
32
+ expect(artifact).toBe(MathServer);
33
+ });
34
+
35
+ it("retrouve un contrôleur exporté en nommé, même avec d’autres exports", () => {
36
+ class ListUsersController {}
37
+ class Unused {}
38
+
39
+ Reflect.defineMetadata(METADATA_KEYS.CONTROLLERS, {prefix: "/users", routes: [], middlewares: {}}, ListUsersController.prototype);
40
+
41
+ const artifact = resolveArtifact(
42
+ {ListUsersController, Unused, helper: () => undefined},
43
+ {...ALL, isController: true},
44
+ );
45
+
46
+ expect(artifact).toBe(ListUsersController);
47
+ });
48
+
49
+ it("conserve l’export default quand aucun artefact n’est décoré", () => {
50
+ class Plain {}
51
+
52
+ expect(resolveArtifact({default: Plain, other: 1}, {...ALL, isMcp: true})).toBe(Plain);
53
+ });
54
+
55
+ it("retombe sur le module importé quand aucun export n’est exploitable", () => {
56
+ const imported = {helper: () => undefined};
57
+
58
+ expect(resolveArtifact(imported, {...ALL, isMcp: true})).toBe(imported);
59
+ });
60
+ });