Skip to content
This repository has been archived by the owner on Oct 3, 2023. It is now read-only.

Commit

Permalink
ES6 rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
Evan Williams authored and webpro committed Feb 5, 2017
1 parent 0fe8be0 commit ee2f13a
Show file tree
Hide file tree
Showing 16 changed files with 193 additions and 264 deletions.
22 changes: 22 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"env": {
"es6": true,
"node": true
},
"extends": "eslint:recommended",
"rules": {
"no-console": "off",
"indent": [
"error",
4
],
"quotes": [
"error",
"single"
],
"semi": [
"error",
"always"
]
}
}
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
_site
node_modules
test/delete
test/get
test/post
test/put
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
REPORTER = spec

test:
@NODE_ENV=test ./node_modules/.bin/mocha --require should --reporter $(REPORTER)
@cross-env NODE_ENV=test ./node_modules/.bin/mocha --require should --reporter spec

testw:
@NODE_ENV=test ./node_modules/.bin/mocha --require should --reporter min --watch
@cross-env NODE_ENV=test ./node_modules/.bin/mocha --require should --reporter min --watch

.PHONY: test
2 changes: 1 addition & 1 deletion bin/dyson.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env node

var cli = require('../lib/cli');
const cli = require('../lib/cli');

cli.execute(process.argv.slice(2));
84 changes: 31 additions & 53 deletions lib/cli.js
Original file line number Diff line number Diff line change
@@ -1,84 +1,62 @@
var dyson = require('./dyson'),
const dyson = require('./dyson'),
pkg = require('../package.json'),
_ = require('lodash'),
ncp = require('ncp').ncp,
fs = require('fs'),
path = require('path');

