Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

How to add a plugin? #68

Open
johnjbarton opened this Issue · 9 comments

3 participants

@johnjbarton

The jsDAV server seems to support 'plugins', but I have not been able to figure out how to get them to work.

I tried adding an options.plugins but once I read the code it seems that this object is some sort of filter on the DEFAULT_PLUGINS (that alone is very confusing).

Then I tried

jsDAV.createServer({
    node: __dirname + "/..",
    locksBackend: new jsDAV_Locks_Backend_FS(__dirname + "/jsDAV_locks_8686")
}, 8686).plugins.CORS_Plugin = jsDAV_CORS_Plugin;

No luck. I could not find any doc pages or online info.

@mikedeboer
Owner

Hi John, this feature is indeed undocumented at the moment.

I understand that the plugin system may appear as confusing, but allow me to explain:

The DEFAULT_PLUGINS is a map, populated at the first require() of server.js with the plugins bundled inside the lib/DAV/plugins/ folder. If you put your plugin inside that folder, it will load automatically.

If you want to use a plugin that is located some place other than the plugins folder of jsDAV - which is quite common, because jsDAV is often used as a module dependency within projects - you can do something like this:

var jsDAV = require("jsdav/lib/jsdav");
var jsDAV_Server = require("jsdav/lib/DAV/server");
var jsDAV_Util = require("jsdav/lib/shared/util");

jsDAV.createServer({
    node: __dirname + "/..",
    plugins: jsDAV_Util.extend(jsDAV_Server.DEFAULT_PLUGINS, {
        "cors": require("./cors")
    })
});

This takes the bundled plugins and combines them with the one you wrote. Additionally, you can forget about the defaults and hand-pick all plugins.

Next up is how to start writing your own plugin, but that's a story that goes a bit further than your question, which I hope I answered (if I did, I will add this to the wiki :wink: ).

@johnjbarton
@mikedeboer
Owner

I see the confusion... so far I was lucky enough that my direct colleagues or I were the only ones who wanted to create plugins :smile:

The lifetime of plugins is the following:

  1. The server object is created once, will be resident during the lifetime of the node process. It fires up the HTTP server (if used standalone) and dispatches incoming requests. It also keeps a reference to the plugin objects, so the handler object can find them efficiently.
  2. When a request comes in at the server, it instantiates a new handler, to which the request is passed along. The handler is responsible for handling the request and the response to it. The lifetime of a handler object is parallel to the lifetime of the response object. Upon instantiation, the handler also instantiates each and every plugin. In other words: the lifetime of a plugin is parallel to the handler, thus parallel to the response object.

An important note is that the handler indeed emits events, but it's not the NodeJS EventEmitter. Instead I use something that I 'invented': asynchronous events. I wanted the plugins to hook into the system through a signal-slot type of connection, but these plugins are very likely to perform asynchronous operations and that doesn't match with the synchronous EventEmitter object provided by NodeJS' events module. Therefore I created an async-capable EventEmitter that mirrors the NodeJS API, which simply adds two simple functions to the Event object: next() and stop(). A good example that shows off this API at work can be found at https://github.com/mikedeboer/jsDAV/blob/master/lib/DAV/plugins/browser.js

I hope this explains it a bit for ya!

@mikedeboer
Owner

@johnjbarton I just published a new version of jsDAV: v0.3.0, which includes an entirely revamped object inheritance model, DAVACL and CardDAV (address book) support. Another goal for this version and onward is improved end-user documentation. I will explain how to use and extend jsDAV in more detail on the wiki soon.

If you have any questions, please feel free to drop me a line.

@bedney

@johnjbarton - Sorry to spam this issue, but your post here intrigued me. Are you adding CORS support to jsDAV? If so, are you going to make this publicly available. I have a great interest in this! :-).

Thanks!

@johnjbarton

I started, but the problem I was working around stopped failing, so I stopped. This area is not my focus so as it turns out I left my experimental code around. It may hurt more than help, but here:

https://github.com/google/qpp/blob/master/http/DAVServer.js

@bedney
@bedney
@johnjbarton
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.