-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
60 lines (54 loc) · 1.76 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
var getProtocol = require('./lib/protocol')
, ServiceRequest = require('./lib/service-request')
, http = require('http')
, https = require('https')
, request = require('request')
exports.attach = function (options) {
var amino = this;
amino.protocol = getProtocol(options);
var customRequest = request.defaults({
httpModules: { 'amino:': amino.protocol, 'http:': http, 'https:': https },
json: true
});
amino.request = function (service, path, options, cb) {
if (typeof service === 'object' || (typeof service === 'string' && service.indexOf('://') !== -1)) {
cb = path;
return customRequest(service, cb);
}
else if (typeof options === 'function') {
cb = options;
options = {};
}
var pathMatches = path.match(/^(?:(\w+) )?(\/.*)$/);
if (!pathMatches) {
var err = new Error('invalid path spec: ' + path);
if (typeof cb === 'function') {
cb(err);
}
else {
throw err;
}
}
var method = pathMatches[1] ? pathMatches[1].toUpperCase() : 'GET';
path = pathMatches[2]
var reqSpec = new amino.Spec(service);
var opts = amino.utils.copy(options);
opts.url || (opts.url = 'amino://' + reqSpec.service + path);
opts.headers || (opts.headers = {});
opts.headers['X-Amino-Version'] = reqSpec.version;
opts.method || (opts.method = method);
return customRequest(opts, cb);
};
amino.requestService = function (reqSpec, cb) {
reqSpec = new amino.Spec(reqSpec);
var req = new ServiceRequest(reqSpec);
req.once('spec', cb);
amino.protocol.globalAgent.addRequest(req, reqSpec.service);
return req;
};
var _reset = amino.reset;
amino.reset = function () {
amino.protocol.globalAgent.reset();
_reset && _reset();
};
};