millas 0.2.12-beta → 0.2.12-beta-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.
- package/package.json +3 -16
- package/src/auth/Auth.js +13 -8
- package/src/auth/AuthController.js +3 -1
- package/src/auth/AuthUser.js +98 -0
- package/src/cli.js +1 -1
- package/src/commands/serve.js +81 -110
- package/src/container/AppInitializer.js +158 -0
- package/src/container/Application.js +278 -253
- package/src/container/HttpServer.js +156 -0
- package/src/container/MillasApp.js +23 -280
- package/src/container/MillasConfig.js +163 -0
- package/src/core/auth.js +9 -0
- package/src/core/db.js +8 -0
- package/src/core/foundation.js +67 -0
- package/src/core/http.js +11 -0
- package/src/core/mail.js +6 -0
- package/src/core/queue.js +7 -0
- package/src/core/validation.js +29 -0
- package/src/facades/Admin.js +1 -1
- package/src/facades/Auth.js +22 -39
- package/src/facades/Cache.js +21 -10
- package/src/facades/Database.js +1 -1
- package/src/facades/Events.js +18 -17
- package/src/facades/Facade.js +197 -0
- package/src/facades/Http.js +42 -45
- package/src/facades/Log.js +25 -49
- package/src/facades/Mail.js +27 -32
- package/src/facades/Queue.js +22 -15
- package/src/facades/Storage.js +18 -10
- package/src/facades/Url.js +53 -0
- package/src/http/HttpClient.js +673 -0
- package/src/http/ResponseDispatcher.js +18 -111
- package/src/http/UrlGenerator.js +375 -0
- package/src/http/WelcomePage.js +273 -0
- package/src/http/adapters/ExpressAdapter.js +315 -0
- package/src/http/adapters/HttpAdapter.js +168 -0
- package/src/http/adapters/index.js +9 -0
- package/src/index.js +5 -144
- package/src/logger/formatters/PrettyFormatter.js +15 -5
- package/src/logger/internal.js +2 -2
- package/src/logger/patchConsole.js +91 -81
- package/src/middleware/MiddlewareRegistry.js +62 -82
- package/src/orm/migration/ModelInspector.js +339 -340
- package/src/providers/AuthServiceProvider.js +9 -5
- package/src/providers/CacheStorageServiceProvider.js +3 -1
- package/src/providers/EventServiceProvider.js +2 -1
- package/src/providers/LogServiceProvider.js +3 -2
- package/src/providers/MailServiceProvider.js +3 -2
- package/src/providers/QueueServiceProvider.js +3 -2
- package/src/router/Router.js +119 -152
- package/src/scaffold/templates.js +8 -7
- package/src/facades/Validation.js +0 -69
|
@@ -1,54 +1,68 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const MillasRequest = require('../http/MillasRequest');
|
|
4
|
-
const MillasResponse = require('../http/MillasResponse');
|
|
5
|
-
const ResponseDispatcher = require('../http/ResponseDispatcher');
|
|
6
|
-
const RequestContext = require('../http/RequestContext');
|
|
7
|
-
|
|
8
3
|
/**
|
|
9
4
|
* MiddlewareRegistry
|
|
10
5
|
*
|
|
11
|
-
* Maps string aliases → middleware
|
|
12
|
-
*
|
|
6
|
+
* Maps string aliases → Millas middleware classes or instances.
|
|
7
|
+
* Resolution produces adapter-native handler functions via the adapter,
|
|
8
|
+
* so this class has zero knowledge of Express (or any HTTP engine).
|
|
9
|
+
*
|
|
10
|
+
* The adapter is injected at resolution time (not construction time)
|
|
11
|
+
* so the registry can be built before the adapter exists.
|
|
13
12
|
*/
|
|
14
13
|
class MiddlewareRegistry {
|
|
15
|
-
constructor(
|
|
16
|
-
this._map
|
|
17
|
-
this._container = container;
|
|
14
|
+
constructor() {
|
|
15
|
+
this._map = {};
|
|
18
16
|
}
|
|
19
17
|
|
|
18
|
+
/**
|
|
19
|
+
* Register a middleware alias.
|
|
20
|
+
*
|
|
21
|
+
* registry.register('auth', AuthMiddleware)
|
|
22
|
+
* registry.register('throttle', new ThrottleMiddleware({ max: 60 }))
|
|
23
|
+
*/
|
|
20
24
|
register(alias, handler) {
|
|
21
25
|
this._map[alias] = handler;
|
|
22
26
|
return this;
|
|
23
27
|
}
|
|
24
28
|
|
|
25
29
|
/**
|
|
26
|
-
* Resolve a middleware alias or
|
|
27
|
-
*
|
|
28
|
-
* Millas middleware (class with handle(req, next)):
|
|
29
|
-
* - Receives MillasRequest
|
|
30
|
-
* - Returns MillasResponse or calls next()
|
|
31
|
-
* - Kernel dispatches the MillasResponse if returned
|
|
30
|
+
* Resolve a middleware alias or class/instance into an adapter-native handler.
|
|
32
31
|
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
32
|
+
* @param {string|Function|object} aliasOrFn
|
|
33
|
+
* @param {import('../http/adapters/HttpAdapter')} adapter
|
|
34
|
+
* @param {object|null} container
|
|
35
|
+
* @returns {Function} adapter-native handler
|
|
35
36
|
*/
|
|
36
|
-
resolve(aliasOrFn) {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
}
|
|
37
|
+
resolve(aliasOrFn, adapter, container = null) {
|
|
38
|
+
const Handler = typeof aliasOrFn === 'string'
|
|
39
|
+
? this._map[aliasOrFn]
|
|
40
|
+
: aliasOrFn;
|
|
41
41
|
|
|
42
|
-
const Handler = this._map[aliasOrFn];
|
|
43
42
|
if (!Handler) {
|
|
44
43
|
throw new Error(`Middleware "${aliasOrFn}" is not registered.`);
|
|
45
44
|
}
|
|
46
45
|
|
|
47
|
-
return this.
|
|
46
|
+
return this._wrap(Handler, adapter, container);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Resolve all aliases in a list.
|
|
51
|
+
*/
|
|
52
|
+
resolveAll(list = [], adapter, container = null) {
|
|
53
|
+
return list.map(m => this.resolve(m, adapter, container));
|
|
48
54
|
}
|
|
49
55
|
|
|
50
|
-
|
|
51
|
-
|
|
56
|
+
/**
|
|
57
|
+
* Return a no-op passthrough handler for the given adapter.
|
|
58
|
+
* Used when a middleware alias is missing but should not crash the app.
|
|
59
|
+
*/
|
|
60
|
+
resolvePassthrough(adapter) {
|
|
61
|
+
// Adapter-agnostic: return a function matching the native signature
|
|
62
|
+
// by asking the adapter to wrap a no-op middleware instance.
|
|
63
|
+
return adapter.wrapMiddleware({
|
|
64
|
+
handle: (_ctx, next) => next(),
|
|
65
|
+
}, null);
|
|
52
66
|
}
|
|
53
67
|
|
|
54
68
|
has(alias) {
|
|
@@ -56,71 +70,37 @@ class MiddlewareRegistry {
|
|
|
56
70
|
}
|
|
57
71
|
|
|
58
72
|
all() {
|
|
59
|
-
return { ...this._map };
|
|
73
|
+
return { ...this._map };
|
|
74
|
+
}
|
|
60
75
|
|
|
61
|
-
//
|
|
76
|
+
// ── Internal ────────────────────────────────────────────────────────────────
|
|
62
77
|
|
|
63
|
-
|
|
64
|
-
// Pre-instantiated Millas middleware object
|
|
65
|
-
if (
|
|
66
|
-
|
|
67
|
-
|
|
78
|
+
_wrap(Handler, adapter, container) {
|
|
79
|
+
// Pre-instantiated Millas middleware object with handle()
|
|
80
|
+
if (
|
|
81
|
+
typeof Handler === 'object' &&
|
|
82
|
+
Handler !== null &&
|
|
83
|
+
typeof Handler.handle === 'function'
|
|
84
|
+
) {
|
|
85
|
+
return adapter.wrapMiddleware(Handler, container);
|
|
68
86
|
}
|
|
69
87
|
|
|
70
|
-
// Millas middleware class (
|
|
71
|
-
if (
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
88
|
+
// Millas middleware class (handle on prototype)
|
|
89
|
+
if (
|
|
90
|
+
typeof Handler === 'function' &&
|
|
91
|
+
Handler.prototype &&
|
|
92
|
+
typeof Handler.prototype.handle === 'function'
|
|
93
|
+
) {
|
|
94
|
+
return adapter.wrapMiddleware(new Handler(), container);
|
|
76
95
|
}
|
|
77
96
|
|
|
78
|
-
//
|
|
79
|
-
// Pass through unchanged — developers using old style still work
|
|
97
|
+
// Raw adapter-native function — pass through as-is (escape hatch)
|
|
80
98
|
if (typeof Handler === 'function') {
|
|
81
99
|
return Handler;
|
|
82
100
|
}
|
|
83
101
|
|
|
84
|
-
throw new Error(
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* Build an Express-compatible function from a Millas middleware instance.
|
|
89
|
-
*
|
|
90
|
-
* The middleware's handle(req, next) is called with a MillasRequest.
|
|
91
|
-
* If it returns a MillasResponse, that response is dispatched immediately.
|
|
92
|
-
* If it calls next(), Express continues down the chain.
|
|
93
|
-
*/
|
|
94
|
-
_buildMillasWrapper(instance) {
|
|
95
|
-
const container = this._container;
|
|
96
|
-
|
|
97
|
-
return (expressReq, expressRes, expressNext) => {
|
|
98
|
-
const millaReq = new MillasRequest(expressReq);
|
|
99
|
-
const ctx = new RequestContext(millaReq, container);
|
|
100
|
-
|
|
101
|
-
const next = () => {
|
|
102
|
-
expressNext();
|
|
103
|
-
return undefined;
|
|
104
|
-
};
|
|
105
|
-
|
|
106
|
-
new Promise((resolve, reject) => {
|
|
107
|
-
try {
|
|
108
|
-
resolve(instance.handle(ctx, next));
|
|
109
|
-
} catch (err) {
|
|
110
|
-
reject(err);
|
|
111
|
-
}
|
|
112
|
-
})
|
|
113
|
-
.then(value => {
|
|
114
|
-
if (value !== undefined && value !== null && !expressRes.headersSent) {
|
|
115
|
-
const response = MillasResponse.isResponse(value)
|
|
116
|
-
? value
|
|
117
|
-
: ResponseDispatcher.autoWrap(value);
|
|
118
|
-
ResponseDispatcher.dispatch(response, expressRes);
|
|
119
|
-
}
|
|
120
|
-
})
|
|
121
|
-
.catch(expressNext);
|
|
122
|
-
};
|
|
102
|
+
throw new Error('Middleware must be a function or a class with handle().');
|
|
123
103
|
}
|
|
124
104
|
}
|
|
125
105
|
|
|
126
|
-
module.exports = MiddlewareRegistry;
|
|
106
|
+
module.exports = MiddlewareRegistry;
|