@cley_faye/boilerplate 3.21.3 → 4.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/README.md +7 -219
- package/bin/serve_spa.js +0 -1
- package/lib/bin/serve_spa.js +67 -39
- package/lib/express/middlewares/singlepageapp.d.ts +2 -2
- package/lib/express/middlewares/singlepageapp.js +20 -13
- package/lib/grunt/args.d.ts +14 -42
- package/lib/grunt/args.js +27 -41
- package/lib/grunt/reactapp/copy.d.ts +8 -34
- package/lib/grunt/reactapp/copy.js +47 -49
- package/lib/grunt/reactapp/image.d.ts +8 -23
- package/lib/grunt/reactapp/image.js +45 -42
- package/lib/grunt/reactapp/pug.d.ts +8 -30
- package/lib/grunt/reactapp/pug.js +49 -50
- package/lib/grunt/reactapp/sass.d.ts +8 -29
- package/lib/grunt/reactapp/sass.js +48 -43
- package/lib/grunt/reactapp/util.d.ts +5 -9
- package/lib/grunt/reactapp/util.js +12 -15
- package/lib/grunt/reactapp/webpack.d.ts +9 -80
- package/lib/grunt/reactapp/webpack.js +71 -125
- package/lib/grunt/reactapp.d.ts +22 -64
- package/lib/grunt/reactapp.js +70 -93
- package/lib/grunt/util.d.ts +4 -7
- package/lib/grunt/util.js +5 -8
- package/lib/grunt.d.ts +2 -3
- package/lib/grunt.js +5 -5
- package/lib/util.js +5 -4
- package/package.json +22 -29
- package/grunt.js +0 -3
- package/lib/express/autoclose.d.ts +0 -17
- package/lib/express/autoclose.js +0 -76
- package/lib/express/logger.d.ts +0 -25
- package/lib/express/logger.js +0 -132
- package/lib/express/middlewares/bodylogger.d.ts +0 -9
- package/lib/express/middlewares/bodylogger.js +0 -90
- package/lib/express/pipelinebuilder.d.ts +0 -115
- package/lib/express/pipelinebuilder.js +0 -288
- package/lib/express/unprocessableentityerror.d.ts +0 -8
- package/lib/express/unprocessableentityerror.js +0 -57
- package/lib/express.d.ts +0 -156
- package/lib/express.js +0 -218
- package/lib/repl.d.ts +0 -27
- package/lib/repl.js +0 -68
- package/lib/winston.d.ts +0 -38
- package/lib/winston.js +0 -224
package/lib/express.js
DELETED
|
@@ -1,218 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @license
|
|
3
|
-
* @preserve
|
|
4
|
-
* MIT License
|
|
5
|
-
*
|
|
6
|
-
* Copyright (c) 2021 Gabriel Paul "Cley Faye" Risterucci
|
|
7
|
-
*
|
|
8
|
-
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
9
|
-
* of this software and associated documentation files (the "Software"), to deal
|
|
10
|
-
* in the Software without restriction, including without limitation the rights
|
|
11
|
-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
12
|
-
* copies of the Software, and to permit persons to whom the Software is
|
|
13
|
-
* furnished to do so, subject to the following conditions:
|
|
14
|
-
*
|
|
15
|
-
* The above copyright notice and this permission notice shall be included in all
|
|
16
|
-
* copies or substantial portions of the Software.
|
|
17
|
-
*
|
|
18
|
-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
19
|
-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
20
|
-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
21
|
-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
22
|
-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
23
|
-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
24
|
-
* SOFTWARE.
|
|
25
|
-
*
|
|
26
|
-
*/
|
|
27
|
-
"use strict";
|
|
28
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
29
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
30
|
-
};
|
|
31
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
32
|
-
exports.appStart = exports.setViewEngine = exports.createPipeline = void 0;
|
|
33
|
-
const autoclose_js_1 = require("./express/autoclose.js");
|
|
34
|
-
const pipelinebuilder_js_1 = __importDefault(require("./express/pipelinebuilder.js"));
|
|
35
|
-
/** Create the full processing pipeline for an Express server
|
|
36
|
-
*
|
|
37
|
-
* This function creates a pipeline this way:
|
|
38
|
-
* - Pass generic middlewares (see options)
|
|
39
|
-
* - Pass custom top-level middlewares
|
|
40
|
-
* - Pass logging middleware (see options)
|
|
41
|
-
* - Pass routes
|
|
42
|
-
* - Pass error logging middleware (see options)
|
|
43
|
-
* - Pass error handlers
|
|
44
|
-
* - Fallback to a default error handler (see below)
|
|
45
|
-
*
|
|
46
|
-
* The default error handler is a layer above the express default error
|
|
47
|
-
* handler.
|
|
48
|
-
* If it is reached, it will check if the error object looks like an
|
|
49
|
-
* http-errors exception, and if that's the case, send the error code and the
|
|
50
|
-
* message (if applicable).
|
|
51
|
-
* If the request accepts json, a json object with the error is returned.
|
|
52
|
-
* Otherwise text is returned.
|
|
53
|
-
*
|
|
54
|
-
* For convenience, providing timestamp and collapseNodeModules here will automatically
|
|
55
|
-
* update the basic consoleLogger configuration.
|
|
56
|
-
*
|
|
57
|
-
* @param settings
|
|
58
|
-
*
|
|
59
|
-
* @param [settings.topLevels]
|
|
60
|
-
* List top-level middlewares. See routes.
|
|
61
|
-
*
|
|
62
|
-
* @param [settings.routes]
|
|
63
|
-
* List routes and routers.
|
|
64
|
-
* For each router and function, a simple "use()" call is used.
|
|
65
|
-
* If an object is provided, it must have two properties:
|
|
66
|
-
* - route: the actual route to listen to
|
|
67
|
-
* - handler: either a function, a router, or an array of functions that will
|
|
68
|
-
* be called in sequence on this request.
|
|
69
|
-
* - method: the method ("get", "post", "put", "options"). If missing,
|
|
70
|
-
* default to "get".
|
|
71
|
-
*
|
|
72
|
-
* @param [settings.statics]
|
|
73
|
-
* List of directory to serve as static content.
|
|
74
|
-
* Can be either a string (the path) or an object with the following
|
|
75
|
-
* properties:
|
|
76
|
-
* - root: the root in the filesystem for this static provider
|
|
77
|
-
* - options: (optional) the options to pass to static()
|
|
78
|
-
* - route: (optional) the route where the static resources will be served
|
|
79
|
-
*
|
|
80
|
-
* @param [settings.postStatics]
|
|
81
|
-
* Register routes handled after statics. See routes.
|
|
82
|
-
*
|
|
83
|
-
* @param [settings.errorHandlers]
|
|
84
|
-
* Process errors. Must be function with four parameters.
|
|
85
|
-
*
|
|
86
|
-
* @param [settings.options]
|
|
87
|
-
* @param [settings.options.log]
|
|
88
|
-
* @param [settings.options.log.route]
|
|
89
|
-
* Log each route. Can be an object with all properties expected from
|
|
90
|
-
* winston-express.logger()
|
|
91
|
-
*
|
|
92
|
-
* @param [settings.options.log.error]
|
|
93
|
-
* Log error content. Can be either a boolean or an object with the
|
|
94
|
-
* "collapseNodeModules" property. The error log will then display a truncated
|
|
95
|
-
* stacktrace where lines containing node_modules are removed.
|
|
96
|
-
*
|
|
97
|
-
* @param [settings.options.log.timestamp]
|
|
98
|
-
* Log timestamps on each lines
|
|
99
|
-
*
|
|
100
|
-
* @param [settings.options.log.logger]
|
|
101
|
-
* An existing winston logger to use. Only the transport will be used.
|
|
102
|
-
*
|
|
103
|
-
* @param [settings.options.middleware]
|
|
104
|
-
* @param [settings.options.middleware.urlencoded]
|
|
105
|
-
* Parse URL encoded requests. Can be true or settings for urlencoded().
|
|
106
|
-
* Default to handling application/x-www-form-urlencoded.
|
|
107
|
-
*
|
|
108
|
-
* @param [settings.options.middleware.text]
|
|
109
|
-
* Parse raw text body requests. Can be true or settings for text()
|
|
110
|
-
* Default to handling text/plain
|
|
111
|
-
*
|
|
112
|
-
* @param [settings.options.middleware.raw]
|
|
113
|
-
* Parse raw body requests into an arraybuffer. Can be true or settings for
|
|
114
|
-
* raw()
|
|
115
|
-
* Default to handling application/octet-stream
|
|
116
|
-
*
|
|
117
|
-
* @param [settings.options.middleware.json]
|
|
118
|
-
* Parse JSON body requests. Can be true or settings for json()
|
|
119
|
-
* Default to handling application/json
|
|
120
|
-
*
|
|
121
|
-
* @param [settings.options.defaultErrorHandler]
|
|
122
|
-
* Setup the default error handler. Defaults to false.
|
|
123
|
-
*
|
|
124
|
-
* @param [debugLog]
|
|
125
|
-
* Function to log debug informations
|
|
126
|
-
*
|
|
127
|
-
* @return
|
|
128
|
-
* The returned object can be used
|
|
129
|
-
*/
|
|
130
|
-
const createPipeline = (settings, debugLog) => {
|
|
131
|
-
if (debugLog) {
|
|
132
|
-
debugLog("Creating PipelineBuilder");
|
|
133
|
-
}
|
|
134
|
-
const builder = new pipelinebuilder_js_1.default(debugLog);
|
|
135
|
-
return builder.createPipeline(settings);
|
|
136
|
-
};
|
|
137
|
-
exports.createPipeline = createPipeline;
|
|
138
|
-
/**
|
|
139
|
-
* Configure the views rendering engine for an ExpressJS App
|
|
140
|
-
*/
|
|
141
|
-
const setViewEngine = (app, viewEngine, viewsDirectory, extraLocals) => {
|
|
142
|
-
app.set("views", viewsDirectory);
|
|
143
|
-
app.set("view engine", viewEngine);
|
|
144
|
-
if (extraLocals) {
|
|
145
|
-
app.locals = {
|
|
146
|
-
...app.locals,
|
|
147
|
-
...extraLocals,
|
|
148
|
-
};
|
|
149
|
-
}
|
|
150
|
-
};
|
|
151
|
-
exports.setViewEngine = setViewEngine;
|
|
152
|
-
const handleShutdown = (logger, shutdownFunction) => () => {
|
|
153
|
-
const fn = async () => {
|
|
154
|
-
const noExit = shutdownFunction ? Boolean(await shutdownFunction()) : false;
|
|
155
|
-
if (noExit)
|
|
156
|
-
return;
|
|
157
|
-
// We *want* to exit on server shutdown
|
|
158
|
-
// eslint-disable-next-line no-process-exit
|
|
159
|
-
process.exit();
|
|
160
|
-
};
|
|
161
|
-
fn().catch(err => {
|
|
162
|
-
logger?.error(err);
|
|
163
|
-
});
|
|
164
|
-
};
|
|
165
|
-
/** Start the server and register signal for automatic closing.
|
|
166
|
-
*
|
|
167
|
-
* @param app
|
|
168
|
-
* @param [allowNonLocal]
|
|
169
|
-
* Allow requests from outside of localhost
|
|
170
|
-
*
|
|
171
|
-
* @param [port]
|
|
172
|
-
* Port to listen from. Default to a random available port.
|
|
173
|
-
*
|
|
174
|
-
* @param [shutdownFunction]
|
|
175
|
-
* Function to call after the server is shutdown (all connection closed)
|
|
176
|
-
*
|
|
177
|
-
* @param [logger]
|
|
178
|
-
*
|
|
179
|
-
* @return
|
|
180
|
-
* @property port
|
|
181
|
-
* The port the server is listening on
|
|
182
|
-
*
|
|
183
|
-
* @property server
|
|
184
|
-
*/
|
|
185
|
-
const appStart = ({ app, allowNonLocal, listenInterface, port, shutdownFunction, logger, noReady, }) => new Promise(resolve => {
|
|
186
|
-
let server = null;
|
|
187
|
-
const readyCallback = () => {
|
|
188
|
-
if (server === null) {
|
|
189
|
-
throw new Error("Server reference can not be null");
|
|
190
|
-
}
|
|
191
|
-
const effectivePort = server.address().port;
|
|
192
|
-
if (logger) {
|
|
193
|
-
logger.info(`Listening on port ${effectivePort} (PID: ${process.pid})`);
|
|
194
|
-
}
|
|
195
|
-
(0, autoclose_js_1.setRef)(server);
|
|
196
|
-
(0, autoclose_js_1.setSignalHandler)();
|
|
197
|
-
resolve({
|
|
198
|
-
port: effectivePort,
|
|
199
|
-
server,
|
|
200
|
-
});
|
|
201
|
-
if (!noReady && process.send)
|
|
202
|
-
process.send("ready");
|
|
203
|
-
};
|
|
204
|
-
if (allowNonLocal !== undefined && listenInterface !== undefined) {
|
|
205
|
-
throw new Error("You can't specify both allowNonLocal and listenInterface at the same time");
|
|
206
|
-
}
|
|
207
|
-
if (allowNonLocal ?? listenInterface === true) {
|
|
208
|
-
server = app.listen(port ?? 0, readyCallback);
|
|
209
|
-
}
|
|
210
|
-
else if (typeof listenInterface === "string") {
|
|
211
|
-
server = app.listen(port ?? 0, listenInterface, readyCallback);
|
|
212
|
-
}
|
|
213
|
-
else {
|
|
214
|
-
server = app.listen(port ?? 0, "localhost", readyCallback);
|
|
215
|
-
}
|
|
216
|
-
server.on("close", handleShutdown(logger, shutdownFunction));
|
|
217
|
-
});
|
|
218
|
-
exports.appStart = appStart;
|
package/lib/repl.d.ts
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
/** Function called when the REPL is closed */
|
|
2
|
-
export type ReplCloseFunc = () => void;
|
|
3
|
-
/** Mimic the required elements from a db object */
|
|
4
|
-
interface DBLikeObject {
|
|
5
|
-
/** db object can have a reference to an initialized sequelize instance */
|
|
6
|
-
sequelize?: {
|
|
7
|
-
/** Sequelize instance should have a close() method */
|
|
8
|
-
close?: () => void;
|
|
9
|
-
};
|
|
10
|
-
}
|
|
11
|
-
interface ReplArgs {
|
|
12
|
-
/** Display a service name in the prompt */
|
|
13
|
-
serviceName: string;
|
|
14
|
-
/**
|
|
15
|
-
* A DB object. If it exposes a db.sequelize.close() function, it will be called when the REPL is
|
|
16
|
-
* closed.
|
|
17
|
-
*/
|
|
18
|
-
db?: DBLikeObject;
|
|
19
|
-
srv?: Record<string, unknown>;
|
|
20
|
-
/** Extra properties to attach to the REPL context */
|
|
21
|
-
extraContext?: Record<string, unknown>;
|
|
22
|
-
onClose?: ReplCloseFunc;
|
|
23
|
-
historyFile?: string;
|
|
24
|
-
}
|
|
25
|
-
/** Start the REPL prompt */
|
|
26
|
-
export declare const startRepl: ({ serviceName, db, srv, extraContext, onClose, historyFile, }: ReplArgs) => void;
|
|
27
|
-
export {};
|
package/lib/repl.js
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @license
|
|
3
|
-
* @preserve
|
|
4
|
-
* MIT License
|
|
5
|
-
*
|
|
6
|
-
* Copyright (c) 2021 Gabriel Paul "Cley Faye" Risterucci
|
|
7
|
-
*
|
|
8
|
-
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
9
|
-
* of this software and associated documentation files (the "Software"), to deal
|
|
10
|
-
* in the Software without restriction, including without limitation the rights
|
|
11
|
-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
12
|
-
* copies of the Software, and to permit persons to whom the Software is
|
|
13
|
-
* furnished to do so, subject to the following conditions:
|
|
14
|
-
*
|
|
15
|
-
* The above copyright notice and this permission notice shall be included in all
|
|
16
|
-
* copies or substantial portions of the Software.
|
|
17
|
-
*
|
|
18
|
-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
19
|
-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
20
|
-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
21
|
-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
22
|
-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
23
|
-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
24
|
-
* SOFTWARE.
|
|
25
|
-
*
|
|
26
|
-
*/
|
|
27
|
-
"use strict";
|
|
28
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
29
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
30
|
-
};
|
|
31
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
32
|
-
exports.startRepl = void 0;
|
|
33
|
-
const repl_1 = __importDefault(require("repl"));
|
|
34
|
-
const setupContext = (r, db, srv, extraContext) => {
|
|
35
|
-
const doContextSetup = () => {
|
|
36
|
-
Object.assign(r.context, {
|
|
37
|
-
...extraContext,
|
|
38
|
-
db,
|
|
39
|
-
srv,
|
|
40
|
-
});
|
|
41
|
-
};
|
|
42
|
-
doContextSetup();
|
|
43
|
-
r.on("reset", doContextSetup);
|
|
44
|
-
};
|
|
45
|
-
/** Configure action when the REPL is closed */
|
|
46
|
-
const setupClose = (r, db, onClose) => {
|
|
47
|
-
if (!db && !onClose)
|
|
48
|
-
return;
|
|
49
|
-
r.on("exit", () => {
|
|
50
|
-
if (db?.sequelize?.close) {
|
|
51
|
-
db.sequelize.close();
|
|
52
|
-
}
|
|
53
|
-
if (onClose) {
|
|
54
|
-
onClose();
|
|
55
|
-
}
|
|
56
|
-
});
|
|
57
|
-
};
|
|
58
|
-
/** Start the REPL prompt */
|
|
59
|
-
const startRepl = ({ serviceName = "", db, srv, extraContext, onClose, historyFile = ".node_repl_history", }) => {
|
|
60
|
-
const r = repl_1.default.start({
|
|
61
|
-
prompt: `${serviceName}> `,
|
|
62
|
-
breakEvalOnSigint: true,
|
|
63
|
-
});
|
|
64
|
-
setupContext(r, db, srv, extraContext);
|
|
65
|
-
setupClose(r, db, onClose);
|
|
66
|
-
r.setupHistory(historyFile, () => { });
|
|
67
|
-
};
|
|
68
|
-
exports.startRepl = startRepl;
|
package/lib/winston.d.ts
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import winston from "winston";
|
|
2
|
-
import { Format } from "logform";
|
|
3
|
-
/**
|
|
4
|
-
* Collapse useless lines in stacktrace
|
|
5
|
-
*/
|
|
6
|
-
export declare const filterStacktrace: (message: string, extraFilter?: Array<RegExp> | RegExp) => Generator<string>;
|
|
7
|
-
/**
|
|
8
|
-
* Prefix all lines with the level and the timestamp if needed
|
|
9
|
-
*/
|
|
10
|
-
export declare const prefixOutput: (level: string, message: string, timestamp: boolean | undefined) => string;
|
|
11
|
-
export interface LogConfig {
|
|
12
|
-
timestamp: boolean;
|
|
13
|
-
/** @deprecated Use collapseStacktrace instead */
|
|
14
|
-
collapseNodeModules: boolean;
|
|
15
|
-
collapseStacktrace: boolean;
|
|
16
|
-
stacktraceFilter?: Array<RegExp> | RegExp;
|
|
17
|
-
}
|
|
18
|
-
interface ExtendedErrorFields {
|
|
19
|
-
cause?: unknown;
|
|
20
|
-
response?: {
|
|
21
|
-
data?: string | Record<string, unknown>;
|
|
22
|
-
};
|
|
23
|
-
request?: {
|
|
24
|
-
_currentUrl?: string;
|
|
25
|
-
};
|
|
26
|
-
fields?: Set<string>;
|
|
27
|
-
statusCode?: number;
|
|
28
|
-
expose?: boolean;
|
|
29
|
-
}
|
|
30
|
-
export type ExtendedError = Error & ExtendedErrorFields;
|
|
31
|
-
/** Return a string with the full error and stacktrace, including causes */
|
|
32
|
-
export declare const filterError: (error: ExtendedError, collapseStacktrace?: boolean, extraFilter?: Array<RegExp> | RegExp) => string;
|
|
33
|
-
export declare const customFormat: (timestamp?: boolean, collapseStacktrace?: boolean, stacktraceFilter?: Array<RegExp> | RegExp) => Format;
|
|
34
|
-
/** Edit the configuration applied to consoleLogger */
|
|
35
|
-
export declare const setConfig: (config: LogConfig) => void;
|
|
36
|
-
export declare const transports: winston.transports.ConsoleTransportInstance[];
|
|
37
|
-
export declare const consoleLogger: winston.Logger;
|
|
38
|
-
export {};
|
package/lib/winston.js
DELETED
|
@@ -1,224 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @license
|
|
3
|
-
* @preserve
|
|
4
|
-
* MIT License
|
|
5
|
-
*
|
|
6
|
-
* Copyright (c) 2021 Gabriel Paul "Cley Faye" Risterucci
|
|
7
|
-
*
|
|
8
|
-
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
9
|
-
* of this software and associated documentation files (the "Software"), to deal
|
|
10
|
-
* in the Software without restriction, including without limitation the rights
|
|
11
|
-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
12
|
-
* copies of the Software, and to permit persons to whom the Software is
|
|
13
|
-
* furnished to do so, subject to the following conditions:
|
|
14
|
-
*
|
|
15
|
-
* The above copyright notice and this permission notice shall be included in all
|
|
16
|
-
* copies or substantial portions of the Software.
|
|
17
|
-
*
|
|
18
|
-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
19
|
-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
20
|
-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
21
|
-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
22
|
-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
23
|
-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
24
|
-
* SOFTWARE.
|
|
25
|
-
*
|
|
26
|
-
*/
|
|
27
|
-
"use strict";
|
|
28
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
29
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
30
|
-
};
|
|
31
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
32
|
-
exports.consoleLogger = exports.transports = exports.setConfig = exports.customFormat = exports.filterError = exports.prefixOutput = exports.filterStacktrace = void 0;
|
|
33
|
-
const winston_1 = __importDefault(require("winston"));
|
|
34
|
-
const unprocessableentityerror_js_1 = require("./express/unprocessableentityerror.js");
|
|
35
|
-
const LOG_LEVEL_PADDING_SIZE = 10;
|
|
36
|
-
const stackFilterRegex = /(?:at process|at runMicrotasks)/u;
|
|
37
|
-
const isFiltered = (line, extraFilter) => {
|
|
38
|
-
if (stackFilterRegex.exec(line))
|
|
39
|
-
return true;
|
|
40
|
-
if (extraFilter) {
|
|
41
|
-
for (const filter of Array.isArray(extraFilter) ? extraFilter : [extraFilter]) {
|
|
42
|
-
if (filter.exec(line))
|
|
43
|
-
return true;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
return false;
|
|
47
|
-
};
|
|
48
|
-
/**
|
|
49
|
-
* Collapse useless lines in stacktrace
|
|
50
|
-
*/
|
|
51
|
-
const filterStacktrace = function* (message, extraFilter) {
|
|
52
|
-
let inCollapse = false;
|
|
53
|
-
const lines = message.split("\n");
|
|
54
|
-
for (const line of lines) {
|
|
55
|
-
if (isFiltered(line, extraFilter)) {
|
|
56
|
-
if (!inCollapse) {
|
|
57
|
-
inCollapse = true;
|
|
58
|
-
yield "[...]";
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
else {
|
|
62
|
-
inCollapse = false;
|
|
63
|
-
yield line;
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
};
|
|
67
|
-
exports.filterStacktrace = filterStacktrace;
|
|
68
|
-
/**
|
|
69
|
-
* Prefix all lines with the level and the timestamp if needed
|
|
70
|
-
*/
|
|
71
|
-
const prefixOutput = (level, message, timestamp) => {
|
|
72
|
-
const timestampPrefix = (timestamp ?? (level.toLowerCase() === "error"))
|
|
73
|
-
? `${new Date().toISOString()} `
|
|
74
|
-
: "";
|
|
75
|
-
const levelPrefix = `${level.toString().padStart(LOG_LEVEL_PADDING_SIZE)}: `;
|
|
76
|
-
const levelEmpty = `${" ".repeat(LOG_LEVEL_PADDING_SIZE)} `;
|
|
77
|
-
return message.split("\n").map((line, id) => `${timestampPrefix}${id === 0 ? levelPrefix : levelEmpty}${line}`)
|
|
78
|
-
.join("\n");
|
|
79
|
-
};
|
|
80
|
-
exports.prefixOutput = prefixOutput;
|
|
81
|
-
const logConfig = {
|
|
82
|
-
timestamp: true,
|
|
83
|
-
collapseNodeModules: true,
|
|
84
|
-
collapseStacktrace: true,
|
|
85
|
-
stacktraceFilter: undefined,
|
|
86
|
-
};
|
|
87
|
-
const indent = (msg, indentLevel) => {
|
|
88
|
-
if (indentLevel === 0)
|
|
89
|
-
return msg;
|
|
90
|
-
const indentList = [];
|
|
91
|
-
for (let i = 0; i < indentLevel;) {
|
|
92
|
-
++i;
|
|
93
|
-
const str = `${i}|`;
|
|
94
|
-
if (i < indentLevel) {
|
|
95
|
-
indentList.push(" ".repeat(str.length));
|
|
96
|
-
}
|
|
97
|
-
else {
|
|
98
|
-
indentList.push(str);
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
const indentStr = indentList.join("");
|
|
102
|
-
return msg.split("\n")
|
|
103
|
-
.map(c => `${indentStr} ${c.trimEnd()}`)
|
|
104
|
-
.join("\n");
|
|
105
|
-
};
|
|
106
|
-
/** Add context from axios */
|
|
107
|
-
const addAxiosContent = (error) => {
|
|
108
|
-
const res = [];
|
|
109
|
-
if (error.request?._currentUrl) {
|
|
110
|
-
res.push(`HTTP Request URL: ${error.request._currentUrl}`);
|
|
111
|
-
}
|
|
112
|
-
if (error.response?.data) {
|
|
113
|
-
res.push("HTTP Response body:");
|
|
114
|
-
if (typeof error.response.data === "string") {
|
|
115
|
-
res.push(error.response.data);
|
|
116
|
-
}
|
|
117
|
-
else {
|
|
118
|
-
try {
|
|
119
|
-
res.push(JSON.stringify(error.response.data));
|
|
120
|
-
}
|
|
121
|
-
catch {
|
|
122
|
-
res.push("<cannot output body>");
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
if (res.length === 0)
|
|
127
|
-
return;
|
|
128
|
-
return res.join("\n");
|
|
129
|
-
};
|
|
130
|
-
/** Add context from http-errors */
|
|
131
|
-
const addHttpErrorContent = (error) => {
|
|
132
|
-
if (error.statusCode === undefined)
|
|
133
|
-
return;
|
|
134
|
-
return `HttpError status: ${error.statusCode}`;
|
|
135
|
-
};
|
|
136
|
-
const getCause = (error) => {
|
|
137
|
-
const cause = error.cause;
|
|
138
|
-
if (cause === undefined)
|
|
139
|
-
return;
|
|
140
|
-
if (cause instanceof Error)
|
|
141
|
-
return cause;
|
|
142
|
-
try {
|
|
143
|
-
return new Error(JSON.stringify(cause));
|
|
144
|
-
}
|
|
145
|
-
catch {
|
|
146
|
-
return new Error("Unknown cause");
|
|
147
|
-
}
|
|
148
|
-
};
|
|
149
|
-
/** Handle 422
|
|
150
|
-
*
|
|
151
|
-
* If return a string, do not process the error normally.
|
|
152
|
-
*/
|
|
153
|
-
const process422Error = (error) => {
|
|
154
|
-
const errorInfo = (0, unprocessableentityerror_js_1.get422Fields)(error);
|
|
155
|
-
return errorInfo?.message;
|
|
156
|
-
};
|
|
157
|
-
/** Return a string with the full error and stacktrace, including causes */
|
|
158
|
-
const filterError = (error, collapseStacktrace = true, extraFilter) => {
|
|
159
|
-
const resultRows = [];
|
|
160
|
-
const unprocessableCheck = process422Error(error);
|
|
161
|
-
if (unprocessableCheck)
|
|
162
|
-
return unprocessableCheck;
|
|
163
|
-
let cursor = error;
|
|
164
|
-
let indentLevel = 0;
|
|
165
|
-
while (cursor) {
|
|
166
|
-
if (indentLevel === 0) {
|
|
167
|
-
resultRows.push(cursor.name);
|
|
168
|
-
}
|
|
169
|
-
else {
|
|
170
|
-
resultRows.push(indent(`Caused by: ${cursor.name}`, indentLevel));
|
|
171
|
-
}
|
|
172
|
-
resultRows.push(indent(cursor.message, indentLevel));
|
|
173
|
-
if (cursor.stack) {
|
|
174
|
-
if (collapseStacktrace) {
|
|
175
|
-
resultRows.push(indent([...(0, exports.filterStacktrace)(cursor.stack, extraFilter)].join("\n"), indentLevel));
|
|
176
|
-
}
|
|
177
|
-
else {
|
|
178
|
-
resultRows.push(indent(cursor.stack, indentLevel));
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
const axiosContent = addAxiosContent(cursor);
|
|
182
|
-
if (axiosContent)
|
|
183
|
-
resultRows.push(indent(axiosContent, indentLevel));
|
|
184
|
-
const httpErrorsContent = addHttpErrorContent(cursor);
|
|
185
|
-
if (httpErrorsContent)
|
|
186
|
-
resultRows.push(indent(httpErrorsContent, indentLevel));
|
|
187
|
-
cursor = getCause(cursor);
|
|
188
|
-
++indentLevel;
|
|
189
|
-
}
|
|
190
|
-
return resultRows.join("\n");
|
|
191
|
-
};
|
|
192
|
-
exports.filterError = filterError;
|
|
193
|
-
const getErrorFromTransformable = (info) => {
|
|
194
|
-
if (info instanceof Error)
|
|
195
|
-
return info;
|
|
196
|
-
if (info.meta) {
|
|
197
|
-
const meta = info.meta;
|
|
198
|
-
if (meta.error && meta.error instanceof Error)
|
|
199
|
-
return meta.error;
|
|
200
|
-
}
|
|
201
|
-
};
|
|
202
|
-
const customFormat = (timestamp, collapseStacktrace, stacktraceFilter) => winston_1.default.format.printf((info) => {
|
|
203
|
-
const effectiveTimestamp = timestamp ?? logConfig.timestamp;
|
|
204
|
-
const effectiveCollapse = collapseStacktrace
|
|
205
|
-
?? (logConfig.collapseNodeModules || logConfig.collapseStacktrace);
|
|
206
|
-
const effectiveStacktraceFilter = stacktraceFilter ?? logConfig.stacktraceFilter;
|
|
207
|
-
const error = getErrorFromTransformable(info);
|
|
208
|
-
const message = error
|
|
209
|
-
? (0, exports.filterError)(error, effectiveCollapse, effectiveStacktraceFilter)
|
|
210
|
-
: info.message.toString();
|
|
211
|
-
return (0, exports.prefixOutput)(info.level, message, effectiveTimestamp);
|
|
212
|
-
});
|
|
213
|
-
exports.customFormat = customFormat;
|
|
214
|
-
/** Edit the configuration applied to consoleLogger */
|
|
215
|
-
const setConfig = (config) => {
|
|
216
|
-
Object.assign(logConfig, config);
|
|
217
|
-
};
|
|
218
|
-
exports.setConfig = setConfig;
|
|
219
|
-
exports.transports = [new winston_1.default.transports.Console()];
|
|
220
|
-
exports.consoleLogger = winston_1.default.createLogger({
|
|
221
|
-
level: "info",
|
|
222
|
-
format: (0, exports.customFormat)(),
|
|
223
|
-
transports: exports.transports,
|
|
224
|
-
});
|