@noxfly/noxus 2.5.0 → 3.0.0-dev.1

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.
Files changed (58) hide show
  1. package/README.md +405 -340
  2. package/dist/app-injector-Bz3Upc0y.d.mts +125 -0
  3. package/dist/app-injector-Bz3Upc0y.d.ts +125 -0
  4. package/dist/child.d.mts +157 -23
  5. package/dist/child.d.ts +157 -23
  6. package/dist/child.js +1111 -1341
  7. package/dist/child.mjs +1086 -1294
  8. package/dist/main.d.mts +720 -284
  9. package/dist/main.d.ts +720 -284
  10. package/dist/main.js +1471 -1650
  11. package/dist/main.mjs +1409 -1559
  12. package/dist/preload.d.mts +28 -0
  13. package/dist/preload.d.ts +28 -0
  14. package/dist/preload.js +95 -0
  15. package/dist/preload.mjs +70 -0
  16. package/dist/renderer.d.mts +159 -22
  17. package/dist/renderer.d.ts +159 -22
  18. package/dist/renderer.js +104 -177
  19. package/dist/renderer.mjs +100 -172
  20. package/dist/request-BlTtiHbi.d.ts +112 -0
  21. package/dist/request-qJ9EiDZc.d.mts +112 -0
  22. package/package.json +24 -19
  23. package/src/DI/app-injector.ts +95 -106
  24. package/src/DI/injector-explorer.ts +93 -119
  25. package/src/DI/token.ts +53 -0
  26. package/src/decorators/controller.decorator.ts +38 -27
  27. package/src/decorators/guards.decorator.ts +5 -64
  28. package/src/decorators/injectable.decorator.ts +68 -15
  29. package/src/decorators/method.decorator.ts +40 -81
  30. package/src/decorators/middleware.decorator.ts +5 -72
  31. package/src/index.ts +4 -5
  32. package/src/internal/app.ts +217 -0
  33. package/src/internal/bootstrap.ts +108 -0
  34. package/src/{preload-bridge.ts → internal/preload-bridge.ts} +1 -1
  35. package/src/{renderer-client.ts → internal/renderer-client.ts} +2 -2
  36. package/src/{renderer-events.ts → internal/renderer-events.ts} +1 -1
  37. package/src/{request.ts → internal/request.ts} +3 -3
  38. package/src/internal/router.ts +353 -0
  39. package/src/internal/routes.ts +78 -0
  40. package/src/{socket.ts → internal/socket.ts} +4 -4
  41. package/src/main.ts +10 -14
  42. package/src/non-electron-process.ts +1 -2
  43. package/src/preload.ts +10 -0
  44. package/src/renderer.ts +13 -0
  45. package/src/window/window-manager.ts +255 -0
  46. package/tsconfig.json +5 -10
  47. package/tsup.config.ts +29 -13
  48. package/dist/app-injector-B3MvgV3k.d.mts +0 -95
  49. package/dist/app-injector-B3MvgV3k.d.ts +0 -95
  50. package/dist/request-CdpZ9qZL.d.ts +0 -167
  51. package/dist/request-Dx_5Prte.d.mts +0 -167
  52. package/src/app.ts +0 -244
  53. package/src/bootstrap.ts +0 -84
  54. package/src/decorators/inject.decorator.ts +0 -24
  55. package/src/decorators/injectable.metadata.ts +0 -15
  56. package/src/decorators/module.decorator.ts +0 -75
  57. package/src/router.ts +0 -594
  58. /package/src/{exceptions.ts → internal/exceptions.ts} +0 -0
