From bc503c2a62dcf9aed6b8d90b68f0f27a7755ac01 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 11 May 2016 21:19:34 +0200 Subject: Update to standard 7. Goodbye snake_case, I used to love you --- server/lib/friends.js | 72 ++++++++++++++++++++--------------------- server/lib/requestsScheduler.js | 66 ++++++++++++++++++------------------- server/lib/videos.js | 54 +++++++++++++++---------------- server/lib/webtorrent.js | 34 +++++++++---------- server/lib/webtorrentProcess.js | 14 ++++---- 5 files changed, 119 insertions(+), 121 deletions(-) (limited to 'server/lib') diff --git a/server/lib/friends.js b/server/lib/friends.js index 3b8a52060..f4f2ada87 100644 --- a/server/lib/friends.js +++ b/server/lib/friends.js @@ -39,8 +39,8 @@ function hasFriends (callback) { Pods.count(function (err, count) { if (err) return callback(err) - const has_friends = (count !== 0) - callback(null, has_friends) + const hasFriends = (count !== 0) + callback(null, hasFriends) }) } @@ -49,7 +49,7 @@ function getMyCertificate (callback) { } function makeFriends (callback) { - const pods_score = {} + const podsScore = {} logger.info('Make friends!') getMyCertificate(function (err, cert) { @@ -60,16 +60,16 @@ function makeFriends (callback) { const urls = config.get('network.friends') - async.each(urls, function (url, callback_each) { - computeForeignPodsList(url, pods_score, callback_each) + async.each(urls, function (url, callbackEach) { + computeForeignPodsList(url, podsScore, callbackEach) }, function (err) { if (err) return callback(err) - logger.debug('Pods scores computed.', { pods_score: pods_score }) - const pods_list = computeWinningPods(urls, pods_score) - logger.debug('Pods that we keep.', { pods_to_keep: pods_list }) + logger.debug('Pods scores computed.', { podsScore: podsScore }) + const podsList = computeWinningPods(urls, podsScore) + logger.debug('Pods that we keep.', { podsToKeep: podsList }) - makeRequestsToWinningPods(cert, pods_list, callback) + makeRequestsToWinningPods(cert, podsList, callback) }) }) } @@ -102,10 +102,10 @@ function quitFriends (callback) { logger.info('Broke friends, so sad :(') - Videos.listFromRemotes(function (err, videos_list) { + Videos.listFromRemotes(function (err, videosList) { if (err) return callback(err) - videos.removeRemoteVideos(videos_list, function (err) { + videos.removeRemoteVideos(videosList, function (err) { if (err) { logger.error('Cannot remove remote videos.', { error: err }) return callback(err) @@ -132,35 +132,35 @@ module.exports = pods // --------------------------------------------------------------------------- -function computeForeignPodsList (url, pods_score, callback) { +function computeForeignPodsList (url, podsScore, callback) { // Let's give 1 point to the pod we ask the friends list - pods_score[url] = 1 + podsScore[url] = 1 - getForeignPodsList(url, function (err, foreign_pods_list) { + getForeignPodsList(url, function (err, foreignPodsList) { if (err) return callback(err) - if (foreign_pods_list.length === 0) return callback() + if (foreignPodsList.length === 0) return callback() - foreign_pods_list.forEach(function (foreign_pod) { - const foreign_url = foreign_pod.url + foreignPodsList.forEach(function (foreignPod) { + const foreignUrl = foreignPod.url - if (pods_score[foreign_url]) pods_score[foreign_url]++ - else pods_score[foreign_url] = 1 + if (podsScore[foreignUrl]) podsScore[foreignUrl]++ + else podsScore[foreignUrl] = 1 }) callback() }) } -function computeWinningPods (urls, pods_score) { +function computeWinningPods (urls, podsScore) { // Build the list of pods to add // Only add a pod if it exists in more than a half base pods - const pods_list = [] - const base_score = urls.length / 2 - Object.keys(pods_score).forEach(function (pod) { - if (pods_score[pod] > base_score) pods_list.push({ url: pod }) + const podsList = [] + const baseScore = urls.length / 2 + Object.keys(baseScore).forEach(function (pod) { + if (podsScore[pod] > baseScore) podsList.push({ url: pod }) }) - return pods_list + return podsList } function getForeignPodsList (url, callback) { @@ -173,14 +173,14 @@ function getForeignPodsList (url, callback) { }) } -function makeRequestsToWinningPods (cert, pods_list, callback) { +function makeRequestsToWinningPods (cert, podsList, callback) { // Stop pool requests requestsScheduler.deactivate() // Flush pool requests requestsScheduler.forceSend() // Get the list of our videos to send to our new friends - Videos.listOwned(function (err, videos_list) { + Videos.listOwned(function (err, videosList) { if (err) { logger.error('Cannot get the list of videos we own.') return callback(err) @@ -189,38 +189,36 @@ function makeRequestsToWinningPods (cert, pods_list, callback) { const data = { url: http + '://' + host + ':' + port, publicKey: cert, - videos: videos_list + videos: videosList } requests.makeMultipleRetryRequest( { method: 'POST', path: '/api/' + constants.API_VERSION + '/pods/', data: data }, - pods_list, + podsList, - function eachRequest (err, response, body, url, pod, callback_each_request) { + function eachRequest (err, response, body, url, pod, callbackEachRequest) { // We add the pod if it responded correctly with its public certificate if (!err && response.statusCode === 200) { Pods.add({ url: pod.url, publicKey: body.cert, score: constants.FRIEND_BASE_SCORE }, function (err) { if (err) { logger.error('Error with adding %s pod.', pod.url, { error: err }) - return callback_each_request() + return callbackEachRequest() } - console.log('hihi') + videos.createRemoteVideos(body.videos, function (err) { if (err) { logger.error('Error with adding videos of pod.', pod.url, { error: err }) - return callback_each_request() + return callbackEachRequest() } - console.log('kik') - logger.debug('Adding remote videos from %s.', pod.url, { videos: body.videos }) - return callback_each_request() + return callbackEachRequest() }) }) } else { logger.error('Error with adding %s pod.', pod.url, { error: err || new Error('Status not 200') }) - return callback_each_request() + return callbackEachRequest() } }, diff --git a/server/lib/requestsScheduler.js b/server/lib/requestsScheduler.js index 4953f6a91..f10de6276 100644 --- a/server/lib/requestsScheduler.js +++ b/server/lib/requestsScheduler.js @@ -72,7 +72,7 @@ module.exports = requestsScheduler // --------------------------------------------------------------------------- -function makeRequest (type, requests_to_make, callback) { +function makeRequest (type, requestsToMake, callback) { if (!callback) callback = function () {} Pods.list(function (err, pods) { @@ -83,7 +83,7 @@ function makeRequest (type, requests_to_make, callback) { sign: true, method: 'POST', path: null, - data: requests_to_make + data: requestsToMake } if (type === 'add') { @@ -94,26 +94,26 @@ function makeRequest (type, requests_to_make, callback) { return callback(new Error('Unkown pool request type.')) } - const bad_pods = [] - const good_pods = [] + const badPods = [] + const goodPods = [] requests.makeMultipleRetryRequest(params, pods, callbackEachPodFinished, callbackAllPodsFinished) - function callbackEachPodFinished (err, response, body, url, pod, callback_each_pod_finished) { + function callbackEachPodFinished (err, response, body, url, pod, callbackEachPodFinished) { if (err || (response.statusCode !== 200 && response.statusCode !== 201 && response.statusCode !== 204)) { - bad_pods.push(pod._id) + badPods.push(pod._id) logger.error('Error sending secure request to %s pod.', url, { error: err || new Error('Status code not 20x') }) } else { - good_pods.push(pod._id) + goodPods.push(pod._id) } - return callback_each_pod_finished() + return callbackEachPodFinished() } function callbackAllPodsFinished (err) { if (err) return callback(err) - updatePodsScore(good_pods, bad_pods) + updatePodsScore(goodPods, badPods) callback(null) } }) @@ -130,7 +130,7 @@ function makeRequests () { if (requests.length === 0) return - const requests_to_make = { + const requestsToMake = { add: { ids: [], requests: [] @@ -141,35 +141,35 @@ function makeRequests () { } } - async.each(requests, function (pool_request, callback_each) { - if (pool_request.type === 'add') { - requests_to_make.add.requests.push(pool_request.request) - requests_to_make.add.ids.push(pool_request._id) - } else if (pool_request.type === 'remove') { - requests_to_make.remove.requests.push(pool_request.request) - requests_to_make.remove.ids.push(pool_request._id) + async.each(requests, function (poolRequest, callbackEach) { + if (poolRequest.type === 'add') { + requestsToMake.add.requests.push(poolRequest.request) + requestsToMake.add.ids.push(poolRequest._id) + } else if (poolRequest.type === 'remove') { + requestsToMake.remove.requests.push(poolRequest.request) + requestsToMake.remove.ids.push(poolRequest._id) } else { - logger.error('Unkown request type.', { request_type: pool_request.type }) + logger.error('Unkown request type.', { request_type: poolRequest.type }) return // abort } - callback_each() + callbackEach() }, function () { // Send the add requests - if (requests_to_make.add.requests.length !== 0) { - makeRequest('add', requests_to_make.add.requests, function (err) { + if (requestsToMake.add.requests.length !== 0) { + makeRequest('add', requestsToMake.add.requests, function (err) { if (err) logger.error('Errors when sent add requests.', { error: err }) - Requests.removeRequests(requests_to_make.add.ids) + Requests.removeRequests(requestsToMake.add.ids) }) } // Send the remove requests - if (requests_to_make.remove.requests.length !== 0) { - makeRequest('remove', requests_to_make.remove.requests, function (err) { + if (requestsToMake.remove.requests.length !== 0) { + makeRequest('remove', requestsToMake.remove.requests, function (err) { if (err) logger.error('Errors when sent remove pool requests.', { error: err }) - Requests.removeRequests(requests_to_make.remove.ids) + Requests.removeRequests(requestsToMake.remove.ids) }) } }) @@ -188,11 +188,11 @@ function removeBadPods () { const urls = map(pods, 'url') const ids = map(pods, '_id') - Videos.listFromUrls(urls, function (err, videos_list) { + Videos.listFromUrls(urls, function (err, videosList) { if (err) { logger.error('Cannot list videos urls.', { error: err, urls: urls }) } else { - videos.removeRemoteVideos(videos_list, function (err) { + videos.removeRemoteVideos(videosList, function (err) { if (err) logger.error('Cannot remove remote videos.', { error: err }) }) } @@ -201,22 +201,22 @@ function removeBadPods () { if (err) { logger.error('Cannot remove bad pods.', { error: err }) } else { - const pods_removed = r.result.n - logger.info('Removed %d pods.', pods_removed) + const podsRemoved = r.result.n + logger.info('Removed %d pods.', podsRemoved) } }) }) }) } -function updatePodsScore (good_pods, bad_pods) { - logger.info('Updating %d good pods and %d bad pods scores.', good_pods.length, bad_pods.length) +function updatePodsScore (goodPods, badPods) { + logger.info('Updating %d good pods and %d bad pods scores.', goodPods.length, badPods.length) - Pods.incrementScores(good_pods, constants.PODS_SCORE.BONUS, function (err) { + Pods.incrementScores(goodPods, constants.PODS_SCORE.BONUS, function (err) { if (err) logger.error('Cannot increment scores of good pods.') }) - Pods.incrementScores(bad_pods, constants.PODS_SCORE.MALUS, function (err) { + Pods.incrementScores(badPods, constants.PODS_SCORE.MALUS, function (err) { if (err) logger.error('Cannot increment scores of bad pods.') removeBadPods() }) diff --git a/server/lib/videos.js b/server/lib/videos.js index b3497743a..7da4b11d2 100644 --- a/server/lib/videos.js +++ b/server/lib/videos.js @@ -29,15 +29,15 @@ const videos = { function createRemoteVideos (videos, callback) { // Create the remote videos from the new pod - createRemoteVideoObjects(videos, function (err, remote_videos) { + createRemoteVideoObjects(videos, function (err, remoteVideos) { if (err) return callback(err) - Videos.addRemotes(remote_videos, callback) + Videos.addRemotes(remoteVideos, callback) }) } -function getVideoDuration (video_path, callback) { - ffmpeg.ffprobe(video_path, function (err, metadata) { +function getVideoDuration (videoPath, callback) { + ffmpeg.ffprobe(videoPath, function (err, metadata) { if (err) return callback(err) return callback(null, Math.floor(metadata.format.duration)) @@ -54,9 +54,9 @@ function getVideoState (video) { return { exist: exist, owned: owned } } -function getVideoThumbnail (video_path, callback) { - const filename = pathUtils.basename(video_path) + '.jpg' - ffmpeg(video_path) +function getVideoThumbnail (videoPath, callback) { + const filename = pathUtils.basename(videoPath) + '.jpg' + ffmpeg(videoPath) .on('error', callback) .on('end', function () { callback(null, filename) @@ -71,7 +71,7 @@ function getVideoThumbnail (video_path, callback) { // Remove video datas from disk (video file, thumbnail...) function removeVideosDataFromDisk (videos, callback) { - async.each(videos, function (video, callback_each) { + async.each(videos, function (video, callbackEach) { fs.unlink(thumbnailsDir + video.thumbnail, function (err) { if (err) logger.error('Cannot remove the video thumbnail') @@ -79,13 +79,13 @@ function removeVideosDataFromDisk (videos, callback) { fs.unlink(uploadDir + video.namePath, function (err) { if (err) { logger.error('Cannot remove this video file.') - return callback_each(err) + return callbackEach(err) } - callback_each(null) + callbackEach(null) }) } else { - callback_each(null) + callbackEach(null) } }) }, callback) @@ -110,20 +110,20 @@ function seed (path, callback) { } function seedAllExisting (callback) { - Videos.listOwned(function (err, videos_list) { + Videos.listOwned(function (err, videosList) { if (err) { logger.error('Cannot get list of the videos to seed.') return callback(err) } - async.each(videos_list, function (video, each_callback) { + async.each(videosList, function (video, callbackEach) { seed(uploadDir + video.namePath, function (err) { if (err) { logger.error('Cannot seed this video.') return callback(err) } - each_callback(null) + callbackEach(null) }) }, callback) }) @@ -136,16 +136,16 @@ module.exports = videos // --------------------------------------------------------------------------- function createRemoteVideoObjects (videos, callback) { - const remote_videos = [] + const remoteVideos = [] - async.each(videos, function (video, callback_each) { + async.each(videos, function (video, callbackEach) { // Creating the thumbnail for this remote video - utils.generateRandomString(16, function (err, random_string) { - if (err) return callback_each(err) + utils.generateRandomString(16, function (err, randomString) { + if (err) return callbackEach(err) - const thumbnail_name = random_string + '.jpg' - createThumbnailFromBase64(thumbnail_name, video.thumbnail_base64, function (err) { - if (err) return callback_each(err) + const thumbnailName = randomString + '.jpg' + createThumbnailFromBase64(thumbnailName, video.thumbnailBase64, function (err) { + if (err) return callbackEach(err) const params = { name: video.name, @@ -153,21 +153,21 @@ function createRemoteVideoObjects (videos, callback) { magnetUri: video.magnetUri, podUrl: video.podUrl, duration: video.duration, - thumbnail: thumbnail_name + thumbnail: thumbnailName } - remote_videos.push(params) + remoteVideos.push(params) - callback_each(null) + callbackEach(null) }) }) }, function (err) { if (err) return callback(err) - callback(null, remote_videos) + callback(null, remoteVideos) }) } -function createThumbnailFromBase64 (thumbnail_name, data, callback) { - fs.writeFile(thumbnailsDir + thumbnail_name, data, { encoding: 'base64' }, callback) +function createThumbnailFromBase64 (thumbnailName, data, callback) { + fs.writeFile(thumbnailsDir + thumbnailName, data, { encoding: 'base64' }, callback) } diff --git a/server/lib/webtorrent.js b/server/lib/webtorrent.js index 656f8c7a8..fe2ee357f 100644 --- a/server/lib/webtorrent.js +++ b/server/lib/webtorrent.js @@ -7,7 +7,7 @@ const spawn = require('electron-spawn') const logger = require('../helpers/logger') -const electron_debug = config.get('electron.debug') +const electronDebug = config.get('electron.debug') let host = config.get('webserver.host') let port = config.get('webserver.port') let nodeKey = 'webtorrentnode' + port @@ -43,13 +43,13 @@ function create (options, callback) { if (!webtorrent.silent) logger.info('IPC server ready.') // Run a timeout of 30s after which we exit the process - const timeout_webtorrent_process = setTimeout(function () { + const timeoutWebtorrentProcess = setTimeout(function () { throw new Error('Timeout : cannot run the webtorrent process. Please ensure you have electron-prebuilt npm package installed with xvfb-run.') }, 30000) ipc.server.on(processKey + '.ready', function () { if (!webtorrent.silent) logger.info('Webtorrent process ready.') - clearTimeout(timeout_webtorrent_process) + clearTimeout(timeoutWebtorrentProcess) callback() }) @@ -57,19 +57,19 @@ function create (options, callback) { throw new Error('Received exception error from webtorrent process : ' + data.exception) }) - const webtorrent_process = spawn(pathUtils.join(__dirname, 'webtorrentProcess.js'), host, port, { detached: true }) + const webtorrentProcess = spawn(pathUtils.join(__dirname, 'webtorrentProcess.js'), host, port, { detached: true }) - if (electron_debug === true) { - webtorrent_process.stderr.on('data', function (data) { + if (electronDebug === true) { + webtorrentProcess.stderr.on('data', function (data) { logger.debug('Webtorrent process stderr: ', data.toString()) }) - webtorrent_process.stdout.on('data', function (data) { + webtorrentProcess.stdout.on('data', function (data) { logger.debug('Webtorrent process:', data.toString()) }) } - webtorrent.app = webtorrent_process + webtorrent.app = webtorrentProcess }) ipc.server.start() @@ -88,8 +88,8 @@ function seed (path, callback) { if (!webtorrent.silent) logger.debug('Node wants to seed %s.', data._id) // Finish signal - const event_key = nodeKey + '.seedDone.' + data._id - ipc.server.on(event_key, function listener (received) { + const eventKey = nodeKey + '.seedDone.' + data._id + ipc.server.on(eventKey, function listener (received) { if (!webtorrent.silent) logger.debug('Process seeded torrent %s.', received.magnetUri) // This is a fake object, we just use the magnetUri in this project @@ -97,7 +97,7 @@ function seed (path, callback) { magnetURI: received.magnetUri } - ipc.server.off(event_key) + ipc.server.off(eventKey) callback(torrent) }) @@ -115,8 +115,8 @@ function add (magnetUri, callback) { if (!webtorrent.silent) logger.debug('Node wants to add ' + data._id) // Finish signal - const event_key = nodeKey + '.addDone.' + data._id - ipc.server.on(event_key, function (received) { + const eventKey = nodeKey + '.addDone.' + data._id + ipc.server.on(eventKey, function (received) { if (!webtorrent.silent) logger.debug('Process added torrent.') // This is a fake object, we just use the magnetUri in this project @@ -124,7 +124,7 @@ function add (magnetUri, callback) { files: received.files } - ipc.server.off(event_key) + ipc.server.off(eventKey) callback(torrent) }) @@ -142,14 +142,14 @@ function remove (magnetUri, callback) { if (!webtorrent.silent) logger.debug('Node wants to stop seeding %s.', data._id) // Finish signal - const event_key = nodeKey + '.removeDone.' + data._id - ipc.server.on(event_key, function (received) { + const eventKey = nodeKey + '.removeDone.' + data._id + ipc.server.on(eventKey, function (received) { if (!webtorrent.silent) logger.debug('Process removed torrent %s.', data._id) let err = null if (received.err) err = received.err - ipc.server.off(event_key) + ipc.server.off(eventKey) callback(err) }) diff --git a/server/lib/webtorrentProcess.js b/server/lib/webtorrentProcess.js index 7889e7128..be7ac5bb4 100644 --- a/server/lib/webtorrentProcess.js +++ b/server/lib/webtorrentProcess.js @@ -26,11 +26,11 @@ function webtorrent (args) { const _id = data._id wt.seed(path, { announceList: '' }, function (torrent) { - const to_send = { + const toSend = { magnetUri: torrent.magnetURI } - ipc.of[nodeKey].emit(nodeKey + '.seedDone.' + _id, to_send) + ipc.of[nodeKey].emit(nodeKey + '.seedDone.' + _id, toSend) }) } @@ -40,15 +40,15 @@ function webtorrent (args) { const _id = data._id wt.add(magnetUri, function (torrent) { - const to_send = { + const toSend = { files: [] } torrent.files.forEach(function (file) { - to_send.files.push({ path: file.path }) + toSend.files.push({ path: file.path }) }) - ipc.of[nodeKey].emit(nodeKey + '.addDone.' + _id, to_send) + ipc.of[nodeKey].emit(nodeKey + '.addDone.' + _id, toSend) }) } @@ -65,8 +65,8 @@ function webtorrent (args) { } function callback () { - const to_send = {} - ipc.of[nodeKey].emit(nodeKey + '.removeDone.' + _id, to_send) + const toSend = {} + ipc.of[nodeKey].emit(nodeKey + '.removeDone.' + _id, toSend) } } -- cgit v1.2.3