forked from parse-community/parse-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.js
174 lines (160 loc) · 4.54 KB
/
request.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
import querystring from 'querystring';
import log from './logger';
import { http, https } from 'follow-redirects';
import { parse } from 'url';
class HTTPResponse {
constructor(response, body) {
let _text, _data;
this.status = response.statusCode;
this.headers = response.headers || {};
this.cookies = this.headers['set-cookie'];
if (typeof body == 'string') {
_text = body;
} else if (Buffer.isBuffer(body)) {
this.buffer = body;
} else if (typeof body == 'object') {
_data = body;
}
const getText = () => {
if (!_text && this.buffer) {
_text = this.buffer.toString('utf-8');
} else if (!_text && _data) {
_text = JSON.stringify(_data);
}
return _text;
};
const getData = () => {
if (!_data) {
try {
_data = JSON.parse(getText());
} catch (e) {
/* */
}
}
return _data;
};
Object.defineProperty(this, 'body', {
get: () => {
return body;
},
});
Object.defineProperty(this, 'text', {
enumerable: true,
get: getText,
});
Object.defineProperty(this, 'data', {
enumerable: true,
get: getData,
});
}
}
const clients = {
'http:': http,
'https:': https,
};
function makeCallback(resolve, reject) {
return function (response) {
const chunks = [];
response.on('data', chunk => {
chunks.push(chunk);
});
response.on('end', () => {
const body = Buffer.concat(chunks);
const httpResponse = new HTTPResponse(response, body);
// Consider <200 && >= 400 as errors
if (httpResponse.status < 200 || httpResponse.status >= 400) {
return reject(httpResponse);
} else {
return resolve(httpResponse);
}
});
response.on('error', reject);
};
}
const encodeBody = function ({ body, headers = {} }) {
if (typeof body !== 'object') {
return { body, headers };
}
var contentTypeKeys = Object.keys(headers).filter(key => {
return key.match(/content-type/i) != null;
});
if (contentTypeKeys.length == 0) {
// no content type
// As per https://parse.com/docs/cloudcode/guide#cloud-code-advanced-sending-a-post-request the default encoding is supposedly x-www-form-urlencoded
body = querystring.stringify(body);
headers['Content-Type'] = 'application/x-www-form-urlencoded';
} else {
/* istanbul ignore next */
if (contentTypeKeys.length > 1) {
log.error('Parse.Cloud.httpRequest', 'multiple content-type headers are set.');
}
// There maybe many, we'll just take the 1st one
var contentType = contentTypeKeys[0];
if (headers[contentType].match(/application\/json/i)) {
body = JSON.stringify(body);
} else if (headers[contentType].match(/application\/x-www-form-urlencoded/i)) {
body = querystring.stringify(body);
}
}
return { body, headers };
};
function httpRequest(options) {
let url;
try {
url = parse(options.url);
} catch (e) {
return Promise.reject(e);
}
options = Object.assign(options, encodeBody(options));
// support params options
if (typeof options.params === 'object') {
options.qs = options.params;
} else if (typeof options.params === 'string') {
options.qs = querystring.parse(options.params);
}
const client = clients[url.protocol];
if (!client) {
return Promise.reject(`Unsupported protocol ${url.protocol}`);
}
const requestOptions = {
method: options.method,
port: Number(url.port),
path: url.pathname,
hostname: url.hostname,
headers: options.headers,
encoding: null,
followRedirects: options.followRedirects === true,
};
if (requestOptions.headers) {
Object.keys(requestOptions.headers).forEach(key => {
if (typeof requestOptions.headers[key] === 'undefined') {
delete requestOptions.headers[key];
}
});
}
if (url.search) {
options.qs = Object.assign({}, options.qs, querystring.parse(url.query));
}
if (url.auth) {
requestOptions.auth = url.auth;
}
if (options.qs) {
requestOptions.path += `?${querystring.stringify(options.qs)}`;
}
if (options.agent) {
requestOptions.agent = options.agent;
}
return new Promise((resolve, reject) => {
const req = client.request(requestOptions, makeCallback(resolve, reject, options));
if (options.body) {
req.write(options.body);
}
req.on('error', error => {
reject(error);
});
req.end();
});
}
module.exports = httpRequest;
module.exports.encodeBody = encodeBody;
module.exports.HTTPResponse = HTTPResponse;