Skip to content

Commit

Permalink
Source formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathantneal committed Dec 28, 2012
1 parent c79dbde commit 812beb9
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 48 deletions.
10 changes: 5 additions & 5 deletions directory.jshtml
Expand Up @@ -4,14 +4,14 @@
function pad2(number) {
var zero = 3 - number.toString().length;

return Array( +(zero > 0 && zero)).join('0') + number;
return Array( +(zero > 0 && zero)).join("0") + number;
}

function bytesToSize(bytes, precision) {
var
sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'], posttxt = 0;
sizes = ["Bytes", "KB", "MB", "GB", "TB"], posttxt = 0;
if (bytes == 0) {
return 'n/a';
return "n/a";
}

while(bytes >= 1024) {
Expand All @@ -20,7 +20,7 @@ function bytesToSize(bytes, precision) {
bytes = bytes / 1024;
}

return bytes.toFixed(precision) + ' ' + sizes[posttxt];
return bytes.toFixed(precision) + " " + sizes[posttxt];
}

%>
Expand Down Expand Up @@ -84,7 +84,7 @@ for (index in files) {
stat = require.fs.statSync(require.config.site_base + path + file);
mtime = stat.mtime;

stat.mtime = mtime.getFullYear() + '-' + pad2(mtime.getMonth()) + '-' + pad2(mtime.getDate()) + ' ' + pad2(mtime.getHours()) + ':' + pad2(mtime.getMinutes());
stat.mtime = mtime.getFullYear() + "-" + pad2(mtime.getMonth()) + "-" + pad2(mtime.getDate()) + " " + pad2(mtime.getHours()) + ":" + pad2(mtime.getMinutes());

stat.name = file;

Expand Down
78 changes: 39 additions & 39 deletions server.js
@@ -1,17 +1,17 @@
var
GLOBAL = this,
REQUIRE = GLOBAL.require = {
config: require('./server.config.js'),
mime: require('./server.mimetypes.js'),
http: require('http'),
path: require('path'),
fs: require('fs'),
crypto: require('crypto'),
jshtml: require('./jshtml.js')
config: require("./server.config.js"),
mime: require("./server.mimetypes.js"),
http: require("http"),
path: require("path"),
fs: require("fs"),
crypto: require("crypto"),
jshtml: require("./jshtml.js")
};

process.on('uncaughtException', function (err) {
console.log('Caught exception: ' + err);
process.on("uncaughtException", function (err) {
console.log("Caught exception: " + err);
});


Expand All @@ -20,13 +20,13 @@ process.on('uncaughtException', function (err) {

function ResponseObject(metadata) {
this.data = metadata.data || false;
this.path = metadata.path || '';
this.path = metadata.path || "";
this.status = metadata.status || 200;
this.type = metadata.type || false;
}

ResponseObject.prototype.getEtag = function () {
return REQUIRE.crypto.createHash('md5').update(this.data).digest('hex');
return REQUIRE.crypto.createHash("md5").update(this.data).digest("hex");
};


Expand All @@ -43,7 +43,7 @@ function handleRequest(path, callback) {

if (isDirectory && directoryListing) {
// Permitted directory
path = path.replace(/\/$/, '') + '/';
path = path.replace(/\/$/, "") + "/";

for (index in indexes) {
if (REQUIRE.fs.existsSync(REQUIRE.config.site_base + path + indexes[index])) {
Expand All @@ -55,17 +55,17 @@ function handleRequest(path, callback) {
} else if (isDirectory) {
// Forbidden directory
callback(new ResponseObject({
'path': path,
'status': 403
"path": path,
"status": 403
}));
} else if (isPath) {
// Permitted file
getFileResponse(path, callback)
} else {
// Missing file
return callback(new ResponseObject({
'path': path,
'status': 404
"path": path,
"status": 404
}));
}
}
Expand All @@ -81,24 +81,24 @@ function getFileResponse(path, callback) {
if (error) {
// Internal error
callback(new ResponseObject({
'data': error.stack,
'path': path,
'status': 500
"data": error.stack,
"path": path,
"status": 500
}));
} else {
// Success
GLOBAL.path = path;

if (REQUIRE.path.extname(path) == '.jshtml') {
if (REQUIRE.path.extname(path) == ".jshtml") {
tpl = new REQUIRE.jshtml.JSHTML();

data = tpl.template(data.toString()).context(GLOBAL).render();
}

callback(new ResponseObject({
'data': new Buffer(data),
'path': path,
'type': REQUIRE.mime.getType(path)
"data": new Buffer(data),
"path": path,
"type": REQUIRE.mime.getType(path)
}));
}
});
Expand All @@ -115,9 +115,9 @@ function getDirectoryResponse(path, callback) {
if (error) {
// Internal error
callback(new ResponseObject({
'data': error.stack,
'path': path,
'status': 500
"data": error.stack,
"path": path,
"status": 500
}));
} else {
// Success
Expand All @@ -128,22 +128,22 @@ function getDirectoryResponse(path, callback) {
if (error) {
// Internal error
callback(new ResponseObject({
'data': error.stack,
'path': path,
'status': 500
"data": error.stack,
"path": path,
"status": 500
}));
} else {
// Success
if (REQUIRE.path.extname(REQUIRE.config.directory_template) == '.jshtml') {
if (REQUIRE.path.extname(REQUIRE.config.directory_template) == ".jshtml") {
tpl = new REQUIRE.jshtml.JSHTML();

data = tpl.template(data.toString()).context(GLOBAL).render();
}

callback(new ResponseObject({
'data': new Buffer(data),
'path': path,
'type': REQUIRE.mime.getType(REQUIRE.config.site_base + path)
"data": new Buffer(data),
"path": path,
"type": REQUIRE.mime.getType(REQUIRE.config.site_base + path)
}));
}
});
Expand All @@ -166,16 +166,16 @@ REQUIRE.http.createServer(function (request, response) {
if (response_object.data && response_object.data.length > 0) {
etag = response_object.getEtag();

if (request.headers.hasOwnProperty('if-none-match') && request.headers['if-none-match'] === etag) {
if (request.headers.hasOwnProperty("if-none-match") && request.headers["if-none-match"] === etag) {
// Not Modified
response.writeHead(304);
response.end();
} else {
headers = {
'Content-Type': response_object.type,
'Content-Length' : response_object.data.length,
'Cache-Control' : 'max-age=' + (REQUIRE.config.file_expiry_time * 60).toString(),
'ETag' : etag
"Content-Type": response_object.type,
"Content-Length" : response_object.data.length,
"Cache-Control" : "max-age=" + (REQUIRE.config.file_expiry_time * 60).toString(),
"ETag" : etag
};

response.writeHead(response_object.status, headers);
Expand All @@ -188,4 +188,4 @@ REQUIRE.http.createServer(function (request, response) {
});
}).listen(REQUIRE.config.port, REQUIRE.config.host);

console.log('Server started: http://' + REQUIRE.config.host + ':' + REQUIRE.config.port);
console.log("Server started: http://" + REQUIRE.config.host + ":" + REQUIRE.config.port);
8 changes: 4 additions & 4 deletions server.mimetypes.js
Expand Up @@ -44,15 +44,15 @@ function Main() {
}
};

this.defaultType = 'text/html';
this.defaultType = "text/html";

this.getType = function (path) {
var ext = path.replace(/^[\W\w]*\./, ''), type, subtype;
var ext = path.replace(/^[\W\w]*\./, ""), type, subtype;

for (type in this.types) {
for (subtype in this.types[type]) {
if ((' ' + this.types[type][subtype] + ' ').indexOf(ext) > -1) {
return type + '/' + subtype;
if ((" " + this.types[type][subtype] + " ").indexOf(ext) > -1) {
return type + "/" + subtype;
}
}
}
Expand Down

0 comments on commit 812beb9

Please sign in to comment.