raiton 6.0.1-beta.2 → 6.1.0-beta.4

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.
@@ -4817,7 +4817,7 @@ var {
4817
4817
  Help
4818
4818
  } = import__.default;
4819
4819
  // package.json
4820
- var version = "6.0.1-beta.2";
4820
+ var version = "6.1.0-beta.4";
4821
4821
 
4822
4822
  // source/bin/cli.ts
4823
4823
  var CLI = new Command;
@@ -6597,6 +6597,29 @@ class Injection {
6597
6597
  this._classes.set(name_, { ...this._classes.get(this.normalizeName(name)), construct });
6598
6598
  return this;
6599
6599
  }
6600
+ static invalidate(name) {
6601
+ const name_ = this.normalizeName(name);
6602
+ if (this._instances.has(name_)) {
6603
+ const scopeInstances = this._instances.get(name_);
6604
+ for (const [scope, instance] of scopeInstances) {
6605
+ if (typeof instance.onUnmount === "function") {
6606
+ try {
6607
+ instance.onUnmount();
6608
+ } catch {}
6609
+ }
6610
+ }
6611
+ this._instances.delete(name_);
6612
+ }
6613
+ return this;
6614
+ }
6615
+ static invalidateCascade(name) {
6616
+ this.invalidate(name);
6617
+ const dependents = this.getDependents(name);
6618
+ for (const dep of dependents) {
6619
+ this.invalidateCascade(dep);
6620
+ }
6621
+ return this;
6622
+ }
6600
6623
  static getDependents(name) {
6601
6624
  return Array.from(this._dependents.get(this.normalizeName(name)) || []);
6602
6625
  }
@@ -6736,6 +6759,8 @@ class Injection {
6736
6759
  return this.get(name);
6737
6760
  }
6738
6761
  }
6762
+ // source/core/controller/tracker.ts
6763
+ var controllerRouteMap = new Map;
6739
6764
  // source/core/socket/metadata.ts
6740
6765
  var import_reflect_metadata3 = __toESM(require_Reflect2(), 1);
6741
6766
  var registry = new Map;
@@ -7019,6 +7044,36 @@ class HookStore {
7019
7044
  list.push(handler2);
7020
7045
  this.hooks.set(name, list);
7021
7046
  }
7047
+ remove(name, handler2) {
7048
+ if (!this.hooks.has(name))
7049
+ return;
7050
+ if (!handler2) {
7051
+ this.hooks.delete(name);
7052
+ return;
7053
+ }
7054
+ const list = this.hooks.get(name);
7055
+ const idx = list.indexOf(handler2);
7056
+ if (idx !== -1)
7057
+ list.splice(idx, 1);
7058
+ if (list.length === 0)
7059
+ this.hooks.delete(name);
7060
+ else
7061
+ this.hooks.set(name, list);
7062
+ }
7063
+ replace(name, oldHandler, newHandler) {
7064
+ if (!this.hooks.has(name)) {
7065
+ this.add(name, newHandler);
7066
+ return;
7067
+ }
7068
+ const list = this.hooks.get(name);
7069
+ const idx = list.indexOf(oldHandler);
7070
+ if (idx !== -1) {
7071
+ list[idx] = newHandler;
7072
+ } else {
7073
+ list.push(newHandler);
7074
+ }
7075
+ this.hooks.set(name, list);
7076
+ }
7022
7077
  async run(name, ctx) {
7023
7078
  const list = this.hooks.get(name);
7024
7079
  if (!list)
@@ -7067,6 +7122,35 @@ class MiddlewarePipeline {
7067
7122
  this.stack.push(mw);
7068
7123
  return this;
7069
7124
  }
7125
+ replace(name, mw) {
7126
+ const normalized = name.toLowerCase();
7127
+ for (let i = 0;i < this.stack.length; i++) {
7128
+ const entry = this.stack[i];
7129
+ let entryName;
7130
+ if (typeof entry === "function") {
7131
+ entryName = entry.name?.toLowerCase();
7132
+ } else if (typeof entry === "object" && entry !== null && "name" in entry) {
7133
+ entryName = entry.name?.toLowerCase();
7134
+ }
7135
+ if (entryName === normalized) {
7136
+ this.stack[i] = mw;
7137
+ return this;
7138
+ }
7139
+ }
7140
+ this.stack.push(mw);
7141
+ return this;
7142
+ }
7143
+ remove(name) {
7144
+ const normalized = name.toLowerCase();
7145
+ this.stack = this.stack.filter((entry) => {
7146
+ if (typeof entry === "function")
7147
+ return entry.name?.toLowerCase() !== normalized;
7148
+ if (typeof entry === "object" && entry !== null && "name" in entry)
7149
+ return entry.name?.toLowerCase() !== normalized;
7150
+ return true;
7151
+ });
7152
+ return this;
7153
+ }
7070
7154
  clear() {
7071
7155
  this.stack = [];
7072
7156
  return this;
@@ -7260,6 +7344,35 @@ class RaitonDirectories {
7260
7344
  }
7261
7345
 
7262
7346
  // source/framework/artifacts.ts
7347
+ var HMR_CHANNELS = {
7348
+ "hmr:di": [
7349
+ "service",
7350
+ "provider",
7351
+ "type",
7352
+ "interface",
7353
+ "repository",
7354
+ "database",
7355
+ "db",
7356
+ "util",
7357
+ "utility",
7358
+ "source",
7359
+ "use-case",
7360
+ "dto",
7361
+ "vm",
7362
+ "entity",
7363
+ "model",
7364
+ "schema",
7365
+ "validator",
7366
+ "strategy",
7367
+ "strategy-provider",
7368
+ "strategy-type"
7369
+ ],
7370
+ "hmr:controller": ["controller"],
7371
+ "hmr:socket": ["socket"],
7372
+ "hmr:middleware": ["middleware"],
7373
+ "hmr:hook": ["hook", "event", "listener"]
7374
+ };
7375
+
7263
7376
  class Artifacts {
7264
7377
  static types = new Set;
7265
7378
  static defaultTypes = [
@@ -7285,6 +7398,7 @@ class Artifacts {
7285
7398
  "strategy-type",
7286
7399
  "use-case",
7287
7400
  "dto",
7401
+ "vm",
7288
7402
  "entity",
7289
7403
  "model",
7290
7404
  "schema",
@@ -7302,6 +7416,16 @@ class Artifacts {
7302
7416
  static is(filename) {
7303
7417
  return [...this.types].map((type) => isArtifact(filename, type)).some(Boolean);
7304
7418
  }
7419
+ static classify(filename) {
7420
+ for (const [channel, types] of Object.entries(HMR_CHANNELS)) {
7421
+ for (const type of types) {
7422
+ if (isArtifact(filename, type)) {
7423
+ return { channel, artifactType: type };
7424
+ }
7425
+ }
7426
+ }
7427
+ return null;
7428
+ }
7305
7429
  static reload(modulo, filename) {
7306
7430
  if (Raiton.thread?.builder.options.serve === false)
7307
7431
  return Logger.warn("Artifact reload is only available in development mode");
@@ -7314,6 +7438,61 @@ class Artifacts {
7314
7438
  }
7315
7439
  }
7316
7440
  }
7441
+ static reloadDi(modulo, filename) {
7442
+ if (Raiton.thread?.builder.options.serve === false)
7443
+ return Logger.warn("Artifact reload is only available in development mode");
7444
+ for (const mod of Object.values(modulo)) {
7445
+ const name = mod && typeof mod === "object" && "name" in mod ? mod.name : typeof mod === "function" ? mod.name ?? mod.constructor.name : undefined;
7446
+ if (typeof name === "string" && Injection.has(name)) {
7447
+ if (filename)
7448
+ Injection.registerArtifactPath(name, filename);
7449
+ Injection.updateConstruct(name, mod);
7450
+ Injection.invalidateCascade(name);
7451
+ }
7452
+ }
7453
+ }
7454
+ static reloadMiddleware(modulo, filename) {
7455
+ if (Raiton.thread?.builder.options.serve === false)
7456
+ return Logger.warn("Artifact reload is only available in development mode");
7457
+ const app = Raiton.thread?.application;
7458
+ if (!app)
7459
+ return;
7460
+ const rootScope = app.root;
7461
+ if (!rootScope?.middleware)
7462
+ return;
7463
+ for (const mod of Object.values(modulo)) {
7464
+ const name = mod && typeof mod === "object" && "name" in mod ? mod.name : typeof mod === "function" ? mod.name ?? mod.constructor.name : undefined;
7465
+ if (typeof name === "string") {
7466
+ if (typeof mod === "function") {
7467
+ const instance = new mod;
7468
+ rootScope.middleware.replace(name, instance);
7469
+ } else if (typeof mod === "object" && mod !== null) {
7470
+ rootScope.middleware.replace(name, mod);
7471
+ }
7472
+ }
7473
+ }
7474
+ }
7475
+ static reloadHook(modulo, filename) {
7476
+ if (Raiton.thread?.builder.options.serve === false)
7477
+ return Logger.warn("Artifact reload is only available in development mode");
7478
+ const app = Raiton.thread?.application;
7479
+ if (!app)
7480
+ return;
7481
+ const rootScope = app.root;
7482
+ if (!rootScope?.hooks)
7483
+ return;
7484
+ for (const mod of Object.values(modulo)) {
7485
+ const name = mod && typeof mod === "object" && "name" in mod ? mod.name : typeof mod === "function" ? mod.name ?? mod.constructor.name : undefined;
7486
+ if (typeof name === "string") {
7487
+ if (typeof mod === "function") {
7488
+ const instance = new mod;
7489
+ rootScope.hooks.replace(name, instance);
7490
+ } else if (typeof mod === "object" && mod !== null) {
7491
+ rootScope.hooks.replace(name, mod);
7492
+ }
7493
+ }
7494
+ }
7495
+ }
7317
7496
  }
7318
7497
  // source/core/command.ts
7319
7498
  class RaitonCommand {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "raiton",
3
- "version": "6.0.1-beta.2",
3
+ "version": "6.1.0-beta.4",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "description": "Modular TypeScript Backend Framework with Dependency Injection, Plugins, Controllers, Middleware and Hot Reloading.",
@@ -5,12 +5,11 @@ import fs, {WatchEventType} from "node:fs";
5
5
  import type {BuilderConfigInterface, BuilderInterface, ThreadInterface,} from "../types";
6
6
  import {RaitonThread} from "./thread";
7
7
  import {Raiton} from "./raiton";
8
- import {isControllerArtifact, isSocketArtifact} from "../framework";
9
8
  import {ControllerBuilder} from "./controller";
10
9
  import {watch} from "fs";
11
10
  import {LBadge, Logger} from "@protorians/logger";
12
11
  import {Throwable} from "../framework/exceptions";
13
- import {Artifacts} from "../framework/artifacts";
12
+ import {Artifacts, HMR_CHANNELS, HmrChannel} from "../framework/artifacts";
14
13
  import {build} from "esbuild";
15
14
  import {execSync, spawn, spawnSync} from "node:child_process";
16
15
 
@@ -53,18 +52,19 @@ export class RaitonBuilder implements BuilderInterface {
53
52
  protected async parse(filename: string, type?: WatchEventType) {
54
53
  if (!fs.existsSync(filename)) return;
55
54
 
55
+ const classification = Artifacts.classify(filename)
56
+ if (!classification) return
57
+
56
58
  const payload = {
57
59
  filename,
58
60
  timestamp: Date.now(),
59
61
  version: this._compiledVersionNumber,
60
- type
62
+ type,
63
+ channel: classification.channel,
64
+ artifactType: classification.artifactType,
61
65
  }
62
66
 
63
- // Logger.log(LBadge.info('HMR'), 'activated');
64
-
65
- if (Artifacts.is(filename) || isControllerArtifact(filename) || isSocketArtifact(filename))
66
- Raiton.signals.dispatch('hmr:artifact', payload)
67
-
67
+ Raiton.signals.dispatch(classification.channel as any, payload)
68
68
  return payload;
69
69
  }
70
70
 
@@ -112,27 +112,72 @@ export class RaitonBuilder implements BuilderInterface {
112
112
  await this.initialize()
113
113
 
114
114
  if (this.options.hmr && this.options.serve) {
115
- Raiton.signals.listen(
116
- 'hmr:artifact',
117
- async ({filename, version, timestamp}) => {
118
- const imported = await import(`${filename}?v=${version || 1}&t=${timestamp || Date.now()}`)
119
-
120
- if (isControllerArtifact(filename) || isSocketArtifact(filename)) {
121
- await ControllerBuilder.build({filename, version, timestamp})
122
- } else if (Artifacts.is(filename)) {
123
- Artifacts.reload(imported, filename)
124
- }
125
-
126
- Logger.log(LBadge.debug('HMR'), (path.relative(this.workdir, filename)))
127
- }
128
- )
129
- // this.parsing();
115
+ this.listenHmrDi()
116
+ this.listenHmrController()
117
+ this.listenHmrSocket()
118
+ this.listenHmrMiddleware()
119
+ this.listenHmrHook()
130
120
  this.watching();
131
121
  }
132
122
 
133
123
  return this;
134
124
  }
135
125
 
126
+ protected listenHmrDi(): void {
127
+ Raiton.signals.listen(
128
+ 'hmr:di',
129
+ async ({filename, version, timestamp}) => {
130
+ const imported = await import(`${filename}?v=${version || 1}&t=${timestamp || Date.now()}`)
131
+ Artifacts.reloadDi(imported, filename)
132
+ Logger.log(LBadge.debug('HMR'), `[di] ${path.relative(this.workdir, filename)}`)
133
+ }
134
+ )
135
+ }
136
+
137
+ protected listenHmrController(): void {
138
+ Raiton.signals.listen(
139
+ 'hmr:controller',
140
+ async ({filename, version, timestamp}) => {
141
+ const imported = await import(`${filename}?v=${version || 1}&t=${timestamp || Date.now()}`)
142
+ await ControllerBuilder.recompile(imported, filename)
143
+ Logger.log(LBadge.debug('HMR'), `[controller] ${path.relative(this.workdir, filename)}`)
144
+ }
145
+ )
146
+ }
147
+
148
+ protected listenHmrSocket(): void {
149
+ Raiton.signals.listen(
150
+ 'hmr:socket',
151
+ async ({filename, version, timestamp}) => {
152
+ const imported = await import(`${filename}?v=${version || 1}&t=${timestamp || Date.now()}`)
153
+ await ControllerBuilder.build({filename, version, timestamp})
154
+ Logger.log(LBadge.debug('HMR'), `[socket] ${path.relative(this.workdir, filename)}`)
155
+ }
156
+ )
157
+ }
158
+
159
+ protected listenHmrMiddleware(): void {
160
+ Raiton.signals.listen(
161
+ 'hmr:middleware',
162
+ async ({filename, version, timestamp}) => {
163
+ const imported = await import(`${filename}?v=${version || 1}&t=${timestamp || Date.now()}`)
164
+ Artifacts.reloadMiddleware(imported, filename)
165
+ Logger.log(LBadge.debug('HMR'), `[middleware] ${path.relative(this.workdir, filename)}`)
166
+ }
167
+ )
168
+ }
169
+
170
+ protected listenHmrHook(): void {
171
+ Raiton.signals.listen(
172
+ 'hmr:hook',
173
+ async ({filename, version, timestamp}) => {
174
+ const imported = await import(`${filename}?v=${version || 1}&t=${timestamp || Date.now()}`)
175
+ Artifacts.reloadHook(imported, filename)
176
+ Logger.log(LBadge.debug('HMR'), `[hook] ${path.relative(this.workdir, filename)}`)
177
+ }
178
+ )
179
+ }
180
+
136
181
  protected async build(thread: ThreadInterface): Promise<this> {
137
182
  if (!this._source) throw new Error('Application source not found');
138
183
  if (!this._out) throw new Error('Application output not found');
@@ -7,6 +7,7 @@ import {RaitonThread} from "../thread";
7
7
  import {Injection} from "../injection";
8
8
  import {isControllerArtifact, isSocketArtifact} from "../../framework";
9
9
  import path from "node:path";
10
+ import {ControllerRouteTracker} from "./tracker";
10
11
 
11
12
  export class ControllerBuilder {
12
13
 
@@ -46,4 +47,25 @@ export class ControllerBuilder {
46
47
  return compilated;
47
48
  }
48
49
 
49
- }
50
+ static async recompile(modulo: any, filename: string): Promise<void> {
51
+ const app = RaitonThread.current?.application
52
+ if (!app) return
53
+
54
+ for (const mod of Object.values(modulo)) {
55
+ const name = (mod && typeof mod === 'object' && 'name' in mod) ? mod.name : (
56
+ typeof mod === 'function' ? mod.name ?? mod.constructor.name : undefined
57
+ );
58
+
59
+ if (typeof name === 'string' && typeof mod === 'function') {
60
+ Injection.invalidateCascade(name)
61
+ const rootScope = (app as any).root
62
+ if (rootScope?.router) {
63
+ ControllerRouteTracker.removeRoutes(name, rootScope.router)
64
+ }
65
+ Injection.updateConstruct(name, mod as any)
66
+ if (filename) Injection.registerArtifactPath(name, filename)
67
+ compileController(mod, app)
68
+ }
69
+ }
70
+ }
71
+ }
@@ -3,6 +3,7 @@ import {getControllerMetadata} from "..";
3
3
  import {createHandler} from "../router";
4
4
  import {Injection} from "../injection";
5
5
  import {ControllerMetaInterface} from "../../types";
6
+ import {ControllerRouteTracker} from "./tracker";
6
7
 
7
8
  export function compileController(
8
9
  ControllerClass: any,
@@ -10,13 +11,23 @@ export function compileController(
10
11
  ) {
11
12
  const instance: any = Injection.resolve<typeof ControllerClass>(ControllerClass)
12
13
  const metadata: ControllerMetaInterface = getControllerMetadata(ControllerClass.prototype)
14
+ const name = ControllerClass.name
13
15
 
14
- for (const route of metadata.routes)
15
- app.route(
16
+ const rootScope = (app as any).root
17
+ if (rootScope?.router) {
18
+ ControllerRouteTracker.removeRoutes(name, rootScope.router)
19
+ }
20
+
21
+ const routes: any[] = []
22
+ for (const route of metadata.routes) {
23
+ const registered = app.route(
16
24
  route.method as any,
17
25
  `${metadata.prefix ?? ''}${route.path}`,
18
26
  createHandler(instance, route, metadata),
19
27
  )
28
+ routes.push(registered)
29
+ }
20
30
 
31
+ ControllerRouteTracker.setRoutes(name, routes)
21
32
  return instance;
22
33
  }
@@ -1,3 +1,4 @@
1
1
  export * from "./metadata"
2
2
  export * from "./compiler"
3
- export * from "./builder"
3
+ export * from "./builder"
4
+ export * from "./tracker"
@@ -0,0 +1,26 @@
1
+ import {Route} from "../router"
2
+
3
+ const controllerRouteMap = new Map<string, Route[]>()
4
+
5
+ export class ControllerRouteTracker {
6
+ static getRoutes(controllerName: string): Route[] {
7
+ return controllerRouteMap.get(controllerName) || []
8
+ }
9
+
10
+ static setRoutes(controllerName: string, routes: Route[]): void {
11
+ controllerRouteMap.set(controllerName, routes)
12
+ }
13
+
14
+ static removeRoutes(controllerName: string, router: { remove(route: Route): void }): void {
15
+ const routes = controllerRouteMap.get(controllerName)
16
+ if (!routes) return
17
+ for (const route of routes) {
18
+ router.remove(route)
19
+ }
20
+ controllerRouteMap.delete(controllerName)
21
+ }
22
+
23
+ static clear(): void {
24
+ controllerRouteMap.clear()
25
+ }
26
+ }
@@ -9,6 +9,34 @@ export class HookStore {
9
9
  this.hooks.set(name, list)
10
10
  }
11
11
 
12
+ remove(name: HookNameType, handler?: HookHandlerCallable) {
13
+ if (!this.hooks.has(name)) return
14
+ if (!handler) {
15
+ this.hooks.delete(name)
16
+ return
17
+ }
18
+ const list = this.hooks.get(name)!
19
+ const idx = list.indexOf(handler)
20
+ if (idx !== -1) list.splice(idx, 1)
21
+ if (list.length === 0) this.hooks.delete(name)
22
+ else this.hooks.set(name, list)
23
+ }
24
+
25
+ replace(name: HookNameType, oldHandler: HookHandlerCallable, newHandler: HookHandlerCallable) {
26
+ if (!this.hooks.has(name)) {
27
+ this.add(name, newHandler)
28
+ return
29
+ }
30
+ const list = this.hooks.get(name)!
31
+ const idx = list.indexOf(oldHandler)
32
+ if (idx !== -1) {
33
+ list[idx] = newHandler
34
+ } else {
35
+ list.push(newHandler)
36
+ }
37
+ this.hooks.set(name, list)
38
+ }
39
+
12
40
  async run(name: HookNameType, ctx: any) {
13
41
  const list = this.hooks.get(name)
14
42
  if (!list) return
@@ -61,6 +61,29 @@ export class Injection {
61
61
  return this;
62
62
  }
63
63
 
64
+ static invalidate(name: string): typeof this {
65
+ const name_ = this.normalizeName(name);
66
+ if (this._instances.has(name_)) {
67
+ const scopeInstances = this._instances.get(name_)!;
68
+ for (const [scope, instance] of scopeInstances) {
69
+ if (typeof instance.onUnmount === 'function') {
70
+ try { instance.onUnmount() } catch { /* noop */ }
71
+ }
72
+ }
73
+ this._instances.delete(name_);
74
+ }
75
+ return this;
76
+ }
77
+
78
+ static invalidateCascade(name: string): typeof this {
79
+ this.invalidate(name);
80
+ const dependents = this.getDependents(name);
81
+ for (const dep of dependents) {
82
+ this.invalidateCascade(dep);
83
+ }
84
+ return this;
85
+ }
86
+
64
87
  static getDependents(name: string): string[] {
65
88
  return Array.from(this._dependents.get(this.normalizeName(name)) || []);
66
89
  }
@@ -9,6 +9,36 @@ export class MiddlewarePipeline {
9
9
  return this;
10
10
  }
11
11
 
12
+ replace(name: string, mw: MiddlewareType): this {
13
+ const normalized = name.toLowerCase()
14
+ for (let i = 0; i < this.stack.length; i++) {
15
+ const entry = this.stack[i]
16
+ let entryName: string | undefined
17
+ if (typeof entry === 'function') {
18
+ entryName = entry.name?.toLowerCase()
19
+ } else if (typeof entry === 'object' && entry !== null && 'name' in entry) {
20
+ entryName = (entry as any).name?.toLowerCase()
21
+ }
22
+ if (entryName === normalized) {
23
+ this.stack[i] = mw
24
+ return this
25
+ }
26
+ }
27
+ this.stack.push(mw)
28
+ return this;
29
+ }
30
+
31
+ remove(name: string): this {
32
+ const normalized = name.toLowerCase()
33
+ this.stack = this.stack.filter(entry => {
34
+ if (typeof entry === 'function') return entry.name?.toLowerCase() !== normalized
35
+ if (typeof entry === 'object' && entry !== null && 'name' in entry)
36
+ return (entry as any).name?.toLowerCase() !== normalized
37
+ return true
38
+ })
39
+ return this;
40
+ }
41
+
12
42
  clear(): this {
13
43
  this.stack = []
14
44
  return this;
@@ -1,5 +1,10 @@
1
- import {registerSocket} from "./metadata";
1
+ import {registerSocket, unregisterSocket, getSocketMetadata} from "./metadata";
2
+ import {Injection} from "../injection";
2
3
 
3
4
  export function compileSocket(SocketClass: any) {
5
+ const metadata = getSocketMetadata(SocketClass.prototype || SocketClass)
6
+ const namespace = metadata.namespace || '/'
7
+ unregisterSocket(namespace)
8
+ Injection.invalidateCascade(SocketClass.name)
4
9
  return registerSocket(SocketClass);
5
10
  }
@@ -28,6 +28,15 @@ export function registerSocket(construct: any): SocketRegistryEntryInterface {
28
28
  return entry;
29
29
  }
30
30
 
31
+ export function unregisterSocket(constructOrNamespace: string | any): boolean {
32
+ if (typeof constructOrNamespace === 'string') {
33
+ return registry.delete(constructOrNamespace);
34
+ }
35
+ const metadata = getSocketMetadata(constructOrNamespace.prototype || constructOrNamespace);
36
+ const namespace = metadata.namespace || '/';
37
+ return registry.delete(namespace);
38
+ }
39
+
31
40
  export function getSocketRegistry(): Map<string, SocketRegistryEntryInterface> {
32
41
  return registry;
33
42
  }
@@ -4,6 +4,47 @@ import type {ConstructorType} from "../types";
4
4
  import {Raiton} from "../core/raiton";
5
5
  import {isArtifact} from "./utilities";
6
6
 
7
+ export type HmrChannel =
8
+ | 'hmr:di'
9
+ | 'hmr:controller'
10
+ | 'hmr:socket'
11
+ | 'hmr:middleware'
12
+ | 'hmr:hook'
13
+
14
+ export interface ArtifactClassification {
15
+ channel: HmrChannel
16
+ artifactType: string
17
+ }
18
+
19
+ export const HMR_CHANNELS: Record<HmrChannel, readonly string[]> = {
20
+ 'hmr:di': [
21
+ 'service',
22
+ 'provider',
23
+ 'type',
24
+ 'interface',
25
+ 'repository',
26
+ 'database',
27
+ 'db',
28
+ 'util',
29
+ 'utility',
30
+ 'source',
31
+ 'use-case',
32
+ 'dto',
33
+ 'vm',
34
+ 'entity',
35
+ 'model',
36
+ 'schema',
37
+ 'validator',
38
+ 'strategy',
39
+ 'strategy-provider',
40
+ 'strategy-type',
41
+ ],
42
+ 'hmr:controller': ['controller'],
43
+ 'hmr:socket': ['socket'],
44
+ 'hmr:middleware': ['middleware'],
45
+ 'hmr:hook': ['hook', 'event', 'listener'],
46
+ }
47
+
7
48
  export class Artifacts {
8
49
 
9
50
  static readonly types: Set<string> = new Set();
@@ -31,6 +72,7 @@ export class Artifacts {
31
72
  'strategy-type',
32
73
  'use-case',
33
74
  'dto',
75
+ 'vm',
34
76
  'entity',
35
77
  'model',
36
78
  'schema',
@@ -53,6 +95,17 @@ export class Artifacts {
53
95
  .some(Boolean)
54
96
  }
55
97
 
98
+ static classify(filename: string): ArtifactClassification | null {
99
+ for (const [channel, types] of Object.entries(HMR_CHANNELS)) {
100
+ for (const type of types) {
101
+ if (isArtifact(filename, type)) {
102
+ return {channel: channel as HmrChannel, artifactType: type}
103
+ }
104
+ }
105
+ }
106
+ return null
107
+ }
108
+
56
109
  static reload(modulo: any, filename?: string) {
57
110
 
58
111
  if (Raiton.thread?.builder.options.serve === false)
@@ -73,4 +126,82 @@ export class Artifacts {
73
126
  }
74
127
 
75
128
  }
129
+
130
+ static reloadDi(modulo: any, filename?: string) {
131
+
132
+ if (Raiton.thread?.builder.options.serve === false)
133
+ return Logger.warn(
134
+ 'Artifact reload is only available in development mode'
135
+ )
136
+
137
+ for (const mod of Object.values(modulo)) {
138
+ const name = (mod && typeof mod === 'object' && 'name' in mod) ? mod.name : (
139
+ typeof mod === 'function' ? mod.name ?? mod.constructor.name : undefined
140
+ );
141
+
142
+ if (typeof name === 'string' && Injection.has(name)) {
143
+ if (filename) Injection.registerArtifactPath(name, filename);
144
+ Injection.updateConstruct(name, mod as ConstructorType)
145
+ Injection.invalidateCascade(name)
146
+ }
147
+ }
148
+ }
149
+
150
+ static reloadMiddleware(modulo: any, filename?: string) {
151
+
152
+ if (Raiton.thread?.builder.options.serve === false)
153
+ return Logger.warn(
154
+ 'Artifact reload is only available in development mode'
155
+ )
156
+
157
+ const app = Raiton.thread?.application
158
+ if (!app) return
159
+
160
+ const rootScope = (app as any).root
161
+ if (!rootScope?.middleware) return
162
+
163
+ for (const mod of Object.values(modulo)) {
164
+ const name = (mod && typeof mod === 'object' && 'name' in mod) ? mod.name : (
165
+ typeof mod === 'function' ? mod.name ?? mod.constructor.name : undefined
166
+ );
167
+
168
+ if (typeof name === 'string') {
169
+ if (typeof mod === 'function') {
170
+ const instance = new (mod as ConstructorType)()
171
+ rootScope.middleware.replace(name, instance)
172
+ } else if (typeof mod === 'object' && mod !== null) {
173
+ rootScope.middleware.replace(name, mod)
174
+ }
175
+ }
176
+ }
177
+ }
178
+
179
+ static reloadHook(modulo: any, filename?: string) {
180
+
181
+ if (Raiton.thread?.builder.options.serve === false)
182
+ return Logger.warn(
183
+ 'Artifact reload is only available in development mode'
184
+ )
185
+
186
+ const app = Raiton.thread?.application
187
+ if (!app) return
188
+
189
+ const rootScope = (app as any).root
190
+ if (!rootScope?.hooks) return
191
+
192
+ for (const mod of Object.values(modulo)) {
193
+ const name = (mod && typeof mod === 'object' && 'name' in mod) ? mod.name : (
194
+ typeof mod === 'function' ? mod.name ?? mod.constructor.name : undefined
195
+ );
196
+
197
+ if (typeof name === 'string') {
198
+ if (typeof mod === 'function') {
199
+ const instance = new (mod as ConstructorType)()
200
+ rootScope.hooks.replace(name, instance)
201
+ } else if (typeof mod === 'object' && mod !== null) {
202
+ rootScope.hooks.replace(name, mod)
203
+ }
204
+ }
205
+ }
206
+ }
76
207
  }
@@ -1,4 +1,5 @@
1
1
  import {WatchEventType} from "node:fs";
2
+ import type {HmrChannel} from "../framework/artifacts";
2
3
 
3
4
  export interface StartCommandOptionsInterface {
4
5
  develop?: boolean;
@@ -46,4 +47,6 @@ export interface BuilderHMRDeclarationInterface {
46
47
  timestamp?: number;
47
48
  version?: number;
48
49
  type?: WatchEventType
50
+ channel?: HmrChannel
51
+ artifactType?: string
49
52
  }
@@ -4,4 +4,9 @@ export interface RaitonSignalMapInterface {
4
4
  ready?: undefined;
5
5
  errors: any;
6
6
  'hmr:artifact': BuilderHMRDeclarationInterface;
7
+ 'hmr:di': BuilderHMRDeclarationInterface;
8
+ 'hmr:controller': BuilderHMRDeclarationInterface;
9
+ 'hmr:socket': BuilderHMRDeclarationInterface;
10
+ 'hmr:middleware': BuilderHMRDeclarationInterface;
11
+ 'hmr:hook': BuilderHMRDeclarationInterface;
7
12
  }