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