-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
77 lines (59 loc) · 1.86 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
"use strict";
var url = require("url"),
util = require("util");
var clone = require("clone"),
debug = require("debug")("tilelive-fieldpapers"),
holdtime = require("holdtime"),
request = require("request");
var meta = require("./package.json");
var PREFIX = "fieldpapers+",
NAME = meta.name,
VERSION = meta.version;
module.exports = function(tilelive) {
var load = function(uri, callback) {
var headers = {
"User-Agent": [NAME, VERSION].join("/")
};
var sourceUrl = url.format(uri);
debug("Requesting %s...", sourceUrl);
return request({
uri: sourceUrl,
headers: headers,
json: true,
timeout: 5e3
}, holdtime(function(err, rsp, body, elapsedMS) {
debug("%s took %dms", sourceUrl, elapsedMS);
if (err) {
debug(err);
return callback(err);
}
if (rsp.statusCode !== 200) {
debug("Received %d response for %s", rsp.statusCode, sourceUrl);
return callback(new Error(util.format("Received %d response for %s", rsp.statusCode, sourceUrl)));
}
return tilelive.load("raster+" + body.geotiff.url, callback);
}));
};
var Source = function(uri, callback) {
if (typeof uri === "string") {
uri = url.parse(uri, true);
} else {
uri = clone(uri);
}
uri.protocol = uri.protocol.replace(PREFIX, "");
switch (uri.protocol) {
case "http:":
case "https:":
return load(uri, callback);
default:
debug("Unsupported %s transport: %s", NAME, url.format(uri));
return callback(new Error(util.format("Unsupported %s transport: %s", NAME, url.format(uri))));
}
};
Source.registerProtocols = function(_tilelive) {
_tilelive.protocols[PREFIX + "http:"] = Source;
_tilelive.protocols[PREFIX + "https:"] = Source;
};
Source.registerProtocols(tilelive);
return Source;
};