@webda/core 2.0.0-beta.1 → 2.0.0
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/lib/application.d.ts +9 -21
- package/lib/application.js +71 -84
- package/lib/application.js.map +1 -1
- package/lib/application.spec.js +121 -140
- package/lib/application.spec.js.map +1 -1
- package/lib/core.d.ts +86 -46
- package/lib/core.js +254 -316
- package/lib/core.js.map +1 -1
- package/lib/core.spec.js +326 -391
- package/lib/core.spec.js.map +1 -1
- package/lib/index.d.ts +49 -45
- package/lib/index.js +54 -67
- package/lib/index.js.map +1 -1
- package/lib/models/aclmodel.d.ts +2 -2
- package/lib/models/aclmodel.js +63 -90
- package/lib/models/aclmodel.js.map +1 -1
- package/lib/models/aclmodel.spec.js +127 -151
- package/lib/models/aclmodel.spec.js.map +1 -1
- package/lib/models/coremodel.d.ts +84 -11
- package/lib/models/coremodel.js +104 -103
- package/lib/models/coremodel.js.map +1 -1
- package/lib/models/coremodel.spec.js +91 -91
- package/lib/models/coremodel.spec.js.map +1 -1
- package/lib/models/deployment.js +1 -2
- package/lib/models/ident.d.ts +5 -3
- package/lib/models/ident.js +4 -9
- package/lib/models/ident.js.map +1 -1
- package/lib/models/ident.spec.js +9 -11
- package/lib/models/ident.spec.js.map +1 -1
- package/lib/models/ownermodel.d.ts +12 -2
- package/lib/models/ownermodel.js +54 -62
- package/lib/models/ownermodel.js.map +1 -1
- package/lib/models/ownermodel.spec.js +129 -152
- package/lib/models/ownermodel.spec.js.map +1 -1
- package/lib/models/rolemodel.d.ts +2 -2
- package/lib/models/rolemodel.js +24 -40
- package/lib/models/rolemodel.js.map +1 -1
- package/lib/models/rolemodel.spec.js +44 -61
- package/lib/models/rolemodel.spec.js.map +1 -1
- package/lib/models/user.d.ts +5 -5
- package/lib/models/user.js +3 -8
- package/lib/models/user.js.map +1 -1
- package/lib/models/user.spec.js +14 -16
- package/lib/models/user.spec.js.map +1 -1
- package/lib/queues/filequeue.d.ts +2 -2
- package/lib/queues/filequeue.js +72 -93
- package/lib/queues/filequeue.js.map +1 -1
- package/lib/queues/filequeue.spec.js +53 -70
- package/lib/queues/filequeue.spec.js.map +1 -1
- package/lib/queues/memoryqueue.d.ts +2 -2
- package/lib/queues/memoryqueue.js +37 -61
- package/lib/queues/memoryqueue.js.map +1 -1
- package/lib/queues/memoryqueue.spec.js +142 -165
- package/lib/queues/memoryqueue.spec.js.map +1 -1
- package/lib/queues/pubsubservice.d.ts +2 -2
- package/lib/queues/pubsubservice.js +3 -7
- package/lib/queues/pubsubservice.js.map +1 -1
- package/lib/queues/queue.spec.d.ts +1 -1
- package/lib/queues/queue.spec.js +42 -56
- package/lib/queues/queue.spec.js.map +1 -1
- package/lib/queues/queueservice.d.ts +4 -4
- package/lib/queues/queueservice.js +52 -67
- package/lib/queues/queueservice.js.map +1 -1
- package/lib/router.d.ts +7 -3
- package/lib/router.js +49 -48
- package/lib/router.js.map +1 -1
- package/lib/router.spec.js +64 -61
- package/lib/router.spec.js.map +1 -1
- package/lib/services/asyncevents.d.ts +2 -2
- package/lib/services/asyncevents.js +44 -64
- package/lib/services/asyncevents.js.map +1 -1
- package/lib/services/asyncevents.spec.js +81 -98
- package/lib/services/asyncevents.spec.js.map +1 -1
- package/lib/services/authentication.d.ts +28 -19
- package/lib/services/authentication.js +539 -571
- package/lib/services/authentication.js.map +1 -1
- package/lib/services/authentication.spec.js +411 -447
- package/lib/services/authentication.spec.js.map +1 -1
- package/lib/services/binary.d.ts +8 -6
- package/lib/services/binary.js +212 -244
- package/lib/services/binary.js.map +1 -1
- package/lib/services/binary.spec.d.ts +3 -3
- package/lib/services/binary.spec.js +390 -443
- package/lib/services/binary.spec.js.map +1 -1
- package/lib/services/cloudbinary.d.ts +3 -3
- package/lib/services/cloudbinary.js +46 -71
- package/lib/services/cloudbinary.js.map +1 -1
- package/lib/services/cloudbinary.spec.d.ts +3 -3
- package/lib/services/cloudbinary.spec.js +133 -162
- package/lib/services/cloudbinary.spec.js.map +1 -1
- package/lib/services/configuration.d.ts +6 -3
- package/lib/services/configuration.js +78 -103
- package/lib/services/configuration.js.map +1 -1
- package/lib/services/configuration.spec.js +63 -83
- package/lib/services/configuration.spec.js.map +1 -1
- package/lib/services/cron.d.ts +3 -2
- package/lib/services/cron.js +9 -13
- package/lib/services/cron.js.map +1 -1
- package/lib/services/cron.spec.js +18 -21
- package/lib/services/cron.spec.js.map +1 -1
- package/lib/services/cryptoservice.d.ts +258 -0
- package/lib/services/cryptoservice.js +312 -0
- package/lib/services/cryptoservice.js.map +1 -0
- package/lib/services/cryptoservice.spec.d.ts +1 -0
- package/lib/services/cryptoservice.spec.js +137 -0
- package/lib/services/cryptoservice.spec.js.map +1 -0
- package/lib/services/debugmailer.d.ts +1 -1
- package/lib/services/debugmailer.js +13 -34
- package/lib/services/debugmailer.js.map +1 -1
- package/lib/services/debugmailer.spec.js +16 -29
- package/lib/services/debugmailer.spec.js.map +1 -1
- package/lib/services/echo.d.ts +2 -2
- package/lib/services/echo.js +15 -31
- package/lib/services/echo.js.map +1 -1
- package/lib/services/echo.spec.js +27 -40
- package/lib/services/echo.spec.js.map +1 -1
- package/lib/services/filebinary.d.ts +15 -5
- package/lib/services/filebinary.js +171 -200
- package/lib/services/filebinary.js.map +1 -1
- package/lib/services/filebinary.spec.d.ts +2 -1
- package/lib/services/filebinary.spec.js +106 -117
- package/lib/services/filebinary.spec.js.map +1 -1
- package/lib/services/fileconfiguration.d.ts +2 -2
- package/lib/services/fileconfiguration.js +25 -43
- package/lib/services/fileconfiguration.js.map +1 -1
- package/lib/services/fileconfiguration.spec.js +79 -109
- package/lib/services/fileconfiguration.spec.js.map +1 -1
- package/lib/services/invitationservice.d.ts +9 -9
- package/lib/services/invitationservice.js +275 -313
- package/lib/services/invitationservice.js.map +1 -1
- package/lib/services/invitationservice.spec.js +442 -448
- package/lib/services/invitationservice.spec.js.map +1 -1
- package/lib/services/kubernetesconfiguration.d.ts +2 -2
- package/lib/services/kubernetesconfiguration.js +41 -59
- package/lib/services/kubernetesconfiguration.js.map +1 -1
- package/lib/services/kubernetesconfiguration.spec.js +80 -98
- package/lib/services/kubernetesconfiguration.spec.js.map +1 -1
- package/lib/services/mailer.d.ts +4 -4
- package/lib/services/mailer.js +62 -86
- package/lib/services/mailer.js.map +1 -1
- package/lib/services/mailer.spec.js +86 -127
- package/lib/services/mailer.spec.js.map +1 -1
- package/lib/services/notificationservice.d.ts +4 -4
- package/lib/services/notificationservice.js +23 -42
- package/lib/services/notificationservice.js.map +1 -1
- package/lib/services/notificationservice.spec.js +47 -73
- package/lib/services/notificationservice.spec.js.map +1 -1
- package/lib/services/oauth.d.ts +32 -6
- package/lib/services/oauth.js +73 -78
- package/lib/services/oauth.js.map +1 -1
- package/lib/services/oauth.spec.js +128 -147
- package/lib/services/oauth.spec.js.map +1 -1
- package/lib/services/proxy.d.ts +74 -0
- package/lib/services/proxy.js +126 -0
- package/lib/services/proxy.js.map +1 -0
- package/lib/services/proxy.spec.d.ts +1 -0
- package/lib/services/proxy.spec.js +59 -0
- package/lib/services/proxy.spec.js.map +1 -0
- package/lib/services/resource.d.ts +2 -2
- package/lib/services/resource.js +12 -18
- package/lib/services/resource.js.map +1 -1
- package/lib/services/resource.spec.js +67 -99
- package/lib/services/resource.spec.js.map +1 -1
- package/lib/services/service.d.ts +33 -7
- package/lib/services/service.js +74 -42
- package/lib/services/service.js.map +1 -1
- package/lib/services/service.spec.js +62 -67
- package/lib/services/service.spec.js.map +1 -1
- package/lib/services/version.d.ts +1 -1
- package/lib/services/version.js +3 -8
- package/lib/services/version.js.map +1 -1
- package/lib/services/version.spec.js +11 -24
- package/lib/services/version.spec.js.map +1 -1
- package/lib/stores/aggregator.d.ts +3 -3
- package/lib/stores/aggregator.js +37 -57
- package/lib/stores/aggregator.js.map +1 -1
- package/lib/stores/file.d.ts +3 -3
- package/lib/stores/file.js +93 -131
- package/lib/stores/file.js.map +1 -1
- package/lib/stores/file.spec.d.ts +2 -2
- package/lib/stores/file.spec.js +261 -289
- package/lib/stores/file.spec.js.map +1 -1
- package/lib/stores/mapper.d.ts +4 -4
- package/lib/stores/mapper.js +112 -137
- package/lib/stores/mapper.js.map +1 -1
- package/lib/stores/mapper.spec.js +131 -150
- package/lib/stores/mapper.spec.js.map +1 -1
- package/lib/stores/memory.d.ts +67 -6
- package/lib/stores/memory.js +155 -117
- package/lib/stores/memory.js.map +1 -1
- package/lib/stores/memory.spec.d.ts +3 -2
- package/lib/stores/memory.spec.js +127 -105
- package/lib/stores/memory.spec.js.map +1 -1
- package/lib/stores/store.d.ts +35 -15
- package/lib/stores/store.js +839 -821
- package/lib/stores/store.js.map +1 -1
- package/lib/stores/store.spec.d.ts +6 -4
- package/lib/stores/store.spec.js +627 -647
- package/lib/stores/store.spec.js.map +1 -1
- package/lib/stores/webdaql/WebdaQLLexer.d.ts +4 -4
- package/lib/stores/webdaql/WebdaQLLexer.js +9 -13
- package/lib/stores/webdaql/WebdaQLLexer.js.map +1 -1
- package/lib/stores/webdaql/WebdaQLParserLexer.d.ts +4 -4
- package/lib/stores/webdaql/WebdaQLParserLexer.js +9 -13
- package/lib/stores/webdaql/WebdaQLParserLexer.js.map +1 -1
- package/lib/stores/webdaql/WebdaQLParserListener.d.ts +2 -2
- package/lib/stores/webdaql/WebdaQLParserListener.js +1 -2
- package/lib/stores/webdaql/WebdaQLParserListener.js.map +1 -1
- package/lib/stores/webdaql/WebdaQLParserParser.d.ts +10 -10
- package/lib/stores/webdaql/WebdaQLParserParser.js +62 -91
- package/lib/stores/webdaql/WebdaQLParserParser.js.map +1 -1
- package/lib/stores/webdaql/WebdaQLParserVisitor.d.ts +2 -2
- package/lib/stores/webdaql/WebdaQLParserVisitor.js +1 -2
- package/lib/stores/webdaql/WebdaQLParserVisitor.js.map +1 -1
- package/lib/stores/webdaql/query.d.ts +4 -5
- package/lib/stores/webdaql/query.js +16 -19
- package/lib/stores/webdaql/query.js.map +1 -1
- package/lib/stores/webdaql/query.spec.js +30 -32
- package/lib/stores/webdaql/query.spec.js.map +1 -1
- package/lib/test.d.ts +3 -3
- package/lib/test.js +74 -94
- package/lib/test.js.map +1 -1
- package/lib/unpackedapplication.d.ts +1 -1
- package/lib/unpackedapplication.js +25 -31
- package/lib/unpackedapplication.js.map +1 -1
- package/lib/utils/abstractdeployer.js +1 -5
- package/lib/utils/abstractdeployer.js.map +1 -1
- package/lib/utils/abstractdeployer.spec.js +6 -8
- package/lib/utils/abstractdeployer.spec.js.map +1 -1
- package/lib/utils/context.d.ts +61 -29
- package/lib/utils/context.js +133 -127
- package/lib/utils/context.js.map +1 -1
- package/lib/utils/context.spec.js +147 -153
- package/lib/utils/context.spec.js.map +1 -1
- package/lib/utils/cookie.d.ts +66 -45
- package/lib/utils/cookie.js +69 -158
- package/lib/utils/cookie.js.map +1 -1
- package/lib/utils/cookie.spec.js +220 -238
- package/lib/utils/cookie.spec.js.map +1 -1
- package/lib/utils/esm.d.ts +4 -0
- package/lib/utils/esm.js +10 -0
- package/lib/utils/esm.js.map +1 -0
- package/lib/utils/httpcontext.d.ts +37 -9
- package/lib/utils/httpcontext.js +86 -57
- package/lib/utils/httpcontext.js.map +1 -1
- package/lib/utils/httpcontext.spec.js +37 -48
- package/lib/utils/httpcontext.spec.js.map +1 -1
- package/lib/utils/logger.d.ts +2 -2
- package/lib/utils/logger.js +15 -28
- package/lib/utils/logger.js.map +1 -1
- package/lib/utils/logger.spec.js +9 -11
- package/lib/utils/logger.spec.js.map +1 -1
- package/lib/utils/serializers.js +17 -20
- package/lib/utils/serializers.js.map +1 -1
- package/lib/utils/serializers.spec.js +43 -44
- package/lib/utils/serializers.spec.js.map +1 -1
- package/lib/utils/session.d.ts +120 -0
- package/lib/utils/session.js +155 -0
- package/lib/utils/session.js.map +1 -0
- package/lib/utils/session.spec.d.ts +1 -0
- package/lib/utils/session.spec.js +66 -0
- package/lib/utils/session.spec.js.map +1 -0
- package/lib/utils/waiter.d.ts +1 -1
- package/lib/utils/waiter.js +32 -52
- package/lib/utils/waiter.js.map +1 -1
- package/lib/utils/waiter.spec.js +75 -92
- package/lib/utils/waiter.spec.js.map +1 -1
- package/package.json +22 -14
- package/test/config-invitation.json +1 -3
- package/test/config.json +3 -3
- package/test/config.old.json +1 -2
- package/test/moddas/annotatedservice.js +5 -5
- package/test/moddas/debugmailer.js +2 -3
- package/test/moddas/fakeservice.js +2 -3
- package/test/moddas/voidstore.js +6 -4
- package/test/models/ident.js +2 -6
- package/test/models/task.js +3 -7
- package/test/package.json +2 -1
- package/webda.module.json +956 -188
package/lib/core.js
CHANGED
|
@@ -1,31 +1,34 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
1
|
+
import Ajv from "ajv";
|
|
2
|
+
import addFormats from "ajv-formats";
|
|
3
|
+
import { deepmerge } from "deepmerge-ts";
|
|
4
|
+
import * as events from "events";
|
|
5
|
+
import jsonpath from "jsonpath";
|
|
6
|
+
import pkg from "node-machine-id";
|
|
7
|
+
import { v4 as uuidv4 } from "uuid";
|
|
8
|
+
import { Context, Logger, Store } from "./index.js";
|
|
9
|
+
import { CoreModel } from "./models/coremodel.js";
|
|
10
|
+
import { Router } from "./router.js";
|
|
11
|
+
const { machineIdSync } = pkg;
|
|
12
|
+
/**
|
|
13
|
+
* Copy from https://github.com/ajv-validator/ajv/blob/master/lib/runtime/validation_error.ts
|
|
14
|
+
* It is not exported by ajv
|
|
15
|
+
*/
|
|
16
|
+
export class ValidationError extends Error {
|
|
17
|
+
constructor(errors) {
|
|
18
|
+
super("validation failed");
|
|
19
|
+
this.errors = errors;
|
|
20
|
+
this.ajv = this.validation = true;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
*
|
|
25
|
+
*/
|
|
26
|
+
export class RegistryEntry extends CoreModel {
|
|
27
|
+
}
|
|
25
28
|
/**
|
|
26
29
|
* Error with a code
|
|
27
30
|
*/
|
|
28
|
-
class WebdaError extends Error {
|
|
31
|
+
export class WebdaError extends Error {
|
|
29
32
|
/**
|
|
30
33
|
*
|
|
31
34
|
* @param code
|
|
@@ -42,13 +45,12 @@ class WebdaError extends Error {
|
|
|
42
45
|
return this.code;
|
|
43
46
|
}
|
|
44
47
|
}
|
|
45
|
-
exports.WebdaError = WebdaError;
|
|
46
48
|
/**
|
|
47
49
|
* Filter request based on their origin
|
|
48
50
|
*
|
|
49
51
|
* @category CoreFeatures
|
|
50
52
|
*/
|
|
51
|
-
class OriginFilter {
|
|
53
|
+
export class OriginFilter {
|
|
52
54
|
constructor(origins) {
|
|
53
55
|
this.regexs = origins.map(origin => {
|
|
54
56
|
if (!origin.endsWith("$")) {
|
|
@@ -65,26 +67,23 @@ class OriginFilter {
|
|
|
65
67
|
* @param context
|
|
66
68
|
* @returns
|
|
67
69
|
*/
|
|
68
|
-
checkRequest(context) {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
return true;
|
|
74
|
-
}
|
|
75
|
-
if (httpContext.origin.match(regexp)) {
|
|
76
|
-
return true;
|
|
77
|
-
}
|
|
70
|
+
async checkRequest(context) {
|
|
71
|
+
let httpContext = context.getHttpContext();
|
|
72
|
+
for (let regexp of this.regexs) {
|
|
73
|
+
if (httpContext.hostname.match(regexp)) {
|
|
74
|
+
return true;
|
|
78
75
|
}
|
|
79
|
-
|
|
80
|
-
|
|
76
|
+
if (httpContext.origin.match(regexp)) {
|
|
77
|
+
return true;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return false;
|
|
81
81
|
}
|
|
82
82
|
}
|
|
83
|
-
exports.OriginFilter = OriginFilter;
|
|
84
83
|
/**
|
|
85
84
|
* Authorize requests based on the website
|
|
86
85
|
*/
|
|
87
|
-
class WebsiteOriginFilter {
|
|
86
|
+
export class WebsiteOriginFilter {
|
|
88
87
|
constructor(website) {
|
|
89
88
|
this.websites = [];
|
|
90
89
|
if (!Array.isArray(website)) {
|
|
@@ -99,58 +98,35 @@ class WebsiteOriginFilter {
|
|
|
99
98
|
this.websites = [...website];
|
|
100
99
|
}
|
|
101
100
|
}
|
|
102
|
-
checkRequest(context) {
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
return false;
|
|
111
|
-
});
|
|
101
|
+
async checkRequest(context) {
|
|
102
|
+
let httpContext = context.getHttpContext();
|
|
103
|
+
if (this.websites.indexOf(httpContext.origin) >= 0 ||
|
|
104
|
+
this.websites.indexOf(httpContext.host) >= 0 ||
|
|
105
|
+
this.websites.indexOf("*") >= 0) {
|
|
106
|
+
return true;
|
|
107
|
+
}
|
|
108
|
+
return false;
|
|
112
109
|
}
|
|
113
110
|
}
|
|
114
|
-
exports.WebsiteOriginFilter = WebsiteOriginFilter;
|
|
115
111
|
const beans = {};
|
|
116
112
|
// @Bean to declare as a Singleton service
|
|
117
|
-
function Bean(constructor) {
|
|
113
|
+
export function Bean(constructor) {
|
|
118
114
|
let name = constructor.name.toLowerCase();
|
|
119
115
|
beans[name] = beans[name] || { constructor };
|
|
120
|
-
beans[name] =
|
|
121
|
-
}
|
|
122
|
-
exports.Bean = Bean;
|
|
123
|
-
// @Route to declare route on Bean
|
|
124
|
-
function Route(route, methods = ["GET"], allowPath = false, openapi = {}) {
|
|
125
|
-
return function (target, executor) {
|
|
126
|
-
let targetName = target.constructor.name.toLowerCase();
|
|
127
|
-
beans[targetName] = beans[targetName] || {
|
|
128
|
-
constructor: target.constructor
|
|
129
|
-
};
|
|
130
|
-
beans[targetName].routes = beans[targetName].routes || {};
|
|
131
|
-
beans[targetName].routes[route] = beans[targetName].routes[route] || [];
|
|
132
|
-
beans[targetName].routes[route].push({
|
|
133
|
-
methods: Array.isArray(methods) ? methods : [methods],
|
|
134
|
-
executor,
|
|
135
|
-
allowPath,
|
|
136
|
-
openapi
|
|
137
|
-
});
|
|
138
|
-
};
|
|
116
|
+
beans[name] = { ...beans[name], bean: true };
|
|
139
117
|
}
|
|
140
|
-
exports.Route = Route;
|
|
141
118
|
/**
|
|
142
119
|
* This is the main class of the framework, it handles the routing, the services initialization and resolution
|
|
143
120
|
*
|
|
144
121
|
* @class Core
|
|
145
122
|
* @category CoreFeatures
|
|
146
123
|
*/
|
|
147
|
-
class Core extends events.EventEmitter {
|
|
124
|
+
export class Core extends events.EventEmitter {
|
|
148
125
|
/**
|
|
149
126
|
* @params {Object} config - The configuration Object, if undefined will load the configuration file
|
|
150
127
|
*/
|
|
151
128
|
constructor(application) {
|
|
152
129
|
var _a, _b, _c;
|
|
153
|
-
var _d, _e, _f;
|
|
154
130
|
/** @ignore */
|
|
155
131
|
super();
|
|
156
132
|
/**
|
|
@@ -158,8 +134,17 @@ class Core extends events.EventEmitter {
|
|
|
158
134
|
* @hidden
|
|
159
135
|
*/
|
|
160
136
|
this.services = {};
|
|
161
|
-
|
|
137
|
+
/**
|
|
138
|
+
* Router that will route http request in
|
|
139
|
+
*/
|
|
140
|
+
this.router = new Router(this);
|
|
141
|
+
/**
|
|
142
|
+
* If Core is already initiated
|
|
143
|
+
*/
|
|
162
144
|
this._initiated = false;
|
|
145
|
+
/**
|
|
146
|
+
* Services who failed to create or initialize
|
|
147
|
+
*/
|
|
163
148
|
this.failedServices = {};
|
|
164
149
|
/**
|
|
165
150
|
* Request Filter registry
|
|
@@ -176,21 +161,21 @@ class Core extends events.EventEmitter {
|
|
|
176
161
|
*/
|
|
177
162
|
this._requestCORSFilters = [];
|
|
178
163
|
this.workerOutput = application.getWorkerOutput();
|
|
179
|
-
this.logger = new
|
|
164
|
+
this.logger = new Logger(this.workerOutput, "@webda/core/lib/core.js");
|
|
180
165
|
this.application = application;
|
|
181
166
|
this._initTime = new Date().getTime();
|
|
182
167
|
// Schema validations
|
|
183
|
-
this._ajv = new
|
|
184
|
-
(
|
|
168
|
+
this._ajv = new Ajv();
|
|
169
|
+
addFormats(this._ajv);
|
|
185
170
|
this._ajvSchemas = {};
|
|
186
171
|
// Load the configuration and migrate
|
|
187
172
|
this.configuration = this.application.getCurrentConfiguration();
|
|
188
173
|
// Set the global context
|
|
189
|
-
|
|
174
|
+
Context.setGlobalContext(new (this.getModel(this.parameter("contextModel") || "Webda/Context"))(this));
|
|
190
175
|
// Init default values for configuration
|
|
191
|
-
(_a =
|
|
192
|
-
(_b =
|
|
193
|
-
(_c =
|
|
176
|
+
(_a = this.configuration).parameters ?? (_a.parameters = {});
|
|
177
|
+
(_b = this.configuration.parameters).apiUrl ?? (_b.apiUrl = "http://localhost:18080");
|
|
178
|
+
(_c = this.configuration).services ?? (_c.services = {});
|
|
194
179
|
// Add CSRF origins filtering
|
|
195
180
|
if (this.configuration.parameters.csrfOrigins) {
|
|
196
181
|
this.registerCORSFilter(new OriginFilter(this.configuration.parameters.csrfOrigins));
|
|
@@ -206,8 +191,7 @@ class Core extends events.EventEmitter {
|
|
|
206
191
|
* It is a random generated string
|
|
207
192
|
*/
|
|
208
193
|
getInstanceId() {
|
|
209
|
-
|
|
210
|
-
(_a = this.instanceId) !== null && _a !== void 0 ? _a : (this.instanceId = this.getUuid());
|
|
194
|
+
this.instanceId ?? (this.instanceId = this.getUuid());
|
|
211
195
|
return this.instanceId;
|
|
212
196
|
}
|
|
213
197
|
/**
|
|
@@ -231,21 +215,6 @@ class Core extends events.EventEmitter {
|
|
|
231
215
|
getAppPath(subpath = "") {
|
|
232
216
|
return this.application.getAppPath(subpath);
|
|
233
217
|
}
|
|
234
|
-
/**
|
|
235
|
-
* Reinit the @Route
|
|
236
|
-
*/
|
|
237
|
-
reinitResolvedRoutes() {
|
|
238
|
-
for (let i in beans) {
|
|
239
|
-
if (beans[i].routes) {
|
|
240
|
-
for (let j in beans[i].routes) {
|
|
241
|
-
beans[i].routes[j].forEach(r => {
|
|
242
|
-
r.resolved = false;
|
|
243
|
-
this.router.removeRoute(j, r.info);
|
|
244
|
-
});
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
218
|
/**
|
|
250
219
|
* Retrieve all detected modules definition
|
|
251
220
|
*/
|
|
@@ -268,76 +237,83 @@ class Core extends events.EventEmitter {
|
|
|
268
237
|
* Init one service
|
|
269
238
|
* @param service
|
|
270
239
|
*/
|
|
271
|
-
initService(service) {
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
240
|
+
async initService(service) {
|
|
241
|
+
try {
|
|
242
|
+
// TODO Define parralel initialization
|
|
243
|
+
this.log("TRACE", "Initializing service", service);
|
|
244
|
+
this.services[service]._initTime = Date.now();
|
|
245
|
+
await this.services[service].init();
|
|
246
|
+
}
|
|
247
|
+
catch (err) {
|
|
248
|
+
this.services[service]._initException = err;
|
|
249
|
+
this.failedServices[service] = { _initException: err };
|
|
250
|
+
this.log("ERROR", "Init service " + service + " failed: " + err.message);
|
|
251
|
+
this.log("TRACE", err.stack);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Get an object from the application based on its full uuid
|
|
256
|
+
* @param fullUuid
|
|
257
|
+
* @param partials
|
|
258
|
+
*/
|
|
259
|
+
async getModelObject(fullUuid, partials) {
|
|
260
|
+
return CoreModel.fromFullUuid(this, fullUuid, partials);
|
|
287
261
|
}
|
|
288
262
|
/**
|
|
289
263
|
* Init Webda
|
|
290
264
|
*
|
|
291
265
|
* It will resolve Services init method and autolink
|
|
292
266
|
*/
|
|
293
|
-
init() {
|
|
267
|
+
async init() {
|
|
294
268
|
var _a, _b;
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
this.configuration.
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
}
|
|
269
|
+
if (this._init) {
|
|
270
|
+
return this._init;
|
|
271
|
+
}
|
|
272
|
+
if (this.configuration.parameters.configurationService) {
|
|
273
|
+
try {
|
|
274
|
+
this.log("INFO", "Create and init ConfigurationService", this.configuration.parameters.configurationService);
|
|
275
|
+
// Create the configuration service
|
|
276
|
+
this.createService(this.configuration.services, this.configuration.parameters.configurationService);
|
|
277
|
+
let cfg = await this.getService(this.configuration.parameters.configurationService).initConfiguration();
|
|
278
|
+
if (cfg.webda) {
|
|
279
|
+
(_a = cfg.webda).parameters ?? (_a.parameters = {});
|
|
280
|
+
(_b = cfg.webda).services ?? (_b.services = {});
|
|
281
|
+
this.configuration.parameters = { ...this.configuration.parameters, ...cfg.webda.parameters };
|
|
282
|
+
for (let i in this.configuration.services) {
|
|
283
|
+
this.configuration.services[i] = {
|
|
284
|
+
...deepmerge(this.configuration.services[i], cfg.webda.services[i] || {}),
|
|
285
|
+
type: this.configuration.services[i].type
|
|
286
|
+
};
|
|
313
287
|
}
|
|
314
|
-
yield this.getService(this.configuration.parameters.configurationService).init();
|
|
315
|
-
}
|
|
316
|
-
catch (err) {
|
|
317
|
-
this.log("ERROR", "Cannot use ConfigurationService", this.configuration.parameters.configurationService, err);
|
|
318
|
-
this.services = {};
|
|
319
288
|
}
|
|
289
|
+
await this.getService(this.configuration.parameters.configurationService).init();
|
|
320
290
|
}
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
291
|
+
catch (err) {
|
|
292
|
+
this.log("ERROR", "Cannot use ConfigurationService", this.configuration.parameters.configurationService, err);
|
|
293
|
+
this.services = {};
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
// Init the other services
|
|
297
|
+
this.initStatics();
|
|
298
|
+
this.log("TRACE", "Create Webda init promise");
|
|
299
|
+
this._init = new Promise(async (resolve) => {
|
|
300
|
+
await this.initService("registry");
|
|
301
|
+
await this.initService("cryptoservice");
|
|
302
|
+
// Init services
|
|
303
|
+
let service;
|
|
304
|
+
let inits = [];
|
|
305
|
+
for (service in this.services) {
|
|
306
|
+
if (this.services[service].init !== undefined &&
|
|
307
|
+
!this.services[service]._createException &&
|
|
308
|
+
!this.services[service]._initTime) {
|
|
309
|
+
inits.push(this.initService(service));
|
|
334
310
|
}
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
return this._init;
|
|
311
|
+
}
|
|
312
|
+
await Promise.all(inits);
|
|
313
|
+
await this.emitSync("Webda.Init.Services", this.services);
|
|
314
|
+
resolve();
|
|
340
315
|
});
|
|
316
|
+
return this._init;
|
|
341
317
|
}
|
|
342
318
|
/**
|
|
343
319
|
* Register a request filtering
|
|
@@ -357,23 +333,6 @@ class Core extends events.EventEmitter {
|
|
|
357
333
|
registerCORSFilter(filter) {
|
|
358
334
|
this._requestCORSFilters.push(filter);
|
|
359
335
|
}
|
|
360
|
-
/**
|
|
361
|
-
*
|
|
362
|
-
* @param {Object} executor The executor to expose as executor
|
|
363
|
-
* @param {String} code to execute
|
|
364
|
-
*/
|
|
365
|
-
sandbox(code, context) {
|
|
366
|
-
context !== null && context !== void 0 ? context : (context = {
|
|
367
|
-
require: () => {
|
|
368
|
-
throw Error("not allowed");
|
|
369
|
-
}
|
|
370
|
-
});
|
|
371
|
-
context.module = {
|
|
372
|
-
exports: {}
|
|
373
|
-
};
|
|
374
|
-
vm.runInNewContext(code, context);
|
|
375
|
-
return context.module.exports();
|
|
376
|
-
}
|
|
377
336
|
/**
|
|
378
337
|
* Validate the object with schema
|
|
379
338
|
*
|
|
@@ -396,7 +355,7 @@ class Core extends events.EventEmitter {
|
|
|
396
355
|
if (this._ajv.validate(name, object)) {
|
|
397
356
|
return true;
|
|
398
357
|
}
|
|
399
|
-
throw new
|
|
358
|
+
throw new ValidationError(this._ajv.errors);
|
|
400
359
|
}
|
|
401
360
|
/**
|
|
402
361
|
* Return webda current version
|
|
@@ -433,7 +392,7 @@ class Core extends events.EventEmitter {
|
|
|
433
392
|
}
|
|
434
393
|
}
|
|
435
394
|
}
|
|
436
|
-
return new
|
|
395
|
+
return new Logger(this.workerOutput, className);
|
|
437
396
|
}
|
|
438
397
|
/**
|
|
439
398
|
* Add a route dynamicaly
|
|
@@ -497,7 +456,7 @@ class Core extends events.EventEmitter {
|
|
|
497
456
|
* @returns {{}}
|
|
498
457
|
*/
|
|
499
458
|
getStores() {
|
|
500
|
-
return this.getServicesOfType(
|
|
459
|
+
return this.getServicesOfType(Store);
|
|
501
460
|
}
|
|
502
461
|
/**
|
|
503
462
|
* Return a map of defined models
|
|
@@ -514,49 +473,17 @@ class Core extends events.EventEmitter {
|
|
|
514
473
|
getModel(name) {
|
|
515
474
|
return this.application.getModel(name);
|
|
516
475
|
}
|
|
517
|
-
/**
|
|
518
|
-
* This should return a "turning" secret with cache and a service to modify it every x mins
|
|
519
|
-
* WARNING The security is lower without this "turning" secret, you can still set the global.secret parameter
|
|
520
|
-
*
|
|
521
|
-
* Dont rely on this method, it will probably disapear to avoid secret leak
|
|
522
|
-
*
|
|
523
|
-
* @deprecated
|
|
524
|
-
* @returns {String} Current secret
|
|
525
|
-
*/
|
|
526
|
-
getSecret() {
|
|
527
|
-
// For now a static config file but should have a rolling service secret
|
|
528
|
-
return this.configuration.parameters.sessionSecret;
|
|
529
|
-
}
|
|
530
|
-
/**
|
|
531
|
-
* Retrieve a HMAC for a string
|
|
532
|
-
* @param data
|
|
533
|
-
* @returns
|
|
534
|
-
*/
|
|
535
|
-
getHmac(data) {
|
|
536
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
537
|
-
return crypto.createHmac("sha256", this.getSecret()).update(data).digest("hex");
|
|
538
|
-
});
|
|
539
|
-
}
|
|
540
|
-
/**
|
|
541
|
-
* Return a salt to use when doing digest
|
|
542
|
-
*
|
|
543
|
-
* @returns {String} Current salt
|
|
544
|
-
*/
|
|
545
|
-
getSalt() {
|
|
546
|
-
// For now a static config file but should have a rolling service secret
|
|
547
|
-
return this.configuration.parameters.salt;
|
|
548
|
-
}
|
|
549
476
|
/**
|
|
550
477
|
* Add to context information and executor based on the http context
|
|
551
478
|
*/
|
|
552
479
|
updateContextWithRoute(ctx) {
|
|
553
480
|
let http = ctx.getHttpContext();
|
|
554
481
|
// Check mapping
|
|
555
|
-
|
|
482
|
+
let route = this.router.getRouteFromUrl(ctx, http.getMethod(), http.getRelativeUri());
|
|
556
483
|
if (route === undefined) {
|
|
557
484
|
return false;
|
|
558
485
|
}
|
|
559
|
-
ctx.setRoute(
|
|
486
|
+
ctx.setRoute({ ...this.configuration, ...route });
|
|
560
487
|
ctx.setExecutor(this.getService(route.executor));
|
|
561
488
|
return true;
|
|
562
489
|
}
|
|
@@ -592,41 +519,37 @@ class Core extends events.EventEmitter {
|
|
|
592
519
|
* Reinit one service
|
|
593
520
|
* @param service
|
|
594
521
|
*/
|
|
595
|
-
reinitService(service) {
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
}
|
|
606
|
-
});
|
|
522
|
+
async reinitService(service) {
|
|
523
|
+
try {
|
|
524
|
+
this.log("TRACE", "Re-Initializing service", service);
|
|
525
|
+
let serviceBean = this.services[service];
|
|
526
|
+
await serviceBean.reinit(this.getServiceParams(serviceBean.getName()));
|
|
527
|
+
}
|
|
528
|
+
catch (err) {
|
|
529
|
+
this.log("ERROR", "Re-Init service " + service + " failed", err);
|
|
530
|
+
this.log("TRACE", err.stack);
|
|
531
|
+
}
|
|
607
532
|
}
|
|
608
533
|
/**
|
|
609
534
|
* Reinit all services with updated parameters
|
|
610
535
|
* @param updates
|
|
611
536
|
* @returns
|
|
612
537
|
*/
|
|
613
|
-
reinit(updates) {
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
yield Promise.all(inits);
|
|
629
|
-
});
|
|
538
|
+
async reinit(updates) {
|
|
539
|
+
let configuration = JSON.parse(JSON.stringify(this.configuration.services));
|
|
540
|
+
for (let service in updates) {
|
|
541
|
+
jsonpath.value(configuration, service, updates[service]);
|
|
542
|
+
}
|
|
543
|
+
if (JSON.stringify(Object.keys(configuration)) !== JSON.stringify(Object.keys(this.configuration.services))) {
|
|
544
|
+
this.log("ERROR", "Configuration update cannot modify services");
|
|
545
|
+
throw new WebdaError("REINIT_SERVICE_INJECTION", "Configuration is not designed to add dynamically services");
|
|
546
|
+
}
|
|
547
|
+
this.configuration.services = configuration;
|
|
548
|
+
let inits = [];
|
|
549
|
+
for (let service in this.services) {
|
|
550
|
+
inits.push(this.reinitService(service));
|
|
551
|
+
}
|
|
552
|
+
await Promise.all(inits);
|
|
630
553
|
}
|
|
631
554
|
/**
|
|
632
555
|
* Get a full resolved service parameter
|
|
@@ -636,21 +559,20 @@ class Core extends events.EventEmitter {
|
|
|
636
559
|
* @returns
|
|
637
560
|
*/
|
|
638
561
|
getServiceParams(service, configuration = { parameters: {}, services: {} }) {
|
|
639
|
-
var _a
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
(
|
|
643
|
-
|
|
644
|
-
const params = (0, deepmerge_ts_1.deepmerge)(this.configuration.parameters || {}, configuration.parameters || {}, this.configuration.services[service] || {}, configuration.services[service] || {});
|
|
562
|
+
var _a;
|
|
563
|
+
configuration.parameters ?? (configuration.parameters = {});
|
|
564
|
+
configuration.services ?? (configuration.services = {});
|
|
565
|
+
(_a = configuration.services)[service] ?? (_a[service] = {});
|
|
566
|
+
const params = deepmerge(this.configuration.parameters || {}, configuration.parameters || {}, this.configuration.services[service] || {}, configuration.services[service] || {});
|
|
645
567
|
delete params.require;
|
|
646
568
|
return params;
|
|
647
569
|
}
|
|
648
570
|
createService(services, service) {
|
|
649
|
-
|
|
571
|
+
let type = services[service].type;
|
|
650
572
|
if (type === undefined) {
|
|
651
573
|
type = service;
|
|
652
574
|
}
|
|
653
|
-
|
|
575
|
+
let serviceConstructor = undefined;
|
|
654
576
|
try {
|
|
655
577
|
serviceConstructor = this.application.getModda(type);
|
|
656
578
|
}
|
|
@@ -677,9 +599,6 @@ class Core extends events.EventEmitter {
|
|
|
677
599
|
const services = this.configuration.services;
|
|
678
600
|
this.log("DEBUG", beans);
|
|
679
601
|
for (let i in beans) {
|
|
680
|
-
if (!beans[i].bean) {
|
|
681
|
-
this.log("DEBUG", "Implicit @Bean due to a @Route", beans[i].constructor.name);
|
|
682
|
-
}
|
|
683
602
|
let name = beans[i].constructor.name;
|
|
684
603
|
if (!services[name]) {
|
|
685
604
|
services[name] = {};
|
|
@@ -711,6 +630,20 @@ class Core extends events.EventEmitter {
|
|
|
711
630
|
}
|
|
712
631
|
return methods;
|
|
713
632
|
}
|
|
633
|
+
/**
|
|
634
|
+
* A registry is a predefined store
|
|
635
|
+
* @returns
|
|
636
|
+
*/
|
|
637
|
+
getRegistry() {
|
|
638
|
+
return this.registry;
|
|
639
|
+
}
|
|
640
|
+
/**
|
|
641
|
+
* Return the crypto service
|
|
642
|
+
* @returns
|
|
643
|
+
*/
|
|
644
|
+
getCrypto() {
|
|
645
|
+
return this.cryptoService;
|
|
646
|
+
}
|
|
714
647
|
/**
|
|
715
648
|
* Auto connect services with setters
|
|
716
649
|
*/
|
|
@@ -747,38 +680,42 @@ class Core extends events.EventEmitter {
|
|
|
747
680
|
* Init services and Beans along with Routes
|
|
748
681
|
*/
|
|
749
682
|
initStatics() {
|
|
683
|
+
var _a, _b, _c;
|
|
684
|
+
// Init the registry
|
|
685
|
+
const autoRegistry = this.configuration.services["Registry"] === undefined;
|
|
686
|
+
(_a = this.configuration.services)["Registry"] ?? (_a["Registry"] = {
|
|
687
|
+
type: "webda/memorystore",
|
|
688
|
+
persistence: {
|
|
689
|
+
path: ".registry",
|
|
690
|
+
key: machineIdSync()
|
|
691
|
+
}
|
|
692
|
+
});
|
|
693
|
+
this.createService(this.configuration.services, "Registry");
|
|
694
|
+
this.registry = this.getService("Registry").resolve();
|
|
695
|
+
// Init the key service
|
|
696
|
+
(_b = this.configuration.services)["CryptoService"] ?? (_b["CryptoService"] = {
|
|
697
|
+
type: "webda/cryptoservice",
|
|
698
|
+
autoRotate: autoRegistry ? 30 : undefined,
|
|
699
|
+
autoCreate: true
|
|
700
|
+
});
|
|
701
|
+
this.createService(this.configuration.services, "CryptoService");
|
|
702
|
+
this.cryptoService = this.getService("CryptoService").resolve();
|
|
703
|
+
// Session Manager
|
|
704
|
+
(_c = this.configuration.services)["SessionManager"] ?? (_c["SessionManager"] = {
|
|
705
|
+
type: "webda/cookiesessionmanager"
|
|
706
|
+
});
|
|
750
707
|
if (this.configuration.services !== undefined) {
|
|
751
|
-
|
|
708
|
+
let excludes = ["registry", "cryptoservice"];
|
|
709
|
+
if (this.configuration.parameters.configurationService) {
|
|
710
|
+
excludes.push(this.configuration.parameters.configurationService.toLowerCase());
|
|
711
|
+
}
|
|
712
|
+
// Do not recreate the configuration services
|
|
713
|
+
this.createServices(excludes);
|
|
752
714
|
}
|
|
753
715
|
this.router.remapRoutes();
|
|
754
716
|
this._initiated = true;
|
|
755
717
|
this.emit("Webda.Init", this.configuration);
|
|
756
718
|
}
|
|
757
|
-
/**
|
|
758
|
-
* Init Routes declare by @Route annotation
|
|
759
|
-
*/
|
|
760
|
-
initBeanRoutes(serviceBean) {
|
|
761
|
-
let service = serviceBean.getName().toLowerCase();
|
|
762
|
-
if (beans[service] !== undefined && beans[service].routes) {
|
|
763
|
-
for (let j in beans[service].routes) {
|
|
764
|
-
this.log("TRACE", "Adding route", j, "for bean", service);
|
|
765
|
-
beans[service].routes[j].forEach(route => {
|
|
766
|
-
if (route.resolved) {
|
|
767
|
-
return;
|
|
768
|
-
}
|
|
769
|
-
route.info = {
|
|
770
|
-
methods: route.methods,
|
|
771
|
-
_method: this.services[service][route.executor],
|
|
772
|
-
allowPath: route.allowPath || false,
|
|
773
|
-
openapi: route.openapi,
|
|
774
|
-
executor: beans[service].constructor.name // Name of the service
|
|
775
|
-
};
|
|
776
|
-
this.addRoute(j, route.info);
|
|
777
|
-
route.resolved = true;
|
|
778
|
-
});
|
|
779
|
-
}
|
|
780
|
-
}
|
|
781
|
-
}
|
|
782
719
|
/**
|
|
783
720
|
* Create a new context for a request
|
|
784
721
|
*
|
|
@@ -787,15 +724,13 @@ class Core extends events.EventEmitter {
|
|
|
787
724
|
* @param stream - The request output stream if any
|
|
788
725
|
* @return A new context object to pass along
|
|
789
726
|
*/
|
|
790
|
-
newContext(httpContext, stream = undefined, noInit = false) {
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
return res;
|
|
798
|
-
});
|
|
727
|
+
async newContext(httpContext, stream = undefined, noInit = false) {
|
|
728
|
+
let res = (new (this.getModel(this.parameter("contextModel") || "Webda/Context"))(this, httpContext, stream));
|
|
729
|
+
if (!noInit) {
|
|
730
|
+
await res.init();
|
|
731
|
+
}
|
|
732
|
+
await this.emitSync("Webda.NewContext", res);
|
|
733
|
+
return res;
|
|
799
734
|
}
|
|
800
735
|
/**
|
|
801
736
|
* Convert an object to JSON using the Webda json filter
|
|
@@ -815,10 +750,10 @@ class Core extends events.EventEmitter {
|
|
|
815
750
|
*/
|
|
816
751
|
getUuid(format = "uuid") {
|
|
817
752
|
if (format === "uuid") {
|
|
818
|
-
return (
|
|
753
|
+
return uuidv4().toString();
|
|
819
754
|
}
|
|
820
755
|
let buffer = Buffer.alloc(16);
|
|
821
|
-
(
|
|
756
|
+
uuidv4(undefined, buffer);
|
|
822
757
|
if (format === "base64") {
|
|
823
758
|
// Remove useless = we won't transfer back to original value or could just add ==
|
|
824
759
|
// https://datatracker.ietf.org/doc/html/rfc4648#page-7
|
|
@@ -836,9 +771,9 @@ class Core extends events.EventEmitter {
|
|
|
836
771
|
* Emit the event with data and wait for Promise to finish if listener returned a Promise
|
|
837
772
|
*/
|
|
838
773
|
emitSync(eventType, event, ...data) {
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
774
|
+
let result;
|
|
775
|
+
let promises = [];
|
|
776
|
+
let listeners = this.listeners(eventType);
|
|
842
777
|
for (let listener of listeners) {
|
|
843
778
|
result = listener(event, ...data);
|
|
844
779
|
if (result instanceof Promise) {
|
|
@@ -877,28 +812,23 @@ class Core extends events.EventEmitter {
|
|
|
877
812
|
*
|
|
878
813
|
* @param context Context of the request
|
|
879
814
|
*/
|
|
880
|
-
checkRequest(ctx) {
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
return (yield Promise.all(this._requestFilters.map(filter => filter.checkRequest(ctx)))).some(v => v);
|
|
887
|
-
});
|
|
815
|
+
async checkRequest(ctx) {
|
|
816
|
+
// Do not need to filter on OPTIONS as CORS is for that
|
|
817
|
+
if (ctx.getHttpContext().getMethod() === "OPTIONS") {
|
|
818
|
+
return true;
|
|
819
|
+
}
|
|
820
|
+
return (await Promise.all(this._requestFilters.map(filter => filter.checkRequest(ctx)))).some(v => v);
|
|
888
821
|
}
|
|
889
822
|
/**
|
|
890
823
|
* Verify if an origin is allowed to do request on the API
|
|
891
824
|
*
|
|
892
825
|
* @param context Context of the request
|
|
893
826
|
*/
|
|
894
|
-
checkCORSRequest(ctx) {
|
|
895
|
-
return
|
|
896
|
-
return (yield Promise.all(this._requestCORSFilters.map(filter => filter.checkRequest(ctx)))).some(v => v);
|
|
897
|
-
});
|
|
827
|
+
async checkCORSRequest(ctx) {
|
|
828
|
+
return (await Promise.all(this._requestCORSFilters.map(filter => filter.checkRequest(ctx)))).some(v => v);
|
|
898
829
|
}
|
|
899
830
|
exportOpenAPI(skipHidden = true) {
|
|
900
831
|
var _a;
|
|
901
|
-
var _b;
|
|
902
832
|
let packageInfo = this.application.getPackageDescription();
|
|
903
833
|
let contact;
|
|
904
834
|
if (typeof packageInfo.author === "string") {
|
|
@@ -918,7 +848,7 @@ class Core extends events.EventEmitter {
|
|
|
918
848
|
else if (packageInfo.license) {
|
|
919
849
|
license = packageInfo.license;
|
|
920
850
|
}
|
|
921
|
-
let openapi =
|
|
851
|
+
let openapi = deepmerge({
|
|
922
852
|
openapi: "3.0.3",
|
|
923
853
|
info: {
|
|
924
854
|
description: packageInfo.description,
|
|
@@ -928,11 +858,11 @@ class Core extends events.EventEmitter {
|
|
|
928
858
|
contact,
|
|
929
859
|
license
|
|
930
860
|
},
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
861
|
+
components: {
|
|
862
|
+
schemas: {
|
|
863
|
+
Object: {
|
|
864
|
+
type: "object"
|
|
865
|
+
}
|
|
936
866
|
}
|
|
937
867
|
},
|
|
938
868
|
paths: {},
|
|
@@ -945,21 +875,30 @@ class Core extends events.EventEmitter {
|
|
|
945
875
|
type: "object"
|
|
946
876
|
};
|
|
947
877
|
let modelDescription = this.getModel(i);
|
|
878
|
+
let modelName = model.name || i.split("/").pop();
|
|
948
879
|
// Only export CoreModel info
|
|
949
|
-
if (!this.application.extends(modelDescription,
|
|
880
|
+
if (!this.application.extends(modelDescription, CoreModel)) {
|
|
950
881
|
continue;
|
|
951
882
|
}
|
|
952
883
|
let schema = this.application.getSchema(i);
|
|
953
884
|
if (schema) {
|
|
954
885
|
for (let j in schema.definitions) {
|
|
955
886
|
// @ts-ignore
|
|
956
|
-
(_a =
|
|
887
|
+
(_a = openapi.components.schemas)[j] ?? (_a[j] = schema.definitions[j]);
|
|
957
888
|
}
|
|
958
889
|
delete schema.definitions;
|
|
959
890
|
desc = schema;
|
|
960
891
|
}
|
|
961
|
-
//
|
|
962
|
-
|
|
892
|
+
// Remove empty required as openapi does not like that
|
|
893
|
+
// Our compiler is not generating this anymore but it is additional protection
|
|
894
|
+
/* c8 ignore next 3 */
|
|
895
|
+
if (desc.required && desc.required.length === 0) {
|
|
896
|
+
delete desc.required;
|
|
897
|
+
}
|
|
898
|
+
// Remove $schema
|
|
899
|
+
delete desc.$schema;
|
|
900
|
+
// Rename all #/definitions/ by #/components/schemas/
|
|
901
|
+
openapi.components.schemas[modelName] = JSON.parse(JSON.stringify(desc).replace(/#\/definitions\//g, "#/components/schemas/"));
|
|
963
902
|
}
|
|
964
903
|
this.router.completeOpenAPI(openapi, skipHidden);
|
|
965
904
|
openapi.tags.sort((a, b) => a.name.toLowerCase().localeCompare(b.name.toLowerCase()));
|
|
@@ -971,5 +910,4 @@ class Core extends events.EventEmitter {
|
|
|
971
910
|
return openapi;
|
|
972
911
|
}
|
|
973
912
|
}
|
|
974
|
-
exports.Core = Core;
|
|
975
913
|
//# sourceMappingURL=core.js.map
|