@@ -0,0 +1,255 @@
1
+ /**
2
+ * @copyright 2025 NoxFly
3
+ * @license MIT
4
+ * @author NoxFly
5
+ */
6
+
7
+ import { BrowserWindow, screen } from 'electron/main';
8
+ import { Injectable } from '../decorators/injectable.decorator';
9
+ import { Logger } from '../utils/logger';
10
+
11
+ export interface WindowConfig extends Electron.BrowserWindowConstructorOptions {
12
+ /**
13
+ * If true, the window expands to fill the work area after creation
14
+ * using an animated setBounds. The content is loaded only after
15
+ * the animation completes, preventing the viewbox freeze issue.
16
+ * @default false
17
+ */
18
+ expandToWorkArea?: boolean;
19
+
20
+ /**
21
+ * Duration in ms to wait for the setBounds animation to complete
22
+ * before loading content. Only used when expandToWorkArea is true.
23
+ * @default 600
24
+ */
25
+ expandAnimationDuration?: number;
26
+ }
27
+
28
+ export interface WindowRecord {
29
+ window: BrowserWindow;
30
+ id: number;
31
+ }
32
+
33
+ /**
34
+ * WindowManager is a singleton service that centralizes BrowserWindow lifecycle.
35
+ *
36
+ * Features:
37
+ * - Creates and tracks all application windows.
38
+ * - Handles the animated expand-to-work-area pattern correctly,
39
+ * loading content only after the animation ends to avoid the viewbox freeze.
40
+ * - Provides convenience methods to get windows by id, get the main window, etc.
41
+ * - Automatically removes windows from the registry on close.
42
+ *
43
+ * @example
44
+ * // In your IApp.onReady():
45
+ * const wm = inject(WindowManager);
46
+ *
47
+ * const win = await wm.create({
48
+ * width: 600, height: 600, center: true,
49
+ * expandToWorkArea: true,
50
+ * webPreferences: { preload: path.join(__dirname, 'preload.js') },
51
+ * });
52
+ *
53
+ * win.loadFile('index.html');
54
+ */
55
+ @Injectable({ lifetime: 'singleton' })
56
+ export class WindowManager {
57
+ private readonly _windows = new Map<number, BrowserWindow>();
58
+ private _mainWindowId: number | undefined;
59
+
60
+ // -------------------------------------------------------------------------
61
+ // Creation
62
+ // -------------------------------------------------------------------------
63
+
64
+ /**
65
+ * Creates a BrowserWindow, optionally performs an animated expand to the
66
+ * work area, and registers it in the manager.
67
+ *
68
+ * If expandToWorkArea is true:
69
+ * 1. The window is created at the given initial size (defaults to 600×600, centered).
70
+ * 2. An animated setBounds expands it to the full work area.
71
+ * 3. The returned promise resolves only after the animation, so callers
72
+ * can safely call win.loadFile() without the viewbox freeze.
73
+ *
74
+ * @param config Window configuration.
75
+ * @param isMain Mark this window as the main window (accessible via getMain()).
76
+ */
77
+ public async create(config: WindowConfig, isMain = false): Promise<BrowserWindow> {
78
+ const {
79
+ expandToWorkArea = false,
80
+ expandAnimationDuration = 600,
81
+ ...bwOptions
82
+ } = config;
83
+
84
+ // show: false by default during creation — we control visibility
85
+ const win = new BrowserWindow({ show: false, ...bwOptions });
86
+
87
+ this._register(win, isMain);
88
+
89
+ if (expandToWorkArea) {
90
+ await this._expandToWorkArea(win, expandAnimationDuration);
91
+ }
92
+
93
+ win.once('ready-to-show', () => win.show());
94
+
95
+ Logger.log(`[WindowManager] Created window #${win.id}${isMain ? ' (main)' : ''}`);
96
+
97
+ return win;
98
+ }
99
+
100
+ /**
101
+ * Creates the initial "splash" window that is shown immediately after
102
+ * app.whenReady(). It is displayed instantly (show: true, no preload
103
+ * loading) and then expanded to the work area with animation.
104
+ *
105
+ * After the animation completes you can call win.loadFile() without
106
+ * experiencing the viewbox freeze.
107
+ *
108
+ * This is the recommended way to get pixels on screen as fast as possible.
109
+ *
110
+ * @example
111
+ * const win = await wm.createSplash({
112
+ * webPreferences: { preload: path.join(__dirname, 'preload.js') }
113
+ * });
114
+ * win.loadFile('index.html');
115
+ */
116
+ public async createSplash(
117
+ options: Electron.BrowserWindowConstructorOptions & { animationDuration?: number } = {},
118
+ ): Promise<BrowserWindow> {
119
+ const { animationDuration = 600, ...bwOptions } = options;
120
+
121
+ const win = new BrowserWindow({
122
+ width: 600,
123
+ height: 600,
124
+ center: true,
125
+ frame: false,
126
+ show: true,
127
+ ...bwOptions,
128
+ });
129
+
130
+ this._register(win, true);
131
+
132
+ Logger.log(`[WindowManager] Splash window #${win.id} created`);
133
+
134
+ await this._expandToWorkArea(win, animationDuration);
135
+
136
+ return win;
137
+ }
138
+
139
+ // -------------------------------------------------------------------------
140
+ // Accessors
141
+ // -------------------------------------------------------------------------
142
+
143
+ /** Returns all currently open windows. */
144
+ public getAll(): BrowserWindow[] {
145
+ return [...this._windows.values()];
146
+ }
147
+
148
+ /** Returns the window designated as main, or undefined. */
149
+ public getMain(): BrowserWindow | undefined {
150
+ return this._mainWindowId !== undefined
151
+ ? this._windows.get(this._mainWindowId)
152
+ : undefined;
153
+ }
154
+
155
+ /** Returns a window by its Electron id, or undefined. */
156
+ public getById(id: number): BrowserWindow | undefined {
157
+ return this._windows.get(id);
158
+ }
159
+
160
+ /** Returns the number of open windows. */
161
+ public get count(): number {
162
+ return this._windows.size;
163
+ }
164
+
165
+ // -------------------------------------------------------------------------
166
+ // Actions
167
+ // -------------------------------------------------------------------------
168
+
169
+ /** Closes and destroys a window by id. */
170
+ public close(id: number): void {
171
+ const win = this._windows.get(id);
172
+ if (!win) {
173
+ Logger.warn(`[WindowManager] Window #${id} not found`);
174
+ return;
175
+ }
176
+ win.destroy();
177
+ }
178
+
179
+ /** Closes all windows. */
180
+ public closeAll(): void {
181
+ for (const win of this._windows.values()) {
182
+ win.destroy();
183
+ }
184
+ }
185
+
186
+ /**
187
+ * Sends a message to a specific window via webContents.send.
188
+ * @param id Target window id.
189
+ * @param channel IPC channel name.
190
+ * @param args Payload.
191
+ */
192
+ public send(id: number, channel: string, ...args: unknown[]): void {
193
+ const win = this._windows.get(id);
194
+ if (!win || win.isDestroyed()) {
195
+ Logger.warn(`[WindowManager] Cannot send to window #${id}: not found or destroyed`);
196
+ return;
197
+ }
198
+ win.webContents.send(channel, ...args);
199
+ }
200
+
201
+ /**
202
+ * Broadcasts a message to all open windows.
203
+ */
204
+ public broadcast(channel: string, ...args: unknown[]): void {
205
+ for (const win of this._windows.values()) {
206
+ if (!win.isDestroyed()) win.webContents.send(channel, ...args);
207
+ }
208
+ }
209
+
210
+ // -------------------------------------------------------------------------
211
+ // Private
212
+ // -------------------------------------------------------------------------
213
+
214
+ private _register(win: BrowserWindow, isMain: boolean): void {
215
+ this._windows.set(win.id, win);
216
+
217
+ if (isMain && this._mainWindowId === undefined) {
218
+ this._mainWindowId = win.id;
219
+ }
220
+
221
+ win.once('closed', () => {
222
+ this._windows.delete(win.id);
223
+ if (this._mainWindowId === win.id) this._mainWindowId = undefined;
224
+ Logger.log(`[WindowManager] Window #${win.id} closed`);
225
+ });
226
+ }
227
+
228
+ /**
229
+ * Animates the window to the full work area of the primary display.
230
+ * Resolves only after the animation is complete, so that content loaded
231
+ * afterward gets the correct surface size (no viewbox freeze).
232
+ */
233
+ private _expandToWorkArea(win: BrowserWindow, animationDuration: number): Promise<void> {
234
+ return new Promise((resolve) => {
235
+ const { x, y, width, height } = screen.getPrimaryDisplay().workArea;
236
+
237
+ win.setBounds({ x, y, width, height }, true);
238
+
239
+ // Wait for the animation to finish before resolving.
240
+ // We listen to the 'resize' event which fires once the OS
241
+ // animation completes, with a safety timeout as fallback.
242
+ let resolved = false;
243
+
244
+ const done = (): void => {
245
+ if (resolved) return;
246
+ resolved = true;
247
+ win.removeListener('resize', done);
248
+ resolve();
249
+ };
250
+
251
+ win.once('resize', done);
252
+ setTimeout(done, animationDuration + 100); // safety fallback
253
+ });
254
+ }
255
+ }
package/tsconfig.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "rootDir": "./src",
5
5
  "baseUrl": "./",
