-
Notifications
You must be signed in to change notification settings - Fork 42
/
index.js
61 lines (54 loc) · 1.69 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
var TileLayer = require('./lib/TileLayer.js');
var TileRequest = require('./lib/TileRequest.js');
var TileRequestHandler = require('./lib/TileRequestHandler.js');
var TileServer = require('./lib/TileServer.js');
var utils = require('./lib/utils.js');
module.exports = function(options) {
return new TileServer(options);
};
module.exports.TileServer = TileServer;
module.exports.TileRequest = TileRequest;
module.exports.TileRequestHandler = TileRequestHandler;
module.exports.TileLayer = TileLayer;
module.exports.utils = utils;
module.exports.createServer = function() {
console.warn('tilestrata.createServer() is deprecated, use tilestrata() instead');
return new TileServer();
};
module.exports.middleware = function(options) {
options = options || {};
var prefix = (options.prefix || '').replace(/\/$/, '');
var prefix_len = prefix.length;
var server = options.server;
if (!(server instanceof TileServer)) {
throw new Error('"server" option required, and must be a TileServer instance');
}
var pendingRequests = [];
server.initialize(function(err) {
if (err) throw err;
while (pendingRequests.length) {
var args = pendingRequests.shift();
handleRequest(args[0], args[1], args[2]);
}
});
function handleRequest(req, res, next) {
var original_url = req.url;
if (original_url.substring(0, prefix_len) === prefix) {
req.url = original_url.substring(prefix_len);
} else {
return next();
}
server._handleRequest(req, res, function() {
req.url = original_url;
next();
});
}
return function(req, res, next) {
if (!server.initialized) {
var args = Array.prototype.slice.call(arguments);
pendingRequests.push(args);
} else {
handleRequest(req, res, next);
}
};
};