vortez 5.0.0-dev.18 → 5.0.0-dev.19
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/.gitignore +9 -4
- package/README.md +681 -176
- package/build/Vortez.d.ts +1 -0
- package/build/Vortez.js +1 -0
- package/build/Vortez.js.map +1 -1
- package/build/server/Response.d.ts +1 -1
- package/build/server/Response.js +1 -1
- package/build/server/Response.js.map +1 -1
- package/build/server/Server.d.ts +4 -4
- package/build/server/Server.js +5 -5
- package/build/server/Server.js.map +1 -1
- package/build/server/ServerDebug.d.ts +10 -1
- package/build/server/ServerDebug.js +85 -17
- package/build/server/ServerDebug.js.map +1 -1
- package/build/server/config/Config.d.ts +274 -47
- package/build/server/config/Config.js +68 -47
- package/build/server/config/Config.js.map +1 -1
- package/build/server/config/{ConfigLoader.d.ts → Loader.d.ts} +4 -5
- package/build/server/config/{ConfigLoader.js → Loader.js} +7 -10
- package/build/server/config/Loader.js.map +1 -0
- package/build/server/router/Router.d.ts +87 -30
- package/build/server/router/Router.js +110 -48
- package/build/server/router/Router.js.map +1 -1
- package/build/server/router/algorithm/Algorithm.d.ts +39 -0
- package/build/server/router/algorithm/Algorithm.js +20 -0
- package/build/server/router/algorithm/Algorithm.js.map +1 -0
- package/build/server/router/algorithm/FIFO.d.ts +15 -0
- package/build/server/router/algorithm/FIFO.js +24 -0
- package/build/server/router/algorithm/FIFO.js.map +1 -0
- package/build/server/router/algorithm/Tree.d.ts +38 -0
- package/build/server/router/algorithm/Tree.js +126 -0
- package/build/server/router/algorithm/Tree.js.map +1 -0
- package/build/server/router/middleware/WsMiddleware.js +1 -1
- package/build/server/router/middleware/WsMiddleware.js.map +1 -1
- package/build/utilities/Flatten.d.ts +56 -0
- package/build/utilities/Flatten.js +59 -0
- package/build/utilities/Flatten.js.map +1 -0
- package/build/utilities/Utilities.d.ts +7 -58
- package/build/utilities/Utilities.js +8 -33
- package/build/utilities/Utilities.js.map +1 -1
- package/build/utilities/schema/Introspection.d.ts +24 -0
- package/build/utilities/schema/Introspection.js +87 -0
- package/build/utilities/schema/Introspection.js.map +1 -0
- package/build/utilities/schema/JSONSchema.d.ts +68 -0
- package/build/utilities/schema/JSONSchema.js +13 -0
- package/build/utilities/schema/JSONSchema.js.map +1 -0
- package/build/utilities/schema/Schema.d.ts +253 -0
- package/build/utilities/schema/Schema.js +241 -0
- package/build/utilities/schema/Schema.js.map +1 -0
- package/build/utilities/schema/SchemaError.d.ts +10 -0
- package/build/utilities/schema/SchemaError.js +13 -0
- package/build/utilities/schema/SchemaError.js.map +1 -0
- package/build/utilities/schema/Validator.d.ts +94 -0
- package/build/utilities/schema/Validator.js +246 -0
- package/build/utilities/schema/Validator.js.map +1 -0
- package/package.json +1 -1
- package/tests/config/config.js +233 -0
- package/tests/router.js +596 -0
- package/tests/schema/schema.js +368 -0
- package/tests/test.env +0 -0
- package/tests/test.js +3 -3
- package/build/server/config/ConfigLoader.js.map +0 -1
- package/build/server/config/ConfigValidator.d.ts +0 -71
- package/build/server/config/ConfigValidator.js +0 -131
- package/build/server/config/ConfigValidator.js.map +0 -1
- package/examples/in-docs.js +0 -96
|
@@ -0,0 +1,368 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
import { strict as assert } from 'assert';
|
|
3
|
+
|
|
4
|
+
import { Logger } from '../../build/Vortez.js';
|
|
5
|
+
import { Schema } from '../../build/utilities/schema/Schema.js';
|
|
6
|
+
import { SchemaError } from '../../build/utilities/schema/SchemaError.js';
|
|
7
|
+
|
|
8
|
+
const logger = new Logger({ prefix: 'SCHEMA' });
|
|
9
|
+
|
|
10
|
+
let testsPassed = 0;
|
|
11
|
+
let testsFailed = 0;
|
|
12
|
+
|
|
13
|
+
const x = new Schema({
|
|
14
|
+
host: { type: 'string', required: true, minLength: 1 },
|
|
15
|
+
port: { type: 'number', minimum: 0, maximum: 65535, default: 80 },
|
|
16
|
+
ssl: {
|
|
17
|
+
type: 'object',
|
|
18
|
+
nullable: true,
|
|
19
|
+
default: null,
|
|
20
|
+
schema: {
|
|
21
|
+
pubKey: { type: 'string', required: true },
|
|
22
|
+
privKey: { type: 'string', required: true }
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
const y = x.processData({ host: 'localhost' });
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Logs test results.
|
|
30
|
+
* @param { string } testName The test name.
|
|
31
|
+
* @param { boolean } passed Whether the test passed.
|
|
32
|
+
* @param { unknown | null } error Optional error object for failures.
|
|
33
|
+
*/
|
|
34
|
+
function logTestResult(testName, passed, error = null) {
|
|
35
|
+
if (passed) {
|
|
36
|
+
logger.log(`&C2✓ ${testName}`);
|
|
37
|
+
testsPassed++;
|
|
38
|
+
} else {
|
|
39
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
40
|
+
logger.error(`&C1✗ ${testName}${error ? ': ' + message : ''}`);
|
|
41
|
+
testsFailed++;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Ensures processData applies defaults and keeps valid values.
|
|
47
|
+
*/
|
|
48
|
+
function testProcessDataWithDefaults() {
|
|
49
|
+
try {
|
|
50
|
+
const schema = new Schema({
|
|
51
|
+
host: { type: 'string', required: true, minLength: 1 },
|
|
52
|
+
port: { type: 'number', minimum: 0, maximum: 65535, default: 80 },
|
|
53
|
+
ssl: {
|
|
54
|
+
type: 'object',
|
|
55
|
+
nullable: true,
|
|
56
|
+
default: null,
|
|
57
|
+
schema: {
|
|
58
|
+
pubKey: { type: 'string', required: true },
|
|
59
|
+
privKey: { type: 'string', required: true }
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
const data = schema.processData({ host: 'localhost' });
|
|
65
|
+
assert.equal(data.host, 'localhost');
|
|
66
|
+
assert.equal(data.port, 80);
|
|
67
|
+
assert.equal(data.ssl, null);
|
|
68
|
+
|
|
69
|
+
logTestResult('processData - Applies defaults', true);
|
|
70
|
+
} catch (error) {
|
|
71
|
+
logTestResult('processData - Applies defaults', false, error);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Ensures required keys fail when not provided.
|
|
77
|
+
*/
|
|
78
|
+
function testRequiredFieldFailure() {
|
|
79
|
+
try {
|
|
80
|
+
const schema = new Schema({
|
|
81
|
+
host: { type: 'string', required: true }
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
try {
|
|
85
|
+
// @ts-expect-error Runtime validation test with missing required key on purpose.
|
|
86
|
+
schema.processData({});
|
|
87
|
+
logTestResult('required - Missing required key fails', false, new Error('Should have thrown'));
|
|
88
|
+
} catch (error) {
|
|
89
|
+
assert.ok(error instanceof SchemaError);
|
|
90
|
+
logTestResult('required - Missing required key fails', true);
|
|
91
|
+
}
|
|
92
|
+
} catch (error) {
|
|
93
|
+
logTestResult('required - Missing required key fails', false, error);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Covers edge cases where numeric/string limits are 0.
|
|
99
|
+
*/
|
|
100
|
+
function testZeroBoundaries() {
|
|
101
|
+
try {
|
|
102
|
+
const stringSchema = new Schema({
|
|
103
|
+
emptyOnly: { type: 'string', required: true, maxLength: 0, minLength: 0 }
|
|
104
|
+
});
|
|
105
|
+
const numberSchema = new Schema({
|
|
106
|
+
count: { type: 'number', required: true, minimum: 0, maximum: 0 }
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
const validString = stringSchema.processData({ emptyOnly: '' });
|
|
110
|
+
const validNumber = numberSchema.processData({ count: 0 });
|
|
111
|
+
|
|
112
|
+
assert.equal(validString.emptyOnly, '');
|
|
113
|
+
assert.equal(validNumber.count, 0);
|
|
114
|
+
|
|
115
|
+
assert.throws(() => stringSchema.processData({ emptyOnly: 'x' }), SchemaError);
|
|
116
|
+
assert.throws(() => numberSchema.processData({ count: -1 }), SchemaError);
|
|
117
|
+
|
|
118
|
+
logTestResult('limits - Zero boundaries enforced', true);
|
|
119
|
+
} catch (error) {
|
|
120
|
+
logTestResult('limits - Zero boundaries enforced', false, error);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Ensures array size validation and element validation work together.
|
|
126
|
+
*/
|
|
127
|
+
function testArrayValidation() {
|
|
128
|
+
try {
|
|
129
|
+
const schema = new Schema({
|
|
130
|
+
tags: {
|
|
131
|
+
type: 'array',
|
|
132
|
+
required: true,
|
|
133
|
+
minimum: 0,
|
|
134
|
+
maximum: 2,
|
|
135
|
+
property: { type: 'string', minLength: 1 }
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
const valid = schema.processData({ tags: [] });
|
|
140
|
+
assert.deepEqual(valid.tags, []);
|
|
141
|
+
|
|
142
|
+
assert.throws(() => schema.processData({ tags: ['a', 'b', 'c'] }), SchemaError);
|
|
143
|
+
assert.throws(() => schema.processData({ tags: [''] }), SchemaError);
|
|
144
|
+
|
|
145
|
+
logTestResult('array - Limits and item validation', true);
|
|
146
|
+
} catch (error) {
|
|
147
|
+
logTestResult('array - Limits and item validation', false, error);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Ensures deep dot-path updates in processPartialData work.
|
|
153
|
+
*/
|
|
154
|
+
function testDeepPartialProcessing() {
|
|
155
|
+
try {
|
|
156
|
+
const schema = new Schema({
|
|
157
|
+
profile: {
|
|
158
|
+
type: 'object',
|
|
159
|
+
required: true,
|
|
160
|
+
schema: {
|
|
161
|
+
address: {
|
|
162
|
+
type: 'object',
|
|
163
|
+
required: true,
|
|
164
|
+
schema: {
|
|
165
|
+
zip: { type: 'number', required: true, minimum: 0 }
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
const partial = schema.processPartialData({ 'profile.address.zip': 1200 });
|
|
173
|
+
assert.equal(partial['profile.address.zip'], 1200);
|
|
174
|
+
|
|
175
|
+
assert.throws(() => schema.processPartialData({ 'profile.address.missing': 1 }), SchemaError);
|
|
176
|
+
|
|
177
|
+
logTestResult('partial - Deep object path validation', true);
|
|
178
|
+
} catch (error) {
|
|
179
|
+
logTestResult('partial - Deep object path validation', false, error);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Ensures processData in partial mode rejects unknown root keys.
|
|
185
|
+
*/
|
|
186
|
+
function testPartialUnknownRootKey() {
|
|
187
|
+
try {
|
|
188
|
+
const schema = new Schema({
|
|
189
|
+
host: { type: 'string', required: true }
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
assert.throws(
|
|
193
|
+
// @ts-expect-error Runtime validation test with unknown key on purpose.
|
|
194
|
+
() => schema.processData({ host: 'localhost', extra: 'x' }, true),
|
|
195
|
+
SchemaError
|
|
196
|
+
);
|
|
197
|
+
|
|
198
|
+
logTestResult('partial - Unknown root key rejected', true);
|
|
199
|
+
} catch (error) {
|
|
200
|
+
logTestResult('partial - Unknown root key rejected', false, error);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Ensures object properties reject arrays as invalid object values.
|
|
206
|
+
*/
|
|
207
|
+
function testObjectRejectsArrayValue() {
|
|
208
|
+
try {
|
|
209
|
+
const schema = new Schema({
|
|
210
|
+
profile: {
|
|
211
|
+
type: 'object',
|
|
212
|
+
required: true,
|
|
213
|
+
schema: {
|
|
214
|
+
zip: { type: 'number', required: true }
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
assert.throws(
|
|
220
|
+
// @ts-expect-error Runtime validation test with invalid object value on purpose.
|
|
221
|
+
() => schema.processData({ profile: [] }),
|
|
222
|
+
SchemaError
|
|
223
|
+
);
|
|
224
|
+
|
|
225
|
+
logTestResult('object - Arrays are rejected', true);
|
|
226
|
+
} catch (error) {
|
|
227
|
+
logTestResult('object - Arrays are rejected', false, error);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Ensures JSON Schema output maps arrays with items/minItems/maxItems.
|
|
233
|
+
*/
|
|
234
|
+
function testJsonSchemaArrayMapping() {
|
|
235
|
+
try {
|
|
236
|
+
const schema = new Schema({
|
|
237
|
+
tags: {
|
|
238
|
+
type: 'array',
|
|
239
|
+
minimum: 1,
|
|
240
|
+
maximum: 3,
|
|
241
|
+
property: { type: 'string', minLength: 2 }
|
|
242
|
+
}
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
const json = schema.jsonSchema;
|
|
246
|
+
const tags = json.properties?.tags;
|
|
247
|
+
const tagsItems = tags?.items;
|
|
248
|
+
|
|
249
|
+
assert.equal(tags?.type, 'array');
|
|
250
|
+
assert.equal(tags?.minItems, 1);
|
|
251
|
+
assert.equal(tags?.maxItems, 3);
|
|
252
|
+
assert.ok(tagsItems && !Array.isArray(tagsItems));
|
|
253
|
+
assert.equal(tagsItems.type, 'string');
|
|
254
|
+
assert.equal(tagsItems.minLength, 2);
|
|
255
|
+
|
|
256
|
+
logTestResult('jsonSchema - Array mapping', true);
|
|
257
|
+
} catch (error) {
|
|
258
|
+
logTestResult('jsonSchema - Array mapping', false, error);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Ensures string enum validation accepts only allowed values.
|
|
264
|
+
*/
|
|
265
|
+
function testStringEnumValidation() {
|
|
266
|
+
try {
|
|
267
|
+
const schema = new Schema({
|
|
268
|
+
mode: { type: 'string', required: true, enum: ['A', 'B'] }
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
const valid = schema.processData({ mode: 'A' });
|
|
272
|
+
assert.equal(valid.mode, 'A');
|
|
273
|
+
|
|
274
|
+
assert.throws(() => schema.processData({ mode: 'C' }), SchemaError);
|
|
275
|
+
|
|
276
|
+
logTestResult('string - Enum validation', true);
|
|
277
|
+
} catch (error) {
|
|
278
|
+
logTestResult('string - Enum validation', false, error);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* Ensures JSON Schema output contains enum for string properties.
|
|
284
|
+
*/
|
|
285
|
+
function testJsonSchemaStringEnumMapping() {
|
|
286
|
+
try {
|
|
287
|
+
const schema = new Schema({
|
|
288
|
+
mode: { type: 'string', enum: ['A', 'B'] }
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
const json = schema.jsonSchema;
|
|
292
|
+
const mode = json.properties?.mode;
|
|
293
|
+
|
|
294
|
+
assert.equal(mode?.type, 'string');
|
|
295
|
+
assert.deepEqual(mode?.enum, ['A', 'B']);
|
|
296
|
+
|
|
297
|
+
logTestResult('jsonSchema - String enum mapping', true);
|
|
298
|
+
} catch (error) {
|
|
299
|
+
logTestResult('jsonSchema - String enum mapping', false, error);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* Ensures nested unique keys are listed with dot notation.
|
|
305
|
+
*/
|
|
306
|
+
function testUniqueKeyCollection() {
|
|
307
|
+
try {
|
|
308
|
+
const schema = new Schema({
|
|
309
|
+
username: { type: 'string', unique: true },
|
|
310
|
+
profile: {
|
|
311
|
+
type: 'object',
|
|
312
|
+
schema: {
|
|
313
|
+
email: { type: 'string', unique: true },
|
|
314
|
+
country: { type: 'string' }
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
});
|
|
318
|
+
|
|
319
|
+
const uniques = schema.uniques.sort();
|
|
320
|
+
assert.deepEqual(uniques, ['profile.email', 'username']);
|
|
321
|
+
|
|
322
|
+
logTestResult('uniques - Nested keys collected', true);
|
|
323
|
+
} catch (error) {
|
|
324
|
+
logTestResult('uniques - Nested keys collected', false, error);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* Ensures invalid schema definitions fail at construction time.
|
|
330
|
+
*/
|
|
331
|
+
function testInvalidSchemaDefinition() {
|
|
332
|
+
try {
|
|
333
|
+
assert.throws(() => {
|
|
334
|
+
// @ts-ignore Runtime validation test with invalid shape on purpose.
|
|
335
|
+
new Schema({ name: { type: 'string', pattern: 'not-a-regexp' } });
|
|
336
|
+
}, SchemaError);
|
|
337
|
+
|
|
338
|
+
logTestResult('schema - Invalid definition rejected', true);
|
|
339
|
+
} catch (error) {
|
|
340
|
+
logTestResult('schema - Invalid definition rejected', false, error);
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
function runTests() {
|
|
345
|
+
logger.log('\n&C6=== Schema Test Suite ===\n');
|
|
346
|
+
|
|
347
|
+
testProcessDataWithDefaults();
|
|
348
|
+
testRequiredFieldFailure();
|
|
349
|
+
testZeroBoundaries();
|
|
350
|
+
testArrayValidation();
|
|
351
|
+
testDeepPartialProcessing();
|
|
352
|
+
testPartialUnknownRootKey();
|
|
353
|
+
testObjectRejectsArrayValue();
|
|
354
|
+
testJsonSchemaArrayMapping();
|
|
355
|
+
testStringEnumValidation();
|
|
356
|
+
testJsonSchemaStringEnumMapping();
|
|
357
|
+
testUniqueKeyCollection();
|
|
358
|
+
testInvalidSchemaDefinition();
|
|
359
|
+
|
|
360
|
+
logger.log('\n&C6=== Results ===');
|
|
361
|
+
logger.log(`&C2✓ Passed: ${testsPassed}`);
|
|
362
|
+
logger.log(`&C1✗ Failed: ${testsFailed}`);
|
|
363
|
+
logger.log(`&C3Total: ${testsPassed + testsFailed}\n`);
|
|
364
|
+
|
|
365
|
+
process.exit(testsFailed > 0 ? 1 : 0);
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
runTests();
|
package/tests/test.env
ADDED
|
File without changes
|
package/tests/test.js
CHANGED
|
@@ -15,7 +15,7 @@ await Utilities.Env.load('tests/test.env');
|
|
|
15
15
|
// Utilities.Env.loadSync('tests/test.env');
|
|
16
16
|
|
|
17
17
|
/* | CREATING SERVER | */
|
|
18
|
-
const server = new Vortez({ port:
|
|
18
|
+
const server = new Vortez({ port: 8001 });
|
|
19
19
|
|
|
20
20
|
/* | FILE RULE TEST | */
|
|
21
21
|
server.router.addFile('/File', 'changes.md')
|
|
@@ -62,7 +62,7 @@ server.router.addAction('ALL', '/RuleParams/$?param1/$?b/$?c/*', (Rq, Rs) => {
|
|
|
62
62
|
|
|
63
63
|
/* | WEBSOCKET RULE TEST | */
|
|
64
64
|
/**
|
|
65
|
-
* @typedef {import('../build/
|
|
65
|
+
* @typedef {import('../build/server/websocket/Websocket.js').Websocket} WebSocket
|
|
66
66
|
*/
|
|
67
67
|
/** @type {Set<WebSocket>} */
|
|
68
68
|
const clients = new Set();
|
|
@@ -86,7 +86,7 @@ server.router.addAction('ALL', 'WebSocket', (Rq, Rs) => {
|
|
|
86
86
|
});
|
|
87
87
|
});
|
|
88
88
|
|
|
89
|
-
server.router.
|
|
89
|
+
server.router.addWebsocket('/WebSocket/$?username', (request, socket) => {
|
|
90
90
|
const username = request.ruleParams.username;
|
|
91
91
|
if (!username) {
|
|
92
92
|
socket.send('error: no username');
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"ConfigLoader.js","sourceRoot":"","sources":["../../../src/server/config/ConfigLoader.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,QAAQ,IAAI,GAAG,EAAE,MAAM,IAAI,CAAC;AACrC,OAAO,MAAM,MAAM,wBAAwB,CAAC;AAC5C,OAAO,MAAM,MAAM,aAAa,CAAC;AACjC,OAAO,SAAS,MAAM,8BAA8B,CAAC;AACrD,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAEvD,MAAM,MAAM,GAAG,IAAI,MAAM,CAAC,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,CAAC;AAEhD,MAAM,OAAO,YAAY;IACrB;;;;;OAKG;IACI,MAAM,CAAC,KA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AC,SAAS,CAAC,IAAI,EAAE,OAAO,EAAE,MAAM,CAAC,CAAC;IAC/C,CAAC;CACJ;AAED,eAAe,YAAY,CAAC"}
|
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @author NetFeez <netfeez.dev@gmail.com>
|
|
3
|
-
* @description add the config validator to use in the config loader.
|
|
4
|
-
* @license Apache-2.0
|
|
5
|
-
*/
|
|
6
|
-
import Config from "./Config.js";
|
|
7
|
-
export declare class ConfigValidator {
|
|
8
|
-
/**
|
|
9
|
-
* Validates the config.
|
|
10
|
-
* @param config - The config to validate.
|
|
11
|
-
* @returns The validated config.
|
|
12
|
-
*/
|
|
13
|
-
static validate(config: Config.options): Config.Main;
|
|
14
|
-
/**
|
|
15
|
-
* Validates the host.
|
|
16
|
-
* @param host - The host to validate.
|
|
17
|
-
* @returns The validated host.
|
|
18
|
-
*/
|
|
19
|
-
static validateHost(host: unknown): string;
|
|
20
|
-
/**
|
|
21
|
-
* Validates the port.
|
|
22
|
-
* @param port - The port to validate.
|
|
23
|
-
* @returns The validated port.
|
|
24
|
-
*/
|
|
25
|
-
static validatePort(port: unknown): number;
|
|
26
|
-
/**
|
|
27
|
-
* Validates the templates.
|
|
28
|
-
* @param templates - The templates to validate.
|
|
29
|
-
* @returns The validated templates.
|
|
30
|
-
*/
|
|
31
|
-
static validateTemplates(templates: unknown): Config.Templates;
|
|
32
|
-
/**
|
|
33
|
-
* Validates the ssl.
|
|
34
|
-
* @param ssl - The ssl to validate.
|
|
35
|
-
* @returns The validated ssl.
|
|
36
|
-
*/
|
|
37
|
-
static validateSSL(ssl: unknown): Config.SSLOptions | null;
|
|
38
|
-
/**
|
|
39
|
-
* Validates a string value.
|
|
40
|
-
* @param value - The value to validate.
|
|
41
|
-
* @param defaultValue - The default value to use if the value is not a string.
|
|
42
|
-
* @param fieldName - The name of the field being validated.
|
|
43
|
-
* @returns The validated string value.
|
|
44
|
-
*/
|
|
45
|
-
private static validateString;
|
|
46
|
-
/**
|
|
47
|
-
* Validates a number value.
|
|
48
|
-
* @param value - The value to validate.
|
|
49
|
-
* @param defaultValue - The default value to use if the value is not a number.
|
|
50
|
-
* @param fieldName - The name of the field being validated.
|
|
51
|
-
* @returns The validated number value.
|
|
52
|
-
*/
|
|
53
|
-
private static validateNumber;
|
|
54
|
-
/**
|
|
55
|
-
* Validates a number value within a given range.
|
|
56
|
-
* @param value - The value to validate.
|
|
57
|
-
* @param min - The minimum value of the range.
|
|
58
|
-
* @param max - The maximum value of the range.
|
|
59
|
-
*/
|
|
60
|
-
private static validateRange;
|
|
61
|
-
/**
|
|
62
|
-
* Validates a number value within a given range.
|
|
63
|
-
* @param value - The value to validate.
|
|
64
|
-
* @param min - The minimum value of the range.
|
|
65
|
-
* @param max - The maximum value of the range.
|
|
66
|
-
* @param defaultValue - The default value to use if the value is not a number or is out of range.
|
|
67
|
-
*/
|
|
68
|
-
private static validateNumberInRange;
|
|
69
|
-
}
|
|
70
|
-
export declare namespace ConfigValidator { }
|
|
71
|
-
export default ConfigValidator;
|
|
@@ -1,131 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @author NetFeez <netfeez.dev@gmail.com>
|
|
3
|
-
* @description add the config validator to use in the config loader.
|
|
4
|
-
* @license Apache-2.0
|
|
5
|
-
*/
|
|
6
|
-
import Config from "./Config.js";
|
|
7
|
-
import Logger from "../../logger/Logger.js";
|
|
8
|
-
const logger = new Logger({ prefix: 'Config' });
|
|
9
|
-
export class ConfigValidator {
|
|
10
|
-
/**
|
|
11
|
-
* Validates the config.
|
|
12
|
-
* @param config - The config to validate.
|
|
13
|
-
* @returns The validated config.
|
|
14
|
-
*/
|
|
15
|
-
static validate(config) {
|
|
16
|
-
const { host, port, ssl, templates } = config;
|
|
17
|
-
return {
|
|
18
|
-
host: this.validateHost(host),
|
|
19
|
-
port: this.validatePort(port),
|
|
20
|
-
ssl: this.validateSSL(ssl),
|
|
21
|
-
templates: this.validateTemplates(templates)
|
|
22
|
-
};
|
|
23
|
-
}
|
|
24
|
-
/**
|
|
25
|
-
* Validates the host.
|
|
26
|
-
* @param host - The host to validate.
|
|
27
|
-
* @returns The validated host.
|
|
28
|
-
*/
|
|
29
|
-
static validateHost(host) {
|
|
30
|
-
return this.validateString(host, 'localhost', 'host');
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* Validates the port.
|
|
34
|
-
* @param port - The port to validate.
|
|
35
|
-
* @returns The validated port.
|
|
36
|
-
*/
|
|
37
|
-
static validatePort(port) {
|
|
38
|
-
return this.validateNumberInRange(port, 0, 65535, Config.DEFAULT.port, 'port');
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* Validates the templates.
|
|
42
|
-
* @param templates - The templates to validate.
|
|
43
|
-
* @returns The validated templates.
|
|
44
|
-
*/
|
|
45
|
-
static validateTemplates(templates) {
|
|
46
|
-
if (!templates || typeof templates !== 'object') {
|
|
47
|
-
logger.warn('templates must be an object, using defaults');
|
|
48
|
-
return Config.DEFAULT.templates;
|
|
49
|
-
}
|
|
50
|
-
const defTemplates = Config.DEFAULT.templates;
|
|
51
|
-
const templatesP = templates;
|
|
52
|
-
const result = {};
|
|
53
|
-
for (const key in defTemplates) {
|
|
54
|
-
result[key] = templatesP[key] != null ? templatesP[key] : defTemplates[key];
|
|
55
|
-
}
|
|
56
|
-
return result;
|
|
57
|
-
}
|
|
58
|
-
/**
|
|
59
|
-
* Validates the ssl.
|
|
60
|
-
* @param ssl - The ssl to validate.
|
|
61
|
-
* @returns The validated ssl.
|
|
62
|
-
*/
|
|
63
|
-
static validateSSL(ssl) {
|
|
64
|
-
if (!ssl || typeof ssl !== 'object') {
|
|
65
|
-
return null;
|
|
66
|
-
}
|
|
67
|
-
const sslP = ssl;
|
|
68
|
-
const { pubKey, privKey, port = 443 } = sslP;
|
|
69
|
-
if (typeof pubKey !== 'string') {
|
|
70
|
-
logger.warn('ssl.pubKey must be a string, skipping ssl');
|
|
71
|
-
return Config.DEFAULT.ssl;
|
|
72
|
-
}
|
|
73
|
-
if (typeof privKey !== 'string') {
|
|
74
|
-
logger.warn('ssl.privKey must be a string, skipping ssl');
|
|
75
|
-
return Config.DEFAULT.ssl;
|
|
76
|
-
}
|
|
77
|
-
const validPort = this.validateNumberInRange(port, 0, 65535, 443, 'ssl.port');
|
|
78
|
-
return { pubKey, privKey, port: validPort };
|
|
79
|
-
}
|
|
80
|
-
/**
|
|
81
|
-
* Validates a string value.
|
|
82
|
-
* @param value - The value to validate.
|
|
83
|
-
* @param defaultValue - The default value to use if the value is not a string.
|
|
84
|
-
* @param fieldName - The name of the field being validated.
|
|
85
|
-
* @returns The validated string value.
|
|
86
|
-
*/
|
|
87
|
-
static validateString(value, defaultValue, fieldName) {
|
|
88
|
-
if (typeof value === 'string')
|
|
89
|
-
return value;
|
|
90
|
-
logger.warn(`${fieldName} must be a string, using default: ${defaultValue}`);
|
|
91
|
-
return defaultValue;
|
|
92
|
-
}
|
|
93
|
-
/**
|
|
94
|
-
* Validates a number value.
|
|
95
|
-
* @param value - The value to validate.
|
|
96
|
-
* @param defaultValue - The default value to use if the value is not a number.
|
|
97
|
-
* @param fieldName - The name of the field being validated.
|
|
98
|
-
* @returns The validated number value.
|
|
99
|
-
*/
|
|
100
|
-
static validateNumber(value, defaultValue, fieldName) {
|
|
101
|
-
if (typeof value === 'number')
|
|
102
|
-
return value;
|
|
103
|
-
logger.warn(`${fieldName} must be a number, using default: ${defaultValue}`);
|
|
104
|
-
return defaultValue;
|
|
105
|
-
}
|
|
106
|
-
/**
|
|
107
|
-
* Validates a number value within a given range.
|
|
108
|
-
* @param value - The value to validate.
|
|
109
|
-
* @param min - The minimum value of the range.
|
|
110
|
-
* @param max - The maximum value of the range.
|
|
111
|
-
*/
|
|
112
|
-
static validateRange(value, min, max, defaultValue, fieldName) {
|
|
113
|
-
if (value >= min && value <= max)
|
|
114
|
-
return value;
|
|
115
|
-
logger.warn(`${fieldName} must be between ${min} and ${max}, using default: ${defaultValue}`);
|
|
116
|
-
return defaultValue;
|
|
117
|
-
}
|
|
118
|
-
/**
|
|
119
|
-
* Validates a number value within a given range.
|
|
120
|
-
* @param value - The value to validate.
|
|
121
|
-
* @param min - The minimum value of the range.
|
|
122
|
-
* @param max - The maximum value of the range.
|
|
123
|
-
* @param defaultValue - The default value to use if the value is not a number or is out of range.
|
|
124
|
-
*/
|
|
125
|
-
static validateNumberInRange(value, min, max, defaultValue, fieldName) {
|
|
126
|
-
const num = this.validateNumber(value, defaultValue, fieldName);
|
|
127
|
-
return this.validateRange(num, min, max, defaultValue, fieldName);
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
export default ConfigValidator;
|
|
131
|
-
//# sourceMappingURL=ConfigValidator.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"ConfigValidator.js","sourceRoot":"","sources":["../../../src/server/config/ConfigValidator.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,MAAM,MAAM,aAAa,CAAC;AACjC,OAAO,MAAM,MAAM,wBAAwB,CAAC;AAE5C,MAAM,MAAM,GAAG,IAAI,MAAM,CAAC,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,CAAC;AAEhD,MAAM,OAAO,eAAe;IACxB;;;;OAIG;IACI,MAAM,CAAC,QAAQ,CAAC,MAAsB;QACzC,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,GAAG,EAAE,SAAS,EAAE,GAAG,MAAM,CAAC;QAC9C,OAAO;YACH,IAAI,EAAE,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC;YAC7B,IAAI,EAAE,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC;YAC7B,GAAG,EAAE,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC;YAC1B,SAAS,EAAE,IAAI,CAAC,iBAAiB,CAAC,SAAS,CAAC;SAC/C,CAAC;IACN,CAAC;IACD;;;;OAIG;IACI,MAAM,CAAC,YAAY,CAAC,IAAa;QACpC,OAAO,IAAI,CAAC,c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