Merge branch 'development'
commit
bff4b27fa7
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,58 @@
|
|||||||
|
const fs = require('fs');
|
||||||
|
|
||||||
|
const _ = require('lodash');
|
||||||
|
|
||||||
|
const ENCODING = 'utf8';
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
start,
|
||||||
|
};
|
||||||
|
|
||||||
|
function start(name, targetPath) {
|
||||||
|
let cachedValue = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const text = fs.readFileSync(targetPath, ENCODING);
|
||||||
|
cachedValue = JSON.parse(text);
|
||||||
|
console.log(`config/get: Successfully read ${name} config file`);
|
||||||
|
|
||||||
|
if (!cachedValue) {
|
||||||
|
console.log(
|
||||||
|
`config/get: ${name} config value was falsy, cache is now empty object`
|
||||||
|
);
|
||||||
|
cachedValue = Object.create(null);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
if (error.code !== 'ENOENT') {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(
|
||||||
|
`config/get: Did not find ${name} config file, cache is now empty object`
|
||||||
|
);
|
||||||
|
cachedValue = Object.create(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
function get(keyPath) {
|
||||||
|
return _.get(cachedValue, keyPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
function set(keyPath, value) {
|
||||||
|
_.set(cachedValue, keyPath, value);
|
||||||
|
console.log(`config/set: Saving ${name} config to disk`);
|
||||||
|
const text = JSON.stringify(cachedValue, null, ' ');
|
||||||
|
fs.writeFileSync(targetPath, text, ENCODING);
|
||||||
|
}
|
||||||
|
|
||||||
|
function remove() {
|
||||||
|
console.log(`config/remove: Deleting ${name} config from disk`);
|
||||||
|
fs.unlinkSync(targetPath);
|
||||||
|
cachedValue = Object.create(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
set,
|
||||||
|
get,
|
||||||
|
remove,
|
||||||
|
};
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
const path = require('path');
|
||||||
|
|
||||||
|
const { app } = require('electron');
|
||||||
|
|
||||||
|
const { start } = require('./base_config');
|
||||||
|
|
||||||
|
const userDataPath = app.getPath('userData');
|
||||||
|
const targetPath = path.join(userDataPath, 'ephemeral.json');
|
||||||
|
|
||||||
|
const ephemeralConfig = start('ephemeral', targetPath);
|
||||||
|
|
||||||
|
module.exports = ephemeralConfig;
|
@ -0,0 +1,44 @@
|
|||||||
|
/* global dcodeIO */
|
||||||
|
/* eslint-disable strict */
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
const functions = {
|
||||||
|
stringToArrayBufferBase64,
|
||||||
|
arrayBufferToStringBase64,
|
||||||
|
};
|
||||||
|
|
||||||
|
onmessage = async e => {
|
||||||
|
const [jobId, fnName, ...args] = e.data;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const fn = functions[fnName];
|
||||||
|
if (!fn) {
|
||||||
|
throw new Error(`Worker: job ${jobId} did not find function ${fnName}`);
|
||||||
|
}
|
||||||
|
const result = await fn(...args);
|
||||||
|
postMessage([jobId, null, result]);
|
||||||
|
} catch (error) {
|
||||||
|
const errorForDisplay = prepareErrorForPostMessage(error);
|
||||||
|
postMessage([jobId, errorForDisplay]);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
function prepareErrorForPostMessage(error) {
|
||||||
|
if (!error) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (error.stack) {
|
||||||
|
return error.stack;
|
||||||
|
}
|
||||||
|
|
||||||
|
return error.message;
|
||||||
|
}
|
||||||
|
|
||||||
|
function stringToArrayBufferBase64(string) {
|
||||||
|
return dcodeIO.ByteBuffer.wrap(string, 'base64').toArrayBuffer();
|
||||||
|
}
|
||||||
|
function arrayBufferToStringBase64(arrayBuffer) {
|
||||||
|
return dcodeIO.ByteBuffer.wrap(arrayBuffer).toString('base64');
|
||||||
|
}
|
Loading…
Reference in New Issue