Skip to content

Commit

Permalink
Added server wide events
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrienCastex committed Aug 2, 2017
1 parent 9a5688c commit 6786998
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
29 changes: 29 additions & 0 deletions lib/server/v2/webDAVServer/WebDAVServer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,19 @@ 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;
options: WebDAVServerOptions;
methods: {
[methodName: string]: HTTPMethod;
};
events: {
[event: string]: EventCallback[];
};
protected beforeManagers: beforeAfter.RequestListener[];
protected afterManagers: beforeAfter.RequestListener[];
protected unknownMethod: HTTPMethod;
Expand Down Expand Up @@ -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;
Expand Down
20 changes: 20 additions & 0 deletions lib/server/v2/webDAVServer/WebDAVServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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();
};
Expand Down
45 changes: 45 additions & 0 deletions src/server/v2/webDAVServer/WebDAVServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand All @@ -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;
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 6786998

Please sign in to comment.