-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
154 lines (124 loc) · 3.78 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
/*jslint browser: false*/
'use strict';
const url = require('url');
// ============================================================================
// Helpers
// Returns a copy of the object
function copy(object, dest) {
return Object.keys(object).reduce(function (o, key) {
o[key] = object[key];
return o;
}, dest || {});
}
// Content types we parse as JSON
const contentTypes = {
'application/json': true
};
// Checks if the given content type is one we accept
// Note: NodeJS makes HTTP headers lower case by default
function inContentTypes(type) {
type += '';
return Object.keys(contentTypes).some(function (ct) {
return type.includes(ct) && contentTypes[ct];
});
}
// Returns HTTP response callback
const response = (function () {
const CT = 'content-type';
function data(d) {
if (undefined === this.body) {
this.body = '';
}
this.body += d;
}
function end(callback, res) {
try {
// Parse accepted content types as JSON
if (!inContentTypes(res.headers[CT])) {
throw new Error('Content type \"'
+ res.headers[CT]
+ '\" failed the check against '
+ JSON.stringify(contentTypes));
}
callback(null, res, JSON.parse(this.body));
} catch (err) {
return callback(err, res, this.body);
}
}
return function (callback, res) {
const o = {
body: undefined
};
res.setEncoding('utf8')
.on('data', data.bind(o))
.on('end', end.bind(o, callback, res));
};
}());
// Returns a HTTP method
function method(opt, path, data, callback) {
// Check permutation of arguments
if ('function' === typeof path) {
callback = path;
path = undefined;
data = undefined;
} else if ('function' === typeof data) {
callback = data;
if ('string' === typeof path) {
data = undefined;
} else {
data = path;
path = undefined;
}
}
// Add path if any
if (path) {
opt = copy(opt);
opt.path += path;
}
const res = response.bind(this, callback);
const req = this.http.request(opt, res);
req.on('error', callback);
if ('object' === typeof data && data !== null) {
req.write(JSON.stringify(data));
}
req.end();
}
// ============================================================================
//
function Remote(uri, opt) {
const parsed = url.parse(uri);
opt = copy({
host: parsed.host,
path: parsed.path
}, opt);
opt.headers = opt.headers || {};
opt.headers['Content-Type'] = 'application/json';
switch (parsed.protocol) {
case 'https:':
if (!module.exports.https) {
module.exports.https = require('https');
}
this.http = module.exports.https;
break;
case 'http:':
if (!module.exports.http) {
module.exports.http = require('http');
}
this.http = module.exports.http;
break;
default:
throw new Error('Not supported protocol: ' + parsed.protocol);
}
// Constructors of HTTP methods
this.post = method.bind(this, copy(opt, {method: 'POST'}));
this.get = method.bind(this, copy(opt, {method: 'GET'}));
this.put = method.bind(this, copy(opt, {method: 'PUT'}));
this.del = method.bind(this, copy(opt, {method: 'DELETE'}));
this.patch = method.bind(this, copy(opt, {method: 'PATCH'}));
}
// ============================================================================
// Module exports
module.exports = function (uri, opt) {
return new Remote(uri, opt);
};
module.exports.contentTypes = contentTypes;