diff --git a/backend/init.js b/backend/init.js new file mode 100644 index 000000000..9ac75bcb4 --- /dev/null +++ b/backend/init.js @@ -0,0 +1,25 @@ +import Logger from './logger'; + +import { + deleteAllConnections, + deleteBadConnections +} from './persistent/Connections.js'; +import {getSetting} from './settings.js'; + +const setCSVStorageSize = require('./persistent/datastores/csv.js').setStorageSize; + +export default function init() { + try { + deleteBadConnections(); + } catch (error) { + Logger.log(`Failed to delete bad connections: ${error.message}`); + deleteAllConnections(); + } + + try { + setCSVStorageSize(getSetting('CSV_STORAGE_SIZE')); + } catch (error) { + Logger.log(`Failed to get setting CSV_STORAGE_SIZE: ${error.message}`); + setCSVStorageSize(0); + } +} diff --git a/backend/persistent/Connections.js b/backend/persistent/Connections.js index b6562143a..58c31cb07 100644 --- a/backend/persistent/Connections.js +++ b/backend/persistent/Connections.js @@ -5,6 +5,7 @@ import {assoc, dissoc, findIndex} from 'ramda'; import uuid from 'uuid'; import YAML from 'yamljs'; import * as Datastores from './datastores/Datastores.js'; +import {DIALECTS} from '../../app/constants/constants.js'; import {getSetting} from '../settings'; @@ -39,11 +40,32 @@ export function deleteConnectionById(id) { const connections = getConnections(); const index = findIndex(connection => connection.id === id, connections); if (index > -1) { + Datastores.disconnect(connections[index]); connections.splice(index, 1); fs.writeFileSync(getSetting('CONNECTIONS_PATH'), YAML.stringify(connections, 4)); } } +export function deleteBadConnections() { + getConnections().forEach(connection => { + const {id, dialect} = connection; + + const dialects = Object.getOwnPropertyNames(DIALECTS).map(k => DIALECTS[k]); + + const isUnknownDialect = (dialects.indexOf(dialect) === -1); + if (isUnknownDialect) { + deleteConnectionById(id); + } + }); +} + +export function deleteAllConnections() { + if (!fs.existsSync(getSetting('STORAGE_PATH'))) { + createStoragePath(); + } + fs.writeFileSync(getSetting('CONNECTIONS_PATH'), YAML.stringify([], 4)); +} + export function getSanitizedConnections() { const connections = getConnections(); return connections.map(cred => sanitize(cred)); @@ -60,7 +82,7 @@ export function saveConnection(connectionObject) { return connectionId; } -export function validateConnection (connectionObject) { +export function validateConnection(connectionObject) { return Datastores.connect(connectionObject).then(() => { return {}; }).catch(err => { diff --git a/backend/routes.js b/backend/routes.js index 5af56b5f7..e386555a9 100644 --- a/backend/routes.js +++ b/backend/routes.js @@ -1,11 +1,11 @@ -var restify = require('restify'); -var CookieParser = require('restify-cookies'); +const restify = require('restify'); +const CookieParser = require('restify-cookies'); +const fetch = require('node-fetch'); + import * as fs from 'fs'; import path from 'path'; import * as Datastores from './persistent/datastores/Datastores.js'; -const setCSVStorageSize = require('./persistent/datastores/csv.js').setStorageSize; - import {PlotlyOAuth} from './plugins/authorization.js'; import {getQueries, getQuery, deleteQuery} from './persistent/Queries'; import { @@ -27,7 +27,7 @@ import {checkWritePermissions, newDatacache} from './persistent/PlotlyAPI.js'; import {contains, keys, isEmpty, merge, pluck} from 'ramda'; import {getCerts, timeoutFetchAndSaveCerts, setRenewalJob} from './certificates'; import Logger from './logger'; -import fetch from 'node-fetch'; +import init from './init.js'; export default class Servers { /* @@ -36,12 +36,7 @@ export default class Servers { * The httpsServer starts when certificates have been created. */ constructor(args = {createCerts: true, startHttps: true, isElectron: false}) { - try { - setCSVStorageSize(getSetting('CSV_STORAGE_SIZE')); - } catch (error) { - Logger.log(`Failed to get setting CSV_STORAGE_SIZE: ${error.message}`); - setCSVStorageSize(0); - } + init(); this.httpServer = { port: null,