@redmix/api-server 0.0.1
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/LICENSE +21 -0
- package/README.md +64 -0
- package/dist/apiCLIConfig.d.ts +6 -0
- package/dist/apiCLIConfig.d.ts.map +1 -0
- package/dist/apiCLIConfig.js +71 -0
- package/dist/apiCLIConfigHandler.d.ts +3 -0
- package/dist/apiCLIConfigHandler.d.ts.map +1 -0
- package/dist/apiCLIConfigHandler.js +68 -0
- package/dist/bin.d.ts +2 -0
- package/dist/bin.d.ts.map +1 -0
- package/dist/bin.js +850 -0
- package/dist/bothCLIConfig.d.ts +6 -0
- package/dist/bothCLIConfig.d.ts.map +1 -0
- package/dist/bothCLIConfig.js +76 -0
- package/dist/bothCLIConfigHandler.d.ts +3 -0
- package/dist/bothCLIConfigHandler.d.ts.map +1 -0
- package/dist/bothCLIConfigHandler.js +107 -0
- package/dist/buildManager.d.ts +15 -0
- package/dist/buildManager.d.ts.map +1 -0
- package/dist/buildManager.js +74 -0
- package/dist/cliHelpers.d.ts +5 -0
- package/dist/cliHelpers.d.ts.map +1 -0
- package/dist/cliHelpers.js +52 -0
- package/dist/createServer.d.ts +30 -0
- package/dist/createServer.d.ts.map +1 -0
- package/dist/createServer.js +142 -0
- package/dist/createServerHelpers.d.ts +39 -0
- package/dist/createServerHelpers.d.ts.map +1 -0
- package/dist/createServerHelpers.js +101 -0
- package/dist/fastify.d.ts +14 -0
- package/dist/fastify.d.ts.map +1 -0
- package/dist/fastify.js +88 -0
- package/dist/logFormatter/ambient.d.js +1 -0
- package/dist/logFormatter/bin.d.ts +2 -0
- package/dist/logFormatter/bin.d.ts.map +1 -0
- package/dist/logFormatter/bin.js +369 -0
- package/dist/logFormatter/formatters.d.ts +30 -0
- package/dist/logFormatter/formatters.d.ts.map +1 -0
- package/dist/logFormatter/formatters.js +275 -0
- package/dist/logFormatter/index.d.ts +2 -0
- package/dist/logFormatter/index.d.ts.map +1 -0
- package/dist/logFormatter/index.js +184 -0
- package/dist/plugins/api.d.ts +13 -0
- package/dist/plugins/api.d.ts.map +1 -0
- package/dist/plugins/api.js +77 -0
- package/dist/plugins/graphql.d.ts +10 -0
- package/dist/plugins/graphql.d.ts.map +1 -0
- package/dist/plugins/graphql.js +115 -0
- package/dist/plugins/lambdaLoader.d.ts +23 -0
- package/dist/plugins/lambdaLoader.d.ts.map +1 -0
- package/dist/plugins/lambdaLoader.js +116 -0
- package/dist/requestHandlers/awsLambdaFastify.d.ts +5 -0
- package/dist/requestHandlers/awsLambdaFastify.d.ts.map +1 -0
- package/dist/requestHandlers/awsLambdaFastify.js +103 -0
- package/dist/requestHandlers/utils.d.ts +26 -0
- package/dist/requestHandlers/utils.d.ts.map +1 -0
- package/dist/requestHandlers/utils.js +55 -0
- package/dist/serverManager.d.ts +8 -0
- package/dist/serverManager.d.ts.map +1 -0
- package/dist/serverManager.js +138 -0
- package/dist/types.d.ts +19 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/watch.d.ts +2 -0
- package/dist/watch.d.ts.map +1 -0
- package/dist/watch.js +274 -0
- package/package.json +77 -0
@@ -0,0 +1,101 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __defProp = Object.defineProperty;
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
6
|
+
var __export = (target, all) => {
|
7
|
+
for (var name in all)
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
9
|
+
};
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
12
|
+
for (let key of __getOwnPropNames(from))
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
15
|
+
}
|
16
|
+
return to;
|
17
|
+
};
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
19
|
+
var createServerHelpers_exports = {};
|
20
|
+
__export(createServerHelpers_exports, {
|
21
|
+
DEFAULT_CREATE_SERVER_OPTIONS: () => DEFAULT_CREATE_SERVER_OPTIONS,
|
22
|
+
resolveOptions: () => resolveOptions
|
23
|
+
});
|
24
|
+
module.exports = __toCommonJS(createServerHelpers_exports);
|
25
|
+
var import_util = require("util");
|
26
|
+
var import_helpers = require("@redmix/fastify-web/dist/helpers");
|
27
|
+
var import_cliHelpers = require("./cliHelpers");
|
28
|
+
const DEFAULT_CREATE_SERVER_OPTIONS = {
|
29
|
+
apiRootPath: "/",
|
30
|
+
logger: {
|
31
|
+
level: process.env.LOG_LEVEL ?? (process.env.NODE_ENV === "development" ? "debug" : "warn")
|
32
|
+
},
|
33
|
+
fastifyServerOptions: {
|
34
|
+
requestTimeout: 15e3,
|
35
|
+
bodyLimit: 1024 * 1024 * 100
|
36
|
+
// 100MB
|
37
|
+
},
|
38
|
+
configureApiServer: () => {
|
39
|
+
},
|
40
|
+
parseArgs: true
|
41
|
+
};
|
42
|
+
function resolveOptions(options = {}, args) {
|
43
|
+
options.parseArgs ??= true;
|
44
|
+
options.logger ??= DEFAULT_CREATE_SERVER_OPTIONS.logger;
|
45
|
+
const resolvedOptions = {
|
46
|
+
apiRootPath: options.apiRootPath ?? DEFAULT_CREATE_SERVER_OPTIONS.apiRootPath,
|
47
|
+
fastifyServerOptions: options.fastifyServerOptions ?? {
|
48
|
+
requestTimeout: DEFAULT_CREATE_SERVER_OPTIONS.fastifyServerOptions.requestTimeout,
|
49
|
+
logger: options.logger ?? DEFAULT_CREATE_SERVER_OPTIONS.logger,
|
50
|
+
bodyLimit: DEFAULT_CREATE_SERVER_OPTIONS.fastifyServerOptions.bodyLimit
|
51
|
+
},
|
52
|
+
configureApiServer: options.configureApiServer ?? DEFAULT_CREATE_SERVER_OPTIONS.configureApiServer,
|
53
|
+
apiHost: (0, import_cliHelpers.getAPIHost)(),
|
54
|
+
apiPort: (0, import_cliHelpers.getAPIPort)()
|
55
|
+
};
|
56
|
+
resolvedOptions.fastifyServerOptions.requestTimeout ??= DEFAULT_CREATE_SERVER_OPTIONS.fastifyServerOptions.requestTimeout;
|
57
|
+
resolvedOptions.fastifyServerOptions.logger = options.logger;
|
58
|
+
if (options.parseArgs) {
|
59
|
+
const { values } = (0, import_util.parseArgs)({
|
60
|
+
options: {
|
61
|
+
apiHost: {
|
62
|
+
type: "string"
|
63
|
+
},
|
64
|
+
apiPort: {
|
65
|
+
type: "string",
|
66
|
+
short: "p"
|
67
|
+
},
|
68
|
+
apiRootPath: {
|
69
|
+
type: "string"
|
70
|
+
}
|
71
|
+
},
|
72
|
+
strict: false,
|
73
|
+
...args && { args }
|
74
|
+
});
|
75
|
+
if (values.apiHost && typeof values.apiHost !== "string") {
|
76
|
+
throw new Error("`apiHost` must be a string");
|
77
|
+
}
|
78
|
+
if (values.apiHost) {
|
79
|
+
resolvedOptions.apiHost = values.apiHost;
|
80
|
+
}
|
81
|
+
if (values.apiPort) {
|
82
|
+
resolvedOptions.apiPort = +values.apiPort;
|
83
|
+
if (isNaN(resolvedOptions.apiPort)) {
|
84
|
+
throw new Error("`apiPort` must be an integer");
|
85
|
+
}
|
86
|
+
}
|
87
|
+
if (values.apiRootPath && typeof values.apiRootPath !== "string") {
|
88
|
+
throw new Error("`apiRootPath` must be a string");
|
89
|
+
}
|
90
|
+
if (values.apiRootPath) {
|
91
|
+
resolvedOptions.apiRootPath = values.apiRootPath;
|
92
|
+
}
|
93
|
+
}
|
94
|
+
resolvedOptions.apiRootPath = (0, import_helpers.coerceRootPath)(resolvedOptions.apiRootPath);
|
95
|
+
return resolvedOptions;
|
96
|
+
}
|
97
|
+
// Annotate the CommonJS export names for ESM import in node:
|
98
|
+
0 && (module.exports = {
|
99
|
+
DEFAULT_CREATE_SERVER_OPTIONS,
|
100
|
+
resolveOptions
|
101
|
+
});
|
@@ -0,0 +1,14 @@
|
|
1
|
+
import type { FastifyInstance, FastifyServerOptions } from 'fastify';
|
2
|
+
import type { FastifySideConfigFn } from './types';
|
3
|
+
export declare const DEFAULT_OPTIONS: {
|
4
|
+
logger: {
|
5
|
+
level: string;
|
6
|
+
};
|
7
|
+
};
|
8
|
+
export declare function loadFastifyConfig(): Promise<{
|
9
|
+
config: FastifyServerOptions;
|
10
|
+
configureFastify: FastifySideConfigFn;
|
11
|
+
}>;
|
12
|
+
export declare const createFastifyInstance: (options?: FastifyServerOptions) => Promise<FastifyInstance>;
|
13
|
+
export default createFastifyInstance;
|
14
|
+
//# sourceMappingURL=fastify.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"fastify.d.ts","sourceRoot":"","sources":["../src/fastify.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,eAAe,EAAE,oBAAoB,EAAE,MAAM,SAAS,CAAA;AAOpE,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAA;AAGlD,eAAO,MAAM,eAAe;;;;CAI3B,CAAA;AAiBD,wBAAsB,iBAAiB;YAb7B,oBAAoB;sBACV,mBAAmB;GAkCtC;AAED,eAAO,MAAM,qBAAqB,aACtB,oBAAoB,KAC7B,OAAO,CAAC,eAAe,CAWzB,CAAA;AAED,eAAe,qBAAqB,CAAA"}
|
package/dist/fastify.js
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __create = Object.create;
|
3
|
+
var __defProp = Object.defineProperty;
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
8
|
+
var __export = (target, all) => {
|
9
|
+
for (var name in all)
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
11
|
+
};
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
14
|
+
for (let key of __getOwnPropNames(from))
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
17
|
+
}
|
18
|
+
return to;
|
19
|
+
};
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
26
|
+
mod
|
27
|
+
));
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
29
|
+
var fastify_exports = {};
|
30
|
+
__export(fastify_exports, {
|
31
|
+
DEFAULT_OPTIONS: () => DEFAULT_OPTIONS,
|
32
|
+
createFastifyInstance: () => createFastifyInstance,
|
33
|
+
default: () => fastify_default,
|
34
|
+
loadFastifyConfig: () => loadFastifyConfig
|
35
|
+
});
|
36
|
+
module.exports = __toCommonJS(fastify_exports);
|
37
|
+
var import_fs = __toESM(require("fs"));
|
38
|
+
var import_path = __toESM(require("path"));
|
39
|
+
var import_fastify = __toESM(require("fastify"));
|
40
|
+
var import_store = require("@redmix/context/dist/store");
|
41
|
+
var import_project_config = require("@redmix/project-config");
|
42
|
+
const DEFAULT_OPTIONS = {
|
43
|
+
logger: {
|
44
|
+
level: process.env.NODE_ENV === "development" ? "debug" : "info"
|
45
|
+
}
|
46
|
+
};
|
47
|
+
let isServerConfigLoaded = false;
|
48
|
+
let serverConfigFile = {
|
49
|
+
config: DEFAULT_OPTIONS,
|
50
|
+
configureFastify: async (fastify, options) => {
|
51
|
+
fastify.log.trace(
|
52
|
+
options,
|
53
|
+
`In configureFastify hook for side: ${options?.side}`
|
54
|
+
);
|
55
|
+
return fastify;
|
56
|
+
}
|
57
|
+
};
|
58
|
+
async function loadFastifyConfig() {
|
59
|
+
const serverConfigPath = import_path.default.join(
|
60
|
+
(0, import_project_config.getPaths)().base,
|
61
|
+
(0, import_project_config.getConfig)().api.serverConfig
|
62
|
+
);
|
63
|
+
if (!import_fs.default.existsSync(serverConfigPath)) {
|
64
|
+
return serverConfigFile;
|
65
|
+
}
|
66
|
+
if (!isServerConfigLoaded) {
|
67
|
+
console.log(`Loading server config from ${serverConfigPath}`);
|
68
|
+
const config = await import(`file://${serverConfigPath}`);
|
69
|
+
serverConfigFile = { ...config.default };
|
70
|
+
isServerConfigLoaded = true;
|
71
|
+
}
|
72
|
+
return serverConfigFile;
|
73
|
+
}
|
74
|
+
const createFastifyInstance = async (options) => {
|
75
|
+
const { config } = await loadFastifyConfig();
|
76
|
+
const fastify = (0, import_fastify.default)(options || config || DEFAULT_OPTIONS);
|
77
|
+
fastify.addHook("onRequest", (_req, _reply, done) => {
|
78
|
+
(0, import_store.getAsyncStoreInstance)().run(/* @__PURE__ */ new Map(), done);
|
79
|
+
});
|
80
|
+
return fastify;
|
81
|
+
};
|
82
|
+
var fastify_default = createFastifyInstance;
|
83
|
+
// Annotate the CommonJS export names for ESM import in node:
|
84
|
+
0 && (module.exports = {
|
85
|
+
DEFAULT_OPTIONS,
|
86
|
+
createFastifyInstance,
|
87
|
+
loadFastifyConfig
|
88
|
+
});
|
@@ -0,0 +1 @@
|
|
1
|
+
"use strict";
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"bin.d.ts","sourceRoot":"","sources":["../../src/logFormatter/bin.ts"],"names":[],"mappings":""}
|
@@ -0,0 +1,369 @@
|
|
1
|
+
#!/usr/bin/env node
|
2
|
+
"use strict";
|
3
|
+
var __create = Object.create;
|
4
|
+
var __defProp = Object.defineProperty;
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
11
|
+
for (let key of __getOwnPropNames(from))
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
14
|
+
}
|
15
|
+
return to;
|
16
|
+
};
|
17
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
18
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
19
|
+
// file that has been converted to a CommonJS file using a Babel-
|
20
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
21
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
22
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
23
|
+
mod
|
24
|
+
));
|
25
|
+
|
26
|
+
// src/logFormatter/bin.ts
|
27
|
+
var import_split2 = __toESM(require("split2"));
|
28
|
+
|
29
|
+
// src/logFormatter/index.ts
|
30
|
+
var import_fast_json_parse = __toESM(require("fast-json-parse"));
|
31
|
+
|
32
|
+
// src/logFormatter/formatters.ts
|
33
|
+
var import_chalk = __toESM(require("chalk"));
|
34
|
+
var import_pretty_bytes = __toESM(require("pretty-bytes"));
|
35
|
+
var import_pretty_ms = __toESM(require("pretty-ms"));
|
36
|
+
var NEWLINE = "\n";
|
37
|
+
var emojiLog = {
|
38
|
+
warn: "\u{1F6A6}",
|
39
|
+
info: "\u{1F332}",
|
40
|
+
error: "\u{1F6A8}",
|
41
|
+
debug: "\u{1F41B}",
|
42
|
+
fatal: "\u{1F480}",
|
43
|
+
trace: "\u{1F9F5}"
|
44
|
+
};
|
45
|
+
var ignoredCustomData = [
|
46
|
+
"time",
|
47
|
+
"pid",
|
48
|
+
"hostname",
|
49
|
+
"msg",
|
50
|
+
"res",
|
51
|
+
"req",
|
52
|
+
"reqId",
|
53
|
+
"responseTime"
|
54
|
+
];
|
55
|
+
var isObject = (object) => {
|
56
|
+
return object && Object.prototype.toString.apply(object) === "[object Object]";
|
57
|
+
};
|
58
|
+
var isEmptyObject = (object) => {
|
59
|
+
return object && !Object.keys(object).length;
|
60
|
+
};
|
61
|
+
var isPinoLog = (log) => {
|
62
|
+
return log && Object.prototype.hasOwnProperty.call(log, "level");
|
63
|
+
};
|
64
|
+
var isWideEmoji = (character) => {
|
65
|
+
return character !== "\u{1F6A6}";
|
66
|
+
};
|
67
|
+
var formatBundleSize = (bundle) => {
|
68
|
+
const bytes = parseInt(bundle, 10);
|
69
|
+
const size = (0, import_pretty_bytes.default)(bytes).replace(/ /, "");
|
70
|
+
return import_chalk.default.gray(size);
|
71
|
+
};
|
72
|
+
var formatCustom = (query) => {
|
73
|
+
if (!query) {
|
74
|
+
return;
|
75
|
+
}
|
76
|
+
ignoredCustomData.forEach((key) => {
|
77
|
+
delete query[key];
|
78
|
+
});
|
79
|
+
if (!isEmptyObject(query)) {
|
80
|
+
return import_chalk.default.white(
|
81
|
+
NEWLINE + "\u{1F5D2} Custom" + NEWLINE + JSON.stringify(query, null, 2)
|
82
|
+
);
|
83
|
+
}
|
84
|
+
return;
|
85
|
+
};
|
86
|
+
var formatData = (data) => {
|
87
|
+
if (!isEmptyObject(data)) {
|
88
|
+
return import_chalk.default.white(
|
89
|
+
NEWLINE + "\u{1F4E6} Result Data" + NEWLINE + JSON.stringify(data, null, 2)
|
90
|
+
);
|
91
|
+
}
|
92
|
+
return;
|
93
|
+
};
|
94
|
+
var formatDate = (instant) => {
|
95
|
+
const date = new Date(instant);
|
96
|
+
const hours = date.getHours().toString().padStart(2, "0");
|
97
|
+
const minutes = date.getMinutes().toString().padStart(2, "0");
|
98
|
+
const seconds = date.getSeconds().toString().padStart(2, "0");
|
99
|
+
const prettyDate = hours + ":" + minutes + ":" + seconds;
|
100
|
+
return import_chalk.default.gray(prettyDate);
|
101
|
+
};
|
102
|
+
var formatErrorProp = (errorPropValue) => {
|
103
|
+
const errorType = errorPropValue["type"] || "Error";
|
104
|
+
delete errorPropValue["message"];
|
105
|
+
delete errorPropValue["stack"];
|
106
|
+
delete errorPropValue["type"];
|
107
|
+
return import_chalk.default.redBright(
|
108
|
+
NEWLINE + NEWLINE + `\u{1F6A8} ${errorType} Info` + NEWLINE + NEWLINE + JSON.stringify(errorPropValue, null, 2) + NEWLINE
|
109
|
+
);
|
110
|
+
};
|
111
|
+
var formatLevel = (level) => {
|
112
|
+
const emoji = emojiLog[level];
|
113
|
+
const padding = isWideEmoji(emoji) ? "" : " ";
|
114
|
+
return emoji + padding;
|
115
|
+
};
|
116
|
+
var formatLoadTime = (elapsedTime) => {
|
117
|
+
const elapsed = parseInt(elapsedTime, 10);
|
118
|
+
const time = (0, import_pretty_ms.default)(elapsed);
|
119
|
+
return import_chalk.default.gray(time);
|
120
|
+
};
|
121
|
+
var formatMessage = (logData) => {
|
122
|
+
const { level, message } = logData;
|
123
|
+
const msg = formatMessageName(message);
|
124
|
+
let pretty;
|
125
|
+
if (level === "error") {
|
126
|
+
pretty = import_chalk.default.red(msg);
|
127
|
+
}
|
128
|
+
if (level === "trace") {
|
129
|
+
pretty = import_chalk.default.white(msg);
|
130
|
+
}
|
131
|
+
if (level === "warn") {
|
132
|
+
const orange = "#ffa500";
|
133
|
+
pretty = import_chalk.default.hex(orange)(msg);
|
134
|
+
}
|
135
|
+
if (level === "debug") {
|
136
|
+
pretty = import_chalk.default.yellow(msg);
|
137
|
+
}
|
138
|
+
if (level === "info" || level === "customlevel") {
|
139
|
+
pretty = import_chalk.default.green(msg);
|
140
|
+
}
|
141
|
+
if (level === "fatal") {
|
142
|
+
pretty = import_chalk.default.white.bgRed(msg);
|
143
|
+
}
|
144
|
+
return pretty;
|
145
|
+
};
|
146
|
+
var formatMethod = (method) => {
|
147
|
+
return method && import_chalk.default.white(method);
|
148
|
+
};
|
149
|
+
var formatRequestId = (requestId) => {
|
150
|
+
return requestId && import_chalk.default.cyan(requestId);
|
151
|
+
};
|
152
|
+
var formatNs = (ns) => {
|
153
|
+
return ns && import_chalk.default.cyan(ns);
|
154
|
+
};
|
155
|
+
var formatName = (name) => {
|
156
|
+
return name && import_chalk.default.blue(name);
|
157
|
+
};
|
158
|
+
var formatMessageName = (message) => {
|
159
|
+
if (message === void 0) {
|
160
|
+
return "";
|
161
|
+
}
|
162
|
+
if (message === "request") {
|
163
|
+
return "<--";
|
164
|
+
}
|
165
|
+
if (message === "response") {
|
166
|
+
return "-->";
|
167
|
+
}
|
168
|
+
return message;
|
169
|
+
};
|
170
|
+
var formatOperationName = (operationName) => {
|
171
|
+
return import_chalk.default.white(NEWLINE + "\u{1F3F7} " + operationName);
|
172
|
+
};
|
173
|
+
var formatQuery = (query) => {
|
174
|
+
if (!isEmptyObject(query)) {
|
175
|
+
return import_chalk.default.white(
|
176
|
+
NEWLINE + "\u{1F52D} Query" + NEWLINE + JSON.stringify(query, null, 2)
|
177
|
+
);
|
178
|
+
}
|
179
|
+
return;
|
180
|
+
};
|
181
|
+
var formatResponseCache = (responseCache) => {
|
182
|
+
if (!isEmptyObject(responseCache)) {
|
183
|
+
return import_chalk.default.white(
|
184
|
+
NEWLINE + "\u{1F4BE} Response Cache" + NEWLINE + JSON.stringify(responseCache, null, 2)
|
185
|
+
);
|
186
|
+
}
|
187
|
+
return;
|
188
|
+
};
|
189
|
+
var formatStatusCode = (statusCode) => {
|
190
|
+
statusCode = statusCode || "xxx";
|
191
|
+
return import_chalk.default.white(statusCode);
|
192
|
+
};
|
193
|
+
var formatStack = (stack) => {
|
194
|
+
return import_chalk.default.redBright(
|
195
|
+
stack ? NEWLINE + "\u{1F95E} Error Stack" + NEWLINE + NEWLINE + stack + NEWLINE : ""
|
196
|
+
);
|
197
|
+
};
|
198
|
+
var formatTracing = (data) => {
|
199
|
+
if (!isEmptyObject(data)) {
|
200
|
+
return import_chalk.default.white(
|
201
|
+
NEWLINE + "\u23F0 Timing" + NEWLINE + JSON.stringify(data, null, 2)
|
202
|
+
);
|
203
|
+
}
|
204
|
+
return;
|
205
|
+
};
|
206
|
+
var formatUrl = (url) => {
|
207
|
+
return import_chalk.default.white(url);
|
208
|
+
};
|
209
|
+
var formatUserAgent = (userAgent) => {
|
210
|
+
return import_chalk.default.grey(NEWLINE + "\u{1F575}\uFE0F\u200D\u2640\uFE0F " + userAgent);
|
211
|
+
};
|
212
|
+
var noEmpty = (value) => {
|
213
|
+
return !!value;
|
214
|
+
};
|
215
|
+
|
216
|
+
// src/logFormatter/index.ts
|
217
|
+
var LogFormatter = () => {
|
218
|
+
const parse = (inputData) => {
|
219
|
+
let logData;
|
220
|
+
if (typeof inputData === "string") {
|
221
|
+
const parsedData = (0, import_fast_json_parse.default)(inputData);
|
222
|
+
if (!parsedData.value || parsedData.err || !isPinoLog(parsedData.value)) {
|
223
|
+
return inputData + NEWLINE;
|
224
|
+
}
|
225
|
+
logData = parsedData.value;
|
226
|
+
} else if (isObject(inputData) && isPinoLog(inputData)) {
|
227
|
+
logData = inputData;
|
228
|
+
} else {
|
229
|
+
return inputData + NEWLINE;
|
230
|
+
}
|
231
|
+
if (!logData.level) {
|
232
|
+
return inputData + NEWLINE;
|
233
|
+
}
|
234
|
+
if (!logData.message) {
|
235
|
+
logData.message = logData.msg;
|
236
|
+
}
|
237
|
+
if (typeof logData.level === "number") {
|
238
|
+
convertLogNumber(logData);
|
239
|
+
}
|
240
|
+
return output2(logData) + NEWLINE;
|
241
|
+
};
|
242
|
+
const convertLogNumber = (logData) => {
|
243
|
+
if (logData.level === 10) {
|
244
|
+
logData.level = "trace";
|
245
|
+
}
|
246
|
+
if (logData.level === 20) {
|
247
|
+
logData.level = "debug";
|
248
|
+
}
|
249
|
+
if (logData.level === 30) {
|
250
|
+
logData.level = "info";
|
251
|
+
}
|
252
|
+
if (logData.level === 40) {
|
253
|
+
logData.level = "warn";
|
254
|
+
}
|
255
|
+
if (logData.level === 50) {
|
256
|
+
logData.level = "error";
|
257
|
+
}
|
258
|
+
if (logData.level === 60) {
|
259
|
+
logData.level = "fatal";
|
260
|
+
}
|
261
|
+
};
|
262
|
+
const output2 = (logData) => {
|
263
|
+
const output3 = [];
|
264
|
+
output3.push(formatDate(logData.time || Date.now()));
|
265
|
+
output3.push(formatLevel(logData.level));
|
266
|
+
output3.push(formatNs(logData.ns));
|
267
|
+
output3.push(formatName(logData.name));
|
268
|
+
output3.push(formatRequestId(logData.requestId));
|
269
|
+
output3.push(formatMessage(logData));
|
270
|
+
const req = logData.req;
|
271
|
+
const res = logData.res;
|
272
|
+
const { statusCode: responseStatusCode } = res || {};
|
273
|
+
const { method: requestMethod, url: requestUrl } = req || {};
|
274
|
+
const {
|
275
|
+
level,
|
276
|
+
message,
|
277
|
+
name,
|
278
|
+
ns,
|
279
|
+
err: logDataErr,
|
280
|
+
stack: logDataStack,
|
281
|
+
statusCode: logDataStatusCode,
|
282
|
+
elapsed,
|
283
|
+
responseTime: logDataResponseTime,
|
284
|
+
method: logDataMethod,
|
285
|
+
custom,
|
286
|
+
contentLength,
|
287
|
+
operationName,
|
288
|
+
query,
|
289
|
+
data: graphQLData,
|
290
|
+
responseCache,
|
291
|
+
tracing,
|
292
|
+
url: logDataUrl,
|
293
|
+
userAgent,
|
294
|
+
...rest
|
295
|
+
} = logData;
|
296
|
+
const statusCode = responseStatusCode || logDataStatusCode;
|
297
|
+
const responseTime = logDataResponseTime || elapsed;
|
298
|
+
const method = requestMethod || logDataMethod;
|
299
|
+
const url = requestUrl || logDataUrl;
|
300
|
+
const logDataErrStack = logDataErr && logDataErr.stack;
|
301
|
+
const stack = level === "fatal" || level === "error" ? logDataStack || logDataErr && logDataErrStack : null;
|
302
|
+
const err = (level === "fatal" || level === "error") && logDataErr && Object.keys(logDataErr).find((key) => key !== "stack") ? logDataErr : null;
|
303
|
+
if (!message) {
|
304
|
+
logData.message = "";
|
305
|
+
}
|
306
|
+
if (!level) {
|
307
|
+
logData.level = "customlevel";
|
308
|
+
}
|
309
|
+
if (!name) {
|
310
|
+
logData.name = "";
|
311
|
+
}
|
312
|
+
if (!ns) {
|
313
|
+
logData.ns = "";
|
314
|
+
}
|
315
|
+
if (method != null) {
|
316
|
+
output3.push(formatMethod(method));
|
317
|
+
output3.push(formatStatusCode(statusCode));
|
318
|
+
}
|
319
|
+
if (url != null) {
|
320
|
+
output3.push(formatUrl(url));
|
321
|
+
}
|
322
|
+
if (contentLength != null) {
|
323
|
+
output3.push(formatBundleSize(contentLength));
|
324
|
+
}
|
325
|
+
if (custom) {
|
326
|
+
output3.push(formatCustom(custom));
|
327
|
+
}
|
328
|
+
if (responseTime != null) {
|
329
|
+
output3.push(formatLoadTime(responseTime));
|
330
|
+
}
|
331
|
+
if (userAgent != null) {
|
332
|
+
output3.push(formatUserAgent(userAgent));
|
333
|
+
}
|
334
|
+
if (operationName != null) {
|
335
|
+
output3.push(formatOperationName(operationName));
|
336
|
+
}
|
337
|
+
if (query != null) {
|
338
|
+
output3.push(formatQuery(query));
|
339
|
+
}
|
340
|
+
if (graphQLData != null) {
|
341
|
+
output3.push(formatData(graphQLData));
|
342
|
+
}
|
343
|
+
if (responseCache != null) {
|
344
|
+
output3.push(formatResponseCache(responseCache));
|
345
|
+
}
|
346
|
+
if (tracing != null) {
|
347
|
+
output3.push(formatTracing(tracing));
|
348
|
+
}
|
349
|
+
if (err != null) {
|
350
|
+
output3.push(formatErrorProp(err));
|
351
|
+
}
|
352
|
+
if (stack != null) {
|
353
|
+
output3.push(formatStack(stack));
|
354
|
+
}
|
355
|
+
if (rest) {
|
356
|
+
output3.push(formatCustom(rest));
|
357
|
+
}
|
358
|
+
return output3.filter(noEmpty).join(" ");
|
359
|
+
};
|
360
|
+
return parse;
|
361
|
+
};
|
362
|
+
|
363
|
+
// src/logFormatter/bin.ts
|
364
|
+
var input = process.stdin;
|
365
|
+
var output = process.stdout;
|
366
|
+
input.pipe((0, import_split2.default)(LogFormatter())).pipe(output);
|
367
|
+
process.on("SIGINT", () => {
|
368
|
+
process.exit(0);
|
369
|
+
});
|
@@ -0,0 +1,30 @@
|
|
1
|
+
export declare const NEWLINE = "\n";
|
2
|
+
export declare const emojiLog: Record<string, string>;
|
3
|
+
export declare const ignoredCustomData: string[];
|
4
|
+
export declare const isObject: (object?: Record<string, unknown>) => boolean | undefined;
|
5
|
+
export declare const isEmptyObject: (object?: Record<string, unknown>) => boolean | undefined;
|
6
|
+
export declare const isPinoLog: (log?: Record<string, unknown>) => boolean | undefined;
|
7
|
+
export declare const isWideEmoji: (character: string) => boolean;
|
8
|
+
export declare const formatBundleSize: (bundle: string) => string;
|
9
|
+
export declare const formatCustom: (query?: Record<string, unknown>) => string | undefined;
|
10
|
+
export declare const formatData: (data?: Record<string, unknown>) => string | undefined;
|
11
|
+
export declare const formatDate: (instant: Date) => string;
|
12
|
+
export declare const formatErrorProp: (errorPropValue: Record<string, unknown>) => string;
|
13
|
+
export declare const formatLevel: (level: any) => string;
|
14
|
+
export declare const formatLoadTime: (elapsedTime: any) => string;
|
15
|
+
export declare const formatMessage: (logData: any) => string | undefined;
|
16
|
+
export declare const formatMethod: (method: string) => string;
|
17
|
+
export declare const formatRequestId: (requestId: string) => string;
|
18
|
+
export declare const formatNs: (ns: string) => string;
|
19
|
+
export declare const formatName: (name: string) => string;
|
20
|
+
export declare const formatMessageName: (message: string) => string;
|
21
|
+
export declare const formatOperationName: (operationName: string) => string;
|
22
|
+
export declare const formatQuery: (query?: Record<string, unknown>) => string | undefined;
|
23
|
+
export declare const formatResponseCache: (responseCache?: Record<string, unknown>) => string | undefined;
|
24
|
+
export declare const formatStatusCode: (statusCode: string) => string;
|
25
|
+
export declare const formatStack: (stack?: string | Record<string, unknown>) => string;
|
26
|
+
export declare const formatTracing: (data?: Record<string, unknown>) => string | undefined;
|
27
|
+
export declare const formatUrl: (url: string) => string;
|
28
|
+
export declare const formatUserAgent: (userAgent: string) => string;
|
29
|
+
export declare const noEmpty: (value: any) => boolean;
|
30
|
+
//# sourceMappingURL=formatters.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"formatters.d.ts","sourceRoot":"","sources":["../../src/logFormatter/formatters.ts"],"names":[],"mappings":"AAIA,eAAO,MAAM,OAAO,OAAO,CAAA;AAE3B,eAAO,MAAM,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAO3C,CAAA;AAED,eAAO,MAAM,iBAAiB,EAAE,MAAM,EASrC,CAAA;AAED,eAAO,MAAM,QAAQ,YAAa,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,wBAExD,CAAA;AAED,eAAO,MAAM,aAAa,YAAa,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,wBAE7D,CAAA;AAED,eAAO,MAAM,SAAS,SAAU,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,wBAEtD,CAAA;AAED,eAAO,MAAM,WAAW,cAAe,MAAM,YAE5C,CAAA;AAED,eAAO,MAAM,gBAAgB,WAAY,MAAM,WAI9C,CAAA;AAED,eAAO,MAAM,YAAY,WAAY,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,uBAgB3D,CAAA;AAED,eAAO,MAAM,UAAU,UAAW,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,uBAQxD,CAAA;AAED,eAAO,MAAM,UAAU,YAAa,IAAI,WAOvC,CAAA;AAED,eAAO,MAAM,eAAe,mBAAoB,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,WAgBtE,CAAA;AAED,eAAO,MAAM,WAAW,UAAW,GAAG,WAIrC,CAAA;AAED,eAAO,MAAM,cAAc,gBAAiB,GAAG,WAI9C,CAAA;AAED,eAAO,MAAM,aAAa,YAAa,GAAG,uBAyBzC,CAAA;AAED,eAAO,MAAM,YAAY,WAAY,MAAM,WAE1C,CAAA;AAED,eAAO,MAAM,eAAe,cAAe,MAAM,WAEhD,CAAA;AAED,eAAO,MAAM,QAAQ,OAAQ,MAAM,WAElC,CAAA;AAED,eAAO,MAAM,UAAU,SAAU,MAAM,WAEtC,CAAA;AAED,eAAO,MAAM,iBAAiB,YAAa,MAAM,WAYhD,CAAA;AAED,eAAO,MAAM,mBAAmB,kBAAmB,MAAM,WAExD,CAAA;AAED,eAAO,MAAM,WAAW,WAAY,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,uBAQ1D,CAAA;AAED,eAAO,MAAM,mBAAmB,mBACd,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,uBAYxC,CAAA;AAED,eAAO,MAAM,gBAAgB,eAAgB,MAAM,WAGlD,CAAA;AAED,eAAO,MAAM,WAAW,WAAY,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,WAMnE,CAAA;AAED,eAAO,MAAM,aAAa,UAAW,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,uBAQ3D,CAAA;AAED,eAAO,MAAM,SAAS,QAAS,MAAM,WAEpC,CAAA;AAED,eAAO,MAAM,eAAe,cAAe,MAAM,WAEhD,CAAA;AAED,eAAO,MAAM,OAAO,UAAW,GAAG,YAEjC,CAAA"}
|