@boo-labs/server 1.0.1 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/source/core/IPCMain/IPCProtocol.mjs +80 -75
- package/source/shared/domain/UUIDV7/index.mjs +31 -39
- package/source/shared/errors/ServerError/index.mjs +48 -2
- package/source/shared/outcome/Result/index.mjs +77 -130
- package/source/shared/outcome/Validation/index.mjs +56 -114
- package/source/shared/validations/Middleware/index.mjs +32 -23
- package/source/shared/validations/PathnamePattern/index.mjs +38 -39
- package/source/shared/validations/Server/NotFoundHandler/index.mjs +25 -15
- package/source/shared/validations/Server/Router/index.mjs +24 -14
- package/source/shared/validations/Server/ServerConfiguration/index.mjs +24 -14
- package/source/shared/validations/Server/SystemErrorHandler/index.mjs +25 -15
- package/source/shared/validations/ServerConfiguration/CORSOptions/index.mjs +95 -91
- package/source/shared/validations/ServerConfiguration/CSRFOptions/index.mjs +96 -96
- package/source/shared/validations/ServerConfiguration/CompressOptions/index.mjs +55 -43
- package/source/shared/validations/ServerConfiguration/CookieParserOptions/index.mjs +28 -13
- package/source/shared/validations/ServerConfiguration/CookieSessionOptions/index.mjs +101 -111
- package/source/shared/validations/ServerConfiguration/ListenOptions/index.mjs +55 -43
- package/source/shared/validations/ServerConfiguration/RateLimitOptions/index.mjs +71 -61
- package/source/shared/validations/ServerConfiguration/RequestOptions/index.mjs +44 -31
- package/source/shared/validations/ServerConfiguration/ResponseOptions/index.mjs +39 -25
- package/source/shared/validations/ServerConfiguration/SecurityHeadersOptions/index.mjs +27 -14
- package/source/shared/validations/ServerConfiguration/StaticOptions/index.mjs +85 -84
- package/types/index.d.ts +612 -76
- package/source/shared/outcome/ErrorSchema.mjs +0 -361
|
@@ -1,361 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file Defines the structured schema used to describe operation errors.
|
|
3
|
-
*
|
|
4
|
-
* @description ErrorSchema normalizes literal-message and code-based error
|
|
5
|
-
* definitions into a consistent schema used to instantiate Error objects.
|
|
6
|
-
*
|
|
7
|
-
* @module ErrorSchema
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
// ━━ IMPORT MODULES ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
|
11
|
-
// » IMPORT NATIVE NODEJS MODULES
|
|
12
|
-
import { inspect } from 'node:util';
|
|
13
|
-
|
|
14
|
-
// » IMPORT THIRD PARTY MODULES
|
|
15
|
-
|
|
16
|
-
// » IMPORT LOCAL MODULES
|
|
17
|
-
|
|
18
|
-
// ━━ TYPE DEFINITIONS ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
|
19
|
-
/**
|
|
20
|
-
* Represents an Error constructor.
|
|
21
|
-
*
|
|
22
|
-
* @typedef {new (identifier: string, options?: Record<string, unknown>) => Error} ErrorConstructor
|
|
23
|
-
*/
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Represents options passed to a native Error constructor.
|
|
27
|
-
*
|
|
28
|
-
* @typedef {object} NativeErrorOptions
|
|
29
|
-
* @property {unknown} [cause] - Cause associated with the error.
|
|
30
|
-
*/
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Represents a native Error schema definition.
|
|
34
|
-
*
|
|
35
|
-
* @typedef {object} NativeErrorSchema
|
|
36
|
-
* @property {ErrorConstructor} error - Error constructor used to create the error.
|
|
37
|
-
* @property {string} message - Literal error message.
|
|
38
|
-
* @property {NativeErrorOptions} [options] - Options passed to the Error constructor.
|
|
39
|
-
* @property {false} custom - Indicates that the constructor represents a native Error.
|
|
40
|
-
* @property {string} code - Error identifier.
|
|
41
|
-
*/
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Represents options passed to a custom Error constructor.
|
|
45
|
-
*
|
|
46
|
-
* @typedef {object} CustomErrorOptions
|
|
47
|
-
* @property {Record<string, unknown>} [values] - Values used to resolve message placeholders.
|
|
48
|
-
* @property {Record<string, unknown>} [details] - Additional contextual information about the error.
|
|
49
|
-
* @property {unknown} [cause] - Cause associated with the error.
|
|
50
|
-
*/
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Represents a custom Error schema definition.
|
|
54
|
-
*
|
|
55
|
-
* @typedef {object} CustomErrorSchema
|
|
56
|
-
* @property {ErrorConstructor} error - Error constructor used to create the error.
|
|
57
|
-
* @property {string} code - Error code used to resolve the error message and error identifier.
|
|
58
|
-
* @property {string} [message] - Literal error message.
|
|
59
|
-
* @property {CustomErrorOptions} [options] - Options passed to the Error constructor.
|
|
60
|
-
* @property {true} custom - Indicates that the constructor represents a custom Error.
|
|
61
|
-
*/
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Represents a supported Error schema definition.
|
|
65
|
-
*
|
|
66
|
-
* @typedef {NativeErrorSchema|CustomErrorSchema} ErrorSchemaDefinition
|
|
67
|
-
* @exports ErrorSchemaDefinition
|
|
68
|
-
*/
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Represents the normalized state of an ErrorSchema.
|
|
72
|
-
*
|
|
73
|
-
* @typedef {object} ErrorSchemaState
|
|
74
|
-
* @property {string} [message] - Literal error message.
|
|
75
|
-
* @property {string} type - Error constructor name.
|
|
76
|
-
* @property {Record<string, unknown>} options - Options passed to the Error constructor.
|
|
77
|
-
* @property {string} code - Error identifier.
|
|
78
|
-
*/
|
|
79
|
-
|
|
80
|
-
// ━━ FUNCTIONS ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
|
81
|
-
/**
|
|
82
|
-
* Determines whether a value is a plain object.
|
|
83
|
-
*
|
|
84
|
-
* @private
|
|
85
|
-
* @param {unknown} value - Value to check.
|
|
86
|
-
* @returns {boolean} Whether the value is a plain object.
|
|
87
|
-
*/
|
|
88
|
-
const isObject = value => Object.prototype.toString.call(value) === '[object Object]';
|
|
89
|
-
|
|
90
|
-
// ━━ MODULE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
|
91
|
-
/**
|
|
92
|
-
* Defines a structured error schema and creates its corresponding Error.
|
|
93
|
-
*
|
|
94
|
-
* @classdesc ErrorSchema accepts either a literal error message or a domain
|
|
95
|
-
* error code. Message-based schemas create errors directly from the message,
|
|
96
|
-
* while code-based schemas preserve the code, values, and details required by
|
|
97
|
-
* the consuming error system.
|
|
98
|
-
*/
|
|
99
|
-
class ErrorSchema {
|
|
100
|
-
/**
|
|
101
|
-
* Normalized error schema definition.
|
|
102
|
-
*
|
|
103
|
-
* @type {ErrorSchemaState}
|
|
104
|
-
*/
|
|
105
|
-
#schema;
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* Error constructor used to reconstruct the error.
|
|
109
|
-
*
|
|
110
|
-
* @type {typeof Error}
|
|
111
|
-
*/
|
|
112
|
-
#error;
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* Indicates whether the error uses the custom Error flow.
|
|
116
|
-
*
|
|
117
|
-
* @type {boolean}
|
|
118
|
-
*/
|
|
119
|
-
#custom;
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* Creates an error schema from an Error definition.
|
|
123
|
-
*
|
|
124
|
-
* The schema must provide an Error constructor. Native Errors require a
|
|
125
|
-
* message and code, while custom Errors require a code and may provide a message.
|
|
126
|
-
*
|
|
127
|
-
* @param {ErrorSchemaDefinition} schema - Error schema configuration.
|
|
128
|
-
* @throws {TypeError} If `error` is not an Error constructor.
|
|
129
|
-
* @throws {TypeError} If a native Error does not provide a message.
|
|
130
|
-
* @throws {TypeError} If message is not a string.
|
|
131
|
-
* @throws {TypeError} If a custom Error provides neither message nor code.
|
|
132
|
-
* @example
|
|
133
|
-
* ```js
|
|
134
|
-
* const schema = new ErrorSchema({
|
|
135
|
-
* error: TypeError,
|
|
136
|
-
* message: 'Invalid request.'
|
|
137
|
-
* });
|
|
138
|
-
*
|
|
139
|
-
* schema.error;
|
|
140
|
-
* ```
|
|
141
|
-
*
|
|
142
|
-
* @example
|
|
143
|
-
* ```js
|
|
144
|
-
* const schema = new ErrorSchema({
|
|
145
|
-
* error: RequestError,
|
|
146
|
-
* code: 'INVALID_REQUEST',
|
|
147
|
-
* options: {
|
|
148
|
-
* values: { field: 'email' },
|
|
149
|
-
* details: { source: 'RequestValidator' }
|
|
150
|
-
* },
|
|
151
|
-
* custom: true
|
|
152
|
-
* });
|
|
153
|
-
*
|
|
154
|
-
* schema.error;
|
|
155
|
-
* ```
|
|
156
|
-
*/
|
|
157
|
-
constructor(schema) {
|
|
158
|
-
if (!isObject(schema)) {
|
|
159
|
-
throw new TypeError('Schema options must be an object.');
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
if (!('error' in schema)) {
|
|
163
|
-
throw new TypeError('ErrorSchema error constructor is required.');
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
if (!ErrorSchema.isErrorConstructor(schema.error)) {
|
|
167
|
-
throw new TypeError('Expected an Error constructor');
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
const custom = schema.custom ?? false;
|
|
171
|
-
|
|
172
|
-
if (!custom && !('message' in schema)) {
|
|
173
|
-
throw new TypeError('Message is required for a native Error');
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
if (!custom && !('code' in schema)) {
|
|
177
|
-
throw new TypeError('Code is required for Error Schema');
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
if ('message' in schema && typeof schema.message !== 'string') {
|
|
181
|
-
throw new TypeError('Message must be a string');
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
if (custom && !('message' in schema) && !('code' in schema)) {
|
|
185
|
-
throw new TypeError('Message or code is required for a Custom Error');
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
this.#error = schema.error;
|
|
189
|
-
this.#custom = custom;
|
|
190
|
-
|
|
191
|
-
this.#schema = Object.freeze({
|
|
192
|
-
message: schema.message,
|
|
193
|
-
type: schema.error.name,
|
|
194
|
-
options: schema.options ?? {},
|
|
195
|
-
code: schema.code,
|
|
196
|
-
});
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
/**
|
|
200
|
-
* Normalized error schema.
|
|
201
|
-
*
|
|
202
|
-
* @readonly
|
|
203
|
-
* @type {object}
|
|
204
|
-
*/
|
|
205
|
-
get schema() {
|
|
206
|
-
return this.#schema;
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
/**
|
|
210
|
-
* Creates a new Error instance from the configured schema.
|
|
211
|
-
*
|
|
212
|
-
* @readonly
|
|
213
|
-
* @type {Error}
|
|
214
|
-
*/
|
|
215
|
-
get error() {
|
|
216
|
-
const { message, options, code } = this.#schema;
|
|
217
|
-
|
|
218
|
-
if (this.#custom) {
|
|
219
|
-
return new this.#error(message ?? code, options);
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
return new this.#error(message, options);
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
/**
|
|
226
|
-
* Custom string tag for type identification.
|
|
227
|
-
*
|
|
228
|
-
* @readonly
|
|
229
|
-
* @type {string}
|
|
230
|
-
*/
|
|
231
|
-
get [Symbol.toStringTag]() {
|
|
232
|
-
return `${this.constructor.name}`;
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
/**
|
|
236
|
-
* Creates a plain object representation of the error schema.
|
|
237
|
-
*
|
|
238
|
-
* @returns {ErrorSchemaState} Normalized error schema.
|
|
239
|
-
* @example
|
|
240
|
-
* ```js
|
|
241
|
-
* const schema = new ErrorSchema({
|
|
242
|
-
* error: TypeError,
|
|
243
|
-
* message: 'Invalid middleware.',
|
|
244
|
-
* code: 'INVALID_MIDDLEWARE',
|
|
245
|
-
* custom: false
|
|
246
|
-
* });
|
|
247
|
-
*
|
|
248
|
-
* console.log(schema.toObject());
|
|
249
|
-
* // {
|
|
250
|
-
* // message: 'Invalid middleware.',
|
|
251
|
-
* // type: 'TypeError',
|
|
252
|
-
* // options: {},
|
|
253
|
-
* // code: 'INVALID_MIDDLEWARE'
|
|
254
|
-
* // }
|
|
255
|
-
* ```
|
|
256
|
-
*/
|
|
257
|
-
toObject() {
|
|
258
|
-
return { ...this.#schema };
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
/**
|
|
262
|
-
* Creates a JSON-compatible representation of the error schema.
|
|
263
|
-
*
|
|
264
|
-
* @returns {ErrorSchemaState} Normalized error schema.
|
|
265
|
-
* @example
|
|
266
|
-
* ```js
|
|
267
|
-
* const schema = new ErrorSchema({
|
|
268
|
-
* error: RequestError,
|
|
269
|
-
* code: 'INVALID_REQUEST',
|
|
270
|
-
* options: {
|
|
271
|
-
* values: { field: 'email' },
|
|
272
|
-
* details: { source: 'RequestValidator' }
|
|
273
|
-
* },
|
|
274
|
-
* custom: true
|
|
275
|
-
* });
|
|
276
|
-
*
|
|
277
|
-
* console.log(JSON.stringify(schema));
|
|
278
|
-
* // {"message":undefined,"type":"RequestError",
|
|
279
|
-
* // "options":{"values":{"field":"email"},
|
|
280
|
-
* // "details":{"source":"RequestValidator"}},
|
|
281
|
-
* // "code":"INVALID_REQUEST"}
|
|
282
|
-
* ```
|
|
283
|
-
*/
|
|
284
|
-
toJSON() {
|
|
285
|
-
const schema = this.toObject();
|
|
286
|
-
const options = schema.options;
|
|
287
|
-
|
|
288
|
-
const base = {
|
|
289
|
-
message: schema.message,
|
|
290
|
-
type: schema.type,
|
|
291
|
-
code: schema.code,
|
|
292
|
-
};
|
|
293
|
-
|
|
294
|
-
if ('cause' in options && options.cause instanceof Error) {
|
|
295
|
-
const { cause, ...rest } = options;
|
|
296
|
-
|
|
297
|
-
return {
|
|
298
|
-
...base,
|
|
299
|
-
options: {
|
|
300
|
-
...rest,
|
|
301
|
-
cause: {
|
|
302
|
-
name: cause.name,
|
|
303
|
-
message: cause.message,
|
|
304
|
-
stack: cause.stack,
|
|
305
|
-
},
|
|
306
|
-
},
|
|
307
|
-
};
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
return {
|
|
311
|
-
...base,
|
|
312
|
-
options,
|
|
313
|
-
};
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
/**
|
|
317
|
-
* Custom inspect implementation for Node.js `util.inspect()`.
|
|
318
|
-
*
|
|
319
|
-
* @returns {string} Custom string representation for debugging.
|
|
320
|
-
* @example
|
|
321
|
-
* ```js
|
|
322
|
-
* const schema = new ErrorSchema({
|
|
323
|
-
* error: TypeError,
|
|
324
|
-
* message: 'Invalid middleware.',
|
|
325
|
-
* code: 'INVALID_MIDDLEWARE',
|
|
326
|
-
* custom: false
|
|
327
|
-
* });
|
|
328
|
-
*
|
|
329
|
-
* console.log(inspect(schema));
|
|
330
|
-
* // ErrorSchema({
|
|
331
|
-
* // message: 'Invalid middleware.',
|
|
332
|
-
* // type: 'TypeError',
|
|
333
|
-
* // options: {},
|
|
334
|
-
* // code: 'INVALID_MIDDLEWARE'
|
|
335
|
-
* // })
|
|
336
|
-
* ```
|
|
337
|
-
*/
|
|
338
|
-
[inspect.custom]() {
|
|
339
|
-
return `${this.constructor.name}(${inspect(this.toObject(), { colors: true, depth: 2 })})`;
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
/**
|
|
343
|
-
* Determines whether a value is an Error constructor.
|
|
344
|
-
*
|
|
345
|
-
* @param {unknown} error - Value to check.
|
|
346
|
-
* @returns {boolean} Whether `error` is a constructor that inherits from `Error`.
|
|
347
|
-
* @example
|
|
348
|
-
* ```js
|
|
349
|
-
* ErrorSchema.isErrorConstructor(TypeError); // true
|
|
350
|
-
* ErrorSchema.isErrorConstructor(RangeError); // true
|
|
351
|
-
* ErrorSchema.isErrorConstructor(Math); // false
|
|
352
|
-
* ```
|
|
353
|
-
*/
|
|
354
|
-
static isErrorConstructor(error) {
|
|
355
|
-
if (error === Error) return true;
|
|
356
|
-
if (typeof error !== 'function') return false;
|
|
357
|
-
return Object.prototype.isPrototypeOf.call(Error.prototype, error.prototype);
|
|
358
|
-
}
|
|
359
|
-
}
|
|
360
|
-
// ━━ EXPORT MODULE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
|
361
|
-
export default ErrorSchema;
|