aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/utils.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/helpers/utils.ts')
-rw-r--r--server/helpers/utils.ts54
1 files changed, 54 insertions, 0 deletions
diff --git a/server/helpers/utils.ts b/server/helpers/utils.ts
new file mode 100644
index 000000000..09c35a533
--- /dev/null
+++ b/server/helpers/utils.ts
@@ -0,0 +1,54 @@
1import { pseudoRandomBytes } from 'crypto'
2
3import { logger } from './logger'
4
5function badRequest (req, res, next) {
6 res.type('json').status(400).end()
7}
8
9function 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
17function cleanForExit (webtorrentProcess) {
18 logger.info('Gracefully exiting.')
19 process.kill(-webtorrentProcess.pid)
20}
21
22function createEmptyCallback () {
23 return function (err) {
24 if (err) logger.error('Error in empty callback.', { error: err })
25 }
26}
27
28function isTestInstance () {
29 return (process.env.NODE_ENV === 'test')
30}
31
32function 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
47export {
48 badRequest,
49 createEmptyCallback,
50 cleanForExit,
51 generateRandomString,
52 isTestInstance,
53 getFormatedObjects
54}