@oxog/spark 1.1.1 → 1.3.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/CHANGELOG.md +656 -315
- package/LICENSE +20 -20
- package/README.md +288 -241
- package/package.json +90 -89
- package/src/core/application.js +1068 -956
- package/src/core/context.js +1098 -830
- package/src/core/middleware.js +303 -217
- package/src/index.js +183 -280
- package/src/middleware/body-parser.js +482 -526
- package/src/middleware/cache.js +177 -170
- package/src/middleware/compression.js +177 -182
- package/src/middleware/cors.js +176 -95
- package/src/middleware/health.js +219 -218
- package/src/middleware/logger.js +195 -194
- package/src/middleware/metrics.js +339 -327
- package/src/middleware/rate-limit.js +367 -319
- package/src/middleware/security.js +442 -332
- package/src/middleware/session.js +647 -529
- package/src/middleware/static.js +404 -299
- package/src/router/layer.js +197 -117
- package/src/router/route.js +149 -123
- package/src/router/router.js +705 -755
- package/src/utils/async-handler.js +145 -147
- package/src/utils/context-pool.js +83 -82
- package/src/utils/regex-validator.js +187 -163
- package/src/utils/safe-json.js +110 -0
- package/types/index.d.ts +538 -291
- package/dist/LICENSE +0 -21
- package/dist/README.md +0 -242
- package/dist/core/application.js +0 -959
- package/dist/core/context.js +0 -833
- package/dist/core/middleware.js +0 -220
- package/dist/core/request.js +0 -335
- package/dist/core/response.js +0 -377
- package/dist/index.js +0 -280
- package/dist/middleware/body-parser.js +0 -528
- package/dist/middleware/cache.js +0 -173
- package/dist/middleware/compression.js +0 -185
- package/dist/middleware/cors.js +0 -97
- package/dist/middleware/health.js +0 -221
- package/dist/middleware/index.js +0 -23
- package/dist/middleware/logger.js +0 -197
- package/dist/middleware/metrics.js +0 -330
- package/dist/middleware/rate-limit.js +0 -322
- package/dist/middleware/security.js +0 -335
- package/dist/middleware/session.js +0 -532
- package/dist/middleware/static.js +0 -301
- package/dist/package.json +0 -62
- package/dist/router/layer.js +0 -119
- package/dist/router/route.js +0 -125
- package/dist/router/router.js +0 -758
- package/dist/types/index.d.ts +0 -291
- package/dist/utils/async-handler.js +0 -150
- package/dist/utils/cache.js +0 -443
- package/dist/utils/context-pool.js +0 -85
- package/dist/utils/error-types.js +0 -134
- package/dist/utils/http.js +0 -415
- package/dist/utils/regex-validator.js +0 -166
- package/src/core/request.js +0 -335
- package/src/core/response.js +0 -377
- package/src/middleware/index.js +0 -23
- package/src/utils/cache.js +0 -443
- package/src/utils/error-types.js +0 -134
- package/src/utils/http.js +0 -415
|
@@ -1,150 +1,148 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
/**
|
|
4
|
-
* Wraps async route handlers to properly catch errors
|
|
5
|
-
* @param {Function} fn - Async function to wrap
|
|
6
|
-
* @returns {Function} Wrapped function that catches errors
|
|
7
|
-
*/
|
|
8
|
-
function asyncHandler(fn) {
|
|
9
|
-
return (ctx, next) => {
|
|
10
|
-
const result = fn(ctx, next);
|
|
11
|
-
|
|
12
|
-
// If it's a promise, catch any errors
|
|
13
|
-
if (result && typeof result.then === 'function') {
|
|
14
|
-
return result.catch((error) => {
|
|
15
|
-
// Handle error appropriately
|
|
16
|
-
if (ctx && ctx.app) {
|
|
17
|
-
ctx.app.emit('error', error, ctx);
|
|
18
|
-
}
|
|
19
|
-
throw error; // Re-throw to be caught by framework
|
|
20
|
-
});
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Wraps all methods of a router or controller
|
|
29
|
-
* @param {Object} target - Object with methods to wrap
|
|
30
|
-
* @returns {Object} Object with wrapped methods
|
|
31
|
-
*/
|
|
32
|
-
function wrapAsync(target) {
|
|
33
|
-
const wrapped = {};
|
|
34
|
-
|
|
35
|
-
for (const key in target) {
|
|
36
|
-
if (typeof target[key] === 'function') {
|
|
37
|
-
wrapped[key] = asyncHandler(target[key]);
|
|
38
|
-
} else {
|
|
39
|
-
wrapped[key] = target[key];
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
return wrapped;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Error handler middleware that can work as both regular middleware and error handler
|
|
48
|
-
* @param {Error|Context} errOrCtx - Error object or Context (when used as regular middleware)
|
|
49
|
-
* @param {Context|Function} ctxOrNext - Context or next function
|
|
50
|
-
* @param {Function} [next] - Next middleware function
|
|
51
|
-
*/
|
|
52
|
-
function errorHandler(errOrCtx, ctxOrNext, next) {
|
|
53
|
-
// Check if this is being used as error handler (3 args) or regular middleware (2 args)
|
|
54
|
-
if (next && typeof next === 'function') {
|
|
55
|
-
// This is an error handler (err, ctx, next)
|
|
56
|
-
const err = errOrCtx;
|
|
57
|
-
const ctx = ctxOrNext;
|
|
58
|
-
|
|
59
|
-
// Skip if response already sent
|
|
60
|
-
if (ctx.responded) {
|
|
61
|
-
return;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
// Log error for debugging
|
|
65
|
-
console.error('Error handler caught:', err);
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
const status =
|
|
69
|
-
ctx.status(status);
|
|
70
|
-
|
|
71
|
-
handleErrorResponse(err, ctx, status);
|
|
72
|
-
} else {
|
|
73
|
-
// This is regular middleware (ctx, next)
|
|
74
|
-
const
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
//
|
|
84
|
-
const isDevelopment = process.env.NODE_ENV
|
|
85
|
-
const
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
if (isDevelopment) {
|
|
92
|
-
errorResponse.stack = err.stack;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
*
|
|
110
|
-
* @param {string}
|
|
111
|
-
* @
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
createError,
|
|
149
|
-
errors
|
|
3
|
+
/**
|
|
4
|
+
* Wraps async route handlers to properly catch errors
|
|
5
|
+
* @param {Function} fn - Async function to wrap
|
|
6
|
+
* @returns {Function} Wrapped function that catches errors
|
|
7
|
+
*/
|
|
8
|
+
function asyncHandler(fn) {
|
|
9
|
+
return (ctx, next) => {
|
|
10
|
+
const result = fn(ctx, next);
|
|
11
|
+
|
|
12
|
+
// If it's a promise, catch any errors
|
|
13
|
+
if (result && typeof result.then === 'function') {
|
|
14
|
+
return result.catch((error) => {
|
|
15
|
+
// Handle error appropriately
|
|
16
|
+
if (ctx && ctx.app) {
|
|
17
|
+
ctx.app.emit('error', error, ctx);
|
|
18
|
+
}
|
|
19
|
+
throw error; // Re-throw to be caught by framework
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Wraps all methods of a router or controller
|
|
29
|
+
* @param {Object} target - Object with methods to wrap
|
|
30
|
+
* @returns {Object} Object with wrapped methods
|
|
31
|
+
*/
|
|
32
|
+
function wrapAsync(target) {
|
|
33
|
+
const wrapped = {};
|
|
34
|
+
|
|
35
|
+
for (const key in target) {
|
|
36
|
+
if (typeof target[key] === 'function') {
|
|
37
|
+
wrapped[key] = asyncHandler(target[key]);
|
|
38
|
+
} else {
|
|
39
|
+
wrapped[key] = target[key];
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return wrapped;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Error handler middleware that can work as both regular middleware and error handler
|
|
48
|
+
* @param {Error|Context} errOrCtx - Error object or Context (when used as regular middleware)
|
|
49
|
+
* @param {Context|Function} ctxOrNext - Context or next function
|
|
50
|
+
* @param {Function} [next] - Next middleware function
|
|
51
|
+
*/
|
|
52
|
+
function errorHandler(errOrCtx, ctxOrNext, next) {
|
|
53
|
+
// Check if this is being used as error handler (3 args) or regular middleware (2 args)
|
|
54
|
+
if (next && typeof next === 'function') {
|
|
55
|
+
// This is an error handler (err, ctx, next)
|
|
56
|
+
const err = errOrCtx;
|
|
57
|
+
const ctx = ctxOrNext;
|
|
58
|
+
|
|
59
|
+
// Skip if response already sent
|
|
60
|
+
if (ctx.responded) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Log error for debugging
|
|
65
|
+
console.error('Error handler caught:', err);
|
|
66
|
+
|
|
67
|
+
const raw = Number(err && (err.status || err.statusCode));
|
|
68
|
+
const status = Number.isInteger(raw) && raw >= 400 && raw <= 599 ? raw : 500;
|
|
69
|
+
ctx.status(status);
|
|
70
|
+
|
|
71
|
+
handleErrorResponse(err, ctx, status);
|
|
72
|
+
} else {
|
|
73
|
+
// This is regular middleware (ctx, next)
|
|
74
|
+
const nextFn = ctxOrNext;
|
|
75
|
+
|
|
76
|
+
// Just pass through - this shouldn't normally happen
|
|
77
|
+
return nextFn();
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function handleErrorResponse(err, ctx, status) {
|
|
82
|
+
// Same exposure rules as the application error handler: messages of 5xx
|
|
83
|
+
// errors and stack traces are only sent when NODE_ENV is 'development'.
|
|
84
|
+
const isDevelopment = process.env.NODE_ENV === 'development';
|
|
85
|
+
const exposeDetails = status < 500 || isDevelopment;
|
|
86
|
+
const errorResponse = {
|
|
87
|
+
error: exposeDetails ? (err.message || 'Internal Server Error') : 'Internal Server Error',
|
|
88
|
+
status
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
if (isDevelopment && process.env.EXPOSE_STACK_TRACES !== 'false') {
|
|
92
|
+
errorResponse.stack = err.stack;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (err.code && exposeDetails) {
|
|
96
|
+
errorResponse.code = err.code;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (err.details && exposeDetails) {
|
|
100
|
+
errorResponse.details = err.details;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
ctx.json(errorResponse);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Creates a custom error with status code
|
|
108
|
+
* @param {string} message - Error message
|
|
109
|
+
* @param {number} status - HTTP status code
|
|
110
|
+
* @param {string} code - Error code
|
|
111
|
+
* @returns {Error} Custom error object
|
|
112
|
+
*/
|
|
113
|
+
function createError(message, status = 500, code = null) {
|
|
114
|
+
const error = new Error(message);
|
|
115
|
+
error.status = status;
|
|
116
|
+
if (code) {
|
|
117
|
+
error.code = code;
|
|
118
|
+
}
|
|
119
|
+
return error;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Common HTTP errors
|
|
124
|
+
*/
|
|
125
|
+
const errors = {
|
|
126
|
+
badRequest: (message = 'Bad Request') => createError(message, 400, 'BAD_REQUEST'),
|
|
127
|
+
unauthorized: (message = 'Unauthorized') => createError(message, 401, 'UNAUTHORIZED'),
|
|
128
|
+
forbidden: (message = 'Forbidden') => createError(message, 403, 'FORBIDDEN'),
|
|
129
|
+
notFound: (message = 'Not Found') => createError(message, 404, 'NOT_FOUND'),
|
|
130
|
+
methodNotAllowed: (message = 'Method Not Allowed') => createError(message, 405, 'METHOD_NOT_ALLOWED'),
|
|
131
|
+
conflict: (message = 'Conflict') => createError(message, 409, 'CONFLICT'),
|
|
132
|
+
gone: (message = 'Gone') => createError(message, 410, 'GONE'),
|
|
133
|
+
unprocessableEntity: (message = 'Unprocessable Entity') => createError(message, 422, 'UNPROCESSABLE_ENTITY'),
|
|
134
|
+
tooManyRequests: (message = 'Too Many Requests') => createError(message, 429, 'TOO_MANY_REQUESTS'),
|
|
135
|
+
internalServerError: (message = 'Internal Server Error') => createError(message, 500, 'INTERNAL_SERVER_ERROR'),
|
|
136
|
+
notImplemented: (message = 'Not Implemented') => createError(message, 501, 'NOT_IMPLEMENTED'),
|
|
137
|
+
badGateway: (message = 'Bad Gateway') => createError(message, 502, 'BAD_GATEWAY'),
|
|
138
|
+
serviceUnavailable: (message = 'Service Unavailable') => createError(message, 503, 'SERVICE_UNAVAILABLE'),
|
|
139
|
+
gatewayTimeout: (message = 'Gateway Timeout') => createError(message, 504, 'GATEWAY_TIMEOUT')
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
module.exports = {
|
|
143
|
+
asyncHandler,
|
|
144
|
+
wrapAsync,
|
|
145
|
+
errorHandler,
|
|
146
|
+
createError,
|
|
147
|
+
errors
|
|
150
148
|
};
|
|
@@ -1,85 +1,86 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const Context = require('../core/context');
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Context Object Pool for performance optimization
|
|
7
|
-
* Reuses Context objects to reduce garbage collection pressure
|
|
8
|
-
*/
|
|
9
|
-
class ContextPool {
|
|
10
|
-
constructor(maxSize = 100) {
|
|
11
|
-
this.pool = [];
|
|
12
|
-
this.maxSize = maxSize;
|
|
13
|
-
this.created = 0;
|
|
14
|
-
this.reused = 0;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Acquire a Context object from the pool or create new one
|
|
19
|
-
* @param {IncomingMessage} req - HTTP request object
|
|
20
|
-
* @param {ServerResponse} res - HTTP response object
|
|
21
|
-
* @param {Application} app - Spark application instance
|
|
22
|
-
* @returns {Context} Context instance
|
|
23
|
-
*/
|
|
24
|
-
acquire(req, res, app) {
|
|
25
|
-
let ctx;
|
|
26
|
-
|
|
27
|
-
if (this.pool.length > 0) {
|
|
28
|
-
ctx = this.pool.pop();
|
|
29
|
-
ctx.init(req, res, app);
|
|
30
|
-
this.reused++;
|
|
31
|
-
} else {
|
|
32
|
-
ctx = new Context(req, res, app);
|
|
33
|
-
this.created++;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
return ctx;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Release a Context object back to the pool
|
|
41
|
-
* @param {Context} ctx - Context to release
|
|
42
|
-
*/
|
|
43
|
-
release(ctx) {
|
|
44
|
-
if (this.pool.length < this.maxSize) {
|
|
45
|
-
ctx.reset();
|
|
46
|
-
this.pool.push(ctx);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Get pool statistics
|
|
52
|
-
* @returns {Object} Pool stats
|
|
53
|
-
*/
|
|
54
|
-
getStats() {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
*
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
}
|
|
84
|
-
|
|
3
|
+
const Context = require('../core/context');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Context Object Pool for performance optimization
|
|
7
|
+
* Reuses Context objects to reduce garbage collection pressure
|
|
8
|
+
*/
|
|
9
|
+
class ContextPool {
|
|
10
|
+
constructor(maxSize = 100) {
|
|
11
|
+
this.pool = [];
|
|
12
|
+
this.maxSize = maxSize;
|
|
13
|
+
this.created = 0;
|
|
14
|
+
this.reused = 0;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Acquire a Context object from the pool or create new one
|
|
19
|
+
* @param {IncomingMessage} req - HTTP request object
|
|
20
|
+
* @param {ServerResponse} res - HTTP response object
|
|
21
|
+
* @param {Application} app - Spark application instance
|
|
22
|
+
* @returns {Context} Context instance
|
|
23
|
+
*/
|
|
24
|
+
acquire(req, res, app) {
|
|
25
|
+
let ctx;
|
|
26
|
+
|
|
27
|
+
if (this.pool.length > 0) {
|
|
28
|
+
ctx = this.pool.pop();
|
|
29
|
+
ctx.init(req, res, app);
|
|
30
|
+
this.reused++;
|
|
31
|
+
} else {
|
|
32
|
+
ctx = new Context(req, res, app);
|
|
33
|
+
this.created++;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return ctx;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Release a Context object back to the pool
|
|
41
|
+
* @param {Context} ctx - Context to release
|
|
42
|
+
*/
|
|
43
|
+
release(ctx) {
|
|
44
|
+
if (this.pool.length < this.maxSize) {
|
|
45
|
+
ctx.reset();
|
|
46
|
+
this.pool.push(ctx);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Get pool statistics
|
|
52
|
+
* @returns {Object} Pool stats
|
|
53
|
+
*/
|
|
54
|
+
getStats() {
|
|
55
|
+
const totalAcquisitions = this.created + this.reused;
|
|
56
|
+
return {
|
|
57
|
+
poolSize: this.pool.length,
|
|
58
|
+
maxSize: this.maxSize,
|
|
59
|
+
created: this.created,
|
|
60
|
+
reused: this.reused,
|
|
61
|
+
reuseRatio: totalAcquisitions > 0 ? (this.reused / totalAcquisitions * 100).toFixed(2) + '%' : '0%'
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Clear the pool
|
|
67
|
+
*/
|
|
68
|
+
clear() {
|
|
69
|
+
this.pool.length = 0;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Resize the pool
|
|
74
|
+
* @param {number} newSize - New maximum pool size
|
|
75
|
+
*/
|
|
76
|
+
resize(newSize) {
|
|
77
|
+
this.maxSize = newSize;
|
|
78
|
+
|
|
79
|
+
// Trim pool if new size is smaller
|
|
80
|
+
if (this.pool.length > newSize) {
|
|
81
|
+
this.pool.length = newSize;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
85
86
|
module.exports = ContextPool;
|