Created
October 4, 2019 15:50
-
-
Save TechnotronicOz/c7af9492bb051e8953bab874629935c7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const comb = require('comb'); | |
const stream = require('stream'); | |
const util = require('util'); | |
const path = require('path'); | |
const streamBuffers = require('stream-buffers'); | |
const config = require('../../../../../../config').loadSync().facade.awardFile; | |
const AWARD_DIR = config.localSettings.outputDir; | |
const Writable = stream.Writable; | |
module.exports = { | |
saveFile, | |
getFileStream, | |
fileExists, | |
clear: clearStore, | |
}; | |
// this will be our store | |
const memoryStore = {}; | |
function WritableMemoryStream(key, opts) { | |
if (!(this instanceof WritableMemoryStream)) { | |
return new WritableMemoryStream(key, opts); | |
} | |
Writable.call(this, opts); | |
this.key = key; | |
memoryStore[key] = Buffer.from(''); | |
} | |
util.inherits(WritableMemoryStream, Writable); | |
WritableMemoryStream.prototype._write = function _write(chunk, enc, cb) { | |
const buffer = (Buffer.isBuffer(chunk)) ? chunk : Buffer.from(chunk, enc); | |
memoryStore[this.key] = Buffer.concat([ memoryStore[this.key], buffer ]); | |
cb(); | |
}; | |
function clearStore() { | |
for (const k in memoryStore) { | |
/* eslint guard-for-in:0 no-restricted-syntax:0 */ | |
delete memoryStore[k]; | |
} | |
} | |
function fileExists(fileName) { | |
if (memoryStore[fileName]) { | |
return comb.resolved(true); | |
} if (memoryStore[`${AWARD_DIR}/${fileName}`]) { | |
// backup method is looking by full file path | |
return comb.resolved(true); | |
} | |
return comb.resolved(false); | |
} | |
function getFileStream(fileName) { | |
return fileExists(fileName).chain((exists) => { | |
if (!exists) { | |
return new comb.Promise().errback(new Error(`${fileName} is not in memory store`)); | |
} | |
const data = (memoryStore[fileName] || memoryStore[`${AWARD_DIR}/${fileName}`]); | |
const readableStreamBuffer = new streamBuffers.ReadableStreamBuffer({ | |
frequency: 10, // in milliseconds. | |
chunkSize: 2048, // in bytes. | |
}); | |
// push the uint8array buffer into the readable stream buffer | |
readableStreamBuffer.put(data); | |
// tell the readableStreamBuffer we are done writing | |
readableStreamBuffer.stop(); | |
return comb.resolved(readableStreamBuffer); | |
}); | |
} | |
function saveFile(filePath, fileStream) { | |
// update filePath to he full path | |
const fullPath = path.resolve(AWARD_DIR, filePath); | |
const ret = new comb.Promise(); | |
const ws = new WritableMemoryStream(fullPath); | |
fileStream.on('error', ret.errback); | |
fileStream.pipe(ws) | |
.on('error', (err) => { | |
ret.errback(err); | |
}) | |
.on('finish', () => { | |
ret.callback(); | |
}); | |
return ret.promise(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment