diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-05-15 22:22:03 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-05-20 09:57:40 +0200 |
commit | 65fcc3119c334b75dd13bcfdebf186afdc580a8f (patch) | |
tree | 4f2158c61a9b7c3f47cfa233d01413b946ee53c0 /server/helpers/utils.ts | |
parent | d5f345ed4cfac4e1fa84dcb4fce1cda4d32f9c73 (diff) | |
download | PeerTube-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.ts | 54 |
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 @@ | |||
1 | import { pseudoRandomBytes } from 'crypto' | ||
2 | |||
3 | import { logger } from './logger' | ||
4 | |||
5 | function badRequest (req, res, next) { | ||
6 | res.type('json').status(400).end() | ||
7 | } | ||
8 | |||
9 | function 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 | |||
17 | function cleanForExit (webtorrentProcess) { | ||
18 | logger.info('Gracefully exiting.') | ||
19 | process.kill(-webtorrentProcess.pid) | ||
20 | } | ||
21 | |||
22 | function createEmptyCallback () { | ||
23 | return function (err) { | ||
24 | if (err) logger.error('Error in empty callback.', { error: err }) | ||
25 | } | ||
26 | } | ||
27 | |||
28 | function isTestInstance () { | ||
29 | return (process.env.NODE_ENV === 'test') | ||
30 | } | ||
31 | |||
32 | function 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 | |||
47 | export { | ||
48 | badRequest, | ||
49 | createEmptyCallback, | ||
50 | cleanForExit, | ||
51 | generateRandomString, | ||
52 | isTestInstance, | ||
53 | getFormatedObjects | ||
54 | } | ||