@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
@@ -1,528 +0,0 @@
1
- 'use strict';
2
-
3
- /**
4
- * @fileoverview Body parsing middleware for Spark Framework
5
- * @author Spark Framework Team
6
- * @since 1.0.0
7
- * @version 1.0.0
8
- */
9
-
10
- const querystring = require('querystring');
11
- const { Readable } = require('stream');
12
-
13
- /** @constant {number} Default size limit for request bodies (1MB) */
14
- const DEFAULT_LIMIT = 1024 * 1024; // 1MB
15
-
16
- /** @constant {boolean} Default extended parsing setting */
17
- const DEFAULT_EXTENDED = true;
18
-
19
- /**
20
- * Create a body parsing middleware
21
- *
22
- * Automatically parses request bodies based on Content-Type header.
23
- * Supports JSON, URL-encoded, multipart/form-data, text, and raw binary data.
24
- * Provides security features like size limits and content validation.
25
- *
26
- * @param {Object} [options={}] - Body parser configuration options
27
- * @param {number} [options.limit=1048576] - Maximum request body size in bytes (default: 1MB)
28
- * @param {boolean} [options.extended=true] - Use extended URL encoding parsing
29
- * @param {string} [options.type='auto'] - Force parsing type ('auto', 'json', 'urlencoded', 'multipart', 'text', 'raw')
30
- * @param {string} [options.encoding='utf8'] - Text encoding for parsing
31
- * @param {boolean} [options.inflate=true] - Enable gzip/deflate decompression
32
- * @param {boolean} [options.strict=true] - Enable strict parsing
33
- * @param {Function} [options.verify] - Custom verification function
34
- * @returns {Function} Express-style middleware function
35
- *
36
- * @since 1.0.0
37
- *
38
- * @example
39
- * // Auto-detect content type
40
- * app.use(bodyParser());
41
- *
42
- * @example
43
- * // JSON only with custom limit
44
- * app.use(bodyParser({
45
- * type: 'json',
46
- * limit: 1024 * 512 // 512KB
47
- * }));
48
- *
49
- * @example
50
- * // With verification
51
- * app.use(bodyParser({
52
- * verify: (req, res, body) => {
53
- * if (body.length > 1000) {
54
- * throw new Error('Body too large');
55
- * }
56
- * }
57
- * }));
58
- *
59
- * @example
60
- * // Multiple content types
61
- * app.use(bodyParser({ type: 'json' }));
62
- * app.use(bodyParser({ type: 'urlencoded' }));
63
- * app.use(bodyParser({ type: 'text' }));
64
- */
65
- function bodyParser(options = {}) {
66
- const opts = {
67
- limit: options.limit || DEFAULT_LIMIT,
68
- extended: options.extended !== false,
69
- type: options.type || 'auto',
70
- encoding: options.encoding || 'utf8',
71
- inflate: options.inflate !== false,
72
- strict: options.strict !== false,
73
- verify: options.verify,
74
- ...options
75
- };
76
-
77
- return async (ctx, next) => {
78
- if (ctx.body !== null || ctx.method === 'GET' || ctx.method === 'HEAD') {
79
- return next();
80
- }
81
-
82
- const contentType = ctx.get('content-type') || '';
83
- const contentLength = parseInt(ctx.get('content-length')) || 0;
84
-
85
- if (contentLength > opts.limit) {
86
- ctx.status(413).json({ error: 'Request entity too large' });
87
- return;
88
- }
89
-
90
- try {
91
- if (shouldParseJson(contentType, opts.type)) {
92
- await parseJson(ctx, opts);
93
- } else if (shouldParseUrlencoded(contentType, opts.type)) {
94
- await parseUrlencoded(ctx, opts);
95
- } else if (shouldParseMultipart(contentType, opts.type)) {
96
- await parseMultipart(ctx, opts);
97
- } else if (shouldParseText(contentType, opts.type)) {
98
- await parseText(ctx, opts);
99
- } else if (shouldParseRaw(contentType, opts.type)) {
100
- await parseRaw(ctx, opts);
101
- }
102
-
103
- if (opts.verify) {
104
- await Promise.resolve(opts.verify(ctx.req, ctx.res, ctx.body));
105
- }
106
-
107
- await next();
108
- } catch (error) {
109
- ctx.status(400).json({
110
- error: 'Bad Request',
111
- message: error.message,
112
- type: error.name
113
- });
114
- }
115
- };
116
- }
117
-
118
- /**
119
- * Check if content should be parsed as JSON
120
- *
121
- * @param {string} contentType - Request Content-Type header
122
- * @param {string} type - Parser type setting
123
- * @returns {boolean} Whether to parse as JSON
124
- *
125
- * @private
126
- * @since 1.0.0
127
- */
128
- function shouldParseJson(contentType, type) {
129
- if (type === 'json') return true;
130
- if (type === 'auto') return contentType.includes('application/json');
131
- return false;
132
- }
133
-
134
- /**
135
- * Check if content should be parsed as URL-encoded
136
- *
137
- * @param {string} contentType - Request Content-Type header
138
- * @param {string} type - Parser type setting
139
- * @returns {boolean} Whether to parse as URL-encoded
140
- *
141
- * @private
142
- * @since 1.0.0
143
- */
144
- function shouldParseUrlencoded(contentType, type) {
145
- if (type === 'urlencoded') return true;
146
- if (type === 'auto') return contentType.includes('application/x-www-form-urlencoded');
147
- return false;
148
- }
149
-
150
- /**
151
- * Check if content should be parsed as multipart
152
- *
153
- * @param {string} contentType - Request Content-Type header
154
- * @param {string} type - Parser type setting
155
- * @returns {boolean} Whether to parse as multipart
156
- *
157
- * @private
158
- * @since 1.0.0
159
- */
160
- function shouldParseMultipart(contentType, type) {
161
- if (type === 'multipart') return true;
162
- if (type === 'auto') return contentType.includes('multipart/form-data');
163
- return false;
164
- }
165
-
166
- /**
167
- * Check if content should be parsed as text
168
- *
169
- * @param {string} contentType - Request Content-Type header
170
- * @param {string} type - Parser type setting
171
- * @returns {boolean} Whether to parse as text
172
- *
173
- * @private
174
- * @since 1.0.0
175
- */
176
- function shouldParseText(contentType, type) {
177
- if (type === 'text') return true;
178
- if (type === 'auto') return contentType.includes('text/');
179
- return false;
180
- }
181
-
182
- /**
183
- * Check if content should be parsed as raw binary
184
- *
185
- * @param {string} contentType - Request Content-Type header
186
- * @param {string} type - Parser type setting
187
- * @returns {boolean} Whether to parse as raw binary
188
- *
189
- * @private
190
- * @since 1.0.0
191
- */
192
- function shouldParseRaw(contentType, type) {
193
- return type === 'raw';
194
- }
195
-
196
- /**
197
- * Parse request body as JSON
198
- *
199
- * @param {Context} ctx - Request context
200
- * @param {Object} opts - Parser options
201
- * @returns {Promise<void>}
202
- *
203
- * @throws {Error} When JSON parsing fails
204
- *
205
- * @private
206
- * @since 1.0.0
207
- */
208
- async function parseJson(ctx, opts) {
209
- const body = await readBody(ctx.req, opts);
210
-
211
- try {
212
- ctx.body = JSON.parse(body);
213
- } catch (error) {
214
- throw new Error(`Invalid JSON: ${error.message}`);
215
- }
216
- }
217
-
218
- /**
219
- * Parse request body as URL-encoded form data
220
- *
221
- * @param {Context} ctx - Request context
222
- * @param {Object} opts - Parser options
223
- * @returns {Promise<void>}
224
- *
225
- * @private
226
- * @since 1.0.0
227
- */
228
- async function parseUrlencoded(ctx, opts) {
229
- const body = await readBody(ctx.req, opts);
230
- ctx.body = querystring.parse(body);
231
- }
232
-
233
- async function parseMultipart(ctx, opts) {
234
- const contentType = ctx.get('content-type');
235
- const boundary = getBoundary(contentType);
236
-
237
- if (!boundary) {
238
- throw new Error('Invalid multipart/form-data: missing boundary');
239
- }
240
-
241
- const chunks = [];
242
- let size = 0;
243
-
244
- await new Promise((resolve, reject) => {
245
- const onData = (chunk) => {
246
- size += chunk.length;
247
- if (size > opts.limit) {
248
- ctx.req.removeListener('data', onData);
249
- ctx.req.removeListener('end', onEnd);
250
- ctx.req.removeListener('error', onError);
251
- return reject(new Error('Request entity too large'));
252
- }
253
- chunks.push(chunk);
254
- };
255
-
256
- const onEnd = () => {
257
- ctx.req.removeListener('data', onData);
258
- ctx.req.removeListener('error', onError);
259
- resolve();
260
- };
261
-
262
- const onError = (error) => {
263
- ctx.req.removeListener('data', onData);
264
- ctx.req.removeListener('end', onEnd);
265
- reject(error);
266
- };
267
-
268
- ctx.req.on('data', onData);
269
- ctx.req.on('end', onEnd);
270
- ctx.req.on('error', onError);
271
- });
272
-
273
- const body = Buffer.concat(chunks).toString('binary');
274
- const parsed = parseMultipartData(body, boundary);
275
-
276
- ctx.body = parsed.fields;
277
- ctx.files = parsed.files;
278
- }
279
-
280
- /**
281
- * Parse request body as plain text
282
- *
283
- * @param {Context} ctx - Request context
284
- * @param {Object} opts - Parser options
285
- * @returns {Promise<void>}
286
- *
287
- * @private
288
- * @since 1.0.0
289
- */
290
- async function parseText(ctx, opts) {
291
- ctx.body = await readBody(ctx.req, opts);
292
- }
293
-
294
- /**
295
- * Parse request body as raw binary data
296
- *
297
- * @param {Context} ctx - Request context
298
- * @param {Object} opts - Parser options
299
- * @returns {Promise<void>}
300
- *
301
- * @throws {Error} When request is too large
302
- *
303
- * @private
304
- * @since 1.0.0
305
- */
306
- async function parseRaw(ctx, opts) {
307
- const chunks = [];
308
-
309
- return new Promise((resolve, reject) => {
310
- let size = 0;
311
-
312
- const onData = (chunk) => {
313
- size += chunk.length;
314
- if (size > opts.limit) {
315
- ctx.req.removeListener('data', onData);
316
- ctx.req.removeListener('end', onEnd);
317
- ctx.req.removeListener('error', onError);
318
- return reject(new Error('Request entity too large'));
319
- }
320
- chunks.push(chunk);
321
- };
322
-
323
- const onEnd = () => {
324
- ctx.req.removeListener('data', onData);
325
- ctx.req.removeListener('error', onError);
326
- ctx.body = Buffer.concat(chunks);
327
- resolve();
328
- };
329
-
330
- const onError = (error) => {
331
- ctx.req.removeListener('data', onData);
332
- ctx.req.removeListener('end', onEnd);
333
- reject(error);
334
- };
335
-
336
- ctx.req.on('data', onData);
337
- ctx.req.on('end', onEnd);
338
- ctx.req.on('error', onError);
339
- });
340
- }
341
-
342
- /**
343
- * Read and decode request body as text
344
- *
345
- * @param {http.IncomingMessage} req - HTTP request object
346
- * @param {Object} opts - Parser options with encoding and limit
347
- * @returns {Promise<string>} Promise resolving to decoded body text
348
- *
349
- * @throws {Error} When request is too large
350
- *
351
- * @private
352
- * @since 1.0.0
353
- */
354
- function readBody(req, opts) {
355
- return new Promise((resolve, reject) => {
356
- const chunks = [];
357
- let size = 0;
358
-
359
- const onData = (chunk) => {
360
- size += chunk.length;
361
- if (size > opts.limit) {
362
- req.removeListener('data', onData);
363
- req.removeListener('end', onEnd);
364
- req.removeListener('error', onError);
365
- return reject(new Error('Request entity too large'));
366
- }
367
- chunks.push(chunk);
368
- };
369
-
370
- const onEnd = () => {
371
- req.removeListener('data', onData);
372
- req.removeListener('error', onError);
373
- const body = Buffer.concat(chunks).toString(opts.encoding || 'utf8');
374
- resolve(body);
375
- };
376
-
377
- const onError = (error) => {
378
- req.removeListener('data', onData);
379
- req.removeListener('end', onEnd);
380
- reject(error);
381
- };
382
-
383
- req.on('data', onData);
384
- req.on('end', onEnd);
385
- req.on('error', onError);
386
- });
387
- }
388
-
389
- /**
390
- * Extract boundary from multipart Content-Type header
391
- *
392
- * @param {string} contentType - Content-Type header value
393
- * @returns {string|null} Boundary string or null if not found
394
- *
395
- * @private
396
- * @since 1.0.0
397
- */
398
- function getBoundary(contentType) {
399
- const match = contentType.match(/boundary=([^;]+)/);
400
- if (!match || !match[1]) return null;
401
-
402
- // Remove quotes if present
403
- return match[1].replace(/^["']|["']$/g, '');
404
- }
405
-
406
- /**
407
- * Parse multipart/form-data body content
408
- *
409
- * @param {string} body - Raw multipart body as binary string
410
- * @param {string} boundary - Multipart boundary string
411
- * @returns {Object} Object with fields and files properties
412
- * @returns {Object} returns.fields - Parsed form fields
413
- * @returns {Object} returns.files - Parsed file uploads with metadata
414
- *
415
- * @private
416
- * @since 1.0.0
417
- */
418
- function parseMultipartData(body, boundary) {
419
- const fields = {};
420
- const files = {};
421
-
422
- const parts = body.split(`--${boundary}`);
423
-
424
- for (let i = 1; i < parts.length - 1; i++) {
425
- const part = parts[i];
426
- const [headers, content] = part.split('\r\n\r\n');
427
-
428
- if (!headers || content === undefined) continue;
429
-
430
- const headerLines = headers.split('\r\n');
431
- const contentDisposition = headerLines.find(line =>
432
- line.startsWith('Content-Disposition:')
433
- );
434
-
435
- if (!contentDisposition) continue;
436
-
437
- const nameMatch = contentDisposition.match(/name="([^"]+)"/);
438
- const filenameMatch = contentDisposition.match(/filename="([^"]+)"/);
439
-
440
- if (!nameMatch) continue;
441
-
442
- const name = nameMatch[1];
443
- const filename = filenameMatch ? filenameMatch[1] : null;
444
-
445
- if (filename) {
446
- const contentType = headerLines.find(line =>
447
- line.startsWith('Content-Type:')
448
- );
449
-
450
- files[name] = {
451
- filename,
452
- contentType: contentType ? contentType.split(': ')[1] : 'application/octet-stream',
453
- size: Buffer.byteLength(content.slice(0, -2)),
454
- data: Buffer.from(content.slice(0, -2))
455
- };
456
- } else {
457
- fields[name] = content.slice(0, -2);
458
- }
459
- }
460
-
461
- return { fields, files };
462
- }
463
-
464
- /**
465
- * Create a JSON body parser middleware
466
- *
467
- * @param {Object} [options] - Parser options (merged with type: 'json')
468
- * @returns {Function} Middleware function for parsing JSON bodies
469
- *
470
- * @since 1.0.0
471
- *
472
- * @example
473
- * app.use(json({ limit: '10mb' }));
474
- */
475
- function json(options) {
476
- return bodyParser({ ...options, type: 'json' });
477
- }
478
-
479
- /**
480
- * Create a URL-encoded body parser middleware
481
- *
482
- * @param {Object} [options] - Parser options (merged with type: 'urlencoded')
483
- * @returns {Function} Middleware function for parsing URL-encoded bodies
484
- *
485
- * @since 1.0.0
486
- *
487
- * @example
488
- * app.use(urlencoded({ extended: true }));
489
- */
490
- function urlencoded(options) {
491
- return bodyParser({ ...options, type: 'urlencoded' });
492
- }
493
-
494
- /**
495
- * Create a text body parser middleware
496
- *
497
- * @param {Object} [options] - Parser options (merged with type: 'text')
498
- * @returns {Function} Middleware function for parsing text bodies
499
- *
500
- * @since 1.0.0
501
- *
502
- * @example
503
- * app.use(text({ encoding: 'utf16' }));
504
- */
505
- function text(options) {
506
- return bodyParser({ ...options, type: 'text' });
507
- }
508
-
509
- /**
510
- * Create a raw binary body parser middleware
511
- *
512
- * @param {Object} [options] - Parser options (merged with type: 'raw')
513
- * @returns {Function} Middleware function for parsing raw binary bodies
514
- *
515
- * @since 1.0.0
516
- *
517
- * @example
518
- * app.use(raw({ limit: '5mb' }));
519
- */
520
- function raw(options) {
521
- return bodyParser({ ...options, type: 'raw' });
522
- }
523
-
524
- module.exports = bodyParser;
525
- module.exports.json = json;
526
- module.exports.urlencoded = urlencoded;
527
- module.exports.text = text;
528
- module.exports.raw = raw;
@@ -1,173 +0,0 @@
1
- 'use strict';
2
-
3
- /**
4
- * Cache middleware for @oxog/spark
5
- * Provides response caching functionality
6
- */
7
-
8
- /**
9
- * Creates a cache middleware
10
- * @param {Object} options - Cache configuration options
11
- * @param {number} [options.maxAge=3600] - Cache max age in seconds
12
- * @param {Function} [options.key] - Function to generate cache key
13
- * @param {Function} [options.condition] - Function to determine if response should be cached
14
- * @param {Object} [options.store] - Custom cache store (default: in-memory)
15
- * @returns {Function} Cache middleware function
16
- */
17
- function cache(options = {}) {
18
- const {
19
- maxAge = 3600,
20
- key = defaultKeyGenerator,
21
- condition = defaultCondition,
22
- store = new Map()
23
- } = options;
24
-
25
- return async function cacheMiddleware(ctx, next) {
26
- // Only cache GET and HEAD requests
27
- if (ctx.method !== 'GET' && ctx.method !== 'HEAD') {
28
- return next();
29
- }
30
-
31
- // Check if caching should be applied
32
- if (!condition(ctx)) {
33
- return next();
34
- }
35
-
36
- // Generate cache key
37
- const cacheKey = key(ctx);
38
-
39
- // Check if cached response exists
40
- const cached = store.get(cacheKey);
41
- if (cached && cached.expires > Date.now()) {
42
- // Serve from cache
43
- ctx.status = cached.status;
44
- ctx.body = cached.body;
45
- ctx.set(cached.headers);
46
- ctx.set('X-Cache', 'HIT');
47
- ctx.set('Age', Math.floor((Date.now() - cached.created) / 1000));
48
- return;
49
- }
50
-
51
- // Not in cache, proceed with request
52
- await next();
53
-
54
- // Store response in cache if successful
55
- if (ctx.status >= 200 && ctx.status < 300 && ctx.body) {
56
- const cacheEntry = {
57
- status: ctx.status,
58
- body: ctx.body,
59
- headers: filterHeaders(ctx.headers),
60
- created: Date.now(),
61
- expires: Date.now() + (maxAge * 1000)
62
- };
63
-
64
- store.set(cacheKey, cacheEntry);
65
-
66
- // Set cache headers
67
- ctx.set('Cache-Control', `public, max-age=${maxAge}`);
68
- ctx.set('X-Cache', 'MISS');
69
- }
70
- };
71
- }
72
-
73
- /**
74
- * Default cache key generator
75
- * @param {Object} ctx - Koa context
76
- * @returns {string} Cache key
77
- */
78
- function defaultKeyGenerator(ctx) {
79
- return ctx.url;
80
- }
81
-
82
- /**
83
- * Default condition to determine if response should be cached
84
- * @param {Object} ctx - Koa context
85
- * @returns {boolean} Whether to cache the response
86
- */
87
- function defaultCondition(ctx) {
88
- // Don't cache if no-cache header is present
89
- const cacheControl = ctx.get('Cache-Control');
90
- if (cacheControl && cacheControl.includes('no-cache')) {
91
- return false;
92
- }
93
-
94
- // Don't cache authenticated requests by default
95
- if (ctx.get('Authorization')) {
96
- return false;
97
- }
98
-
99
- return true;
100
- }
101
-
102
- /**
103
- * Filter headers to store in cache
104
- * @param {Object} headers - Response headers
105
- * @returns {Object} Filtered headers
106
- */
107
- function filterHeaders(headers) {
108
- const filtered = {};
109
- const allowedHeaders = [
110
- 'content-type',
111
- 'content-encoding',
112
- 'content-language',
113
- 'vary',
114
- 'etag',
115
- 'last-modified'
116
- ];
117
-
118
- for (const [key, value] of Object.entries(headers)) {
119
- if (allowedHeaders.includes(key.toLowerCase())) {
120
- filtered[key] = value;
121
- }
122
- }
123
-
124
- return filtered;
125
- }
126
-
127
- /**
128
- * Memory cache store with size limits
129
- */
130
- class MemoryCacheStore {
131
- constructor(maxSize = 100) {
132
- this.cache = new Map();
133
- this.maxSize = maxSize;
134
- }
135
-
136
- get(key) {
137
- const entry = this.cache.get(key);
138
- if (entry && entry.expires > Date.now()) {
139
- // Move to end (LRU)
140
- this.cache.delete(key);
141
- this.cache.set(key, entry);
142
- return entry;
143
- }
144
- // Remove expired entry
145
- if (entry) {
146
- this.cache.delete(key);
147
- }
148
- return null;
149
- }
150
-
151
- set(key, value) {
152
- // Remove oldest entry if at capacity
153
- if (this.cache.size >= this.maxSize && !this.cache.has(key)) {
154
- const firstKey = this.cache.keys().next().value;
155
- this.cache.delete(firstKey);
156
- }
157
-
158
- this.cache.set(key, value);
159
- }
160
-
161
- clear() {
162
- this.cache.clear();
163
- }
164
-
165
- size() {
166
- return this.cache.size;
167
- }
168
- }
169
-
170
- // Export factory function and store class
171
- cache.MemoryCacheStore = MemoryCacheStore;
172
-
173
- module.exports = cache;