diff --git a/app/backend/dns.js b/app/backend/dns.js index c2add0077f..a64510d325 100644 --- a/app/backend/dns.js +++ b/app/backend/dns.js @@ -30,9 +30,13 @@ function lookup (url, forceIPv4) { const v6 = results.find(r => r.family === 6); if (v6) { resolve(v6.address); - } else { + } else if (results.length) { // If no v6, return the first result resolve(results[0].address); + } else { + // The only case I can find that hits this is sending in an IPv6 + // address like `::1` + reject(new Error('Could not resolve host')) } }); }) diff --git a/app/backend/sync/index.js b/app/backend/sync/index.js index 9a4956c76e..b7f9786fb3 100644 --- a/app/backend/sync/index.js +++ b/app/backend/sync/index.js @@ -297,7 +297,7 @@ export async function getOrCreateConfig (resourceGroupId) { export async function createOrUpdateConfig (resourceGroupId, syncMode) { const config = await store.getConfig(resourceGroupId); - const patch = {resourceGroupId, syncMode}; + const patch = {resourceGroupId}; if (config) { return await store.updateConfig(config, patch); @@ -464,7 +464,7 @@ async function _createResourceGroup (name = '') { } // Create a config for it - await createOrUpdateConfig(resourceGroup.id, store.SYNC_MODE_OFF); + await createOrUpdateConfig(resourceGroup.id, store.SYNC_MODE_ON); logger.debug(`created ResourceGroup ${resourceGroup.id}`); return resourceGroup; diff --git a/app/localstorage.js b/app/localstorage.js deleted file mode 100644 index af2fb8edf8..0000000000 --- a/app/localstorage.js +++ /dev/null @@ -1,42 +0,0 @@ -const fs = require('fs'); -const path = require('path'); -const mkdirp = require('mkdirp'); - -class LocalStorage { - constructor (path) { - this._path = path; - this._timeouts = {}; - - // No need to wait for this. - mkdirp(path, err => { - if (err) { - console.warn('[localStorage] Failed to create directory', path, err); - } else { - console.log('[localStorage] initialized'); - } - }); - } - - /** - * Set an item in localStorage. Will debounce on a per-key basis - * - * @param key - * @param value - */ - setItem (key, value) { - clearTimeout(this._timeouts[key]); - this._timeouts[key] = setTimeout(() => { - fs.writeFileSync(path.join(this._path, key), value); - }, 100); - } - - getItem (key) { - try { - return fs.readFileSync(path.join(this._path, key)); - } catch (e) { - return null; - } - } -} - -module.exports.LocalStorage = LocalStorage; diff --git a/app/main.js b/app/main.js index bb158efaa7..becf2f9caf 100644 --- a/app/main.js +++ b/app/main.js @@ -26,8 +26,8 @@ if (!IS_DEV) { const request = require('request'); const path = require('path'); const {version: appVersion, productName: appName} = require('./package.json'); -const {LocalStorage} = require('./localstorage'); const electron = require('electron'); +const mkdirp = require('mkdirp'); const fs = require('fs'); const { app, @@ -40,6 +40,8 @@ const { webContents } = require('electron'); +const LOCAL_STORAGE_DIR = path.join(app.getPath('userData'), 'localStorage'); + const UPDATE_URLS = { // Add `r` param to help cache bust darwin: `https://updates.insomnia.rest/builds/check/mac?v=${appVersion}`, @@ -50,7 +52,6 @@ const UPDATE_URLS = { const DOWNLOAD_URL = 'http://download.insomnia.rest'; let mainWindow = null; -let localStorage = null; let hasPromptedForUpdates = false; // Enable this for CSS grid layout :) @@ -178,9 +179,14 @@ ipcMain.on('check-for-updates', () => { checkForUpdates(); }); + +const _localStorageTimeouts = {}; function storeValue (key, obj) { try { - localStorage.setItem(key, JSON.stringify(obj)); + clearTimeout(_localStorageTimeouts[key]); + _localStorageTimeouts[key] = setTimeout(() => { + fs.writeFileSync(path.join(LOCAL_STORAGE_DIR, key), JSON.stringify(obj)); + }, 100); } catch (e) { console.error('Failed to save to LocalStorage', obj, e); ravenClient.captureError(e, { @@ -190,8 +196,18 @@ function storeValue (key, obj) { } function getValue (key, defaultObj) { + let value = null; try { - return JSON.parse(localStorage.getItem(key) || JSON.stringify(defaultObj)) + value = fs.readFileSync(path.join(LOCAL_STORAGE_DIR, key)); + } catch (e) { + if (e.code === 'ENOENT') { + // If we fail to read it, write the default value to it + storeValue(key, defaultObj); + } + } + + try { + return JSON.parse(value || JSON.stringify(defaultObj)) } catch (e) { console.error('Failed to get from LocalStorage', e); ravenClient.captureError(e, { @@ -274,10 +290,11 @@ app.on('ready', () => { }); function initLocalStorage () { - if (!localStorage) { - localStorage = new LocalStorage( - path.join(app.getPath('userData'), 'localStorage') - ); + try { + mkdirp.sync(LOCAL_STORAGE_DIR); + console.log('[localStorage] initialized'); + } catch (e) { + console.warn('[localStorage] Failed to create directory', path, e); } }