@noego/app 0.0.18 → 0.0.20
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 +2 -1
- package/src/client.js +16 -3
- package/src/container.js +36 -0
- package/src/index.js +1 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@noego/app",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.20",
|
|
4
4
|
"description": "Production build tool for Dinner/Forge apps.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -43,6 +43,7 @@
|
|
|
43
43
|
"peerDependencies": {
|
|
44
44
|
"@noego/dinner": "*",
|
|
45
45
|
"@noego/forge": "*",
|
|
46
|
+
"@noego/ioc": "*",
|
|
46
47
|
"express": "*"
|
|
47
48
|
},
|
|
48
49
|
"devDependencies": {
|
package/src/client.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { createRequire } from 'node:module';
|
|
2
2
|
import path from 'node:path';
|
|
3
|
+
import { getContainer } from './container.js';
|
|
3
4
|
|
|
4
5
|
// Runtime context - set by the runtime before calling server.main
|
|
5
6
|
let runtimeContext = null;
|
|
@@ -44,9 +45,21 @@ export async function boot(assets, options = {}) {
|
|
|
44
45
|
server: app,
|
|
45
46
|
ajv_formats: true,
|
|
46
47
|
assets: assets,
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
48
|
+
// Default context_builder creates a scoped container per request
|
|
49
|
+
context_builder: options.context_builder || ((req, res) => {
|
|
50
|
+
const globalContainer = getContainer();
|
|
51
|
+
const scopedContainer = globalContainer.extend();
|
|
52
|
+
// Attach scoped container to request for middleware/decorators to access
|
|
53
|
+
req.container = scopedContainer;
|
|
54
|
+
return { container: scopedContainer };
|
|
55
|
+
}),
|
|
56
|
+
// Default controller_builder uses the scoped container from context
|
|
57
|
+
controller_builder: options.controller_builder || (async (Controller, context) => {
|
|
58
|
+
if (context?.container) {
|
|
59
|
+
return context.container.get(Controller);
|
|
60
|
+
}
|
|
61
|
+
// Fallback: use global container if no scoped container in context
|
|
62
|
+
return getContainer().get(Controller);
|
|
50
63
|
}),
|
|
51
64
|
controller_args_provider: options.controller_args_provider || (async (req, res) => ({ req, res })),
|
|
52
65
|
});
|
package/src/container.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Global IoC container management for @noego/app
|
|
3
|
+
*
|
|
4
|
+
* Uses Symbol.for() to ensure a true singleton across module boundaries,
|
|
5
|
+
* even if multiple versions of the package are loaded.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { createRequire } from 'node:module';
|
|
9
|
+
|
|
10
|
+
const CONTAINER_KEY = Symbol.for('@noego/app.container');
|
|
11
|
+
|
|
12
|
+
// Create require function for ES module context
|
|
13
|
+
const require = createRequire(import.meta.url);
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Get the global IoC container instance.
|
|
17
|
+
* Creates it on first access using @noego/ioc.
|
|
18
|
+
*
|
|
19
|
+
* @returns {import('@noego/ioc').Container} The global container instance
|
|
20
|
+
*/
|
|
21
|
+
export function getContainer() {
|
|
22
|
+
if (!globalThis[CONTAINER_KEY]) {
|
|
23
|
+
// Dynamically require @noego/ioc to use the version from the user's project
|
|
24
|
+
const { createContainer } = require('@noego/ioc');
|
|
25
|
+
globalThis[CONTAINER_KEY] = createContainer();
|
|
26
|
+
}
|
|
27
|
+
return globalThis[CONTAINER_KEY];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Reset the global container (primarily for testing).
|
|
32
|
+
* This clears the singleton and allows a fresh container to be created.
|
|
33
|
+
*/
|
|
34
|
+
export function resetContainer() {
|
|
35
|
+
delete globalThis[CONTAINER_KEY];
|
|
36
|
+
}
|
package/src/index.js
CHANGED