@oxog/spark 1.1.1 → 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.
Files changed (64) hide show
  1. package/CHANGELOG.md +632 -315
  2. package/LICENSE +20 -20
  3. package/README.md +288 -241
  4. package/package.json +83 -89
  5. package/src/core/application.js +1068 -956
  6. package/src/core/context.js +1098 -830
  7. package/src/core/middleware.js +303 -217
  8. package/src/index.js +183 -280
  9. package/src/middleware/body-parser.js +482 -526
  10. package/src/middleware/cache.js +177 -170
  11. package/src/middleware/compression.js +177 -182
  12. package/src/middleware/cors.js +176 -95
  13. package/src/middleware/health.js +219 -218
  14. package/src/middleware/logger.js +195 -194
  15. package/src/middleware/metrics.js +339 -327
  16. package/src/middleware/rate-limit.js +367 -319
  17. package/src/middleware/security.js +442 -332
  18. package/src/middleware/session.js +647 -529
  19. package/src/middleware/static.js +404 -299
  20. package/src/router/layer.js +197 -117
  21. package/src/router/route.js +149 -123
  22. package/src/router/router.js +703 -755
  23. package/src/utils/async-handler.js +145 -147
  24. package/src/utils/context-pool.js +83 -82
  25. package/src/utils/regex-validator.js +187 -163
  26. package/src/utils/safe-json.js +110 -0
  27. package/types/index.d.ts +536 -291
  28. package/dist/LICENSE +0 -21
  29. package/dist/README.md +0 -242
  30. package/dist/core/application.js +0 -959
  31. package/dist/core/context.js +0 -833
  32. package/dist/core/middleware.js +0 -220
  33. package/dist/core/request.js +0 -335
  34. package/dist/core/response.js +0 -377
  35. package/dist/index.js +0 -280
  36. package/dist/middleware/body-parser.js +0 -528
  37. package/dist/middleware/cache.js +0 -173
  38. package/dist/middleware/compression.js +0 -185
  39. package/dist/middleware/cors.js +0 -97
  40. package/dist/middleware/health.js +0 -221
  41. package/dist/middleware/index.js +0 -23
  42. package/dist/middleware/logger.js +0 -197
  43. package/dist/middleware/metrics.js +0 -330
  44. package/dist/middleware/rate-limit.js +0 -322
  45. package/dist/middleware/security.js +0 -335
  46. package/dist/middleware/session.js +0 -532
  47. package/dist/middleware/static.js +0 -301
  48. package/dist/package.json +0 -62
  49. package/dist/router/layer.js +0 -119
  50. package/dist/router/route.js +0 -125
  51. package/dist/router/router.js +0 -758
  52. package/dist/types/index.d.ts +0 -291
  53. package/dist/utils/async-handler.js +0 -150
  54. package/dist/utils/cache.js +0 -443
  55. package/dist/utils/context-pool.js +0 -85
  56. package/dist/utils/error-types.js +0 -134
  57. package/dist/utils/http.js +0 -415
  58. package/dist/utils/regex-validator.js +0 -166
  59. package/src/core/request.js +0 -335
  60. package/src/core/response.js +0 -377
  61. package/src/middleware/index.js +0 -23
  62. package/src/utils/cache.js +0 -443
  63. package/src/utils/error-types.js +0 -134
  64. package/src/utils/http.js +0 -415
