Skip to content

Commit

Permalink
Missing placeholders mean TileJSON endpoints
Browse files Browse the repository at this point in the history
Allows TM2 project files to work unmodified, as this now matches its
behavior.

Switches to factory construction to make `tilelive` available without
directly depending on it.

Fixes #2
  • Loading branch information
mojodna committed May 8, 2014
1 parent 7b4ae23 commit 2d1c09f
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 52 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ I am an HTTP source for [tilelive](https://github.com/mapbox/tilelive.js).

```javascript
var tilelive = require("tilelive");
require("tilelive-http").registerProtocols(tilelive);
require("tilelive-http")(tilelive);
require("mbtiles").registerProtocols(tilelive);

var template = "http://tile.stamen.com/watercolor/{z}/{x}/{y}.jpg";
Expand Down
114 changes: 63 additions & 51 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,66 +7,78 @@ var version = require("./package.json").version;

http.globalAgent.maxSockets = 100;

var HttpSource = function(uri, callback) {
this.source = url.format(uri);
module.exports = function(tilelive, options) {
var HttpSource = function(uri, callback) {
this.source = url.format(uri);

return callback(null, this);
};

HttpSource.prototype.getTile = function(z, x, y, callback) {
var tileUrl = this.source
.replace(/{z}/i, z)
.replace(/{x}/i, x)
.replace(/{y}/i, y);

var headers = {
"User-Agent": "tilelive-http/" + version
};
if (!(this.source.match(/{z}/) &&
this.source.match(/{x}/) &&
this.source.match(/{y}/))) {
console.log("Coordinate placeholders missing; assuming %s is a TileJSON endpoint (tilejson+).", this.source);

return request.get({
uri: tileUrl,
encoding: null,
headers: headers
}, function(err, rsp, body) {
if (err) {
return callback(err);
return tilelive.load("tilejson+" + this.source, callback);
}

switch (rsp.statusCode) {
case 200:
var rspHeaders = {
"Content-Type": rsp.headers["content-type"]
};
return callback(null, this);
};

return callback(null, body, rspHeaders);
HttpSource.prototype.getTile = function(z, x, y, callback) {
var tileUrl = this.source
.replace(/{z}/i, z)
.replace(/{x}/i, x)
.replace(/{y}/i, y);

var headers = {
"User-Agent": "tilelive-http/" + version
};

return request.get({
uri: tileUrl,
encoding: null,
headers: headers
}, function(err, rsp, body) {
if (err) {
return callback(err);
}

switch (rsp.statusCode) {
case 200:
var rspHeaders = {
"Content-Type": rsp.headers["content-type"]
};

return callback(null, body, rspHeaders);

case 404:
return callback(new Error('Tile does not exist'));

default:
return callback(new Error("Upstream error: " + rsp.statusCode));
}
});
};

case 404:
return callback(new Error('Tile does not exist'));
HttpSource.prototype.getInfo = function(callback) {
return callback(null, {
format: url.parse(this.source).pathname.split(".").pop(),
bounds: [-180, -85.0511, 180, 85.0511],
minzoom: 0,
maxzoom: Infinity
});
};

default:
return callback(new Error("Upstream error: " + rsp.statusCode));
}
});
};
HttpSource.prototype.close = function(callback) {
callback = callback || function() {};

HttpSource.prototype.getInfo = function(callback) {
return callback(null, {
format: url.parse(this.source).pathname.split(".").pop(),
bounds: [-180, -85.0511, 180, 85.0511],
minzoom: 0,
maxzoom: Infinity
});
};
return callback();
};

HttpSource.prototype.close = function(callback) {
callback = callback || function() {};
HttpSource.registerProtocols = function(tilelive) {
tilelive.protocols["http:"] = HttpSource;
tilelive.protocols["https:"] = HttpSource;
};

return callback();
};
HttpSource.registerProtocols(tilelive);

HttpSource.registerProtocols = function(tilelive) {
tilelive.protocols["http:"] = HttpSource;
tilelive.protocols["https:"] = HttpSource;
return HttpSource;
};

module.exports = HttpSource;

0 comments on commit 2d1c09f

Please sign in to comment.