var execute = function(options) {
const execute = (options) => {

if(options.length) {

var arg1 = options[0];
var arg2 = options[1];

if(arg1 === 'init') {

console.log('WARNING: "dyson init" is deprecated and will be removed in the next major release.');

var source = path.resolve(__dirname + '/../dummy'),
if (options.length) {
const [arg1, arg2] = options;
if (arg1 === 'init') {
console.warn('WARNING: "dyson init" is deprecated and will be removed in the next major release.');
const source = path.resolve(`${__dirname}/../dummy`),
destination = path.resolve(arg2 || '.');

ncp(source, destination, function(error) {
if(error) {
ncp(source, destination, (error) => {
if (error) {
return console.error(error, source, destination);
}
console.log('Copied dummy files to', destination);
console.info(`Copied dummy files to ${destination}`);
});

} else {

var opts = _.defaults(readOptions(arg1), {
const opts = _.defaults(readOptions(arg1), {
port: arg2 || 3000,
configDir: arg1,
proxy: false,
multiRequest: ','
});

opts.configDir = path.resolve(opts.configDir);

fs.stat(opts.configDir, function(error, stats) {

if(!error && stats.isDirectory()) {

fs.stat(opts.configDir, (error, stats) => {
if (!error && stats.isDirectory()) {
dyson.bootstrap(opts);

console.log('Dyson listening at port', opts.port);

console.info(`Dyson listening at port: ${opts.port}`);
} else {

console.error('Directory does not exist:', opts.configDir);

console.error(`Directory does not exist: ${opts.configDir}`);
}
});

}

} else {

var version = pkg.version;

console.log();
console.log('dyson v' + version);
console.log();
console.log('Usage:');
console.log();
console.log('dyson init <dir>');
console.log('dyson <dir> [port]');
console.log();

const version = pkg.version;
console.info(`dyson version: ${version}
Usage:
dyson init <dir>
dyson <dir> [port]
`);
}

};

var readOptions = function(configDir) {

var source = path.resolve(configDir, 'dyson.json'),
config = {};
const readOptions = (configDir) => {
const source = path.resolve(configDir, 'dyson.json');
let config = {};

if(fs.existsSync(source)) {
var stats = fs.statSync(source);
if(stats && stats.isFile()) {
if (fs.existsSync(source)) {
const stats = fs.statSync(source);
if (stats && stats.isFile()) {
config = require(source);
}
}
Expand All @@ -87,5 +65,5 @@ var readOptions = function(configDir) {
};

module.exports = {
execute: execute
execute
};
54 changes: 22 additions & 32 deletions lib/defaults.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,35 @@
var response = require('./response'),
const response = require('./response'),
_ = require('lodash');

var defaults = {},
const defaults = {},
methods = ['get', 'post', 'put', 'delete', 'patch', 'options'];

defaults.get = defaults.post = defaults.put = defaults.delete = defaults.patch = defaults.options = {
cache: false,
delay: false,
proxy: false,
size: function() {
return _.random(2, 10);
},
collection: false,
callback: response.generate,
render: response.render
};
methods.forEach((method) => {
defaults[method] = {
cache: false,
delay: false,
proxy: false,
size: () => _.random(2, 10),
collection: false,
callback: response.generate,
render: response.render
};
});

defaults.get.cache = true;

var assignToAll = function(rawConfigs) {

var configs = {};

methods.forEach(function(method) {

const assignToAll = (rawConfigs) => {
const configs = {};
methods.forEach((method) => {
configs[method] = assign(rawConfigs[method], method);

});

return configs;

};

var assign = function(configs, method) {

const assign = (configs, method) => {
configs = _.isArray(configs) ? configs : [configs];

return _.compact(configs.map(function(config) {

if(!config || !config.path) {
return _.compact(configs.map((config) => {
if (!config || !config.path) {
return;
}

Expand All @@ -47,12 +38,11 @@ var assign = function(configs, method) {

// Bind each method to the config itself
return _.bindAll(config);

}));

};

module.exports = {
assignToAll: assignToAll,
assign: assign
};
assignToAll,
assign
};
6 changes: 3 additions & 3 deletions lib/delay.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var _ = require('lodash');
const _ = require('lodash');

var delay = function(req, res, next) {
var delay = res.locals.config.delay;
const delay = (req, res, next) => {
const delay = res.locals.config.delay;
if(typeof delay === 'number') {
_.delay(next, delay);
} else if(_.isArray(delay)) {
Expand Down
63 changes: 22 additions & 41 deletions lib/dyson.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
var path = require('path'),
https = require('https'),
const https = require('https'),
express = require('express'),
favicon = require('serve-favicon'),
bodyParser = require('body-parser'),
Expand All @@ -20,29 +19,22 @@ var path = require('path'),
* 2. Create Express server
* 3. Register configured services with Express
*/

var bootstrap = function(options) {

var configs = getConfigurations(options);

var app = createServer(options);
const bootstrap = (options) => {
const configs = getConfigurations(options);
const app = createServer(options);

return registerServices(app, options, configs);

};

var getConfigurations = function(options) {

const getConfigurations = (options) => {
util.options.set(options);

var rawConfigs = loader.load(options.configDir);
const rawConfigs = loader.load(options.configDir);

return defaults.assignToAll(rawConfigs);
};

var createServer = function(options) {

var app = express();
const createServer = (options) => {
const app = express();

if(options.https) {
https.createServer(options.https, app).listen(options.port);
Expand All @@ -54,12 +46,10 @@ var createServer = function(options) {
};

// Register middleware to Express as service for each config (as in: `app.get(config.path, config.callback);`)

var registerServices = function(app, options, configs) {

var request = express();
const registerServices = (app, options, configs) => {
const request = express();
request.use(rawBody());
request.use(favicon(__dirname + '/favicon.ico'));
request.use(favicon(`${__dirname}/favicon.ico`));
if(options.bodyParserJsonLimit) {
request.use(bodyParser.json({limit: options.bodyParserJsonLimit}));
} else {
Expand All @@ -72,19 +62,15 @@ var registerServices = function(app, options, configs) {
}
request.use(cookieParser());
request.use(cors({origin: true, credentials: true}));

for(var method in configs) {

configs[method].forEach(function(config) {

Object.keys(configs).forEach((method) => {
configs[method].forEach((config) => {
if(options.proxy === true && config.proxy !== false) {
// Allows each config file to be bypassed without removing it
util.log('Proxying', method.toUpperCase(), 'service at', config.path);
} else {

var middlewares = [
const middlewares = [
request,
function(req, res, next) {
(req, res, next) => {
res.locals.config = config;
next();
},
Expand All @@ -96,23 +82,18 @@ var registerServices = function(app, options, configs) {

util.log('Registering', method.toUpperCase(), 'service at', config.path);
app[method].apply(app, [config.path].concat(middlewares));

if(method !== 'options') {
app.options(config.path, cors({origin: true, credentials: true}));
}
}

});
}
});

if(options.proxy) {

app.all('*', delay.middleware.bind(null, options.proxyDelay), proxy.middleware);

} else {

!util.isTest() && app.all('*', function(req, res, next) {
util.error('404 NOT FOUND:', req.url);
!util.isTest() && app.all('*', function(req, res) {
util.error(`404 NOT FOUND: ${req.url}`);
res.writeHead(404);
res.end();
});
Expand All @@ -122,8 +103,8 @@ var registerServices = function(app, options, configs) {
};

module.exports = {
bootstrap: bootstrap,
getConfigurations: getConfigurations,
createServer: createServer,
registerServices: registerServices
bootstrap,
getConfigurations,
createServer,
registerServices
};
Loading

0 comments on commit ee2f13a

Please sign in to comment.