Skip to content

Commit

Permalink
Modified the 'IAutoSave' interface to fit cases + Removed the initial…
Browse files Browse the repository at this point in the history
… GZip/GUnZip of the persistence => now it is a default behaviour
  • Loading branch information
AdrienCastex committed Jul 8, 2017
1 parent c9f34e3 commit ac1fd34
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 22 deletions.
2 changes: 1 addition & 1 deletion lib/server/v2/WebDAVServerOptions.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface IAutoSave {
treeFilePath: string;
tempTreeFilePath?: string;
onSaveError?: (error: Error) => void;
streamProvider?: (inputStream: Writable, callback: (outputStream?: Writable) => void) => void;
streamProvider?: (callback: (inputStream?: Writable, outputStream?: Writable) => void) => void;
}
export interface IAutoLoad {
treeFilePath?: string;
Expand Down
19 changes: 9 additions & 10 deletions lib/server/v2/webDAVServer/Persistence.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,11 @@ function autoLoad(callback) {
return callback(new Error('The "treeFilePath" of the "autoLoad" option is not found.'));
else
options.treeFilePath = this.options.autoSave.treeFilePath;
var oStream = fs.createReadStream(options.treeFilePath);
var stream = oStream.pipe(zlib.createGunzip());
oStream.on('error', callback);
var stream = fs.createReadStream(options.treeFilePath);
stream.on('error', callback);
var streamProvider = options.streamProvider;
if (!streamProvider)
streamProvider = function (s, cb) { return cb(s); };
streamProvider = function (s, cb) { return cb(s.pipe(zlib.createGunzip())); };
streamProvider(stream, function (s) {
if (!s)
s = stream;
Expand All @@ -60,7 +58,7 @@ exports.save = save;
function autoSave(options) {
var _this = this;
if (!options.streamProvider)
options.streamProvider = function (s, cb) { return cb(s); };
options.streamProvider = function (cb) { return cb(); };
if (!options.onSaveError)
options.onSaveError = function () { };
if (!options.tempTreeFilePath)
Expand Down Expand Up @@ -89,19 +87,20 @@ function autoSave(options) {
next();
}
else {
var stream_1 = zlib.createGzip();
options.streamProvider(stream_1, function (outputStream) {
options.streamProvider(function (inputStream, outputStream) {
if (!inputStream)
inputStream = zlib.createGzip();
if (!outputStream)
outputStream = stream_1;
outputStream = inputStream;
outputStream.pipe(fs.createWriteStream(options.tempTreeFilePath));
stream_1.end(JSON.stringify(data), function (e) {
inputStream.end(JSON.stringify(data), function (e) {
if (e) {
options.onSaveError(e);
next();
return;
}
});
stream_1.on('close', function () {
inputStream.on('close', function () {
fs.unlink(options.treeFilePath, function (e) {
if (e && e.code !== 'ENOENT') {
options.onSaveError(e);
Expand Down
2 changes: 1 addition & 1 deletion src/server/v2/WebDAVServerOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export interface IAutoSave
treeFilePath : string,
tempTreeFilePath ?: string,
onSaveError ?: (error : Error) => void,
streamProvider ?: (inputStream : Writable, callback : (outputStream ?: Writable) => void) => void
streamProvider ?: (callback : (inputStream ?: Writable, outputStream ?: Writable) => void) => void
}

export interface IAutoLoad
Expand Down
19 changes: 9 additions & 10 deletions src/server/v2/webDAVServer/Persistence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,14 @@ export function autoLoad(callback : SimpleCallback)
else
options.treeFilePath = this.options.autoSave.treeFilePath;

const oStream = fs.createReadStream(options.treeFilePath);
const stream = oStream.pipe(zlib.createGunzip());
const stream = fs.createReadStream(options.treeFilePath);

oStream.on('error', callback)
stream.on('error', callback)

let streamProvider = options.streamProvider;

if(!streamProvider)
streamProvider = (s, cb) => cb(s);
streamProvider = (s, cb) => cb(s.pipe(zlib.createGunzip()));

streamProvider(stream, (s : Readable) => {
if(!s)
Expand All @@ -74,7 +72,7 @@ export function save(callback : (error : Error, obj : SerializedData) => void)
export function autoSave(options : IAutoSave)
{
if(!options.streamProvider)
options.streamProvider = (s, cb) => cb(s);
options.streamProvider = (cb) => cb();
if(!options.onSaveError)
options.onSaveError = () => {};
if(!options.tempTreeFilePath)
Expand Down Expand Up @@ -110,13 +108,14 @@ export function autoSave(options : IAutoSave)
}
else
{
const stream = zlib.createGzip();
options.streamProvider(stream, (outputStream) => {
options.streamProvider((inputStream, outputStream) => {
if(!inputStream)
inputStream = zlib.createGzip();
if(!outputStream)
outputStream = stream;
outputStream = inputStream;
outputStream.pipe(fs.createWriteStream(options.tempTreeFilePath));

stream.end(JSON.stringify(data), (e) => {
inputStream.end(JSON.stringify(data), (e) => {
if(e)
{
options.onSaveError(e);
Expand All @@ -125,7 +124,7 @@ export function autoSave(options : IAutoSave)
}
});

stream.on('close', () => {
inputStream.on('close', () => {
fs.unlink(options.treeFilePath, (e) => {
if(e && e.code !== 'ENOENT') // An error other than ENOENT (no file/folder found)
{
Expand Down

0 comments on commit ac1fd34

Please sign in to comment.