From 528a9efa8272532bbd0dafc35c3e05e57c50f61e Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Sat, 18 Jun 2016 16:13:54 +0200 Subject: Try to make a better communication (between pods) module --- server/helpers/customValidators.js | 48 +++++++++------- server/helpers/requests.js | 114 ++++++++++++++----------------------- 2 files changed, 72 insertions(+), 90 deletions(-) (limited to 'server/helpers') diff --git a/server/helpers/customValidators.js b/server/helpers/customValidators.js index 9c3ff38ef..a6cf680e5 100644 --- a/server/helpers/customValidators.js +++ b/server/helpers/customValidators.js @@ -7,8 +7,7 @@ const VIDEOS_CONSTRAINTS_FIELDS = constants.VIDEOS_CONSTRAINTS_FIELDS const customValidators = { exists: exists, - isEachAddRemoteVideosValid: isEachAddRemoteVideosValid, - isEachRemoveRemoteVideosValid: isEachRemoveRemoteVideosValid, + isEachRemoteVideosValid: isEachRemoteVideosValid, isArray: isArray, isVideoAuthorValid: isVideoAuthorValid, isVideoDateValid: isVideoDateValid, @@ -25,23 +24,26 @@ function exists (value) { return value !== undefined && value !== null } -function isEachAddRemoteVideosValid (videos) { - return videos.every(function (video) { - return isVideoAuthorValid(video.author) && - isVideoDateValid(video.createdDate) && - isVideoDescriptionValid(video.description) && - isVideoDurationValid(video.duration) && - isVideoMagnetUriValid(video.magnetUri) && - isVideoNameValid(video.name) && - isVideoPodUrlValid(video.podUrl) && - isVideoTagsValid(video.tags) && - isVideoThumbnailValid(video.thumbnailBase64) - }) -} - -function isEachRemoveRemoteVideosValid (videos) { - return videos.every(function (video) { - return isVideoMagnetUriValid(video.magnetUri) +function isEachRemoteVideosValid (requests) { + return requests.every(function (request) { + const video = request.data + return ( + isRequestTypeAddValid(request.type) && + isVideoAuthorValid(video.author) && + isVideoDateValid(video.createdDate) && + isVideoDescriptionValid(video.description) && + isVideoDurationValid(video.duration) && + isVideoMagnetUriValid(video.magnetUri) && + isVideoNameValid(video.name) && + isVideoPodUrlValid(video.podUrl) && + isVideoTagsValid(video.tags) && + isVideoThumbnailValid(video.thumbnailBase64) + ) || + ( + isRequestTypeRemoveValid(request.type) && + isVideoNameValid(video.name) && + isVideoMagnetUriValid(video.magnetUri) + ) }) } @@ -49,6 +51,14 @@ function isArray (value) { return Array.isArray(value) } +function isRequestTypeAddValid (value) { + return value === 'add' +} + +function isRequestTypeRemoveValid (value) { + return value === 'remove' +} + function isVideoAuthorValid (value) { return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.AUTHOR) } diff --git a/server/helpers/requests.js b/server/helpers/requests.js index 1e1bb4111..871342d60 100644 --- a/server/helpers/requests.js +++ b/server/helpers/requests.js @@ -1,12 +1,10 @@ 'use strict' -const async = require('async') const config = require('config') -const request = require('request') const replay = require('request-replay') +const request = require('request') const constants = require('../initializers/constants') -const logger = require('./logger') const peertubeCrypto = require('./peertubeCrypto') const http = config.get('webserver.https') ? 'https' : 'http' @@ -14,93 +12,67 @@ const host = config.get('webserver.host') const port = config.get('webserver.port') const requests = { - makeMultipleRetryRequest: makeMultipleRetryRequest + makeRetryRequest: makeRetryRequest, + makeSecureRequest: makeSecureRequest } -function makeMultipleRetryRequest (allData, pods, callbackEach, callback) { - if (!callback) { - callback = callbackEach - callbackEach = null - } +function makeRetryRequest (params, callback) { + replay( + request(params, callback), + { + retries: constants.RETRY_REQUESTS, + factor: 3, + maxTimeout: Infinity, + errorCodes: [ 'EADDRINFO', 'ETIMEDOUT', 'ECONNRESET', 'ESOCKETTIMEDOUT', 'ENOTFOUND', 'ECONNREFUSED' ] + } + ) +} - const url = http + '://' + host + ':' + port - let signature +function makeSecureRequest (params, callback) { + const myUrl = http + '://' + host + ':' + port - // Add signature if it is specified in the params - if (allData.method === 'POST' && allData.data && allData.sign === true) { - signature = peertubeCrypto.sign(url) + const requestParams = { + url: params.toPod.url + params.path } - // Make a request for each pod - async.each(pods, function (pod, callbackEachAsync) { - function callbackEachRetryRequest (err, response, body, url, pod) { - if (callbackEach !== null) { - callbackEach(err, response, body, url, pod, function () { - callbackEachAsync() - }) - } else { - callbackEachAsync() - } - } + // Add data with POST requst ? + if (params.method === 'POST') { + requestParams.json = {} - const params = { - url: pod.url + allData.path, - method: allData.method + // Add signature if it is specified in the params + if (params.sign === true) { + requestParams.json.signature = { + url: myUrl, + signature: peertubeCrypto.sign(myUrl) + } } - // Add data with POST requst ? - if (allData.method === 'POST' && allData.data) { - // Encrypt data ? - if (allData.encrypt === true) { - peertubeCrypto.encrypt(pod.publicKey, JSON.stringify(allData.data), function (err, encrypted) { + // If there are data informations + if (params.data) { + // Encrypt data + if (params.encrypt === true) { + peertubeCrypto.encrypt(params.toPod.publicKey, JSON.stringify(params.data), function (err, encrypted) { if (err) return callback(err) - params.json = { - data: encrypted.data, - key: encrypted.key - } + requestParams.json.data = encrypted.data + requestParams.json.key = encrypted.key - makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest) + request.post(requestParams, callback) }) } else { - params.json = { data: allData.data } - makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest) + // No encryption + requestParams.json.data = params.data + request.post(requestParams, callback) } } else { - makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest) + // No data + request.post(requestParams, callback) } - }, callback) + } else { + request.get(requestParams, callback) + } } // --------------------------------------------------------------------------- module.exports = requests - -// --------------------------------------------------------------------------- - -function makeRetryRequest (params, fromUrl, toPod, signature, callbackEach) { - // Append the signature - if (signature) { - params.json.signature = { - url: fromUrl, - signature: signature - } - } - - logger.debug('Make retry requests to %s.', toPod.url) - - replay( - request.post(params, function (err, response, body) { - callbackEach(err, response, body, params.url, toPod) - }), - { - retries: constants.REQUEST_RETRIES, - factor: 3, - maxTimeout: Infinity, - errorCodes: [ 'EADDRINFO', 'ETIMEDOUT', 'ECONNRESET', 'ESOCKETTIMEDOUT', 'ENOTFOUND', 'ECONNREFUSED' ] - } - ).on('replay', function (replay) { - logger.info('Replaying request to %s. Request failed: %d %s. Replay number: #%d. Will retry in: %d ms.', - params.url, replay.error.code, replay.error.message, replay.number, replay.delay) - }) -} -- cgit v1.2.3