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

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,7 +398,6 @@ var Validator = class {
398
398
  };
399
399
 
400
400
  // src/index.ts
401
- var import_zlib = require("zlib");
402
401
  var ContentType = {
403
402
  plain: "text/plain",
404
403
  html: "text/html",
@@ -471,7 +470,7 @@ var Http = class {
471
470
  await next();
472
471
  }
473
472
  async onInvoke(data, next) {
474
- var _a, _b;
473
+ var _a;
475
474
  this.headers = data.event.headers || /* @__PURE__ */ Object.create(null);
476
475
  this.body = data.event.body;
477
476
  this.params = /* @__PURE__ */ Object.create(null);
@@ -530,31 +529,12 @@ var Http = class {
530
529
  "Cache-Control": "no-cache, no-store"
531
530
  }, this.cookie.headers(), this.response.headers);
532
531
  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);
533
534
  const originBody = data.response.body;
534
535
  data.response.originBody = originBody;
535
- if (data.response.isBase64Encoded || typeof data.response.body !== "string" || !((_b = data.response.headers["Content-Type"]) == null ? void 0 : _b.includes("json")) || data.response.body.length < 100)
536
- return;
537
- const acceptEncoding = this.headers["accept-encoding"] || this.headers["Accept-Encoding"];
538
- if (!acceptEncoding || !/(br|gzip|deflate)/.test(acceptEncoding))
536
+ if (!data.response.body || data.response.isBase64Encoded || typeof data.response.body !== "string" || data.response.body.length < 100)
539
537
  return;
540
- try {
541
- if (acceptEncoding.includes("br")) {
542
- data.response.headers["Content-Encoding"] = "br";
543
- data.response.body = (0, import_zlib.brotliCompressSync)(originBody).toString("base64");
544
- } else if (acceptEncoding.includes("gzip")) {
545
- data.response.headers["Content-Encoding"] = "gzip";
546
- data.response.body = (0, import_zlib.gzipSync)(originBody).toString("base64");
547
- } else if (acceptEncoding.includes("deflate")) {
548
- data.response.headers["Content-Encoding"] = "deflate";
549
- data.response.body = (0, import_zlib.deflateSync)(originBody).toString("base64");
550
- } else
551
- throw Error("No matched compression.");
552
- data.response.isBase64Encoded = true;
553
- } catch (error) {
554
- console.error(error);
555
- data.response.body = originBody;
556
- delete data.response.headers["Content-Encoding"];
557
- }
558
538
  }
559
539
  setHeader(key, value) {
560
540
  this.response.headers[key] = value;
package/dist/index.mjs CHANGED
@@ -384,11 +384,6 @@ var Validator = class {
384
384
  };
385
385
 
386
386
  // src/index.ts
387
- import {
388
- gzipSync,
389
- deflateSync,
390
- brotliCompressSync
391
- } from "zlib";
392
387
  var ContentType = {
393
388
  plain: "text/plain",
394
389
  html: "text/html",
@@ -461,7 +456,7 @@ var Http = class {
461
456
  await next();
462
457
  }
463
458
  async onInvoke(data, next) {
464
- var _a, _b;
459
+ var _a;
465
460
  this.headers = data.event.headers || /* @__PURE__ */ Object.create(null);
466
461
  this.body = data.event.body;
467
462
  this.params = /* @__PURE__ */ Object.create(null);
@@ -520,31 +515,12 @@ var Http = class {
520
515
  "Cache-Control": "no-cache, no-store"
521
516
  }, this.cookie.headers(), this.response.headers);
522
517
  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);
523
520
  const originBody = data.response.body;
524
521
  data.response.originBody = originBody;
525
- if (data.response.isBase64Encoded || typeof data.response.body !== "string" || !((_b = data.response.headers["Content-Type"]) == null ? void 0 : _b.includes("json")) || data.response.body.length < 100)
522
+ if (!data.response.body || data.response.isBase64Encoded || typeof data.response.body !== "string" || data.response.body.length < 100)
526
523
  return;
527
- const acceptEncoding = this.headers["accept-encoding"] || this.headers["Accept-Encoding"];
528
- if (!acceptEncoding || !/(br|gzip|deflate)/.test(acceptEncoding))
529
- return;
530
- try {
531
- if (acceptEncoding.includes("br")) {
532
- data.response.headers["Content-Encoding"] = "br";
533
- data.response.body = brotliCompressSync(originBody).toString("base64");
534
- } else if (acceptEncoding.includes("gzip")) {
535
- data.response.headers["Content-Encoding"] = "gzip";
536
- data.response.body = gzipSync(originBody).toString("base64");
537
- } else if (acceptEncoding.includes("deflate")) {
538
- data.response.headers["Content-Encoding"] = "deflate";
539
- data.response.body = deflateSync(originBody).toString("base64");
540
- } else
541
- throw Error("No matched compression.");
542
- data.response.isBase64Encoded = true;
543
- } catch (error) {
544
- console.error(error);
545
- data.response.body = originBody;
546
- delete data.response.headers["Content-Encoding"];
547
- }
548
524
  }
549
525
  setHeader(key, value) {
550
526
  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.426",
3
+ "version": "0.0.2-beta.427",
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.426",
27
- "@faasjs/logger": "^0.0.2-beta.426"
26
+ "@faasjs/func": "^0.0.2-beta.427",
27
+ "@faasjs/logger": "^0.0.2-beta.427"
28
28
  },
29
29
  "devDependencies": {
30
30
  "tsup": "*",