@faasjs/http 0.0.3-beta.7 → 0.0.3-beta.71
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/README.md +2 -2
- package/dist/index.d.ts +1 -1
- package/dist/index.js +751 -39
- package/dist/index.mjs +761 -39
- package/package.json +4 -5
package/dist/index.mjs
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
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,41 +11,722 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
|
|
|
5
11
|
return require.apply(this, arguments);
|
|
6
12
|
throw new Error('Dynamic require of "' + x + '" is not supported');
|
|
7
13
|
});
|
|
14
|
+
var __commonJS = (cb, mod) => function __require2() {
|
|
15
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
16
|
+
};
|
|
17
|
+
var __copyProps = (to, from, except, desc) => {
|
|
18
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
19
|
+
for (let key of __getOwnPropNames(from))
|
|
20
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
21
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
22
|
+
}
|
|
23
|
+
return to;
|
|
24
|
+
};
|
|
25
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
26
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
27
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
28
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
29
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
30
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
31
|
+
mod
|
|
32
|
+
));
|
|
8
33
|
|
|
9
|
-
//
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
34
|
+
// ../func/dist/index.js
|
|
35
|
+
var require_dist = __commonJS({
|
|
36
|
+
"../func/dist/index.js"(exports, module) {
|
|
37
|
+
"use strict";
|
|
38
|
+
var __create2 = Object.create;
|
|
39
|
+
var __defProp2 = Object.defineProperty;
|
|
40
|
+
var __getOwnPropDesc2 = Object.getOwnPropertyDescriptor;
|
|
41
|
+
var __getOwnPropNames2 = Object.getOwnPropertyNames;
|
|
42
|
+
var __getProtoOf2 = Object.getPrototypeOf;
|
|
43
|
+
var __hasOwnProp2 = Object.prototype.hasOwnProperty;
|
|
44
|
+
var __commonJS2 = (cb, mod) => function __require2() {
|
|
45
|
+
return mod || (0, cb[__getOwnPropNames2(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
46
|
+
};
|
|
47
|
+
var __export = (target, all) => {
|
|
48
|
+
for (var name in all)
|
|
49
|
+
__defProp2(target, name, { get: all[name], enumerable: true });
|
|
50
|
+
};
|
|
51
|
+
var __copyProps2 = (to, from, except, desc) => {
|
|
52
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
53
|
+
for (let key of __getOwnPropNames2(from))
|
|
54
|
+
if (!__hasOwnProp2.call(to, key) && key !== except)
|
|
55
|
+
__defProp2(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc2(from, key)) || desc.enumerable });
|
|
56
|
+
}
|
|
57
|
+
return to;
|
|
58
|
+
};
|
|
59
|
+
var __toESM2 = (mod, isNodeMode, target) => (target = mod != null ? __create2(__getProtoOf2(mod)) : {}, __copyProps2(
|
|
60
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
61
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
62
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
63
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
64
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp2(target, "default", { value: mod, enumerable: true }) : target,
|
|
65
|
+
mod
|
|
66
|
+
));
|
|
67
|
+
var __toCommonJS = (mod) => __copyProps2(__defProp2({}, "__esModule", { value: true }), mod);
|
|
68
|
+
var require_dist4 = __commonJS2({
|
|
69
|
+
"../logger/dist/index.js"(exports2, module2) {
|
|
70
|
+
"use strict";
|
|
71
|
+
var __defProp22 = Object.defineProperty;
|
|
72
|
+
var __getOwnPropDesc22 = Object.getOwnPropertyDescriptor;
|
|
73
|
+
var __getOwnPropNames22 = Object.getOwnPropertyNames;
|
|
74
|
+
var __hasOwnProp22 = Object.prototype.hasOwnProperty;
|
|
75
|
+
var __export2 = (target, all) => {
|
|
76
|
+
for (var name in all)
|
|
77
|
+
__defProp22(target, name, { get: all[name], enumerable: true });
|
|
78
|
+
};
|
|
79
|
+
var __copyProps22 = (to, from, except, desc) => {
|
|
80
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
81
|
+
for (let key of __getOwnPropNames22(from))
|
|
82
|
+
if (!__hasOwnProp22.call(to, key) && key !== except)
|
|
83
|
+
__defProp22(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc22(from, key)) || desc.enumerable });
|
|
84
|
+
}
|
|
85
|
+
return to;
|
|
86
|
+
};
|
|
87
|
+
var __toCommonJS2 = (mod) => __copyProps22(__defProp22({}, "__esModule", { value: true }), mod);
|
|
88
|
+
var src_exports2 = {};
|
|
89
|
+
__export2(src_exports2, {
|
|
90
|
+
Color: () => Color,
|
|
91
|
+
Logger: () => Logger2
|
|
92
|
+
});
|
|
93
|
+
module2.exports = __toCommonJS2(src_exports2);
|
|
94
|
+
var import_util = __require("util");
|
|
95
|
+
var Color = /* @__PURE__ */ ((Color2) => {
|
|
96
|
+
Color2[Color2["DEFAULT"] = 39] = "DEFAULT";
|
|
97
|
+
Color2[Color2["BLACK"] = 30] = "BLACK";
|
|
98
|
+
Color2[Color2["RED"] = 31] = "RED";
|
|
99
|
+
Color2[Color2["GREEN"] = 32] = "GREEN";
|
|
100
|
+
Color2[Color2["ORANGE"] = 33] = "ORANGE";
|
|
101
|
+
Color2[Color2["BLUE"] = 34] = "BLUE";
|
|
102
|
+
Color2[Color2["MAGENTA"] = 35] = "MAGENTA";
|
|
103
|
+
Color2[Color2["CYAN"] = 36] = "CYAN";
|
|
104
|
+
Color2[Color2["GRAY"] = 90] = "GRAY";
|
|
105
|
+
return Color2;
|
|
106
|
+
})(Color || {});
|
|
107
|
+
var LevelColor = ((LevelColor2) => {
|
|
108
|
+
LevelColor2[
|
|
109
|
+
LevelColor2["debug"] = 90
|
|
110
|
+
/* GRAY */
|
|
111
|
+
] = "debug";
|
|
112
|
+
LevelColor2[
|
|
113
|
+
LevelColor2["info"] = 32
|
|
114
|
+
/* GREEN */
|
|
115
|
+
] = "info";
|
|
116
|
+
LevelColor2[
|
|
117
|
+
LevelColor2["warn"] = 33
|
|
118
|
+
/* ORANGE */
|
|
119
|
+
] = "warn";
|
|
120
|
+
LevelColor2[
|
|
121
|
+
LevelColor2["error"] = 31
|
|
122
|
+
/* RED */
|
|
123
|
+
] = "error";
|
|
124
|
+
return LevelColor2;
|
|
125
|
+
})(LevelColor || {});
|
|
126
|
+
var LevelPriority = {
|
|
127
|
+
debug: 0,
|
|
128
|
+
info: 1,
|
|
129
|
+
warn: 2,
|
|
130
|
+
error: 3
|
|
131
|
+
};
|
|
132
|
+
var Logger2 = class {
|
|
133
|
+
/**
|
|
134
|
+
* @param label {string} Prefix label
|
|
135
|
+
*/
|
|
136
|
+
constructor(label) {
|
|
137
|
+
this.colorfyOutput = true;
|
|
138
|
+
if (label)
|
|
139
|
+
this.label = label;
|
|
140
|
+
this.silent = !process.env.FaasLog && process.env.npm_config_argv && JSON.parse(process.env.npm_config_argv).original.includes("--silent");
|
|
141
|
+
if (["remote", "mono"].includes(process.env.FaasMode))
|
|
142
|
+
this.colorfyOutput = false;
|
|
143
|
+
this.level = process.env.FaasLog ? LevelPriority[process.env.FaasLog.toLowerCase()] : 0;
|
|
144
|
+
this.cachedTimers = {};
|
|
145
|
+
this.size = 1e3;
|
|
146
|
+
this.stdout = console.log;
|
|
147
|
+
this.stderr = console.error;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* @param message {string} message
|
|
151
|
+
* @param args {...any=} arguments
|
|
152
|
+
*/
|
|
153
|
+
debug(message, ...args) {
|
|
154
|
+
this.log("debug", message, ...args);
|
|
155
|
+
return this;
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* @param message {string} message
|
|
159
|
+
* @param args {...any=} arguments
|
|
160
|
+
*/
|
|
161
|
+
info(message, ...args) {
|
|
162
|
+
this.log("info", message, ...args);
|
|
163
|
+
return this;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* @param message {string} message
|
|
167
|
+
* @param args {...any=} arguments
|
|
168
|
+
*/
|
|
169
|
+
warn(message, ...args) {
|
|
170
|
+
this.log("warn", message, ...args);
|
|
171
|
+
return this;
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* @param message {any} message or Error object
|
|
175
|
+
* @param args {...any=} arguments
|
|
176
|
+
*/
|
|
177
|
+
error(message, ...args) {
|
|
178
|
+
let stack = false;
|
|
179
|
+
[message].concat(Array.from(args)).forEach((e) => {
|
|
180
|
+
if (e.stack) {
|
|
181
|
+
stack = true;
|
|
182
|
+
this.log("error", e.stack);
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
if (!stack)
|
|
186
|
+
this.log("error", message, ...args);
|
|
187
|
+
return this;
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* @param key {string} timer's label
|
|
191
|
+
* @param level [string=debug] 日志级别,支持 debug、info、warn、error
|
|
192
|
+
*/
|
|
193
|
+
time(key, level = "debug") {
|
|
194
|
+
this.cachedTimers[key] = {
|
|
195
|
+
level,
|
|
196
|
+
time: (/* @__PURE__ */ new Date()).getTime()
|
|
197
|
+
};
|
|
198
|
+
return this;
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* @param key {string} timer's label
|
|
202
|
+
* @param message {string} message
|
|
203
|
+
* @param args {...any=} arguments
|
|
204
|
+
*/
|
|
205
|
+
timeEnd(key, message, ...args) {
|
|
206
|
+
if (this.cachedTimers[key]) {
|
|
207
|
+
const timer = this.cachedTimers[key];
|
|
208
|
+
message = message + " +%ims";
|
|
209
|
+
args.push((/* @__PURE__ */ new Date()).getTime() - timer.time);
|
|
210
|
+
this[timer.level](message, ...args);
|
|
211
|
+
delete this.cachedTimers[key];
|
|
212
|
+
} else {
|
|
213
|
+
this.warn("timeEnd not found key %s", key);
|
|
214
|
+
this.debug(message);
|
|
215
|
+
}
|
|
216
|
+
return this;
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* @param message {string} message
|
|
220
|
+
* @param args {...any=} arguments
|
|
221
|
+
*/
|
|
222
|
+
raw(message, ...args) {
|
|
223
|
+
if (this.silent)
|
|
224
|
+
return this;
|
|
225
|
+
this.stdout((0, import_util.format)(message, ...args));
|
|
226
|
+
return this;
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* @param color {number} color code
|
|
230
|
+
* @param message {string} message
|
|
231
|
+
*/
|
|
232
|
+
colorfy(color, message) {
|
|
233
|
+
return `\x1B[0${color}m${message}\x1B[39m`;
|
|
234
|
+
}
|
|
235
|
+
log(level, message, ...args) {
|
|
236
|
+
if (this.silent)
|
|
237
|
+
return this;
|
|
238
|
+
if (LevelPriority[level] < this.level)
|
|
239
|
+
return this;
|
|
240
|
+
let output = level.toUpperCase() + " " + (this.label ? `[${this.label}] ` : "") + (0, import_util.format)(message, ...args);
|
|
241
|
+
if (this.colorfyOutput && level !== "error")
|
|
242
|
+
output = this.colorfy(LevelColor[level], output);
|
|
243
|
+
else if (!this.colorfyOutput)
|
|
244
|
+
output = output.replace(/\n/g, "");
|
|
245
|
+
if (!output)
|
|
246
|
+
return this;
|
|
247
|
+
if (output.length > this.size && !["error", "warn"].includes(level))
|
|
248
|
+
output = output.slice(0, this.size - 100) + "..." + output.slice(output.length - 100);
|
|
249
|
+
if (level === "error")
|
|
250
|
+
this.stderr(output);
|
|
251
|
+
else
|
|
252
|
+
this.stdout(output);
|
|
253
|
+
return this;
|
|
254
|
+
}
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
});
|
|
258
|
+
var src_exports = {};
|
|
259
|
+
__export(src_exports, {
|
|
260
|
+
Func: () => Func,
|
|
261
|
+
useFunc: () => useFunc,
|
|
262
|
+
usePlugin: () => usePlugin2
|
|
263
|
+
});
|
|
264
|
+
module.exports = __toCommonJS(src_exports);
|
|
265
|
+
var import_logger2 = __toESM2(require_dist4());
|
|
266
|
+
var RunHandler = class {
|
|
267
|
+
constructor() {
|
|
268
|
+
this.type = "handler";
|
|
269
|
+
this.name = "handler";
|
|
270
|
+
}
|
|
271
|
+
async onInvoke(data, next) {
|
|
272
|
+
if (data.handler)
|
|
273
|
+
if (!data.runHandler) {
|
|
274
|
+
try {
|
|
275
|
+
data.response = await new Promise(function(resolve, reject) {
|
|
276
|
+
data.callback = function(error, result) {
|
|
277
|
+
if (error)
|
|
278
|
+
reject(error);
|
|
279
|
+
else
|
|
280
|
+
resolve(result);
|
|
281
|
+
};
|
|
282
|
+
Promise.resolve(data.handler(data)).then(resolve).catch(reject);
|
|
283
|
+
});
|
|
284
|
+
} catch (error) {
|
|
285
|
+
data.logger.error(error);
|
|
286
|
+
data.response = error;
|
|
287
|
+
}
|
|
288
|
+
data.runHandler = true;
|
|
289
|
+
} else
|
|
290
|
+
data.logger.warn("[RunHandler] handler has been run");
|
|
291
|
+
await next();
|
|
292
|
+
}
|
|
293
|
+
};
|
|
294
|
+
var import_crypto2 = __require("crypto");
|
|
295
|
+
var Func = class {
|
|
296
|
+
/**
|
|
297
|
+
* Create a cloud function
|
|
298
|
+
* @param config {object} config
|
|
299
|
+
* @param config.plugins {Plugin[]} plugins list
|
|
300
|
+
* @param config.handler {Handler} business logic
|
|
301
|
+
*/
|
|
302
|
+
constructor(config) {
|
|
303
|
+
this.handler = config.handler;
|
|
304
|
+
this.plugins = config.plugins || [];
|
|
305
|
+
this.plugins.push(new RunHandler());
|
|
306
|
+
this.config = {
|
|
307
|
+
providers: /* @__PURE__ */ Object.create(null),
|
|
308
|
+
plugins: /* @__PURE__ */ Object.create(null)
|
|
309
|
+
};
|
|
310
|
+
this.mounted = false;
|
|
311
|
+
this.cachedFunctions = /* @__PURE__ */ Object.create(null);
|
|
312
|
+
try {
|
|
313
|
+
this.filename = new Error().stack.split("\n").find((s) => /[^/]\.func\.ts/.test(s)).match(/\((.*\.func\.ts).*\)/)[1];
|
|
314
|
+
} catch (error) {
|
|
315
|
+
new import_logger2.Logger("Func").warn(error.message);
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
compose(key) {
|
|
319
|
+
let list = [];
|
|
320
|
+
if (this.cachedFunctions[key])
|
|
321
|
+
list = this.cachedFunctions[key];
|
|
322
|
+
else {
|
|
323
|
+
for (const plugin of this.plugins) {
|
|
324
|
+
const handler = plugin[key];
|
|
325
|
+
if (typeof handler === "function")
|
|
326
|
+
list.push({
|
|
327
|
+
key: plugin.name,
|
|
328
|
+
handler: handler.bind(plugin)
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
this.cachedFunctions[key] = list;
|
|
332
|
+
}
|
|
333
|
+
return async function(data, next) {
|
|
334
|
+
let index = -1;
|
|
335
|
+
const logger = (data == null ? void 0 : data.logger) || new import_logger2.Logger();
|
|
336
|
+
const dispatch = async function(i) {
|
|
337
|
+
if (i <= index)
|
|
338
|
+
return Promise.reject(Error("next() called multiple times"));
|
|
339
|
+
index = i;
|
|
340
|
+
let fn = list[i];
|
|
341
|
+
if (i === list.length)
|
|
342
|
+
fn = next;
|
|
343
|
+
if (!fn)
|
|
344
|
+
return Promise.resolve();
|
|
345
|
+
if (typeof fn.key === "undefined")
|
|
346
|
+
fn.key = `UnNamedPlugin#${i}`;
|
|
347
|
+
logger.debug("[%s][%s] begin", fn.key, key);
|
|
348
|
+
logger.time(fn.key);
|
|
349
|
+
try {
|
|
350
|
+
const res = await Promise.resolve(fn.handler(data, dispatch.bind(null, i + 1)));
|
|
351
|
+
logger.timeEnd(fn.key, "[%s][%s] end", fn.key, key);
|
|
352
|
+
return res;
|
|
353
|
+
} catch (err) {
|
|
354
|
+
logger.timeEnd(fn.key, "[%s][%s] failed", fn.key, key);
|
|
355
|
+
logger.error(err);
|
|
356
|
+
return Promise.reject(err);
|
|
357
|
+
}
|
|
358
|
+
};
|
|
359
|
+
return await dispatch(0);
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
/**
|
|
363
|
+
* Deploy the function
|
|
364
|
+
* @param data {object} data
|
|
365
|
+
* @param data.root {string} root path
|
|
366
|
+
* @param data.filename {string} filename
|
|
367
|
+
* @param data.env {string} environment
|
|
368
|
+
*/
|
|
369
|
+
deploy(data) {
|
|
370
|
+
if (!data.logger)
|
|
371
|
+
data.logger = new import_logger2.Logger("Func");
|
|
372
|
+
data.logger.debug("onDeploy");
|
|
373
|
+
data.logger.debug("Plugins: " + this.plugins.map((p) => `${p.type}#${p.name}`).join(","));
|
|
374
|
+
return this.compose("onDeploy")(data);
|
|
375
|
+
}
|
|
376
|
+
/**
|
|
377
|
+
* First time mount the function
|
|
378
|
+
*/
|
|
379
|
+
async mount(data) {
|
|
380
|
+
if (!data.logger)
|
|
381
|
+
data.logger = new import_logger2.Logger("Func");
|
|
382
|
+
data.logger.debug("onMount");
|
|
383
|
+
if (this.mounted) {
|
|
384
|
+
data.logger.warn("mount() has been called, skipped.");
|
|
385
|
+
return;
|
|
386
|
+
}
|
|
387
|
+
if (!data.config)
|
|
388
|
+
data.config = this.config;
|
|
389
|
+
try {
|
|
390
|
+
data.logger.time("mount");
|
|
391
|
+
data.logger.debug("Plugins: " + this.plugins.map((p) => `${p.type}#${p.name}`).join(","));
|
|
392
|
+
await this.compose("onMount")(data);
|
|
393
|
+
this.mounted = true;
|
|
394
|
+
} finally {
|
|
395
|
+
data.logger.timeEnd("mount", "mounted");
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
/**
|
|
399
|
+
* Invoke the function
|
|
400
|
+
* @param data {object} data
|
|
401
|
+
*/
|
|
402
|
+
async invoke(data) {
|
|
403
|
+
if (!this.mounted)
|
|
404
|
+
await this.mount({
|
|
405
|
+
event: data.event,
|
|
406
|
+
context: data.context,
|
|
407
|
+
config: data.config,
|
|
408
|
+
logger: data.logger
|
|
409
|
+
});
|
|
410
|
+
try {
|
|
411
|
+
await this.compose("onInvoke")(data);
|
|
412
|
+
} catch (error) {
|
|
413
|
+
data.logger.error(error);
|
|
414
|
+
data.response = error;
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
/**
|
|
418
|
+
* Export the function
|
|
419
|
+
*/
|
|
420
|
+
export() {
|
|
421
|
+
const handler = async (event, context, callback) => {
|
|
422
|
+
if (typeof context === "undefined")
|
|
423
|
+
context = {};
|
|
424
|
+
if (!context.request_id)
|
|
425
|
+
context.request_id = (0, import_crypto2.randomBytes)(16).toString("hex");
|
|
426
|
+
if (!context.request_at)
|
|
427
|
+
context.request_at = Math.round((/* @__PURE__ */ new Date()).getTime() / 1e3);
|
|
428
|
+
context.callbackWaitsForEmptyEventLoop = false;
|
|
429
|
+
const logger = new import_logger2.Logger(context.request_id);
|
|
430
|
+
logger.debug("event: %j", event);
|
|
431
|
+
logger.debug("context: %j", context);
|
|
432
|
+
const data = {
|
|
433
|
+
event,
|
|
434
|
+
context,
|
|
435
|
+
callback,
|
|
436
|
+
response: void 0,
|
|
437
|
+
handler: this.handler,
|
|
438
|
+
logger,
|
|
439
|
+
config: this.config
|
|
440
|
+
};
|
|
441
|
+
await this.invoke(data);
|
|
442
|
+
if (Object.prototype.toString.call(data.response) === "[object Error]")
|
|
443
|
+
throw data.response;
|
|
444
|
+
return data.response;
|
|
445
|
+
};
|
|
446
|
+
handler.bind(this);
|
|
447
|
+
return { handler };
|
|
448
|
+
}
|
|
449
|
+
};
|
|
450
|
+
var plugins = [];
|
|
451
|
+
function usePlugin2(plugin) {
|
|
452
|
+
if (!plugins.find((p) => p.name === plugin.name))
|
|
453
|
+
plugins.push(plugin);
|
|
454
|
+
if (!plugin.mount)
|
|
455
|
+
plugin.mount = async function(data) {
|
|
456
|
+
if (plugin.onMount)
|
|
457
|
+
await plugin.onMount({
|
|
458
|
+
config: (data == null ? void 0 : data.config) || /* @__PURE__ */ Object.create(null),
|
|
459
|
+
event: /* @__PURE__ */ Object.create(null),
|
|
460
|
+
context: /* @__PURE__ */ Object.create(null),
|
|
461
|
+
logger: new import_logger2.Logger(plugin.name)
|
|
462
|
+
}, async () => Promise.resolve());
|
|
463
|
+
return plugin;
|
|
464
|
+
};
|
|
465
|
+
return plugin;
|
|
466
|
+
}
|
|
467
|
+
function useFunc(handler) {
|
|
468
|
+
plugins = [];
|
|
469
|
+
const invokeHandler = handler();
|
|
470
|
+
const func = new Func({
|
|
471
|
+
plugins,
|
|
472
|
+
handler: invokeHandler
|
|
473
|
+
});
|
|
474
|
+
plugins = [];
|
|
475
|
+
return func;
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
});
|
|
13
479
|
|
|
14
|
-
// ../deep_merge/
|
|
15
|
-
var
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
let
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
480
|
+
// ../deep_merge/dist/index.js
|
|
481
|
+
var require_dist2 = __commonJS({
|
|
482
|
+
"../deep_merge/dist/index.js"(exports, module) {
|
|
483
|
+
"use strict";
|
|
484
|
+
var __defProp2 = Object.defineProperty;
|
|
485
|
+
var __getOwnPropDesc2 = Object.getOwnPropertyDescriptor;
|
|
486
|
+
var __getOwnPropNames2 = Object.getOwnPropertyNames;
|
|
487
|
+
var __hasOwnProp2 = Object.prototype.hasOwnProperty;
|
|
488
|
+
var __export = (target, all) => {
|
|
489
|
+
for (var name in all)
|
|
490
|
+
__defProp2(target, name, { get: all[name], enumerable: true });
|
|
491
|
+
};
|
|
492
|
+
var __copyProps2 = (to, from, except, desc) => {
|
|
493
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
494
|
+
for (let key of __getOwnPropNames2(from))
|
|
495
|
+
if (!__hasOwnProp2.call(to, key) && key !== except)
|
|
496
|
+
__defProp2(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc2(from, key)) || desc.enumerable });
|
|
497
|
+
}
|
|
498
|
+
return to;
|
|
499
|
+
};
|
|
500
|
+
var __toCommonJS = (mod) => __copyProps2(__defProp2({}, "__esModule", { value: true }), mod);
|
|
501
|
+
var src_exports = {};
|
|
502
|
+
__export(src_exports, {
|
|
503
|
+
deepMerge: () => deepMerge3
|
|
504
|
+
});
|
|
505
|
+
module.exports = __toCommonJS(src_exports);
|
|
506
|
+
var shouldMerge = function(item) {
|
|
507
|
+
const type = Object.prototype.toString.call(item);
|
|
508
|
+
return type === "[object Object]" || type === "[object Array]";
|
|
509
|
+
};
|
|
510
|
+
function deepMerge3(...sources) {
|
|
511
|
+
let acc = /* @__PURE__ */ Object.create(null);
|
|
512
|
+
for (const source of sources)
|
|
513
|
+
if (source instanceof Array) {
|
|
514
|
+
if (!(acc instanceof Array))
|
|
515
|
+
acc = [];
|
|
516
|
+
acc = [...new Set(source.concat(...acc))];
|
|
517
|
+
} else if (shouldMerge(source))
|
|
518
|
+
for (const [key, value] of Object.entries(source)) {
|
|
519
|
+
let val;
|
|
520
|
+
if (shouldMerge(value))
|
|
521
|
+
val = deepMerge3(acc[key], value);
|
|
522
|
+
else
|
|
523
|
+
val = value;
|
|
524
|
+
acc = {
|
|
525
|
+
...acc,
|
|
526
|
+
[key]: val
|
|
527
|
+
};
|
|
528
|
+
}
|
|
529
|
+
return acc;
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
});
|
|
533
|
+
|
|
534
|
+
// ../logger/dist/index.js
|
|
535
|
+
var require_dist3 = __commonJS({
|
|
536
|
+
"../logger/dist/index.js"(exports, module) {
|
|
537
|
+
"use strict";
|
|
538
|
+
var __defProp2 = Object.defineProperty;
|
|
539
|
+
var __getOwnPropDesc2 = Object.getOwnPropertyDescriptor;
|
|
540
|
+
var __getOwnPropNames2 = Object.getOwnPropertyNames;
|
|
541
|
+
var __hasOwnProp2 = Object.prototype.hasOwnProperty;
|
|
542
|
+
var __export = (target, all) => {
|
|
543
|
+
for (var name in all)
|
|
544
|
+
__defProp2(target, name, { get: all[name], enumerable: true });
|
|
545
|
+
};
|
|
546
|
+
var __copyProps2 = (to, from, except, desc) => {
|
|
547
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
548
|
+
for (let key of __getOwnPropNames2(from))
|
|
549
|
+
if (!__hasOwnProp2.call(to, key) && key !== except)
|
|
550
|
+
__defProp2(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc2(from, key)) || desc.enumerable });
|
|
551
|
+
}
|
|
552
|
+
return to;
|
|
553
|
+
};
|
|
554
|
+
var __toCommonJS = (mod) => __copyProps2(__defProp2({}, "__esModule", { value: true }), mod);
|
|
555
|
+
var src_exports = {};
|
|
556
|
+
__export(src_exports, {
|
|
557
|
+
Color: () => Color,
|
|
558
|
+
Logger: () => Logger2
|
|
559
|
+
});
|
|
560
|
+
module.exports = __toCommonJS(src_exports);
|
|
561
|
+
var import_util = __require("util");
|
|
562
|
+
var Color = /* @__PURE__ */ ((Color2) => {
|
|
563
|
+
Color2[Color2["DEFAULT"] = 39] = "DEFAULT";
|
|
564
|
+
Color2[Color2["BLACK"] = 30] = "BLACK";
|
|
565
|
+
Color2[Color2["RED"] = 31] = "RED";
|
|
566
|
+
Color2[Color2["GREEN"] = 32] = "GREEN";
|
|
567
|
+
Color2[Color2["ORANGE"] = 33] = "ORANGE";
|
|
568
|
+
Color2[Color2["BLUE"] = 34] = "BLUE";
|
|
569
|
+
Color2[Color2["MAGENTA"] = 35] = "MAGENTA";
|
|
570
|
+
Color2[Color2["CYAN"] = 36] = "CYAN";
|
|
571
|
+
Color2[Color2["GRAY"] = 90] = "GRAY";
|
|
572
|
+
return Color2;
|
|
573
|
+
})(Color || {});
|
|
574
|
+
var LevelColor = ((LevelColor2) => {
|
|
575
|
+
LevelColor2[
|
|
576
|
+
LevelColor2["debug"] = 90
|
|
577
|
+
/* GRAY */
|
|
578
|
+
] = "debug";
|
|
579
|
+
LevelColor2[
|
|
580
|
+
LevelColor2["info"] = 32
|
|
581
|
+
/* GREEN */
|
|
582
|
+
] = "info";
|
|
583
|
+
LevelColor2[
|
|
584
|
+
LevelColor2["warn"] = 33
|
|
585
|
+
/* ORANGE */
|
|
586
|
+
] = "warn";
|
|
587
|
+
LevelColor2[
|
|
588
|
+
LevelColor2["error"] = 31
|
|
589
|
+
/* RED */
|
|
590
|
+
] = "error";
|
|
591
|
+
return LevelColor2;
|
|
592
|
+
})(LevelColor || {});
|
|
593
|
+
var LevelPriority = {
|
|
594
|
+
debug: 0,
|
|
595
|
+
info: 1,
|
|
596
|
+
warn: 2,
|
|
597
|
+
error: 3
|
|
598
|
+
};
|
|
599
|
+
var Logger2 = class {
|
|
600
|
+
/**
|
|
601
|
+
* @param label {string} Prefix label
|
|
602
|
+
*/
|
|
603
|
+
constructor(label) {
|
|
604
|
+
this.colorfyOutput = true;
|
|
605
|
+
if (label)
|
|
606
|
+
this.label = label;
|
|
607
|
+
this.silent = !process.env.FaasLog && process.env.npm_config_argv && JSON.parse(process.env.npm_config_argv).original.includes("--silent");
|
|
608
|
+
if (["remote", "mono"].includes(process.env.FaasMode))
|
|
609
|
+
this.colorfyOutput = false;
|
|
610
|
+
this.level = process.env.FaasLog ? LevelPriority[process.env.FaasLog.toLowerCase()] : 0;
|
|
611
|
+
this.cachedTimers = {};
|
|
612
|
+
this.size = 1e3;
|
|
613
|
+
this.stdout = console.log;
|
|
614
|
+
this.stderr = console.error;
|
|
615
|
+
}
|
|
616
|
+
/**
|
|
617
|
+
* @param message {string} message
|
|
618
|
+
* @param args {...any=} arguments
|
|
619
|
+
*/
|
|
620
|
+
debug(message, ...args) {
|
|
621
|
+
this.log("debug", message, ...args);
|
|
622
|
+
return this;
|
|
623
|
+
}
|
|
624
|
+
/**
|
|
625
|
+
* @param message {string} message
|
|
626
|
+
* @param args {...any=} arguments
|
|
627
|
+
*/
|
|
628
|
+
info(message, ...args) {
|
|
629
|
+
this.log("info", message, ...args);
|
|
630
|
+
return this;
|
|
631
|
+
}
|
|
632
|
+
/**
|
|
633
|
+
* @param message {string} message
|
|
634
|
+
* @param args {...any=} arguments
|
|
635
|
+
*/
|
|
636
|
+
warn(message, ...args) {
|
|
637
|
+
this.log("warn", message, ...args);
|
|
638
|
+
return this;
|
|
639
|
+
}
|
|
640
|
+
/**
|
|
641
|
+
* @param message {any} message or Error object
|
|
642
|
+
* @param args {...any=} arguments
|
|
643
|
+
*/
|
|
644
|
+
error(message, ...args) {
|
|
645
|
+
let stack = false;
|
|
646
|
+
[message].concat(Array.from(args)).forEach((e) => {
|
|
647
|
+
if (e.stack) {
|
|
648
|
+
stack = true;
|
|
649
|
+
this.log("error", e.stack);
|
|
650
|
+
}
|
|
651
|
+
});
|
|
652
|
+
if (!stack)
|
|
653
|
+
this.log("error", message, ...args);
|
|
654
|
+
return this;
|
|
655
|
+
}
|
|
656
|
+
/**
|
|
657
|
+
* @param key {string} timer's label
|
|
658
|
+
* @param level [string=debug] 日志级别,支持 debug、info、warn、error
|
|
659
|
+
*/
|
|
660
|
+
time(key, level = "debug") {
|
|
661
|
+
this.cachedTimers[key] = {
|
|
662
|
+
level,
|
|
663
|
+
time: (/* @__PURE__ */ new Date()).getTime()
|
|
36
664
|
};
|
|
665
|
+
return this;
|
|
37
666
|
}
|
|
38
|
-
|
|
39
|
-
}
|
|
667
|
+
/**
|
|
668
|
+
* @param key {string} timer's label
|
|
669
|
+
* @param message {string} message
|
|
670
|
+
* @param args {...any=} arguments
|
|
671
|
+
*/
|
|
672
|
+
timeEnd(key, message, ...args) {
|
|
673
|
+
if (this.cachedTimers[key]) {
|
|
674
|
+
const timer = this.cachedTimers[key];
|
|
675
|
+
message = message + " +%ims";
|
|
676
|
+
args.push((/* @__PURE__ */ new Date()).getTime() - timer.time);
|
|
677
|
+
this[timer.level](message, ...args);
|
|
678
|
+
delete this.cachedTimers[key];
|
|
679
|
+
} else {
|
|
680
|
+
this.warn("timeEnd not found key %s", key);
|
|
681
|
+
this.debug(message);
|
|
682
|
+
}
|
|
683
|
+
return this;
|
|
684
|
+
}
|
|
685
|
+
/**
|
|
686
|
+
* @param message {string} message
|
|
687
|
+
* @param args {...any=} arguments
|
|
688
|
+
*/
|
|
689
|
+
raw(message, ...args) {
|
|
690
|
+
if (this.silent)
|
|
691
|
+
return this;
|
|
692
|
+
this.stdout((0, import_util.format)(message, ...args));
|
|
693
|
+
return this;
|
|
694
|
+
}
|
|
695
|
+
/**
|
|
696
|
+
* @param color {number} color code
|
|
697
|
+
* @param message {string} message
|
|
698
|
+
*/
|
|
699
|
+
colorfy(color, message) {
|
|
700
|
+
return `\x1B[0${color}m${message}\x1B[39m`;
|
|
701
|
+
}
|
|
702
|
+
log(level, message, ...args) {
|
|
703
|
+
if (this.silent)
|
|
704
|
+
return this;
|
|
705
|
+
if (LevelPriority[level] < this.level)
|
|
706
|
+
return this;
|
|
707
|
+
let output = level.toUpperCase() + " " + (this.label ? `[${this.label}] ` : "") + (0, import_util.format)(message, ...args);
|
|
708
|
+
if (this.colorfyOutput && level !== "error")
|
|
709
|
+
output = this.colorfy(LevelColor[level], output);
|
|
710
|
+
else if (!this.colorfyOutput)
|
|
711
|
+
output = output.replace(/\n/g, "");
|
|
712
|
+
if (!output)
|
|
713
|
+
return this;
|
|
714
|
+
if (output.length > this.size && !["error", "warn"].includes(level))
|
|
715
|
+
output = output.slice(0, this.size - 100) + "..." + output.slice(output.length - 100);
|
|
716
|
+
if (level === "error")
|
|
717
|
+
this.stderr(output);
|
|
718
|
+
else
|
|
719
|
+
this.stdout(output);
|
|
720
|
+
return this;
|
|
721
|
+
}
|
|
722
|
+
};
|
|
723
|
+
}
|
|
724
|
+
});
|
|
40
725
|
|
|
41
726
|
// src/index.ts
|
|
42
|
-
|
|
727
|
+
var import_func = __toESM(require_dist());
|
|
728
|
+
var import_deep_merge2 = __toESM(require_dist2());
|
|
729
|
+
var import_logger = __toESM(require_dist3());
|
|
43
730
|
|
|
44
731
|
// src/session.ts
|
|
45
732
|
import {
|
|
@@ -52,6 +739,8 @@ import {
|
|
|
52
739
|
var Session = class {
|
|
53
740
|
constructor(cookie, config) {
|
|
54
741
|
this.cookie = cookie;
|
|
742
|
+
if (!(config == null ? void 0 : config.secret))
|
|
743
|
+
console.warn("Session's secret is missing.");
|
|
55
744
|
this.config = Object.assign({
|
|
56
745
|
key: "key",
|
|
57
746
|
secret: randomBytes(128).toString("hex"),
|
|
@@ -106,7 +795,7 @@ var Session = class {
|
|
|
106
795
|
hmac.update(signedParts[0]);
|
|
107
796
|
const digest = hmac.digest("hex");
|
|
108
797
|
if (signedParts[1] !== digest)
|
|
109
|
-
throw Error("Not valid");
|
|
798
|
+
throw Error("Session Not valid");
|
|
110
799
|
const message = Buffer.from(signedParts[0], "base64").toString();
|
|
111
800
|
const parts = message.split("--").map(function(part2) {
|
|
112
801
|
return Buffer.from(part2, "base64");
|
|
@@ -136,9 +825,10 @@ var Session = class {
|
|
|
136
825
|
};
|
|
137
826
|
|
|
138
827
|
// src/cookie.ts
|
|
828
|
+
var import_deep_merge = __toESM(require_dist2());
|
|
139
829
|
var Cookie = class {
|
|
140
830
|
constructor(config) {
|
|
141
|
-
this.config = deepMerge({
|
|
831
|
+
this.config = (0, import_deep_merge.deepMerge)({
|
|
142
832
|
path: "/",
|
|
143
833
|
expires: 31536e3,
|
|
144
834
|
secure: true,
|
|
@@ -420,18 +1110,15 @@ var Http = class {
|
|
|
420
1110
|
this.config = (config == null ? void 0 : config.config) || /* @__PURE__ */ Object.create(null);
|
|
421
1111
|
if (config == null ? void 0 : config.validator)
|
|
422
1112
|
this.validatorOptions = config.validator;
|
|
423
|
-
this.headers = /* @__PURE__ */ Object.create(null);
|
|
424
|
-
this.cookie = new Cookie(this.config.cookie || {});
|
|
425
|
-
this.session = this.cookie.session;
|
|
426
1113
|
}
|
|
427
1114
|
async onDeploy(data, next) {
|
|
428
1115
|
var _a;
|
|
429
1116
|
data.dependencies["@faasjs/http"] = "*";
|
|
430
1117
|
await next();
|
|
431
|
-
const logger = new Logger(this.name);
|
|
1118
|
+
const logger = new import_logger.Logger(this.name);
|
|
432
1119
|
logger.debug("Generate api gateway's config");
|
|
433
1120
|
logger.debug("%j", data);
|
|
434
|
-
const config = data.config.plugins ? deepMerge(data.config.plugins[this.name || this.type], { config: this.config }) : { config: this.config };
|
|
1121
|
+
const config = data.config.plugins ? (0, import_deep_merge2.deepMerge)(data.config.plugins[this.name || this.type], { config: this.config }) : { config: this.config };
|
|
435
1122
|
if (!config.config.path) {
|
|
436
1123
|
config.config.path = "/" + ((_a = data.name) == null ? void 0 : _a.replace(/_/g, "/").replace(/\/index$/, ""));
|
|
437
1124
|
if (config.config.path === "/index")
|
|
@@ -449,8 +1136,25 @@ var Http = class {
|
|
|
449
1136
|
}
|
|
450
1137
|
async onMount(data, next) {
|
|
451
1138
|
data.logger.debug("[onMount] merge config");
|
|
1139
|
+
const prefix = `SECRET_${this.name.toUpperCase()}_`;
|
|
1140
|
+
for (let key in process.env)
|
|
1141
|
+
if (key.startsWith(prefix)) {
|
|
1142
|
+
const value = process.env[key];
|
|
1143
|
+
key = key.replace(prefix, "").toLowerCase();
|
|
1144
|
+
if (key.includes("_")) {
|
|
1145
|
+
let config = this.config;
|
|
1146
|
+
const keys = key.split("_");
|
|
1147
|
+
keys.slice(0, keys.length - 1).forEach((k) => {
|
|
1148
|
+
if (!config[k])
|
|
1149
|
+
config[k] = /* @__PURE__ */ Object.create(null);
|
|
1150
|
+
config = config[k];
|
|
1151
|
+
});
|
|
1152
|
+
config[keys[keys.length - 1]] = value;
|
|
1153
|
+
} else
|
|
1154
|
+
this.config[key] = value;
|
|
1155
|
+
}
|
|
452
1156
|
if (data.config.plugins && data.config.plugins[this.name || this.type])
|
|
453
|
-
this.config = deepMerge(this.config, data.config.plugins[this.name || this.type].config);
|
|
1157
|
+
this.config = (0, import_deep_merge2.deepMerge)(this.config, data.config.plugins[this.name || this.type].config);
|
|
454
1158
|
data.logger.debug("[onMount] prepare cookie & session");
|
|
455
1159
|
this.cookie = new Cookie(this.config.cookie || {});
|
|
456
1160
|
this.session = this.cookie.session;
|
|
@@ -469,7 +1173,7 @@ var Http = class {
|
|
|
469
1173
|
if (data.event.body) {
|
|
470
1174
|
if ((_a = this.headers["content-type"]) == null ? void 0 : _a.includes("application/json")) {
|
|
471
1175
|
data.logger.debug("[onInvoke] Parse params from json body");
|
|
472
|
-
this.params = Object.assign(this.params, JSON.parse(data.event.body));
|
|
1176
|
+
this.params = Object.keys(this.params).length ? Object.assign(this.params, JSON.parse(data.event.body)) : JSON.parse(data.event.body);
|
|
473
1177
|
} else {
|
|
474
1178
|
data.logger.debug("[onInvoke] Parse params from raw body");
|
|
475
1179
|
this.params = data.event.body || /* @__PURE__ */ Object.create(null);
|
|
@@ -544,10 +1248,20 @@ var Http = class {
|
|
|
544
1248
|
delete data.response.headers["Content-Encoding"];
|
|
545
1249
|
}
|
|
546
1250
|
}
|
|
1251
|
+
/**
|
|
1252
|
+
* set header
|
|
1253
|
+
* @param key {string} key
|
|
1254
|
+
* @param value {*} value
|
|
1255
|
+
*/
|
|
547
1256
|
setHeader(key, value) {
|
|
548
1257
|
this.response.headers[key] = value;
|
|
549
1258
|
return this;
|
|
550
1259
|
}
|
|
1260
|
+
/**
|
|
1261
|
+
* set Content-Type
|
|
1262
|
+
* @param type {string} 类型
|
|
1263
|
+
* @param charset {string} 编码
|
|
1264
|
+
*/
|
|
551
1265
|
setContentType(type, charset = "utf-8") {
|
|
552
1266
|
if (ContentType[type])
|
|
553
1267
|
this.setHeader("Content-Type", `${ContentType[type]}; charset=${charset}`);
|
|
@@ -555,17 +1269,25 @@ var Http = class {
|
|
|
555
1269
|
this.setHeader("Content-Type", `${type}; charset=${charset}`);
|
|
556
1270
|
return this;
|
|
557
1271
|
}
|
|
1272
|
+
/**
|
|
1273
|
+
* set status code
|
|
1274
|
+
* @param code {number} 状态码
|
|
1275
|
+
*/
|
|
558
1276
|
setStatusCode(code) {
|
|
559
1277
|
this.response.statusCode = code;
|
|
560
1278
|
return this;
|
|
561
1279
|
}
|
|
1280
|
+
/**
|
|
1281
|
+
* set body
|
|
1282
|
+
* @param body {*} 内容
|
|
1283
|
+
*/
|
|
562
1284
|
setBody(body) {
|
|
563
1285
|
this.response.body = body;
|
|
564
1286
|
return this;
|
|
565
1287
|
}
|
|
566
1288
|
};
|
|
567
1289
|
function useHttp(config) {
|
|
568
|
-
return usePlugin(new Http(config));
|
|
1290
|
+
return (0, import_func.usePlugin)(new Http(config));
|
|
569
1291
|
}
|
|
570
1292
|
export {
|
|
571
1293
|
ContentType,
|