X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fhelpers%2Fcore-utils.ts;h=b1e9af0a17a61325b7d8241814ad294498692799;hb=1a12f66d631d28a5a58ebbcd274426f2e6e5d203;hp=3fb824e36220832da1b44c42ed383f5a05daea26;hpb=a4101923e699e49ceb9ff36e971c75417fafc9f0;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/helpers/core-utils.ts b/server/helpers/core-utils.ts index 3fb824e36..b1e9af0a1 100644 --- a/server/helpers/core-utils.ts +++ b/server/helpers/core-utils.ts @@ -11,14 +11,13 @@ import * as pem from 'pem' import { URL } from 'url' import { truncate } from 'lodash' import { exec } from 'child_process' -import { isArray } from './custom-validators/misc' const objectConverter = (oldObject: any, keyConverter: (e: string) => string, valueConverter: (e: any) => any) => { if (!oldObject || typeof oldObject !== 'object') { return valueConverter(oldObject) } - if (isArray(oldObject)) { + if (Array.isArray(oldObject)) { return oldObject.map(e => objectConverter(e, keyConverter, valueConverter)) } @@ -41,7 +40,7 @@ const timeTable = { month: 3600000 * 24 * 30 } -export function parseDuration (duration: number | string): number { +export function parseDurationToMs (duration: number | string): number { if (typeof duration === 'number') return duration if (typeof duration === 'string') { @@ -58,7 +57,7 @@ export function parseDuration (duration: number | string): number { } } - throw new Error('Duration could not be properly parsed') + throw new Error(`Duration ${duration} could not be properly parsed`) } export function parseBytes (value: string | number): number { @@ -135,6 +134,10 @@ function isProdInstance () { return process.env.NODE_ENV === 'production' } +function getAppNumber () { + return process.env.NODE_APP_INSTANCE +} + function root () { // We are in /helpers/utils.js const paths = [ __dirname, '..', '..' ] @@ -193,10 +196,14 @@ function peertubeTruncate (str: string, maxLength: number) { return truncate(str, options) } -function sha256 (str: string, encoding: HexBase64Latin1Encoding = 'hex') { +function sha256 (str: string | Buffer, encoding: HexBase64Latin1Encoding = 'hex') { return createHash('sha256').update(str).digest(encoding) } +function sha1 (str: string | Buffer, encoding: HexBase64Latin1Encoding = 'hex') { + return createHash('sha1').update(str).digest(encoding) +} + function promisify0 (func: (cb: (err: any, result: A) => void) => void): () => Promise { return function promisified (): Promise { return new Promise((resolve: (arg: A) => void, reject: (err: any) => void) => { @@ -253,6 +260,7 @@ const execPromise = promisify1(exec) export { isTestInstance, isProdInstance, + getAppNumber, objectConverter, root, @@ -262,7 +270,9 @@ export { sanitizeHost, buildPath, peertubeTruncate, + sha256, + sha1, promisify0, promisify1,