This repository has been archived by the owner on Aug 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
336 lines (291 loc) · 7.09 KB
/
index.js
1
2
3
4
5
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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
'use strict'
// **Github:** https://github.com/toajs/toa-morgan
//
// **License:** MIT
// Modified from https://github.com/expressjs/morgan
module.exports = toaMorgan
/**
* Create a logger middleware.
*
* @param {String|Function} format
* @param {Object} [options]
* @return {Function} middleware
* @public
*/
function toaMorgan (format, options) {
format = format || 'combined'
options = options || {}
// output on request instead of response
const immediate = !!options.immediate
// check if log entry should be skipped
const skip = typeof options.skip === 'function' ? options.skip : null
// format function
const formatLine = compile(formats[format] || format)
// stream
const stream = options.stream || process.stdout
return function logger (done) {
this._startTime = Date.now()
this._endTime = 0
if (immediate) logRequest.call(this)
else this.on('end', handle).on('close', handle)
done()
}
function handle () {
this.removeListener('end', handle).removeListener('close', handle)
this._endTime = Date.now()
logRequest.call(this)
}
function logRequest () {
if (skip && skip.call(this)) return
let line = formatLine.call(this)
if (line != null) stream.write(line + '\n')
}
}
/**
* Define a format with the given name.
*
* @param {string} name
* @param {string|function} format
* @public
*/
const formats = Object.create(null)
toaMorgan.format = function (name, format) {
let type = typeof format
if (type !== 'string' && type !== 'function') {
throw new TypeError('argument format must be a string or a function')
}
formats[name] = format
return toaMorgan
}
/**
* Define a token function with the given name,
* and callback fn() with toa context.
*
* @param {string} name
* @param {function} fn
* @public
*/
const tokens = Object.create(null)
toaMorgan.token = function (name, fn) {
if (typeof fn !== 'function') {
throw new TypeError('argument fn must be a function')
}
tokens[name] = fn
return toaMorgan
}
/**
* Apache combined log format.
*/
toaMorgan.format('combined', ':remote-addr - :remote-user [:date[clf]] ":method :url HTTP/:http-version" :status :res[content-length] ":referrer" ":user-agent"')
/**
* Apache common log format.
*/
toaMorgan.format('common', ':remote-addr - :remote-user [:date[clf]] ":method :url HTTP/:http-version" :status :res[content-length]')
/**
* Short format.
*/
toaMorgan.format('short', ':remote-addr :remote-user :method :url HTTP/:http-version :status :res[content-length] - :response-time ms')
/**
* Tiny format.
*/
toaMorgan.format('tiny', ':method :url :status :res[content-length] - :response-time ms')
/**
* dev (colored)
*/
toaMorgan.format('dev', function developmentFormatLine () {
// get the status code if response written
let status = this.status
// get status color
let color = status >= 500 ? 31 // red
: status >= 400 ? 33 // yellow
: status >= 300 ? 36 // cyan
: status >= 200 ? 32 // green
: 0 // no color
// get colored function
let fn = developmentFormatLine[color]
if (!fn) {
// compile
let format = '\x1b[0m:method :url \x1b[' + color +
'm:status \x1b[0m:response-time ms - :res[content-length]\x1b[0m'
fn = developmentFormatLine[color] = compile(format)
}
return fn.call(this)
})
/**
* request url
*/
toaMorgan.token('url', function () {
return this.originalUrl
})
/**
* request method
*/
toaMorgan.token('method', function () {
return this.method
})
/**
* response time in milliseconds
*/
toaMorgan.token('response-time', function () {
return this._endTime ? (this._endTime - this._startTime) : '-'
})
/**
* current date
*/
toaMorgan.token('date', function (format) {
let date = this._endTime ? new Date(this._endTime) : new Date()
switch (format || 'web') {
case 'clf':
return clfdate(date)
case 'iso':
return date.toISOString()
case 'web':
return date.toUTCString()
}
})
/**
* response status code
*/
toaMorgan.token('status', function () {
return this.res.headersSent ? this.status : '-'
})
/**
* normalized referrer
*/
toaMorgan.token('referrer', function () {
return this.get('referrer')
})
/**
* remote address
*/
toaMorgan.token('remote-addr', function () {
return this.ip
})
/**
* remote user
*/
toaMorgan.token('remote-user', function () {
return '-'
})
/**
* HTTP version
*/
toaMorgan.token('http-version', function () {
return this.req.httpVersion
})
/**
* UA string
*/
toaMorgan.token('user-agent', function () {
return this.get('user-agent')
})
/**
* request header
*/
toaMorgan.token('req', function (field) {
let header = this.get(field)
return Array.isArray(header) ? header.join(', ') : header
})
/**
* response header
*/
toaMorgan.token('res', function (field) {
let header = this.response.get(field)
return Array.isArray(header) ? header.join(', ') : header
})
/**
* Compile a format string into a function.
*
* @param {string} format
* @return {function}
* @private
*/
const regex = /:([-\w]{2,})(?:\[([^\]]+)])?/
function compile (str) {
if (typeof str === 'function') return str
if (typeof str !== 'string') throw new TypeError('argument format must be a string')
let fns = []
let tokenFn = 0
let res = regex.exec(str)
while (res) {
if (res.index) fns.push(compileStr(str.slice(0, res.index)))
tokenFn++
fns.push(compileToken(res[1], res[2]))
str = str.slice(res.index + res[0].length)
res = regex.exec(str)
}
if (!tokenFn) throw new Error(str + ' is invalid format(no token)')
if (str) fns.push(compileStr(str))
return function () {
let ctx = this
return fns.reduce((log, fn) => log + toStr(fn.call(ctx)), '')
}
}
/**
* Compile a token string into a function.
*
* @param {string} token
* @param {string} arg
* @return {function}
* @private
*/
function compileToken (token, arg) {
let fn = tokens[token] || noOp
return function () {
return arg ? fn.call(this, arg) : fn.call(this)
}
}
/**
* Wrap a string into a function that return the string.
*
* @param {string} str
* @return {function}
* @private
*/
function compileStr (str) {
return () => str
}
/**
* Format a Date in the common log format.
*
* @param {Date} dateTime
* @return {string}
* @private
*/
const clfmonth = [
'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
]
function clfdate (dateTime) {
let date = dateTime.getUTCDate()
let hour = dateTime.getUTCHours()
let mins = dateTime.getUTCMinutes()
let secs = dateTime.getUTCSeconds()
let year = dateTime.getUTCFullYear()
let month = clfmonth[dateTime.getUTCMonth()]
return pad2(date) + '/' + month + '/' + year + ':' +
pad2(hour) + ':' + pad2(mins) + ':' + pad2(secs) + ' +0000'
}
/**
* Pad number to two digits.
*
* @param {number} num
* @return {string}
* @private
*/
function pad2 (num) {
let str = String(num)
return (str.length === 1 ? '0' : '') + str
}
/**
* convert value to string.
*
* @param {any} value
* @return {string}
* @private
*/
function toStr (value) {
if (typeof value !== 'string' && value != null) value = String(value)
return value || '-'
}
function noOp () {}