aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/utils.ts
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-05-15 22:22:03 +0200
committerChocobozzz <florian.bigard@gmail.com>2017-05-20 09:57:40 +0200
commit65fcc3119c334b75dd13bcfdebf186afdc580a8f (patch)
tree4f2158c61a9b7c3f47cfa233d01413b946ee53c0 /server/helpers/utils.ts
parentd5f345ed4cfac4e1fa84dcb4fce1cda4d32f9c73 (diff)
downloadPeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.tar.gz
PeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.tar.zst
PeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.zip
First typescript iteration
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}