@keq-request/cache 5.0.0-alpha.24 → 5.0.0-alpha.26
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 +20 -0
- package/dist/cache.d.ts +2 -3
- package/dist/cache.d.ts.map +1 -1
- package/dist/index.js +104 -26
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +104 -26
- package/dist/index.mjs.map +1 -1
- package/dist/request-cache-handler/index.d.ts +0 -1
- package/dist/request-cache-handler/index.d.ts.map +1 -1
- package/dist/request-cache-handler/request-cache-handler.d.ts +5 -4
- package/dist/request-cache-handler/request-cache-handler.d.ts.map +1 -1
- package/dist/strategies/cache-first.d.ts.map +1 -1
- package/dist/strategies/network-first.d.ts.map +1 -1
- package/dist/strategies/stale-while-revalidate.d.ts.map +1 -1
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/keq-cache-rule.d.ts +1 -1
- package/dist/types/keq-cache-rule.d.ts.map +1 -1
- package/dist/{request-cache-handler/types → types}/request-cache-options.d.ts +9 -1
- package/dist/types/request-cache-options.d.ts.map +1 -0
- package/package.json +3 -3
- package/dist/request-cache-handler/types/index.d.ts +0 -2
- package/dist/request-cache-handler/types/index.d.ts.map +0 -1
- package/dist/request-cache-handler/types/request-cache-options.d.ts.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,25 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 5.0.0-alpha.26
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Updated dependencies [22ce01a]
|
|
8
|
+
- Updated dependencies [63161c4]
|
|
9
|
+
- keq@5.0.0-alpha.26
|
|
10
|
+
|
|
11
|
+
## 5.0.0-alpha.25
|
|
12
|
+
|
|
13
|
+
### Minor Changes
|
|
14
|
+
|
|
15
|
+
- d1090d3: **Feat:** add debug logs and Server-Timing header.
|
|
16
|
+
|
|
17
|
+
### Patch Changes
|
|
18
|
+
|
|
19
|
+
- Updated dependencies [214ae66]
|
|
20
|
+
- Updated dependencies [9290139]
|
|
21
|
+
- keq@5.0.0-alpha.25
|
|
22
|
+
|
|
3
23
|
## 5.0.0-alpha.24
|
|
4
24
|
|
|
5
25
|
### Patch Changes
|
package/dist/cache.d.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import type { Keq, KeqMiddleware, KeqContext } from 'keq';
|
|
2
2
|
import { KeqCacheStorage } from './storage/keq-cache-storage.js';
|
|
3
|
-
import { RequestCacheOptions } from './
|
|
4
|
-
|
|
5
|
-
export interface KeqCacheOptions {
|
|
3
|
+
import { KeqCacheKeyFactory, KeqCacheRule, RequestCacheOptions } from './types/index.js';
|
|
4
|
+
export interface KeqCacheOptions extends Pick<RequestCacheOptions, 'serverTiming'> {
|
|
6
5
|
storage: KeqCacheStorage;
|
|
7
6
|
/**
|
|
8
7
|
* Cache Key Factory
|
package/dist/cache.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cache.d.ts","sourceRoot":"","sources":["../src/cache.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,GAAG,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,KAAK,CAAA;AACzD,OAAO,EAAE,eAAe,EAAE,MAAM,gCAAgC,CAAA;
|
|
1
|
+
{"version":3,"file":"cache.d.ts","sourceRoot":"","sources":["../src/cache.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,GAAG,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,KAAK,CAAA;AACzD,OAAO,EAAE,eAAe,EAAE,MAAM,gCAAgC,CAAA;AAEhE,OAAO,EAAE,kBAAkB,EAAE,YAAY,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAA;AAGxF,MAAM,WAAW,eAAgB,SAAQ,IAAI,CAAC,mBAAmB,EAAE,cAAc,CAAC;IAChF,OAAO,EAAE,eAAe,CAAA;IAExB;;OAEG;IACH,UAAU,CAAC,EAAE,kBAAkB,CAAA;IAE/B,KAAK,CAAC,EAAE,YAAY,EAAE,CAAA;CACvB;AAGD,OAAO,QAAQ,KAAK,CAAC;IACnB,UAAiB,oBAAoB,CAAC,EAAE;QACtC;;WAEG;QACH,KAAK,CAAC,MAAM,EAAE,mBAAmB,GAAG,KAAK,GAAG,GAAG,CAAC,EAAE,CAAC,CAAA;KACpD;IAED,UAAiB,SAAS;QACxB,WAAW,EAAE;YACX,GAAG,EAAE,MAAM,CAAA;YACX,QAAQ,EAAE,QAAQ,CAAA;YAClB,OAAO,EAAE,UAAU,CAAA;SACpB,CAAA;QAED,YAAY,EAAE;YACZ,GAAG,EAAE,MAAM,CAAA;YACX,OAAO,EAAE,UAAU,CAAA;SACpB,CAAA;QAED,cAAc,EAAE;YACd,GAAG,EAAE,MAAM,CAAA;YACX,WAAW,CAAC,EAAE,QAAQ,CAAA;YACtB,WAAW,EAAE,QAAQ,CAAA;YACrB,OAAO,EAAE,UAAU,CAAA;SACpB,CAAA;KACF;CACF;AAGD,wBAAgB,KAAK,CAAC,OAAO,EAAE,eAAe,GAAG,aAAa,CA4C7D"}
|
package/dist/index.js
CHANGED
|
@@ -92,9 +92,33 @@ var Size = /* @__PURE__ */ ((Size2) => {
|
|
|
92
92
|
return Size2;
|
|
93
93
|
})(Size || {});
|
|
94
94
|
|
|
95
|
+
// src/utils/logger.ts
|
|
96
|
+
var Logger = class {
|
|
97
|
+
static debug(...args) {
|
|
98
|
+
console.debug("[@keq-request/cache] [DEBUG] ", ...args);
|
|
99
|
+
}
|
|
100
|
+
static error(...args) {
|
|
101
|
+
console.error("[@keq-request/cache] [ERROR] ", ...args);
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
// src/utils/random.ts
|
|
106
|
+
function random(min, max) {
|
|
107
|
+
return Math.floor(Math.random() * (max - min)) + min;
|
|
108
|
+
}
|
|
109
|
+
|
|
95
110
|
// src/strategies/cache-first.ts
|
|
96
111
|
var cacheFirst = async function cacheFirst2(handler, context, next) {
|
|
97
112
|
const [cacheKey, cacheValue] = await handler.getCache(context);
|
|
113
|
+
if (handler.options.debug) {
|
|
114
|
+
Logger.debug([
|
|
115
|
+
"",
|
|
116
|
+
"Request: ".concat(context.request.method.toUpperCase(), " ").concat(context.request.__url__.href),
|
|
117
|
+
"Strategy: Cache First",
|
|
118
|
+
"Cache Key: ".concat(cacheKey),
|
|
119
|
+
"Cache Status: ".concat(cacheValue ? "HIT" : "MISS")
|
|
120
|
+
].join("\n"));
|
|
121
|
+
}
|
|
98
122
|
if (cacheValue) {
|
|
99
123
|
context.emitter.emit("cache:hit", { key: cacheKey, response: cacheValue.response, context });
|
|
100
124
|
context.res = cacheValue.response;
|
|
@@ -103,6 +127,15 @@ var cacheFirst = async function cacheFirst2(handler, context, next) {
|
|
|
103
127
|
context.emitter.emit("cache:miss", { key: cacheKey, context });
|
|
104
128
|
await next();
|
|
105
129
|
const [, entry] = await handler.setCache(context);
|
|
130
|
+
if (handler.options.debug) {
|
|
131
|
+
Logger.debug([
|
|
132
|
+
"",
|
|
133
|
+
"Request: ".concat(context.request.method.toUpperCase(), " ").concat(context.request.__url__.href),
|
|
134
|
+
"Strategy: Cache First",
|
|
135
|
+
"Cache Key: ".concat(cacheKey),
|
|
136
|
+
"ACTIONS: ".concat(entry ? "UPDATED" : "EXCLUDED")
|
|
137
|
+
].join("\n"));
|
|
138
|
+
}
|
|
106
139
|
if (entry) {
|
|
107
140
|
context.emitter.emit("cache:update", {
|
|
108
141
|
key: entry.key,
|
|
@@ -117,8 +150,17 @@ var cacheFirst = async function cacheFirst2(handler, context, next) {
|
|
|
117
150
|
var networkFirst = async function networkFirst2(handler, context, next) {
|
|
118
151
|
try {
|
|
119
152
|
await next();
|
|
120
|
-
const [, cache2] = await handler.getCache(context);
|
|
153
|
+
const [cacheKey, cache2] = await handler.getCache(context);
|
|
121
154
|
const [, entry] = await handler.setCache(context);
|
|
155
|
+
if (handler.options.debug) {
|
|
156
|
+
Logger.debug([
|
|
157
|
+
"",
|
|
158
|
+
"Request: ".concat(context.request.method.toUpperCase(), " ").concat(context.request.__url__.href),
|
|
159
|
+
"Strategy: Network First",
|
|
160
|
+
"Cache Key: ".concat(cacheKey),
|
|
161
|
+
"ACTIONS: ".concat(entry ? "UPDATED" : "EXCLUDED")
|
|
162
|
+
].join("\n"));
|
|
163
|
+
}
|
|
122
164
|
if (entry) {
|
|
123
165
|
context.emitter.emit("cache:update", {
|
|
124
166
|
key: entry.key,
|
|
@@ -128,12 +170,21 @@ var networkFirst = async function networkFirst2(handler, context, next) {
|
|
|
128
170
|
});
|
|
129
171
|
}
|
|
130
172
|
} catch (err) {
|
|
131
|
-
const [
|
|
173
|
+
const [cacheKey, cache2] = await handler.getCache(context);
|
|
174
|
+
if (handler.options.debug) {
|
|
175
|
+
Logger.debug([
|
|
176
|
+
"",
|
|
177
|
+
"Request: ".concat(context.request.method.toUpperCase(), " ").concat(context.request.__url__.href),
|
|
178
|
+
"Strategy: Network First",
|
|
179
|
+
"Cache Key: ".concat(cacheKey),
|
|
180
|
+
"Cache Status: ".concat(cache2 ? "HIT" : "MISS")
|
|
181
|
+
].join("\n"));
|
|
182
|
+
}
|
|
132
183
|
if (!cache2) {
|
|
133
|
-
context.emitter.emit("cache:miss", { key, context });
|
|
184
|
+
context.emitter.emit("cache:miss", { key: cacheKey, context });
|
|
134
185
|
throw err;
|
|
135
186
|
}
|
|
136
|
-
context.emitter.emit("cache:hit", { key, response: cache2.response, context });
|
|
187
|
+
context.emitter.emit("cache:hit", { key: cacheKey, response: cache2.response, context });
|
|
137
188
|
context.res = cache2.response;
|
|
138
189
|
}
|
|
139
190
|
};
|
|
@@ -145,19 +196,41 @@ var networkOnly = async function(handler, context, next) {
|
|
|
145
196
|
|
|
146
197
|
// src/strategies/stale-while-revalidate.ts
|
|
147
198
|
var staleWhileRevalidate = async function(handler, context, next) {
|
|
148
|
-
const [
|
|
199
|
+
const [cacheKey, cache2] = await handler.getCache(context);
|
|
200
|
+
if (handler.options.debug) {
|
|
201
|
+
Logger.debug([
|
|
202
|
+
"",
|
|
203
|
+
"Request: ".concat(context.request.method.toUpperCase(), " ").concat(context.request.__url__.href),
|
|
204
|
+
"Strategy: Stale While Revalidate",
|
|
205
|
+
"Cache Key: ".concat(cacheKey),
|
|
206
|
+
"Cache Status: ".concat(cache2 ? "HIT" : "MISS")
|
|
207
|
+
].join("\n"));
|
|
208
|
+
}
|
|
209
|
+
if (cache2) {
|
|
210
|
+
context.emitter.emit("cache:hit", { key: cacheKey, response: cache2.response, context });
|
|
211
|
+
} else {
|
|
212
|
+
context.emitter.emit("cache:miss", { key: cacheKey, context });
|
|
213
|
+
}
|
|
149
214
|
if (cache2) {
|
|
150
|
-
context.emitter.emit("cache:hit", { key, response: cache2.response, context });
|
|
151
215
|
const orchestrator = context.orchestration.fork();
|
|
152
216
|
context.res = cache2.response;
|
|
153
217
|
setTimeout(async () => {
|
|
154
218
|
try {
|
|
155
219
|
await orchestrator.execute();
|
|
156
220
|
const context2 = orchestrator.context;
|
|
157
|
-
const [, entry] = await handler.setCache(context2);
|
|
221
|
+
const [cacheKey2, entry] = await handler.setCache(context2);
|
|
222
|
+
if (handler.options.debug) {
|
|
223
|
+
Logger.debug([
|
|
224
|
+
"",
|
|
225
|
+
"Request: ".concat(context2.request.method.toUpperCase(), " ").concat(context2.request.__url__.href),
|
|
226
|
+
"Strategy: Stale While Revalidate",
|
|
227
|
+
"Cache Key: ".concat(cacheKey2),
|
|
228
|
+
"ACTIONS: ".concat(entry ? "UPDATED" : "EXCLUDED")
|
|
229
|
+
].join("\n"));
|
|
230
|
+
}
|
|
158
231
|
if (entry) {
|
|
159
232
|
context2.emitter.emit("cache:update", {
|
|
160
|
-
key,
|
|
233
|
+
key: cacheKey2,
|
|
161
234
|
oldResponse: cache2.response,
|
|
162
235
|
newResponse: entry.response,
|
|
163
236
|
context: context2
|
|
@@ -167,12 +240,20 @@ var staleWhileRevalidate = async function(handler, context, next) {
|
|
|
167
240
|
}
|
|
168
241
|
}, 1);
|
|
169
242
|
} else {
|
|
170
|
-
context.emitter.emit("cache:miss", { key, context });
|
|
171
243
|
await next();
|
|
172
|
-
const [, entry] = await handler.setCache(context);
|
|
244
|
+
const [cacheKey2, entry] = await handler.setCache(context);
|
|
245
|
+
if (handler.options.debug) {
|
|
246
|
+
Logger.debug([
|
|
247
|
+
"",
|
|
248
|
+
"Request: ".concat(context.request.method.toUpperCase(), " ").concat(context.request.__url__.href),
|
|
249
|
+
"Strategy: Stale While Revalidate",
|
|
250
|
+
"Cache Key: ".concat(cacheKey2),
|
|
251
|
+
"ACTIONS: ".concat(entry ? "UPDATED" : "EXCLUDED")
|
|
252
|
+
].join("\n"));
|
|
253
|
+
}
|
|
173
254
|
if (entry) {
|
|
174
255
|
context.emitter.emit("cache:update", {
|
|
175
|
-
key,
|
|
256
|
+
key: cacheKey2,
|
|
176
257
|
oldResponse: void 0,
|
|
177
258
|
newResponse: entry.response,
|
|
178
259
|
context
|
|
@@ -255,6 +336,15 @@ var RequestCacheHandler = class {
|
|
|
255
336
|
*/
|
|
256
337
|
async getCache(context) {
|
|
257
338
|
const key = this.getRequestCacheKey(context);
|
|
339
|
+
if (this.options.serverTiming) {
|
|
340
|
+
const startAt = /* @__PURE__ */ new Date();
|
|
341
|
+
const entry = await this.storage.get(key);
|
|
342
|
+
if (entry) {
|
|
343
|
+
const dur = (/* @__PURE__ */ new Date()).getTime() - startAt.getTime();
|
|
344
|
+
entry.response.headers.append("Server-Timing", "keq-cache; dur=".concat(dur, '; desc="HIT"'));
|
|
345
|
+
}
|
|
346
|
+
return [key, entry];
|
|
347
|
+
}
|
|
258
348
|
return [key, await this.storage.get(key)];
|
|
259
349
|
}
|
|
260
350
|
/**
|
|
@@ -301,6 +391,9 @@ function cache(options) {
|
|
|
301
391
|
await next();
|
|
302
392
|
return;
|
|
303
393
|
}
|
|
394
|
+
if (requestCacheOptions.serverTiming === void 0 && options.serverTiming !== void 0) {
|
|
395
|
+
requestCacheOptions.serverTiming = options.serverTiming;
|
|
396
|
+
}
|
|
304
397
|
const handler = new RequestCacheHandler(storage, requestCacheOptions);
|
|
305
398
|
const strategy = requestCacheOptions.strategy;
|
|
306
399
|
await strategy(handler, ctx, next);
|
|
@@ -319,21 +412,6 @@ var R3 = __toESM(require("ramda"));
|
|
|
319
412
|
var KeqCacheStorage = class {
|
|
320
413
|
};
|
|
321
414
|
|
|
322
|
-
// src/utils/logger.ts
|
|
323
|
-
var Logger = class {
|
|
324
|
-
static debug(...args) {
|
|
325
|
-
console.debug("[@keq-cache] [DEBUG] ", ...args);
|
|
326
|
-
}
|
|
327
|
-
static error(...args) {
|
|
328
|
-
console.error("[@keq-cache] [ERROR] ", ...args);
|
|
329
|
-
}
|
|
330
|
-
};
|
|
331
|
-
|
|
332
|
-
// src/utils/random.ts
|
|
333
|
-
function random(min, max) {
|
|
334
|
-
return Math.floor(Math.random() * (max - min)) + min;
|
|
335
|
-
}
|
|
336
|
-
|
|
337
415
|
// src/storage/internal-storage/internal-storage.ts
|
|
338
416
|
var InternalStorage = class extends KeqCacheStorage {
|
|
339
417
|
constructor(options) {
|