-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
175 lines (154 loc) · 3.73 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
var c = require('chalk')
var json = require('prettyjson')
// req, res | http, raw | body, json
var debug = {}
if (process.env.DEBUG) {
process.env.DEBUG.split(',').forEach(function (key) {
debug[key] = true
})
}
var opt = {
keysColor: 'blue',
stringColor: 'grey'
}
function log (req) {
var _options // raw options
req.on('options', function (options) {
_options = options
if (debug.raw) {
console.log(c.gray.inverse('options'))
console.log(json.render(options, opt, 4))
}
})
req.on('request', function (req, options) {
if (debug.http) {
console.log(c.gray.inverse('options'))
console.log(json.render(options, opt, 4))
}
if (debug.req) {
var mt = method(req.method)
console.log(c.cyan.inverse('req'), mt(req.method), c.yellow(uri(req)))
var headers = {}
for (var key in req._headerNames) {
var name = req._headerNames[key]
headers[name] = req._headers[key]
}
console.log(json.render(headers, opt, 4))
}
if (debug.body) {
if (_options.body) {
console.log(c.gray.inverse('body'))
if (_options.multipart) {
multipart(_options)
}
else {
console.log(_options.body)
}
}
}
})
req.on('onresponse', function (res) {
var code = res.statusCode
var st = status(res.statusCode)
if (debug.res) {
console.log(
c.yellow.inverse('res'), st(res.statusCode + ' ' + res.statusMessage))
console.log(json.render(res.headers, opt, 4))
}
})
req.on('body', function (body) {
if (debug.body) {
if (body) {
console.log(c.gray.inverse('body'))
console.log(body)
}
}
})
req.on('json', function (body) {
if (debug.json) {
if (body) {
console.log(c.gray.inverse('json'))
console.log(json.render(body, opt, 4))
}
}
})
}
function method (verb) {
if (/GET/.test(verb)) {
return c.green
}
else if (/POST/.test(verb)) {
return c.cyan
}
else if (/PUT/.test(verb)) {
return c.cyan
}
else if (/DELETE/.test(verb)) {
return c.red
}
else if (/HEAD|OPTIONS|CONNECT/.test(verb)) {
return c.yellow
}
else if (/TRACE/.test(verb)) {
return c.gray
}
}
function uri (req) {
if (/^https?/.test(req.path)) {
return req.path
}
else {
return req.agent.protocol + '//' + req._headers.host +
(req.path === '/' ? '' : req.path)
}
// return url.format({
// protocol: req.agent.protocol,
// host: req._headers.host,
// pathname: (req.path === '/' ? '' : req.path)
// })
}
function status (code) {
if (code >= 100 && code <= 199) {
return c.white
}
else if (code >= 200 && code <= 299) {
return c.green
}
else if (code >= 300 && code <= 399) {
return c.yellow
}
else if (code >= 400 && code <= 499) {
return c.red
}
else if (code >= 500 && code <= 599) {
return c.red.bold
}
}
function multipart (_options) {
var header = _options.headers.get('content-type')
var boundary = header.replace(/.*boundary=([^\s;]+).*/, '$1')
_options.body._items.forEach(function (item) {
// file system
if (item.hasOwnProperty('fd')) {
console.log(c.blue('fs'), c.yellow(item.path))
}
// @request/core
else if (item._client) {
var mt = method(item._req.method)
var url = _options.protocol+'//'+item._req._headers.host + item._req.path
console.log(
c.blue('@request/core'), mt(item._req.method), c.yellow(url))
}
else {
item.split('\r\n').forEach(function (line) {
if (line.indexOf(boundary) !== -1) {
console.log(c.grey(line))
}
else {
console.log(line)
}
})
}
})
}
module.exports = log