Skip to content
This repository has been archived by the owner on Mar 7, 2021. It is now read-only.

Commit

Permalink
Cleanup whitespace.
Browse files Browse the repository at this point in the history
  • Loading branch information
nisaacson committed Oct 22, 2013
1 parent cac521f commit 39dc343
Show file tree
Hide file tree
Showing 19 changed files with 328 additions and 303 deletions.
14 changes: 14 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# EditorConfig is awesome: http://EditorConfig.org

# top-most EditorConfig file
root = false

# Unix-style newlines with a newline ending every file
[*]
end_of_line = lf
insert_final_newline = true

# Tab indentation (no size specified)
[*.js]
indent_style = tab
indent_size = 2
11 changes: 11 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"asi": false,
"node": true,
"require": true,
"process": true,
"module": true,
"setInterval": true,
"setTimeout": true,
"clearTimeout": true,
"Buffer": true
}
48 changes: 24 additions & 24 deletions lib/cache-backend-fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,53 +28,53 @@ var FSBackend = function FSBackend(loadParameter) {
function sanitisePath(path,queueObject) {
// Remove first slash (as we set one later.)
path = path.replace(/^\//,"");

var pathStack = [];

// Trim whitespace. If no path is present - assume index.html.
var sanitisedPath = path.length ? path.replace(/\s*$/ig,"") : "index.html";
var headers = queueObject.stateData.headers, sanitisedPathParts;

if (sanitisedPath.match(/\?/)) {
sanitisedPathParts = sanitisedPath.split(/\?/g);
var resource = sanitisedPathParts.shift();
var hashedQS = crypto.createHash("sha1").update(sanitisedPathParts.join("?")).digest("hex");
sanitisedPath = resource + "?" + hashedQS;
}

pathStack = sanitisedPath.split(/\//g);
pathStack = pathStack.map(function(pathChunk,count) {
if (pathChunk.length >= 250) {
return crypto.createHash("sha1").update(pathChunk).digest("hex");
}

return pathChunk;
});

sanitisedPath = pathStack.join("/");

// Try to get a file extension for the file - for ease of identification
// We run through this if we either:
// 1) haven't got a file extension at all, or:
// 2) have an HTML file without an HTML file extension (might be .php, .aspx, .do, or some other server-processed type)

if (!sanitisedPath.match(/\.[a-z0-9]{1,6}$/i) || (headers["content-type"] && headers["content-type"].match(/text\/html/i) && !sanitisedPath.match(/\.htm[l]?$/i))) {
var subMimeType = "";
var mimeParts = [];

if (headers["content-type"] && headers["content-type"].match(/text\/html/i)) {
if (sanitisedPath.match(/\/$/)) {
sanitisedPath += "index.html";
} else {
sanitisedPath += ".html";
}

} else if (headers["content-type"] && (mimeParts = headers["content-type"].match(/(image|video|audio|application)\/([a-z0-9]+)/i))) {
subMimeType = mimeParts[2];
sanitisedPath += "." + subMimeType;
}
}

return sanitisedPath;
}

Expand All @@ -101,7 +101,7 @@ FSBackend.prototype.isDirectory = function(location) {

FSBackend.prototype.load = function() {
var backend = this;

if (!this.fileExists(this.location) && this.isDirectory(this.location)) {
throw new Error("Unable to verify cache location exists.");
}
Expand Down Expand Up @@ -134,11 +134,11 @@ FSBackend.prototype.saveCache = function(callback) {

FSBackend.prototype.setItem = function(queueObject,data,callback) {
callback = callback instanceof Function ? callback : function(){};

var backend = this;
var pathStack = [queueObject.protocol, queueObject.domain, queueObject.port];
pathStack = pathStack.concat(sanitisePath(queueObject.path,queueObject).split(/\/+/g));

var cacheItemExists = false;
var firstInstanceIndex = NaN;
if (this.index.reduce(function(prev,current,index,array) {
Expand All @@ -147,27 +147,27 @@ FSBackend.prototype.setItem = function(queueObject,data,callback) {
},false)) {
cacheItemExists = true;
}

var writeFileData = function(currentPath,data) {
fs.writeFile(currentPath,data,function(error) {
if (error) throw error;
fs.writeFile(currentPath + ".cacheData.json",JSON.stringify(queueObject),function(error) {
if (error) throw error;

var cacheObject = {
url: queueObject.url,
etag: queueObject.stateData.headers.etag,
lastModified: queueObject.stateData.headers['last-modified'],
dataFile: currentPath,
metaFile: currentPath + ".cacheData.json"
};

if (cacheItemExists) {
backend.index[firstInstanceIndex] = cacheObject;
} else {
backend.index.push(cacheObject);
}

callback(cacheObject);
});
});
Expand Down Expand Up @@ -199,10 +199,10 @@ FSBackend.prototype.getItem = function(queueObject,callback) {
var cacheItemResult = this.index.filter(function(item) {
return item.url === queueObject.url;
});

if (cacheItemResult.length) {
var cacheItem = cacheItemResult.shift();

callback({
"url": cacheItem.url,
"etag": cacheItem.etag,
Expand All @@ -213,7 +213,7 @@ FSBackend.prototype.getItem = function(queueObject,callback) {
callback(error);
return false;
}

callback(null,data);
});
},
Expand All @@ -223,16 +223,16 @@ FSBackend.prototype.getItem = function(queueObject,callback) {
callback(error);
return false;
}

callback(null,JSON.parse(data.toString("utf8")));
});
}
});

} else {
callback(null);
}

return false;
};

8 changes: 4 additions & 4 deletions lib/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ var FilesystemBackend = require("./cache-backend-fs.js");

// Init cache wrapper for backend...
var Cache = function Cache(cacheLoadParameter,cacheBackend) {

// Ensure parameters are how we want them...
cacheBackend = typeof cacheBackend === "object" ? cacheBackend : FilesystemBackend;
cacheLoadParameter = cacheLoadParameter instanceof Array ? cacheLoadParameter : [cacheLoadParameter];

// Now we can just run the factory.
this.datastore = cacheBackend.apply(cacheBackend,cacheLoadParameter);

// Instruct the backend to load up.
this.datastore.load();
};
Expand All @@ -41,4 +41,4 @@ Cache.prototype.saveCache = function() {

module.exports = Cache;
module.exports.Cache = Cache;
module.exports.FilesystemBackend = FilesystemBackend;
module.exports.FilesystemBackend = FilesystemBackend;
2 changes: 1 addition & 1 deletion lib/cli.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// CLI module for crawling.
// Not yet built.
// Not yet built.

0 comments on commit 39dc343

Please sign in to comment.