diff options
Diffstat (limited to 'server/helpers/utils.ts')
-rw-r--r-- | server/helpers/utils.ts | 24 |
1 files changed, 7 insertions, 17 deletions
diff --git a/server/helpers/utils.ts b/server/helpers/utils.ts index bbf135fa1..e99a48393 100644 --- a/server/helpers/utils.ts +++ b/server/helpers/utils.ts | |||
@@ -1,25 +1,14 @@ | |||
1 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | 2 | ||
3 | import { pseudoRandomBytes } from 'crypto' | 3 | import { pseudoRandomBytesPromise } from './core-utils' |
4 | 4 | import { ResultList } from '../../shared' | |
5 | import { logger } from './logger' | ||
6 | 5 | ||
7 | function badRequest (req: express.Request, res: express.Response, next: express.NextFunction) { | 6 | function badRequest (req: express.Request, res: express.Response, next: express.NextFunction) { |
8 | res.type('json').status(400).end() | 7 | res.type('json').status(400).end() |
9 | } | 8 | } |
10 | 9 | ||
11 | function generateRandomString (size: number, callback: (err: Error, randomString?: string) => void) { | 10 | function generateRandomString (size: number) { |
12 | pseudoRandomBytes(size, function (err, raw) { | 11 | return pseudoRandomBytesPromise(size).then(raw => raw.toString('hex')) |
13 | if (err) return callback(err) | ||
14 | |||
15 | callback(null, raw.toString('hex')) | ||
16 | }) | ||
17 | } | ||
18 | |||
19 | function createEmptyCallback () { | ||
20 | return function (err) { | ||
21 | if (err) logger.error('Error in empty callback.', { error: err }) | ||
22 | } | ||
23 | } | 12 | } |
24 | 13 | ||
25 | interface FormatableToJSON { | 14 | interface FormatableToJSON { |
@@ -33,17 +22,18 @@ function getFormatedObjects<U, T extends FormatableToJSON> (objects: T[], object | |||
33 | formatedObjects.push(object.toFormatedJSON()) | 22 | formatedObjects.push(object.toFormatedJSON()) |
34 | }) | 23 | }) |
35 | 24 | ||
36 | return { | 25 | const res: ResultList<U> = { |
37 | total: objectsTotal, | 26 | total: objectsTotal, |
38 | data: formatedObjects | 27 | data: formatedObjects |
39 | } | 28 | } |
29 | |||
30 | return res | ||
40 | } | 31 | } |
41 | 32 | ||
42 | // --------------------------------------------------------------------------- | 33 | // --------------------------------------------------------------------------- |
43 | 34 | ||
44 | export { | 35 | export { |
45 | badRequest, | 36 | badRequest, |
46 | createEmptyCallback, | ||
47 | generateRandomString, | 37 | generateRandomString, |
48 | getFormatedObjects | 38 | getFormatedObjects |
49 | } | 39 | } |