6
6
  "moduleResolution": "node",
7
- "target": "ES6",
7
+ "target": "ES2020",
8
8
  "module": "CommonJS",
9
9
  "forceConsistentCasingInFileNames": true,
10
10
  "strict": true,
@@ -13,22 +13,17 @@
13
13
  "noFallthroughCasesInSwitch": true,
14
14
  "sourceMap": true,
15
15
  "experimentalDecorators": true,
16
- "emitDecoratorMetadata": true,
16
+ "emitDecoratorMetadata": false,
17
17
  "importHelpers": false,
18
18
  "esModuleInterop": true,
19
19
  "useDefineForClassFields": false,
20
20
  "noPropertyAccessFromIndexSignature": false,
21
21
  "noUncheckedIndexedAccess": true,
22
- "skipLibCheck": true,
23
- "types": ["reflect-metadata"],
24
- },
25
- "tsc-alias": {
26
- "resolveFullPaths": true,
27
- "verbose": false,
22
+ "skipLibCheck": true
28
23
  },
29
24
  "include": [
30
25
  "src/**/*.ts",
31
26
  "src/**/*.d.ts",
32
- "node_modules/electron/electron.d.ts",
33
- ],
27
+ "node_modules/electron/electron.d.ts"
28
+ ]
34
29
  }
package/tsup.config.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { defineConfig } from "tsup";
1
+ import { defineConfig, Options } from "tsup";
2
2
 
3
3
  const copyrights = `
4
4
  /**
@@ -6,21 +6,16 @@ const copyrights = `
6
6
  * @license MIT
7
7
  * @author NoxFly
8
8
  */
9
- `.trim()
9
+ `.trim();
10
10
 
11
- export default defineConfig({
12
- entry: {
13
- renderer: "src/index.ts",
14
- main: "src/main.ts",
15
- child: "src/non-electron-process.ts",
16
- },
11
+ const options: Options = {
17
12
  keepNames: true,
18
13
  minifyIdentifiers: false,
19
14
  name: "noxus",
20
15
  format: ["cjs", "esm"],
21
16
  dts: true,
22
17
  sourcemap: true,
23
- clean: true,
18
+ clean: false, // ← false dans le base config
24
19
  outDir: "dist",
25
20
  external: ["electron"],
26
21
  target: "es2020",
@@ -28,7 +23,28 @@ export default defineConfig({
28
23
  splitting: false,
29
24
  shims: false,
30
25
  treeshake: false,
31
- banner: {
32
- js: copyrights,
33
- }
34
- });
26
+ banner: { js: copyrights },
27
+ };
28
+
29
+ export default defineConfig([
30
+ {
31
+ entry: { main: 'src/main.ts' },
32
+ external: ['electron', 'electron/main'],
33
+ clean: true, // ← true uniquement ici
34
+ ...options,
35
+ },
36
+ {
37
+ entry: { renderer: 'src/renderer.ts' },
38
+ external: ['electron', 'electron/renderer'],
39
+ ...options,
40
+ },
41
+ {
42
+ entry: { preload: 'src/preload.ts' },
43
+ external: ['electron', 'electron/renderer'],
44
+ ...options,
45
+ },
46
+ {
47
+ entry: { child: 'src/non-electron-process.ts' },
48
+ ...options,
49
+ },
50
+ ]);
@@ -1,95 +0,0 @@
1
- /**
2
- * @copyright 2025 NoxFly
3
- * @license MIT
4
- * @author NoxFly
5
- */
6
- interface Type<T> extends Function {
7
- new (...args: any[]): T;
8
- }
9
- /**
10
- * Represents a generic type that can be either a value or a promise resolving to that value.
11
- */
12
- type MaybeAsync<T> = T | Promise<T>;
13
-
14
-
15
- /**
16
- * A function that returns a type.
17
- * Used for forward references to types that are not yet defined.
18
- */
19
- interface ForwardRefFn<T = any> {
20
- (): Type<T>;
21
- }
22
- /**
23
- * A wrapper class for forward referenced types.
24
- */
25
- declare class ForwardReference<T = any> {
26
- readonly forwardRefFn: ForwardRefFn<T>;
27
- constructor(forwardRefFn: ForwardRefFn<T>);
28
- }
29
- /**
30
- * Creates a forward reference to a type.
31
- * @param fn A function that returns the type.
32
- * @returns A ForwardReference instance.
33
- */
34
- declare function forwardRef<T = any>(fn: ForwardRefFn<T>): ForwardReference<T>;
35
-
36
-
37
- /**
38
- * Represents a lifetime of a binding in the dependency injection system.
39
- * It can be one of the following:
40
- * - 'singleton': The instance is created once and shared across the application.
41
- * - 'scope': The instance is created once per scope (e.g., per request).
42
- * - 'transient': A new instance is created every time it is requested.
43
- */
44
- type Lifetime = 'singleton' | 'scope' | 'transient';
45
- /**
46
- * Represents a binding in the dependency injection system.
47
- * It contains the lifetime of the binding, the implementation type, and optionally an instance.
48
- */
49
- interface IBinding {
50
- lifetime: Lifetime;
51
- implementation: Type<unknown>;
52
- instance?: InstanceType<Type<unknown>>;
53
- }
54
- /**
55
- * AppInjector is the root dependency injection container.
56
- * It is used to register and resolve dependencies in the application.
57
- * It supports different lifetimes for dependencies:
58
- * This should not be manually instantiated, outside of the framework.
59
- * Use the `RootInjector` instance instead.
60
- */
61
- declare class AppInjector {
62
- readonly name: string | null;
63
- bindings: Map<Type<unknown>, IBinding>;
64
- singletons: Map<Type<unknown>, unknown>;
65
- scoped: Map<Type<unknown>, unknown>;
66
- constructor(name?: string | null);
67
- /**
68
- * Typically used to create a dependency injection scope
69
- * at the "scope" level (i.e., per-request lifetime).
70
- *
71
- * SHOULD NOT BE USED by anything else than the framework itself.
72
- */
73
- createScope(): AppInjector;
74
- /**
75
- * Called when resolving a dependency,
76
- * i.e., retrieving the instance of a given class.
77
- */
78
- resolve<T>(target: Type<T> | ForwardReference<T>): T;
79
- /**
80
- * Instantiates a class, resolving its dependencies.
81
- */
82
- private instantiate;
83
- }
84
- /**
85
- * Injects a type from the dependency injection system.
86
- * This function is used to retrieve an instance of a type that has been registered in the dependency injection system.
87
- * It is typically used in the constructor of a class to inject dependencies.
88
- * @param t - The type to inject.
89
- * @returns An instance of the type.
90
- * @throws If the type is not registered in the dependency injection system.
91
- */
92
- declare function inject<T>(t: Type<T> | ForwardReference<T>): T;
93
- declare const RootInjector: AppInjector;
94
-
95
- export { AppInjector as A, type ForwardRefFn as F, type IBinding as I, type Lifetime as L, type MaybeAsync as M, RootInjector as R, type Type as T, ForwardReference as a, forwardRef as f, inject as i };
@@ -1,95 +0,0 @@
1
- /**
2
- * @copyright 2025 NoxFly
3
- * @license MIT
4
- * @author NoxFly
5
- */
6
- interface Type<T> extends Function {
7
- new (...args: any[]): T;
8
- }
9
- /**
10
- * Represents a generic type that can be either a value or a promise resolving to that value.
11
- */
12
- type MaybeAsync<T> = T | Promise<T>;
13
-
14
-
15
- /**
16
- * A function that returns a type.
17
- * Used for forward references to types that are not yet defined.
18
- */
19
- interface ForwardRefFn<T = any> {
20
- (): Type<T>;
21
- }
22
- /**
23
- * A wrapper class for forward referenced types.
24
- */
25
- declare class ForwardReference<T = any> {
26
- readonly forwardRefFn: ForwardRefFn<T>;
27
- constructor(forwardRefFn: ForwardRefFn<T>);
28
- }
29
- /**
30
- * Creates a forward reference to a type.
31
- * @param fn A function that returns the type.
32
- * @returns A ForwardReference instance.
33
- */
34
- declare function forwardRef<T = any>(fn: ForwardRefFn<T>): ForwardReference<T>;
35
-
36
-
37
- /**
38
- * Represents a lifetime of a binding in the dependency injection system.
39
- * It can be one of the following:
40
- * - 'singleton': The instance is created once and shared across the application.
41
- * - 'scope': The instance is created once per scope (e.g., per request).
42
- * - 'transient': A new instance is created every time it is requested.
43
- */
44
- type Lifetime = 'singleton' | 'scope' | 'transient';
45
- /**
46
- * Represents a binding in the dependency injection system.
47
- * It contains the lifetime of the binding, the implementation type, and optionally an instance.
48
- */
49
- interface IBinding {
50
- lifetime: Lifetime;
51
- implementation: Type<unknown>;
52
- instance?: InstanceType<Type<unknown>>;
53
- }
54
- /**
55
- * AppInjector is the root dependency injection container.
56
- * It is used to register and resolve dependencies in the application.
57
- * It supports different lifetimes for dependencies:
58
- * This should not be manually instantiated, outside of the framework.
59
- * Use the `RootInjector` instance instead.
60
- */
61
- declare class AppInjector {
62
- readonly name: string | null;
63
- bindings: Map<Type<unknown>, IBinding>;
64
- singletons: Map<Type<unknown>, unknown>;
65
- scoped: Map<Type<unknown>, unknown>;
66
- constructor(name?: string | null);
67
- /**
68
- * Typically used to create a dependency injection scope
69
- * at the "scope" level (i.e., per-request lifetime).
70
- *
71
- * SHOULD NOT BE USED by anything else than the framework itself.
72
- */
73
- createScope(): AppInjector;
74
- /**
75
- * Called when resolving a dependency,
76
- * i.e., retrieving the instance of a given class.
77
- */
78
- resolve<T>(target: Type<T> | ForwardReference<T>): T;
79
- /**
80
- * Instantiates a class, resolving its dependencies.
81
- */
82
- private instantiate;
83
- }
84
- /**
85
- * Injects a type from the dependency injection system.
86
- * This function is used to retrieve an instance of a type that has been registered in the dependency injection system.
87
- * It is typically used in the constructor of a class to inject dependencies.
88
- * @param t - The type to inject.
89
- * @returns An instance of the type.
90
- * @throws If the type is not registered in the dependency injection system.
91
- */
92
- declare function inject<T>(t: Type<T> | ForwardReference<T>): T;
93
- declare const RootInjector: AppInjector;
94
-
95
- export { AppInjector as A, type ForwardRefFn as F, type IBinding as I, type Lifetime as L, type MaybeAsync as M, RootInjector as R, type Type as T, ForwardReference as a, forwardRef as f, inject as i };