@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.
Files changed (214) hide show
  1. package/LICENSE +674 -0
  2. package/dist/Carno.d.ts +65 -0
  3. package/dist/Carno.js +307 -0
  4. package/dist/Cheetah.d.ts +65 -0
  5. package/dist/Cheetah.js +307 -0
  6. package/dist/cache/bento-cache.driver.d.ts +13 -0
  7. package/dist/cache/bento-cache.driver.js +55 -0
  8. package/dist/cache/cache.service.d.ts +8 -0
  9. package/dist/cache/cache.service.js +6 -0
  10. package/dist/commons/decorators/Injectable.decorator.d.ts +20 -0
  11. package/dist/commons/decorators/Injectable.decorator.js +33 -0
  12. package/dist/commons/decorators/controller.decorator.d.ts +8 -0
  13. package/dist/commons/decorators/controller.decorator.js +22 -0
  14. package/dist/commons/decorators/http.decorators.d.ts +13 -0
  15. package/dist/commons/decorators/http.decorators.js +44 -0
  16. package/dist/commons/decorators/index.d.ts +5 -0
  17. package/dist/commons/decorators/index.js +21 -0
  18. package/dist/commons/decorators/inject.decorator.d.ts +1 -0
  19. package/dist/commons/decorators/inject.decorator.js +5 -0
  20. package/dist/commons/decorators/middleware.decorator.d.ts +2 -0
  21. package/dist/commons/decorators/middleware.decorator.js +30 -0
  22. package/dist/commons/decorators/service.decorator.d.ts +2 -0
  23. package/dist/commons/decorators/service.decorator.js +7 -0
  24. package/dist/commons/http-code.enum.d.ts +50 -0
  25. package/dist/commons/http-code.enum.js +54 -0
  26. package/dist/commons/index.d.ts +3 -0
  27. package/dist/commons/index.js +19 -0
  28. package/dist/commons/registries/ProviderControl.d.ts +77 -0
  29. package/dist/commons/registries/ProviderControl.js +112 -0
  30. package/dist/commons/registries/ProviderRegistry.d.ts +7 -0
  31. package/dist/commons/registries/ProviderRegistry.js +20 -0
  32. package/dist/constants.d.ts +7 -0
  33. package/dist/constants.js +10 -0
  34. package/dist/container/ContainerConfiguration.d.ts +45 -0
  35. package/dist/container/ContainerConfiguration.js +121 -0
  36. package/dist/container/DependencyResolver.d.ts +18 -0
  37. package/dist/container/DependencyResolver.js +81 -0
  38. package/dist/container/InjectorService.d.ts +34 -0
  39. package/dist/container/InjectorService.js +157 -0
  40. package/dist/container/MethodInvoker.d.ts +17 -0
  41. package/dist/container/MethodInvoker.js +69 -0
  42. package/dist/container/RouteResolver.d.ts +27 -0
  43. package/dist/container/RouteResolver.js +173 -0
  44. package/dist/container/container.d.ts +41 -0
  45. package/dist/container/container.js +71 -0
  46. package/dist/container/createContainer.d.ts +3 -0
  47. package/dist/container/createContainer.js +12 -0
  48. package/dist/container/createInjector.d.ts +2 -0
  49. package/dist/container/createInjector.js +7 -0
  50. package/dist/container/index.d.ts +6 -0
  51. package/dist/container/index.js +22 -0
  52. package/dist/container/middleware.resolver.d.ts +9 -0
  53. package/dist/container/middleware.resolver.js +35 -0
  54. package/dist/default-routes-carno.d.ts +3 -0
  55. package/dist/default-routes-carno.js +29 -0
  56. package/dist/default-routes-cheetah.d.ts +3 -0
  57. package/dist/default-routes-cheetah.js +29 -0
  58. package/dist/domain/CarnoClosure.d.ts +1 -0
  59. package/dist/domain/CarnoClosure.js +2 -0
  60. package/dist/domain/CarnoMiddleware.d.ts +5 -0
  61. package/dist/domain/CarnoMiddleware.js +2 -0
  62. package/dist/domain/CheetahClosure.d.ts +1 -0
  63. package/dist/domain/CheetahClosure.js +2 -0
  64. package/dist/domain/CheetahMiddleware.d.ts +5 -0
  65. package/dist/domain/CheetahMiddleware.js +2 -0
  66. package/dist/domain/Context.d.ts +26 -0
  67. package/dist/domain/Context.js +107 -0
  68. package/dist/domain/LocalsContainer.d.ts +4 -0
  69. package/dist/domain/LocalsContainer.js +10 -0
  70. package/dist/domain/Metadata.d.ts +449 -0
  71. package/dist/domain/Metadata.js +511 -0
  72. package/dist/domain/cors-config.d.ts +12 -0
  73. package/dist/domain/cors-config.js +18 -0
  74. package/dist/domain/http-method.d.ts +7 -0
  75. package/dist/domain/http-method.js +11 -0
  76. package/dist/domain/index.d.ts +10 -0
  77. package/dist/domain/index.js +26 -0
  78. package/dist/domain/provider-scope.d.ts +5 -0
  79. package/dist/domain/provider-scope.js +9 -0
  80. package/dist/domain/provider-type.d.ts +6 -0
  81. package/dist/domain/provider-type.js +10 -0
  82. package/dist/domain/provider.d.ts +37 -0
  83. package/dist/domain/provider.js +70 -0
  84. package/dist/events/hooks.decorator.d.ts +3 -0
  85. package/dist/events/hooks.decorator.js +29 -0
  86. package/dist/events/index.d.ts +2 -0
  87. package/dist/events/index.js +18 -0
  88. package/dist/events/on-event.d.ts +13 -0
  89. package/dist/events/on-event.js +11 -0
  90. package/dist/exceptions/HttpException.d.ts +9 -0
  91. package/dist/exceptions/HttpException.js +26 -0
  92. package/dist/exceptions/index.d.ts +1 -0
  93. package/dist/exceptions/index.js +17 -0
  94. package/dist/index.d.ts +16 -0
  95. package/dist/index.js +32 -0
  96. package/dist/route/Matcher.d.ts +16 -0
  97. package/dist/route/Matcher.js +48 -0
  98. package/dist/route/RouteExecutor.d.ts +13 -0
  99. package/dist/route/RouteExecutor.js +70 -0
  100. package/dist/route/memoirist.d.ts +25 -0
  101. package/dist/route/memoirist.js +250 -0
  102. package/dist/services/logger.service.d.ts +23 -0
  103. package/dist/services/logger.service.js +54 -0
  104. package/dist/services/request-logger.service.d.ts +15 -0
  105. package/dist/services/request-logger.service.js +50 -0
  106. package/dist/testing/core-testing.d.ts +24 -0
  107. package/dist/testing/core-testing.js +98 -0
  108. package/dist/testing/index.d.ts +1 -0
  109. package/dist/testing/index.js +17 -0
  110. package/dist/utils/ancestorOf.d.ts +2 -0
  111. package/dist/utils/ancestorOf.js +10 -0
  112. package/dist/utils/ancestorsOf.d.ts +6 -0
  113. package/dist/utils/ancestorsOf.js +20 -0
  114. package/dist/utils/classOf.d.ts +13 -0
  115. package/dist/utils/classOf.js +21 -0
  116. package/dist/utils/cleanObject.d.ts +6 -0
  117. package/dist/utils/cleanObject.js +22 -0
  118. package/dist/utils/constructorOf.d.ts +11 -0
  119. package/dist/utils/constructorOf.js +18 -0
  120. package/dist/utils/createInstance.d.ts +1 -0
  121. package/dist/utils/createInstance.js +7 -0
  122. package/dist/utils/decoratorTypeOf.d.ts +11 -0
  123. package/dist/utils/decoratorTypeOf.js +32 -0
  124. package/dist/utils/deepClone.d.ts +6 -0
  125. package/dist/utils/deepClone.js +63 -0
  126. package/dist/utils/deepMerge.d.ts +9 -0
  127. package/dist/utils/deepMerge.js +62 -0
  128. package/dist/utils/descriptorOf.d.ts +8 -0
  129. package/dist/utils/descriptorOf.js +16 -0
  130. package/dist/utils/getClassOrSymbol.d.ts +1 -0
  131. package/dist/utils/getClassOrSymbol.js +8 -0
  132. package/dist/utils/getConstructorArgNames.d.ts +1 -0
  133. package/dist/utils/getConstructorArgNames.js +12 -0
  134. package/dist/utils/getMethodArgTypes.d.ts +1 -0
  135. package/dist/utils/getMethodArgTypes.js +9 -0
  136. package/dist/utils/getValue.d.ts +32 -0
  137. package/dist/utils/getValue.js +47 -0
  138. package/dist/utils/hasJsonMethod.d.ts +1 -0
  139. package/dist/utils/hasJsonMethod.js +6 -0
  140. package/dist/utils/index.d.ts +15 -0
  141. package/dist/utils/index.js +31 -0
  142. package/dist/utils/isArray.d.ts +13 -0
  143. package/dist/utils/isArray.js +21 -0
  144. package/dist/utils/isArrowFn.d.ts +1 -0
  145. package/dist/utils/isArrowFn.js +7 -0
  146. package/dist/utils/isBoolean.d.ts +7 -0
  147. package/dist/utils/isBoolean.js +15 -0
  148. package/dist/utils/isBuffer.d.ts +7 -0
  149. package/dist/utils/isBuffer.js +19 -0
  150. package/dist/utils/isClass.d.ts +1 -0
  151. package/dist/utils/isClass.js +26 -0
  152. package/dist/utils/isClassValidator.d.ts +6 -0
  153. package/dist/utils/isClassValidator.js +13 -0
  154. package/dist/utils/isCollection.d.ts +6 -0
  155. package/dist/utils/isCollection.js +20 -0
  156. package/dist/utils/isDate.d.ts +6 -0
  157. package/dist/utils/isDate.js +11 -0
  158. package/dist/utils/isEmpty.d.ts +6 -0
  159. package/dist/utils/isEmpty.js +12 -0
  160. package/dist/utils/isFunction.d.ts +1 -0
  161. package/dist/utils/isFunction.js +6 -0
  162. package/dist/utils/isInheritedFrom.d.ts +1 -0
  163. package/dist/utils/isInheritedFrom.js +24 -0
  164. package/dist/utils/isMomentObject.d.ts +1 -0
  165. package/dist/utils/isMomentObject.js +6 -0
  166. package/dist/utils/isMongooseObject.d.ts +2 -0
  167. package/dist/utils/isMongooseObject.js +11 -0
  168. package/dist/utils/isNil.d.ts +1 -0
  169. package/dist/utils/isNil.js +6 -0
  170. package/dist/utils/isNumber.d.ts +7 -0
  171. package/dist/utils/isNumber.js +15 -0
  172. package/dist/utils/isObject.d.ts +1 -0
  173. package/dist/utils/isObject.js +6 -0
  174. package/dist/utils/isObservable.d.ts +1 -0
  175. package/dist/utils/isObservable.js +6 -0
  176. package/dist/utils/isPlainObject.d.ts +7 -0
  177. package/dist/utils/isPlainObject.js +16 -0
  178. package/dist/utils/isPrimitive.d.ts +14 -0
  179. package/dist/utils/isPrimitive.js +28 -0
  180. package/dist/utils/isPrimitiveType.d.ts +1 -0
  181. package/dist/utils/isPrimitiveType.js +11 -0
  182. package/dist/utils/isPromise.d.ts +7 -0
  183. package/dist/utils/isPromise.js +14 -0
  184. package/dist/utils/isProtectedKey.d.ts +5 -0
  185. package/dist/utils/isProtectedKey.js +10 -0
  186. package/dist/utils/isRegExp.d.ts +1 -0
  187. package/dist/utils/isRegExp.js +6 -0
  188. package/dist/utils/isRequestScope.d.ts +11 -0
  189. package/dist/utils/isRequestScope.js +23 -0
  190. package/dist/utils/isSerializable.d.ts +1 -0
  191. package/dist/utils/isSerializable.js +11 -0
  192. package/dist/utils/isStream.d.ts +1 -0
  193. package/dist/utils/isStream.js +6 -0
  194. package/dist/utils/isString.d.ts +6 -0
  195. package/dist/utils/isString.js +14 -0
  196. package/dist/utils/isSymbol.d.ts +6 -0
  197. package/dist/utils/isSymbol.js +14 -0
  198. package/dist/utils/methodsOf.d.ts +9 -0
  199. package/dist/utils/methodsOf.js +24 -0
  200. package/dist/utils/nameOf.d.ts +14 -0
  201. package/dist/utils/nameOf.js +31 -0
  202. package/dist/utils/objectKeys.d.ts +1 -0
  203. package/dist/utils/objectKeys.js +7 -0
  204. package/dist/utils/primitiveOf.d.ts +1 -0
  205. package/dist/utils/primitiveOf.js +18 -0
  206. package/dist/utils/prototypeOf.d.ts +6 -0
  207. package/dist/utils/prototypeOf.js +12 -0
  208. package/dist/utils/setValue.d.ts +1 -0
  209. package/dist/utils/setValue.js +32 -0
  210. package/dist/utils/toMap.d.ts +3 -0
  211. package/dist/utils/toMap.js +34 -0
  212. package/dist/utils/toStringConstructor.d.ts +1 -0
  213. package/dist/utils/toStringConstructor.js +10 -0
  214. 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,2 @@
1
+ export declare function ancestorOf(target: any): any;
2
+ export declare function getInheritedClass(target: any): any;
@@ -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,6 @@
1
+ /**
2
+ *
3
+ * @param target
4
+ * @returns {Array}
5
+ */
6
+ export declare function ancestorsOf(target: any): any[];
@@ -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,6 @@
1
+ /**
2
+ * Remove undefined value
3
+ * @param obj
4
+ * @param ignore
5
+ */
6
+ export declare function cleanObject(obj: any, ignore?: string[]): any;
@@ -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
+ }