]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/utils.ts
First typescript iteration
[github/Chocobozzz/PeerTube.git] / server / helpers / utils.ts
1 import { pseudoRandomBytes } from 'crypto'
2
3 import { logger } from './logger'
4
5 function badRequest (req, res, next) {
6 res.type('json').status(400).end()
7 }
8
9 function generateRandomString (size, callback) {
10 pseudoRandomBytes(size, function (err, raw) {
11 if (err) return callback(err)
12
13 callback(null, raw.toString('hex'))
14 })
15 }
16
17 function cleanForExit (webtorrentProcess) {
18 logger.info('Gracefully exiting.')
19 process.kill(-webtorrentProcess.pid)
20 }
21
22 function createEmptyCallback () {
23 return function (err) {
24 if (err) logger.error('Error in empty callback.', { error: err })
25 }
26 }
27
28 function isTestInstance () {
29 return (process.env.NODE_ENV === 'test')
30 }
31
32 function getFormatedObjects (objects, objectsTotal) {
33 const formatedObjects = []
34
35 objects.forEach(function (object) {
36 formatedObjects.push(object.toFormatedJSON())
37 })
38
39 return {
40 total: objectsTotal,
41 data: formatedObjects
42 }
43 }
44
45 // ---------------------------------------------------------------------------
46
47 export {
48 badRequest,
49 createEmptyCallback,
50 cleanForExit,
51 generateRandomString,
52 isTestInstance,
53 getFormatedObjects
54 }