@less-is-more/less-js 1.4.31 → 1.4.32-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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@less-is-more/less-js",
3
- "version": "1.4.31",
3
+ "version": "1.4.32-0",
4
4
  "description": "Fast develop kit for nodejs",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
package/src/router.js CHANGED
@@ -1,9 +1,10 @@
1
1
  const Ret = require("./ret.js");
2
2
  const Param = require("./param.js");
3
3
  const qs = require("qs");
4
- const body = require("body");
4
+ const content = require("body");
5
5
  const multiparty = require("multiparty");
6
6
  const { promisify } = require("util");
7
+ const zlib = require("zlib");
7
8
 
8
9
  module.exports = class Router {
9
10
  static #defaultFormat = true;
@@ -43,7 +44,7 @@ module.exports = class Router {
43
44
  return {
44
45
  statusCode: 200,
45
46
  headers: Object.assign(res.headers || {}, event.resHeaders || {}),
46
- body: res.content.startsWith("{") ? JSON.parse(res.content) : res.content,
47
+ body: res.content,
47
48
  };
48
49
  }
49
50
 
@@ -144,7 +145,7 @@ module.exports = class Router {
144
145
  if (isFile) {
145
146
  await this._getFile(req);
146
147
  } else if (isJson) {
147
- const data = req.body || (await promisify(body)(req));
148
+ const data = req.body || (await promisify(content)(req));
148
149
  if (!Param.isBlank(data)) {
149
150
  try {
150
151
  req.sourceBody = data.toString();
@@ -154,7 +155,7 @@ module.exports = class Router {
154
155
  }
155
156
  }
156
157
  } else {
157
- const data = req.body || (await promisify(body)(req));
158
+ const data = req.body || (await promisify(content)(req));
158
159
  req.sourceBody = decodeURIComponent(data.toString());
159
160
  req.body = qs.parse(data.toString());
160
161
  }
@@ -206,6 +207,26 @@ module.exports = class Router {
206
207
  "response:",
207
208
  content.substring(0, 500)
208
209
  );
210
+ // 处理header带压缩支持的情况
211
+ if (req.headers) {
212
+ const acceptEncoding =
213
+ req.headers["Accept-Encoding"] || req.headers["accept-encoding"];
214
+ if (acceptEncoding) {
215
+ if (!res.headers) {
216
+ res.headers = {};
217
+ }
218
+ if (acceptEncoding.includes("gzip")) {
219
+ content = zlib.gzipSync(content).toString("base64");
220
+ res.headers["Content-Encoding"] = "gzip";
221
+ } else if (acceptEncoding.includes("deflate")) {
222
+ content = zlib.deflateSync(content).toString("base64");
223
+ res.headers["Content-Encoding"] = "deflate";
224
+ } else if (acceptEncoding.includes("br")) {
225
+ content = zlib.brotliCompressSync(content).toString("base64");
226
+ res.headers["Content-Encoding"] = "br";
227
+ }
228
+ }
229
+ }
209
230
  res.send(content);
210
231
  }
211
232
  }
@@ -238,4 +238,23 @@ describe("router.js", () => {
238
238
  };
239
239
  await Router.route(targets, req, res);
240
240
  });
241
+
242
+ it("header compress", async () => {
243
+ let targets = {
244
+ "/": {
245
+ add: function (req, res) {
246
+ res.send("ok");
247
+ },
248
+ },
249
+ };
250
+ let req = { path: "/add", headers: { "accept-encoding": "br" } };
251
+ let res = {
252
+ send: function (data) {
253
+ console.log("send: " + data);
254
+ },
255
+ };
256
+ console.log("start");
257
+ await Router.route(targets, req, res);
258
+ console.log("end");
259
+ });
241
260
  });