1 /* eslint-disable no-useless-call */
4 Different from 'utils' because we don't import other PeerTube modules.
5 Useful to avoid circular dependencies.
8 import { createHash, HexBase64Latin1Encoding, randomBytes } from 'crypto'
9 import { basename, isAbsolute, join, resolve } from 'path'
10 import * as pem from 'pem'
11 import { URL } from 'url'
12 import { truncate } from 'lodash'
13 import { exec, ExecOptions } from 'child_process'
15 const objectConverter = (oldObject: any, keyConverter: (e: string) => string, valueConverter: (e: any) => any) => {
16 if (!oldObject || typeof oldObject !== 'object') {
17 return valueConverter(oldObject)
20 if (Array.isArray(oldObject)) {
21 return oldObject.map(e => objectConverter(e, keyConverter, valueConverter))
25 Object.keys(oldObject).forEach(oldKey => {
26 const newKey = keyConverter(oldKey)
27 newObject[newKey] = objectConverter(oldObject[oldKey], keyConverter, valueConverter)
39 week: 3600000 * 24 * 7,
40 month: 3600000 * 24 * 30
43 export function parseDurationToMs (duration: number | string): number {
44 if (typeof duration === 'number') return duration
46 if (typeof duration === 'string') {
47 const split = duration.match(/^([\d.,]+)\s?(\w+)$/)
49 if (split.length === 3) {
50 const len = parseFloat(split[1])
51 let unit = split[2].replace(/s$/i, '').toLowerCase()
56 return (len || 1) * (timeTable[unit] || 0)
60 throw new Error(`Duration ${duration} could not be properly parsed`)
63 export function parseBytes (value: string | number): number {
64 if (typeof value === 'number') return value
66 const tgm = /^(\d+)\s*TB\s*(\d+)\s*GB\s*(\d+)\s*MB$/
67 const tg = /^(\d+)\s*TB\s*(\d+)\s*GB$/
68 const tm = /^(\d+)\s*TB\s*(\d+)\s*MB$/
69 const gm = /^(\d+)\s*GB\s*(\d+)\s*MB$/
70 const t = /^(\d+)\s*TB$/
71 const g = /^(\d+)\s*GB$/
72 const m = /^(\d+)\s*MB$/
73 const b = /^(\d+)\s*B$/
76 if (value.match(tgm)) {
77 match = value.match(tgm)
78 return parseInt(match[1], 10) * 1024 * 1024 * 1024 * 1024 +
79 parseInt(match[2], 10) * 1024 * 1024 * 1024 +
80 parseInt(match[3], 10) * 1024 * 1024
81 } else if (value.match(tg)) {
82 match = value.match(tg)
83 return parseInt(match[1], 10) * 1024 * 1024 * 1024 * 1024 +
84 parseInt(match[2], 10) * 1024 * 1024 * 1024
85 } else if (value.match(tm)) {
86 match = value.match(tm)
87 return parseInt(match[1], 10) * 1024 * 1024 * 1024 * 1024 +
88 parseInt(match[2], 10) * 1024 * 1024
89 } else if (value.match(gm)) {
90 match = value.match(gm)
91 return parseInt(match[1], 10) * 1024 * 1024 * 1024 +
92 parseInt(match[2], 10) * 1024 * 1024
93 } else if (value.match(t)) {
94 match = value.match(t)
95 return parseInt(match[1], 10) * 1024 * 1024 * 1024 * 1024
96 } else if (value.match(g)) {
97 match = value.match(g)
98 return parseInt(match[1], 10) * 1024 * 1024 * 1024
99 } else if (value.match(m)) {
100 match = value.match(m)
101 return parseInt(match[1], 10) * 1024 * 1024
102 } else if (value.match(b)) {
103 match = value.match(b)
104 return parseInt(match[1], 10) * 1024
106 return parseInt(value, 10)
110 function sanitizeUrl (url: string) {
111 const urlObject = new URL(url)
113 if (urlObject.protocol === 'https:' && urlObject.port === '443') {
115 } else if (urlObject.protocol === 'http:' && urlObject.port === '80') {
119 return urlObject.href.replace(/\/$/, '')
122 // Don't import remote scheme from constants because we are in core utils
123 function sanitizeHost (host: string, remoteScheme: string) {
124 const toRemove = remoteScheme === 'https' ? 443 : 80
126 return host.replace(new RegExp(`:${toRemove}$`), '')
129 function isTestInstance () {
130 return process.env.NODE_ENV === 'test'
133 function isProdInstance () {
134 return process.env.NODE_ENV === 'production'
137 function getAppNumber () {
138 return process.env.NODE_APP_INSTANCE
144 if (rootPath) return rootPath
146 // We are in /helpers/utils.js
147 rootPath = join(__dirname, '..', '..')
149 if (basename(rootPath) === 'dist') rootPath = resolve(rootPath, '..')
154 // Thanks: https://stackoverflow.com/a/12034334
155 function escapeHTML (stringParam) {
156 if (!stringParam) return ''
169 return String(stringParam).replace(/[&<>"'`=/]/g, s => entityMap[s])
172 function pageToStartAndCount (page: number, itemsPerPage: number) {
173 const start = (page - 1) * itemsPerPage
175 return { start, count: itemsPerPage }
178 function buildPath (path: string) {
179 if (isAbsolute(path)) return path
181 return join(root(), path)
184 // Consistent with .length, lodash truncate function is not
185 function peertubeTruncate (str: string, options: { length: number, separator?: RegExp, omission?: string }) {
186 const truncatedStr = truncate(str, options)
188 // The truncated string is okay, we can return it
189 if (truncatedStr.length <= options.length) return truncatedStr
191 // Lodash takes into account all UTF characters, whereas String.prototype.length does not: some characters have a length of 2
192 // We always use the .length so we need to truncate more if needed
193 options.length -= truncatedStr.length - options.length
194 return truncate(str, options)
197 function sha256 (str: string | Buffer, encoding: HexBase64Latin1Encoding = 'hex') {
198 return createHash('sha256').update(str).digest(encoding)
201 function sha1 (str: string | Buffer, encoding: HexBase64Latin1Encoding = 'hex') {
202 return createHash('sha1').update(str).digest(encoding)
205 function execShell (command: string, options?: ExecOptions) {
206 return new Promise<{ err?: Error, stdout: string, stderr: string }>((res, rej) => {
207 exec(command, options, (err, stdout, stderr) => {
208 // eslint-disable-next-line prefer-promise-reject-errors
209 if (err) return rej({ err, stdout, stderr })
211 return res({ stdout, stderr })
216 function promisify0<A> (func: (cb: (err: any, result: A) => void) => void): () => Promise<A> {
217 return function promisified (): Promise<A> {
218 return new Promise<A>((resolve: (arg: A) => void, reject: (err: any) => void) => {
219 func.apply(null, [ (err: any, res: A) => err ? reject(err) : resolve(res) ])
224 // Thanks to https://gist.github.com/kumasento/617daa7e46f13ecdd9b2
225 function promisify1<T, A> (func: (arg: T, cb: (err: any, result: A) => void) => void): (arg: T) => Promise<A> {
226 return function promisified (arg: T): Promise<A> {
227 return new Promise<A>((resolve: (arg: A) => void, reject: (err: any) => void) => {
228 func.apply(null, [ arg, (err: any, res: A) => err ? reject(err) : resolve(res) ])
233 function promisify2<T, U, A> (func: (arg1: T, arg2: U, cb: (err: any, result: A) => void) => void): (arg1: T, arg2: U) => Promise<A> {
234 return function promisified (arg1: T, arg2: U): Promise<A> {
235 return new Promise<A>((resolve: (arg: A) => void, reject: (err: any) => void) => {
236 func.apply(null, [ arg1, arg2, (err: any, res: A) => err ? reject(err) : resolve(res) ])
241 const randomBytesPromise = promisify1<number, Buffer>(randomBytes)
242 const createPrivateKey = promisify1<number, { key: string }>(pem.createPrivateKey)
243 const getPublicKey = promisify1<string, { publicKey: string }>(pem.getPublicKey)
244 const execPromise2 = promisify2<string, any, string>(exec)
245 const execPromise = promisify1<string, string>(exec)
247 // ---------------------------------------------------------------------------