package/src/index.js CHANGED
@@ -1,280 +1,183 @@
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('spark-framework');
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
- /**
106
- * Main exports for the Spark Web Framework
107
- *
108
- * @namespace SparkExports
109
- * @since 1.0.0
110
- *
111
- * @example
112
- * // ES6 destructuring
113
- * const { Spark, Router, bodyParser } = require('spark-framework');
114
- *
115
- * @example
116
- * // CommonJS
117
- * const Spark = require('spark-framework').Spark;
118
- *
119
- * @example
120
- * // Access middleware
121
- * const { middleware } = require('spark-framework');
122
- * app.use(middleware.bodyParser.json());
123
- */
124
- module.exports = {
125
- /**
126
- * Main Spark application class
127
- * @type {typeof Spark}
128
- */
129
- Spark,
130
-
131
- /**
132
- * Backwards compatibility alias for Spark
133
- * @deprecated Use Spark instead
134
- * @type {typeof Spark}
135
- */
136
- App, // Deprecated: Use Spark instead
137
-
138
- /**
139
- * Base Application class
140
- * @type {typeof Application}
141
- */
142
- Application,
143
-
144
- /**
145
- * Router class for handling routes
146
- * @type {typeof Router}
147
- */
148
- Router,
149
-
150
- /**
151
- * Context class for request/response handling
152
- * @type {typeof Context}
153
- */
154
- Context,
155
-
156
- /**
157
- * Middleware collection
158
- * @namespace middleware
159
- * @property {Function} bodyParser - Body parsing middleware
160
- * @property {Function} cors - CORS middleware
161
- * @property {Function} compression - Response compression middleware
162
- * @property {Function} static - Static file serving middleware
163
- * @property {Function} session - Session management middleware
164
- * @property {Function} rateLimit - Rate limiting middleware
165
- * @property {Function} security - Security headers middleware
166
- * @property {Function} healthCheck - Health check endpoint middleware
167
- * @property {Function} metrics - Metrics collection middleware
168
- * @property {Function} logger - Request logging middleware
169
- */
170
- middleware: {
171
- bodyParser,
172
- cors,
173
- compression,
174
- static: staticFiles,
175
- session,
176
- rateLimit,
177
- security,
178
- helmet: security.helmet || security, // Alias for security middleware
179
- healthCheck,
180
- health: healthCheck, // Alias
181
- metrics,
182
- logger,
183
- cache: require('./middleware/cache'), // Add cache middleware
184
- compress: compression // Alias for compression
185
- },
186
-
187
- /**
188
- * Body parsing middleware - Direct export
189
- * @type {Function}
190
- */
191
- bodyParser,
192
-
193
- /**
194
- * CORS middleware - Direct export
195
- * @type {Function}
196
- */
197
- cors,
198
-
199
- /**
200
- * Compression middleware - Direct export
201
- * @type {Function}
202
- */
203
- compression,
204
-
205
- /**
206
- * Static file middleware - Direct export
207
- * @type {Function}
208
- */
209
- static: staticFiles,
210
-
211
- /**
212
- * Session middleware - Direct export
213
- * @type {Function}
214
- */
215
- session,
216
-
217
- /**
218
- * Rate limiting middleware - Direct export
219
- * @type {Function}
220
- */
221
- rateLimit,
222
-
223
- /**
224
- * Security headers middleware - Direct export
225
- * @type {Function}
226
- */
227
- security,
228
-
229
- /**
230
- * Health check middleware - Direct export
231
- * @type {Function}
232
- */
233
- healthCheck,
234
-
235
- /**
236
- * Metrics collection middleware - Direct export
237
- * @type {Function}
238
- */
239
- metrics,
240
-
241
- /**
242
- * Logger middleware - Direct export
243
- * @type {Function}
244
- */
245
- logger,
246
-
247
- /**
248
- * Error handling utilities
249
- * @namespace errorHandling
250
- * @property {Function} asyncHandler - Wraps async functions for error handling
251
- * @property {Function} errorHandler - Global error handler middleware
252
- * @property {Function} createError - Creates custom errors with status codes
253
- * @property {Object} errors - Predefined error types
254
- */
255
- errorHandling: {
256
- asyncHandler,
257
- errorHandler,
258
- createError,
259
- errors
260
- },
261
-
262
- /**
263
- * Utility classes and functions
264
- * @namespace utils
265
- * @property {Class} ContextPool - Object pool for Context instances
266
- * @property {Class} RegexValidator - Regex validation utilities
267
- * @property {Class} SafeRegexCache - Safe regex caching system
268
- */
269
- utils: {
270
- ContextPool,
271
- RegexValidator,
272
- SafeRegexCache
273
- }
274
- };
275
-
276
- /**
277
- * Default export for ES6 compatibility
278
- * @type {Object}
279
- */
280
- 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;