diff --git a/lib/server/v2/webDAVServer/WebDAVServer.d.ts b/lib/server/v2/webDAVServer/WebDAVServer.d.ts index 233b9c20..b8d1f016 100644 --- a/lib/server/v2/webDAVServer/WebDAVServer.d.ts +++ b/lib/server/v2/webDAVServer/WebDAVServer.d.ts @@ -14,6 +14,9 @@ import * as startStop from './StartStop'; import * as https from 'https'; import * as http from 'http'; export declare type WebDAVServerStartCallback = (server?: http.Server) => void; +export declare type FileSystemEvent = 'create' | 'delete' | 'openReadStream' | 'openWriteStream' | 'move' | 'copy' | 'rename'; +export declare type ServerEvent = FileSystemEvent; +export declare type EventCallback = (ctx: RequestContext, fs: FileSystem, path: Path, data?: any) => void; export declare class WebDAVServer { httpAuthentication: HTTPAuthentication; privilegeManager: PrivilegeManager; @@ -21,6 +24,9 @@ export declare class WebDAVServer { methods: { [methodName: string]: HTTPMethod; }; + events: { + [event: string]: EventCallback[]; + }; protected beforeManagers: beforeAfter.RequestListener[]; protected afterManagers: beforeAfter.RequestListener[]; protected unknownMethod: HTTPMethod; @@ -70,6 +76,29 @@ export declare class WebDAVServer { load: typeof persistence.load; save: typeof persistence.save; method(name: string, manager: HTTPMethod): void; + /** + * Attach a listener to an event. + * + * @param event Name of the event. + * @param listener Listener of the event. + */ + on(event: ServerEvent, listener: EventCallback): this; + /** + * Attach a listener to an event. + * + * @param event Name of the event. + * @param listener Listener of the event. + */ + on(event: string, listener: EventCallback): this; + /** + * Trigger an event. + * + * @param event Name of the event. + * @param ctx Context of the event. + * @param fs File system on which the event happened. + * @param path Path of the resource on which the event happened. + */ + emit(event: string, ctx: RequestContext, fs: FileSystem, path: Path | string, data?: any): void; protected normalizeMethodName(method: string): string; invokeBeforeRequest(base: RequestContext, callback: any): void; invokeAfterRequest(base: RequestContext, callback: any): void; diff --git a/lib/server/v2/webDAVServer/WebDAVServer.js b/lib/server/v2/webDAVServer/WebDAVServer.js index 6f2a9454..d939a0e4 100644 --- a/lib/server/v2/webDAVServer/WebDAVServer.js +++ b/lib/server/v2/webDAVServer/WebDAVServer.js @@ -18,6 +18,7 @@ var WebDAVServer = (function () { this.afterManagers = []; this.methods = {}; this.options = WebDAVServerOptions_1.setDefaultServerOptions(options); + this.events = {}; this.httpAuthentication = this.options.httpAuthentication; this.privilegeManager = this.options.privilegeManager; this.fileSystems = { @@ -168,6 +169,25 @@ var WebDAVServer = (function () { WebDAVServer.prototype.method = function (name, manager) { this.methods[this.normalizeMethodName(name)] = manager; }; + WebDAVServer.prototype.on = function (event, listener) { + if (!this.events[event]) + this.events[event] = []; + this.events[event].push(listener); + return this; + }; + /** + * Trigger an event. + * + * @param event Name of the event. + * @param ctx Context of the event. + * @param fs File system on which the event happened. + * @param path Path of the resource on which the event happened. + */ + WebDAVServer.prototype.emit = function (event, ctx, fs, path, data) { + if (!this.events[event]) + return; + this.events[event].forEach(function (l) { return process.nextTick(function () { return l(ctx, fs, path.constructor === String ? new Path_1.Path(path) : path, data); }); }); + }; WebDAVServer.prototype.normalizeMethodName = function (method) { return method.toLowerCase(); }; diff --git a/src/server/v2/webDAVServer/WebDAVServer.ts b/src/server/v2/webDAVServer/WebDAVServer.ts index 6defbb57..e904b4d3 100644 --- a/src/server/v2/webDAVServer/WebDAVServer.ts +++ b/src/server/v2/webDAVServer/WebDAVServer.ts @@ -18,12 +18,17 @@ import * as http from 'http' export type WebDAVServerStartCallback = (server ?: http.Server) => void; +export type FileSystemEvent = 'create' | 'delete' | 'openReadStream' | 'openWriteStream' | 'move' | 'copy' | 'rename'; +export type ServerEvent = FileSystemEvent; +export type EventCallback = (ctx : RequestContext, fs : FileSystem, path : Path, data ?: any) => void; + export class WebDAVServer { public httpAuthentication : HTTPAuthentication public privilegeManager : PrivilegeManager public options : WebDAVServerOptions public methods : { [methodName : string]: HTTPMethod } + public events : { [event : string] : EventCallback[] } protected beforeManagers : beforeAfter.RequestListener[] protected afterManagers : beforeAfter.RequestListener[] @@ -40,6 +45,7 @@ export class WebDAVServer this.afterManagers = []; this.methods = {}; this.options = setDefaultServerOptions(options); + this.events = {}; this.httpAuthentication = this.options.httpAuthentication; this.privilegeManager = this.options.privilegeManager; @@ -266,6 +272,45 @@ export class WebDAVServer this.methods[this.normalizeMethodName(name)] = manager; } + /** + * Attach a listener to an event. + * + * @param event Name of the event. + * @param listener Listener of the event. + */ + on(event : ServerEvent, listener : EventCallback) : this + /** + * Attach a listener to an event. + * + * @param event Name of the event. + * @param listener Listener of the event. + */ + on(event : string, listener : EventCallback) : this + on(event : ServerEvent | string, listener : EventCallback) : this + { + if(!this.events[event]) + this.events[event] = []; + this.events[event].push(listener); + + return this; + } + + /** + * Trigger an event. + * + * @param event Name of the event. + * @param ctx Context of the event. + * @param fs File system on which the event happened. + * @param path Path of the resource on which the event happened. + */ + emit(event : string, ctx : RequestContext, fs : FileSystem, path : Path | string, data ?: any) : void + { + if(!this.events[event]) + return; + + this.events[event].forEach((l) => process.nextTick(() => l(ctx, fs, path.constructor === String ? new Path(path as string) : path as Path, data))); + } + protected normalizeMethodName(method : string) : string { return method.toLowerCase();