Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup #42

Merged
merged 5 commits into from
Mar 8, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion client/oc-client.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions components/oc-client/_package/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "oc-client",
"description": "",
"version": "0.11.0",
"version": "0.11.1",
"dependencies": {},
"repository": "",
"oc": {
Expand All @@ -21,6 +21,6 @@
"src": "server.js"
}
},
"version": "0.11.0"
"version": "0.11.1"
}
}
2 changes: 1 addition & 1 deletion components/oc-client/_package/src/oc-client.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion components/oc-client/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "oc-client",
"description": "",
"version": "0.11.0",
"version": "0.11.1",
"dependencies": {},
"repository": "",
"oc": {
Expand Down
2 changes: 1 addition & 1 deletion components/oc-client/src/oc-client.min.js

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions grunt-tasks/githooks.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

module.exports = {
all: {
'pre-commit': 'build'
Expand Down
8 changes: 4 additions & 4 deletions grunt-tasks/mochaTest.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
var task = {
'use strict';

module.exports = {
options: {
reporter: '<%= grunt.option("mocha") || "spec" %>'
},
Expand All @@ -8,6 +10,4 @@ var task = {
unit:{
src: ['test/unit/**/*.js']
}
};

module.exports = task;
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "oc",
"version": "0.11.0",
"version": "0.11.1",
"description": "An experimental framework to develop and distribute html components",
"main": "index.js",
"bin": {
Expand Down
1 change: 0 additions & 1 deletion registry/app-start.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ var colors = require('colors/safe');
var format = require('stringformat');
var packageInfo = require('../package.json');
var path = require('path');
var Repository = require('./domain/repository');
var _ = require('underscore');


Expand Down
25 changes: 25 additions & 0 deletions registry/domain/options-sanitiser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

var express = require('express');
var settings = require('../../resources/settings');
var _ = require('underscore');

module.exports = function(input){
var options = _.clone(input);

if(!options.publishAuth){
options.beforePublish = function(req, res, next){ next(); };
} else {
options.beforePublish = express.basicAuth(options.publishAuth.username, options.publishAuth.password);
}

if(!options.prefix){
options.prefix = '/';
}

if(!options.tempDir){
options.tempDir = settings.registry.defaultTempPath;
}

return options;
};
16 changes: 10 additions & 6 deletions registry/domain/s3.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,6 @@ module.exports = function(conf){
force = false;
}

var cached = cache.get('s3-file', filePath);

if(!!cached && !force){
return callback(null, cached);
}

var getFromAws = function(callback){
client.getObject({
Bucket: bucket,
Expand All @@ -82,6 +76,16 @@ module.exports = function(conf){
});
};

if(force){
return getFromAws(callback);
}

var cached = cache.get('s3-file', filePath);

if(!!cached){
return callback(null, cached);
}

getFromAws(function(err, result){
if(err){ return callback(err); }
cache.set('s3-file', filePath, result);
Expand Down
108 changes: 34 additions & 74 deletions registry/index.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
'use strict';

var appStart = require('./app-start');
var baseUrlHandler = require('./middleware/base-url-handler');
var colors = require('colors');
var cors = require('./middleware/cors');
var express = require('express');
var fileUploads = require('./middleware/file-uploads');
var format = require('stringformat');
var http = require('http');
var multer = require('multer');
var path = require('path');
var sanitiseOptions = require('./domain/options-sanitiser');
var Repository = require('./domain/repository');
var RequestInterceptor = require('./request-interceptor');
var routes = require('./routes');
var requestInterceptor = require('./middleware/request-interceptor');
var Router = require('./router');
var settings = require('../resources/settings');
var validator = require('./domain/validator');
var _ = require('underscore');

module.exports = function(options){

var hasPrefix = false,
self = this,
var self = this,
server,
withLogging = !_.has(options, 'verbosity') || options.verbosity > 0,
validationResult = validator.registryConfiguration(options),
baseUrlFunc,
repository;

options = sanitiseOptions(options);

if(!validationResult.isValid){
throw validationResult.message;
}
Expand All @@ -37,100 +39,58 @@ module.exports = function(options){
};

this.init = function(callback){
var app = express();

repository = new Repository(options);
routes.init(options, repository);

var app = express();

// middleware
app.set('port', process.env.PORT || options.port);
app.use(function (req, res, next) {
res.removeHeader('X-Powered-By');
res.header('Access-Control-Allow-Credentials', 'true');
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
res.header('Access-Control-Allow-Methods', 'GET, OPTIONS, PUT');

if(_.isFunction(options.baseUrl)){
baseUrlFunc = options.baseUrl;
}

if(!_.isUndefined(baseUrlFunc)){
options.baseUrl = baseUrlFunc({
host: req.headers.host,
secure: req.secure
});
}
app.set('json spaces', 0);

app.use(function(req, res, next){
res.conf = options;

if(!!options.dependencies){
res.injectedDependencies = options.dependencies;
}

next();
});

if(!options.local){
app.use(multer({
dest: options.tempDir || settings.registry.defaultTempPath,
fieldSize: 10,
rename: function(fieldname, filename){
return format('{0}-{1}.tar', filename.replace('.tar', '').replace(/\W+/g, '-').toLowerCase(), Date.now());
}
}));
}

if(!options.publishAuth){
options.beforePublish = function(req, res, next){ next(); };
} else {
options.beforePublish = express.basicAuth(options.publishAuth.username, options.publishAuth.password);
}

if(withLogging){
app.use(express.logger('dev'));
}

app.use(express.json());
app.use(express.urlencoded());
app.set('json spaces', 0);
app.use(cors);
app.use(fileUploads);
app.use(baseUrlHandler);
app.use(requestInterceptor);

if(!!options.onRequest){
var interceptor = new RequestInterceptor(options.onRequest);
app.use(interceptor);
if(withLogging){
app.use(express.logger('dev'));
}

app.use(app.router);

if('development' === app.get('env')){
app.use(express.errorHandler());
}

if(!options.prefix){
options.prefix = '/';
} else {
hasPrefix = true;
// routes
app.use(app.router);
var router = new Router(options, repository);

if(options.prefix !== '/'){
app.get('/', function(req, res){ res.redirect(options.prefix); });
app.get(options.prefix.substr(0, options.prefix.length - 1), router.listComponents);
}

app.get(options.prefix + 'oc-client/client.js', routes.staticRedirector);
app.get(options.prefix + 'oc-client/client.js', router.staticRedirector);

if(options.local){
app.get(format('{0}:componentName/:componentVersion/{1}*', options.prefix, settings.registry.localStaticRedirectorPath), routes.staticRedirector);
app.get(format('{0}:componentName/:componentVersion/{1}*', options.prefix, settings.registry.localStaticRedirectorPath), router.staticRedirector);
} else {
app.put(options.prefix + ':componentName/:componentVersion', options.beforePublish, routes.publish);
app.put(options.prefix + ':componentName/:componentVersion', options.beforePublish, router.publish);
}

app.get(options.prefix, routes.index);
app.get(options.prefix, router.listComponents);

if(hasPrefix){
app.get(options.prefix.substr(0, options.prefix.length - 1), routes.index);
}
app.get(format('{0}:componentName/:componentVersion{1}', options.prefix, settings.registry.componentInfoPath), router.componentInfo);
app.get(format('{0}:componentName{1}', options.prefix, settings.registry.componentInfoPath), router.componentInfo);

app.get(format('{0}{1}{2}', options.prefix, ':componentName/:componentVersion', settings.registry.componentInfoPath), routes.componentInfo);
app.get(format('{0}{1}{2}', options.prefix, ':componentName', settings.registry.componentInfoPath), routes.componentInfo);
app.get(options.prefix + ':componentName/:componentVersion', routes.component);
app.get(options.prefix + ':componentName', routes.component);
app.get(options.prefix + ':componentName/:componentVersion', router.component);
app.get(options.prefix + ':componentName', router.component);

if(!!options.routes){
_.forEach(options.routes, function(route){
Expand Down
17 changes: 17 additions & 0 deletions registry/middleware/base-url-handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

var _ = require('underscore');

module.exports = function(req, res, next){

var baseUrlFunc = _.isFunction(res.conf.baseUrl) ? res.conf.baseUrl : undefined;

if(!_.isUndefined(baseUrlFunc)){
res.conf.baseUrl = baseUrlFunc({
host: req.headers.host,
secure: req.secure
});
}

next();
};
10 changes: 10 additions & 0 deletions registry/middleware/cors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

module.exports = function(req, res, next){
res.removeHeader('X-Powered-By');
res.header('Access-Control-Allow-Credentials', 'true');
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
res.header('Access-Control-Allow-Methods', 'GET, OPTIONS, PUT');
next();
};
19 changes: 19 additions & 0 deletions registry/middleware/file-uploads.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

var format = require('stringformat');
var multer = require('multer');

module.exports = function(req, res, next){

if(res.conf.local){
return next();
}

return multer({
dest: res.conf.tempDir,
fieldSize: 10,
rename: function(fieldname, filename){
return format('{0}-{1}.tar', filename.replace('.tar', '').replace(/\W+/g, '-').toLowerCase(), Date.now());
}
})(req, res, next);
};
40 changes: 40 additions & 0 deletions registry/middleware/request-interceptor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict';

var now = require('performance-now');
var querystring = require('querystring');
var _ = require('underscore');

module.exports = function(req, res, next){
var handler = res.conf.onRequest;

if(!handler || !_.isFunction(handler)){
return next();
}

req.startTime = now();
var impl = res.end;

res.send = function(chunk, encoding){
res.send = impl;
res.send(chunk, encoding);

var data = {
duration: parseFloat((now() - req.startTime).toFixed(3)) * 1000,
headers: req.headers,
method: req.method,
path: req.path,
relativeUrl: req.originalUrl,
query: req.query,
url: req.protocol + '://' + req.get('host') + req.originalUrl,
statusCode: res.statusCode
};

if(!!res.errorDetails){
data.errorDetails = res.errorDetails;
}

handler(data);
};

next();
};
Loading