forked from evanx/redexutil
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRequests.js
137 lines (126 loc) · 4.46 KB
/
Requests.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
// Copyright (c) 2015, Evan Summers (twitter.com/evanxsummers)
// ISC license, see http://github.com/evanx/redexutil/LICENSE
import fs from 'fs';
import requestf from 'request';
import lodash from 'lodash';
import Loggers from './Loggers';
const logger = Loggers.create(__filename, 'info');
export function requestJson(options) { // TODO deprecated
return json(options);
}
export function json(options) {
options = processOptions(options);
options.json = true;
logger.debug('json', options.url);
return request(options);
}
export function jsonPost(options) {
options = processOptions(options);
options.json = true;
logger.debug('json', options.url);
return requestPost(options);
}
export function request(options) {
options = processOptions(options);
logger.ndebug('request', options);
let startTime = new Date().getTime();
return new Promise((resolve, reject) => {
requestf(options, (err, response, content) => {
let duration = Millis.getElapsedDuration(startTime);
logger.debug('response', options.url || options, err || response.statusCode, Millis.formatDuration(duration));
if (err) {
err.options = options;
err.duration = duration;
reject(err);
} else if (response.statusCode !== 200) {
reject({options: options, statusCode: response.statusCode});
} else {
if (duration > options.slow) {
logger.warn('request slow', Millis.formatDuration(duration), options.url);
}
resolve(content);
}
});
});
}
export function requestPost(options) {
options = processOptions(options);
logger.ndebug('request', options);
let startTime = new Date().getTime();
return new Promise((resolve, reject) => {
requestf.post(options, (err, response, content) => {
let duration = Millis.getElapsedDuration(startTime);
logger.debug('response', options.url || options, err || response.statusCode, Millis.formatDuration(duration));
if (err) {
err.options = options;
err.duration = duration;
reject(err);
} else if (response.statusCode !== 200) {
reject({options: options, statusCode: response.statusCode});
} else {
if (duration > options.slow) {
logger.warn('request slow', Millis.formatDuration(duration), options.url);
}
resolve(content);
}
});
});
}
export function response(options) {
options = processOptions(options);
logger.debug('request', options);
let startTime = new Date().getTime();
return new Promise((resolve, reject) => {
requestf(options, (err, response, content) => {
let duration = Millis.getElapsedDuration(startTime);
if (duration > options.slow) {
logger.warn('request slow', options.url, err || response.statusCode, Millis.formatDuration(duration));
} else {
logger.debug('response', options, err || response.statusCode, Millis.formatDuration(duration));
}
if (err) {
reject(err);
} else if (response.statusCode === 200) {
resolve([response, content]);
} else {
resolve([response]);
}
});
});
}
export function head(options) {
options = processOptions(options);
options.method = 'HEAD';
logger.debug('head', options);
return new Promise((resolve, reject) => {
requestf(options, (err, response) => {
logger.debug('response', options.url, err || response.statusCode);
if (err) {
reject(err);
} else {
resolve(response);
}
});
});
}
function processOptions(options) {
if (typeof options === 'string') {
return {url: options, slow: 8000};
} else if (typeof options === 'object') {
assert(options.url, 'url');
options.headers = options.headers || {};
if (options.lastModified) {
Object.assign(options.headers, {'If-Modified-Since': options.lastModified});
}
if (options.username && options.password) {
let auth = 'Basic ' + new Buffer(options.username + ':' + options.password).toString('base64');
Object.assign(options.headers, {'Authorization': auth});
}
if (!options.slow) {
options.slow = 8000;
}
return options;
} else {
throw {message: 'Invalid request options', options: options};
}
}