]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/core-utils.ts
Upgrade server dep
[github/Chocobozzz/PeerTube.git] / server / helpers / core-utils.ts
CommitLineData
1840c2f7
C
1/*
2 Different from 'utils' because we don't not import other PeerTube modules.
3 Useful to avoid circular dependencies.
4*/
5
729bb184 6import { createHash, HexBase64Latin1Encoding, pseudoRandomBytes } from 'crypto'
9b474844 7import { basename, isAbsolute, join, resolve } from 'path'
e4f97bab 8import * as pem from 'pem'
225a89c2 9import { URL } from 'url'
c73e83da 10import { truncate } from 'lodash'
f023a19c 11import { exec, ExecOptions } from 'child_process'
a4101923
C
12
13const objectConverter = (oldObject: any, keyConverter: (e: string) => string, valueConverter: (e: any) => any) => {
14 if (!oldObject || typeof oldObject !== 'object') {
15 return valueConverter(oldObject)
16 }
17
74dc3bca 18 if (Array.isArray(oldObject)) {
a4101923
C
19 return oldObject.map(e => objectConverter(e, keyConverter, valueConverter))
20 }
21
22 const newObject = {}
23 Object.keys(oldObject).forEach(oldKey => {
24 const newKey = keyConverter(oldKey)
25 newObject[ newKey ] = objectConverter(oldObject[ oldKey ], keyConverter, valueConverter)
26 })
27
28 return newObject
29}
225a89c2 30
06215f15
C
31const timeTable = {
32 ms: 1,
33 second: 1000,
34 minute: 60000,
35 hour: 3600000,
36 day: 3600000 * 24,
37 week: 3600000 * 24 * 7,
38 month: 3600000 * 24 * 30
39}
0e5ff97f 40
8f0bc73d 41export function parseDurationToMs (duration: number | string): number {
06215f15
C
42 if (typeof duration === 'number') return duration
43
44 if (typeof duration === 'string') {
45 const split = duration.match(/^([\d\.,]+)\s?(\w+)$/)
46
47 if (split.length === 3) {
48 const len = parseFloat(split[1])
49 let unit = split[2].replace(/s$/i,'').toLowerCase()
50 if (unit === 'm') {
51 unit = 'ms'
52 }
53
54 return (len || 1) * (timeTable[unit] || 0)
55 }
56 }
57
ae9bbed4 58 throw new Error(`Duration ${duration} could not be properly parsed`)
06215f15
C
59}
60
0e5ff97f
BY
61export function parseBytes (value: string | number): number {
62 if (typeof value === 'number') return value
63
64 const tgm = /^(\d+)\s*TB\s*(\d+)\s*GB\s*(\d+)\s*MB$/
65 const tg = /^(\d+)\s*TB\s*(\d+)\s*GB$/
66 const tm = /^(\d+)\s*TB\s*(\d+)\s*MB$/
67 const gm = /^(\d+)\s*GB\s*(\d+)\s*MB$/
68 const t = /^(\d+)\s*TB$/
69 const g = /^(\d+)\s*GB$/
70 const m = /^(\d+)\s*MB$/
71 const b = /^(\d+)\s*B$/
72 let match
73
74 if (value.match(tgm)) {
75 match = value.match(tgm)
76 return parseInt(match[1], 10) * 1024 * 1024 * 1024 * 1024
77 + parseInt(match[2], 10) * 1024 * 1024 * 1024
78 + parseInt(match[3], 10) * 1024 * 1024
79 } else if (value.match(tg)) {
80 match = value.match(tg)
81 return parseInt(match[1], 10) * 1024 * 1024 * 1024 * 1024
82 + parseInt(match[2], 10) * 1024 * 1024 * 1024
83 } else if (value.match(tm)) {
84 match = value.match(tm)
85 return parseInt(match[1], 10) * 1024 * 1024 * 1024 * 1024
86 + parseInt(match[2], 10) * 1024 * 1024
87 } else if (value.match(gm)) {
88 match = value.match(gm)
89 return parseInt(match[1], 10) * 1024 * 1024 * 1024
90 + parseInt(match[2], 10) * 1024 * 1024
91 } else if (value.match(t)) {
92 match = value.match(t)
93 return parseInt(match[1], 10) * 1024 * 1024 * 1024 * 1024
94 } else if (value.match(g)) {
95 match = value.match(g)
96 return parseInt(match[1], 10) * 1024 * 1024 * 1024
97 } else if (value.match(m)) {
98 match = value.match(m)
99 return parseInt(match[1], 10) * 1024 * 1024
100 } else if (value.match(b)) {
101 match = value.match(b)
102 return parseInt(match[1], 10) * 1024
103 } else {
104 return parseInt(value, 10)
105 }
106}
107
225a89c2
C
108function sanitizeUrl (url: string) {
109 const urlObject = new URL(url)
110
111 if (urlObject.protocol === 'https:' && urlObject.port === '443') {
112 urlObject.port = ''
113 } else if (urlObject.protocol === 'http:' && urlObject.port === '80') {
114 urlObject.port = ''
115 }
116
117 return urlObject.href.replace(/\/$/, '')
118}
119
120// Don't import remote scheme from constants because we are in core utils
121function sanitizeHost (host: string, remoteScheme: string) {
604abfbe 122 const toRemove = remoteScheme === 'https' ? 443 : 80
225a89c2
C
123
124 return host.replace(new RegExp(`:${toRemove}$`), '')
125}
1840c2f7
C
126
127function isTestInstance () {
128 return process.env.NODE_ENV === 'test'
129}
130
00f9e41e
C
131function isProdInstance () {
132 return process.env.NODE_ENV === 'production'
133}
134
1a12f66d
C
135function getAppNumber () {
136 return process.env.NODE_APP_INSTANCE
137}
138
9b474844 139let rootPath: string
1840c2f7 140function root () {
9b474844
C
141 if (rootPath) return rootPath
142
fdbda9e3 143 // We are in /helpers/utils.js
9b474844 144 rootPath = join(__dirname, '..', '..')
fdbda9e3 145
9b474844 146 if (basename(rootPath) === 'dist') rootPath = resolve(rootPath, '..')
fdbda9e3 147
9b474844 148 return rootPath
1840c2f7
C
149}
150
49347a0a
C
151// Thanks: https://stackoverflow.com/a/12034334
152function escapeHTML (stringParam) {
23e27dd5
C
153 if (!stringParam) return ''
154
49347a0a
C
155 const entityMap = {
156 '&': '&',
157 '<': '&lt;',
158 '>': '&gt;',
159 '"': '&quot;',
cf7a61b5 160 '\'': '&#39;',
49347a0a
C
161 '/': '&#x2F;',
162 '`': '&#x60;',
163 '=': '&#x3D;'
164 }
165
166 return String(stringParam).replace(/[&<>"'`=\/]/g, s => entityMap[s])
167}
168
e4f97bab
C
169function pageToStartAndCount (page: number, itemsPerPage: number) {
170 const start = (page - 1) * itemsPerPage
171
172 return { start, count: itemsPerPage }
173}
174
0b4204f9
C
175function buildPath (path: string) {
176 if (isAbsolute(path)) return path
177
178 return join(root(), path)
179}
180
c73e83da 181// Consistent with .length, lodash truncate function is not
687c6180 182function peertubeTruncate (str: string, options: { length: number, separator?: RegExp, omission?: string }) {
c73e83da
C
183 const truncatedStr = truncate(str, options)
184
185 // The truncated string is okay, we can return it
687c6180 186 if (truncatedStr.length <= options.length) return truncatedStr
c73e83da
C
187
188 // Lodash takes into account all UTF characters, whereas String.prototype.length does not: some characters have a length of 2
189 // We always use the .length so we need to truncate more if needed
687c6180 190 options.length -= truncatedStr.length - options.length
c73e83da
C
191 return truncate(str, options)
192}
193
09209296 194function sha256 (str: string | Buffer, encoding: HexBase64Latin1Encoding = 'hex') {
729bb184 195 return createHash('sha256').update(str).digest(encoding)
990b6a0b
C
196}
197
09209296
C
198function sha1 (str: string | Buffer, encoding: HexBase64Latin1Encoding = 'hex') {
199 return createHash('sha1').update(str).digest(encoding)
200}
201
f023a19c
C
202function execShell (command: string, options?: ExecOptions) {
203 return new Promise<{ err?: Error, stdout: string, stderr: string }>((res, rej) => {
204 exec(command, options, (err, stdout, stderr) => {
205 if (err) return rej({ err, stdout, stderr })
206
207 return res({ stdout, stderr })
208 })
209 })
210}
211
6fcd19ba
C
212function promisify0<A> (func: (cb: (err: any, result: A) => void) => void): () => Promise<A> {
213 return function promisified (): Promise<A> {
214 return new Promise<A>((resolve: (arg: A) => void, reject: (err: any) => void) => {
215 func.apply(null, [ (err: any, res: A) => err ? reject(err) : resolve(res) ])
216 })
217 }
218}
219
220// Thanks to https://gist.github.com/kumasento/617daa7e46f13ecdd9b2
221function promisify1<T, A> (func: (arg: T, cb: (err: any, result: A) => void) => void): (arg: T) => Promise<A> {
222 return function promisified (arg: T): Promise<A> {
223 return new Promise<A>((resolve: (arg: A) => void, reject: (err: any) => void) => {
224 func.apply(null, [ arg, (err: any, res: A) => err ? reject(err) : resolve(res) ])
225 })
226 }
227}
228
229function promisify1WithVoid<T> (func: (arg: T, cb: (err: any) => void) => void): (arg: T) => Promise<void> {
230 return function promisified (arg: T): Promise<void> {
231 return new Promise<void>((resolve: () => void, reject: (err: any) => void) => {
232 func.apply(null, [ arg, (err: any) => err ? reject(err) : resolve() ])
233 })
234 }
235}
236
237function promisify2<T, U, A> (func: (arg1: T, arg2: U, cb: (err: any, result: A) => void) => void): (arg1: T, arg2: U) => Promise<A> {
238 return function promisified (arg1: T, arg2: U): Promise<A> {
239 return new Promise<A>((resolve: (arg: A) => void, reject: (err: any) => void) => {
240 func.apply(null, [ arg1, arg2, (err: any, res: A) => err ? reject(err) : resolve(res) ])
241 })
242 }
243}
244
245function promisify2WithVoid<T, U> (func: (arg1: T, arg2: U, cb: (err: any) => void) => void): (arg1: T, arg2: U) => Promise<void> {
246 return function promisified (arg1: T, arg2: U): Promise<void> {
247 return new Promise<void>((resolve: () => void, reject: (err: any) => void) => {
248 func.apply(null, [ arg1, arg2, (err: any) => err ? reject(err) : resolve() ])
249 })
250 }
251}
252
6fcd19ba 253const pseudoRandomBytesPromise = promisify1<number, Buffer>(pseudoRandomBytes)
e4f97bab
C
254const createPrivateKey = promisify1<number, { key: string }>(pem.createPrivateKey)
255const getPublicKey = promisify1<string, { publicKey: string }>(pem.getPublicKey)
499d9015
C
256const execPromise2 = promisify2<string, any, string>(exec)
257const execPromise = promisify1<string, string>(exec)
6fcd19ba 258
1840c2f7
C
259// ---------------------------------------------------------------------------
260
261export {
262 isTestInstance,
00f9e41e 263 isProdInstance,
1a12f66d 264 getAppNumber,
00f9e41e 265
a4101923 266 objectConverter,
6fcd19ba 267 root,
49347a0a 268 escapeHTML,
e4f97bab 269 pageToStartAndCount,
225a89c2
C
270 sanitizeUrl,
271 sanitizeHost,
0b4204f9 272 buildPath,
f023a19c 273 execShell,
c73e83da 274 peertubeTruncate,
09209296 275
990b6a0b 276 sha256,
09209296 277 sha1,
6fcd19ba
C
278
279 promisify0,
280 promisify1,
8d2be0ed 281 promisify2,
e4f97bab 282
6fcd19ba 283 pseudoRandomBytesPromise,
e4f97bab
C
284 createPrivateKey,
285 getPublicKey,
499d9015
C
286 execPromise2,
287 execPromise
1840c2f7 288}