-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
199 lines (184 loc) · 5.57 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
'use strict';
console.warn('request-sync is deprecated, use sync-request');
var url = require('url');
var qs = require('qs');
var type = require('./lib/type');
// we use `qs` to support nesting but need `unescape` which is available in `querystring`
qs.unescape = qs.unescape || require('querystring').unescape;
var native = false;
var httpSync;
try {
httpSync = require('http-sync');
native = true;
} catch (ex) {
httpSync = require('http-sync-win');
native = false;
}
var hasOwnProperty = Object.prototype.hasOwnProperty;
module.exports = request;
module.exports.httpSync = httpSync;
module.exports.native = native;
/**
* Make a web request
*
* Options:
*
* - `uri` || `url` - fully qualified uri or parsed url object from `url.parse()`
* - `method` - http method (default: `"GET"`)
* - `qs` - object containing querystring values to be appended to the `uri`
* - `headers` - http headers (default: `{}`)
* - `body` - entity body for PATCH, POST and PUT requests. Must be a `Buffer` or `String`.
* - `auth` - A hash containing values `user` || `username`, `password` || `pass`
* - `encoding` - encoding to stringify the result body, set this to `null` to get a `Buffer`
*
* @param {String} uri
* @param {Object} options
* @return {Response}
*/
function request(uri, options) {
// 1 - handle variable list of arguments
if (typeof uri === 'undefined') {
throw new TypeError('undefined is not a valid uri or options object.');
}
if (options && typeof options === 'object') {
options.uri = uri;
} else if (typeof uri === 'string') {
options = {uri: uri};
} else {
options = uri;
}
options = copy(options);
if (options.url && !options.uri) {
options.uri = options.url;
delete options.url;
}
// 2 - check types
type('uri', options.uri, 'String|Object');
type('options.method', options.method, 'String?');
type('options.qs', options.qs, 'Object?');
type('options.headers', options.headers, 'Object?');
if (options.body !== undefined && !Buffer.isBuffer(options.body)) {
type('options.body', options.body, 'String|Buffer');
}
type('options.auth', options.auth, 'Object?');
// 3 - normalize types
if (typeof options.uri === 'string') {
options.uri = url.parse(options.uri);
}
options.method = (options.method || 'GET').toUpperCase();
if (options.qs) {
var baseQs = qs.parse(options.uri.query);
for (var i in options.qs) {
baseQs[i] = options.qs[i];
}
if (qs.stringify(baseQs) !== '') {
options.uri = url.parse(options.uri.href.split('?')[0] + '?' + qs.stringify(baseQs));
}
}
options.headers = options.headers || {};
if (typeof options.body === 'string') {
options.body = new Buffer(options.body);
}
if (!options.body) {
options.body = new Buffer(0);
}
if (options.auth) {
if (hasOwnProperty.call(options.auth, 'username'))
options.auth.user = options.auth.username;
if (hasOwnProperty.call(options.auth, 'password'))
options.auth.pass = options.auth.password;
} else if (options.uri.auth) {
var authPieces = options.uri.auth.split(':').map(function(item){
return qs.unescape(item);
});
options.auth = {
user: authPieces[0],
pass: authPieces.slice(1).join(':')
};
}
if (options.auth) {
var authHeader = options.auth.pass === undefined ?
options.auth.user :
options.auth.user + ':' + options.auth.pass;
options.headers['authorization'] = 'Basic ' + toBase64(authHeader);
}
for (var key in options.headers) {
type('options.headers[' + key + ']', options.headers[key], 'String');
if (options.headers[key] === '') {
delete options.headers[key];
}
}
if (module.exports.native && !options.headers['content-type']) {
options.headers['content-type'] = '';
}
if (module.exports.native && !options.headers['accept']) {
options.headers['accept'] = '';
}
var request = new Request(options.uri, options.method, options.headers, options.body);
var req = module.exports.httpSync.request({
protocol: request.uri.protocol.replace(/\:$/, ''),
host: request.uri.hostname,
port: request.uri.port,
path: request.uri.path,
method: request.method,
headers: request.headers,
body: request.body
});
var res = req.end();
if (options.encoding !== null) {
res.body = res.body.toString(options.encoding);
}
return new Response(res.statusCode, res.headers, res.body);
}
function Request(uri, method, headers, body) {
this.uri = uri;
this.method = method;
this.headers = headers;
this.headers['content-length'] = body.length;
this.body = body;
}
/**
* A response from a web request
*
* @param {Number} statusCode
* @param {Object} headers
* @param {Buffer} body
*/
function Response(statusCode, headers, body) {
this.statusCode = statusCode;
this.headers = {};
for (var key in headers) {
this.headers[key.toLowerCase()] = headers[key];
}
this.body = body;
}
/**
* Convert a string into its base64 representation
*
* @param {String} str
* @return {String}
*/
function toBase64(str) {
return (new Buffer(str || "", "ascii")).toString("base64")
}
function copy(obj, seen) {
seen = seen || [];
if (seen.indexOf(obj) !== -1) {
throw new Error('Unexpected circular reference in options');
}
if (Array.isArray(obj)) {
seen.push(obj);
return obj.map(function (item) {
return copy(item, seen);
});
} else if (obj && typeof obj === 'object' && !Buffer.isBuffer(obj)) {
seen.push(obj);
var o = {}
Object.keys(obj).forEach(function (i) {
o[i] = copy(obj[i], seen)
})
return o
} else {
return obj;
}
}