Skip to content

Instantly share code, notes, and snippets.

@gravitystorm
Created June 10, 2014 09:49
Show Gist options
  • Select an option

  • Save gravitystorm/d64b6efee6b8b3b8f29c to your computer and use it in GitHub Desktop.

Select an option

Save gravitystorm/d64b6efee6b8b3b8f29c to your computer and use it in GitHub Desktop.
unwrapped tilelive-http
"use strict";
var http = require("http"),
url = require("url");
var request = require("request");
var version = require("./package.json").version;
http.globalAgent.maxSockets = 100;
module.exports = HttpSource;
function HttpSource(uri, callback) {
this.source = url.format(uri);
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 tilelive.load("tilejson+" + this.source, callback);
}
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
};
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));
}
});
};
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
});
};
HttpSource.prototype.close = function(callback) {
callback = callback || function() {};
return callback();
};
HttpSource.registerProtocols = function(tilelive) {
tilelive.protocols["http:"] = HttpSource;
tilelive.protocols["https:"] = HttpSource;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment