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