@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.
Files changed (64) hide show
  1. package/CHANGELOG.md +632 -293
  2. package/LICENSE +20 -20
  3. package/README.md +288 -241
  4. package/package.json +83 -89
  5. package/src/core/application.js +1070 -956
  6. package/src/core/context.js +1100 -830
  7. package/src/core/middleware.js +305 -217
  8. package/src/index.js +183 -278
  9. package/src/middleware/body-parser.js +484 -526
  10. package/src/middleware/cache.js +179 -170
  11. package/src/middleware/compression.js +179 -182
  12. package/src/middleware/cors.js +178 -95
  13. package/src/middleware/health.js +221 -218
  14. package/src/middleware/logger.js +197 -194
  15. package/src/middleware/metrics.js +341 -327
  16. package/src/middleware/rate-limit.js +369 -319
  17. package/src/middleware/security.js +444 -332
  18. package/src/middleware/session.js +649 -529
  19. package/src/middleware/static.js +406 -299
  20. package/src/router/layer.js +199 -117
  21. package/src/router/route.js +151 -123
  22. package/src/router/router.js +705 -755
  23. package/src/utils/async-handler.js +147 -144
  24. package/src/utils/context-pool.js +85 -82
  25. package/src/utils/regex-validator.js +189 -176
  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 -957
  31. package/dist/core/context.js +0 -831
  32. package/dist/core/middleware.js +0 -218
  33. package/dist/core/request.js +0 -333
  34. package/dist/core/response.js +0 -375
  35. package/dist/index.js +0 -278
  36. package/dist/middleware/body-parser.js +0 -526
  37. package/dist/middleware/cache.js +0 -171
  38. package/dist/middleware/compression.js +0 -183
  39. package/dist/middleware/cors.js +0 -95
  40. package/dist/middleware/health.js +0 -219
  41. package/dist/middleware/index.js +0 -21
  42. package/dist/middleware/logger.js +0 -195
  43. package/dist/middleware/metrics.js +0 -328
  44. package/dist/middleware/rate-limit.js +0 -320
  45. package/dist/middleware/security.js +0 -333
  46. package/dist/middleware/session.js +0 -530
  47. package/dist/middleware/static.js +0 -299
  48. package/dist/package.json +0 -62
  49. package/dist/router/layer.js +0 -117
  50. package/dist/router/route.js +0 -123
  51. package/dist/router/router.js +0 -756
  52. package/dist/types/index.d.ts +0 -291
  53. package/dist/utils/async-handler.js +0 -145
  54. package/dist/utils/cache.js +0 -441
  55. package/dist/utils/context-pool.js +0 -83
  56. package/dist/utils/error-types.js +0 -132
  57. package/dist/utils/http.js +0 -413
  58. package/dist/utils/regex-validator.js +0 -177
  59. package/src/core/request.js +0 -333
  60. package/src/core/response.js +0 -375
  61. package/src/middleware/index.js +0 -21
  62. package/src/utils/cache.js +0 -441
  63. package/src/utils/error-types.js +0 -132
  64. package/src/utils/http.js +0 -413
