]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/utils.ts
Type functions
[github/Chocobozzz/PeerTube.git] / server / helpers / utils.ts
1 import * as express from 'express'
2
3 import { pseudoRandomBytes } from 'crypto'
4 import { join } from 'path'
5
6 import { logger } from './logger'
7
8 function badRequest (req: express.Request, res: express.Response, next: express.NextFunction) {
9 res.type('json').status(400).end()
10 }
11
12 function generateRandomString (size: number, callback: (err: Error, randomString?: string) => void) {
13 pseudoRandomBytes(size, function (err, raw) {
14 if (err) return callback(err)
15
16 callback(null, raw.toString('hex'))
17 })
18 }
19
20 function createEmptyCallback () {
21 return function (err) {
22 if (err) logger.error('Error in empty callback.', { error: err })
23 }
24 }
25
26 function isTestInstance () {
27 return process.env.NODE_ENV === 'test'
28 }
29
30 function getFormatedObjects (objects: any[], objectsTotal: number) {
31 const formatedObjects = []
32
33 objects.forEach(function (object) {
34 formatedObjects.push(object.toFormatedJSON())
35 })
36
37 return {
38 total: objectsTotal,
39 data: formatedObjects
40 }
41 }
42
43 function root () {
44 // We are in /dist/helpers/utils.js
45 return join(__dirname, '..', '..', '..')
46 }
47
48 // ---------------------------------------------------------------------------
49
50 export {
51 badRequest,
52 createEmptyCallback,
53 generateRandomString,
54 isTestInstance,
55 getFormatedObjects,
56 root
57 }