@zenweb/cache 4.1.2 → 4.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 +6 -0
- package/dist/cache.js +3 -2
- package/dist/middleware.d.ts +3 -3
- package/dist/middleware.js +14 -6
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
package/dist/cache.js
CHANGED
|
@@ -95,12 +95,13 @@ class Cache {
|
|
|
95
95
|
let _opt = Object.assign({ parse: true, decompress: true }, opt);
|
|
96
96
|
if (data) {
|
|
97
97
|
// 解压处理
|
|
98
|
-
|
|
98
|
+
let compressed = GZ_HEADER.equals(data.subarray(0, GZ_HEADER.length));
|
|
99
99
|
if (compressed) {
|
|
100
100
|
_getDebug('[%s] is compressed', key);
|
|
101
101
|
if (_opt.parse || _opt.decompress) {
|
|
102
102
|
_getDebug('[%s] decompress', key);
|
|
103
103
|
data = await decompress(data);
|
|
104
|
+
compressed = false;
|
|
104
105
|
}
|
|
105
106
|
}
|
|
106
107
|
// 解析处理
|
|
@@ -121,7 +122,7 @@ class Cache {
|
|
|
121
122
|
if ((_opt === null || _opt === void 0 ? void 0 : _opt.parse) !== false) {
|
|
122
123
|
return obj;
|
|
123
124
|
}
|
|
124
|
-
return new CacheResult(!opt.decompress
|
|
125
|
+
return new CacheResult(!opt.decompress, !opt.decompress && result.compressed || Buffer.from(result.data));
|
|
125
126
|
};
|
|
126
127
|
// 预刷新处理
|
|
127
128
|
const preRefresh = async () => {
|
package/dist/middleware.d.ts
CHANGED
|
@@ -10,9 +10,9 @@ interface CachedMiddlewareOption extends Omit<LockGetOption, 'parse' | 'decompre
|
|
|
10
10
|
}
|
|
11
11
|
/**
|
|
12
12
|
* 缓存中间件
|
|
13
|
-
* - 缓存中间件缓存的是 ctx.body 最终结果,所以可以缓存任何返回类型
|
|
13
|
+
* - 缓存中间件缓存的是 `ctx.body` 最终结果,所以可以缓存任何返回类型
|
|
14
14
|
* - 会根据客户端请求头判断是否需要解压
|
|
15
|
-
* @param key
|
|
15
|
+
* @param key 不指定 `key` 则默认使用 `ctx.path`,`key: null` 不适用使用缓存
|
|
16
16
|
*/
|
|
17
|
-
export declare function cached(key?: string | ((ctx: Context) => string | Promise<string>), opt?: CachedMiddlewareOption): Middleware;
|
|
17
|
+
export declare function cached(key?: string | null | ((ctx: Context) => string | null | Promise<string | null>), opt?: CachedMiddlewareOption): Middleware;
|
|
18
18
|
export {};
|
package/dist/middleware.js
CHANGED
|
@@ -3,15 +3,23 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.cached = void 0;
|
|
4
4
|
/**
|
|
5
5
|
* 缓存中间件
|
|
6
|
-
* - 缓存中间件缓存的是 ctx.body 最终结果,所以可以缓存任何返回类型
|
|
6
|
+
* - 缓存中间件缓存的是 `ctx.body` 最终结果,所以可以缓存任何返回类型
|
|
7
7
|
* - 会根据客户端请求头判断是否需要解压
|
|
8
|
-
* @param key
|
|
8
|
+
* @param key 不指定 `key` 则默认使用 `ctx.path`,`key: null` 不适用使用缓存
|
|
9
9
|
*/
|
|
10
10
|
function cached(key, opt) {
|
|
11
11
|
return async function cachedMiddleware(ctx, next) {
|
|
12
|
-
const _key =
|
|
12
|
+
const _key = typeof key === 'string' ? key
|
|
13
|
+
: typeof key === 'function' ? await key(ctx)
|
|
14
|
+
: typeof key === 'undefined' ? `CACHED-PATH:${ctx.path}`
|
|
15
|
+
: null;
|
|
16
|
+
if (!_key) {
|
|
17
|
+
return next();
|
|
18
|
+
}
|
|
13
19
|
const decompress = ctx.acceptsEncodings('gzip') === false;
|
|
14
|
-
const _opt = Object.assign({
|
|
20
|
+
const _opt = Object.assign({
|
|
21
|
+
type: 'json'
|
|
22
|
+
}, opt, {
|
|
15
23
|
parse: false,
|
|
16
24
|
decompress,
|
|
17
25
|
});
|
|
@@ -22,8 +30,8 @@ function cached(key, opt) {
|
|
|
22
30
|
if (result.compressed) {
|
|
23
31
|
ctx.set('Content-Encoding', 'gzip');
|
|
24
32
|
}
|
|
25
|
-
if (
|
|
26
|
-
ctx.type =
|
|
33
|
+
if (_opt.type) {
|
|
34
|
+
ctx.type = _opt.type;
|
|
27
35
|
}
|
|
28
36
|
ctx.body = result.data;
|
|
29
37
|
};
|