@faasjs/request 0.0.3-beta.37 → 0.0.3-beta.38
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 +107 -2
- package/dist/index.mjs +106 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -36,7 +36,112 @@ var https = __toESM(require("https"));
|
|
|
36
36
|
var import_url = require("url");
|
|
37
37
|
var import_fs = require("fs");
|
|
38
38
|
var import_path = require("path");
|
|
39
|
-
|
|
39
|
+
|
|
40
|
+
// ../logger/src/index.ts
|
|
41
|
+
var import_util = require("util");
|
|
42
|
+
var LevelColor = ((LevelColor2) => {
|
|
43
|
+
LevelColor2[LevelColor2["debug"] = 90 /* GRAY */] = "debug";
|
|
44
|
+
LevelColor2[LevelColor2["info"] = 32 /* GREEN */] = "info";
|
|
45
|
+
LevelColor2[LevelColor2["warn"] = 33 /* ORANGE */] = "warn";
|
|
46
|
+
LevelColor2[LevelColor2["error"] = 31 /* RED */] = "error";
|
|
47
|
+
return LevelColor2;
|
|
48
|
+
})(LevelColor || {});
|
|
49
|
+
var LevelPriority = {
|
|
50
|
+
debug: 0,
|
|
51
|
+
info: 1,
|
|
52
|
+
warn: 2,
|
|
53
|
+
error: 3
|
|
54
|
+
};
|
|
55
|
+
var Logger = class {
|
|
56
|
+
constructor(label) {
|
|
57
|
+
this.colorfyOutput = true;
|
|
58
|
+
if (label)
|
|
59
|
+
this.label = label;
|
|
60
|
+
this.silent = !process.env.FaasLog && process.env.npm_config_argv && JSON.parse(process.env.npm_config_argv).original.includes("--silent");
|
|
61
|
+
if (["remote", "mono"].includes(process.env.FaasMode))
|
|
62
|
+
this.colorfyOutput = false;
|
|
63
|
+
this.level = process.env.FaasLog ? LevelPriority[process.env.FaasLog.toLowerCase()] : 0;
|
|
64
|
+
this.cachedTimers = {};
|
|
65
|
+
this.size = 1e3;
|
|
66
|
+
this.stdout = console.log;
|
|
67
|
+
this.stderr = console.error;
|
|
68
|
+
}
|
|
69
|
+
debug(message, ...args) {
|
|
70
|
+
this.log("debug", message, ...args);
|
|
71
|
+
return this;
|
|
72
|
+
}
|
|
73
|
+
info(message, ...args) {
|
|
74
|
+
this.log("info", message, ...args);
|
|
75
|
+
return this;
|
|
76
|
+
}
|
|
77
|
+
warn(message, ...args) {
|
|
78
|
+
this.log("warn", message, ...args);
|
|
79
|
+
return this;
|
|
80
|
+
}
|
|
81
|
+
error(message, ...args) {
|
|
82
|
+
let stack = false;
|
|
83
|
+
[message].concat(Array.from(args)).forEach((e) => {
|
|
84
|
+
if (e.stack) {
|
|
85
|
+
stack = true;
|
|
86
|
+
this.log("error", e.stack);
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
if (!stack)
|
|
90
|
+
this.log("error", message, ...args);
|
|
91
|
+
return this;
|
|
92
|
+
}
|
|
93
|
+
time(key, level = "debug") {
|
|
94
|
+
this.cachedTimers[key] = {
|
|
95
|
+
level,
|
|
96
|
+
time: new Date().getTime()
|
|
97
|
+
};
|
|
98
|
+
return this;
|
|
99
|
+
}
|
|
100
|
+
timeEnd(key, message, ...args) {
|
|
101
|
+
if (this.cachedTimers[key]) {
|
|
102
|
+
const timer = this.cachedTimers[key];
|
|
103
|
+
message = message + " +%ims";
|
|
104
|
+
args.push(new Date().getTime() - timer.time);
|
|
105
|
+
this[timer.level](message, ...args);
|
|
106
|
+
delete this.cachedTimers[key];
|
|
107
|
+
} else {
|
|
108
|
+
this.warn("timeEnd not found key %s", key);
|
|
109
|
+
this.debug(message);
|
|
110
|
+
}
|
|
111
|
+
return this;
|
|
112
|
+
}
|
|
113
|
+
raw(message, ...args) {
|
|
114
|
+
if (this.silent)
|
|
115
|
+
return this;
|
|
116
|
+
this.stdout((0, import_util.format)(message, ...args));
|
|
117
|
+
return this;
|
|
118
|
+
}
|
|
119
|
+
colorfy(color, message) {
|
|
120
|
+
return `\x1B[0${color}m${message}\x1B[39m`;
|
|
121
|
+
}
|
|
122
|
+
log(level, message, ...args) {
|
|
123
|
+
if (this.silent)
|
|
124
|
+
return this;
|
|
125
|
+
if (LevelPriority[level] < this.level)
|
|
126
|
+
return this;
|
|
127
|
+
let output = level.toUpperCase() + " " + (this.label ? `[${this.label}] ` : "") + (0, import_util.format)(message, ...args);
|
|
128
|
+
if (this.colorfyOutput && level !== "error")
|
|
129
|
+
output = this.colorfy(LevelColor[level], output);
|
|
130
|
+
else if (!this.colorfyOutput)
|
|
131
|
+
output = output.replace(/\n/g, "");
|
|
132
|
+
if (!output)
|
|
133
|
+
return this;
|
|
134
|
+
if (output.length > this.size && !["error", "warn"].includes(level))
|
|
135
|
+
output = output.slice(0, this.size - 100) + "..." + output.slice(output.length - 100);
|
|
136
|
+
if (level === "error")
|
|
137
|
+
this.stderr(output);
|
|
138
|
+
else
|
|
139
|
+
this.stdout(output);
|
|
140
|
+
return this;
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
// src/index.ts
|
|
40
145
|
var mock = null;
|
|
41
146
|
function setMock(handler) {
|
|
42
147
|
mock = handler;
|
|
@@ -76,7 +181,7 @@ async function request(url, {
|
|
|
76
181
|
logger
|
|
77
182
|
} = { headers: {} }) {
|
|
78
183
|
if (!logger)
|
|
79
|
-
logger = new
|
|
184
|
+
logger = new Logger("request");
|
|
80
185
|
if (mock)
|
|
81
186
|
return mock(url, {
|
|
82
187
|
headers,
|
package/dist/index.mjs
CHANGED
|
@@ -4,7 +4,112 @@ import * as https from "https";
|
|
|
4
4
|
import { URL } from "url";
|
|
5
5
|
import { readFileSync } from "fs";
|
|
6
6
|
import { basename } from "path";
|
|
7
|
-
|
|
7
|
+
|
|
8
|
+
// ../logger/src/index.ts
|
|
9
|
+
import { format } from "util";
|
|
10
|
+
var LevelColor = ((LevelColor2) => {
|
|
11
|
+
LevelColor2[LevelColor2["debug"] = 90 /* GRAY */] = "debug";
|
|
12
|
+
LevelColor2[LevelColor2["info"] = 32 /* GREEN */] = "info";
|
|
13
|
+
LevelColor2[LevelColor2["warn"] = 33 /* ORANGE */] = "warn";
|
|
14
|
+
LevelColor2[LevelColor2["error"] = 31 /* RED */] = "error";
|
|
15
|
+
return LevelColor2;
|
|
16
|
+
})(LevelColor || {});
|
|
17
|
+
var LevelPriority = {
|
|
18
|
+
debug: 0,
|
|
19
|
+
info: 1,
|
|
20
|
+
warn: 2,
|
|
21
|
+
error: 3
|
|
22
|
+
};
|
|
23
|
+
var Logger = class {
|
|
24
|
+
constructor(label) {
|
|
25
|
+
this.colorfyOutput = true;
|
|
26
|
+
if (label)
|
|
27
|
+
this.label = label;
|
|
28
|
+
this.silent = !process.env.FaasLog && process.env.npm_config_argv && JSON.parse(process.env.npm_config_argv).original.includes("--silent");
|
|
29
|
+
if (["remote", "mono"].includes(process.env.FaasMode))
|
|
30
|
+
this.colorfyOutput = false;
|
|
31
|
+
this.level = process.env.FaasLog ? LevelPriority[process.env.FaasLog.toLowerCase()] : 0;
|
|
32
|
+
this.cachedTimers = {};
|
|
33
|
+
this.size = 1e3;
|
|
34
|
+
this.stdout = console.log;
|
|
35
|
+
this.stderr = console.error;
|
|
36
|
+
}
|
|
37
|
+
debug(message, ...args) {
|
|
38
|
+
this.log("debug", message, ...args);
|
|
39
|
+
return this;
|
|
40
|
+
}
|
|
41
|
+
info(message, ...args) {
|
|
42
|
+
this.log("info", message, ...args);
|
|
43
|
+
return this;
|
|
44
|
+
}
|
|
45
|
+
warn(message, ...args) {
|
|
46
|
+
this.log("warn", message, ...args);
|
|
47
|
+
return this;
|
|
48
|
+
}
|
|
49
|
+
error(message, ...args) {
|
|
50
|
+
let stack = false;
|
|
51
|
+
[message].concat(Array.from(args)).forEach((e) => {
|
|
52
|
+
if (e.stack) {
|
|
53
|
+
stack = true;
|
|
54
|
+
this.log("error", e.stack);
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
if (!stack)
|
|
58
|
+
this.log("error", message, ...args);
|
|
59
|
+
return this;
|
|
60
|
+
}
|
|
61
|
+
time(key, level = "debug") {
|
|
62
|
+
this.cachedTimers[key] = {
|
|
63
|
+
level,
|
|
64
|
+
time: new Date().getTime()
|
|
65
|
+
};
|
|
66
|
+
return this;
|
|
67
|
+
}
|
|
68
|
+
timeEnd(key, message, ...args) {
|
|
69
|
+
if (this.cachedTimers[key]) {
|
|
70
|
+
const timer = this.cachedTimers[key];
|
|
71
|
+
message = message + " +%ims";
|
|
72
|
+
args.push(new Date().getTime() - timer.time);
|
|
73
|
+
this[timer.level](message, ...args);
|
|
74
|
+
delete this.cachedTimers[key];
|
|
75
|
+
} else {
|
|
76
|
+
this.warn("timeEnd not found key %s", key);
|
|
77
|
+
this.debug(message);
|
|
78
|
+
}
|
|
79
|
+
return this;
|
|
80
|
+
}
|
|
81
|
+
raw(message, ...args) {
|
|
82
|
+
if (this.silent)
|
|
83
|
+
return this;
|
|
84
|
+
this.stdout(format(message, ...args));
|
|
85
|
+
return this;
|
|
86
|
+
}
|
|
87
|
+
colorfy(color, message) {
|
|
88
|
+
return `\x1B[0${color}m${message}\x1B[39m`;
|
|
89
|
+
}
|
|
90
|
+
log(level, message, ...args) {
|
|
91
|
+
if (this.silent)
|
|
92
|
+
return this;
|
|
93
|
+
if (LevelPriority[level] < this.level)
|
|
94
|
+
return this;
|
|
95
|
+
let output = level.toUpperCase() + " " + (this.label ? `[${this.label}] ` : "") + format(message, ...args);
|
|
96
|
+
if (this.colorfyOutput && level !== "error")
|
|
97
|
+
output = this.colorfy(LevelColor[level], output);
|
|
98
|
+
else if (!this.colorfyOutput)
|
|
99
|
+
output = output.replace(/\n/g, "");
|
|
100
|
+
if (!output)
|
|
101
|
+
return this;
|
|
102
|
+
if (output.length > this.size && !["error", "warn"].includes(level))
|
|
103
|
+
output = output.slice(0, this.size - 100) + "..." + output.slice(output.length - 100);
|
|
104
|
+
if (level === "error")
|
|
105
|
+
this.stderr(output);
|
|
106
|
+
else
|
|
107
|
+
this.stdout(output);
|
|
108
|
+
return this;
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
// src/index.ts
|
|
8
113
|
var mock = null;
|
|
9
114
|
function setMock(handler) {
|
|
10
115
|
mock = handler;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@faasjs/request",
|
|
3
|
-
"version": "0.0.3-beta.
|
|
3
|
+
"version": "0.0.3-beta.38",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
"dist"
|
|
23
23
|
],
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@faasjs/logger": "^0.0.3-beta.
|
|
25
|
+
"@faasjs/logger": "^0.0.3-beta.38"
|
|
26
26
|
},
|
|
27
27
|
"engines": {
|
|
28
28
|
"npm": ">=8.0.0",
|