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