-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathindex.js
164 lines (141 loc) · 3.64 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
/**
* @fileoverview Http request in node.js
* @author douzi <[email protected]>
*/
var http = require('http');
var util = require('utils-extend');
var url = require('url');
var path = require('path');
var querystring = require('querystring');
var file = require('file-system');
/**
* @description
* http request
* @param {object|string} [options]
* @param {function} [callback]
* @example
* request('url', function(err, res, body) { });
* request({url: '', headers: {}, method: 'POST'}, function(err, res, body) { });
*/
function request(options, callback) {
var opts = {
headers: {
'Content-Type': 'application/json'
},
method: 'GET',
encoding: 'utf8',
// If the callback body is buffer, it can hanlder document pipe simply
isBuffer: false,
json: false
};
if (util.isString(options)) {
opts.url = options;
} else {
util.extend(opts, options);
}
// Append request data
if (opts.data) {
if (opts.method === 'GET') {
opts.url += '?' + querystring.stringify(opts.data);
} else {
opts.data = JSON.stringify(opts.data);
opts.headers['Content-Length'] = new Buffer(opts.data).length;
}
}
// Extend request url object
util.extend(opts, util.pick(url.parse(opts.url), 'hostname', 'port', 'path', 'auth'));
delete opts.url;
var req = http.request(opts, function(res) {
var body = [];
var size = 0;
res.on('data', function(chunk) {
body.push(chunk);
size += chunk.length;
});
res.on('end', function() {
var result = '';
// Buffer
if (opts.isBuffer) {
result = Buffer.concat(body, size);
} else {
var buffer = new Buffer(size);
for (var i = 0, pos = 0, l = body.length; i < l; i++) {
var chunk = body[i];
chunk.copy(buffer, pos);
pos += chunk.length;
}
result = buffer.toString(opts.encoding);
if (opts.json) {
result = JSON.parse(result);
}
}
callback(null, res, result);
});
});
req.on('error', callback);
if (opts.method !== 'GET' && opts.data) {
req.write(opts.data);
}
req.end();
}
/**
* @description
* @example
* request.post('url', function() {});
* request.post({ url: 'url', data: { q1: 'v1' }}, function() {});
*/
request.post = function(options, callback) {
if (util.isString(options)) {
options = {
url: options
};
}
options.method = 'POST';
request(options, callback);
};
/**
* @description
* Download remote resurce to local file
* @example
* request.download({ url: 'path.png' }, function(err, res, body, filepath) {})
* request.download({
url: 'path.png',
rootPath: 'dest/path'
}, function(err, res, body, filepath) {
});
*/
request.download = function(options, callback) {
var opts = util.extend({
rootPath: '',
ignore: false
}, options);
request({
url: opts.url,
isBuffer: true
}, function(err, res, body) {
if (err) return callback(err);
if (res.statusCode !== 200) return callback(err, res, body);
var destPath;
var pathname = url.parse(options.url).pathname.replace(/^\//, '');
if (opts.destPath) {
if (util.isFunction(opts.destPath)) {
destPath = opts.destPath(path.basename(pathname));
} else {
destPath = opts.destPath;
}
} else {
destPath = path.join(
options.rootPath,
pathname
);
}
if (opts.ignore) {
destPath = destPath.toLowerCase();
}
file.writeFile(destPath, body, function(err) {
if (err) return callback(err);
callback(null, res, body, destPath);
});
});
};
module.exports = request;