Fix partial saving for flatfile channel data

This commit is contained in:
Calvin Montgomery 2017-12-10 09:48:40 -08:00
parent 64350cc492
commit a9062159ed
1 changed files with 28 additions and 11 deletions

View File

@ -1,4 +1,4 @@
import * as Promise from 'bluebird'; import Promise from 'bluebird';
import { stat } from 'fs'; import { stat } from 'fs';
import * as fs from 'graceful-fs'; import * as fs from 'graceful-fs';
import path from 'path'; import path from 'path';
@ -37,18 +37,35 @@ export class FileStore {
}); });
} }
save(id, channelName, data) { async save(id, channelName, data) {
const filename = this.filenameForChannel(channelName); let original;
const fileContents = new Buffer(JSON.stringify(data), 'utf8'); try {
if (fileContents.length > SIZE_LIMIT) { original = await this.load(id, channelName);
return Promise.reject(new ChannelStateSizeError( } catch (error) {
'Channel state size is too large', { if (error.code !== 'ENOENT') {
limit: SIZE_LIMIT, throw error;
actual: fileContents.length } 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() { listChannels() {