-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
38 lines (28 loc) · 1.05 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
var request = require("request"),
url = require("url");
var resolveServiceEndpoint = exports.resolveServiceEndpoint = function resolveServiceEndpoint(serviceUrl, cb) {
var parsedUrl = url.parse(serviceUrl);
if (!parsedUrl.hash) {
return cb(Error("url fragment is required"));
}
var fragment = parsedUrl.hash.replace(/^#/, "");
return request({uri: serviceUrl, json: true}, function(err, res, descriptionDocument) {
if (err) {
return cb(err);
}
if (res.statusCode !== 200) {
return cb(Error("invalid status code; expected 200 but got " + res.statusCode));
}
if (!descriptionDocument || !descriptionDocument.services) {
return cb(Error("response was not a valid service description object"));
}
var service = descriptionDocument.services[fragment];
if (!service) {
return cb(Error("couldn't find service `" + fragment + "'"));
}
if (!service.href) {
return cb(Error("service didn't have a `href' property"));
}
return cb(null, url.resolve(serviceUrl, service.href));
});
};