2018-09-03 21:38:30 +02:00
|
|
|
/*
|
2020-03-25 15:28:07 +01:00
|
|
|
Copyright 2018, 2020 New Vector Ltd
|
2018-09-03 21:38:30 +02:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import request from 'browser-request';
|
|
|
|
|
2019-06-26 22:08:04 +02:00
|
|
|
// Load the config file. First try to load up a domain-specific config of the
|
|
|
|
// form "config.$domain.json" and if that fails, fall back to config.json.
|
2020-03-25 15:07:22 +01:00
|
|
|
export async function getVectorConfig(relativeLocation: string='') {
|
2018-09-03 21:38:30 +02:00
|
|
|
if (relativeLocation !== '' && !relativeLocation.endsWith('/')) relativeLocation += '/';
|
2020-03-25 13:39:42 +01:00
|
|
|
|
|
|
|
const specificConfigPromise = getConfig(`${relativeLocation}config.${document.domain}.json`);
|
|
|
|
const generalConfigPromise = getConfig(relativeLocation + "config.json");
|
|
|
|
|
2018-09-03 21:38:30 +02:00
|
|
|
try {
|
2020-03-25 13:39:42 +01:00
|
|
|
const configJson = await specificConfigPromise;
|
2018-09-03 21:38:30 +02:00
|
|
|
// 404s succeed with an empty json config, so check that there are keys
|
|
|
|
if (Object.keys(configJson).length === 0) {
|
|
|
|
throw new Error(); // throw to enter the catch
|
|
|
|
}
|
|
|
|
return configJson;
|
|
|
|
} catch (e) {
|
2020-03-25 13:39:42 +01:00
|
|
|
return await generalConfigPromise;
|
2018-09-03 21:38:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-25 15:07:22 +01:00
|
|
|
function getConfig(configJsonFilename: string): Promise<{}> {
|
2018-10-03 02:20:03 +02:00
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
request(
|
2019-07-16 12:49:10 +02:00
|
|
|
{ method: "GET", url: configJsonFilename, qs: { cachebuster: Date.now() } },
|
2018-10-03 02:20:03 +02:00
|
|
|
(err, response, body) => {
|
2019-06-27 19:47:29 +02:00
|
|
|
try {
|
|
|
|
if (err || response.status < 200 || response.status >= 300) {
|
|
|
|
// Lack of a config isn't an error, we should
|
|
|
|
// just use the defaults.
|
|
|
|
// Also treat a blank config as no config, assuming
|
|
|
|
// the status code is 0, because we don't get 404s
|
|
|
|
// from file: URIs so this is the only way we can
|
|
|
|
// not fail if the file doesn't exist when loading
|
|
|
|
// from a file:// URI.
|
|
|
|
if (response) {
|
|
|
|
if (response.status == 404 || (response.status == 0 && body == '')) {
|
|
|
|
resolve({});
|
|
|
|
}
|
2018-10-03 02:20:03 +02:00
|
|
|
}
|
2019-06-27 19:47:29 +02:00
|
|
|
reject({err: err, response: response});
|
|
|
|
return;
|
2018-09-03 21:38:30 +02:00
|
|
|
}
|
|
|
|
|
2019-06-27 19:47:29 +02:00
|
|
|
// We parse the JSON ourselves rather than use the JSON
|
|
|
|
// parameter, since this throws a parse error on empty
|
|
|
|
// which breaks if there's no config.json and we're
|
|
|
|
// loading from the filesystem (see above).
|
|
|
|
resolve(JSON.parse(body));
|
|
|
|
} catch (e) {
|
|
|
|
reject({err: e});
|
|
|
|
}
|
2018-10-05 17:41:45 +02:00
|
|
|
},
|
2018-10-03 02:20:03 +02:00
|
|
|
);
|
2018-10-05 17:41:45 +02:00
|
|
|
});
|
2018-09-03 21:38:30 +02:00
|
|
|
}
|