-import { pseudoRandomBytes } from 'crypto'
-import { join } from 'path'
+import * as express from 'express'
+import * as Promise from 'bluebird'
-import { logger } from './logger'
+import { pseudoRandomBytesPromise } from './core-utils'
+import { CONFIG, database as db } from '../initializers'
+import { ResultList } from '../../shared'
-function badRequest (req, res, next) {
+function badRequest (req: express.Request, res: express.Response, next: express.NextFunction) {
res.type('json').status(400).end()
}
-function generateRandomString (size, callback) {
- pseudoRandomBytes(size, function (err, raw) {
- if (err) return callback(err)
-
- callback(null, raw.toString('hex'))
- })
-}
-
-function cleanForExit (webtorrentProcess) {
- logger.info('Gracefully exiting.')
- process.kill(-webtorrentProcess.pid)
-}
-
-function createEmptyCallback () {
- return function (err) {
- if (err) logger.error('Error in empty callback.', { error: err })
- }
+function generateRandomString (size: number) {
+ return pseudoRandomBytesPromise(size).then(raw => raw.toString('hex'))
}
-function isTestInstance () {
- return (process.env.NODE_ENV === 'test')
+interface FormatableToJSON {
+ toFormattedJSON ()
}
-function getFormatedObjects (objects, objectsTotal) {
- const formatedObjects = []
+function getFormattedObjects<U, T extends FormatableToJSON> (objects: T[], objectsTotal: number) {
+ const formattedObjects: U[] = []
- objects.forEach(function (object) {
- formatedObjects.push(object.toFormatedJSON())
+ objects.forEach(object => {
+ formattedObjects.push(object.toFormattedJSON())
})
- return {
+ const res: ResultList<U> = {
total: objectsTotal,
- data: formatedObjects
+ data: formattedObjects
}
+
+ return res
}
-function root () {
- // We are in /dist/helpers/utils.js
- return join(__dirname, '..', '..', '..')
+function isSignupAllowed () {
+ if (CONFIG.SIGNUP.ENABLED === false) {
+ return Promise.resolve(false)
+ }
+
+ // No limit and signup is enabled
+ if (CONFIG.SIGNUP.LIMIT === -1) {
+ return Promise.resolve(true)
+ }
+
+ return db.User.countTotal().then(totalUsers => {
+ return totalUsers < CONFIG.SIGNUP.LIMIT
+ })
}
+type SortType = { sortModel: any, sortValue: string }
+
// ---------------------------------------------------------------------------
export {
badRequest,
- createEmptyCallback,
- cleanForExit,
generateRandomString,
- isTestInstance,
- getFormatedObjects,
- root
+ getFormattedObjects,
+ isSignupAllowed,
+ SortType
}