@faasjs/http 0.0.3-beta.87 → 0.0.3-beta.89

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.d.mts CHANGED
@@ -31,7 +31,7 @@ declare class Session<S extends Record<string, string> = any, C extends Record<s
31
31
  private readonly cookie;
32
32
  private changed?;
33
33
  constructor(cookie: Cookie<C, S>, config: SessionOptions);
34
- invoke(cookie?: string): void;
34
+ invoke(cookie?: string, logger?: Logger): void;
35
35
  encode(text: SessionContent): string;
36
36
  decode<TData = any>(text: string): TData | SessionContent;
37
37
  read(key: string): string | number;
@@ -61,9 +61,10 @@ declare class Cookie<C extends Record<string, string> = any, S extends Record<st
61
61
  sameSite?: 'Strict' | 'Lax' | 'None';
62
62
  session: SessionOptions;
63
63
  };
64
+ logger: Logger;
64
65
  private setCookie;
65
- constructor(config: CookieOptions);
66
- invoke(cookie: string | undefined): Cookie<C, S>;
66
+ constructor(config: CookieOptions, logger?: Logger);
67
+ invoke(cookie: string | undefined, logger: Logger): Cookie<C, S>;
67
68
  read(key: string): any;
68
69
  write(key: string, value: string, opts?: {
69
70
  domain?: string;
package/dist/index.d.ts CHANGED
@@ -31,7 +31,7 @@ declare class Session<S extends Record<string, string> = any, C extends Record<s
31
31
  private readonly cookie;
32
32
  private changed?;
33
33
  constructor(cookie: Cookie<C, S>, config: SessionOptions);
34
- invoke(cookie?: string): void;
34
+ invoke(cookie?: string, logger?: Logger): void;
35
35
  encode(text: SessionContent): string;
36
36
  decode<TData = any>(text: string): TData | SessionContent;
37
37
  read(key: string): string | number;
@@ -61,9 +61,10 @@ declare class Cookie<C extends Record<string, string> = any, S extends Record<st
61
61
  sameSite?: 'Strict' | 'Lax' | 'None';
62
62
  session: SessionOptions;
63
63
  };
64
+ logger: Logger;
64
65
  private setCookie;
65
- constructor(config: CookieOptions);
66
- invoke(cookie: string | undefined): Cookie<C, S>;
66
+ constructor(config: CookieOptions, logger?: Logger);
67
+ invoke(cookie: string | undefined, logger: Logger): Cookie<C, S>;
67
68
  read(key: string): any;
68
69
  write(key: string, value: string, opts?: {
69
70
  domain?: string;
package/dist/index.js CHANGED
@@ -1,48 +1,26 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
1
+ 'use strict';
19
2
 
20
- // src/index.ts
21
- var src_exports = {};
22
- __export(src_exports, {
23
- ContentType: () => ContentType,
24
- Cookie: () => Cookie,
25
- Http: () => Http,
26
- HttpError: () => HttpError,
27
- Session: () => Session,
28
- Validator: () => Validator,
29
- useHttp: () => useHttp
30
- });
31
- module.exports = __toCommonJS(src_exports);
32
- var import_func = require("@faasjs/func");
33
- var import_deep_merge2 = require("@faasjs/deep_merge");
34
- var import_logger = require("@faasjs/logger");
3
+ var func = require('@faasjs/func');
4
+ var deep_merge = require('@faasjs/deep_merge');
5
+ var logger = require('@faasjs/logger');
6
+ var crypto = require('crypto');
7
+ var zlib = require('zlib');
35
8
 
36
- // src/session.ts
37
- var import_crypto = require("crypto");
9
+ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
10
+ get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
11
+ }) : x)(function(x) {
12
+ if (typeof require !== "undefined")
13
+ return require.apply(this, arguments);
14
+ throw Error('Dynamic require of "' + x + '" is not supported');
15
+ });
38
16
  var Session = class {
39
17
  constructor(cookie, config) {
40
18
  this.cookie = cookie;
41
19
  if (!(config == null ? void 0 : config.secret))
42
- console.warn("Session's secret is missing.");
20
+ cookie.logger.warn("Session's secret is missing.");
43
21
  this.config = Object.assign({
44
22
  key: "key",
45
- secret: (0, import_crypto.randomBytes)(128).toString("hex"),
23
+ secret: crypto.randomBytes(128).toString("hex"),
46
24
  salt: "salt",
47
25
  signedSalt: "signedSalt",
48
26
  keylen: 64,
@@ -50,14 +28,14 @@ var Session = class {
50
28
  digest: "sha256",
51
29
  cipherName: "aes-256-cbc"
52
30
  }, config);
53
- this.secret = (0, import_crypto.pbkdf2Sync)(
31
+ this.secret = crypto.pbkdf2Sync(
54
32
  this.config.secret,
55
33
  this.config.salt,
56
34
  this.config.iterations,
57
35
  this.config.keylen / 2,
58
36
  this.config.digest
59
37
  );
60
- this.signedSecret = (0, import_crypto.pbkdf2Sync)(
38
+ this.signedSecret = crypto.pbkdf2Sync(
61
39
  this.config.secret,
62
40
  this.config.signedSalt,
63
41
  this.config.iterations,
@@ -66,11 +44,11 @@ var Session = class {
66
44
  );
67
45
  this.content = /* @__PURE__ */ Object.create(null);
68
46
  }
69
- invoke(cookie) {
47
+ invoke(cookie, logger) {
70
48
  try {
71
49
  this.content = cookie ? this.decode(cookie) : /* @__PURE__ */ Object.create(null);
72
50
  } catch (error) {
73
- console.error(error);
51
+ logger == null ? void 0 : logger.error(error);
74
52
  this.content = /* @__PURE__ */ Object.create(null);
75
53
  }
76
54
  this.changed = false;
@@ -78,11 +56,11 @@ var Session = class {
78
56
  encode(text) {
79
57
  if (typeof text !== "string")
80
58
  text = JSON.stringify(text);
81
- const iv = (0, import_crypto.randomBytes)(16);
82
- const cipher = (0, import_crypto.createCipheriv)(this.config.cipherName, this.secret, iv);
59
+ const iv = crypto.randomBytes(16);
60
+ const cipher = crypto.createCipheriv(this.config.cipherName, this.secret, iv);
83
61
  const encrypted = Buffer.concat([cipher.update(text), cipher.final()]).toString("base64");
84
62
  const main = Buffer.from([encrypted, iv.toString("base64")].join("--")).toString("base64");
85
- const hmac = (0, import_crypto.createHmac)(this.config.digest, this.signedSecret);
63
+ const hmac = crypto.createHmac(this.config.digest, this.signedSecret);
86
64
  hmac.update(main);
87
65
  const digest = hmac.digest("hex");
88
66
  return main + "--" + digest;
@@ -90,7 +68,7 @@ var Session = class {
90
68
  decode(text) {
91
69
  text = decodeURIComponent(text);
92
70
  const signedParts = text.split("--");
93
- const hmac = (0, import_crypto.createHmac)(this.config.digest, this.signedSecret);
71
+ const hmac = crypto.createHmac(this.config.digest, this.signedSecret);
94
72
  hmac.update(signedParts[0]);
95
73
  const digest = hmac.digest("hex");
96
74
  if (signedParts[1] !== digest)
@@ -99,7 +77,7 @@ var Session = class {
99
77
  const parts = message.split("--").map(function(part2) {
100
78
  return Buffer.from(part2, "base64");
101
79
  });
102
- const cipher = (0, import_crypto.createDecipheriv)(this.config.cipherName, this.secret, parts[1]);
80
+ const cipher = crypto.createDecipheriv(this.config.cipherName, this.secret, parts[1]);
103
81
  const part = Buffer.from(cipher.update(parts[0])).toString("utf8");
104
82
  const final = cipher.final("utf8");
105
83
  const decrypt = [part, final].join("");
@@ -122,12 +100,10 @@ var Session = class {
122
100
  return this;
123
101
  }
124
102
  };
125
-
126
- // src/cookie.ts
127
- var import_deep_merge = require("@faasjs/deep_merge");
128
103
  var Cookie = class {
129
- constructor(config) {
130
- this.config = (0, import_deep_merge.deepMerge)({
104
+ constructor(config, logger) {
105
+ this.logger = logger;
106
+ this.config = deep_merge.deepMerge({
131
107
  path: "/",
132
108
  expires: 31536e3,
133
109
  secure: true,
@@ -138,7 +114,7 @@ var Cookie = class {
138
114
  this.content = /* @__PURE__ */ Object.create(null);
139
115
  this.setCookie = /* @__PURE__ */ Object.create(null);
140
116
  }
141
- invoke(cookie) {
117
+ invoke(cookie, logger) {
142
118
  this.content = /* @__PURE__ */ Object.create(null);
143
119
  if (cookie)
144
120
  cookie.split(";").forEach((x) => {
@@ -148,7 +124,7 @@ var Cookie = class {
148
124
  this.content[k[0]] = decodeURIComponent(x.replace(`${k[0]}=`, "").replace(/;$/, ""));
149
125
  });
150
126
  this.setCookie = /* @__PURE__ */ Object.create(null);
151
- this.session.invoke(this.read(this.session.config.key));
127
+ this.session.invoke(this.read(this.session.config.key), logger);
152
128
  return this;
153
129
  }
154
130
  read(key) {
@@ -371,9 +347,6 @@ var Validator = class {
371
347
  }
372
348
  }
373
349
  };
374
-
375
- // src/index.ts
376
- var import_zlib = require("zlib");
377
350
  var ContentType = {
378
351
  plain: "text/plain",
379
352
  html: "text/html",
@@ -410,10 +383,10 @@ var Http = class {
410
383
  var _a;
411
384
  data.dependencies["@faasjs/http"] = "*";
412
385
  await next();
413
- const logger = new import_logger.Logger(this.name);
414
- logger.debug("Generate api gateway's config");
415
- logger.debug("%j", data);
416
- const config = data.config.plugins ? (0, import_deep_merge2.deepMerge)(data.config.plugins[this.name || this.type], { config: this.config }) : { config: this.config };
386
+ const logger$1 = new logger.Logger(this.name);
387
+ logger$1.debug("Generate api gateway's config");
388
+ logger$1.debug("%j", data);
389
+ const config = data.config.plugins ? deep_merge.deepMerge(data.config.plugins[this.name || this.type], { config: this.config }) : { config: this.config };
417
390
  if (!config.config.path) {
418
391
  config.config.path = "/" + ((_a = data.name) == null ? void 0 : _a.replace(/_/g, "/").replace(/\/index$/, ""));
419
392
  if (config.config.path === "/index")
@@ -424,8 +397,8 @@ var Http = class {
424
397
  config.config.path = "/";
425
398
  }
426
399
  }
427
- logger.debug("Api gateway's config: %j", config);
428
- const Provider = require(config.provider.type).Provider;
400
+ logger$1.debug("Api gateway's config: %j", config);
401
+ const Provider = __require(config.provider.type).Provider;
429
402
  const provider = new Provider(config.provider.config);
430
403
  await provider.deploy(this.type, data, config);
431
404
  }
@@ -449,9 +422,9 @@ var Http = class {
449
422
  this.config[key] = value;
450
423
  }
451
424
  if (data.config.plugins && data.config.plugins[this.name || this.type])
452
- this.config = (0, import_deep_merge2.deepMerge)(this.config, data.config.plugins[this.name || this.type].config);
425
+ this.config = deep_merge.deepMerge(this.config, data.config.plugins[this.name || this.type].config);
453
426
  data.logger.debug("[onMount] prepare cookie & session");
454
- this.cookie = new Cookie(this.config.cookie || {});
427
+ this.cookie = new Cookie(this.config.cookie || {}, data.logger);
455
428
  this.session = this.cookie.session;
456
429
  if (this.validatorOptions) {
457
430
  data.logger.debug("[onMount] prepare validator");
@@ -477,7 +450,7 @@ var Http = class {
477
450
  delete this.params["_"];
478
451
  data.logger.debug("[onInvoke] Params: %j", this.params);
479
452
  }
480
- this.cookie.invoke(this.headers.cookie);
453
+ this.cookie.invoke(this.headers.cookie, data.logger);
481
454
  if (this.headers.cookie) {
482
455
  data.logger.debug("[onInvoke] Cookie: %j", this.cookie.content);
483
456
  data.logger.debug("[onInvoke] Session: %j", this.session.content);
@@ -529,13 +502,13 @@ var Http = class {
529
502
  try {
530
503
  if (acceptEncoding.includes("br")) {
531
504
  data.response.headers["Content-Encoding"] = "br";
532
- data.response.body = (0, import_zlib.brotliCompressSync)(originBody).toString("base64");
505
+ data.response.body = zlib.brotliCompressSync(originBody).toString("base64");
533
506
  } else if (acceptEncoding.includes("gzip")) {
534
507
  data.response.headers["Content-Encoding"] = "gzip";
535
- data.response.body = (0, import_zlib.gzipSync)(originBody).toString("base64");
508
+ data.response.body = zlib.gzipSync(originBody).toString("base64");
536
509
  } else if (acceptEncoding.includes("deflate")) {
537
510
  data.response.headers["Content-Encoding"] = "deflate";
538
- data.response.body = (0, import_zlib.deflateSync)(originBody).toString("base64");
511
+ data.response.body = zlib.deflateSync(originBody).toString("base64");
539
512
  } else
540
513
  throw Error("No matched compression.");
541
514
  data.response.isBase64Encoded = true;
@@ -584,15 +557,13 @@ var Http = class {
584
557
  }
585
558
  };
586
559
  function useHttp(config) {
587
- return (0, import_func.usePlugin)(new Http(config));
560
+ return func.usePlugin(new Http(config));
588
561
  }
589
- // Annotate the CommonJS export names for ESM import in node:
590
- 0 && (module.exports = {
591
- ContentType,
592
- Cookie,
593
- Http,
594
- HttpError,
595
- Session,
596
- Validator,
597
- useHttp
598
- });
562
+
563
+ exports.ContentType = ContentType;
564
+ exports.Cookie = Cookie;
565
+ exports.Http = Http;
566
+ exports.HttpError = HttpError;
567
+ exports.Session = Session;
568
+ exports.Validator = Validator;
569
+ exports.useHttp = useHttp;
package/dist/index.mjs CHANGED
@@ -1,3 +1,9 @@
1
+ import { usePlugin } from '@faasjs/func';
2
+ import { deepMerge } from '@faasjs/deep_merge';
3
+ import { Logger } from '@faasjs/logger';
4
+ import { randomBytes, pbkdf2Sync, createCipheriv, createHmac, createDecipheriv } from 'crypto';
5
+ import { brotliCompressSync, gzipSync, deflateSync } from 'zlib';
6
+
1
7
  var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
2
8
  get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
3
9
  }) : x)(function(x) {
@@ -5,27 +11,11 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
5
11
  return require.apply(this, arguments);
6
12
  throw Error('Dynamic require of "' + x + '" is not supported');
7
13
  });
8
-
9
- // src/index.ts
10
- import {
11
- usePlugin
12
- } from "@faasjs/func";
13
- import { deepMerge as deepMerge2 } from "@faasjs/deep_merge";
14
- import { Logger } from "@faasjs/logger";
15
-
16
- // src/session.ts
17
- import {
18
- randomBytes,
19
- pbkdf2Sync,
20
- createCipheriv,
21
- createHmac,
22
- createDecipheriv
23
- } from "crypto";
24
14
  var Session = class {
25
15
  constructor(cookie, config) {
26
16
  this.cookie = cookie;
27
17
  if (!(config == null ? void 0 : config.secret))
28
- console.warn("Session's secret is missing.");
18
+ cookie.logger.warn("Session's secret is missing.");
29
19
  this.config = Object.assign({
30
20
  key: "key",
31
21
  secret: randomBytes(128).toString("hex"),
@@ -52,11 +42,11 @@ var Session = class {
52
42
  );
53
43
  this.content = /* @__PURE__ */ Object.create(null);
54
44
  }
55
- invoke(cookie) {
45
+ invoke(cookie, logger) {
56
46
  try {
57
47
  this.content = cookie ? this.decode(cookie) : /* @__PURE__ */ Object.create(null);
58
48
  } catch (error) {
59
- console.error(error);
49
+ logger == null ? void 0 : logger.error(error);
60
50
  this.content = /* @__PURE__ */ Object.create(null);
61
51
  }
62
52
  this.changed = false;
@@ -108,11 +98,9 @@ var Session = class {
108
98
  return this;
109
99
  }
110
100
  };
111
-
112
- // src/cookie.ts
113
- import { deepMerge } from "@faasjs/deep_merge";
114
101
  var Cookie = class {
115
- constructor(config) {
102
+ constructor(config, logger) {
103
+ this.logger = logger;
116
104
  this.config = deepMerge({
117
105
  path: "/",
118
106
  expires: 31536e3,
@@ -124,7 +112,7 @@ var Cookie = class {
124
112
  this.content = /* @__PURE__ */ Object.create(null);
125
113
  this.setCookie = /* @__PURE__ */ Object.create(null);
126
114
  }
127
- invoke(cookie) {
115
+ invoke(cookie, logger) {
128
116
  this.content = /* @__PURE__ */ Object.create(null);
129
117
  if (cookie)
130
118
  cookie.split(";").forEach((x) => {
@@ -134,7 +122,7 @@ var Cookie = class {
134
122
  this.content[k[0]] = decodeURIComponent(x.replace(`${k[0]}=`, "").replace(/;$/, ""));
135
123
  });
136
124
  this.setCookie = /* @__PURE__ */ Object.create(null);
137
- this.session.invoke(this.read(this.session.config.key));
125
+ this.session.invoke(this.read(this.session.config.key), logger);
138
126
  return this;
139
127
  }
140
128
  read(key) {
@@ -357,13 +345,6 @@ var Validator = class {
357
345
  }
358
346
  }
359
347
  };
360
-
361
- // src/index.ts
362
- import {
363
- gzipSync,
364
- deflateSync,
365
- brotliCompressSync
366
- } from "zlib";
367
348
  var ContentType = {
368
349
  plain: "text/plain",
369
350
  html: "text/html",
@@ -403,7 +384,7 @@ var Http = class {
403
384
  const logger = new Logger(this.name);
404
385
  logger.debug("Generate api gateway's config");
405
386
  logger.debug("%j", data);
406
- const config = data.config.plugins ? deepMerge2(data.config.plugins[this.name || this.type], { config: this.config }) : { config: this.config };
387
+ const config = data.config.plugins ? deepMerge(data.config.plugins[this.name || this.type], { config: this.config }) : { config: this.config };
407
388
  if (!config.config.path) {
408
389
  config.config.path = "/" + ((_a = data.name) == null ? void 0 : _a.replace(/_/g, "/").replace(/\/index$/, ""));
409
390
  if (config.config.path === "/index")
@@ -439,9 +420,9 @@ var Http = class {
439
420
  this.config[key] = value;
440
421
  }
441
422
  if (data.config.plugins && data.config.plugins[this.name || this.type])
442
- this.config = deepMerge2(this.config, data.config.plugins[this.name || this.type].config);
423
+ this.config = deepMerge(this.config, data.config.plugins[this.name || this.type].config);
443
424
  data.logger.debug("[onMount] prepare cookie & session");
444
- this.cookie = new Cookie(this.config.cookie || {});
425
+ this.cookie = new Cookie(this.config.cookie || {}, data.logger);
445
426
  this.session = this.cookie.session;
446
427
  if (this.validatorOptions) {
447
428
  data.logger.debug("[onMount] prepare validator");
@@ -467,7 +448,7 @@ var Http = class {
467
448
  delete this.params["_"];
468
449
  data.logger.debug("[onInvoke] Params: %j", this.params);
469
450
  }
470
- this.cookie.invoke(this.headers.cookie);
451
+ this.cookie.invoke(this.headers.cookie, data.logger);
471
452
  if (this.headers.cookie) {
472
453
  data.logger.debug("[onInvoke] Cookie: %j", this.cookie.content);
473
454
  data.logger.debug("[onInvoke] Session: %j", this.session.content);
@@ -576,12 +557,5 @@ var Http = class {
576
557
  function useHttp(config) {
577
558
  return usePlugin(new Http(config));
578
559
  }
579
- export {
580
- ContentType,
581
- Cookie,
582
- Http,
583
- HttpError,
584
- Session,
585
- Validator,
586
- useHttp
587
- };
560
+
561
+ export { ContentType, Cookie, Http, HttpError, Session, Validator, useHttp };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@faasjs/http",
3
- "version": "0.0.3-beta.87",
3
+ "version": "0.0.3-beta.89",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -22,8 +22,8 @@
22
22
  "dist"
23
23
  ],
24
24
  "dependencies": {
25
- "@faasjs/func": "^0.0.3-beta.87",
26
- "@faasjs/logger": "^0.0.3-beta.87"
25
+ "@faasjs/func": "^0.0.3-beta.89",
26
+ "@faasjs/logger": "^0.0.3-beta.89"
27
27
  },
28
28
  "engines": {
29
29
  "npm": ">=8.0.0",