aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/core-utils.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/helpers/core-utils.ts')
-rw-r--r--server/helpers/core-utils.ts56
1 files changed, 2 insertions, 54 deletions
diff --git a/server/helpers/core-utils.ts b/server/helpers/core-utils.ts
index 73bd994c1..242c49e89 100644
--- a/server/helpers/core-utils.ts
+++ b/server/helpers/core-utils.ts
@@ -11,6 +11,7 @@ import { truncate } from 'lodash'
11import { pipeline } from 'stream' 11import { pipeline } from 'stream'
12import { URL } from 'url' 12import { URL } from 'url'
13import { promisify } from 'util' 13import { promisify } from 'util'
14import { promisify1, promisify2, promisify3 } from '@shared/core-utils'
14 15
15const objectConverter = (oldObject: any, keyConverter: (e: string) => string, valueConverter: (e: any) => any) => { 16const objectConverter = (oldObject: any, keyConverter: (e: string) => string, valueConverter: (e: any) => any) => {
16 if (!oldObject || typeof oldObject !== 'object') { 17 if (!oldObject || typeof oldObject !== 'object') {
@@ -229,18 +230,6 @@ function execShell (command: string, options?: ExecOptions) {
229 230
230// --------------------------------------------------------------------------- 231// ---------------------------------------------------------------------------
231 232
232function isOdd (num: number) {
233 return (num % 2) !== 0
234}
235
236function toEven (num: number) {
237 if (isOdd(num)) return num + 1
238
239 return num
240}
241
242// ---------------------------------------------------------------------------
243
244function generateRSAKeyPairPromise (size: number) { 233function generateRSAKeyPairPromise (size: number) {
245 return new Promise<{ publicKey: string, privateKey: string }>((res, rej) => { 234 return new Promise<{ publicKey: string, privateKey: string }>((res, rej) => {
246 const options: RSAKeyPairOptions<'pem', 'pem'> = { 235 const options: RSAKeyPairOptions<'pem', 'pem'> = {
@@ -286,40 +275,6 @@ function generateED25519KeyPairPromise () {
286 275
287// --------------------------------------------------------------------------- 276// ---------------------------------------------------------------------------
288 277
289function promisify0<A> (func: (cb: (err: any, result: A) => void) => void): () => Promise<A> {
290 return function promisified (): Promise<A> {
291 return new Promise<A>((resolve: (arg: A) => void, reject: (err: any) => void) => {
292 func.apply(null, [ (err: any, res: A) => err ? reject(err) : resolve(res) ])
293 })
294 }
295}
296
297// Thanks to https://gist.github.com/kumasento/617daa7e46f13ecdd9b2
298function promisify1<T, A> (func: (arg: T, cb: (err: any, result: A) => void) => void): (arg: T) => Promise<A> {
299 return function promisified (arg: T): Promise<A> {
300 return new Promise<A>((resolve: (arg: A) => void, reject: (err: any) => void) => {
301 func.apply(null, [ arg, (err: any, res: A) => err ? reject(err) : resolve(res) ])
302 })
303 }
304}
305
306function promisify2<T, U, A> (func: (arg1: T, arg2: U, cb: (err: any, result: A) => void) => void): (arg1: T, arg2: U) => Promise<A> {
307 return function promisified (arg1: T, arg2: U): Promise<A> {
308 return new Promise<A>((resolve: (arg: A) => void, reject: (err: any) => void) => {
309 func.apply(null, [ arg1, arg2, (err: any, res: A) => err ? reject(err) : resolve(res) ])
310 })
311 }
312}
313
314// eslint-disable-next-line max-len
315function promisify3<T, U, V, A> (func: (arg1: T, arg2: U, arg3: V, cb: (err: any, result: A) => void) => void): (arg1: T, arg2: U, arg3: V) => Promise<A> {
316 return function promisified (arg1: T, arg2: U, arg3: V): Promise<A> {
317 return new Promise<A>((resolve: (arg: A) => void, reject: (err: any) => void) => {
318 func.apply(null, [ arg1, arg2, arg3, (err: any, res: A) => err ? reject(err) : resolve(res) ])
319 })
320 }
321}
322
323const randomBytesPromise = promisify1<number, Buffer>(randomBytes) 278const randomBytesPromise = promisify1<number, Buffer>(randomBytes)
324const scryptPromise = promisify3<string, string, number, Buffer>(scrypt) 279const scryptPromise = promisify3<string, string, number, Buffer>(scrypt)
325const execPromise2 = promisify2<string, any, string>(exec) 280const execPromise2 = promisify2<string, any, string>(exec)
@@ -345,10 +300,6 @@ export {
345 pageToStartAndCount, 300 pageToStartAndCount,
346 peertubeTruncate, 301 peertubeTruncate,
347 302
348 promisify0,
349 promisify1,
350 promisify2,
351
352 scryptPromise, 303 scryptPromise,
353 304
354 randomBytesPromise, 305 randomBytesPromise,
@@ -360,8 +311,5 @@ export {
360 execPromise, 311 execPromise,
361 pipelinePromise, 312 pipelinePromise,
362 313
363 parseSemVersion, 314 parseSemVersion
364
365 isOdd,
366 toEven
367} 315}