Skip to content

Commit

Permalink
Moved the 'autoSave' signature (in the server options) to an external…
Browse files Browse the repository at this point in the history
… interface
  • Loading branch information
AdrienCastex committed Jun 16, 2017
1 parent d80491a commit fcb22cf
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 41 deletions.
13 changes: 7 additions & 6 deletions lib/server/WebDAVServerOptions.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ import { IUserManager } from '../user/IUserManager';
import { IResource } from '../resource/IResource';
import { Writable } from 'stream';
import * as https from 'https';
export interface IAutoSave {
treeFilePath: string;
tempTreeFilePath: string;
onSaveError?: (error: Error) => void;
streamProvider?: (inputStream: Writable, callback: (outputStream?: Writable) => void) => void;
}
export declare class WebDAVServerOptions {
requireAuthentification?: boolean;
httpAuthentication?: HTTPAuthentication;
Expand All @@ -19,12 +25,7 @@ export declare class WebDAVServerOptions {
port?: number;
serverName?: string;
version?: string;
autoSave?: {
treeFilePath: string;
tempTreeFilePath: string;
onSaveError?: (error: Error) => void;
streamProvider?: (inputStream: Writable, callback: (outputStream?: Writable) => void) => void;
};
autoSave?: IAutoSave;
}
export default WebDAVServerOptions;
export declare function setDefaultServerOptions(options: WebDAVServerOptions): WebDAVServerOptions;
28 changes: 14 additions & 14 deletions lib/server/webDAVServer/StartStop.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ var https = require("https");
var http = require("http");
var zlib = require("zlib");
var fs = require("fs");
function autoSave(treeFilePath, tempTreeFilePath, onSaveError, streamProvider) {
function autoSave(options) {
var _this = this;
if (!streamProvider)
streamProvider = function (s, cb) { return cb(s); };
if (!onSaveError)
onSaveError = function () { };
if (!options.streamProvider)
options.streamProvider = function (s, cb) { return cb(s); };
if (!options.onSaveError)
options.onSaveError = function () { };
var saving = false;
var saveRequested = false;
this.afterRequest(function (arg, next) {
Expand All @@ -31,32 +31,32 @@ function autoSave(treeFilePath, tempTreeFilePath, onSaveError, streamProvider) {
var save_1 = function () {
this.save(function (e, data) {
if (e) {
onSaveError(e);
options.onSaveError(e);
next();
}
else {
var stream_1 = zlib.createGzip();
streamProvider(stream_1, function (outputStream) {
options.streamProvider(stream_1, function (outputStream) {
if (!outputStream)
outputStream = stream_1;
outputStream.pipe(fs.createWriteStream(tempTreeFilePath));
outputStream.pipe(fs.createWriteStream(options.tempTreeFilePath));
stream_1.end(JSON.stringify(data), function (e) {
if (e) {
onSaveError(e);
options.onSaveError(e);
next();
return;
}
});
stream_1.on('close', function () {
fs.unlink(treeFilePath, function (e) {
fs.unlink(options.treeFilePath, function (e) {
if (e && e.code !== 'ENOENT') {
onSaveError(e);
options.onSaveError(e);
next();
return;
}
fs.rename(tempTreeFilePath, treeFilePath, function (e) {
fs.rename(options.tempTreeFilePath, options.treeFilePath, function (e) {
if (e)
onSaveError(e);
options.onSaveError(e);
next();
});
});
Expand Down Expand Up @@ -153,7 +153,7 @@ function start(port, callback) {
});
});
if (this.options.autoSave)
autoSave.bind(this)(this.options.autoSave.treeFilePath, this.options.autoSave.tempTreeFilePath, this.options.autoSave.onError, this.options.autoSave.streamProvider);
autoSave.bind(this)(this.options.autoSave);
}
this.server.listen(_port, this.options.hostname, function () {
if (_callback)
Expand Down
15 changes: 9 additions & 6 deletions src/server/WebDAVServerOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ import { IResource } from '../resource/IResource'
import { Writable } from 'stream'
import * as https from 'https'

export interface IAutoSave
{
treeFilePath : string,
tempTreeFilePath : string,
onSaveError ?: (error : Error) => void,
streamProvider ?: (inputStream : Writable, callback : (outputStream ?: Writable) => void) => void
}

export class WebDAVServerOptions
{
requireAuthentification ?: boolean = false
Expand All @@ -25,12 +33,7 @@ export class WebDAVServerOptions
port ?: number = 1900
serverName ?: string = 'webdav-server'
version ?: string = '1.8.0'
autoSave ?: {
treeFilePath : string
tempTreeFilePath : string
onSaveError ?: (error : Error) => void
streamProvider ?: (inputStream : Writable, callback : (outputStream ?: Writable) => void) => void
} = null
autoSave ?: IAutoSave = null
}
export default WebDAVServerOptions;

Expand Down
31 changes: 16 additions & 15 deletions src/server/webDAVServer/StartStop.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import { HTTPCodes, MethodCallArgs, WebDAVRequest } from '../WebDAVRequest'
import { WebDAVServerStartCallback } from './Types'
import { Writable, Readable } from 'stream'
import { Errors, HTTPError } from '../../Errors'
import { WebDAVServer } from './WebDAVServer'
import { Writable, Readable } from 'stream'
import { IAutoSave } from '../WebDAVServerOptions'
import * as https from 'https'
import * as http from 'http'
import * as zlib from 'zlib'
import * as fs from 'fs'

function autoSave(treeFilePath : string, tempTreeFilePath : string, onSaveError ?: (error : Error) => void, streamProvider ?: (inputStream : Writable, callback : (outputStream ?: Writable) => void) => void)
function autoSave(options : IAutoSave)
{
if(!streamProvider)
streamProvider = (s, cb) => cb(s);
if(!onSaveError)
onSaveError = () => {};
if(!options.streamProvider)
options.streamProvider = (s, cb) => cb(s);
if(!options.onSaveError)
options.onSaveError = () => {};

let saving = false;
let saveRequested = false;
Expand All @@ -40,38 +41,38 @@ function autoSave(treeFilePath : string, tempTreeFilePath : string, onSaveError
this.save((e, data) => {
if(e)
{
onSaveError(e);
options.onSaveError(e);
next();
}
else
{
const stream = zlib.createGzip();
streamProvider(stream, (outputStream) => {
options.streamProvider(stream, (outputStream) => {
if(!outputStream)
outputStream = stream;
outputStream.pipe(fs.createWriteStream(tempTreeFilePath));
outputStream.pipe(fs.createWriteStream(options.tempTreeFilePath));

stream.end(JSON.stringify(data), (e) => {
if(e)
{
onSaveError(e);
options.onSaveError(e);
next();
return;
}
});

stream.on('close', () => {
fs.unlink(treeFilePath, (e) => {
fs.unlink(options.treeFilePath, (e) => {
if(e && e.code !== 'ENOENT') // An error other than ENOENT (no file/folder found)
{
onSaveError(e);
options.onSaveError(e);
next();
return;
}

fs.rename(tempTreeFilePath, treeFilePath, (e) => {
fs.rename(options.tempTreeFilePath, options.treeFilePath, (e) => {
if(e)
onSaveError(e);
options.onSaveError(e);
next();
})
})
Expand Down Expand Up @@ -194,7 +195,7 @@ export function start(port ?: number | WebDAVServerStartCallback, callback ?: We
})

if(this.options.autoSave)
autoSave.bind(this)(this.options.autoSave.treeFilePath, this.options.autoSave.tempTreeFilePath, this.options.autoSave.onError, this.options.autoSave.streamProvider);
autoSave.bind(this)(this.options.autoSave);
}

this.server.listen(_port, this.options.hostname, () => {
Expand Down

0 comments on commit fcb22cf

Please sign in to comment.