Merge branch '3.0' of https://github.com/calzoneman/sync into 3.0
[KisSync.git] / src / configuration / camoconfig.js
blobc3df2edd541f4b2dbf9bf6ca641b5c5af73e2ca6
1 const SPECIALCHARS = /([\\.?+*$^|()[\]{}])/g;
3 class CamoConfig {
4 constructor(config = { camo: { enabled: false } }) {
5 this.config = config.camo;
6 if (this.config.server) {
7 this.config.server = this.config.server.replace(/\/+$/, '');
9 this.validate();
12 validate() {
13 if (this.config.encoding
14 && !~['url', 'hex'].indexOf(this.config.encoding)) {
15 throw new Error(`Value for key 'encoding' must be either 'url' or 'hex', not '${this.config.encoding}'`);
19 isEnabled() {
20 return this.config.enabled;
23 getKey() {
24 return this.config.key;
27 getServer() {
28 return this.config.server;
31 getWhitelistedDomains() {
32 return this.config['whitelisted-domains'] || [];
35 getWhitelistedDomainsRegexp() {
36 const domains = this.getWhitelistedDomains()
37 .map(d => '\\.' + d.replace(SPECIALCHARS, '\\$1') + '$');
38 if (domains.length === 0) {
39 // If no whitelist, match nothing
40 return new RegExp('$^');
43 return new RegExp(domains.join('|'), 'i');
46 getEncoding() {
47 return this.config.encoding || 'url';
51 export { CamoConfig };