-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathapp.js
More file actions
368 lines (304 loc) · 10.3 KB
/
Copy pathapp.js
File metadata and controls
368 lines (304 loc) · 10.3 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
"use strict";
var crypto = require("crypto"),
path = require("path"),
url = require("url"),
util = require("util");
var abaculus = require("@mapbox/abaculus"),
cachecache = require("cachecache"),
clone = require("clone"),
debug = require("debug"),
express = require("express"),
handlebars = require("handlebars"),
mercator = new (require("@mapbox/sphericalmercator"))();
var tessera = require("./index");
debug = debug("tessera");
var FLOAT_PATTERN = "[+-]?(?:\\d+|\\d+\.?\\d+)";
var SCALE_PATTERN = "@(?:\\d+|\\d+\.?\\d+)x";
// TODO a more complete implementation of this exists...somewhere
var getExtension = function(format) {
// trim PNG variant info
switch ((format || "").replace(/^(png).*/, "$1")) {
case "png":
return "png";
default:
return format;
}
};
var getScale = function(scale) {
return parseFloat((scale || "@1x").slice(1));
};
var normalizeHeaders = function(headers) {
var _headers = {};
Object.keys(headers).forEach(function(x) {
_headers[x.toLowerCase()] = headers[x];
});
return _headers;
};
var md5sum = function(data) {
var hash = crypto.createHash("md5");
hash.update(data);
return hash.digest();
};
module.exports = function(tilelive, options) {
var app = express().disable("x-powered-by").enable("trust proxy"),
templates = {},
uri = options,
tilePath = "/{z}/{x}/{y}.{format}",
sourceMaxZoom = null,
tilePattern;
app.use(cachecache());
if (typeof options === "object") {
uri = options.source;
tilePath = options.tilePath || tilePath;
if (options.sourceMaxZoom) {
sourceMaxZoom = parseInt(options.sourceMaxZoom);
}
Object.keys(options.headers || {}).forEach(function(name) {
templates[name] = handlebars.compile(options.headers[name]);
// attempt to parse so we can fail fast
try {
templates[name]();
} catch (e) {
console.error("'%s' header is invalid:", name);
console.error(e.message);
process.exit(1);
}
});
}
if (typeof uri === "string") {
uri = url.parse(uri, true);
} else {
uri = clone(uri);
}
tilePattern = tilePath
.replace(/\.(?!.*\.)/, ":scale(" + SCALE_PATTERN + ")?.")
.replace(/\./g, "\.")
.replace("{z}", ":z(\\d+)")
.replace("{x}", ":x(\\d+)")
.replace("{y}", ":y(\\d+)")
.replace("{format}", ":format([\\w\\.]+)");
var populateHeaders = function(headers, params, extras) {
Object.keys(extras || {}).forEach(function(k) {
params[k] = extras[k];
});
Object.keys(templates).forEach(function(name) {
var val = templates[name](params);
if (val) {
headers[name.toLowerCase()] = val;
}
});
return headers;
};
// warm the cache
tilelive.load(uri);
var sourceURIs = {
1: uri
};
// We cache 100 sourceURIs, randomly evict when over limit
var sourceURIsCacheKeys = [];
var sourceURIsCacheSizeLimit = 100;
var makeScaledSourceURI = function(scale, permanent) {
var retinaURI = clone(uri),
randomIndex;
retinaURI.query.scale = scale;
// explicitly tell tilelive-mapnik to use larger tiles
retinaURI.query.tileSize = (scale * 256) | 0;
if (!permanent) {
// Save the URI to the uri cache, but limit its size
while (sourceURIsCacheKeys.length > sourceURIsCacheSizeLimit) {
// Delete random sourceURI
randomIndex = Math.floor(Math.random()*sourceURIsCacheKeys.length);
delete sourceURIs[sourceURIsCacheKeys[randomIndex]];
sourceURIsCacheKeys.splice(randomIndex, 1);
}
sourceURIsCacheKeys.push(scale);
}
sourceURIs[scale] = retinaURI;
return retinaURI;
};
// make scaled source urls for retina @2x and @3x
[2, 3].forEach(function(scale) {
// these are not added to sourceURIsCacheKeys so that they are not ever removed from the cache
makeScaledSourceURI(scale, true);
});
var getSourceURI = function(scale) {
if (scale in sourceURIs) {
return sourceURIs[scale];
} else {
// scaled uri does not exist so we make one
return makeScaledSourceURI(scale);
}
};
var getTile = function(z, x, y, scale, format, callback) {
var sourceURI = getSourceURI(scale),
params = {
tile: {
zoom: z,
x: x,
y: y,
format: format,
retina: scale > 1,
scale: scale
}
};
// Additional params for vector tile based sources
if (sourceMaxZoom != null) {
params.tile.sourceZoom = z;
params.tile.sourceX = x;
params.tile.sourceY = y;
while (params.tile.sourceZoom > sourceMaxZoom) {
params.tile.sourceZoom--;
params.tile.sourceX = Math.floor(params.tile.sourceX / 2);
params.tile.sourceY = Math.floor(params.tile.sourceY / 2);
}
}
return tilelive.load(sourceURI, function(err, source) {
if (err) {
return callback(err);
}
return tessera.getInfo(source, function(err, info) {
if (err) {
return callback(err);
}
// validate format / extension
var ext = getExtension(info.format);
if (ext !== format) {
debug("Invalid format '%s', expected '%s'", format, ext);
return callback(null, null, populateHeaders({}, params, { 404: true, invalidFormat: true }));
}
// validate zoom
if (z < info.minzoom || z > info.maxzoom) {
debug("Invalid zoom:", z);
return callback(null, null, populateHeaders({}, params, { 404: true, invalidZoom: true }));
}
// validate coords against bounds
var xyz = mercator.xyz(info.bounds, z);
if (x < xyz.minX ||
x > xyz.maxX ||
y < xyz.minY ||
y > xyz.maxY) {
debug("Invalid coordinates: %d,%d relative to bounds:", x, y, xyz);
return callback(null, null, populateHeaders({}, params, { 404: true, invalidCoordinates: true }));
}
return source.getTile(z, x, y, function(err, data, headers) {
headers = normalizeHeaders(headers || {});
if (err) {
if (err.message.match(/(Tile|Grid) does not exist/)) {
return callback(null, null, populateHeaders(headers, params, { 404: true }));
}
return callback(err);
}
if (data === null || data === undefined) {
return callback(null, null, populateHeaders(headers, params, { 404: true }));
}
if (!headers["content-md5"]) {
headers["content-md5"] = md5sum(data).toString("base64");
}
// work-around for PBF MBTiles that don't contain appropriate headers
if (ext === "pbf") {
headers["content-type"] = headers["content-type"] || "application/x-protobuf";
headers["content-encoding"] = headers["content-encoding"] || "gzip";
}
return callback(null, data, populateHeaders(headers, params, { 200: true }));
});
});
});
};
app.get(tilePattern, function(req, res, next) {
var z = req.params.z | 0,
x = req.params.x | 0,
y = req.params.y | 0,
scale = getScale(req.params.scale),
format = req.params.format;
return getTile(z, x, y, scale, format, function(err, data, headers) {
if (err) {
return next(err);
}
if (data == null) {
return res.status(404).send("Not found");
} else {
res.set(headers);
return res.status(200).send(data);
}
}, res, next);
});
var processStaticMap = function(areaParams, req, res, next) {
var scale = getScale(req.params.scale),
format = req.params.format,
params = {
zoom: req.params.z | 0,
scale: scale,
bbox: areaParams.bbox,
center: areaParams.center,
format: format,
getTile: function(z, x, y, callback) {
return getTile(z, x, y, scale, format, function(err, data, headers) {
if (!err && data == null) {
err = new Error("Not found");
err.status = 404;
}
callback(err, data, headers);
});
}
};
return abaculus(params, function(err, data, headers) {
if (err && !err.status) {
return next(err);
}
res.set(headers);
res.status((err && err.status) || 200);
return res.send((err && err.message) || data);
});
};
var staticPattern = "/static/%s:scale(" + SCALE_PATTERN + ")?\.:format([\\w\\.]+)";
var centerPattern = util.format(':lon(%s),:lat(%s),:z(\\d+)/:width(\\d+)x:height(\\d+)',
FLOAT_PATTERN, FLOAT_PATTERN);
app.get(util.format(staticPattern, centerPattern), function(req, res, next) {
return processStaticMap({
center: {
x: +req.params.lon,
y: +req.params.lat,
w: req.params.width | 0,
h: req.params.height | 0
}
}, req, res, next);
});
var boundsPattern = util.format(':minx(%s),:miny(%s),:maxx(%s),:maxy(%s)/:z(\\d+)',
FLOAT_PATTERN, FLOAT_PATTERN, FLOAT_PATTERN, FLOAT_PATTERN);
app.get(util.format(staticPattern, boundsPattern), function(req, res, next) {
return processStaticMap({
bbox: [
+req.params.minx,
+req.params.miny,
+req.params.maxx,
+req.params.maxy
]
}, req, res, next);
});
app.get("/index.json", function(req, res, next) {
var params = {
tileJSON: true
};
return tilelive.load(uri, function(err, source) {
if (err) {
return next(err);
}
return tessera.getInfo(source, function(err, info) {
if (err) {
return next(err);
}
var protocol = req.headers['x-forwarded-proto'] || req.protocol;
var host = req.headers['x-forwarded-host'] || req.headers.host;
var uri = protocol + "://" + host +
(path.dirname(req.originalUrl) +
tilePath.replace("{format}",
getExtension(info.format))).replace(/\/+/g, "/");
info.tiles = [uri];
info.tilejson = "2.0.0";
res.set(populateHeaders({}, params, { 200: true }));
return res.send(info);
});
});
});
return app;
};