@faasjs/http 0.0.2-beta.427 → 0.0.2-beta.428

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/dist/index.js CHANGED
@@ -398,6 +398,7 @@ var Validator = class {
398
398
  };
399
399
 
400
400
  // src/index.ts
401
+ var import_zlib = require("zlib");
401
402
  var ContentType = {
402
403
  plain: "text/plain",
403
404
  html: "text/html",
@@ -529,12 +530,33 @@ var Http = class {
529
530
  "Cache-Control": "no-cache, no-store"
530
531
  }, this.cookie.headers(), this.response.headers);
531
532
  data.response = Object.assign({}, data.response, this.response);
532
- if (data.response.body && !data.response.isBase64Encoded && typeof data.response.body !== "string")
533
- data.response.body = JSON.stringify(data.response.body);
534
533
  const originBody = data.response.body;
535
534
  data.response.originBody = originBody;
535
+ if (originBody && !data.response.isBase64Encoded && typeof originBody !== "string")
536
+ data.response.body = JSON.stringify(originBody);
536
537
  if (!data.response.body || data.response.isBase64Encoded || typeof data.response.body !== "string" || data.response.body.length < 100)
537
538
  return;
539
+ const acceptEncoding = this.headers["accept-encoding"] || this.headers["Accept-Encoding"];
540
+ if (!acceptEncoding || !/(br|gzip|deflate)/.test(acceptEncoding))
541
+ return;
542
+ try {
543
+ if (acceptEncoding.includes("br")) {
544
+ data.response.headers["Content-Encoding"] = "br";
545
+ data.response.body = (0, import_zlib.brotliCompressSync)(originBody).toString("base64");
546
+ } else if (acceptEncoding.includes("gzip")) {
547
+ data.response.headers["Content-Encoding"] = "gzip";
548
+ data.response.body = (0, import_zlib.gzipSync)(originBody).toString("base64");
549
+ } else if (acceptEncoding.includes("deflate")) {
550
+ data.response.headers["Content-Encoding"] = "deflate";
551
+ data.response.body = (0, import_zlib.deflateSync)(originBody).toString("base64");
552
+ } else
553
+ throw Error("No matched compression.");
554
+ data.response.isBase64Encoded = true;
555
+ } catch (error) {
556
+ console.error(error);
557
+ data.response.body = originBody;
558
+ delete data.response.headers["Content-Encoding"];
559
+ }
538
560
  }
539
561
  setHeader(key, value) {
540
562
  this.response.headers[key] = value;
package/dist/index.mjs CHANGED
@@ -384,6 +384,11 @@ var Validator = class {
384
384
  };
385
385
 
386
386
  // src/index.ts
387
+ import {
388
+ gzipSync,
389
+ deflateSync,
390
+ brotliCompressSync
391
+ } from "zlib";
387
392
  var ContentType = {
388
393
  plain: "text/plain",
389
394
  html: "text/html",
@@ -515,12 +520,33 @@ var Http = class {
515
520
  "Cache-Control": "no-cache, no-store"
516
521
  }, this.cookie.headers(), this.response.headers);
517
522
  data.response = Object.assign({}, data.response, this.response);
518
- if (data.response.body && !data.response.isBase64Encoded && typeof data.response.body !== "string")
519
- data.response.body = JSON.stringify(data.response.body);
520
523
  const originBody = data.response.body;
521
524
  data.response.originBody = originBody;
525
+ if (originBody && !data.response.isBase64Encoded && typeof originBody !== "string")
526
+ data.response.body = JSON.stringify(originBody);
522
527
  if (!data.response.body || data.response.isBase64Encoded || typeof data.response.body !== "string" || data.response.body.length < 100)
523
528
  return;
529
+ const acceptEncoding = this.headers["accept-encoding"] || this.headers["Accept-Encoding"];
530
+ if (!acceptEncoding || !/(br|gzip|deflate)/.test(acceptEncoding))
531
+ return;
532
+ try {
533
+ if (acceptEncoding.includes("br")) {
534
+ data.response.headers["Content-Encoding"] = "br";
535
+ data.response.body = brotliCompressSync(originBody).toString("base64");
536
+ } else if (acceptEncoding.includes("gzip")) {
537
+ data.response.headers["Content-Encoding"] = "gzip";
538
+ data.response.body = gzipSync(originBody).toString("base64");
539
+ } else if (acceptEncoding.includes("deflate")) {
540
+ data.response.headers["Content-Encoding"] = "deflate";
541
+ data.response.body = deflateSync(originBody).toString("base64");
542
+ } else
543
+ throw Error("No matched compression.");
544
+ data.response.isBase64Encoded = true;
545
+ } catch (error) {
546
+ console.error(error);
547
+ data.response.body = originBody;
548
+ delete data.response.headers["Content-Encoding"];
549
+ }
524
550
  }
525
551
  setHeader(key, value) {
526
552
  this.response.headers[key] = value;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@faasjs/http",
3
- "version": "0.0.2-beta.427",
3
+ "version": "0.0.2-beta.428",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -23,8 +23,8 @@
23
23
  "dist"
24
24
  ],
25
25
  "dependencies": {
26
- "@faasjs/func": "^0.0.2-beta.427",
27
- "@faasjs/logger": "^0.0.2-beta.427"
26
+ "@faasjs/func": "^0.0.2-beta.428",
27
+ "@faasjs/logger": "^0.0.2-beta.428"
28
28
  },
29
29
  "devDependencies": {
30
30
  "tsup": "*",