]>
Commit | Line | Data |
---|---|---|
9f10b292 | 1 | 'use strict' |
8c308c2b | 2 | |
cbe2f7c3 C |
3 | const crypto = require('crypto') |
4 | ||
f0f5567b | 5 | const logger = require('./logger') |
8c308c2b | 6 | |
f0f5567b | 7 | const 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 |
16 | function badRequest (req, res, next) { |
17 | res.type('json').status(400).end() | |
18 | } | |
19 | ||
cbe2f7c3 C |
20 | function 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 | 28 | function cleanForExit (webtorrentProcess) { |
9f10b292 | 29 | logger.info('Gracefully exiting.') |
bc503c2a | 30 | process.kill(-webtorrentProcess.pid) |
9f10b292 | 31 | } |
0ae2e7f7 | 32 | |
e4c87ec2 C |
33 | function createEmptyCallback () { |
34 | return function (err) { | |
35 | if (err) logger.error('Error in empty callback.', { error: err }) | |
36 | } | |
37 | } | |
38 | ||
441b66f8 C |
39 | function isTestInstance () { |
40 | return (process.env.NODE_ENV === 'test') | |
41 | } | |
42 | ||
55fa55a9 C |
43 | function 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 | 58 | module.exports = utils |