@faasjs/http 0.0.2-beta.452 → 0.0.2-beta.453

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.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { Plugin, DeployData, Next, MountData, InvokeData, UseifyPlugin } from '@faasjs/func';
2
2
  import { Logger } from '@faasjs/logger';
3
3
 
4
- declare type SessionOptions = {
4
+ type SessionOptions = {
5
5
  key: string;
6
6
  secret: string;
7
7
  salt?: string;
@@ -11,7 +11,7 @@ declare type SessionOptions = {
11
11
  digest?: string;
12
12
  cipherName?: string;
13
13
  };
14
- declare type SessionContent = string | number | {
14
+ type SessionContent = string | number | {
15
15
  [key: string]: any;
16
16
  } | null | undefined;
17
17
  declare class Session<S extends Record<string, string> = any, C extends Record<string, string> = any> {
@@ -39,7 +39,7 @@ declare class Session<S extends Record<string, string> = any, C extends Record<s
39
39
  update(): Session<S, C>;
40
40
  }
41
41
 
42
- declare type CookieOptions = {
42
+ type CookieOptions = {
43
43
  domain?: string;
44
44
  path?: string;
45
45
  expires?: number;
@@ -78,8 +78,8 @@ declare class Cookie<C extends Record<string, string> = any, S extends Record<st
78
78
  };
79
79
  }
80
80
 
81
- declare type ValidatorRuleOptionsType = 'string' | 'number' | 'boolean' | 'object' | 'array';
82
- declare type ValidatorRuleOptions = {
81
+ type ValidatorRuleOptionsType = 'string' | 'number' | 'boolean' | 'object' | 'array';
82
+ type ValidatorRuleOptions = {
83
83
  type?: ValidatorRuleOptionsType;
84
84
  required?: boolean;
85
85
  in?: any[];
@@ -87,7 +87,7 @@ declare type ValidatorRuleOptions = {
87
87
  config?: Partial<ValidatorOptions>;
88
88
  regexp?: RegExp;
89
89
  };
90
- declare type ValidatorOptions<Content = Record<string, any>> = {
90
+ type ValidatorOptions<Content = Record<string, any>> = {
91
91
  whitelist?: 'error' | 'ignore';
92
92
  rules: {
93
93
  [k in keyof Content]?: ValidatorRuleOptions;
@@ -97,7 +97,7 @@ declare type ValidatorOptions<Content = Record<string, any>> = {
97
97
  message: any;
98
98
  } | void;
99
99
  };
100
- declare type Request<TParams extends Record<string, any> = any, TCookie extends Record<string, string> = any, TSession extends Record<string, string> = any> = {
100
+ type Request<TParams extends Record<string, any> = any, TCookie extends Record<string, string> = any, TSession extends Record<string, string> = any> = {
101
101
  headers: {
102
102
  [key: string]: string;
103
103
  };
@@ -105,11 +105,11 @@ declare type Request<TParams extends Record<string, any> = any, TCookie extends
105
105
  cookie?: Cookie<TCookie, TSession>;
106
106
  session?: Session<TSession, TCookie>;
107
107
  };
108
- declare type BeforeOption<TParams extends Record<string, any> = any, TCookie extends Record<string, string> = any, TSession extends Record<string, string> = any> = (request: Request<TParams, TCookie, TSession>) => Promise<void | {
108
+ type BeforeOption<TParams extends Record<string, any> = any, TCookie extends Record<string, string> = any, TSession extends Record<string, string> = any> = (request: Request<TParams, TCookie, TSession>) => Promise<void | {
109
109
  statusCode?: number;
110
110
  message: string;
111
111
  }>;
112
- declare type ValidatorConfig<TParams extends Record<string, any> = any, TCookie extends Record<string, string> = any, TSession extends Record<string, string> = any> = {
112
+ type ValidatorConfig<TParams extends Record<string, any> = any, TCookie extends Record<string, string> = any, TSession extends Record<string, string> = any> = {
113
113
  params?: ValidatorOptions<TParams>;
114
114
  cookie?: ValidatorOptions<TCookie>;
115
115
  session?: ValidatorOptions<TSession>;
@@ -131,7 +131,7 @@ declare class Validator<TParams extends Record<string, any> = any, TCookie exten
131
131
  declare const ContentType: {
132
132
  [key: string]: string;
133
133
  };
134
- declare type HttpConfig<TParams extends Record<string, any> = any, TCookie extends Record<string, string> = any, TSession extends Record<string, string> = any> = {
134
+ type HttpConfig<TParams extends Record<string, any> = any, TCookie extends Record<string, string> = any, TSession extends Record<string, string> = any> = {
135
135
  [key: string]: any;
136
136
  name?: string;
137
137
  config?: {
@@ -147,7 +147,7 @@ declare type HttpConfig<TParams extends Record<string, any> = any, TCookie exten
147
147
  };
148
148
  validator?: ValidatorConfig<TParams, TCookie, TSession>;
149
149
  };
150
- declare type Response = {
150
+ type Response = {
151
151
  statusCode?: number;
152
152
  headers?: {
153
153
  [key: string]: string;
package/dist/index.js CHANGED
@@ -29,7 +29,129 @@ __export(src_exports, {
29
29
  useHttp: () => useHttp
30
30
  });
31
31
  module.exports = __toCommonJS(src_exports);
32
- var import_func = require("@faasjs/func");
32
+
33
+ // ../logger/src/index.ts
34
+ var import_util = require("util");
35
+ var LevelColor = ((LevelColor2) => {
36
+ LevelColor2[LevelColor2["debug"] = 90 /* GRAY */] = "debug";
37
+ LevelColor2[LevelColor2["info"] = 32 /* GREEN */] = "info";
38
+ LevelColor2[LevelColor2["warn"] = 33 /* ORANGE */] = "warn";
39
+ LevelColor2[LevelColor2["error"] = 31 /* RED */] = "error";
40
+ return LevelColor2;
41
+ })(LevelColor || {});
42
+ var LevelPriority = {
43
+ debug: 0,
44
+ info: 1,
45
+ warn: 2,
46
+ error: 3
47
+ };
48
+ var Logger = class {
49
+ constructor(label) {
50
+ this.colorfyOutput = true;
51
+ if (label)
52
+ this.label = label;
53
+ this.silent = !process.env.FaasLog && process.env.npm_config_argv && JSON.parse(process.env.npm_config_argv).original.includes("--silent");
54
+ if (["remote", "mono"].includes(process.env.FaasMode))
55
+ this.colorfyOutput = false;
56
+ this.level = process.env.FaasLog ? LevelPriority[process.env.FaasLog.toLowerCase()] : 0;
57
+ this.cachedTimers = {};
58
+ this.size = 500;
59
+ this.stdout = console.log;
60
+ this.stderr = console.error;
61
+ }
62
+ debug(message, ...args) {
63
+ this.log("debug", message, ...args);
64
+ return this;
65
+ }
66
+ info(message, ...args) {
67
+ this.log("info", message, ...args);
68
+ return this;
69
+ }
70
+ warn(message, ...args) {
71
+ this.log("warn", message, ...args);
72
+ return this;
73
+ }
74
+ error(message, ...args) {
75
+ let stack = false;
76
+ [message].concat(Array.from(args)).forEach((e) => {
77
+ if (e.stack) {
78
+ stack = true;
79
+ this.log("error", e.stack);
80
+ }
81
+ });
82
+ if (!stack)
83
+ this.log("error", message, ...args);
84
+ return this;
85
+ }
86
+ time(key, level = "debug") {
87
+ this.cachedTimers[key] = {
88
+ level,
89
+ time: new Date().getTime()
90
+ };
91
+ return this;
92
+ }
93
+ timeEnd(key, message, ...args) {
94
+ if (this.cachedTimers[key]) {
95
+ const timer = this.cachedTimers[key];
96
+ message = message + " +%ims";
97
+ args.push(new Date().getTime() - timer.time);
98
+ this[timer.level](message, ...args);
99
+ delete this.cachedTimers[key];
100
+ } else {
101
+ this.warn("timeEnd not found key %s", key);
102
+ this.debug(message);
103
+ }
104
+ return this;
105
+ }
106
+ raw(message, ...args) {
107
+ if (this.silent)
108
+ return this;
109
+ this.stdout((0, import_util.format)(message, ...args));
110
+ return this;
111
+ }
112
+ colorfy(color, message) {
113
+ return `\x1B[0${color}m${message}\x1B[39m`;
114
+ }
115
+ log(level, message, ...args) {
116
+ if (this.silent)
117
+ return this;
118
+ if (LevelPriority[level] < this.level)
119
+ return this;
120
+ let output = level.toUpperCase() + " " + (this.label ? `[${this.label}] ` : "") + (0, import_util.format)(message, ...args);
121
+ if (this.colorfyOutput && level !== "error")
122
+ output = this.colorfy(LevelColor[level], output);
123
+ else if (!this.colorfyOutput)
124
+ output = output.replace(/\n/g, "");
125
+ if (!output)
126
+ return this;
127
+ if (output.length > this.size)
128
+ output = output.slice(0, this.size) + "...";
129
+ if (level === "error")
130
+ this.stderr(output);
131
+ else
132
+ this.stdout(output);
133
+ return this;
134
+ }
135
+ };
136
+
137
+ // ../func/src/index.ts
138
+ var import_crypto = require("crypto");
139
+ var plugins = [];
140
+ function usePlugin(plugin) {
141
+ if (!plugins.find((p) => p.name === plugin.name))
142
+ plugins.push(plugin);
143
+ if (!plugin.mount)
144
+ plugin.mount = async function({ config }) {
145
+ if (plugin.onMount)
146
+ await plugin.onMount({
147
+ config,
148
+ event: {},
149
+ context: {},
150
+ logger: new Logger(plugin.name)
151
+ }, async () => Promise.resolve());
152
+ };
153
+ return plugin;
154
+ }
33
155
 
34
156
  // ../deep_merge/src/index.ts
35
157
  var shouldMerge = function(item) {
@@ -58,17 +180,14 @@ function deepMerge(...sources) {
58
180
  return acc;
59
181
  }
60
182
 
61
- // src/index.ts
62
- var import_logger = require("@faasjs/logger");
63
-
64
183
  // src/session.ts
65
- var import_crypto = require("crypto");
184
+ var import_crypto2 = require("crypto");
66
185
  var Session = class {
67
186
  constructor(cookie, config) {
68
187
  this.cookie = cookie;
69
188
  this.config = Object.assign({
70
189
  key: "key",
71
- secret: (0, import_crypto.randomBytes)(128).toString("hex"),
190
+ secret: (0, import_crypto2.randomBytes)(128).toString("hex"),
72
191
  salt: "salt",
73
192
  signedSalt: "signedSalt",
74
193
  keylen: 64,
@@ -76,14 +195,14 @@ var Session = class {
76
195
  digest: "sha256",
77
196
  cipherName: "aes-256-cbc"
78
197
  }, config);
79
- this.secret = (0, import_crypto.pbkdf2Sync)(
198
+ this.secret = (0, import_crypto2.pbkdf2Sync)(
80
199
  this.config.secret,
81
200
  this.config.salt,
82
201
  this.config.iterations,
83
202
  this.config.keylen / 2,
84
203
  this.config.digest
85
204
  );
86
- this.signedSecret = (0, import_crypto.pbkdf2Sync)(
205
+ this.signedSecret = (0, import_crypto2.pbkdf2Sync)(
87
206
  this.config.secret,
88
207
  this.config.signedSalt,
89
208
  this.config.iterations,
@@ -104,11 +223,11 @@ var Session = class {
104
223
  encode(text) {
105
224
  if (typeof text !== "string")
106
225
  text = JSON.stringify(text);
107
- const iv = (0, import_crypto.randomBytes)(16);
108
- const cipher = (0, import_crypto.createCipheriv)(this.config.cipherName, this.secret, iv);
226
+ const iv = (0, import_crypto2.randomBytes)(16);
227
+ const cipher = (0, import_crypto2.createCipheriv)(this.config.cipherName, this.secret, iv);
109
228
  const encrypted = Buffer.concat([cipher.update(text), cipher.final()]).toString("base64");
110
229
  const main = Buffer.from([encrypted, iv.toString("base64")].join("--")).toString("base64");
111
- const hmac = (0, import_crypto.createHmac)(this.config.digest, this.signedSecret);
230
+ const hmac = (0, import_crypto2.createHmac)(this.config.digest, this.signedSecret);
112
231
  hmac.update(main);
113
232
  const digest = hmac.digest("hex");
114
233
  return main + "--" + digest;
@@ -116,7 +235,7 @@ var Session = class {
116
235
  decode(text) {
117
236
  text = decodeURIComponent(text);
118
237
  const signedParts = text.split("--");
119
- const hmac = (0, import_crypto.createHmac)(this.config.digest, this.signedSecret);
238
+ const hmac = (0, import_crypto2.createHmac)(this.config.digest, this.signedSecret);
120
239
  hmac.update(signedParts[0]);
121
240
  const digest = hmac.digest("hex");
122
241
  if (signedParts[1] !== digest)
@@ -125,7 +244,7 @@ var Session = class {
125
244
  const parts = message.split("--").map(function(part2) {
126
245
  return Buffer.from(part2, "base64");
127
246
  });
128
- const cipher = (0, import_crypto.createDecipheriv)(this.config.cipherName, this.secret, parts[1]);
247
+ const cipher = (0, import_crypto2.createDecipheriv)(this.config.cipherName, this.secret, parts[1]);
129
248
  const part = Buffer.from(cipher.update(parts[0])).toString("utf8");
130
249
  const final = cipher.final("utf8");
131
250
  const decrypt = [part, final].join("");
@@ -438,7 +557,7 @@ var Http = class {
438
557
  var _a;
439
558
  data.dependencies["@faasjs/http"] = "*";
440
559
  await next();
441
- const logger = new import_logger.Logger(this.name);
560
+ const logger = new Logger(this.name);
442
561
  logger.debug("Generate api gateway's config");
443
562
  logger.debug("%j", data);
444
563
  const config = data.config.plugins ? deepMerge(data.config.plugins[this.name || this.type], { config: this.config }) : { config: this.config };
@@ -575,7 +694,7 @@ var Http = class {
575
694
  }
576
695
  };
577
696
  function useHttp(config) {
578
- return (0, import_func.usePlugin)(new Http(config));
697
+ return usePlugin(new Http(config));
579
698
  }
580
699
  // Annotate the CommonJS export names for ESM import in node:
581
700
  0 && (module.exports = {
package/dist/index.mjs CHANGED
@@ -6,10 +6,128 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
6
6
  throw new Error('Dynamic require of "' + x + '" is not supported');
7
7
  });
8
8
 
9
- // src/index.ts
10
- import {
11
- usePlugin
12
- } from "@faasjs/func";
9
+ // ../logger/src/index.ts
10
+ import { format } from "util";
11
+ var LevelColor = ((LevelColor2) => {
12
+ LevelColor2[LevelColor2["debug"] = 90 /* GRAY */] = "debug";
13
+ LevelColor2[LevelColor2["info"] = 32 /* GREEN */] = "info";
14
+ LevelColor2[LevelColor2["warn"] = 33 /* ORANGE */] = "warn";
15
+ LevelColor2[LevelColor2["error"] = 31 /* RED */] = "error";
16
+ return LevelColor2;
17
+ })(LevelColor || {});
18
+ var LevelPriority = {
19
+ debug: 0,
20
+ info: 1,
21
+ warn: 2,
22
+ error: 3
23
+ };
24
+ var Logger = class {
25
+ constructor(label) {
26
+ this.colorfyOutput = true;
27
+ if (label)
28
+ this.label = label;
29
+ this.silent = !process.env.FaasLog && process.env.npm_config_argv && JSON.parse(process.env.npm_config_argv).original.includes("--silent");
30
+ if (["remote", "mono"].includes(process.env.FaasMode))
31
+ this.colorfyOutput = false;
32
+ this.level = process.env.FaasLog ? LevelPriority[process.env.FaasLog.toLowerCase()] : 0;
33
+ this.cachedTimers = {};
34
+ this.size = 500;
35
+ this.stdout = console.log;
36
+ this.stderr = console.error;
37
+ }
38
+ debug(message, ...args) {
39
+ this.log("debug", message, ...args);
40
+ return this;
41
+ }
42
+ info(message, ...args) {
43
+ this.log("info", message, ...args);
44
+ return this;
45
+ }
46
+ warn(message, ...args) {
47
+ this.log("warn", message, ...args);
48
+ return this;
49
+ }
50
+ error(message, ...args) {
51
+ let stack = false;
52
+ [message].concat(Array.from(args)).forEach((e) => {
53
+ if (e.stack) {
54
+ stack = true;
55
+ this.log("error", e.stack);
56
+ }
57
+ });
58
+ if (!stack)
59
+ this.log("error", message, ...args);
60
+ return this;
61
+ }
62
+ time(key, level = "debug") {
63
+ this.cachedTimers[key] = {
64
+ level,
65
+ time: new Date().getTime()
66
+ };
67
+ return this;
68
+ }
69
+ timeEnd(key, message, ...args) {
70
+ if (this.cachedTimers[key]) {
71
+ const timer = this.cachedTimers[key];
72
+ message = message + " +%ims";
73
+ args.push(new Date().getTime() - timer.time);
74
+ this[timer.level](message, ...args);
75
+ delete this.cachedTimers[key];
76
+ } else {
77
+ this.warn("timeEnd not found key %s", key);
78
+ this.debug(message);
79
+ }
80
+ return this;
81
+ }
82
+ raw(message, ...args) {
83
+ if (this.silent)
84
+ return this;
85
+ this.stdout(format(message, ...args));
86
+ return this;
87
+ }
88
+ colorfy(color, message) {
89
+ return `\x1B[0${color}m${message}\x1B[39m`;
90
+ }
91
+ log(level, message, ...args) {
92
+ if (this.silent)
93
+ return this;
94
+ if (LevelPriority[level] < this.level)
95
+ return this;
96
+ let output = level.toUpperCase() + " " + (this.label ? `[${this.label}] ` : "") + format(message, ...args);
97
+ if (this.colorfyOutput && level !== "error")
98
+ output = this.colorfy(LevelColor[level], output);
99
+ else if (!this.colorfyOutput)
100
+ output = output.replace(/\n/g, "");
101
+ if (!output)
102
+ return this;
103
+ if (output.length > this.size)
104
+ output = output.slice(0, this.size) + "...";
105
+ if (level === "error")
106
+ this.stderr(output);
107
+ else
108
+ this.stdout(output);
109
+ return this;
110
+ }
111
+ };
112
+
113
+ // ../func/src/index.ts
114
+ import { randomBytes } from "crypto";
115
+ var plugins = [];
116
+ function usePlugin(plugin) {
117
+ if (!plugins.find((p) => p.name === plugin.name))
118
+ plugins.push(plugin);
119
+ if (!plugin.mount)
120
+ plugin.mount = async function({ config }) {
121
+ if (plugin.onMount)
122
+ await plugin.onMount({
123
+ config,
124
+ event: {},
125
+ context: {},
126
+ logger: new Logger(plugin.name)
127
+ }, async () => Promise.resolve());
128
+ };
129
+ return plugin;
130
+ }
13
131
 
14
132
  // ../deep_merge/src/index.ts
15
133
  var shouldMerge = function(item) {
@@ -38,12 +156,9 @@ function deepMerge(...sources) {
38
156
  return acc;
39
157
  }
40
158
 
41
- // src/index.ts
42
- import { Logger } from "@faasjs/logger";
43
-
44
159
  // src/session.ts
45
160
  import {
46
- randomBytes,
161
+ randomBytes as randomBytes2,
47
162
  pbkdf2Sync,
48
163
  createCipheriv,
49
164
  createHmac,
@@ -54,7 +169,7 @@ var Session = class {
54
169
  this.cookie = cookie;
55
170
  this.config = Object.assign({
56
171
  key: "key",
57
- secret: randomBytes(128).toString("hex"),
172
+ secret: randomBytes2(128).toString("hex"),
58
173
  salt: "salt",
59
174
  signedSalt: "signedSalt",
60
175
  keylen: 64,
@@ -90,7 +205,7 @@ var Session = class {
90
205
  encode(text) {
91
206
  if (typeof text !== "string")
92
207
  text = JSON.stringify(text);
93
- const iv = randomBytes(16);
208
+ const iv = randomBytes2(16);
94
209
  const cipher = createCipheriv(this.config.cipherName, this.secret, iv);
95
210
  const encrypted = Buffer.concat([cipher.update(text), cipher.final()]).toString("base64");
96
211
  const main = Buffer.from([encrypted, iv.toString("base64")].join("--")).toString("base64");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@faasjs/http",
3
- "version": "0.0.2-beta.452",
3
+ "version": "0.0.2-beta.453",
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.452",
27
- "@faasjs/logger": "^0.0.2-beta.452"
26
+ "@faasjs/func": "^0.0.2-beta.453",
27
+ "@faasjs/logger": "^0.0.2-beta.453"
28
28
  },
29
29
  "devDependencies": {
30
30
  "tsup": "*",