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