@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/middleware/cache.js
CHANGED
|
@@ -1,171 +1,180 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
*
|
|
10
|
-
* @param {
|
|
11
|
-
* @param {
|
|
12
|
-
* @param {
|
|
13
|
-
* @
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
const
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
ctx.
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
//
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* Default
|
|
82
|
-
* @param {Object} ctx - Koa context
|
|
83
|
-
* @returns {
|
|
84
|
-
*/
|
|
85
|
-
function
|
|
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
|
-
this.cache.delete(
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
this.cache.
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
cache.
|
|
170
|
-
|
|
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
|
+
// Bounded by default: an unbounded Map keyed by URL lets clients exhaust
|
|
23
|
+
// memory with unique query strings.
|
|
24
|
+
store = new MemoryCacheStore(options.maxEntries || 1000)
|
|
25
|
+
} = options;
|
|
26
|
+
|
|
27
|
+
return async function cacheMiddleware(ctx, next) {
|
|
28
|
+
// Only cache GET and HEAD requests
|
|
29
|
+
if (ctx.method !== 'GET' && ctx.method !== 'HEAD') {
|
|
30
|
+
return next();
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Check if caching should be applied
|
|
34
|
+
if (!condition(ctx)) {
|
|
35
|
+
return next();
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const cacheKey = key(ctx);
|
|
39
|
+
const cached = store.get(cacheKey);
|
|
40
|
+
|
|
41
|
+
if (cached && cached.expires > Date.now()) {
|
|
42
|
+
ctx.status(cached.status);
|
|
43
|
+
for (const [name, value] of Object.entries(cached.headers)) {
|
|
44
|
+
ctx.set(name, value);
|
|
45
|
+
}
|
|
46
|
+
ctx.set('X-Cache', 'HIT');
|
|
47
|
+
ctx.set('Age', Math.floor((Date.now() - cached.created) / 1000));
|
|
48
|
+
ctx.send(cached.body);
|
|
49
|
+
return undefined;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Capture what the handler sends (ctx.body holds the request body)
|
|
53
|
+
const originalSend = ctx.send;
|
|
54
|
+
ctx.send = function(body) {
|
|
55
|
+
if (!ctx.responded && ctx.statusCode >= 200 && ctx.statusCode < 300 &&
|
|
56
|
+
(typeof body === 'string' || Buffer.isBuffer(body)) &&
|
|
57
|
+
!ctx.getHeader('set-cookie') && !/no-store|private/i.test(String(ctx.getHeader('cache-control') || ''))) {
|
|
58
|
+
ctx.set('Cache-Control', `public, max-age=${maxAge}`);
|
|
59
|
+
ctx.set('X-Cache', 'MISS');
|
|
60
|
+
store.set(cacheKey, {
|
|
61
|
+
status: ctx.statusCode,
|
|
62
|
+
body,
|
|
63
|
+
headers: filterHeaders(ctx.responseHeaders),
|
|
64
|
+
created: Date.now(),
|
|
65
|
+
expires: Date.now() + (maxAge * 1000)
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
return originalSend.call(this, body);
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
try {
|
|
72
|
+
await next();
|
|
73
|
+
} finally {
|
|
74
|
+
ctx.send = originalSend;
|
|
75
|
+
}
|
|
76
|
+
return undefined;
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Default cache key generator
|
|
82
|
+
* @param {Object} ctx - Koa context
|
|
83
|
+
* @returns {string} Cache key
|
|
84
|
+
*/
|
|
85
|
+
function defaultKeyGenerator(ctx) {
|
|
86
|
+
return ctx.originalUrl || ctx.url.toString();
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Default condition to determine if response should be cached
|
|
91
|
+
* @param {Object} ctx - Koa context
|
|
92
|
+
* @returns {boolean} Whether to cache the response
|
|
93
|
+
*/
|
|
94
|
+
function defaultCondition(ctx) {
|
|
95
|
+
// Don't cache if no-cache header is present
|
|
96
|
+
const cacheControl = ctx.get('Cache-Control');
|
|
97
|
+
if (cacheControl && cacheControl.includes('no-cache')) {
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Don't cache authenticated requests by default
|
|
102
|
+
if (ctx.get('Authorization')) {
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return true;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Filter headers to store in cache
|
|
111
|
+
* @param {Object} headers - Response headers
|
|
112
|
+
* @returns {Object} Filtered headers
|
|
113
|
+
*/
|
|
114
|
+
function filterHeaders(headers) {
|
|
115
|
+
const filtered = {};
|
|
116
|
+
const allowedHeaders = [
|
|
117
|
+
'content-type',
|
|
118
|
+
'content-encoding',
|
|
119
|
+
'content-language',
|
|
120
|
+
'vary',
|
|
121
|
+
'etag',
|
|
122
|
+
'last-modified'
|
|
123
|
+
];
|
|
124
|
+
|
|
125
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
126
|
+
if (allowedHeaders.includes(key.toLowerCase())) {
|
|
127
|
+
filtered[key] = value;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return filtered;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Memory cache store with size limits
|
|
136
|
+
*/
|
|
137
|
+
class MemoryCacheStore {
|
|
138
|
+
constructor(maxSize = 100) {
|
|
139
|
+
this.cache = new Map();
|
|
140
|
+
this.maxSize = maxSize;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
get(key) {
|
|
144
|
+
const entry = this.cache.get(key);
|
|
145
|
+
if (entry && entry.expires > Date.now()) {
|
|
146
|
+
// Move to end (LRU)
|
|
147
|
+
this.cache.delete(key);
|
|
148
|
+
this.cache.set(key, entry);
|
|
149
|
+
return entry;
|
|
150
|
+
}
|
|
151
|
+
// Remove expired entry
|
|
152
|
+
if (entry) {
|
|
153
|
+
this.cache.delete(key);
|
|
154
|
+
}
|
|
155
|
+
return null;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
set(key, value) {
|
|
159
|
+
// Remove oldest entry if at capacity
|
|
160
|
+
if (this.cache.size >= this.maxSize && !this.cache.has(key)) {
|
|
161
|
+
const firstKey = this.cache.keys().next().value;
|
|
162
|
+
this.cache.delete(firstKey);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
this.cache.set(key, value);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
clear() {
|
|
169
|
+
this.cache.clear();
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
size() {
|
|
173
|
+
return this.cache.size;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// Export factory function and store class
|
|
178
|
+
cache.MemoryCacheStore = MemoryCacheStore;
|
|
179
|
+
|
|
171
180
|
module.exports = cache;
|