@oxog/spark 1.1.0 → 1.2.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 +632 -293
- package/LICENSE +20 -20
- package/README.md +288 -241
- package/package.json +83 -89
- package/src/core/application.js +1070 -956
- package/src/core/context.js +1100 -830
- package/src/core/middleware.js +305 -217
- package/src/index.js +183 -278
- package/src/middleware/body-parser.js +484 -526
- package/src/middleware/cache.js +179 -170
- package/src/middleware/compression.js +179 -182
- package/src/middleware/cors.js +178 -95
- package/src/middleware/health.js +221 -218
- package/src/middleware/logger.js +197 -194
- package/src/middleware/metrics.js +341 -327
- package/src/middleware/rate-limit.js +369 -319
- package/src/middleware/security.js +444 -332
- package/src/middleware/session.js +649 -529
- package/src/middleware/static.js +406 -299
- package/src/router/layer.js +199 -117
- package/src/router/route.js +151 -123
- package/src/router/router.js +705 -755
- package/src/utils/async-handler.js +147 -144
- package/src/utils/context-pool.js +85 -82
- package/src/utils/regex-validator.js +189 -176
- package/src/utils/safe-json.js +110 -0
- package/types/index.d.ts +536 -291
- package/dist/LICENSE +0 -21
- package/dist/README.md +0 -242
- package/dist/core/application.js +0 -957
- package/dist/core/context.js +0 -831
- package/dist/core/middleware.js +0 -218
- package/dist/core/request.js +0 -333
- package/dist/core/response.js +0 -375
- package/dist/index.js +0 -278
- package/dist/middleware/body-parser.js +0 -526
- package/dist/middleware/cache.js +0 -171
- package/dist/middleware/compression.js +0 -183
- package/dist/middleware/cors.js +0 -95
- package/dist/middleware/health.js +0 -219
- package/dist/middleware/index.js +0 -21
- package/dist/middleware/logger.js +0 -195
- package/dist/middleware/metrics.js +0 -328
- package/dist/middleware/rate-limit.js +0 -320
- package/dist/middleware/security.js +0 -333
- package/dist/middleware/session.js +0 -530
- package/dist/middleware/static.js +0 -299
- package/dist/package.json +0 -62
- package/dist/router/layer.js +0 -117
- package/dist/router/route.js +0 -123
- package/dist/router/router.js +0 -756
- package/dist/types/index.d.ts +0 -291
- package/dist/utils/async-handler.js +0 -145
- package/dist/utils/cache.js +0 -441
- package/dist/utils/context-pool.js +0 -83
- package/dist/utils/error-types.js +0 -132
- package/dist/utils/http.js +0 -413
- package/dist/utils/regex-validator.js +0 -177
- package/src/core/request.js +0 -333
- package/src/core/response.js +0 -375
- package/src/middleware/index.js +0 -21
- package/src/utils/cache.js +0 -441
- package/src/utils/error-types.js +0 -132
- package/src/utils/http.js +0 -413
package/src/index.js
CHANGED
|
@@ -1,278 +1,183 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
* @
|
|
5
|
-
* @
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
const
|
|
15
|
-
const
|
|
16
|
-
const
|
|
17
|
-
const
|
|
18
|
-
const
|
|
19
|
-
const
|
|
20
|
-
const
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
* Spark
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
* @
|
|
39
|
-
*
|
|
40
|
-
* @
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
* const
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
* }
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
* app
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
* app.
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
* @param {
|
|
79
|
-
* @param {
|
|
80
|
-
* @param {
|
|
81
|
-
* @param {
|
|
82
|
-
*
|
|
83
|
-
* @
|
|
84
|
-
*
|
|
85
|
-
* @
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
-
*
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
* }
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
*
|
|
109
|
-
* @
|
|
110
|
-
|
|
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
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
/**
|
|
186
|
-
* Body parsing middleware - Direct export
|
|
187
|
-
* @type {Function}
|
|
188
|
-
*/
|
|
189
|
-
bodyParser,
|
|
190
|
-
|
|
191
|
-
/**
|
|
192
|
-
* CORS middleware - Direct export
|
|
193
|
-
* @type {Function}
|
|
194
|
-
*/
|
|
195
|
-
cors,
|
|
196
|
-
|
|
197
|
-
/**
|
|
198
|
-
* Compression middleware - Direct export
|
|
199
|
-
* @type {Function}
|
|
200
|
-
*/
|
|
201
|
-
compression,
|
|
202
|
-
|
|
203
|
-
/**
|
|
204
|
-
* Static file middleware - Direct export
|
|
205
|
-
* @type {Function}
|
|
206
|
-
*/
|
|
207
|
-
static: staticFiles,
|
|
208
|
-
|
|
209
|
-
/**
|
|
210
|
-
* Session middleware - Direct export
|
|
211
|
-
* @type {Function}
|
|
212
|
-
*/
|
|
213
|
-
session,
|
|
214
|
-
|
|
215
|
-
/**
|
|
216
|
-
* Rate limiting middleware - Direct export
|
|
217
|
-
* @type {Function}
|
|
218
|
-
*/
|
|
219
|
-
rateLimit,
|
|
220
|
-
|
|
221
|
-
/**
|
|
222
|
-
* Security headers middleware - Direct export
|
|
223
|
-
* @type {Function}
|
|
224
|
-
*/
|
|
225
|
-
security,
|
|
226
|
-
|
|
227
|
-
/**
|
|
228
|
-
* Health check middleware - Direct export
|
|
229
|
-
* @type {Function}
|
|
230
|
-
*/
|
|
231
|
-
healthCheck,
|
|
232
|
-
|
|
233
|
-
/**
|
|
234
|
-
* Metrics collection middleware - Direct export
|
|
235
|
-
* @type {Function}
|
|
236
|
-
*/
|
|
237
|
-
metrics,
|
|
238
|
-
|
|
239
|
-
/**
|
|
240
|
-
* Logger middleware - Direct export
|
|
241
|
-
* @type {Function}
|
|
242
|
-
*/
|
|
243
|
-
logger,
|
|
244
|
-
|
|
245
|
-
/**
|
|
246
|
-
* Error handling utilities
|
|
247
|
-
* @namespace errorHandling
|
|
248
|
-
* @property {Function} asyncHandler - Wraps async functions for error handling
|
|
249
|
-
* @property {Function} errorHandler - Global error handler middleware
|
|
250
|
-
* @property {Function} createError - Creates custom errors with status codes
|
|
251
|
-
* @property {Object} errors - Predefined error types
|
|
252
|
-
*/
|
|
253
|
-
errorHandling: {
|
|
254
|
-
asyncHandler,
|
|
255
|
-
errorHandler,
|
|
256
|
-
createError,
|
|
257
|
-
errors
|
|
258
|
-
},
|
|
259
|
-
|
|
260
|
-
/**
|
|
261
|
-
* Utility classes and functions
|
|
262
|
-
* @namespace utils
|
|
263
|
-
* @property {Class} ContextPool - Object pool for Context instances
|
|
264
|
-
* @property {Class} RegexValidator - Regex validation utilities
|
|
265
|
-
* @property {Class} SafeRegexCache - Safe regex caching system
|
|
266
|
-
*/
|
|
267
|
-
utils: {
|
|
268
|
-
ContextPool,
|
|
269
|
-
RegexValidator,
|
|
270
|
-
SafeRegexCache
|
|
271
|
-
}
|
|
272
|
-
};
|
|
273
|
-
|
|
274
|
-
/**
|
|
275
|
-
* Default export for ES6 compatibility
|
|
276
|
-
* @type {Object}
|
|
277
|
-
*/
|
|
278
|
-
module.exports.default = module.exports;
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @fileoverview Main entry point for the Spark Web Framework
|
|
5
|
+
* @author Spark Framework Team
|
|
6
|
+
* @since 1.0.0
|
|
7
|
+
* @version 1.0.0
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
const Application = require('./core/application');
|
|
11
|
+
const Router = require('./router/router');
|
|
12
|
+
const Context = require('./core/context');
|
|
13
|
+
|
|
14
|
+
const bodyParser = require('./middleware/body-parser');
|
|
15
|
+
const cors = require('./middleware/cors');
|
|
16
|
+
const compression = require('./middleware/compression');
|
|
17
|
+
const staticFiles = require('./middleware/static');
|
|
18
|
+
const session = require('./middleware/session');
|
|
19
|
+
const rateLimit = require('./middleware/rate-limit');
|
|
20
|
+
const security = require('./middleware/security');
|
|
21
|
+
const healthCheck = require('./middleware/health');
|
|
22
|
+
const metrics = require('./middleware/metrics');
|
|
23
|
+
const logger = require('./middleware/logger');
|
|
24
|
+
|
|
25
|
+
// Error handling utilities
|
|
26
|
+
const { asyncHandler, errorHandler, createError, errors } = require('./utils/async-handler');
|
|
27
|
+
|
|
28
|
+
// Utility classes
|
|
29
|
+
const ContextPool = require('./utils/context-pool');
|
|
30
|
+
const { RegexValidator, SafeRegexCache } = require('./utils/regex-validator');
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Spark Web Framework - A fast, secure, and modern Node.js web framework
|
|
34
|
+
*
|
|
35
|
+
* Spark extends the base Application class to provide a complete web framework
|
|
36
|
+
* with built-in security, middleware support, and modern development features.
|
|
37
|
+
*
|
|
38
|
+
* @class Spark
|
|
39
|
+
* @extends Application
|
|
40
|
+
* @since 1.0.0
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* // Basic usage
|
|
44
|
+
* const { Spark } = require('@oxog/spark');
|
|
45
|
+
*
|
|
46
|
+
* const app = new Spark({
|
|
47
|
+
* port: 3000,
|
|
48
|
+
* security: {
|
|
49
|
+
* cors: { origin: true },
|
|
50
|
+
* rateLimit: { max: 100, window: 60000 }
|
|
51
|
+
* }
|
|
52
|
+
* });
|
|
53
|
+
*
|
|
54
|
+
* app.get('/', (ctx) => {
|
|
55
|
+
* ctx.json({ message: 'Hello Spark!' });
|
|
56
|
+
* });
|
|
57
|
+
*
|
|
58
|
+
* app.listen();
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* // With middleware
|
|
62
|
+
* const app = new Spark();
|
|
63
|
+
*
|
|
64
|
+
* app.use(bodyParser.json());
|
|
65
|
+
* app.use(cors({ origin: 'https://example.com' }));
|
|
66
|
+
*
|
|
67
|
+
* app.post('/api/users', (ctx) => {
|
|
68
|
+
* const userData = ctx.body;
|
|
69
|
+
* ctx.json({ success: true, data: userData });
|
|
70
|
+
* });
|
|
71
|
+
*
|
|
72
|
+
* app.listen(8080);
|
|
73
|
+
*/
|
|
74
|
+
class Spark extends Application {
|
|
75
|
+
/**
|
|
76
|
+
* Create a new Spark application instance
|
|
77
|
+
*
|
|
78
|
+
* @param {Object} [options={}] - Configuration options for the application
|
|
79
|
+
* @param {number} [options.port=3000] - Port number to listen on
|
|
80
|
+
* @param {string} [options.host='127.0.0.1'] - Host address to bind to
|
|
81
|
+
* @param {boolean} [options.cluster=false] - Enable cluster mode
|
|
82
|
+
* @param {Object} [options.security] - Security configuration
|
|
83
|
+
* @param {Object} [options.https] - HTTPS configuration with key and cert
|
|
84
|
+
*
|
|
85
|
+
* @since 1.0.0
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* const app = new Spark({
|
|
89
|
+
* port: 8080,
|
|
90
|
+
* host: '0.0.0.0',
|
|
91
|
+
* security: {
|
|
92
|
+
* cors: { origin: ['https://example.com'] },
|
|
93
|
+
* rateLimit: { max: 200, window: 60000 }
|
|
94
|
+
* }
|
|
95
|
+
* });
|
|
96
|
+
*/
|
|
97
|
+
constructor(options) {
|
|
98
|
+
super(options);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// Export both Spark and App for backwards compatibility
|
|
103
|
+
const App = Spark;
|
|
104
|
+
|
|
105
|
+
const cache = require('./middleware/cache');
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Middleware collection
|
|
109
|
+
* @namespace middleware
|
|
110
|
+
*/
|
|
111
|
+
const middleware = {
|
|
112
|
+
bodyParser,
|
|
113
|
+
cors,
|
|
114
|
+
compression,
|
|
115
|
+
static: staticFiles,
|
|
116
|
+
session,
|
|
117
|
+
rateLimit,
|
|
118
|
+
security,
|
|
119
|
+
helmet: security, // Alias for security middleware
|
|
120
|
+
healthCheck,
|
|
121
|
+
health: healthCheck, // Alias
|
|
122
|
+
metrics,
|
|
123
|
+
logger,
|
|
124
|
+
cache,
|
|
125
|
+
compress: compression // Alias for compression
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Error handling utilities
|
|
130
|
+
* @namespace errorHandling
|
|
131
|
+
*/
|
|
132
|
+
const errorHandling = {
|
|
133
|
+
asyncHandler,
|
|
134
|
+
errorHandler,
|
|
135
|
+
createError,
|
|
136
|
+
errors
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Utility classes
|
|
141
|
+
* @namespace utils
|
|
142
|
+
*/
|
|
143
|
+
const utils = {
|
|
144
|
+
ContextPool,
|
|
145
|
+
RegexValidator,
|
|
146
|
+
SafeRegexCache
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
// Exports are assigned one by one so Node's CommonJS export detection exposes
|
|
150
|
+
// every name to ESM `import { ... } from '@oxog/spark'`.
|
|
151
|
+
module.exports.Spark = Spark;
|
|
152
|
+
/** @deprecated Use Spark instead */
|
|
153
|
+
module.exports.App = App;
|
|
154
|
+
module.exports.Application = Application;
|
|
155
|
+
module.exports.Router = Router;
|
|
156
|
+
module.exports.Context = Context;
|
|
157
|
+
module.exports.middleware = middleware;
|
|
158
|
+
module.exports.bodyParser = bodyParser;
|
|
159
|
+
module.exports.cors = cors;
|
|
160
|
+
module.exports.compression = compression;
|
|
161
|
+
module.exports.static = staticFiles;
|
|
162
|
+
module.exports.serveStatic = staticFiles;
|
|
163
|
+
module.exports.session = session;
|
|
164
|
+
module.exports.rateLimit = rateLimit;
|
|
165
|
+
module.exports.security = security;
|
|
166
|
+
module.exports.csrf = security.csrf;
|
|
167
|
+
module.exports.csrfToken = security.csrfToken;
|
|
168
|
+
module.exports.healthCheck = healthCheck;
|
|
169
|
+
module.exports.metrics = metrics;
|
|
170
|
+
module.exports.logger = logger;
|
|
171
|
+
module.exports.cache = cache;
|
|
172
|
+
module.exports.errorHandling = errorHandling;
|
|
173
|
+
module.exports.asyncHandler = asyncHandler;
|
|
174
|
+
module.exports.createError = createError;
|
|
175
|
+
module.exports.errors = errors;
|
|
176
|
+
module.exports.utils = utils;
|
|
177
|
+
module.exports.version = require('../package.json').version;
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Default export for ES6 compatibility
|
|
181
|
+
* @type {Object}
|
|
182
|
+
*/
|
|
183
|
+
module.exports.default = module.exports;
|