@oxog/spark 1.1.0 → 1.2.0
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/CHANGELOG.md +632 -293
- package/LICENSE +20 -20
- package/README.md +288 -241
- package/package.json +83 -89
- package/src/core/application.js +1070 -956
- package/src/core/context.js +1100 -830
- package/src/core/middleware.js +305 -217
- package/src/index.js +183 -278
- package/src/middleware/body-parser.js +484 -526
- package/src/middleware/cache.js +179 -170
- package/src/middleware/compression.js +179 -182
- package/src/middleware/cors.js +178 -95
- package/src/middleware/health.js +221 -218
- package/src/middleware/logger.js +197 -194
- package/src/middleware/metrics.js +341 -327
- package/src/middleware/rate-limit.js +369 -319
- package/src/middleware/security.js +444 -332
- package/src/middleware/session.js +649 -529
- package/src/middleware/static.js +406 -299
- package/src/router/layer.js +199 -117
- package/src/router/route.js +151 -123
- package/src/router/router.js +705 -755
- package/src/utils/async-handler.js +147 -144
- package/src/utils/context-pool.js +85 -82
- package/src/utils/regex-validator.js +189 -176
- package/src/utils/safe-json.js +110 -0
- package/types/index.d.ts +536 -291
- package/dist/LICENSE +0 -21
- package/dist/README.md +0 -242
- package/dist/core/application.js +0 -957
- package/dist/core/context.js +0 -831
- package/dist/core/middleware.js +0 -218
- package/dist/core/request.js +0 -333
- package/dist/core/response.js +0 -375
- package/dist/index.js +0 -278
- package/dist/middleware/body-parser.js +0 -526
- package/dist/middleware/cache.js +0 -171
- package/dist/middleware/compression.js +0 -183
- package/dist/middleware/cors.js +0 -95
- package/dist/middleware/health.js +0 -219
- package/dist/middleware/index.js +0 -21
- package/dist/middleware/logger.js +0 -195
- package/dist/middleware/metrics.js +0 -328
- package/dist/middleware/rate-limit.js +0 -320
- package/dist/middleware/security.js +0 -333
- package/dist/middleware/session.js +0 -530
- package/dist/middleware/static.js +0 -299
- package/dist/package.json +0 -62
- package/dist/router/layer.js +0 -117
- package/dist/router/route.js +0 -123
- package/dist/router/router.js +0 -756
- package/dist/types/index.d.ts +0 -291
- package/dist/utils/async-handler.js +0 -145
- package/dist/utils/cache.js +0 -441
- package/dist/utils/context-pool.js +0 -83
- package/dist/utils/error-types.js +0 -132
- package/dist/utils/http.js +0 -413
- package/dist/utils/regex-validator.js +0 -177
- package/src/core/request.js +0 -333
- package/src/core/response.js +0 -375
- package/src/middleware/index.js +0 -21
- package/src/utils/cache.js +0 -441
- package/src/utils/error-types.js +0 -132
- package/src/utils/http.js +0 -413
package/src/middleware/logger.js
CHANGED
|
@@ -1,195 +1,198 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
const
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
'
|
|
47
|
-
'
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
'
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
if (status >=
|
|
114
|
-
return status
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
const
|
|
138
|
-
const
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
module.exports
|
|
192
|
-
module.exports.
|
|
193
|
-
module.exports.
|
|
194
|
-
module.exports.
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { EventEmitter } = require('events');
|
|
4
|
+
|
|
5
|
+
const DEFAULT_FORMAT = ':method :url :status :response-time ms';
|
|
6
|
+
const DEFAULT_SKIP = () => false;
|
|
7
|
+
|
|
8
|
+
class Logger extends EventEmitter {
|
|
9
|
+
constructor(options = {}) {
|
|
10
|
+
super();
|
|
11
|
+
this.options = {
|
|
12
|
+
format: options.format || DEFAULT_FORMAT,
|
|
13
|
+
skip: options.skip || DEFAULT_SKIP,
|
|
14
|
+
stream: options.stream || process.stdout,
|
|
15
|
+
immediate: options.immediate || false,
|
|
16
|
+
...options
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
format(tokens, ctx) {
|
|
21
|
+
const format = this.options.format;
|
|
22
|
+
|
|
23
|
+
if (typeof format === 'function') {
|
|
24
|
+
return format(tokens, ctx);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// Token names may contain hyphens (:response-time, :content-length)
|
|
28
|
+
return format.replace(/:([a-zA-Z][\w-]*\w)/g, (match, token) => {
|
|
29
|
+
const value = tokens[token];
|
|
30
|
+
return value === undefined || value === null || value === '' ? '-' : String(value);
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
log(ctx, start, length) {
|
|
35
|
+
if (this.options.skip(ctx)) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const responseTime = Date.now() - start;
|
|
40
|
+
const contentLength = length || (ctx.res && ctx.res.getHeader && ctx.res.getHeader('content-length')) || ctx.getHeader('content-length') || '-';
|
|
41
|
+
|
|
42
|
+
const tokens = {
|
|
43
|
+
method: ctx.method,
|
|
44
|
+
url: ctx.originalUrl || ctx.url,
|
|
45
|
+
status: ctx.statusCode,
|
|
46
|
+
'response-time': responseTime,
|
|
47
|
+
'content-length': contentLength,
|
|
48
|
+
referrer: ctx.get('referrer') || ctx.get('referer') || '-',
|
|
49
|
+
'user-agent': ctx.get('user-agent') || '-',
|
|
50
|
+
'remote-addr': ctx.ip() || '-',
|
|
51
|
+
'remote-user': ctx.user ? ctx.user.name : '-',
|
|
52
|
+
date: new Date().toISOString(),
|
|
53
|
+
'http-version': ctx.req.httpVersion,
|
|
54
|
+
res: ctx.res
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const line = this.format(tokens, ctx);
|
|
58
|
+
|
|
59
|
+
if (this.options.stream) {
|
|
60
|
+
this.options.stream.write(line + '\n');
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
this.emit('log', tokens, ctx);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function logger(options = {}) {
|
|
68
|
+
const log = new Logger(options);
|
|
69
|
+
|
|
70
|
+
return async (ctx, next) => {
|
|
71
|
+
const start = Date.now();
|
|
72
|
+
|
|
73
|
+
if (options.immediate) {
|
|
74
|
+
log.log(ctx, start);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
await next();
|
|
78
|
+
|
|
79
|
+
if (!options.immediate) {
|
|
80
|
+
log.log(ctx, start);
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function morgan(format, options = {}) {
|
|
86
|
+
const formats = {
|
|
87
|
+
combined: ':remote-addr - :remote-user [:date] ":method :url HTTP/:http-version" :status :content-length ":referrer" ":user-agent"',
|
|
88
|
+
common: ':remote-addr - :remote-user [:date] ":method :url HTTP/:http-version" :status :content-length',
|
|
89
|
+
dev: ':method :url :status :response-time ms - :content-length',
|
|
90
|
+
short: ':remote-addr :remote-user :method :url HTTP/:http-version :status :content-length - :response-time ms',
|
|
91
|
+
tiny: ':method :url :status :content-length - :response-time ms'
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
const logFormat = formats[format] || format || formats.dev;
|
|
95
|
+
|
|
96
|
+
return logger({
|
|
97
|
+
...options,
|
|
98
|
+
format: logFormat
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function accessLog(options = {}) {
|
|
103
|
+
const opts = {
|
|
104
|
+
format: 'combined',
|
|
105
|
+
...options
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
return morgan(opts.format, opts);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function devLogger(options = {}) {
|
|
112
|
+
const colorStatus = (status) => {
|
|
113
|
+
if (status >= 500) return `\x1b[31m${status}\x1b[0m`; // Red
|
|
114
|
+
if (status >= 400) return `\x1b[33m${status}\x1b[0m`; // Yellow
|
|
115
|
+
if (status >= 300) return `\x1b[36m${status}\x1b[0m`; // Cyan
|
|
116
|
+
if (status >= 200) return `\x1b[32m${status}\x1b[0m`; // Green
|
|
117
|
+
return status;
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
const colorMethod = (method) => {
|
|
121
|
+
const colors = {
|
|
122
|
+
GET: '\x1b[32m', // Green
|
|
123
|
+
POST: '\x1b[33m', // Yellow
|
|
124
|
+
PUT: '\x1b[34m', // Blue
|
|
125
|
+
DELETE: '\x1b[31m', // Red
|
|
126
|
+
PATCH: '\x1b[35m', // Magenta
|
|
127
|
+
HEAD: '\x1b[36m', // Cyan
|
|
128
|
+
OPTIONS: '\x1b[37m' // White
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
const color = colors[method] || '\x1b[37m';
|
|
132
|
+
return `${color}${method}\x1b[0m`;
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
return logger({
|
|
136
|
+
format: (tokens, ctx) => {
|
|
137
|
+
const method = colorMethod(tokens.method);
|
|
138
|
+
const status = colorStatus(tokens.status);
|
|
139
|
+
const url = tokens.url;
|
|
140
|
+
const responseTime = tokens['response-time'];
|
|
141
|
+
const contentLength = tokens['content-length'];
|
|
142
|
+
|
|
143
|
+
return `${method} ${url} ${status} ${responseTime}ms - ${contentLength}`;
|
|
144
|
+
},
|
|
145
|
+
...options
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function errorLogger(options = {}) {
|
|
150
|
+
const opts = {
|
|
151
|
+
skip: (ctx) => ctx.statusCode < 400,
|
|
152
|
+
...options
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
return logger(opts);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function requestLogger(options = {}) {
|
|
159
|
+
const opts = {
|
|
160
|
+
immediate: true,
|
|
161
|
+
format: ':method :url',
|
|
162
|
+
...options
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
return logger(opts);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function structuredLogger(options = {}) {
|
|
169
|
+
const opts = {
|
|
170
|
+
format: (tokens, ctx) => {
|
|
171
|
+
const logData = {
|
|
172
|
+
timestamp: new Date().toISOString(),
|
|
173
|
+
method: tokens.method,
|
|
174
|
+
url: tokens.url,
|
|
175
|
+
status: tokens.status,
|
|
176
|
+
responseTime: tokens['response-time'],
|
|
177
|
+
contentLength: tokens['content-length'],
|
|
178
|
+
userAgent: tokens['user-agent'],
|
|
179
|
+
ip: tokens['remote-addr'],
|
|
180
|
+
referrer: tokens.referrer
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
return JSON.stringify(logData);
|
|
184
|
+
},
|
|
185
|
+
...options
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
return logger(opts);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
module.exports = logger;
|
|
192
|
+
module.exports.Logger = Logger;
|
|
193
|
+
module.exports.morgan = morgan;
|
|
194
|
+
module.exports.accessLog = accessLog;
|
|
195
|
+
module.exports.devLogger = devLogger;
|
|
196
|
+
module.exports.errorLogger = errorLogger;
|
|
197
|
+
module.exports.requestLogger = requestLogger;
|
|
195
198
|
module.exports.structuredLogger = structuredLogger;
|