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 { exec, ExecOptions } from 'child_process'
9 import { ED25519KeyPairOptions, generateKeyPair, randomBytes, RSAKeyPairOptions } from 'crypto'
10 import { truncate } from 'lodash'
11 import { pipeline } from 'stream'
12 import { URL } from 'url'
13 import { promisify } from 'util'
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)
33 function mapToJSON (map: Map<any, any>) {
36 for (const [ k, v ] of map) {
43 // ---------------------------------------------------------------------------
51 week: 3600000 * 24 * 7,
52 month: 3600000 * 24 * 30
55 export function parseDurationToMs (duration: number | string): number {
56 if (duration === null) return null
57 if (typeof duration === 'number') return duration
58 if (!isNaN(+duration)) return +duration
60 if (typeof duration === 'string') {
61 const split = duration.match(/^([\d.,]+)\s?(\w+)$/)
63 if (split.length === 3) {
64 const len = parseFloat(split[1])
65 let unit = split[2].replace(/s$/i, '').toLowerCase()
70 return (len || 1) * (timeTable[unit] || 0)
74 throw new Error(`Duration ${duration} could not be properly parsed`)
77 export function parseBytes (value: string | number): number {
78 if (typeof value === 'number') return value
79 if (!isNaN(+value)) return +value
81 const tgm = /^(\d+)\s*TB\s*(\d+)\s*GB\s*(\d+)\s*MB$/
82 const tg = /^(\d+)\s*TB\s*(\d+)\s*GB$/
83 const tm = /^(\d+)\s*TB\s*(\d+)\s*MB$/
84 const gm = /^(\d+)\s*GB\s*(\d+)\s*MB$/
85 const t = /^(\d+)\s*TB$/
86 const g = /^(\d+)\s*GB$/
87 const m = /^(\d+)\s*MB$/
88 const b = /^(\d+)\s*B$/
90 let match: RegExpMatchArray
92 if (value.match(tgm)) {
93 match = value.match(tgm)
94 return parseInt(match[1], 10) * 1024 * 1024 * 1024 * 1024 +
95 parseInt(match[2], 10) * 1024 * 1024 * 1024 +
96 parseInt(match[3], 10) * 1024 * 1024
99 if (value.match(tg)) {
100 match = value.match(tg)
101 return parseInt(match[1], 10) * 1024 * 1024 * 1024 * 1024 +
102 parseInt(match[2], 10) * 1024 * 1024 * 1024
105 if (value.match(tm)) {
106 match = value.match(tm)
107 return parseInt(match[1], 10) * 1024 * 1024 * 1024 * 1024 +
108 parseInt(match[2], 10) * 1024 * 1024
111 if (value.match(gm)) {
112 match = value.match(gm)
113 return parseInt(match[1], 10) * 1024 * 1024 * 1024 +
114 parseInt(match[2], 10) * 1024 * 1024
117 if (value.match(t)) {
118 match = value.match(t)
119 return parseInt(match[1], 10) * 1024 * 1024 * 1024 * 1024
122 if (value.match(g)) {
123 match = value.match(g)
124 return parseInt(match[1], 10) * 1024 * 1024 * 1024
127 if (value.match(m)) {
128 match = value.match(m)
129 return parseInt(match[1], 10) * 1024 * 1024
132 if (value.match(b)) {
133 match = value.match(b)
134 return parseInt(match[1], 10) * 1024
137 return parseInt(value, 10)
140 // ---------------------------------------------------------------------------
142 function sanitizeUrl (url: string) {
143 const urlObject = new URL(url)
145 if (urlObject.protocol === 'https:' && urlObject.port === '443') {
147 } else if (urlObject.protocol === 'http:' && urlObject.port === '80') {
151 return urlObject.href.replace(/\/$/, '')
154 // Don't import remote scheme from constants because we are in core utils
155 function sanitizeHost (host: string, remoteScheme: string) {
156 const toRemove = remoteScheme === 'https' ? 443 : 80
158 return host.replace(new RegExp(`:${toRemove}$`), '')
161 // ---------------------------------------------------------------------------
163 function isTestInstance () {
164 return process.env.NODE_ENV === 'test'
167 function isDevInstance () {
168 return process.env.NODE_ENV === 'dev'
171 function isTestOrDevInstance () {
172 return isTestInstance() || isDevInstance()
175 function isProdInstance () {
176 return process.env.NODE_ENV === 'production'
179 function getAppNumber () {
180 return process.env.NODE_APP_INSTANCE || ''
183 // ---------------------------------------------------------------------------
185 // Consistent with .length, lodash truncate function is not
186 function peertubeTruncate (str: string, options: { length: number, separator?: RegExp, omission?: string }) {
187 const truncatedStr = truncate(str, options)
189 // The truncated string is okay, we can return it
190 if (truncatedStr.length <= options.length) return truncatedStr
192 // Lodash takes into account all UTF characters, whereas String.prototype.length does not: some characters have a length of 2
193 // We always use the .length so we need to truncate more if needed
194 options.length -= truncatedStr.length - options.length
195 return truncate(str, options)
198 function pageToStartAndCount (page: number, itemsPerPage: number) {
199 const start = (page - 1) * itemsPerPage
201 return { start, count: itemsPerPage }
204 // ---------------------------------------------------------------------------
206 type SemVersion = { major: number, minor: number, patch: number }
207 function parseSemVersion (s: string) {
208 const parsed = s.match(/^v?(\d+)\.(\d+)\.(\d+)$/i)
211 major: parseInt(parsed[1]),
212 minor: parseInt(parsed[2]),
213 patch: parseInt(parsed[3])
217 // ---------------------------------------------------------------------------
219 function execShell (command: string, options?: ExecOptions) {
220 return new Promise<{ err?: Error, stdout: string, stderr: string }>((res, rej) => {
221 exec(command, options, (err, stdout, stderr) => {
222 // eslint-disable-next-line prefer-promise-reject-errors
223 if (err) return rej({ err, stdout, stderr })
225 return res({ stdout, stderr })
230 // ---------------------------------------------------------------------------
232 function isOdd (num: number) {
233 return (num % 2) !== 0
236 function toEven (num: number) {
237 if (isOdd(num)) return num + 1
242 // ---------------------------------------------------------------------------
244 function generateRSAKeyPairPromise (size: number) {
245 return new Promise<{ publicKey: string, privateKey: string }>((res, rej) => {
246 const options: RSAKeyPairOptions<'pem', 'pem'> = {
252 privateKeyEncoding: {
258 generateKeyPair('rsa', options, (err, publicKey, privateKey) => {
259 if (err) return rej(err)
261 return res({ publicKey, privateKey })
266 function generateED25519KeyPairPromise () {
267 return new Promise<{ publicKey: string, privateKey: string }>((res, rej) => {
268 const options: ED25519KeyPairOptions<'pem', 'pem'> = {
273 privateKeyEncoding: {
279 generateKeyPair('ed25519', options, (err, publicKey, privateKey) => {
280 if (err) return rej(err)
282 return res({ publicKey, privateKey })
287 // ---------------------------------------------------------------------------
289 function 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) ])
297 // Thanks to https://gist.github.com/kumasento/617daa7e46f13ecdd9b2
298 function 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) ])
306 function 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) ])
314 const randomBytesPromise = promisify1<number, Buffer>(randomBytes)
315 const execPromise2 = promisify2<string, any, string>(exec)
316 const execPromise = promisify1<string, string>(exec)
317 const pipelinePromise = promisify(pipeline)
319 // ---------------------------------------------------------------------------
344 generateRSAKeyPairPromise,
345 generateED25519KeyPairPromise,