Permalink
Browse files

Merge pull request #13 from ajaxorg/jsdom

Jsdom
  • Loading branch information...
2 parents fc9a5ab + 1bedab4 commit a702fad9f0e5fbabe8da351cc235b6e74a5ef7d0 @mikedeboer committed Jun 4, 2012
View
13 lib/DAV/handler.js
@@ -637,6 +637,7 @@ exports.NS_AJAXORG = "http://ajax.org/2005/aml";
return self.handleError(err);
self.httpResponse.writeHead(204, {"content-length": "0"});
self.httpResponse.end();
+ self.dispatchEvent("afterDelete", uri);
});
});
});
@@ -751,6 +752,7 @@ exports.NS_AJAXORG = "http://ajax.org/2005/aml";
return self.handleError(err);
self.httpResponse.writeHead(201, {"content-length": "0"});
self.httpResponse.end();
+ self.dispatchEvent("afterWriteContent", uri);
});
}
else {
@@ -791,6 +793,7 @@ exports.NS_AJAXORG = "http://ajax.org/2005/aml";
self.httpResponse.writeHead(200, {"content-length": "0"});
self.httpResponse.end();
+ self.dispatchEvent("afterWriteContent", uri);
}
});
});
@@ -823,7 +826,7 @@ exports.NS_AJAXORG = "http://ajax.org/2005/aml";
"The request body for the MKCOL request must have an xml Content-Type"));
}
- Util.loadDOMDocument(requestBody, function(err, dom) {
+ Util.loadDOMDocument(requestBody, self.server.options.parser, function(err, dom) {
var firstChild = dom.firstChild;
if (Util.toClarkNotation(firstChild) !== "{DAV:}mkcol") {
// We must throw 415 for unsupport mkcol bodies
@@ -932,6 +935,7 @@ exports.NS_AJAXORG = "http://ajax.org/2005/aml";
self.httpResponse.writeHead(moveInfo["destinationExists"] ? 204 : 201,
{"content-length": "0"});
self.httpResponse.end();
+ self.dispatchEvent("afterMove", moveInfo["destination"]);
});
});
});
@@ -982,6 +986,7 @@ exports.NS_AJAXORG = "http://ajax.org/2005/aml";
self.httpResponse.writeHead(copyInfo["destinationExists"] ? 204 : 201,
{"Content-Length": "0"});
self.httpResponse.end();
+ self.dispatchEvent("afterCopy", copyInfo["destination"]);
});
});
}
@@ -999,7 +1004,7 @@ exports.NS_AJAXORG = "http://ajax.org/2005/aml";
this.httpReport = function() {
var self = this;
this.getRequestBody("utf8", null, function(err, data) {
- Util.loadDOMDocument(data, function(err, dom) {
+ Util.loadDOMDocument(data, self.server.options.parser, function(err, dom) {
var reportName = Util.toClarkNotation(dom);
self.dispatchEvent("report", reportName, dom, function(stop) {
if (stop !== true) {
@@ -1818,7 +1823,7 @@ exports.NS_AJAXORG = "http://ajax.org/2005/aml";
if (!body)
return cbpropfindreq(null, []);
- Util.loadDOMDocument(body, function(err, oXml) {
+ Util.loadDOMDocument(body, this.server.options.parser, function(err, oXml) {
//Util.log("XML ", oXml);
if (!Util.empty(err))
return cbpropfindreq(err);
@@ -1845,7 +1850,7 @@ exports.NS_AJAXORG = "http://ajax.org/2005/aml";
//in order to make it parsable
var operation, innerProperties, propertyValue;
var self = this;
- Util.loadDOMDocument(body, function(err, dom) {
+ Util.loadDOMDocument(body, this.server.options.parser, function(err, dom) {
if (!Util.empty(err))
return cbproppatchreq(err);
var child, propertyName;
View
3 lib/DAV/plugins/codesearch.js
@@ -162,7 +162,8 @@ var prevFile;
child = dom.childNodes[i];
if (!child || child.nodeType != 1)
continue;
- options[child.tagName] = child.nodeValue;
+ if (child.lastChild !== null)
+ options[child.localName] = child.lastChild.nodeValue;
}
return options;
};
View
5 lib/DAV/plugins/filelist.js
@@ -92,7 +92,10 @@ jsDAV_Filelist_Plugin.FIND_CMD = GnuTools.FIND_CMD;
//Hidden Files
if (options.showHiddenFiles == "1")
args.push("!", "-regex", "\\/\\.[^\\/]*$");
-
+
+ if (options.maxdepth)
+ args.push("-maxdepth", options.maxdepth);
+
excludeExtensions.forEach(function(pattern){
args.push("!", "-regex", ".*\\/" + pattern + "$");
});
View
25 lib/DAV/server.js
@@ -37,6 +37,18 @@ exports.DEFAULT_TMPDIR = (function() {
})();
/**
+ * Auto-load bundled plugins.
+ */
+exports.DEFAULT_PLUGINS = {};
+var self = this;
+Fs.readdirSync(__dirname + "/plugins").forEach(function(filename){
+ if (/\.js$/.test(filename)) {
+ var name = filename.substr(0, filename.lastIndexOf('.'));
+ exports.DEFAULT_PLUGINS[name] = require("./plugins/" + name);
+ }
+});
+
+/**
* jsDAV version number
*/
exports.VERSION = JSON.parse(Fs.readFileSync(__dirname + "/../../package.json")).version;
@@ -58,6 +70,8 @@ function Server(options) {
if (typeof options.standalone == "undefined")
options.standalone = true;
+ this.plugins = Util.extend({}, exports.DEFAULT_PLUGINS);
+
if (options.plugins) {
if (!Array.isArray(options.plugins))
options.plugins = Object.keys(options.plugins);
@@ -232,17 +246,6 @@ function Server(options) {
// The fallback is that we're just going to assume the server root.
return "/";
};
-
- /**
- * Auto-load bundled plugins.
- */
- var self = this;
- Fs.readdirSync(__dirname + "/plugins").forEach(function(filename){
- if (/\.js$/.test(filename)) {
- var name = filename.substr(0, filename.lastIndexOf('.'));
- self.plugins[name] = require("./plugins/" + name);
- }
- });
}).call(Server.prototype = Http.Server.prototype);
exports.createServer = function(options, port, host) {
View
28 lib/DAV/util.js
@@ -9,7 +9,6 @@ var jsDAV = require("./../jsdav");
var Crypto = require("crypto");
var Async = require("asyncjs");
var Exc = require("./exceptions");
-var Xml = require("libxml/lib/libxml");
var Util = require("util");
var Fs = require("fs");
@@ -133,7 +132,7 @@ exports.rtrim = function(str, charlist) {
/**
* Splits a string into chunks like JS' String#split(), but with some additional
* options, like each item in the resulting array is stripped from any whitespace.
- *
+ *
* @param {String} s
* @param {String} seperator
* @param {Number} limit
@@ -419,7 +418,7 @@ exports.toClarkNotation = function(dom) {
// Mapping back to the real namespace, in case it was dav
var ns = dom.namespaceURI == "urn:DAV" ? "DAV:" : dom.namespaceURI;
// Mapping to clark notation
- return "{" + ns + "}" + dom.tagName;
+ return "{" + ns + "}" + dom.localName.toLowerCase();
};
/**
@@ -514,10 +513,11 @@ exports.escapeXml = function(str) {
* It does not preserve whitespace, and it converts the DAV: namespace to urn:DAV.
*
* @param string xml
+ * @param string which 'libxml' or 'xmldom'. Default: 'xmldom'
* @throws jsDAV_Exception_BadRequest
* @return DOMDocument
*/
-exports.loadDOMDocument = function(xml, callback) {
+exports.loadDOMDocument = function(xml, which, callback) {
if (!xml)
return callback(new Exc.jsDAV_Exception_BadRequest("Empty XML document sent"));
@@ -532,9 +532,14 @@ exports.loadDOMDocument = function(xml, callback) {
//xml = xml.replace(/<\?xml([^>]*)encoding="UTF-16"([^>]*)>/, "<?xml$1encoding=\"UTF-8\"$2>");
}
+ which = which || "xmldom";
var root;
+ var parser = which == "xmldom" ? require("xmldom").DOMParser : require("libxml/lib/libxml");
+ xml = exports.convertDAVNamespace(xml);
try {
- root = exports.xmlParseError(Xml.parseFromString(exports.convertDAVNamespace(xml)).documentElement);
+ root = which == "xmldom"
+ ? new parser().parseFromString(xml, "text/xml").documentElement
+ : exports.xmlParseError(parser.parseFromString(xml).documentElement);
}
catch (ex) {
return callback(new Exc.jsDAV_Exception_BadRequest(
@@ -584,11 +589,12 @@ exports.xmlParseError = function(xml){
*/
exports.parseProperties = function(parentNode, propertyMap) {
propertyMap = propertyMap || [];
- var propNode, propNodeData, propertyName, j, k, c,
- propList = {},
- childNodes = parentNode.childNodes,
- i = 0,
- l = childNodes.length;
+ var propNode, propNodeData, propertyName, j, k, c;
+ var propList = {};
+ var childNodes = parentNode.childNodes;
+ var i = 0;
+ var l = childNodes.length;
+
for (; i < l; ++i) {
propNode = childNodes[i];
@@ -1020,7 +1026,7 @@ var _slice = Array.prototype.slice;
listeners = this.$eventRegistry[prio][eventName];
if (!listeners)
listeners = this.$eventRegistry[prio][eventName] = [];
- if (allListeners.indexOf(listener) == -1)
+ if (allListeners.indexOf(listener) === -1)
listeners.push(listener);
};
View
5 package.json
@@ -9,6 +9,7 @@
"test": "node test/test_server.js"
},
"main": "lib/jsdav",
+ "pm": "sm",
"repository" : {
"type" : "git",
"url" : "http://github.com/ajaxorg/jsDAV.git"
@@ -17,9 +18,9 @@
"asyncjs": "0.0.7",
"formidable": "1.0.9",
"jsftp": "0.3.3",
- "libxml": "0.0.3",
"node-sftp": "0.1.1",
- "gnu-tools": "~0.0.1"
+ "xmldom": "0.1.2",
+ "gnu-tools": "0.0.x"
},
"licenses": [{
"type": "The MIT License",
View
10 lib/DAV/plugins/codesearch_test.js → test/test_codesearch.js
@@ -8,8 +8,8 @@
"use strict";
var assert = require("assert");
-var jsDAV = require("../../../lib/jsdav");
-var jsDAV_Codesearch_Plugin = require("./codesearch");
+var jsDAV = require("./../lib/jsdav");
+var jsDAV_Codesearch_Plugin = require("./../lib/DAV/plugins/codesearch");
module.exports = {
timeout: 30000,
@@ -25,14 +25,14 @@ module.exports = {
next();
},
- "test retrieving a file list": function(next) {
+ "test retrieving a string": function(next) {
var all = '';
this.plugin.doCodesearch({path: __dirname}, {query: "tearDown", pattern: "*.*", uri: "http://bla/"}, function(out){
all += out;
}, function(err, out) {
all += out;
- var matches = all.match(/<\/d:excerpt>/g);
- assert.equal(matches.length, 2, "count results");
+ assert.ok(all.indexOf("tearDown") > -1);
+
next();
});
}
View
11 test/test_filelist.js
@@ -30,20 +30,21 @@ module.exports = {
"test retrieving a file list": function(next) {
this.plugin.doFilelist({path: "./"}, {}, function(err, out){
- assert.ok(out.indexOf("test_filelist.js") > -1);
+ assert.ok(out.toString().indexOf("test_filelist.js") > -1);
next();
- });
+ }, function(err, code) { });
},
"test retrieving a file list including hidden files": function(next) {
this.plugin.doFilelist({path: "../"}, {
- showHiddenFiles: "1"
+ showHiddenFiles: "1",
+ maxdepth: 1
}, function(err, out){
- assert.ok(out.indexOf(".gitignore") > -1);
+ assert.ok(out.toString().indexOf("./.gitignore") > -1);
next();
- });
+ }, function(err, code) { });
}
};
View
116 test/test_multi_instance_plugin.js
@@ -0,0 +1,116 @@
+/*
+ * @package jsDAV
+ * @subpackage DAV
+ * @copyright Copyright(c) 2011 Ajax.org B.V. <info AT ajax.org>
+ * @author Mike de Boer <info AT mikedeboer DOT nl>
+ * @license http://github.com/mikedeboer/jsDAV/blob/master/LICENSE MIT License
+ */
+"use strict";
+
+var jsDAV = require("./../lib/jsdav");
+var jsDAV_ServerPlugin = require("jsdav/lib/DAV/plugin").jsDAV_ServerPlugin;
+var util = require("util");
+var events = require("events");
+var http = require("http");
+var fs = require("fs");
+
+jsDAV.debugMode = true;
+
+
+
+// Invoked at bottom of file.
+function main() {
+
+ createInstance({
+ mode: "slave",
+ port: 8001
+ });
+
+ createInstance({
+ mode: "master",
+ port: 8002
+ });
+
+ makeSlavePutRequest();
+}
+
+function makeSlavePutRequest() {
+ console.log("Make slave PUT request ...");
+ var req = http.request({
+ host: '127.0.0.1',
+ port: 8001, // slave
+ path: '/1.txt',
+ method: 'PUT'
+ }, function(res) {
+ var data = "";
+ res.on('data', function (chunk) {
+ data += chunk.toString();
+ });
+ res.on('end', function () {
+ console.log("... got status '" + res.statusCode + "' when PUTting to slave.");
+ });
+ });
+ req.write("1");
+ req.end();
+}
+
+
+function createInstance(options) {
+
+ var filewatch = new Filewatch({
+ mode: options.mode
+ });
+
+ filewatch.on("afterWrite", function(event) {
+ console.log("Got JS DAV afterWrite for " + options.mode, event);
+ });
+
+ var davOptions = {
+ node: __dirname + "/assets",
+ path: __dirname + "/assets",
+ mount: "/",
+ plugins: ["auth", /*"browser", */"codesearch", "filelist", "filesearch", "locks", "mount", "temporaryfilefilter"],
+ server: {},
+ standalone: false
+ };
+
+ var davServer = jsDAV.mount(davOptions);
+ davServer.mode = options.mode;
+ davServer.plugins["filewatch"] = filewatch.getPlugin();
+
+ http.createServer(function (req, res) {
+ davServer.exec(req, res);
+ }).listen(options.port);
+
+ return davServer;
+}
+
+
+var Filewatch = function(options) {
+ var self = this;
+
+ var plugin = function(handler) {
+
+ jsDAV_ServerPlugin.call(this, handler);
+
+ handler.addEventListener("afterWriteContent", function(e, uri) {
+console.log("afterWriteContent event for " + options.mode);
+ self.emit("afterWrite", {
+ file: "/" + uri
+ });
+ e.next();
+ });
+ }
+ util.inherits(plugin, jsDAV_ServerPlugin);
+
+ self.getPlugin = function() {
+console.log("PLUGIN INSTANCE FOR " + options.mode);
+ return plugin;
+ }
+};
+
+util.inherits(Filewatch, events.EventEmitter);
+
+
+main();
+

0 comments on commit a702fad

Please sign in to comment.