From a9062159ed9a5ff98542cb8cc60e7bf1c3dfa51a Mon Sep 17 00:00:00 2001 From: Calvin Montgomery Date: Sun, 10 Dec 2017 09:48:40 -0800 Subject: [PATCH] Fix partial saving for flatfile channel data --- src/channel-storage/filestore.js | 39 +++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/src/channel-storage/filestore.js b/src/channel-storage/filestore.js index 3ea6384f..03e22ec6 100644 --- a/src/channel-storage/filestore.js +++ b/src/channel-storage/filestore.js @@ -1,4 +1,4 @@ -import * as Promise from 'bluebird'; +import Promise from 'bluebird'; import { stat } from 'fs'; import * as fs from 'graceful-fs'; import path from 'path'; @@ -37,18 +37,35 @@ export class FileStore { }); } - save(id, channelName, data) { - const filename = this.filenameForChannel(channelName); - const fileContents = new Buffer(JSON.stringify(data), 'utf8'); - if (fileContents.length > SIZE_LIMIT) { - return Promise.reject(new ChannelStateSizeError( - 'Channel state size is too large', { - limit: SIZE_LIMIT, - actual: fileContents.length - })); + async save(id, channelName, data) { + let original; + try { + original = await this.load(id, channelName); + } catch (error) { + if (error.code !== 'ENOENT') { + throw error; + } else { + original = {}; + } } - return writeFileAsync(filename, fileContents); + Object.keys(data).forEach(key => { + original[key] = data[key]; + }); + + const filename = this.filenameForChannel(channelName); + const fileContents = new Buffer(JSON.stringify(original), 'utf8'); + if (fileContents.length > SIZE_LIMIT) { + throw new ChannelStateSizeError( + 'Channel state size is too large', + { + limit: SIZE_LIMIT, + actual: fileContents.length + } + ); + } + + return await writeFileAsync(filename, fileContents); } listChannels() {