This repository has been archived by the owner on Feb 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getMap.js
142 lines (134 loc) · 4.18 KB
/
getMap.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
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
const debug = require("debug")("StaticMaps-gl.getMap");
const genericPool = require("generic-pool");
const mbgl = require("@mapbox/mapbox-gl-native");
const request = require("request");
const fs = require("fs");
mbgl.on("message", function(e) {
debug("mbgl: ", e);
if (e.severity == "WARNING" || e.severity == "ERROR") {
console.log("mbgl:", e);
}
});
function getMap() {
debug("Creating map");
var _map = new mbgl.Map({
mode: "static",
ratio: 1.0,
request: function(req, callback) {
debug("request: " + JSON.stringify(req));
var start = Date.now();
var protocol = req.url.split(":")[0];
if (protocol == "file") {
var path = req.url.split("://")[1];
fs.readFile(path, function(err, data) {
if (err) {
return callback(err);
}
var response = {};
response.data = data;
callback(null, response);
debug("Request for " + req.url + " complete in " + (Date.now() - start) + "ms");
});
} else if (protocol === "http" || protocol === "https") {
request(
{
url: req.url,
encoding: null,
gzip: true
},
function(err, res, body) {
var duration = Date.now() - start;
if (duration > 500) {
if (res === undefined) {
// If request timed out response will be undefined
debug("Request for " + req.url + " failed in " + duration + "ms.");
} else {
// Headers are needed for debugging cases of slow responses from AWS s3
debug(
"Request for " +
req.url +
" complete in " +
duration +
"ms. Headers:" +
JSON.stringify(res.headers || null)
);
}
} else {
debug("Request for " + req.url + " complete in " + duration + "ms");
}
if (err) {
callback(err);
} else if (res.statusCode == 200) {
var response = {};
if (res.headers.modified) {
response.modified = new Date(res.headers.modified);
}
if (res.headers.expires) {
response.expires = new Date(res.headers.expires);
}
if (res.headers.etag) {
response.etag = res.headers.etag;
}
response.data = body;
callback(null, response);
} else {
//Dont make rendering fail if a resource is missing
return callback(null, {});
}
}
);
} else {
debug(`request for invalid url: "${req.url}"`);
return callback(`request for invalid url: "${req.url}"`);
}
}
});
return _map;
}
exports.getMap = getMap;
exports.getMapPool = function() {
const factory = {
create: function() {
return new Promise(function(resolve, reject) {
try {
var map = getMap();
resolve(map);
} catch (err) {
console.error("Error creating map:", err);
reject(err);
}
});
},
destroy: function(resource) {
return new Promise(function(resolve) {
debug("Destroying map, used " + resource.useCount + " times.");
resource.release();
resolve();
});
}
};
const maxMapUses = 0;
if (maxMapUses > 0) {
factory["validate"] = function(resource) {
debug("validate");
return new Promise(function(resolve) {
console.log("validate: usecount:" + resource.useCount);
if (resource.useCount != undefined && resource.useCount > maxMapUses) {
resolve(false);
} else {
resolve(true);
}
});
};
}
const opts = {
max: 20, // maximum size of the pool
min: 0, // minimum size of the pool
testOnBorrow: maxMapUses > 0,
idleTimeoutMillis: 15 * 60 * 1000,
evictionRunIntervalMillis: maxMapUses > 0 ? 5 * 60 * 1000 : 0
};
debug("Creating map pool with opts:", opts);
const mapPool = genericPool.createPool(factory, opts);
return mapPool;
};