aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-10-25 16:03:33 +0200
committerChocobozzz <florian.bigard@gmail.com>2017-10-26 09:11:38 +0200
commitf5028693a896a3076dd286ac0030e3d8f78f5ebf (patch)
tree09144ed6357e49ea575fb110247f933283ad235e /server/helpers
parenteb08047657e739bcd9e592d76307befa3998482b (diff)
downloadPeerTube-f5028693a896a3076dd286ac0030e3d8f78f5ebf.tar.gz
PeerTube-f5028693a896a3076dd286ac0030e3d8f78f5ebf.tar.zst
PeerTube-f5028693a896a3076dd286ac0030e3d8f78f5ebf.zip
Use async/await in lib and initializers
Diffstat (limited to 'server/helpers')
-rw-r--r--server/helpers/peertube-crypto.ts103
-rw-r--r--server/helpers/requests.ts2
-rw-r--r--server/helpers/utils.ts20
3 files changed, 60 insertions, 65 deletions
diff --git a/server/helpers/peertube-crypto.ts b/server/helpers/peertube-crypto.ts
index 89aef99c4..47f0243e7 100644
--- a/server/helpers/peertube-crypto.ts
+++ b/server/helpers/peertube-crypto.ts
@@ -1,5 +1,4 @@
1import * as crypto from 'crypto' 1import * as crypto from 'crypto'
2import * as Promise from 'bluebird'
3import { join } from 'path' 2import { join } from 'path'
4 3
5import { 4import {
@@ -41,7 +40,7 @@ function checkSignature (publicKey: string, data: string, hexSignature: string)
41 return isValid 40 return isValid
42} 41}
43 42
44function sign (data: string|Object) { 43async function sign (data: string|Object) {
45 const sign = crypto.createSign(SIGNATURE_ALGORITHM) 44 const sign = crypto.createSign(SIGNATURE_ALGORITHM)
46 45
47 let dataString: string 46 let dataString: string
@@ -52,33 +51,33 @@ function sign (data: string|Object) {
52 dataString = JSON.stringify(data) 51 dataString = JSON.stringify(data)
53 } catch (err) { 52 } catch (err) {
54 logger.error('Cannot sign data.', err) 53 logger.error('Cannot sign data.', err)
55 return Promise.resolve('') 54 return ''
56 } 55 }
57 } 56 }
58 57
59 sign.update(dataString, 'utf8') 58 sign.update(dataString, 'utf8')
60 59
61 return getMyPrivateCert().then(myKey => { 60 const myKey = await getMyPrivateCert()
62 return sign.sign(myKey, SIGNATURE_ENCODING) 61 return await sign.sign(myKey, SIGNATURE_ENCODING)
63 })
64} 62}
65 63
66function comparePassword (plainPassword: string, hashPassword: string) { 64function comparePassword (plainPassword: string, hashPassword: string) {
67 return bcryptComparePromise(plainPassword, hashPassword) 65 return bcryptComparePromise(plainPassword, hashPassword)
68} 66}
69 67
70function createCertsIfNotExist () { 68async function createCertsIfNotExist () {
71 return certsExist().then(exist => { 69 const exist = await certsExist()
72 if (exist === true) { 70 if (exist === true) {
73 return undefined 71 return undefined
74 } 72 }
75 73
76 return createCerts() 74 return await createCerts()
77 })
78} 75}
79 76
80function cryptPassword (password: string) { 77async function cryptPassword (password: string) {
81 return bcryptGenSaltPromise(BCRYPT_SALT_SIZE).then(salt => bcryptHashPromise(password, salt)) 78 const salt = await bcryptGenSaltPromise(BCRYPT_SALT_SIZE)
79
80 return await bcryptHashPromise(password, salt)
82} 81}
83 82
84function getMyPrivateCert () { 83function getMyPrivateCert () {
@@ -105,51 +104,45 @@ export {
105 104
106// --------------------------------------------------------------------------- 105// ---------------------------------------------------------------------------
107 106
108function certsExist () { 107async function certsExist () {
109 const certPath = join(CONFIG.STORAGE.CERT_DIR, PRIVATE_CERT_NAME) 108 const certPath = join(CONFIG.STORAGE.CERT_DIR, PRIVATE_CERT_NAME)
110 109
111 // If there is an error the certificates do not exist 110 // If there is an error the certificates do not exist
112 return accessPromise(certPath) 111 try {
113 .then(() => true) 112 await accessPromise(certPath)
114 .catch(() => false) 113
114 return true
115 } catch {
116 return false
117 }
115} 118}
116 119
117function createCerts () { 120async function createCerts () {
118 return certsExist().then(exist => { 121 const exist = await certsExist()
119 if (exist === true) { 122 if (exist === true) {
120 const errorMessage = 'Certs already exist.' 123 const errorMessage = 'Certs already exist.'
121 logger.warning(errorMessage) 124 logger.warning(errorMessage)
122 throw new Error(errorMessage) 125 throw new Error(errorMessage)
123 } 126 }
124 127
125 logger.info('Generating a RSA key...') 128 logger.info('Generating a RSA key...')
126 129
127 const privateCertPath = join(CONFIG.STORAGE.CERT_DIR, PRIVATE_CERT_NAME) 130 const privateCertPath = join(CONFIG.STORAGE.CERT_DIR, PRIVATE_CERT_NAME)
128 const genRsaOptions = { 131 const genRsaOptions = {
129 'out': privateCertPath, 132 'out': privateCertPath,
130 '2048': false 133 '2048': false
131 } 134 }
132 return opensslExecPromise('genrsa', genRsaOptions) 135
133 .then(() => { 136 await opensslExecPromise('genrsa', genRsaOptions)
134 logger.info('RSA key generated.') 137 logger.info('RSA key generated.')
135 logger.info('Managing public key...') 138 logger.info('Managing public key...')
136 139
137 const publicCertPath = join(CONFIG.STORAGE.CERT_DIR, 'peertube.pub') 140 const publicCertPath = join(CONFIG.STORAGE.CERT_DIR, 'peertube.pub')
138 const rsaOptions = { 141 const rsaOptions = {
139 'in': privateCertPath, 142 'in': privateCertPath,
140 'pubout': true, 143 'pubout': true,
141 'out': publicCertPath 144 'out': publicCertPath
142 } 145 }
143 return opensslExecPromise('rsa', rsaOptions) 146
144 .then(() => logger.info('Public key managed.')) 147 await opensslExecPromise('rsa', rsaOptions)
145 .catch(err => {
146 logger.error('Cannot create public key on this pod.')
147 throw err
148 })
149 })
150 .catch(err => {
151 logger.error('Cannot create private key on this pod.')
152 throw err
153 })
154 })
155} 148}
diff --git a/server/helpers/requests.ts b/server/helpers/requests.ts
index d67d46044..8c5d848f3 100644
--- a/server/helpers/requests.ts
+++ b/server/helpers/requests.ts
@@ -73,7 +73,7 @@ function makeSecureRequest (params: MakeSecureRequestParams) {
73 signature 73 signature
74 } 74 }
75 75
76 // If there are data informations 76 // If there are data information
77 if (params.data) { 77 if (params.data) {
78 requestParams.json.data = params.data 78 requestParams.json.data = params.data
79 } 79 }
diff --git a/server/helpers/utils.ts b/server/helpers/utils.ts
index 6cabe117c..8b81a61e1 100644
--- a/server/helpers/utils.ts
+++ b/server/helpers/utils.ts
@@ -8,11 +8,13 @@ import { ResultList } from '../../shared'
8import { VideoResolution } from '../../shared/models/videos/video-resolution.enum' 8import { VideoResolution } from '../../shared/models/videos/video-resolution.enum'
9 9
10function badRequest (req: express.Request, res: express.Response, next: express.NextFunction) { 10function badRequest (req: express.Request, res: express.Response, next: express.NextFunction) {
11 res.type('json').status(400).end() 11 return res.type('json').status(400).end()
12} 12}
13 13
14function generateRandomString (size: number) { 14async function generateRandomString (size: number) {
15 return pseudoRandomBytesPromise(size).then(raw => raw.toString('hex')) 15 const raw = await pseudoRandomBytesPromise(size)
16
17 return raw.toString('hex')
16} 18}
17 19
18interface FormattableToJSON { 20interface FormattableToJSON {
@@ -34,19 +36,19 @@ function getFormattedObjects<U, T extends FormattableToJSON> (objects: T[], obje
34 return res 36 return res
35} 37}
36 38
37function isSignupAllowed () { 39async function isSignupAllowed () {
38 if (CONFIG.SIGNUP.ENABLED === false) { 40 if (CONFIG.SIGNUP.ENABLED === false) {
39 return Promise.resolve(false) 41 return false
40 } 42 }
41 43
42 // No limit and signup is enabled 44 // No limit and signup is enabled
43 if (CONFIG.SIGNUP.LIMIT === -1) { 45 if (CONFIG.SIGNUP.LIMIT === -1) {
44 return Promise.resolve(true) 46 return true
45 } 47 }
46 48
47 return db.User.countTotal().then(totalUsers => { 49 const totalUsers = await db.User.countTotal()
48 return totalUsers < CONFIG.SIGNUP.LIMIT 50
49 }) 51 return totalUsers < CONFIG.SIGNUP.LIMIT
50} 52}
51 53
52function computeResolutionsToTranscode (videoFileHeight: number) { 54function computeResolutionsToTranscode (videoFileHeight: number) {