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.ts15
1 files changed, 10 insertions, 5 deletions
diff --git a/server/helpers/core-utils.ts b/server/helpers/core-utils.ts
index 3fb824e36..305d3b71e 100644
--- a/server/helpers/core-utils.ts
+++ b/server/helpers/core-utils.ts
@@ -11,14 +11,13 @@ import * as pem from 'pem'
11import { URL } from 'url' 11import { URL } from 'url'
12import { truncate } from 'lodash' 12import { truncate } from 'lodash'
13import { exec } from 'child_process' 13import { exec } from 'child_process'
14import { isArray } from './custom-validators/misc'
15 14
16const objectConverter = (oldObject: any, keyConverter: (e: string) => string, valueConverter: (e: any) => any) => { 15const objectConverter = (oldObject: any, keyConverter: (e: string) => string, valueConverter: (e: any) => any) => {
17 if (!oldObject || typeof oldObject !== 'object') { 16 if (!oldObject || typeof oldObject !== 'object') {
18 return valueConverter(oldObject) 17 return valueConverter(oldObject)
19 } 18 }
20 19
21 if (isArray(oldObject)) { 20 if (Array.isArray(oldObject)) {
22 return oldObject.map(e => objectConverter(e, keyConverter, valueConverter)) 21 return oldObject.map(e => objectConverter(e, keyConverter, valueConverter))
23 } 22 }
24 23
@@ -41,7 +40,7 @@ const timeTable = {
41 month: 3600000 * 24 * 30 40 month: 3600000 * 24 * 30
42} 41}
43 42
44export function parseDuration (duration: number | string): number { 43export function parseDurationToMs (duration: number | string): number {
45 if (typeof duration === 'number') return duration 44 if (typeof duration === 'number') return duration
46 45
47 if (typeof duration === 'string') { 46 if (typeof duration === 'string') {
@@ -58,7 +57,7 @@ export function parseDuration (duration: number | string): number {
58 } 57 }
59 } 58 }
60 59
61 throw new Error('Duration could not be properly parsed') 60 throw new Error(`Duration ${duration} could not be properly parsed`)
62} 61}
63 62
64export function parseBytes (value: string | number): number { 63export function parseBytes (value: string | number): number {
@@ -193,10 +192,14 @@ function peertubeTruncate (str: string, maxLength: number) {
193 return truncate(str, options) 192 return truncate(str, options)
194} 193}
195 194
196function sha256 (str: string, encoding: HexBase64Latin1Encoding = 'hex') { 195function sha256 (str: string | Buffer, encoding: HexBase64Latin1Encoding = 'hex') {
197 return createHash('sha256').update(str).digest(encoding) 196 return createHash('sha256').update(str).digest(encoding)
198} 197}
199 198
199function sha1 (str: string | Buffer, encoding: HexBase64Latin1Encoding = 'hex') {
200 return createHash('sha1').update(str).digest(encoding)
201}
202
200function promisify0<A> (func: (cb: (err: any, result: A) => void) => void): () => Promise<A> { 203function promisify0<A> (func: (cb: (err: any, result: A) => void) => void): () => Promise<A> {
201 return function promisified (): Promise<A> { 204 return function promisified (): Promise<A> {
202 return new Promise<A>((resolve: (arg: A) => void, reject: (err: any) => void) => { 205 return new Promise<A>((resolve: (arg: A) => void, reject: (err: any) => void) => {
@@ -262,7 +265,9 @@ export {
262 sanitizeHost, 265 sanitizeHost,
263 buildPath, 266 buildPath,
264 peertubeTruncate, 267 peertubeTruncate,
268
265 sha256, 269 sha256,
270 sha1,
266 271
267 promisify0, 272 promisify0,
268 promisify1, 273 promisify1,