Skip to content

Commit

Permalink
Reformatted code and linted files
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamie Hall committed May 12, 2015
1 parent 6d15240 commit 033ce83
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 109 deletions.
6 changes: 3 additions & 3 deletions bin/s3rver.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/usr/bin/env node
'use strict';
var pkg = require('../package.json'),
var pkg = require('../package.json'),
version = pkg.version,
program = require('commander'),
fs = require('fs'),
S3rver = require('../lib');
fs = require('fs'),
S3rver = require('../lib');

program.version(version, '--version');
program.option('-h, --hostname [value]', 'Set the host name or ip for the server', 'localhost')
Expand Down
24 changes: 12 additions & 12 deletions lib/app.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
'use strict';
var app = function (hostname, port, directory, silent, indexDocument, errorDocument) {
var express = require('express'),
app = express(),
logger = require('./logger')(silent),
var express = require('express'),
app = express(),
logger = require('./logger')(silent),
Controllers = require('./controllers'),
controllers = new Controllers(directory, logger, indexDocument, errorDocument),
concat = require('concat-stream'),
path = require('path');
concat = require('concat-stream'),
path = require('path');

/**
* Log all requests
*/
app.use(require('morgan')('tiny', { 'stream': {
write: function (message) {
logger.info(message.slice(0, -1));
app.use(require('morgan')('tiny', {
'stream': {
write: function (message) {
logger.info(message.slice(0, -1));
}
}
}
}));

app.use(function (req, res, next) {
Expand All @@ -25,11 +26,10 @@ var app = function (hostname, port, directory, silent, indexDocument, errorDocum
}));
});

app.use(function(req, res, next) {
app.use(function (req, res, next) {
var host = req.headers.host.split(':')[0];

if (indexDocument && host !== 'localhost' && host !== '127.0.0.1')
{
if (indexDocument && host !== 'localhost' && host !== '127.0.0.1') {
req.url = path.join('/', host, req.url);
}

Expand Down
123 changes: 54 additions & 69 deletions lib/controllers.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

var FileStore = require('./file-store'),
templateBuilder = require('./xml-template-builder'),
path = require('path');
var FileStore = require('./file-store'),
templateBuilder = require('./xml-template-builder'),
path = require('path');

module.exports = function (rootDirectory, logger, indexDocument, errorDocument) {
var fileStore = new FileStore(rootDirectory);
Expand All @@ -14,64 +14,57 @@ module.exports = function (rootDirectory, logger, indexDocument, errorDocument)
};

var buildResponse = function (req, res, status, object, data) {
res.header('Etag', object.md5);
res.header('Last-Modified', new Date(object.modifiedDate).toUTCString());
res.header('Content-Type', object.contentType);
res.header('Content-Length', object.size);
if (object.customMetaData.length > 0) {
object.customMetaData.forEach(function (metaData) {
res.header(metaData.key, metaData.value);
});
}
res.status(status);
if (req.method === 'HEAD') {
return res.end();
}
return res.end(data);
res.header('Etag', object.md5);
res.header('Last-Modified', new Date(object.modifiedDate).toUTCString());
res.header('Content-Type', object.contentType);
res.header('Content-Length', object.size);
if (object.customMetaData.length > 0) {
object.customMetaData.forEach(function (metaData) {
res.header(metaData.key, metaData.value);
});
}
res.status(status);
if (req.method === 'HEAD') {
return res.end();
}
return res.end(data);
};

var errorResponse = function(req, res, keyName) {
var errorResponse = function (req, res, keyName) {
logger.error('Object "%s" in bucket "%s" does not exist', keyName, req.bucket.name);

if (indexDocument)
{
if (errorDocument)
{

if (indexDocument) {
if (errorDocument) {
fileStore.getObject(req.bucket, errorDocument, function (err, object, data) {
if (err)
{
if (err) {
console.error('Custom Error Document not found: ' + errorDocument);
return notFoundResponse(req, res);
}
else
{
else {
return buildResponse(req, res, 404, object, data);
}
});
}
else
{
else {
return notFoundResponse(req, res);
}
}
else
{
else {
var template = templateBuilder.buildKeyNotFound(keyName);
return buildXmlResponse(res, 404, template);
}
};


var notFoundResponse = function(req, res)
{
var ErrorDoc = '<!DOCTYPE html>\n<html><head><title>404 - Resource Not Found</title></head><body><h1>404 - Resource Not Found</h1></body></html>';
var notFoundResponse = function (req, res) {
var ErrorDoc = '<!DOCTYPE html>\n<html><head><title>404 - Resource Not Found</title></head><body><h1>404 - Resource Not Found</h1></body></html>';

return buildResponse(req, res, 404, {
modifiedDate: new Date(),
contentType: 'text/html',
customMetaData: [],
size: ErrorDoc.length
}, ErrorDoc);
return buildResponse(req, res, 404, {
modifiedDate: new Date(),
contentType: 'text/html',
customMetaData: [],
size: ErrorDoc.length
}, ErrorDoc);
};

/**
Expand Down Expand Up @@ -108,29 +101,25 @@ module.exports = function (rootDirectory, logger, indexDocument, errorDocument)
maxKeys: parseInt(req.query['max-keys']) || 1000,
delimiter: req.query.delimiter || null
};

if (indexDocument)
{
fileStore.getObject(req.bucket, indexDocument, function (err, object, data) {
if (err)
{
return errorResponse(req, res, indexDocument);
}
else
{
logger.info('Serving Page: %s', object.key);
return buildResponse(req, res, 200, object, data);
}
});

if (indexDocument) {
fileStore.getObject(req.bucket, indexDocument, function (err, object, data) {
if (err) {
return errorResponse(req, res, indexDocument);
}
else {
logger.info('Serving Page: %s', object.key);
return buildResponse(req, res, 200, object, data);
}
});
}
else
{
else {
logger.info('Fetched bucket "%s" with options %s', req.bucket.name, options);
fileStore.getObjects(req.bucket, options, function (err, results) {
logger.info('Found %d objects for bucket "%s"', results.length, req.bucket.name);
logger.info('Found %d objects for bucket "%s"', results.length, req.bucket.name);

var template = templateBuilder.buildBucketQuery(options, results);
return buildXmlResponse(res, 200, template);
var template = templateBuilder.buildBucketQuery(options, results);
return buildXmlResponse(res, 200, template);
});
}
},
Expand Down Expand Up @@ -192,22 +181,18 @@ module.exports = function (rootDirectory, logger, indexDocument, errorDocument)
fileStore.getObject(req.bucket, keyName, function (err, object, data) {
if (err) {

if (indexDocument)
{
if (indexDocument) {
keyName = path.join(keyName, indexDocument);
return fileStore.getObject(req.bucket, keyName, function (err, object, data) {
if (err)
{
if (err) {
return errorResponse(req, res, keyName);
}
else
{
else {
return buildResponse(req, res, 200, object, data);
}
});
}
else
{
else {
return errorResponse(req, res, keyName);
}
}
Expand All @@ -233,8 +218,8 @@ module.exports = function (rootDirectory, logger, indexDocument, errorDocument)
var copy = req.headers['x-amz-copy-source'];
if (copy) {
var srcObjectParams = copy.split('/'),
srcBucket = srcObjectParams[1],
srcObject = srcObjectParams.slice(2).join('/');
srcBucket = srcObjectParams[1],
srcObject = srcObjectParams.slice(2).join('/');
fileStore.getBucket(srcBucket, function (err, bucket) {
if (err) {
logger.error('No bucket found for "%s"', srcBucket);
Expand Down
22 changes: 11 additions & 11 deletions lib/file-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ var path = require('path'),
_ = require('lodash');

var FileStore = function (rootDirectory) {
var CONTENT_FILE = '.dummys3_content',
var CONTENT_FILE = '.dummys3_content',
METADATA_FILE = '.dummys3_metadata',
Bucket = require('./models/bucket'),
S3Object = require('./models/s3-object');
Bucket = require('./models/bucket'),
S3Object = require('./models/s3-object');

var getBucketPath = function (bucketName) {
return path.join(rootDirectory, bucketName);
Expand Down Expand Up @@ -161,9 +161,9 @@ var FileStore = function (rootDirectory) {

var createMetaData = function (data, done) {
var contentFile = data.contentFile,
type = data.type,
metaFile = data.metaFile,
headers = data.headers;
type = data.type,
metaFile = data.metaFile,
headers = data.headers;
async.parallel([
function (callback) {
fs.stat(contentFile, function (err, stats) {
Expand Down Expand Up @@ -230,12 +230,12 @@ var FileStore = function (rootDirectory) {
};

var copyObject = function (srcBucket, srcKey, destBucket, destKey, done) {
var srcKeyPath = path.resolve(getBucketPath(srcBucket.name), srcKey),
destKeyPath = path.resolve(getBucketPath(destBucket.name), destKey),
srcMetadataFilePath = path.join(srcKeyPath, METADATA_FILE),
srcContentFilePath = path.join(srcKeyPath, CONTENT_FILE),
var srcKeyPath = path.resolve(getBucketPath(srcBucket.name), srcKey),
destKeyPath = path.resolve(getBucketPath(destBucket.name), destKey),
srcMetadataFilePath = path.join(srcKeyPath, METADATA_FILE),
srcContentFilePath = path.join(srcKeyPath, CONTENT_FILE),
destMetadataFilePath = path.join(destKeyPath, METADATA_FILE),
destContentFilePath = path.join(destKeyPath, CONTENT_FILE);
destContentFilePath = path.join(destKeyPath, CONTENT_FILE);

mkdirp.sync(destKeyPath);
fs.copySync(srcMetadataFilePath, destMetadataFilePath);
Expand Down
21 changes: 7 additions & 14 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,6 @@ describe('S3rver Tests with Static Web Hosting', function () {
});



it('should create a site bucket', function (done) {
s3Client.createBucket({Bucket: 'site'}, function (err) {
if (err) {
Expand Down Expand Up @@ -634,17 +633,15 @@ describe('S3rver Tests with Static Web Hosting', function () {

it('should get an index page at / path', function (done) {
request('http://localhost:5694/site/', function (error, response, body) {
if (error)
{
if (error) {
return done(error);
}

if (response.statusCode !== 200) {
return done(new Error('Invalid status: ' + response.statusCode));
}

if (body !== '<html><body>Hello</body></html>')
{
if (body !== '<html><body>Hello</body></html>') {
return done(new Error('Invalid Content: ' + body));
}

Expand All @@ -655,17 +652,15 @@ describe('S3rver Tests with Static Web Hosting', function () {

it('should get an index page at /page/ path', function (done) {
request('http://localhost:5694/site/page/', function (error, response, body) {
if (error)
{
if (error) {
return done(error);
}

if (response.statusCode !== 200) {
return done(new Error('Invalid status: ' + response.statusCode));
}

if (body !== '<html><body>Hello</body></html>')
{
if (body !== '<html><body>Hello</body></html>') {
return done(new Error('Invalid Content: ' + body));
}

Expand All @@ -675,18 +670,16 @@ describe('S3rver Tests with Static Web Hosting', function () {


it('should get a 404 error page', function (done) {
request('http://localhost:5694/site/page/not-exists', function (error, response, body) {
if (error)
{
request('http://localhost:5694/site/page/not-exists', function (error, response) {
if (error) {
return done(error);
}

if (response.statusCode !== 404) {
return done(new Error('Invalid status: ' + response.statusCode));
}

if (response.headers['content-type'] !== 'text/html; charset=utf-8')
{
if (response.headers['content-type'] !== 'text/html; charset=utf-8') {
return done(new Error('Invalid ContentType: ' + response.headers['content-type']));
}

Expand Down

0 comments on commit 033ce83

Please sign in to comment.