UNPKG

cloud-red

Version:

Harnessing Serverless for your cloud integration needs

120 lines (115 loc) 3.63 kB
var fs = require('fs'); const path = require('path'); const fsextra = require('fs-extra'); var when = require('when'); let lambdaBasePath = '/tmp'; const SETTINGS_FILENAME = path.join(lambdaBasePath, 'settings.json'); let settings; let flowFilePath; // creds: <flow_name>_cred.json let storageEphemeral = { _getCredentialsFilename: function() { return `${path.basename(flowFilePath, '.json')}_cred.json`; }, init: function(_settings) { settings = _settings; console.log( `[ephemeral-storage] settings: ${JSON.stringify(settings, '', 2)}` ); basePath = settings.basePath; flowFilePath = settings.flowFilePath; return when.promise(function(resolve, reject) { if (fs.existsSync(`${flowFilePath}`)) { console.log(`[ephemeral-storage] Flow found: ${flowFilePath}`); resolve(); } else { reject(`[ephemeral-storage] Flow not found in: ${flowFilePath}`); } }); }, getFlows: function() { console.log('[ephemeral-storage] GetFlows '); return this.getData(`${flowFilePath}`); }, saveFlows: function(flow) { return when.promise(function(resolve, reject) { console.log( '[ephemeral-storage] Saving flow! - WARNING!! - This should not been invoked' ); resolve(); }); }, getCredentials: function() { console.log( `[ephemeral-storage] GetCredentials:${this._getCredentialsFilename()}` ); return this.getData(this._getCredentialsFilename()); }, saveCredentials: function(creds) { console.log('[ephemeral-storage] SaveCredentials '); return this.saveData(this._getCredentialsFilename(), creds); }, getSettings: function() { console.log(`[ephemeral-storage] GetSettings: ${SETTINGS_FILENAME}`); return this.getData(SETTINGS_FILENAME); }, saveSettings: function(creds) { console.log('[ephemeral-storage] SaveSettings '); return this.saveData(SETTINGS_FILENAME, creds); }, getLibraryEntry: function(type, path) { return when.promise(function(resolve, reject) { console.log( '[ephemeral-storage] Get Library Entry! - WARNING!! - This should not been invoked' ); resolve(); }); }, saveLibraryEntry: function(type, path, meta, body) { return when.promise(function(resolve, reject) { console.log( '[ephemeral-storage] Save Library Entry! - WARNING!! - This should not been invoked' ); resolve(); }); }, getData: function(fileName) { return when.promise(function(resolve, reject) { fs.readFile(fileName, 'utf8', function(err, flowContent) { if (err) { console.log(`[ephemeral-storage] ERROR code: ${err.code}`); if (err.code === 'ENOENT') { console.log('File not found!'); return resolve({}); } else { return reject( `[ephemeral-storage] Error found while reading the file: ${fileName}` ); } } console.log(flowContent); let flowObject = JSON.parse(flowContent); // console.log(flowObject); return resolve(flowObject); }); }); }, saveData: function(fileName, data) { console.log( `[ephemeral-storage] Saving data: ${data} into filename: ${fileName}` ); console.log(data); return when.promise(function(resolve, reject) { fsextra .writeJSON(fileName, data) .then(() => { return resolve(); }) .catch(err => { console.log(err); reject(err.toString()); }); }); } }; module.exports = storageEphemeral;