Skip to content

Commit

Permalink
Serve injected YUI library using sendfiles.
Browse files Browse the repository at this point in the history
  • Loading branch information
reid committed Jul 16, 2010
1 parent 8e2544b commit 184e699
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 6 deletions.
1 change: 1 addition & 0 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ function main (config) {
}

main({
port : parseInt(process.env.PORT) || 8000,
files : process.argv.slice(1),
cwd : process.cwd()
});
16 changes: 16 additions & 0 deletions inc/inject.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
(function attachEventsToYUITest () {

if (!window.YUITest) return window.setTimeout(attachEventsToYUITest, 15);

var Runner = window.YUITest.TestRunner;

Runner.on(Runner.COMPLETE_EVENT, function (data) {
if (!window.YUITest.CLI) return;

YUI().use("test", function (Y) {
var reporter = new Y.Test.Reporter(window.YUITest.CLI.url);
reporter.report(data);
});
});

})();
45 changes: 39 additions & 6 deletions lib/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,61 @@ var sys = require("sys");
var assert = require("assert");
var querystring = require("querystring");
var http = require("http");
var EventEmitter = require("events").EventEmitter;
var sendfiles = require("./sendfiles").sendfiles;

exports.boot = function (config) {

setup(config.path);
serveExpress(
config.path,
config.port
);

run(parseInt(process.env.PORT || 8000), null);
return;
openURL(
config.files
);

console.log("Served: " + config.path);

};

function setup (path) {
function serveExpress (path, port) {

configure(function() {
sys.debug(path);

set("root", __dirname);
use(Static, { path: path });

use(Static, { path: path, bufferSize : 0 });

enable("show exceptions");
});

get("/", function() {
this.render("index.html.haml");
});

post("/results", function () {
console.log("got results");
this.respond(200, "ok");
});

get('/project/*', function(file){
var file = path + "/" + file;

if (/^.+\/build\/yui\/yui.*\.js$/.test(this.url.href)) {
// inject a test reporter into YUI itself
var url = "http://localhost:" + port + "/results";
sendfiles.call(
this,
[file, require("path").normalize(__dirname + "/../inc/inject.js")],
"window.YUITest.CLI = { url : \"" + url + "\" };"
);
} else {
// everything else goes untouched
this.sendfile(file);
}
});

run(port, null);

}
49 changes: 49 additions & 0 deletions lib/sendfiles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
var fs = require("fs");

// never-caching multi-file version of sendfile
exports.sendfiles = function (files, appendString, callback) {
var self = this;
var contentLength = 0;
var filesRead = 0;
var contentStore = [];
var io = new (require("events").EventEmitter);

if (
appendString && "string" !== typeof appendString
) return Error.raise("TypeError", "appendString must be a string");

io.addListener("end", function (file) {
if (appendString) {
contentLength += appendString.length;
contentStore.push(appendString);
}
contentLength += contentStore.length - 1;
self.header('Content-Length', contentLength);
// send the last file's content type.
self.contentType(file);
self.respond(200, contentStore.join("\n"));
});

// the order of files is important
for (
var idx = 0, len = files.length; idx < len; idx++
) (function (idx) { // closure on idx
var file = files[idx];
fs.stat(file, function (err, stat) {
if (err)
return "errno" in err && err.errno === 2
? self.notFound()
: self.error(err, callback)
fs.readFile(file, function (err, content) {
if (err) return self.error(err, callback)
contentStore[idx] = content;
contentLength += stat.size;
if (
++filesRead == files.length
) io.emit("end", file);
})
})
})(idx);

return this;
}

0 comments on commit 184e699

Please sign in to comment.