]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/utils.js
Update README
[github/Chocobozzz/PeerTube.git] / server / helpers / utils.js
CommitLineData
9f10b292 1'use strict'
8c308c2b 2
cbe2f7c3
C
3const crypto = require('crypto')
4
f0f5567b 5const logger = require('./logger')
8c308c2b 6
f0f5567b 7const utils = {
a6fd2b30 8 badRequest,
e4c87ec2 9 createEmptyCallback,
c4403b29 10 cleanForExit,
441b66f8 11 generateRandomString,
55fa55a9 12 isTestInstance,
4df023f2 13 getFormatedObjects
cbe2f7c3
C
14}
15
a6fd2b30
C
16function badRequest (req, res, next) {
17 res.type('json').status(400).end()
18}
19
cbe2f7c3
C
20function generateRandomString (size, callback) {
21 crypto.pseudoRandomBytes(size, function (err, raw) {
22 if (err) return callback(err)
23
24 callback(null, raw.toString('hex'))
25 })
9f10b292 26}
8c308c2b 27
bc503c2a 28function cleanForExit (webtorrentProcess) {
9f10b292 29 logger.info('Gracefully exiting.')
bc503c2a 30 process.kill(-webtorrentProcess.pid)
9f10b292 31}
0ae2e7f7 32
e4c87ec2
C
33function createEmptyCallback () {
34 return function (err) {
35 if (err) logger.error('Error in empty callback.', { error: err })
36 }
37}
38
441b66f8
C
39function isTestInstance () {
40 return (process.env.NODE_ENV === 'test')
41}
42
55fa55a9
C
43function getFormatedObjects (objects, objectsTotal) {
44 const formatedObjects = []
45
46 objects.forEach(function (object) {
47 formatedObjects.push(object.toFormatedJSON())
48 })
49
50 return {
51 total: objectsTotal,
52 data: formatedObjects
53 }
54}
55
9f10b292 56// ---------------------------------------------------------------------------
c45f7f84 57
9f10b292 58module.exports = utils