1 import { pseudoRandomBytes } from 'crypto'
3 import { logger } from './logger'
5 function badRequest (req, res, next) {
6 res.type('json').status(400).end()
9 function generateRandomString (size, callback) {
10 pseudoRandomBytes(size, function (err, raw) {
11 if (err) return callback(err)
13 callback(null, raw.toString('hex'))
17 function cleanForExit (webtorrentProcess) {
18 logger.info('Gracefully exiting.')
19 process.kill(-webtorrentProcess.pid)
22 function createEmptyCallback () {
23 return function (err) {
24 if (err) logger.error('Error in empty callback.', { error: err })
28 function isTestInstance () {
29 return (process.env.NODE_ENV === 'test')
32 function getFormatedObjects (objects, objectsTotal) {
33 const formatedObjects = []
35 objects.forEach(function (object) {
36 formatedObjects.push(object.toFormatedJSON())
45 // ---------------------------------------------------------------------------