@carno.js/core 0.2.3
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/LICENSE +674 -0
- package/dist/Carno.d.ts +65 -0
- package/dist/Carno.js +307 -0
- package/dist/Cheetah.d.ts +65 -0
- package/dist/Cheetah.js +307 -0
- package/dist/cache/bento-cache.driver.d.ts +13 -0
- package/dist/cache/bento-cache.driver.js +55 -0
- package/dist/cache/cache.service.d.ts +8 -0
- package/dist/cache/cache.service.js +6 -0
- package/dist/commons/decorators/Injectable.decorator.d.ts +20 -0
- package/dist/commons/decorators/Injectable.decorator.js +33 -0
- package/dist/commons/decorators/controller.decorator.d.ts +8 -0
- package/dist/commons/decorators/controller.decorator.js +22 -0
- package/dist/commons/decorators/http.decorators.d.ts +13 -0
- package/dist/commons/decorators/http.decorators.js +44 -0
- package/dist/commons/decorators/index.d.ts +5 -0
- package/dist/commons/decorators/index.js +21 -0
- package/dist/commons/decorators/inject.decorator.d.ts +1 -0
- package/dist/commons/decorators/inject.decorator.js +5 -0
- package/dist/commons/decorators/middleware.decorator.d.ts +2 -0
- package/dist/commons/decorators/middleware.decorator.js +30 -0
- package/dist/commons/decorators/service.decorator.d.ts +2 -0
- package/dist/commons/decorators/service.decorator.js +7 -0
- package/dist/commons/http-code.enum.d.ts +50 -0
- package/dist/commons/http-code.enum.js +54 -0
- package/dist/commons/index.d.ts +3 -0
- package/dist/commons/index.js +19 -0
- package/dist/commons/registries/ProviderControl.d.ts +77 -0
- package/dist/commons/registries/ProviderControl.js +112 -0
- package/dist/commons/registries/ProviderRegistry.d.ts +7 -0
- package/dist/commons/registries/ProviderRegistry.js +20 -0
- package/dist/constants.d.ts +7 -0
- package/dist/constants.js +10 -0
- package/dist/container/ContainerConfiguration.d.ts +45 -0
- package/dist/container/ContainerConfiguration.js +121 -0
- package/dist/container/DependencyResolver.d.ts +18 -0
- package/dist/container/DependencyResolver.js +81 -0
- package/dist/container/InjectorService.d.ts +34 -0
- package/dist/container/InjectorService.js +157 -0
- package/dist/container/MethodInvoker.d.ts +17 -0
- package/dist/container/MethodInvoker.js +69 -0
- package/dist/container/RouteResolver.d.ts +27 -0
- package/dist/container/RouteResolver.js +173 -0
- package/dist/container/container.d.ts +41 -0
- package/dist/container/container.js +71 -0
- package/dist/container/createContainer.d.ts +3 -0
- package/dist/container/createContainer.js +12 -0
- package/dist/container/createInjector.d.ts +2 -0
- package/dist/container/createInjector.js +7 -0
- package/dist/container/index.d.ts +6 -0
- package/dist/container/index.js +22 -0
- package/dist/container/middleware.resolver.d.ts +9 -0
- package/dist/container/middleware.resolver.js +35 -0
- package/dist/default-routes-carno.d.ts +3 -0
- package/dist/default-routes-carno.js +29 -0
- package/dist/default-routes-cheetah.d.ts +3 -0
- package/dist/default-routes-cheetah.js +29 -0
- package/dist/domain/CarnoClosure.d.ts +1 -0
- package/dist/domain/CarnoClosure.js +2 -0
- package/dist/domain/CarnoMiddleware.d.ts +5 -0
- package/dist/domain/CarnoMiddleware.js +2 -0
- package/dist/domain/CheetahClosure.d.ts +1 -0
- package/dist/domain/CheetahClosure.js +2 -0
- package/dist/domain/CheetahMiddleware.d.ts +5 -0
- package/dist/domain/CheetahMiddleware.js +2 -0
- package/dist/domain/Context.d.ts +26 -0
- package/dist/domain/Context.js +107 -0
- package/dist/domain/LocalsContainer.d.ts +4 -0
- package/dist/domain/LocalsContainer.js +10 -0
- package/dist/domain/Metadata.d.ts +449 -0
- package/dist/domain/Metadata.js +511 -0
- package/dist/domain/cors-config.d.ts +12 -0
- package/dist/domain/cors-config.js +18 -0
- package/dist/domain/http-method.d.ts +7 -0
- package/dist/domain/http-method.js +11 -0
- package/dist/domain/index.d.ts +10 -0
- package/dist/domain/index.js +26 -0
- package/dist/domain/provider-scope.d.ts +5 -0
- package/dist/domain/provider-scope.js +9 -0
- package/dist/domain/provider-type.d.ts +6 -0
- package/dist/domain/provider-type.js +10 -0
- package/dist/domain/provider.d.ts +37 -0
- package/dist/domain/provider.js +70 -0
- package/dist/events/hooks.decorator.d.ts +3 -0
- package/dist/events/hooks.decorator.js +29 -0
- package/dist/events/index.d.ts +2 -0
- package/dist/events/index.js +18 -0
- package/dist/events/on-event.d.ts +13 -0
- package/dist/events/on-event.js +11 -0
- package/dist/exceptions/HttpException.d.ts +9 -0
- package/dist/exceptions/HttpException.js +26 -0
- package/dist/exceptions/index.d.ts +1 -0
- package/dist/exceptions/index.js +17 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +32 -0
- package/dist/route/Matcher.d.ts +16 -0
- package/dist/route/Matcher.js +48 -0
- package/dist/route/RouteExecutor.d.ts +13 -0
- package/dist/route/RouteExecutor.js +70 -0
- package/dist/route/memoirist.d.ts +25 -0
- package/dist/route/memoirist.js +250 -0
- package/dist/services/logger.service.d.ts +23 -0
- package/dist/services/logger.service.js +54 -0
- package/dist/services/request-logger.service.d.ts +15 -0
- package/dist/services/request-logger.service.js +50 -0
- package/dist/testing/core-testing.d.ts +24 -0
- package/dist/testing/core-testing.js +98 -0
- package/dist/testing/index.d.ts +1 -0
- package/dist/testing/index.js +17 -0
- package/dist/utils/ancestorOf.d.ts +2 -0
- package/dist/utils/ancestorOf.js +10 -0
- package/dist/utils/ancestorsOf.d.ts +6 -0
- package/dist/utils/ancestorsOf.js +20 -0
- package/dist/utils/classOf.d.ts +13 -0
- package/dist/utils/classOf.js +21 -0
- package/dist/utils/cleanObject.d.ts +6 -0
- package/dist/utils/cleanObject.js +22 -0
- package/dist/utils/constructorOf.d.ts +11 -0
- package/dist/utils/constructorOf.js +18 -0
- package/dist/utils/createInstance.d.ts +1 -0
- package/dist/utils/createInstance.js +7 -0
- package/dist/utils/decoratorTypeOf.d.ts +11 -0
- package/dist/utils/decoratorTypeOf.js +32 -0
- package/dist/utils/deepClone.d.ts +6 -0
- package/dist/utils/deepClone.js +63 -0
- package/dist/utils/deepMerge.d.ts +9 -0
- package/dist/utils/deepMerge.js +62 -0
- package/dist/utils/descriptorOf.d.ts +8 -0
- package/dist/utils/descriptorOf.js +16 -0
- package/dist/utils/getClassOrSymbol.d.ts +1 -0
- package/dist/utils/getClassOrSymbol.js +8 -0
- package/dist/utils/getConstructorArgNames.d.ts +1 -0
- package/dist/utils/getConstructorArgNames.js +12 -0
- package/dist/utils/getMethodArgTypes.d.ts +1 -0
- package/dist/utils/getMethodArgTypes.js +9 -0
- package/dist/utils/getValue.d.ts +32 -0
- package/dist/utils/getValue.js +47 -0
- package/dist/utils/hasJsonMethod.d.ts +1 -0
- package/dist/utils/hasJsonMethod.js +6 -0
- package/dist/utils/index.d.ts +15 -0
- package/dist/utils/index.js +31 -0
- package/dist/utils/isArray.d.ts +13 -0
- package/dist/utils/isArray.js +21 -0
- package/dist/utils/isArrowFn.d.ts +1 -0
- package/dist/utils/isArrowFn.js +7 -0
- package/dist/utils/isBoolean.d.ts +7 -0
- package/dist/utils/isBoolean.js +15 -0
- package/dist/utils/isBuffer.d.ts +7 -0
- package/dist/utils/isBuffer.js +19 -0
- package/dist/utils/isClass.d.ts +1 -0
- package/dist/utils/isClass.js +26 -0
- package/dist/utils/isClassValidator.d.ts +6 -0
- package/dist/utils/isClassValidator.js +13 -0
- package/dist/utils/isCollection.d.ts +6 -0
- package/dist/utils/isCollection.js +20 -0
- package/dist/utils/isDate.d.ts +6 -0
- package/dist/utils/isDate.js +11 -0
- package/dist/utils/isEmpty.d.ts +6 -0
- package/dist/utils/isEmpty.js +12 -0
- package/dist/utils/isFunction.d.ts +1 -0
- package/dist/utils/isFunction.js +6 -0
- package/dist/utils/isInheritedFrom.d.ts +1 -0
- package/dist/utils/isInheritedFrom.js +24 -0
- package/dist/utils/isMomentObject.d.ts +1 -0
- package/dist/utils/isMomentObject.js +6 -0
- package/dist/utils/isMongooseObject.d.ts +2 -0
- package/dist/utils/isMongooseObject.js +11 -0
- package/dist/utils/isNil.d.ts +1 -0
- package/dist/utils/isNil.js +6 -0
- package/dist/utils/isNumber.d.ts +7 -0
- package/dist/utils/isNumber.js +15 -0
- package/dist/utils/isObject.d.ts +1 -0
- package/dist/utils/isObject.js +6 -0
- package/dist/utils/isObservable.d.ts +1 -0
- package/dist/utils/isObservable.js +6 -0
- package/dist/utils/isPlainObject.d.ts +7 -0
- package/dist/utils/isPlainObject.js +16 -0
- package/dist/utils/isPrimitive.d.ts +14 -0
- package/dist/utils/isPrimitive.js +28 -0
- package/dist/utils/isPrimitiveType.d.ts +1 -0
- package/dist/utils/isPrimitiveType.js +11 -0
- package/dist/utils/isPromise.d.ts +7 -0
- package/dist/utils/isPromise.js +14 -0
- package/dist/utils/isProtectedKey.d.ts +5 -0
- package/dist/utils/isProtectedKey.js +10 -0
- package/dist/utils/isRegExp.d.ts +1 -0
- package/dist/utils/isRegExp.js +6 -0
- package/dist/utils/isRequestScope.d.ts +11 -0
- package/dist/utils/isRequestScope.js +23 -0
- package/dist/utils/isSerializable.d.ts +1 -0
- package/dist/utils/isSerializable.js +11 -0
- package/dist/utils/isStream.d.ts +1 -0
- package/dist/utils/isStream.js +6 -0
- package/dist/utils/isString.d.ts +6 -0
- package/dist/utils/isString.js +14 -0
- package/dist/utils/isSymbol.d.ts +6 -0
- package/dist/utils/isSymbol.js +14 -0
- package/dist/utils/methodsOf.d.ts +9 -0
- package/dist/utils/methodsOf.js +24 -0
- package/dist/utils/nameOf.d.ts +14 -0
- package/dist/utils/nameOf.js +31 -0
- package/dist/utils/objectKeys.d.ts +1 -0
- package/dist/utils/objectKeys.js +7 -0
- package/dist/utils/primitiveOf.d.ts +1 -0
- package/dist/utils/primitiveOf.js +18 -0
- package/dist/utils/prototypeOf.d.ts +6 -0
- package/dist/utils/prototypeOf.js +12 -0
- package/dist/utils/setValue.d.ts +1 -0
- package/dist/utils/setValue.js +32 -0
- package/dist/utils/toMap.d.ts +3 -0
- package/dist/utils/toMap.js +34 -0
- package/dist/utils/toStringConstructor.d.ts +1 -0
- package/dist/utils/toStringConstructor.js +10 -0
- package/package.json +55 -0
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Memoirist = void 0;
|
|
4
|
+
const createNode = (part, inert) => ({
|
|
5
|
+
part,
|
|
6
|
+
store: null,
|
|
7
|
+
inert: inert !== undefined
|
|
8
|
+
? new Map(inert.map((child) => [child.part.charCodeAt(0), child]))
|
|
9
|
+
: null,
|
|
10
|
+
params: null,
|
|
11
|
+
wildcardStore: null
|
|
12
|
+
});
|
|
13
|
+
const cloneNode = (node, part) => ({
|
|
14
|
+
...node,
|
|
15
|
+
part
|
|
16
|
+
});
|
|
17
|
+
const createParamNode = () => ({
|
|
18
|
+
paramName: null,
|
|
19
|
+
names: new Map(),
|
|
20
|
+
store: null,
|
|
21
|
+
inert: null
|
|
22
|
+
});
|
|
23
|
+
const ensureSameNameForStore = (param, store, name) => {
|
|
24
|
+
const existing = param.names.get(store);
|
|
25
|
+
if (!existing || existing === name)
|
|
26
|
+
return;
|
|
27
|
+
throw new Error(`Route already registered with parameter "${existing}" for this handler`);
|
|
28
|
+
};
|
|
29
|
+
const ensureDefaultName = (param, name) => {
|
|
30
|
+
if (param.paramName)
|
|
31
|
+
return;
|
|
32
|
+
param.paramName = name;
|
|
33
|
+
};
|
|
34
|
+
const persistStoreName = (param, store, name) => {
|
|
35
|
+
if (param.names.has(store))
|
|
36
|
+
return;
|
|
37
|
+
param.names.set(store, name);
|
|
38
|
+
};
|
|
39
|
+
const registerParamName = (param, store, name) => {
|
|
40
|
+
ensureSameNameForStore(param, store, name);
|
|
41
|
+
ensureDefaultName(param, name);
|
|
42
|
+
persistStoreName(param, store, name);
|
|
43
|
+
};
|
|
44
|
+
const resolveParamName = (param, store) => {
|
|
45
|
+
if (store && param.names.has(store)) {
|
|
46
|
+
return param.names.get(store);
|
|
47
|
+
}
|
|
48
|
+
if (param.paramName) {
|
|
49
|
+
return param.paramName;
|
|
50
|
+
}
|
|
51
|
+
throw new Error('Unable to resolve parameter name for route');
|
|
52
|
+
};
|
|
53
|
+
class Memoirist {
|
|
54
|
+
constructor() {
|
|
55
|
+
this.root = {};
|
|
56
|
+
this.history = [];
|
|
57
|
+
}
|
|
58
|
+
add(method, path, store) {
|
|
59
|
+
if (typeof path !== 'string')
|
|
60
|
+
throw new TypeError('Route path must be a string');
|
|
61
|
+
if (path === '')
|
|
62
|
+
path = '/';
|
|
63
|
+
else if (path[0] !== '/')
|
|
64
|
+
path = `/${path}`;
|
|
65
|
+
this.history.push([method, path, store]);
|
|
66
|
+
const isWildcard = path[path.length - 1] === '*';
|
|
67
|
+
if (isWildcard) {
|
|
68
|
+
// Slice off trailing '*'
|
|
69
|
+
path = path.slice(0, -1);
|
|
70
|
+
}
|
|
71
|
+
const inertParts = path.split(Memoirist.regex.static);
|
|
72
|
+
const paramParts = path.match(Memoirist.regex.params) || [];
|
|
73
|
+
if (inertParts[inertParts.length - 1] === '')
|
|
74
|
+
inertParts.pop();
|
|
75
|
+
let node;
|
|
76
|
+
if (!this.root[method])
|
|
77
|
+
node = this.root[method] = createNode('/');
|
|
78
|
+
else
|
|
79
|
+
node = this.root[method];
|
|
80
|
+
let paramPartsIndex = 0;
|
|
81
|
+
for (let i = 0; i < inertParts.length; ++i) {
|
|
82
|
+
let part = inertParts[i];
|
|
83
|
+
if (i > 0) {
|
|
84
|
+
// Set param on the node
|
|
85
|
+
const param = paramParts[paramPartsIndex++].slice(1);
|
|
86
|
+
if (node.params === null) {
|
|
87
|
+
node.params = createParamNode();
|
|
88
|
+
}
|
|
89
|
+
registerParamName(node.params, store, param);
|
|
90
|
+
const params = node.params;
|
|
91
|
+
if (params.inert === null) {
|
|
92
|
+
node = params.inert = createNode(part);
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
node = params.inert;
|
|
96
|
+
}
|
|
97
|
+
for (let j = 0;;) {
|
|
98
|
+
if (j === part.length) {
|
|
99
|
+
if (j < node.part.length) {
|
|
100
|
+
// Move the current node down
|
|
101
|
+
const childNode = cloneNode(node, node.part.slice(j));
|
|
102
|
+
Object.assign(node, createNode(part, [childNode]));
|
|
103
|
+
}
|
|
104
|
+
break;
|
|
105
|
+
}
|
|
106
|
+
if (j === node.part.length) {
|
|
107
|
+
// Add static child
|
|
108
|
+
if (node.inert === null)
|
|
109
|
+
node.inert = new Map();
|
|
110
|
+
else if (node.inert.has(part.charCodeAt(j))) {
|
|
111
|
+
// Re-run loop with existing static node
|
|
112
|
+
node = node.inert.get(part.charCodeAt(j));
|
|
113
|
+
part = part.slice(j);
|
|
114
|
+
j = 0;
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
// Create new node
|
|
118
|
+
const childNode = createNode(part.slice(j));
|
|
119
|
+
node.inert.set(part.charCodeAt(j), childNode);
|
|
120
|
+
node = childNode;
|
|
121
|
+
break;
|
|
122
|
+
}
|
|
123
|
+
if (part[j] !== node.part[j]) {
|
|
124
|
+
// Split the node
|
|
125
|
+
const existingChild = cloneNode(node, node.part.slice(j));
|
|
126
|
+
const newChild = createNode(part.slice(j));
|
|
127
|
+
Object.assign(node, createNode(node.part.slice(0, j), [
|
|
128
|
+
existingChild,
|
|
129
|
+
newChild
|
|
130
|
+
]));
|
|
131
|
+
node = newChild;
|
|
132
|
+
break;
|
|
133
|
+
}
|
|
134
|
+
++j;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
if (paramPartsIndex < paramParts.length) {
|
|
138
|
+
// The final part is a parameter
|
|
139
|
+
const param = paramParts[paramPartsIndex];
|
|
140
|
+
const paramName = param.slice(1);
|
|
141
|
+
if (node.params === null) {
|
|
142
|
+
node.params = createParamNode();
|
|
143
|
+
}
|
|
144
|
+
registerParamName(node.params, store, paramName);
|
|
145
|
+
if (node.params.store === null) {
|
|
146
|
+
node.params.store = store;
|
|
147
|
+
}
|
|
148
|
+
else if (node.params.store !== store) {
|
|
149
|
+
throw new Error(`Route "${path}" already registered`);
|
|
150
|
+
}
|
|
151
|
+
return node.params.store;
|
|
152
|
+
}
|
|
153
|
+
if (isWildcard) {
|
|
154
|
+
// The final part is a wildcard
|
|
155
|
+
if (node.wildcardStore === null)
|
|
156
|
+
node.wildcardStore = store;
|
|
157
|
+
return node.wildcardStore;
|
|
158
|
+
}
|
|
159
|
+
// The final part is static
|
|
160
|
+
if (node.store === null)
|
|
161
|
+
node.store = store;
|
|
162
|
+
return node.store;
|
|
163
|
+
}
|
|
164
|
+
find(method, url) {
|
|
165
|
+
const root = this.root[method];
|
|
166
|
+
if (!root)
|
|
167
|
+
return null;
|
|
168
|
+
return matchRoute(url, url.length, root, 0);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
exports.Memoirist = Memoirist;
|
|
172
|
+
Memoirist.regex = {
|
|
173
|
+
static: /:.+?(?=\/|$)/,
|
|
174
|
+
params: /:.+?(?=\/|$)/g
|
|
175
|
+
};
|
|
176
|
+
const matchRoute = (url, urlLength, node, startIndex) => {
|
|
177
|
+
const part = node?.part;
|
|
178
|
+
const endIndex = startIndex + part.length;
|
|
179
|
+
// Only check the pathPart if its length is > 1 since the parent has
|
|
180
|
+
// already checked that the url matches the first character
|
|
181
|
+
if (part.length > 1) {
|
|
182
|
+
if (endIndex > urlLength)
|
|
183
|
+
return null;
|
|
184
|
+
if (part.length < 15) {
|
|
185
|
+
// Using a loop is faster for short strings
|
|
186
|
+
for (let i = 1, j = startIndex + 1; i < part.length; ++i, ++j)
|
|
187
|
+
if (part.charCodeAt(i) !== url.charCodeAt(j))
|
|
188
|
+
return null;
|
|
189
|
+
}
|
|
190
|
+
else if (url.substring(startIndex, endIndex) !== part)
|
|
191
|
+
return null;
|
|
192
|
+
}
|
|
193
|
+
if (endIndex === urlLength) {
|
|
194
|
+
// Reached the end of the URL
|
|
195
|
+
if (node.store !== null)
|
|
196
|
+
return {
|
|
197
|
+
store: node.store,
|
|
198
|
+
params: {}
|
|
199
|
+
};
|
|
200
|
+
if (node.wildcardStore !== null)
|
|
201
|
+
return {
|
|
202
|
+
store: node.wildcardStore,
|
|
203
|
+
params: { '*': '' }
|
|
204
|
+
};
|
|
205
|
+
return null;
|
|
206
|
+
}
|
|
207
|
+
if (node.inert !== null) {
|
|
208
|
+
const inert = node.inert.get(url.charCodeAt(endIndex));
|
|
209
|
+
if (inert !== undefined) {
|
|
210
|
+
const route = matchRoute(url, urlLength, inert, endIndex);
|
|
211
|
+
if (route !== null)
|
|
212
|
+
return route;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
if (node.params !== null) {
|
|
216
|
+
const param = node.params;
|
|
217
|
+
const slashIndex = url.indexOf('/', endIndex);
|
|
218
|
+
if (slashIndex !== endIndex) {
|
|
219
|
+
// Params cannot be empty
|
|
220
|
+
if (slashIndex === -1 || slashIndex >= urlLength) {
|
|
221
|
+
if (param.store !== null) {
|
|
222
|
+
const params = {};
|
|
223
|
+
const paramName = resolveParamName(param, param.store);
|
|
224
|
+
params[paramName] = url.substring(endIndex, urlLength);
|
|
225
|
+
return {
|
|
226
|
+
store: param.store,
|
|
227
|
+
params
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
else if (param.inert !== null) {
|
|
232
|
+
const route = matchRoute(url, urlLength, param.inert, slashIndex);
|
|
233
|
+
if (route !== null) {
|
|
234
|
+
const paramName = resolveParamName(param, route.store);
|
|
235
|
+
route.params[paramName] = url.substring(endIndex, slashIndex);
|
|
236
|
+
return route;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
if (node.wildcardStore !== null)
|
|
242
|
+
return {
|
|
243
|
+
store: node.wildcardStore,
|
|
244
|
+
params: {
|
|
245
|
+
'*': url.substring(endIndex, urlLength)
|
|
246
|
+
}
|
|
247
|
+
};
|
|
248
|
+
return null;
|
|
249
|
+
};
|
|
250
|
+
exports.default = Memoirist;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import Pino from "pino";
|
|
2
|
+
import { InjectorService } from "../container/InjectorService";
|
|
3
|
+
export interface LoggerAdapter {
|
|
4
|
+
info(message: string, ...args: any[]): void;
|
|
5
|
+
warn(message: string, ...args: any[]): void;
|
|
6
|
+
error(message: string, ...args: any[]): void;
|
|
7
|
+
debug(message: string, ...args: any[]): void;
|
|
8
|
+
fatal(message: string, ...args: any[]): void;
|
|
9
|
+
trace(message: string, ...args: any[]): void;
|
|
10
|
+
}
|
|
11
|
+
export declare class LoggerService implements LoggerAdapter {
|
|
12
|
+
private injector;
|
|
13
|
+
private logger;
|
|
14
|
+
constructor(injector: InjectorService);
|
|
15
|
+
child(bindings: Record<string, any>): LoggerAdapter;
|
|
16
|
+
info(message: string, ...args: any[]): void;
|
|
17
|
+
warn(message: string, ...args: any[]): void;
|
|
18
|
+
error(message: string, ...args: any[]): void;
|
|
19
|
+
debug(message: string, ...args: any[]): void;
|
|
20
|
+
fatal(message: string, ...args: any[]): void;
|
|
21
|
+
trace(message: string, ...args: any[]): void;
|
|
22
|
+
getLogger(): Pino.Logger;
|
|
23
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.LoggerService = void 0;
|
|
7
|
+
const pino_1 = __importDefault(require("pino"));
|
|
8
|
+
class LoggerService {
|
|
9
|
+
constructor(injector) {
|
|
10
|
+
this.injector = injector;
|
|
11
|
+
const pinoConfig = this.injector.applicationConfig.logger || {};
|
|
12
|
+
pinoConfig['transport'] = pinoConfig.transport || {
|
|
13
|
+
target: 'pino-pretty',
|
|
14
|
+
options: {
|
|
15
|
+
colorize: true,
|
|
16
|
+
ignore: 'pid,hostname',
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
this.logger = (0, pino_1.default)(pinoConfig);
|
|
20
|
+
}
|
|
21
|
+
child(bindings) {
|
|
22
|
+
const child = this.logger.child(bindings);
|
|
23
|
+
return {
|
|
24
|
+
info: (message, ...args) => child.info(message, ...args),
|
|
25
|
+
warn: (message, ...args) => child.warn(message, ...args),
|
|
26
|
+
error: (message, ...args) => child.error(message, ...args),
|
|
27
|
+
debug: (message, ...args) => child.debug(message, ...args),
|
|
28
|
+
fatal: (message, ...args) => child.fatal(message, ...args),
|
|
29
|
+
trace: (message, ...args) => child.trace(message, ...args),
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
info(message, ...args) {
|
|
33
|
+
this.logger.info(message, ...args);
|
|
34
|
+
}
|
|
35
|
+
warn(message, ...args) {
|
|
36
|
+
this.logger.warn(message, ...args);
|
|
37
|
+
}
|
|
38
|
+
error(message, ...args) {
|
|
39
|
+
this.logger.error(message, ...args);
|
|
40
|
+
}
|
|
41
|
+
debug(message, ...args) {
|
|
42
|
+
this.logger.debug(message, ...args);
|
|
43
|
+
}
|
|
44
|
+
fatal(message, ...args) {
|
|
45
|
+
this.logger.fatal(message, ...args);
|
|
46
|
+
}
|
|
47
|
+
trace(message, ...args) {
|
|
48
|
+
this.logger.trace(message, ...args);
|
|
49
|
+
}
|
|
50
|
+
getLogger() {
|
|
51
|
+
return this.logger;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
exports.LoggerService = LoggerService;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Context } from "../domain/Context";
|
|
2
|
+
import { LoggerService, LoggerAdapter } from "./logger.service";
|
|
3
|
+
export declare class RequestLogger implements LoggerAdapter {
|
|
4
|
+
private logger;
|
|
5
|
+
private context;
|
|
6
|
+
private childLogger;
|
|
7
|
+
constructor(logger: LoggerService, context: Context);
|
|
8
|
+
private initializeChildLogger;
|
|
9
|
+
info(message: string, ...args: any[]): void;
|
|
10
|
+
warn(message: string, ...args: any[]): void;
|
|
11
|
+
error(message: string, ...args: any[]): void;
|
|
12
|
+
debug(message: string, ...args: any[]): void;
|
|
13
|
+
fatal(message: string, ...args: any[]): void;
|
|
14
|
+
trace(message: string, ...args: any[]): void;
|
|
15
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.RequestLogger = void 0;
|
|
13
|
+
const Injectable_decorator_1 = require("../commons/decorators/Injectable.decorator");
|
|
14
|
+
const provider_scope_1 = require("../domain/provider-scope");
|
|
15
|
+
const Context_1 = require("../domain/Context");
|
|
16
|
+
const logger_service_1 = require("./logger.service");
|
|
17
|
+
let RequestLogger = class RequestLogger {
|
|
18
|
+
constructor(logger, context) {
|
|
19
|
+
this.logger = logger;
|
|
20
|
+
this.context = context;
|
|
21
|
+
this.initializeChildLogger();
|
|
22
|
+
}
|
|
23
|
+
initializeChildLogger() {
|
|
24
|
+
this.childLogger = this.logger.child({ trackingId: this.context.trackingId });
|
|
25
|
+
}
|
|
26
|
+
info(message, ...args) {
|
|
27
|
+
this.childLogger.info(message, ...args);
|
|
28
|
+
}
|
|
29
|
+
warn(message, ...args) {
|
|
30
|
+
this.childLogger.warn(message, ...args);
|
|
31
|
+
}
|
|
32
|
+
error(message, ...args) {
|
|
33
|
+
this.childLogger.error(message, ...args);
|
|
34
|
+
}
|
|
35
|
+
debug(message, ...args) {
|
|
36
|
+
this.childLogger.debug(message, ...args);
|
|
37
|
+
}
|
|
38
|
+
fatal(message, ...args) {
|
|
39
|
+
this.childLogger.fatal(message, ...args);
|
|
40
|
+
}
|
|
41
|
+
trace(message, ...args) {
|
|
42
|
+
this.childLogger.trace(message, ...args);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
exports.RequestLogger = RequestLogger;
|
|
46
|
+
exports.RequestLogger = RequestLogger = __decorate([
|
|
47
|
+
(0, Injectable_decorator_1.Injectable)({ scope: provider_scope_1.ProviderScope.REQUEST }),
|
|
48
|
+
__metadata("design:paramtypes", [logger_service_1.LoggerService,
|
|
49
|
+
Context_1.Context])
|
|
50
|
+
], RequestLogger);
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { Server } from 'bun';
|
|
2
|
+
import { Carno, ApplicationConfig } from '../Carno';
|
|
3
|
+
import { TokenProvider, Type } from '../commons';
|
|
4
|
+
import { InjectorService } from '../container/InjectorService';
|
|
5
|
+
export type CoreTestOptions = {
|
|
6
|
+
config?: ApplicationConfig;
|
|
7
|
+
listen?: boolean | number;
|
|
8
|
+
port?: number;
|
|
9
|
+
plugins?: CoreTestPlugin[];
|
|
10
|
+
};
|
|
11
|
+
export type CoreTestPlugin = Carno | (() => Carno);
|
|
12
|
+
export type CoreTestHarness = {
|
|
13
|
+
app: Carno;
|
|
14
|
+
injector: InjectorService;
|
|
15
|
+
server?: Server<any>;
|
|
16
|
+
port?: number;
|
|
17
|
+
resolve<T>(token: Type<T>): T;
|
|
18
|
+
resolve<T>(token: TokenProvider<T>): T;
|
|
19
|
+
request(target: string | URL, init?: RequestInit): Promise<Response>;
|
|
20
|
+
close(): Promise<void>;
|
|
21
|
+
};
|
|
22
|
+
export type CoreTestRoutine = (harness: CoreTestHarness) => Promise<void>;
|
|
23
|
+
export declare function createCoreTestHarness(options?: CoreTestOptions): Promise<CoreTestHarness>;
|
|
24
|
+
export declare function withCoreApplication(routine: CoreTestRoutine, options?: CoreTestOptions): Promise<void>;
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createCoreTestHarness = createCoreTestHarness;
|
|
4
|
+
exports.withCoreApplication = withCoreApplication;
|
|
5
|
+
const Carno_1 = require("../Carno");
|
|
6
|
+
async function createCoreTestHarness(options = {}) {
|
|
7
|
+
const app = new Carno_1.Carno(options.config);
|
|
8
|
+
applyPlugins(app, options.plugins);
|
|
9
|
+
const boot = await bootApplication(app, options);
|
|
10
|
+
const injector = app.getInjector();
|
|
11
|
+
const resolve = (token) => {
|
|
12
|
+
return injector.invoke(token);
|
|
13
|
+
};
|
|
14
|
+
const request = async (target, init) => {
|
|
15
|
+
if (!boot.server) {
|
|
16
|
+
throw new Error('HTTP server is not running. Enable the listen option to issue requests.');
|
|
17
|
+
}
|
|
18
|
+
const url = buildRequestUrl(boot.port ?? boot.server.port, target);
|
|
19
|
+
return fetch(url, init);
|
|
20
|
+
};
|
|
21
|
+
const close = async () => shutdown(app, boot.server);
|
|
22
|
+
return {
|
|
23
|
+
app,
|
|
24
|
+
injector,
|
|
25
|
+
server: boot.server,
|
|
26
|
+
port: boot.port ?? boot.server?.port,
|
|
27
|
+
resolve,
|
|
28
|
+
request,
|
|
29
|
+
close,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
async function withCoreApplication(routine, options = {}) {
|
|
33
|
+
const harness = await createCoreTestHarness(options);
|
|
34
|
+
try {
|
|
35
|
+
await routine(harness);
|
|
36
|
+
}
|
|
37
|
+
finally {
|
|
38
|
+
await harness.close();
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
async function bootApplication(app, options) {
|
|
42
|
+
if (!shouldListen(options.listen)) {
|
|
43
|
+
await app.init();
|
|
44
|
+
return {};
|
|
45
|
+
}
|
|
46
|
+
const port = resolveListenPort(options);
|
|
47
|
+
await app.listen(port);
|
|
48
|
+
const server = app.getHttpServer();
|
|
49
|
+
const runningPort = server?.port ?? port;
|
|
50
|
+
return { server, port: runningPort };
|
|
51
|
+
}
|
|
52
|
+
function shouldListen(value) {
|
|
53
|
+
if (typeof value === 'number') {
|
|
54
|
+
return true;
|
|
55
|
+
}
|
|
56
|
+
return Boolean(value);
|
|
57
|
+
}
|
|
58
|
+
function resolveListenPort(options) {
|
|
59
|
+
if (typeof options.listen === 'number') {
|
|
60
|
+
return options.listen;
|
|
61
|
+
}
|
|
62
|
+
if (typeof options.port === 'number') {
|
|
63
|
+
return options.port;
|
|
64
|
+
}
|
|
65
|
+
return 0;
|
|
66
|
+
}
|
|
67
|
+
function buildRequestUrl(port, target) {
|
|
68
|
+
if (target instanceof URL) {
|
|
69
|
+
return target.toString();
|
|
70
|
+
}
|
|
71
|
+
if (isAbsoluteUrl(target)) {
|
|
72
|
+
return target;
|
|
73
|
+
}
|
|
74
|
+
const path = target.startsWith('/') ? target : `/${target}`;
|
|
75
|
+
return `http://127.0.0.1:${port}${path}`;
|
|
76
|
+
}
|
|
77
|
+
function isAbsoluteUrl(target) {
|
|
78
|
+
return /^https?:\/\//i.test(target);
|
|
79
|
+
}
|
|
80
|
+
async function shutdown(app, server) {
|
|
81
|
+
if (!server) {
|
|
82
|
+
app.close();
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
app.close(true);
|
|
86
|
+
}
|
|
87
|
+
function applyPlugins(app, plugins) {
|
|
88
|
+
if (!plugins?.length) {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
plugins.forEach((plugin) => app.use(resolvePlugin(plugin)));
|
|
92
|
+
}
|
|
93
|
+
function resolvePlugin(entry) {
|
|
94
|
+
if (typeof entry === 'function') {
|
|
95
|
+
return entry();
|
|
96
|
+
}
|
|
97
|
+
return entry;
|
|
98
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './core-testing';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./core-testing"), exports);
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ancestorOf = ancestorOf;
|
|
4
|
+
exports.getInheritedClass = getInheritedClass;
|
|
5
|
+
function ancestorOf(target) {
|
|
6
|
+
return target && Object.getPrototypeOf(target);
|
|
7
|
+
}
|
|
8
|
+
function getInheritedClass(target) {
|
|
9
|
+
return ancestorOf(target);
|
|
10
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ancestorsOf = ancestorsOf;
|
|
4
|
+
const classOf_1 = require("./classOf");
|
|
5
|
+
const ancestorOf_1 = require("./ancestorOf");
|
|
6
|
+
const nameOf_1 = require("./nameOf");
|
|
7
|
+
/**
|
|
8
|
+
*
|
|
9
|
+
* @param target
|
|
10
|
+
* @returns {Array}
|
|
11
|
+
*/
|
|
12
|
+
function ancestorsOf(target) {
|
|
13
|
+
const classes = [];
|
|
14
|
+
let currentTarget = (0, classOf_1.classOf)(target);
|
|
15
|
+
while (currentTarget && (0, nameOf_1.nameOf)(currentTarget) !== "") {
|
|
16
|
+
classes.unshift(currentTarget);
|
|
17
|
+
currentTarget = (0, ancestorOf_1.getInheritedClass)(currentTarget);
|
|
18
|
+
}
|
|
19
|
+
return classes;
|
|
20
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Get the provide constructor if target is an instance.
|
|
3
|
+
* @param target
|
|
4
|
+
* @returns {*}
|
|
5
|
+
*/
|
|
6
|
+
export declare function getClass(target: any): any;
|
|
7
|
+
/**
|
|
8
|
+
* Get the provide constructor if target is an instance.
|
|
9
|
+
* @param target
|
|
10
|
+
* @returns {*}
|
|
11
|
+
* @alias getClass
|
|
12
|
+
*/
|
|
13
|
+
export declare function classOf(target: any): any;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getClass = getClass;
|
|
4
|
+
exports.classOf = classOf;
|
|
5
|
+
/**
|
|
6
|
+
* Get the provide constructor if target is an instance.
|
|
7
|
+
* @param target
|
|
8
|
+
* @returns {*}
|
|
9
|
+
*/
|
|
10
|
+
function getClass(target) {
|
|
11
|
+
return target.prototype ? target : target.constructor;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Get the provide constructor if target is an instance.
|
|
15
|
+
* @param target
|
|
16
|
+
* @returns {*}
|
|
17
|
+
* @alias getClass
|
|
18
|
+
*/
|
|
19
|
+
function classOf(target) {
|
|
20
|
+
return getClass(target);
|
|
21
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.cleanObject = cleanObject;
|
|
4
|
+
const isProtectedKey_1 = require("./isProtectedKey");
|
|
5
|
+
/**
|
|
6
|
+
* Remove undefined value
|
|
7
|
+
* @param obj
|
|
8
|
+
* @param ignore
|
|
9
|
+
*/
|
|
10
|
+
function cleanObject(obj, ignore = []) {
|
|
11
|
+
return Object.entries(obj).reduce((obj, [key, value]) => {
|
|
12
|
+
if ((0, isProtectedKey_1.isProtectedKey)(key) || ignore.includes(key)) {
|
|
13
|
+
return obj;
|
|
14
|
+
}
|
|
15
|
+
return value === undefined
|
|
16
|
+
? obj
|
|
17
|
+
: {
|
|
18
|
+
...obj,
|
|
19
|
+
[key]: value
|
|
20
|
+
};
|
|
21
|
+
}, {});
|
|
22
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Type } from "../commons";
|
|
2
|
+
/**
|
|
3
|
+
* Get the class constructor
|
|
4
|
+
* @param target
|
|
5
|
+
*/
|
|
6
|
+
export declare function getConstructor(target: any): Type<any>;
|
|
7
|
+
/**
|
|
8
|
+
* Get the class constructor
|
|
9
|
+
* @param target
|
|
10
|
+
*/
|
|
11
|
+
export declare function constructorOf(target: any): Type<any>;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getConstructor = getConstructor;
|
|
4
|
+
exports.constructorOf = constructorOf;
|
|
5
|
+
/**
|
|
6
|
+
* Get the class constructor
|
|
7
|
+
* @param target
|
|
8
|
+
*/
|
|
9
|
+
function getConstructor(target) {
|
|
10
|
+
return typeof target === "function" ? target : target.constructor;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Get the class constructor
|
|
14
|
+
* @param target
|
|
15
|
+
*/
|
|
16
|
+
function constructorOf(target) {
|
|
17
|
+
return getConstructor(target);
|
|
18
|
+
}
|