-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathmap-store-provider.js
82 lines (66 loc) · 2.49 KB
/
map-store-provider.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
'use strict';
const BaseMapConfigProvider = require('./base-mapconfig-adapter');
const dot = require('dot');
// Configure bases for cache keys suitable for string interpolation
const baseKey = '{{=it.dbname}}:{{=it.token}}';
const rendererKey = baseKey + ':{{=it.dbuser}}:{{=it.format}}:{{=it.layer}}:{{=it.scale_factor}}';
const baseKeyTpl = dot.template(baseKey);
const rendererKeyTpl = dot.template(rendererKey);
module.exports = class MapStoreMapConfigProvider extends BaseMapConfigProvider {
/**
* @param {MapStore} mapStore
* @param {String} user
* @param {UserLimitsBackend} userLimitsBackend
* @param {Object} params
* @constructor
* @type {MapStoreMapConfigProvider}
*/
constructor (mapStore, user, userLimitsBackend, pgConnection, affectedTablesCache, params) {
super();
this.mapStore = mapStore;
this.user = user;
this.userLimitsBackend = userLimitsBackend;
this.pgConnection = pgConnection;
this.affectedTablesCache = affectedTablesCache;
this.params = params;
this.token = params.token;
this.cacheBuster = params.cache_buster || 0;
this.mapConfig = null;
this.context = null;
}
getMapConfig (callback) {
if (this.mapConfig !== null) {
return callback(null, this.mapConfig, this.params, this.context);
}
const context = {};
this.userLimitsBackend.getRenderLimits(this.user, this.params.api_key, (err, renderLimits) => {
if (err) {
return callback(err);
}
context.limits = renderLimits;
this.mapStore.load(this.token, (err, mapConfig) => {
if (err) {
return callback(err);
}
this.mapConfig = mapConfig;
this.context = context;
return callback(null, mapConfig, this.params, context);
});
});
}
getKey () {
return this.createKey(false);
}
getCacheBuster () {
return this.cacheBuster;
}
filter (key) {
const regex = new RegExp('^' + this.createKey(true) + '.*');
return key && key.match(regex);
}
createKey (base) {
const { dbname = '', token = '', dbuser = '', format = '', layer = '', scale_factor = 1 } = this.params;
const tplValues = { dbname, token, dbuser, format, layer, scale_factor };
return (base) ? baseKeyTpl(tplValues) : rendererKeyTpl(tplValues);
}
};