@less-is-more/less-js 1.4.27-9 → 1.4.28-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/package.json +1 -1
- package/src/cache.js +19 -3
- package/src/router.js +23 -15
- package/test/test-cache.js +37 -0
package/package.json
CHANGED
package/src/cache.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
const Redis = require("./redis");
|
|
2
2
|
const Param = require("./param");
|
|
3
|
+
const zlib = require("zlib");
|
|
3
4
|
|
|
4
5
|
module.exports = class Cache {
|
|
5
6
|
/**
|
|
@@ -37,9 +38,10 @@ module.exports = class Cache {
|
|
|
37
38
|
* @param {number} timeSecond 缓存时间
|
|
38
39
|
* @param {*} args 直接传arguments,用于拼接缓存key
|
|
39
40
|
* @param {function} fn 没有缓存的实现
|
|
41
|
+
* @param {boolean} zip 是否压缩
|
|
40
42
|
* @returns 优先返回对象
|
|
41
43
|
*/
|
|
42
|
-
static async auto(keyPrefix, timeSecond, args, fn) {
|
|
44
|
+
static async auto(keyPrefix, timeSecond, args, fn, zip = false) {
|
|
43
45
|
let fullKey = keyPrefix;
|
|
44
46
|
for (let i = 0; i < args.length; i++) {
|
|
45
47
|
fullKey += ":" + args[i].toString();
|
|
@@ -57,7 +59,7 @@ module.exports = class Cache {
|
|
|
57
59
|
"setex",
|
|
58
60
|
fullKey,
|
|
59
61
|
timeSecond + "",
|
|
60
|
-
JSON.stringify(result)
|
|
62
|
+
zip ? await _zip(JSON.stringify(result)) : JSON.stringify(result)
|
|
61
63
|
);
|
|
62
64
|
}
|
|
63
65
|
} else {
|
|
@@ -65,13 +67,16 @@ module.exports = class Cache {
|
|
|
65
67
|
"setex",
|
|
66
68
|
fullKey,
|
|
67
69
|
timeSecond + "",
|
|
68
|
-
result.toString()
|
|
70
|
+
zip ? await Cache._zip(result.toString()) : result.toString()
|
|
69
71
|
);
|
|
70
72
|
}
|
|
71
73
|
return result;
|
|
72
74
|
}
|
|
73
75
|
} else {
|
|
74
76
|
console.log("Found cache", fullKey);
|
|
77
|
+
if (zip) {
|
|
78
|
+
savedData = await Cache._unzip(savedData);
|
|
79
|
+
}
|
|
75
80
|
// 优先转成json
|
|
76
81
|
if (savedData.startsWith("{") || savedData.startsWith("[")) {
|
|
77
82
|
return JSON.parse(savedData);
|
|
@@ -81,6 +86,17 @@ module.exports = class Cache {
|
|
|
81
86
|
}
|
|
82
87
|
}
|
|
83
88
|
|
|
89
|
+
static async _zip(text) {
|
|
90
|
+
const zipData = zlib.deflateSync(text).toString("base64");
|
|
91
|
+
return zipData;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
static async _unzip(base64) {
|
|
95
|
+
const buffer = Buffer.from(base64, "base64");
|
|
96
|
+
const text = zlib.inflateSync(buffer).toString();
|
|
97
|
+
return text;
|
|
98
|
+
}
|
|
99
|
+
|
|
84
100
|
static setWebCache(res, expireSecond) {
|
|
85
101
|
res.setHeader("Cache-Control", "max-age=" + expireSecond);
|
|
86
102
|
}
|
package/src/router.js
CHANGED
|
@@ -13,32 +13,38 @@ module.exports = class Router {
|
|
|
13
13
|
* @param {object} targets 路由表。支持before和after,如果提供了会在执行主方法前后执行。需要async的方法。并且before返回值为Ret,如果不为true,会直接返回
|
|
14
14
|
* @param {*} event 事件载体
|
|
15
15
|
* @param {*} context 系统属性
|
|
16
|
-
* @param {*} callback 回调
|
|
17
16
|
* @returns 设置了useDefaultFormat为true,会返回错误信息
|
|
18
17
|
*/
|
|
19
|
-
static async routeEvent(targets, event, context
|
|
18
|
+
static async routeEvent(targets, event, context) {
|
|
20
19
|
const eventObj = JSON.parse(event.toString());
|
|
21
20
|
const req = {
|
|
22
21
|
path: eventObj.rawPath,
|
|
22
|
+
method: eventObj.requestContext.http.method,
|
|
23
|
+
sourceIp: eventObj.requestContext.http.sourceIp,
|
|
23
24
|
queries: eventObj.queryParameters,
|
|
24
25
|
headers: eventObj.headers,
|
|
25
|
-
body: eventObj.
|
|
26
|
+
body: eventObj.isBase64Encoded
|
|
27
|
+
? Buffer.from(eventObj.body, "base64").toString("utf-8")
|
|
28
|
+
: eventObj.body,
|
|
26
29
|
isBase64Encoded: eventObj.isBase64Encoded,
|
|
27
30
|
};
|
|
31
|
+
// 格式兼容
|
|
32
|
+
if (req.headers) {
|
|
33
|
+
req.headers["content-type"] = req.headers["Content-Type"];
|
|
34
|
+
}
|
|
28
35
|
let res = {
|
|
29
|
-
send: (content) => {
|
|
30
|
-
res.content = content;
|
|
31
|
-
},
|
|
32
36
|
content: "",
|
|
37
|
+
send: function (content) {
|
|
38
|
+
this.content = content;
|
|
39
|
+
},
|
|
33
40
|
};
|
|
34
41
|
// 转换后调用原来的方法
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
42
|
+
await this.route(targets, req, res, context);
|
|
43
|
+
return {
|
|
44
|
+
statusCode: 200,
|
|
45
|
+
headers: Object.assign(res.headers || {}, event.resHeaders || {}),
|
|
46
|
+
body: res.content.startsWith("{") ? JSON.parse(res.content) : res.content,
|
|
39
47
|
};
|
|
40
|
-
|
|
41
|
-
callback(null, finalResponse);
|
|
42
48
|
}
|
|
43
49
|
|
|
44
50
|
/**
|
|
@@ -58,7 +64,9 @@ module.exports = class Router {
|
|
|
58
64
|
"p:",
|
|
59
65
|
req.method,
|
|
60
66
|
req.path,
|
|
61
|
-
req.headers
|
|
67
|
+
req.headers
|
|
68
|
+
? req.headers["content-type"] || req.headers["Content-Type"]
|
|
69
|
+
: "",
|
|
62
70
|
"q:",
|
|
63
71
|
req.queries ? qs.stringify(req.queries).substring(0, 500) : "",
|
|
64
72
|
"b:",
|
|
@@ -136,7 +144,7 @@ module.exports = class Router {
|
|
|
136
144
|
if (isFile) {
|
|
137
145
|
await this._getFile(req);
|
|
138
146
|
} else if (isJson) {
|
|
139
|
-
const data = await promisify(body)(req);
|
|
147
|
+
const data = req.body || (await promisify(body)(req));
|
|
140
148
|
if (!Param.isBlank(data)) {
|
|
141
149
|
try {
|
|
142
150
|
req.sourceBody = data.toString();
|
|
@@ -146,7 +154,7 @@ module.exports = class Router {
|
|
|
146
154
|
}
|
|
147
155
|
}
|
|
148
156
|
} else {
|
|
149
|
-
const data = await promisify(body)(req);
|
|
157
|
+
const data = req.body || (await promisify(body)(req));
|
|
150
158
|
req.sourceBody = decodeURIComponent(data.toString());
|
|
151
159
|
req.body = qs.parse(data.toString());
|
|
152
160
|
}
|
package/test/test-cache.js
CHANGED
|
@@ -103,5 +103,42 @@ describe("cache.js", () => {
|
|
|
103
103
|
console.log("result", JSON.stringify(result));
|
|
104
104
|
assert(result.success === false);
|
|
105
105
|
});
|
|
106
|
+
|
|
107
|
+
it("auto base64", async () => {
|
|
108
|
+
let a = 1,
|
|
109
|
+
b = 2;
|
|
110
|
+
let result = await Cache.auto(
|
|
111
|
+
"test",
|
|
112
|
+
20,
|
|
113
|
+
[],
|
|
114
|
+
async () => {
|
|
115
|
+
console.log(a + b);
|
|
116
|
+
return a + b + "";
|
|
117
|
+
},
|
|
118
|
+
true
|
|
119
|
+
);
|
|
120
|
+
console.log("result", result);
|
|
121
|
+
assert(result === "3");
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it("auto long text base64", async () => {
|
|
125
|
+
let result = await Cache.auto(
|
|
126
|
+
"test",
|
|
127
|
+
20,
|
|
128
|
+
[],
|
|
129
|
+
async () => {
|
|
130
|
+
let text = "";
|
|
131
|
+
// 循环1000次
|
|
132
|
+
for (let i = 0; i < 1000; i++) {
|
|
133
|
+
text += "a";
|
|
134
|
+
}
|
|
135
|
+
console.log(text);
|
|
136
|
+
return text;
|
|
137
|
+
},
|
|
138
|
+
true
|
|
139
|
+
);
|
|
140
|
+
console.log("result", result);
|
|
141
|
+
assert(result === "3");
|
|
142
|
+
});
|
|
106
143
|
});
|
|
107
144
|
});
|