From 9f10b2928df655c3672d9607e864e667d4bc903a Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Sun, 7 Feb 2016 11:23:23 +0100 Subject: Remove useless anonymous functions of files --- middlewares/cache.js | 36 ++++++------ middlewares/index.js | 18 +++--- middlewares/reqValidators/index.js | 18 +++--- middlewares/reqValidators/pods.js | 80 +++++++++++++-------------- middlewares/reqValidators/remote.js | 62 ++++++++++----------- middlewares/reqValidators/utils.js | 36 ++++++------ middlewares/reqValidators/videos.js | 106 ++++++++++++++++++------------------ middlewares/secure.js | 100 +++++++++++++++++----------------- 8 files changed, 220 insertions(+), 236 deletions(-) (limited to 'middlewares') diff --git a/middlewares/cache.js b/middlewares/cache.js index 782165155..0d3da0075 100644 --- a/middlewares/cache.js +++ b/middlewares/cache.js @@ -1,25 +1,23 @@ -;(function () { - 'use strict' +'use strict' - var cacheMiddleware = { - cache: cache - } - - function cache (cache) { - return function (req, res, next) { - // If we want explicitly a cache - // Or if we don't specify if we want a cache or no and we are in production - if (cache === true || (cache !== false && process.env.NODE_ENV === 'production')) { - res.setHeader('Cache-Control', 'public') - } else { - res.setHeader('Cache-Control', 'no-cache, no-store, max-age=0, must-revalidate') - } +var cacheMiddleware = { + cache: cache +} - next() +function cache (cache) { + return function (req, res, next) { + // If we want explicitly a cache + // Or if we don't specify if we want a cache or no and we are in production + if (cache === true || (cache !== false && process.env.NODE_ENV === 'production')) { + res.setHeader('Cache-Control', 'public') + } else { + res.setHeader('Cache-Control', 'no-cache, no-store, max-age=0, must-revalidate') } + + next() } +} - // --------------------------------------------------------------------------- +// --------------------------------------------------------------------------- - module.exports = cacheMiddleware -})() +module.exports = cacheMiddleware diff --git a/middlewares/index.js b/middlewares/index.js index bfe325b1e..c76c4fc2e 100644 --- a/middlewares/index.js +++ b/middlewares/index.js @@ -1,13 +1,11 @@ -;(function () { - 'use strict' +'use strict' - var middlewares = { - cache: require('./cache'), - reqValidators: require('./reqValidators'), - secure: require('./secure') - } +var middlewares = { + cache: require('./cache'), + reqValidators: require('./reqValidators'), + secure: require('./secure') +} - // --------------------------------------------------------------------------- +// --------------------------------------------------------------------------- - module.exports = middlewares -})() +module.exports = middlewares diff --git a/middlewares/reqValidators/index.js b/middlewares/reqValidators/index.js index 34d34013c..344387a80 100644 --- a/middlewares/reqValidators/index.js +++ b/middlewares/reqValidators/index.js @@ -1,13 +1,11 @@ -;(function () { - 'use strict' +'use strict' - var reqValidators = { - videos: require('./videos'), - pods: require('./pods'), - remote: require('./remote') - } +var reqValidators = { + videos: require('./videos'), + pods: require('./pods'), + remote: require('./remote') +} - // --------------------------------------------------------------------------- +// --------------------------------------------------------------------------- - module.exports = reqValidators -})() +module.exports = reqValidators diff --git a/middlewares/reqValidators/pods.js b/middlewares/reqValidators/pods.js index 4d649b486..7d1612dde 100644 --- a/middlewares/reqValidators/pods.js +++ b/middlewares/reqValidators/pods.js @@ -1,41 +1,39 @@ -;(function () { - 'use strict' - - var checkErrors = require('./utils').checkErrors - var friends = require('../../lib/friends') - var logger = require('../../helpers/logger') - - var reqValidatorsPod = { - makeFriends: makeFriends, - podsAdd: podsAdd - } - - function makeFriends (req, res, next) { - friends.hasFriends(function (err, has_friends) { - if (err) { - logger.error('Cannot know if we have friends.', { error: err }) - res.sendStatus(500) - } - - if (has_friends === true) { - // We need to quit our friends before make new ones - res.sendStatus(409) - } else { - next() - } - }) - } - - function podsAdd (req, res, next) { - req.checkBody('data.url', 'Should have an url').notEmpty().isURL({ require_protocol: true }) - req.checkBody('data.publicKey', 'Should have a public key').notEmpty() - - logger.debug('Checking podsAdd parameters', { parameters: req.body }) - - checkErrors(req, res, next) - } - - // --------------------------------------------------------------------------- - - module.exports = reqValidatorsPod -})() +'use strict' + +var checkErrors = require('./utils').checkErrors +var friends = require('../../lib/friends') +var logger = require('../../helpers/logger') + +var reqValidatorsPod = { + makeFriends: makeFriends, + podsAdd: podsAdd +} + +function makeFriends (req, res, next) { + friends.hasFriends(function (err, has_friends) { + if (err) { + logger.error('Cannot know if we have friends.', { error: err }) + res.sendStatus(500) + } + + if (has_friends === true) { + // We need to quit our friends before make new ones + res.sendStatus(409) + } else { + next() + } + }) +} + +function podsAdd (req, res, next) { + req.checkBody('data.url', 'Should have an url').notEmpty().isURL({ require_protocol: true }) + req.checkBody('data.publicKey', 'Should have a public key').notEmpty() + + logger.debug('Checking podsAdd parameters', { parameters: req.body }) + + checkErrors(req, res, next) +} + +// --------------------------------------------------------------------------- + +module.exports = reqValidatorsPod diff --git a/middlewares/reqValidators/remote.js b/middlewares/reqValidators/remote.js index 9b61481ad..88de16b49 100644 --- a/middlewares/reqValidators/remote.js +++ b/middlewares/reqValidators/remote.js @@ -1,45 +1,43 @@ -;(function () { - 'use strict' +'use strict' - var checkErrors = require('./utils').checkErrors - var logger = require('../../helpers/logger') +var checkErrors = require('./utils').checkErrors +var logger = require('../../helpers/logger') - var reqValidatorsRemote = { - remoteVideosAdd: remoteVideosAdd, - remoteVideosRemove: remoteVideosRemove, - secureRequest: secureRequest - } +var reqValidatorsRemote = { + remoteVideosAdd: remoteVideosAdd, + remoteVideosRemove: remoteVideosRemove, + secureRequest: secureRequest +} - function remoteVideosAdd (req, res, next) { - req.checkBody('data').isArray() - req.checkBody('data').eachIsRemoteVideosAddValid() +function remoteVideosAdd (req, res, next) { + req.checkBody('data').isArray() + req.checkBody('data').eachIsRemoteVideosAddValid() - logger.debug('Checking remoteVideosAdd parameters', { parameters: req.body }) + logger.debug('Checking remoteVideosAdd parameters', { parameters: req.body }) - checkErrors(req, res, next) - } + checkErrors(req, res, next) +} - function remoteVideosRemove (req, res, next) { - req.checkBody('data').isArray() - req.checkBody('data').eachIsRemoteVideosRemoveValid() +function remoteVideosRemove (req, res, next) { + req.checkBody('data').isArray() + req.checkBody('data').eachIsRemoteVideosRemoveValid() - logger.debug('Checking remoteVideosRemove parameters', { parameters: req.body }) + logger.debug('Checking remoteVideosRemove parameters', { parameters: req.body }) - checkErrors(req, res, next) - } + checkErrors(req, res, next) +} - function secureRequest (req, res, next) { - req.checkBody('signature.url', 'Should have a signature url').isURL() - req.checkBody('signature.signature', 'Should have a signature').notEmpty() - req.checkBody('key', 'Should have a key').notEmpty() - req.checkBody('data', 'Should have data').notEmpty() +function secureRequest (req, res, next) { + req.checkBody('signature.url', 'Should have a signature url').isURL() + req.checkBody('signature.signature', 'Should have a signature').notEmpty() + req.checkBody('key', 'Should have a key').notEmpty() + req.checkBody('data', 'Should have data').notEmpty() - logger.debug('Checking secureRequest parameters', { parameters: { data: req.body.data, keyLength: req.body.key.length } }) + logger.debug('Checking secureRequest parameters', { parameters: { data: req.body.data, keyLength: req.body.key.length } }) - checkErrors(req, res, next) - } + checkErrors(req, res, next) +} - // --------------------------------------------------------------------------- +// --------------------------------------------------------------------------- - module.exports = reqValidatorsRemote -})() +module.exports = reqValidatorsRemote diff --git a/middlewares/reqValidators/utils.js b/middlewares/reqValidators/utils.js index c88f6df2e..46c982571 100644 --- a/middlewares/reqValidators/utils.js +++ b/middlewares/reqValidators/utils.js @@ -1,27 +1,25 @@ -;(function () { - 'use strict' +'use strict' - var util = require('util') +var util = require('util') - var logger = require('../../helpers/logger') +var logger = require('../../helpers/logger') - var reqValidatorsUtils = { - checkErrors: checkErrors - } - - function checkErrors (req, res, next, status_code) { - if (status_code === undefined) status_code = 400 - var errors = req.validationErrors() +var reqValidatorsUtils = { + checkErrors: checkErrors +} - if (errors) { - logger.warn('Incorrect request parameters', { path: req.originalUrl, err: errors }) - return res.status(status_code).send('There have been validation errors: ' + util.inspect(errors)) - } +function checkErrors (req, res, next, status_code) { + if (status_code === undefined) status_code = 400 + var errors = req.validationErrors() - return next() + if (errors) { + logger.warn('Incorrect request parameters', { path: req.originalUrl, err: errors }) + return res.status(status_code).send('There have been validation errors: ' + util.inspect(errors)) } - // --------------------------------------------------------------------------- + return next() +} + +// --------------------------------------------------------------------------- - module.exports = reqValidatorsUtils -})() +module.exports = reqValidatorsUtils diff --git a/middlewares/reqValidators/videos.js b/middlewares/reqValidators/videos.js index f7bd24658..4e5f4391f 100644 --- a/middlewares/reqValidators/videos.js +++ b/middlewares/reqValidators/videos.js @@ -1,76 +1,74 @@ -;(function () { - 'use strict' +'use strict' - var checkErrors = require('./utils').checkErrors - var logger = require('../../helpers/logger') - var Videos = require('../../models/videos') +var checkErrors = require('./utils').checkErrors +var logger = require('../../helpers/logger') +var Videos = require('../../models/videos') - var reqValidatorsVideos = { - videosAdd: videosAdd, - videosGet: videosGet, - videosRemove: videosRemove, - videosSearch: videosSearch - } +var reqValidatorsVideos = { + videosAdd: videosAdd, + videosGet: videosGet, + videosRemove: videosRemove, + videosSearch: videosSearch +} - function videosAdd (req, res, next) { - req.checkFiles('input_video[0].originalname', 'Should have an input video').notEmpty() - req.checkFiles('input_video[0].mimetype', 'Should have a correct mime type').matches(/video\/(webm)|(mp4)|(ogg)/i) - req.checkBody('name', 'Should have a name').isLength(1, 50) - req.checkBody('description', 'Should have a description').isLength(1, 250) +function videosAdd (req, res, next) { + req.checkFiles('input_video[0].originalname', 'Should have an input video').notEmpty() + req.checkFiles('input_video[0].mimetype', 'Should have a correct mime type').matches(/video\/(webm)|(mp4)|(ogg)/i) + req.checkBody('name', 'Should have a name').isLength(1, 50) + req.checkBody('description', 'Should have a description').isLength(1, 250) - logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files }) + logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files }) - checkErrors(req, res, next) - } + checkErrors(req, res, next) +} - function videosGet (req, res, next) { - req.checkParams('id', 'Should have a valid id').notEmpty().isMongoId() +function videosGet (req, res, next) { + req.checkParams('id', 'Should have a valid id').notEmpty().isMongoId() - logger.debug('Checking videosGet parameters', { parameters: req.params }) + logger.debug('Checking videosGet parameters', { parameters: req.params }) - checkErrors(req, res, function () { - Videos.getVideoState(req.params.id, function (err, state) { - if (err) { - logger.error('Error in videosGet request validator.', { error: err }) - res.sendStatus(500) - } + checkErrors(req, res, function () { + Videos.getVideoState(req.params.id, function (err, state) { + if (err) { + logger.error('Error in videosGet request validator.', { error: err }) + res.sendStatus(500) + } - if (state.exist === false) return res.status(404).send('Video not found') + if (state.exist === false) return res.status(404).send('Video not found') - next() - }) + next() }) - } + }) +} - function videosRemove (req, res, next) { - req.checkParams('id', 'Should have a valid id').notEmpty().isMongoId() +function videosRemove (req, res, next) { + req.checkParams('id', 'Should have a valid id').notEmpty().isMongoId() - logger.debug('Checking videosRemove parameters', { parameters: req.params }) + logger.debug('Checking videosRemove parameters', { parameters: req.params }) - checkErrors(req, res, function () { - Videos.getVideoState(req.params.id, function (err, state) { - if (err) { - logger.error('Error in videosRemove request validator.', { error: err }) - res.sendStatus(500) - } + checkErrors(req, res, function () { + Videos.getVideoState(req.params.id, function (err, state) { + if (err) { + logger.error('Error in videosRemove request validator.', { error: err }) + res.sendStatus(500) + } - if (state.exist === false) return res.status(404).send('Video not found') - else if (state.owned === false) return res.status(403).send('Cannot remove video of another pod') + if (state.exist === false) return res.status(404).send('Video not found') + else if (state.owned === false) return res.status(403).send('Cannot remove video of another pod') - next() - }) + next() }) - } + }) +} - function videosSearch (req, res, next) { - req.checkParams('name', 'Should have a name').notEmpty() +function videosSearch (req, res, next) { + req.checkParams('name', 'Should have a name').notEmpty() - logger.debug('Checking videosSearch parameters', { parameters: req.params }) + logger.debug('Checking videosSearch parameters', { parameters: req.params }) - checkErrors(req, res, next) - } + checkErrors(req, res, next) +} - // --------------------------------------------------------------------------- +// --------------------------------------------------------------------------- - module.exports = reqValidatorsVideos -})() +module.exports = reqValidatorsVideos diff --git a/middlewares/secure.js b/middlewares/secure.js index b7a18ad3e..bfd28316a 100644 --- a/middlewares/secure.js +++ b/middlewares/secure.js @@ -1,51 +1,49 @@ -;(function () { - 'use strict' - - var logger = require('../helpers/logger') - var peertubeCrypto = require('../helpers/peertubeCrypto') - var Pods = require('../models/pods') - - var secureMiddleware = { - decryptBody: decryptBody - } - - function decryptBody (req, res, next) { - var url = req.body.signature.url - Pods.findByUrl(url, function (err, pod) { - if (err) { - logger.error('Cannot get signed url in decryptBody.', { error: err }) - return res.sendStatus(500) - } - - if (pod === null) { - logger.error('Unknown pod %s.', url) - return res.sendStatus(403) - } - - logger.debug('Decrypting body from %s.', url) - - var signature_ok = peertubeCrypto.checkSignature(pod.publicKey, url, req.body.signature.signature) - - if (signature_ok === true) { - peertubeCrypto.decrypt(req.body.key, req.body.data, function (err, decrypted) { - if (err) { - logger.error('Cannot decrypt data.', { error: err }) - return res.sendStatus(500) - } - - req.body.data = JSON.parse(decrypted) - delete req.body.key - - next() - }) - } else { - logger.error('Signature is not okay in decryptBody for %s.', req.body.signature.url) - return res.sendStatus(403) - } - }) - } - - // --------------------------------------------------------------------------- - - module.exports = secureMiddleware -})() +'use strict' + +var logger = require('../helpers/logger') +var peertubeCrypto = require('../helpers/peertubeCrypto') +var Pods = require('../models/pods') + +var secureMiddleware = { + decryptBody: decryptBody +} + +function decryptBody (req, res, next) { + var url = req.body.signature.url + Pods.findByUrl(url, function (err, pod) { + if (err) { + logger.error('Cannot get signed url in decryptBody.', { error: err }) + return res.sendStatus(500) + } + + if (pod === null) { + logger.error('Unknown pod %s.', url) + return res.sendStatus(403) + } + + logger.debug('Decrypting body from %s.', url) + + var signature_ok = peertubeCrypto.checkSignature(pod.publicKey, url, req.body.signature.signature) + + if (signature_ok === true) { + peertubeCrypto.decrypt(req.body.key, req.body.data, function (err, decrypted) { + if (err) { + logger.error('Cannot decrypt data.', { error: err }) + return res.sendStatus(500) + } + + req.body.data = JSON.parse(decrypted) + delete req.body.key + + next() + }) + } else { + logger.error('Signature is not okay in decryptBody for %s.', req.body.signature.url) + return res.sendStatus(403) + } + }) +} + +// --------------------------------------------------------------------------- + +module.exports = secureMiddleware -- cgit v1.2.3