diff --git a/lib/server/v2/WebDAVServerOptions.d.ts b/lib/server/v2/WebDAVServerOptions.d.ts index a6fa9f5c..fa12509d 100644 --- a/lib/server/v2/WebDAVServerOptions.d.ts +++ b/lib/server/v2/WebDAVServerOptions.d.ts @@ -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; diff --git a/lib/server/v2/webDAVServer/Persistence.js b/lib/server/v2/webDAVServer/Persistence.js index e5944e8e..84f743c1 100644 --- a/lib/server/v2/webDAVServer/Persistence.js +++ b/lib/server/v2/webDAVServer/Persistence.js @@ -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; @@ -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) @@ -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); diff --git a/src/server/v2/WebDAVServerOptions.ts b/src/server/v2/WebDAVServerOptions.ts index b5cd6825..526df364 100644 --- a/src/server/v2/WebDAVServerOptions.ts +++ b/src/server/v2/WebDAVServerOptions.ts @@ -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 diff --git a/src/server/v2/webDAVServer/Persistence.ts b/src/server/v2/webDAVServer/Persistence.ts index 34582a4f..cf7d0a19 100644 --- a/src/server/v2/webDAVServer/Persistence.ts +++ b/src/server/v2/webDAVServer/Persistence.ts @@ -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) @@ -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) @@ -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); @@ -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) {