package/src/utils/http.js DELETED
@@ -1,413 +0,0 @@
1
- const { STATUS_CODES } = require('http');
2
-
3
- const HTTP_STATUS = {
4
- CONTINUE: 100,
5
- SWITCHING_PROTOCOLS: 101,
6
- PROCESSING: 102,
7
- EARLY_HINTS: 103,
8
-
9
- OK: 200,
10
- CREATED: 201,
11
- ACCEPTED: 202,
12
- NON_AUTHORITATIVE_INFORMATION: 203,
13
- NO_CONTENT: 204,
14
- RESET_CONTENT: 205,
15
- PARTIAL_CONTENT: 206,
16
- MULTI_STATUS: 207,
17
- ALREADY_REPORTED: 208,
18
- IM_USED: 226,
19
-
20
- MULTIPLE_CHOICES: 300,
21
- MOVED_PERMANENTLY: 301,
22
- FOUND: 302,
23
- SEE_OTHER: 303,
24
- NOT_MODIFIED: 304,
25
- USE_PROXY: 305,
26
- TEMPORARY_REDIRECT: 307,
27
- PERMANENT_REDIRECT: 308,
28
-
29
- BAD_REQUEST: 400,
30
- UNAUTHORIZED: 401,
31
- PAYMENT_REQUIRED: 402,
32
- FORBIDDEN: 403,
33
- NOT_FOUND: 404,
34
- METHOD_NOT_ALLOWED: 405,
35
- NOT_ACCEPTABLE: 406,
36
- PROXY_AUTHENTICATION_REQUIRED: 407,
37
- REQUEST_TIMEOUT: 408,
38
- CONFLICT: 409,
39
- GONE: 410,
40
- LENGTH_REQUIRED: 411,
41
- PRECONDITION_FAILED: 412,
42
- PAYLOAD_TOO_LARGE: 413,
43
- URI_TOO_LONG: 414,
44
- UNSUPPORTED_MEDIA_TYPE: 415,
45
- RANGE_NOT_SATISFIABLE: 416,
46
- EXPECTATION_FAILED: 417,
47
- IM_A_TEAPOT: 418,
48
- MISDIRECTED_REQUEST: 421,
49
- UNPROCESSABLE_ENTITY: 422,
50
- LOCKED: 423,
51
- FAILED_DEPENDENCY: 424,
52
- TOO_EARLY: 425,
53
- UPGRADE_REQUIRED: 426,
54
- PRECONDITION_REQUIRED: 428,
55
- TOO_MANY_REQUESTS: 429,
56
- REQUEST_HEADER_FIELDS_TOO_LARGE: 431,
57
- UNAVAILABLE_FOR_LEGAL_REASONS: 451,
58
-
59
- INTERNAL_SERVER_ERROR: 500,
60
- NOT_IMPLEMENTED: 501,
61
- BAD_GATEWAY: 502,
62
- SERVICE_UNAVAILABLE: 503,
63
- GATEWAY_TIMEOUT: 504,
64
- HTTP_VERSION_NOT_SUPPORTED: 505,
65
- VARIANT_ALSO_NEGOTIATES: 506,
66
- INSUFFICIENT_STORAGE: 507,
67
- LOOP_DETECTED: 508,
68
- NOT_EXTENDED: 510,
69
- NETWORK_AUTHENTICATION_REQUIRED: 511
70
- };
71
-
72
- const MIME_TYPES = {
73
- 'html': 'text/html',
74
- 'htm': 'text/html',
75
- 'txt': 'text/plain',
76
- 'css': 'text/css',
77
- 'js': 'application/javascript',
78
- 'json': 'application/json',
79
- 'xml': 'application/xml',
80
- 'pdf': 'application/pdf',
81
- 'zip': 'application/zip',
82
- 'tar': 'application/x-tar',
83
- 'gz': 'application/gzip',
84
- 'png': 'image/png',
85
- 'jpg': 'image/jpeg',
86
- 'jpeg': 'image/jpeg',
87
- 'gif': 'image/gif',
88
- 'svg': 'image/svg+xml',
89
- 'ico': 'image/x-icon',
90
- 'webp': 'image/webp',
91
- 'bmp': 'image/bmp',
92
- 'tiff': 'image/tiff',
93
- 'mp4': 'video/mp4',
94
- 'webm': 'video/webm',
95
- 'ogg': 'video/ogg',
96
- 'avi': 'video/x-msvideo',
97
- 'mov': 'video/quicktime',
98
- 'wmv': 'video/x-ms-wmv',
99
- 'flv': 'video/x-flv',
100
- 'mp3': 'audio/mpeg',
101
- 'wav': 'audio/wav',
102
- 'ogg': 'audio/ogg',
103
- 'aac': 'audio/aac',
104
- 'flac': 'audio/flac',
105
- 'woff': 'font/woff',
106
- 'woff2': 'font/woff2',
107
- 'ttf': 'font/ttf',
108
- 'otf': 'font/otf',
109
- 'eot': 'application/vnd.ms-fontobject'
110
- };
111
-
112
- const HTTP_METHODS = [
113
- 'GET',
114
- 'POST',
115
- 'PUT',
116
- 'DELETE',
117
- 'PATCH',
118
- 'HEAD',
119
- 'OPTIONS',
120
- 'TRACE',
121
- 'CONNECT'
122
- ];
123
-
124
- function isHttpError(status) {
125
- return status >= 400;
126
- }
127
-
128
- function isClientError(status) {
129
- return status >= 400 && status < 500;
130
- }
131
-
132
- function isServerError(status) {
133
- return status >= 500;
134
- }
135
-
136
- function isRedirect(status) {
137
- return status >= 300 && status < 400;
138
- }
139
-
140
- function isSuccess(status) {
141
- return status >= 200 && status < 300;
142
- }
143
-
144
- function isInformational(status) {
145
- return status >= 100 && status < 200;
146
- }
147
-
148
- function getStatusText(status) {
149
- return STATUS_CODES[status] || 'Unknown Status';
150
- }
151
-
152
- function getMimeType(extension) {
153
- if (extension.startsWith('.')) {
154
- extension = extension.slice(1);
155
- }
156
- return MIME_TYPES[extension.toLowerCase()] || 'application/octet-stream';
157
- }
158
-
159
- function parseContentType(contentType) {
160
- if (!contentType) {
161
- return { type: '', charset: '', boundary: '' };
162
- }
163
-
164
- const [type, ...params] = contentType.split(';');
165
- const result = { type: type.trim(), charset: '', boundary: '' };
166
-
167
- for (const param of params) {
168
- const [key, value] = param.split('=');
169
- if (key && value) {
170
- result[key.trim()] = value.trim().replace(/"/g, '');
171
- }
172
- }
173
-
174
- return result;
175
- }
176
-
177
- function parseAccept(accept) {
178
- if (!accept) {
179
- return [];
180
- }
181
-
182
- return accept
183
- .split(',')
184
- .map(type => {
185
- const [mediaType, ...params] = type.split(';');
186
- const result = { type: mediaType.trim(), q: 1.0 };
187
-
188
- for (const param of params) {
189
- const [key, value] = param.split('=');
190
- if (key && value) {
191
- if (key.trim() === 'q') {
192
- result.q = parseFloat(value.trim());
193
- }
194
- }
195
- }
196
-
197
- return result;
198
- })
199
- .sort((a, b) => b.q - a.q);
200
- }
201
-
202
- function parseRange(range, size) {
203
- if (!range || !range.startsWith('bytes=')) {
204
- return null;
205
- }
206
-
207
- const ranges = [];
208
- const parts = range.slice(6).split(',');
209
-
210
- for (const part of parts) {
211
- const [start, end] = part.trim().split('-');
212
- const startNum = parseInt(start);
213
- const endNum = parseInt(end);
214
-
215
- if (isNaN(startNum) && isNaN(endNum)) {
216
- continue;
217
- }
218
-
219
- if (isNaN(startNum)) {
220
- ranges.push({ start: Math.max(0, size - endNum), end: size - 1 });
221
- } else if (isNaN(endNum)) {
222
- ranges.push({ start: startNum, end: size - 1 });
223
- } else {
224
- ranges.push({ start: startNum, end: Math.min(endNum, size - 1) });
225
- }
226
- }
227
-
228
- return ranges.length > 0 ? ranges : null;
229
- }
230
-
231
- function parseAuthorizationHeader(auth) {
232
- if (!auth) {
233
- return null;
234
- }
235
-
236
- const [scheme, credentials] = auth.split(' ');
237
-
238
- if (scheme.toLowerCase() === 'basic') {
239
- const [username, password] = Buffer.from(credentials, 'base64').toString().split(':');
240
- return { scheme: 'basic', username, password };
241
- }
242
-
243
- if (scheme.toLowerCase() === 'bearer') {
244
- return { scheme: 'bearer', token: credentials };
245
- }
246
-
247
- return { scheme: scheme.toLowerCase(), credentials };
248
- }
249
-
250
- function parseUserAgent(userAgent) {
251
- if (!userAgent) {
252
- return null;
253
- }
254
-
255
- const browsers = [
256
- { name: 'Chrome', pattern: /Chrome\/([^\s]+)/ },
257
- { name: 'Firefox', pattern: /Firefox\/([^\s]+)/ },
258
- { name: 'Safari', pattern: /Version\/([^\s]+).*Safari/ },
259
- { name: 'Edge', pattern: /Edge\/([^\s]+)/ },
260
- { name: 'IE', pattern: /MSIE ([^\s]+)/ },
261
- { name: 'Opera', pattern: /OPR\/([^\s]+)/ }
262
- ];
263
-
264
- for (const browser of browsers) {
265
- const match = userAgent.match(browser.pattern);
266
- if (match) {
267
- return { name: browser.name, version: match[1] };
268
- }
269
- }
270
-
271
- return { name: 'Unknown', version: 'Unknown' };
272
- }
273
-
274
- function createError(status, message, properties = {}) {
275
- const error = new Error(message || getStatusText(status));
276
- error.status = status;
277
- error.statusCode = status;
278
- error.expose = isClientError(status);
279
-
280
- Object.assign(error, properties);
281
-
282
- return error;
283
- }
284
-
285
- function isValidMethod(method) {
286
- return HTTP_METHODS.includes(method.toUpperCase());
287
- }
288
-
289
- function parseQuery(queryString) {
290
- if (!queryString) {
291
- return {};
292
- }
293
-
294
- const params = {};
295
- const pairs = queryString.split('&');
296
-
297
- for (const pair of pairs) {
298
- const [key, value] = pair.split('=');
299
- if (key) {
300
- const decodedKey = decodeURIComponent(key);
301
- const decodedValue = value ? decodeURIComponent(value) : '';
302
-
303
- if (params[decodedKey]) {
304
- if (Array.isArray(params[decodedKey])) {
305
- params[decodedKey].push(decodedValue);
306
- } else {
307
- params[decodedKey] = [params[decodedKey], decodedValue];
308
- }
309
- } else {
310
- params[decodedKey] = decodedValue;
311
- }
312
- }
313
- }
314
-
315
- return params;
316
- }
317
-
318
- function stringifyQuery(params) {
319
- if (!params || typeof params !== 'object') {
320
- return '';
321
- }
322
-
323
- const pairs = [];
324
-
325
- for (const [key, value] of Object.entries(params)) {
326
- if (Array.isArray(value)) {
327
- for (const item of value) {
328
- pairs.push(`${encodeURIComponent(key)}=${encodeURIComponent(item)}`);
329
- }
330
- } else if (value !== null && value !== undefined) {
331
- pairs.push(`${encodeURIComponent(key)}=${encodeURIComponent(value)}`);
332
- }
333
- }
334
-
335
- return pairs.join('&');
336
- }
337
-
338
- function parseUrl(url) {
339
- try {
340
- const parsed = new URL(url);
341
- return {
342
- protocol: parsed.protocol,
343
- hostname: parsed.hostname,
344
- port: parsed.port,
345
- pathname: parsed.pathname,
346
- search: parsed.search,
347
- hash: parsed.hash,
348
- query: parseQuery(parsed.search.slice(1))
349
- };
350
- } catch (error) {
351
- return null;
352
- }
353
- }
354
-
355
- function formatUrl(parts) {
356
- let url = '';
357
-
358
- if (parts.protocol) {
359
- url += parts.protocol;
360
- if (!parts.protocol.endsWith(':')) {
361
- url += ':';
362
- }
363
- }
364
-
365
- if (parts.hostname) {
366
- url += '//' + parts.hostname;
367
- }
368
-
369
- if (parts.port) {
370
- url += ':' + parts.port;
371
- }
372
-
373
- if (parts.pathname) {
374
- url += parts.pathname;
375
- }
376
-
377
- if (parts.query && Object.keys(parts.query).length > 0) {
378
- url += '?' + stringifyQuery(parts.query);
379
- } else if (parts.search) {
380
- url += parts.search;
381
- }
382
-
383
- if (parts.hash) {
384
- url += parts.hash;
385
- }
386
-
387
- return url;
388
- }
389
-
390
- module.exports = {
391
- HTTP_STATUS,
392
- MIME_TYPES,
393
- HTTP_METHODS,
394
- isHttpError,
395
- isClientError,
396
- isServerError,
397
- isRedirect,
398
- isSuccess,
399
- isInformational,
400
- getStatusText,
401
- getMimeType,
402
- parseContentType,
403
- parseAccept,
404
- parseRange,
405
- parseAuthorizationHeader,
406
- parseUserAgent,
407
- createError,
408
- isValidMethod,
409
- parseQuery,
410
- stringifyQuery,
411
- parseUrl,
412
- formatUrl
413
- };