aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers
diff options
context:
space:
mode:
Diffstat (limited to 'server/helpers')
-rw-r--r--server/helpers/custom-validators/misc.ts13
-rw-r--r--server/helpers/custom-validators/pods.ts17
-rw-r--r--server/helpers/custom-validators/remote/index.ts2
-rw-r--r--server/helpers/custom-validators/remote/videos.ts32
-rw-r--r--server/helpers/custom-validators/users.ts24
-rw-r--r--server/helpers/custom-validators/videos.ts110
-rw-r--r--server/helpers/database-utils.ts13
-rw-r--r--server/helpers/peertube-crypto.ts26
-rw-r--r--server/helpers/requests.ts19
-rw-r--r--server/helpers/utils.ts16
10 files changed, 176 insertions, 96 deletions
diff --git a/server/helpers/custom-validators/misc.ts b/server/helpers/custom-validators/misc.ts
index 83f50a7fe..b1291ba7a 100644
--- a/server/helpers/custom-validators/misc.ts
+++ b/server/helpers/custom-validators/misc.ts
@@ -1,8 +1,8 @@
1function exists (value) { 1function exists (value: any) {
2 return value !== undefined && value !== null 2 return value !== undefined && value !== null
3} 3}
4 4
5function isArray (value) { 5function isArray (value: any) {
6 return Array.isArray(value) 6 return Array.isArray(value)
7} 7}
8 8
@@ -12,3 +12,12 @@ export {
12 exists, 12 exists,
13 isArray 13 isArray
14} 14}
15
16declare global {
17 namespace ExpressValidator {
18 export interface Validator {
19 exists,
20 isArray
21 }
22 }
23}
diff --git a/server/helpers/custom-validators/pods.ts b/server/helpers/custom-validators/pods.ts
index ee939ad04..ec9f26cc8 100644
--- a/server/helpers/custom-validators/pods.ts
+++ b/server/helpers/custom-validators/pods.ts
@@ -1,12 +1,12 @@
1import * as validator from 'validator' 1import * as validator from 'validator'
2 2
3import { isArray } from './misc' 3import { isArray, exists } from './misc'
4 4
5function isHostValid (host) { 5function isHostValid (host: string) {
6 return validator.isURL(host) && host.split('://').length === 1 6 return exists(host) && validator.isURL(host) && host.split('://').length === 1
7} 7}
8 8
9function isEachUniqueHostValid (hosts) { 9function isEachUniqueHostValid (hosts: string[]) {
10 return isArray(hosts) && 10 return isArray(hosts) &&
11 hosts.length !== 0 && 11 hosts.length !== 0 &&
12 hosts.every(function (host) { 12 hosts.every(function (host) {
@@ -20,3 +20,12 @@ export {
20 isEachUniqueHostValid, 20 isEachUniqueHostValid,
21 isHostValid 21 isHostValid
22} 22}
23
24declare global {
25 namespace ExpressValidator {
26 export interface Validator {
27 isEachUniqueHostValid
28 isHostValid
29 }
30 }
31}
diff --git a/server/helpers/custom-validators/remote/index.ts b/server/helpers/custom-validators/remote/index.ts
index d6f9a7e77..e29a9b767 100644
--- a/server/helpers/custom-validators/remote/index.ts
+++ b/server/helpers/custom-validators/remote/index.ts
@@ -1 +1 @@
export * from './videos'; export * from './videos'
diff --git a/server/helpers/custom-validators/remote/videos.ts b/server/helpers/custom-validators/remote/videos.ts
index 4b904d011..1df7316aa 100644
--- a/server/helpers/custom-validators/remote/videos.ts
+++ b/server/helpers/custom-validators/remote/videos.ts
@@ -31,7 +31,7 @@ import {
31 31
32const ENDPOINT_ACTIONS = REQUEST_ENDPOINT_ACTIONS[REQUEST_ENDPOINTS.VIDEOS] 32const ENDPOINT_ACTIONS = REQUEST_ENDPOINT_ACTIONS[REQUEST_ENDPOINTS.VIDEOS]
33 33
34function isEachRemoteRequestVideosValid (requests) { 34function isEachRemoteRequestVideosValid (requests: any[]) {
35 return isArray(requests) && 35 return isArray(requests) &&
36 requests.every(function (request) { 36 requests.every(function (request) {
37 const video = request.data 37 const video = request.data
@@ -61,7 +61,7 @@ function isEachRemoteRequestVideosValid (requests) {
61 }) 61 })
62} 62}
63 63
64function isEachRemoteRequestVideosQaduValid (requests) { 64function isEachRemoteRequestVideosQaduValid (requests: any[]) {
65 return isArray(requests) && 65 return isArray(requests) &&
66 requests.every(function (request) { 66 requests.every(function (request) {
67 const video = request.data 67 const video = request.data
@@ -70,14 +70,14 @@ function isEachRemoteRequestVideosQaduValid (requests) {
70 70
71 return ( 71 return (
72 isVideoRemoteIdValid(video.remoteId) && 72 isVideoRemoteIdValid(video.remoteId) &&
73 (has(video, 'views') === false || isVideoViewsValid) && 73 (has(video, 'views') === false || isVideoViewsValid(video.views)) &&
74 (has(video, 'likes') === false || isVideoLikesValid) && 74 (has(video, 'likes') === false || isVideoLikesValid(video.likes)) &&
75 (has(video, 'dislikes') === false || isVideoDislikesValid) 75 (has(video, 'dislikes') === false || isVideoDislikesValid(video.dislikes))
76 ) 76 )
77 }) 77 })
78} 78}
79 79
80function isEachRemoteRequestVideosEventsValid (requests) { 80function isEachRemoteRequestVideosEventsValid (requests: any[]) {
81 return isArray(requests) && 81 return isArray(requests) &&
82 requests.every(function (request) { 82 requests.every(function (request) {
83 const eventData = request.data 83 const eventData = request.data
@@ -100,9 +100,19 @@ export {
100 isEachRemoteRequestVideosEventsValid 100 isEachRemoteRequestVideosEventsValid
101} 101}
102 102
103declare global {
104 namespace ExpressValidator {
105 export interface Validator {
106 isEachRemoteRequestVideosValid,
107 isEachRemoteRequestVideosQaduValid,
108 isEachRemoteRequestVideosEventsValid
109 }
110 }
111}
112
103// --------------------------------------------------------------------------- 113// ---------------------------------------------------------------------------
104 114
105function isCommonVideoAttributesValid (video) { 115function isCommonVideoAttributesValid (video: any) {
106 return isVideoDateValid(video.createdAt) && 116 return isVideoDateValid(video.createdAt) &&
107 isVideoDateValid(video.updatedAt) && 117 isVideoDateValid(video.updatedAt) &&
108 isVideoCategoryValid(video.category) && 118 isVideoCategoryValid(video.category) &&
@@ -121,18 +131,18 @@ function isCommonVideoAttributesValid (video) {
121 isVideoDislikesValid(video.dislikes) 131 isVideoDislikesValid(video.dislikes)
122} 132}
123 133
124function isRequestTypeAddValid (value) { 134function isRequestTypeAddValid (value: string) {
125 return value === ENDPOINT_ACTIONS.ADD 135 return value === ENDPOINT_ACTIONS.ADD
126} 136}
127 137
128function isRequestTypeUpdateValid (value) { 138function isRequestTypeUpdateValid (value: string) {
129 return value === ENDPOINT_ACTIONS.UPDATE 139 return value === ENDPOINT_ACTIONS.UPDATE
130} 140}
131 141
132function isRequestTypeRemoveValid (value) { 142function isRequestTypeRemoveValid (value: string) {
133 return value === ENDPOINT_ACTIONS.REMOVE 143 return value === ENDPOINT_ACTIONS.REMOVE
134} 144}
135 145
136function isRequestTypeReportAbuseValid (value) { 146function isRequestTypeReportAbuseValid (value: string) {
137 return value === ENDPOINT_ACTIONS.REPORT_ABUSE 147 return value === ENDPOINT_ACTIONS.REPORT_ABUSE
138} 148}
diff --git a/server/helpers/custom-validators/users.ts b/server/helpers/custom-validators/users.ts
index f303ab8db..7792ffd74 100644
--- a/server/helpers/custom-validators/users.ts
+++ b/server/helpers/custom-validators/users.ts
@@ -1,25 +1,26 @@
1import { values } from 'lodash' 1import { values } from 'lodash'
2import * as validator from 'validator' 2import * as validator from 'validator'
3 3
4import { exists } from './misc'
4import { CONSTRAINTS_FIELDS, USER_ROLES } from '../../initializers' 5import { CONSTRAINTS_FIELDS, USER_ROLES } from '../../initializers'
5const USERS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.USERS 6const USERS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.USERS
6 7
7function isUserPasswordValid (value) { 8function isUserPasswordValid (value: string) {
8 return validator.isLength(value, USERS_CONSTRAINTS_FIELDS.PASSWORD) 9 return validator.isLength(value, USERS_CONSTRAINTS_FIELDS.PASSWORD)
9} 10}
10 11
11function isUserRoleValid (value) { 12function isUserRoleValid (value: string) {
12 return values(USER_ROLES).indexOf(value) !== -1 13 return values(USER_ROLES).indexOf(value) !== -1
13} 14}
14 15
15function isUserUsernameValid (value) { 16function isUserUsernameValid (value: string) {
16 const max = USERS_CONSTRAINTS_FIELDS.USERNAME.max 17 const max = USERS_CONSTRAINTS_FIELDS.USERNAME.max
17 const min = USERS_CONSTRAINTS_FIELDS.USERNAME.min 18 const min = USERS_CONSTRAINTS_FIELDS.USERNAME.min
18 return validator.matches(value, new RegExp(`^[a-zA-Z0-9._]{${min},${max}}$`)) 19 return exists(value) && validator.matches(value, new RegExp(`^[a-zA-Z0-9._]{${min},${max}}$`))
19} 20}
20 21
21function isUserDisplayNSFWValid (value) { 22function isUserDisplayNSFWValid (value: any) {
22 return validator.isBoolean(value) 23 return typeof value === 'boolean' || (typeof value === 'string' && validator.isBoolean(value))
23} 24}
24 25
25// --------------------------------------------------------------------------- 26// ---------------------------------------------------------------------------
@@ -30,3 +31,14 @@ export {
30 isUserUsernameValid, 31 isUserUsernameValid,
31 isUserDisplayNSFWValid 32 isUserDisplayNSFWValid
32} 33}
34
35declare global {
36 namespace ExpressValidator {
37 export interface Validator {
38 isUserPasswordValid,
39 isUserRoleValid,
40 isUserUsernameValid,
41 isUserDisplayNSFWValid
42 }
43 }
44}
diff --git a/server/helpers/custom-validators/videos.ts b/server/helpers/custom-validators/videos.ts
index 6389998e1..c5ef4cb5f 100644
--- a/server/helpers/custom-validators/videos.ts
+++ b/server/helpers/custom-validators/videos.ts
@@ -9,105 +9,105 @@ import {
9 VIDEO_RATE_TYPES 9 VIDEO_RATE_TYPES
10} from '../../initializers' 10} from '../../initializers'
11import { isUserUsernameValid } from './users' 11import { isUserUsernameValid } from './users'
12import { isArray } from './misc' 12import { isArray, exists } from './misc'
13 13
14const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS 14const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS
15const VIDEO_ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_ABUSES 15const VIDEO_ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_ABUSES
16const VIDEO_EVENTS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_EVENTS 16const VIDEO_EVENTS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_EVENTS
17 17
18function isVideoAuthorValid (value) { 18function isVideoAuthorValid (value: string) {
19 return isUserUsernameValid(value) 19 return isUserUsernameValid(value)
20} 20}
21 21
22function isVideoDateValid (value) { 22function isVideoDateValid (value: string) {
23 return validator.isDate(value) 23 return exists(value) && validator.isISO8601(value)
24} 24}
25 25
26function isVideoCategoryValid (value) { 26function isVideoCategoryValid (value: number) {
27 return VIDEO_CATEGORIES[value] !== undefined 27 return VIDEO_CATEGORIES[value] !== undefined
28} 28}
29 29
30function isVideoLicenceValid (value) { 30function isVideoLicenceValid (value: number) {
31 return VIDEO_LICENCES[value] !== undefined 31 return VIDEO_LICENCES[value] !== undefined
32} 32}
33 33
34function isVideoLanguageValid (value) { 34function isVideoLanguageValid (value: number) {
35 return value === null || VIDEO_LANGUAGES[value] !== undefined 35 return value === null || VIDEO_LANGUAGES[value] !== undefined
36} 36}
37 37
38function isVideoNSFWValid (value) { 38function isVideoNSFWValid (value: any) {
39 return validator.isBoolean(value) 39 return typeof value === 'boolean' || (typeof value === 'string' && validator.isBoolean(value))
40} 40}
41 41
42function isVideoDescriptionValid (value) { 42function isVideoDescriptionValid (value: string) {
43 return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.DESCRIPTION) 43 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.DESCRIPTION)
44} 44}
45 45
46function isVideoDurationValid (value) { 46function isVideoDurationValid (value: string) {
47 return validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DURATION) 47 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DURATION)
48} 48}
49 49
50function isVideoExtnameValid (value) { 50function isVideoExtnameValid (value: string) {
51 return VIDEOS_CONSTRAINTS_FIELDS.EXTNAME.indexOf(value) !== -1 51 return VIDEOS_CONSTRAINTS_FIELDS.EXTNAME.indexOf(value) !== -1
52} 52}
53 53
54function isVideoInfoHashValid (value) { 54function isVideoInfoHashValid (value: string) {
55 return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.INFO_HASH) 55 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.INFO_HASH)
56} 56}
57 57
58function isVideoNameValid (value) { 58function isVideoNameValid (value: string) {
59 return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.NAME) 59 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.NAME)
60} 60}
61 61
62function isVideoTagsValid (tags) { 62function isVideoTagsValid (tags: string[]) {
63 return isArray(tags) && 63 return isArray(tags) &&
64 validator.isInt(tags.length, VIDEOS_CONSTRAINTS_FIELDS.TAGS) && 64 validator.isInt(tags.length.toString(), VIDEOS_CONSTRAINTS_FIELDS.TAGS) &&
65 tags.every(function (tag) { 65 tags.every(function (tag) {
66 return validator.isLength(tag, VIDEOS_CONSTRAINTS_FIELDS.TAG) 66 return exists(tag) && validator.isLength(tag, VIDEOS_CONSTRAINTS_FIELDS.TAG)
67 }) 67 })
68} 68}
69 69
70function isVideoThumbnailValid (value) { 70function isVideoThumbnailValid (value: string) {
71 return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL) 71 return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL)
72} 72}
73 73
74function isVideoThumbnailDataValid (value) { 74function isVideoThumbnailDataValid (value: string) {
75 return validator.isByteLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL_DATA) 75 return exists(value) && validator.isByteLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL_DATA)
76} 76}
77 77
78function isVideoRemoteIdValid (value) { 78function isVideoRemoteIdValid (value: string) {
79 return validator.isUUID(value, 4) 79 return exists(value) && validator.isUUID(value, 4)
80} 80}
81 81
82function isVideoAbuseReasonValid (value) { 82function isVideoAbuseReasonValid (value: string) {
83 return validator.isLength(value, VIDEO_ABUSES_CONSTRAINTS_FIELDS.REASON) 83 return exists(value) && validator.isLength(value, VIDEO_ABUSES_CONSTRAINTS_FIELDS.REASON)
84} 84}
85 85
86function isVideoAbuseReporterUsernameValid (value) { 86function isVideoAbuseReporterUsernameValid (value: string) {
87 return isUserUsernameValid(value) 87 return isUserUsernameValid(value)
88} 88}
89 89
90function isVideoViewsValid (value) { 90function isVideoViewsValid (value: string) {
91 return validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.VIEWS) 91 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.VIEWS)
92} 92}
93 93
94function isVideoLikesValid (value) { 94function isVideoLikesValid (value: string) {
95 return validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.LIKES) 95 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.LIKES)
96} 96}
97 97
98function isVideoDislikesValid (value) { 98function isVideoDislikesValid (value: string) {
99 return validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DISLIKES) 99 return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DISLIKES)
100} 100}
101 101
102function isVideoEventCountValid (value) { 102function isVideoEventCountValid (value: string) {
103 return validator.isInt(value + '', VIDEO_EVENTS_CONSTRAINTS_FIELDS.COUNT) 103 return exists(value) && validator.isInt(value + '', VIDEO_EVENTS_CONSTRAINTS_FIELDS.COUNT)
104} 104}
105 105
106function isVideoRatingTypeValid (value) { 106function isVideoRatingTypeValid (value: string) {
107 return values(VIDEO_RATE_TYPES).indexOf(value) !== -1 107 return values(VIDEO_RATE_TYPES).indexOf(value) !== -1
108} 108}
109 109
110function isVideoFile (value, files) { 110function isVideoFile (value: string, files: { [ fieldname: string ]: Express.Multer.File[] }) {
111 // Should have files 111 // Should have files
112 if (!files) return false 112 if (!files) return false
113 113
@@ -149,3 +149,33 @@ export {
149 isVideoDislikesValid, 149 isVideoDislikesValid,
150 isVideoEventCountValid 150 isVideoEventCountValid
151} 151}
152
153declare global {
154 namespace ExpressValidator {
155 export interface Validator {
156 isVideoAuthorValid,
157 isVideoDateValid,
158 isVideoCategoryValid,
159 isVideoLicenceValid,
160 isVideoLanguageValid,
161 isVideoNSFWValid,
162 isVideoDescriptionValid,
163 isVideoDurationValid,
164 isVideoInfoHashValid,
165 isVideoNameValid,
166 isVideoTagsValid,
167 isVideoThumbnailValid,
168 isVideoThumbnailDataValid,
169 isVideoExtnameValid,
170 isVideoRemoteIdValid,
171 isVideoAbuseReasonValid,
172 isVideoAbuseReporterUsernameValid,
173 isVideoFile,
174 isVideoViewsValid,
175 isVideoLikesValid,
176 isVideoRatingTypeValid,
177 isVideoDislikesValid,
178 isVideoEventCountValid
179 }
180 }
181}
diff --git a/server/helpers/database-utils.ts b/server/helpers/database-utils.ts
index 4f49c5825..f8ee9a454 100644
--- a/server/helpers/database-utils.ts
+++ b/server/helpers/database-utils.ts
@@ -1,14 +1,15 @@
1import * as Sequelize from 'sequelize'
1// TODO: import from ES6 when retry typing file will include errorFilter function 2// TODO: import from ES6 when retry typing file will include errorFilter function
2import * as retry from 'async/retry' 3import * as retry from 'async/retry'
3 4
4import { database as db } from '../initializers/database' 5import { database as db } from '../initializers/database'
5import { logger } from './logger' 6import { logger } from './logger'
6 7
7function commitTransaction (t, callback) { 8function commitTransaction (t: Sequelize.Transaction, callback: (err: Error) => void) {
8 return t.commit().asCallback(callback) 9 return t.commit().asCallback(callback)
9} 10}
10 11
11function rollbackTransaction (err, t, callback) { 12function rollbackTransaction (err: Error, t: Sequelize.Transaction, callback: (err: Error) => void) {
12 // Try to rollback transaction 13 // Try to rollback transaction
13 if (t) { 14 if (t) {
14 // Do not catch err, report the original one 15 // Do not catch err, report the original one
@@ -20,8 +21,8 @@ function rollbackTransaction (err, t, callback) {
20 } 21 }
21} 22}
22 23
23// { arguments, errorMessage } 24type RetryTransactionWrapperOptions = { errorMessage: string, arguments?: any[] }
24function retryTransactionWrapper (functionToRetry, options, finalCallback) { 25function retryTransactionWrapper (functionToRetry: Function, options: RetryTransactionWrapperOptions, finalCallback: Function) {
25 const args = options.arguments ? options.arguments : [] 26 const args = options.arguments ? options.arguments : []
26 27
27 transactionRetryer( 28 transactionRetryer(
@@ -39,7 +40,7 @@ function retryTransactionWrapper (functionToRetry, options, finalCallback) {
39 ) 40 )
40} 41}
41 42
42function transactionRetryer (func, callback) { 43function transactionRetryer (func: Function, callback: (err: Error) => void) {
43 retry({ 44 retry({
44 times: 5, 45 times: 5,
45 46
@@ -51,7 +52,7 @@ function transactionRetryer (func, callback) {
51 }, func, callback) 52 }, func, callback)
52} 53}
53 54
54function startSerializableTransaction (callback) { 55function startSerializableTransaction (callback: (err: Error, t: Sequelize.Transaction) => void) {
55 db.sequelize.transaction(/* { isolationLevel: 'SERIALIZABLE' } */).asCallback(function (err, t) { 56 db.sequelize.transaction(/* { isolationLevel: 'SERIALIZABLE' } */).asCallback(function (err, t) {
56 // We force to return only two parameters 57 // We force to return only two parameters
57 return callback(err, t) 58 return callback(err, t)
diff --git a/server/helpers/peertube-crypto.ts b/server/helpers/peertube-crypto.ts
index feb32a4cd..0ac875127 100644
--- a/server/helpers/peertube-crypto.ts
+++ b/server/helpers/peertube-crypto.ts
@@ -14,7 +14,7 @@ import {
14} from '../initializers' 14} from '../initializers'
15import { logger } from './logger' 15import { logger } from './logger'
16 16
17function checkSignature (publicKey, data, hexSignature) { 17function checkSignature (publicKey: string, data: string, hexSignature: string) {
18 const verify = crypto.createVerify(SIGNATURE_ALGORITHM) 18 const verify = crypto.createVerify(SIGNATURE_ALGORITHM)
19 19
20 let dataString 20 let dataString
@@ -35,10 +35,10 @@ function checkSignature (publicKey, data, hexSignature) {
35 return isValid 35 return isValid
36} 36}
37 37
38function sign (data) { 38function sign (data: string|Object) {
39 const sign = crypto.createSign(SIGNATURE_ALGORITHM) 39 const sign = crypto.createSign(SIGNATURE_ALGORITHM)
40 40
41 let dataString 41 let dataString: string
42 if (typeof data === 'string') { 42 if (typeof data === 'string') {
43 dataString = data 43 dataString = data
44 } else { 44 } else {
@@ -60,7 +60,7 @@ function sign (data) {
60 return signature 60 return signature
61} 61}
62 62
63function comparePassword (plainPassword, hashPassword, callback) { 63function comparePassword (plainPassword: string, hashPassword: string, callback: (err: Error, match?: boolean) => void) {
64 bcrypt.compare(plainPassword, hashPassword, function (err, isPasswordMatch) { 64 bcrypt.compare(plainPassword, hashPassword, function (err, isPasswordMatch) {
65 if (err) return callback(err) 65 if (err) return callback(err)
66 66
@@ -68,7 +68,7 @@ function comparePassword (plainPassword, hashPassword, callback) {
68 }) 68 })
69} 69}
70 70
71function createCertsIfNotExist (callback) { 71function createCertsIfNotExist (callback: (err: Error) => void) {
72 certsExist(function (err, exist) { 72 certsExist(function (err, exist) {
73 if (err) return callback(err) 73 if (err) return callback(err)
74 74
@@ -82,7 +82,7 @@ function createCertsIfNotExist (callback) {
82 }) 82 })
83} 83}
84 84
85function cryptPassword (password, callback) { 85function cryptPassword (password: string, callback: (err: Error, hash?: string) => void) {
86 bcrypt.genSalt(BCRYPT_SALT_SIZE, function (err, salt) { 86 bcrypt.genSalt(BCRYPT_SALT_SIZE, function (err, salt) {
87 if (err) return callback(err) 87 if (err) return callback(err)
88 88
@@ -92,12 +92,12 @@ function cryptPassword (password, callback) {
92 }) 92 })
93} 93}
94 94
95function getMyPrivateCert (callback) { 95function getMyPrivateCert (callback: (err: Error, privateCert: string) => void) {
96 const certPath = join(CONFIG.STORAGE.CERT_DIR, PRIVATE_CERT_NAME) 96 const certPath = join(CONFIG.STORAGE.CERT_DIR, PRIVATE_CERT_NAME)
97 fs.readFile(certPath, 'utf8', callback) 97 fs.readFile(certPath, 'utf8', callback)
98} 98}
99 99
100function getMyPublicCert (callback) { 100function getMyPublicCert (callback: (err: Error, publicCert: string) => void) {
101 const certPath = join(CONFIG.STORAGE.CERT_DIR, PUBLIC_CERT_NAME) 101 const certPath = join(CONFIG.STORAGE.CERT_DIR, PUBLIC_CERT_NAME)
102 fs.readFile(certPath, 'utf8', callback) 102 fs.readFile(certPath, 'utf8', callback)
103} 103}
@@ -116,7 +116,7 @@ export {
116 116
117// --------------------------------------------------------------------------- 117// ---------------------------------------------------------------------------
118 118
119function certsExist (callback) { 119function certsExist (callback: (err: Error, certsExist: boolean) => void) {
120 const certPath = join(CONFIG.STORAGE.CERT_DIR, PRIVATE_CERT_NAME) 120 const certPath = join(CONFIG.STORAGE.CERT_DIR, PRIVATE_CERT_NAME)
121 fs.access(certPath, function (err) { 121 fs.access(certPath, function (err) {
122 // If there is an error the certificates do not exist 122 // If there is an error the certificates do not exist
@@ -125,14 +125,14 @@ function certsExist (callback) {
125 }) 125 })
126} 126}
127 127
128function createCerts (callback) { 128function createCerts (callback: (err: Error) => void) {
129 certsExist(function (err, exist) { 129 certsExist(function (err, exist) {
130 if (err) return callback(err) 130 if (err) return callback(err)
131 131
132 if (exist === true) { 132 if (exist === true) {
133 const string = 'Certs already exist.' 133 const errorMessage = 'Certs already exist.'
134 logger.warning(string) 134 logger.warning(errorMessage)
135 return callback(new Error(string)) 135 return callback(new Error(errorMessage))
136 } 136 }
137 137
138 logger.info('Generating a RSA key...') 138 logger.info('Generating a RSA key...')
diff --git a/server/helpers/requests.ts b/server/helpers/requests.ts
index 48b1fd703..b40fc8e39 100644
--- a/server/helpers/requests.ts
+++ b/server/helpers/requests.ts
@@ -6,9 +6,15 @@ import {
6 REMOTE_SCHEME, 6 REMOTE_SCHEME,
7 CONFIG 7 CONFIG
8} from '../initializers' 8} from '../initializers'
9import { PodInstance } from '../models'
9import { sign } from './peertube-crypto' 10import { sign } from './peertube-crypto'
10 11
11function makeRetryRequest (params, callback) { 12type MakeRetryRequestParams = {
13 url: string,
14 method: 'GET'|'POST',
15 json: Object
16}
17function makeRetryRequest (params: MakeRetryRequestParams, callback: request.RequestCallback) {
12 replay( 18 replay(
13 request(params, callback), 19 request(params, callback),
14 { 20 {
@@ -20,14 +26,21 @@ function makeRetryRequest (params, callback) {
20 ) 26 )
21} 27}
22 28
23function makeSecureRequest (params, callback) { 29type MakeSecureRequestParams = {
30 method: 'GET'|'POST'
31 toPod: PodInstance
32 path: string
33 sign: boolean
34 data?: Object
35}
36function makeSecureRequest (params: MakeSecureRequestParams, callback: request.RequestCallback) {
24 const requestParams = { 37 const requestParams = {
25 url: REMOTE_SCHEME.HTTP + '://' + params.toPod.host + params.path, 38 url: REMOTE_SCHEME.HTTP + '://' + params.toPod.host + params.path,
26 json: {} 39 json: {}
27 } 40 }
28 41
29 if (params.method !== 'POST') { 42 if (params.method !== 'POST') {
30 return callback(new Error('Cannot make a secure request with a non POST method.')) 43 return callback(new Error('Cannot make a secure request with a non POST method.'), null, null)
31 } 44 }
32 45
33 // Add signature if it is specified in the params 46 // Add signature if it is specified in the params
diff --git a/server/helpers/utils.ts b/server/helpers/utils.ts
index bc76cfb26..1dcbd31c4 100644
--- a/server/helpers/utils.ts
+++ b/server/helpers/utils.ts
@@ -1,13 +1,15 @@
1import * as express from 'express'
2
1import { pseudoRandomBytes } from 'crypto' 3import { pseudoRandomBytes } from 'crypto'
2import { join } from 'path' 4import { join } from 'path'
3 5
4import { logger } from './logger' 6import { logger } from './logger'
5 7
6function badRequest (req, res, next) { 8function badRequest (req: express.Request, res: express.Response, next: express.NextFunction) {
7 res.type('json').status(400).end() 9 res.type('json').status(400).end()
8} 10}
9 11
10function generateRandomString (size, callback) { 12function generateRandomString (size: number, callback: (err: Error, randomString?: string) => void) {
11 pseudoRandomBytes(size, function (err, raw) { 13 pseudoRandomBytes(size, function (err, raw) {
12 if (err) return callback(err) 14 if (err) return callback(err)
13 15
@@ -15,11 +17,6 @@ function generateRandomString (size, callback) {
15 }) 17 })
16} 18}
17 19
18function cleanForExit (webtorrentProcess) {
19 logger.info('Gracefully exiting.')
20 process.kill(-webtorrentProcess.pid)
21}
22
23function createEmptyCallback () { 20function createEmptyCallback () {
24 return function (err) { 21 return function (err) {
25 if (err) logger.error('Error in empty callback.', { error: err }) 22 if (err) logger.error('Error in empty callback.', { error: err })
@@ -27,10 +24,10 @@ function createEmptyCallback () {
27} 24}
28 25
29function isTestInstance () { 26function isTestInstance () {
30 return (process.env.NODE_ENV === 'test') 27 return process.env.NODE_ENV === 'test'
31} 28}
32 29
33function getFormatedObjects (objects, objectsTotal) { 30function getFormatedObjects (objects: any[], objectsTotal: number) {
34 const formatedObjects = [] 31 const formatedObjects = []
35 32
36 objects.forEach(function (object) { 33 objects.forEach(function (object) {
@@ -53,7 +50,6 @@ function root () {
53export { 50export {
54 badRequest, 51 badRequest,
55 createEmptyCallback, 52 createEmptyCallback,
56 cleanForExit,
57 generateRandomString, 53 generateRandomString,
58 isTestInstance, 54 isTestInstance,
59 getFormatedObjects, 55 getFormatedObjects,