]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/helpers/core-utils.ts
Correctly cleanup sql command
[github/Chocobozzz/PeerTube.git] / server / helpers / core-utils.ts
index 6ebe8e2acb5a2b6dace1282a0f8598ecb2c00a24..242c49e896090249c3e1d6cda61a5e05ba2cd519 100644 (file)
@@ -6,12 +6,12 @@
 */
 
 import { exec, ExecOptions } from 'child_process'
-import { randomBytes } from 'crypto'
+import { ED25519KeyPairOptions, generateKeyPair, randomBytes, RSAKeyPairOptions, scrypt } from 'crypto'
 import { truncate } from 'lodash'
-import { createPrivateKey as createPrivateKey_1, getPublicKey as getPublicKey_1 } from 'pem'
 import { pipeline } from 'stream'
 import { URL } from 'url'
 import { promisify } from 'util'
+import { promisify1, promisify2, promisify3 } from '@shared/core-utils'
 
 const objectConverter = (oldObject: any, keyConverter: (e: string) => string, valueConverter: (e: any) => any) => {
   if (!oldObject || typeof oldObject !== 'object') {
@@ -165,6 +165,14 @@ function isTestInstance () {
   return process.env.NODE_ENV === 'test'
 }
 
+function isDevInstance () {
+  return process.env.NODE_ENV === 'dev'
+}
+
+function isTestOrDevInstance () {
+  return isTestInstance() || isDevInstance()
+}
+
 function isProdInstance () {
   return process.env.NODE_ENV === 'production'
 }
@@ -222,46 +230,53 @@ function execShell (command: string, options?: ExecOptions) {
 
 // ---------------------------------------------------------------------------
 
-function isOdd (num: number) {
-  return (num % 2) !== 0
-}
+function generateRSAKeyPairPromise (size: number) {
+  return new Promise<{ publicKey: string, privateKey: string }>((res, rej) => {
+    const options: RSAKeyPairOptions<'pem', 'pem'> = {
+      modulusLength: size,
+      publicKeyEncoding: {
+        type: 'spki',
+        format: 'pem'
+      },
+      privateKeyEncoding: {
+        type: 'pkcs1',
+        format: 'pem'
+      }
+    }
 
-function toEven (num: number) {
-  if (isOdd(num)) return num + 1
+    generateKeyPair('rsa', options, (err, publicKey, privateKey) => {
+      if (err) return rej(err)
 
-  return num
+      return res({ publicKey, privateKey })
+    })
+  })
 }
 
-// ---------------------------------------------------------------------------
+function generateED25519KeyPairPromise () {
+  return new Promise<{ publicKey: string, privateKey: string }>((res, rej) => {
+    const options: ED25519KeyPairOptions<'pem', 'pem'> = {
+      publicKeyEncoding: {
+        type: 'spki',
+        format: 'pem'
+      },
+      privateKeyEncoding: {
+        type: 'pkcs8',
+        format: 'pem'
+      }
+    }
 
-function promisify0<A> (func: (cb: (err: any, result: A) => void) => void): () => Promise<A> {
-  return function promisified (): Promise<A> {
-    return new Promise<A>((resolve: (arg: A) => void, reject: (err: any) => void) => {
-      func.apply(null, [ (err: any, res: A) => err ? reject(err) : resolve(res) ])
-    })
-  }
-}
+    generateKeyPair('ed25519', options, (err, publicKey, privateKey) => {
+      if (err) return rej(err)
 
-// Thanks to https://gist.github.com/kumasento/617daa7e46f13ecdd9b2
-function promisify1<T, A> (func: (arg: T, cb: (err: any, result: A) => void) => void): (arg: T) => Promise<A> {
-  return function promisified (arg: T): Promise<A> {
-    return new Promise<A>((resolve: (arg: A) => void, reject: (err: any) => void) => {
-      func.apply(null, [ arg, (err: any, res: A) => err ? reject(err) : resolve(res) ])
+      return res({ publicKey, privateKey })
     })
-  }
+  })
 }
 
-function promisify2<T, U, A> (func: (arg1: T, arg2: U, cb: (err: any, result: A) => void) => void): (arg1: T, arg2: U) => Promise<A> {
-  return function promisified (arg1: T, arg2: U): Promise<A> {
-    return new Promise<A>((resolve: (arg: A) => void, reject: (err: any) => void) => {
-      func.apply(null, [ arg1, arg2, (err: any, res: A) => err ? reject(err) : resolve(res) ])
-    })
-  }
-}
+// ---------------------------------------------------------------------------
 
 const randomBytesPromise = promisify1<number, Buffer>(randomBytes)
-const createPrivateKey = promisify1<number, { key: string }>(createPrivateKey_1)
-const getPublicKey = promisify1<string, { publicKey: string }>(getPublicKey_1)
+const scryptPromise = promisify3<string, string, number, Buffer>(scrypt)
 const execPromise2 = promisify2<string, any, string>(exec)
 const execPromise = promisify1<string, string>(exec)
 const pipelinePromise = promisify(pipeline)
@@ -270,6 +285,7 @@ const pipelinePromise = promisify(pipeline)
 
 export {
   isTestInstance,
+  isTestOrDevInstance,
   isProdInstance,
   getAppNumber,
 
@@ -284,19 +300,16 @@ export {
   pageToStartAndCount,
   peertubeTruncate,
 
-  promisify0,
-  promisify1,
-  promisify2,
+  scryptPromise,
 
   randomBytesPromise,
-  createPrivateKey,
-  getPublicKey,
+
+  generateRSAKeyPairPromise,
+  generateED25519KeyPairPromise,
+
   execPromise2,
   execPromise,
   pipelinePromise,
 
-  parseSemVersion,
-
-  isOdd,
-  toEven
+  parseSemVersion
 }