]>
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 C |
58 | if (typeof duration === 'number') return duration |
59 | ||
60 | if (typeof duration === 'string') { | |
a1587156 | 61 | const split = duration.match(/^([\d.,]+)\s?(\w+)$/) |
06215f15 C |
62 | |
63 | if (split.length === 3) { | |
64 | const len = parseFloat(split[1]) | |
a1587156 | 65 | let unit = split[2].replace(/s$/i, '').toLowerCase() |
06215f15 C |
66 | if (unit === 'm') { |
67 | unit = 'ms' | |
68 | } | |
69 | ||
70 | return (len || 1) * (timeTable[unit] || 0) | |
71 | } | |
72 | } | |
73 | ||
ae9bbed4 | 74 | throw new Error(`Duration ${duration} could not be properly parsed`) |
06215f15 C |
75 | } |
76 | ||
0e5ff97f BY |
77 | export function parseBytes (value: string | number): number { |
78 | if (typeof value === 'number') return value | |
79 | ||
80 | const tgm = /^(\d+)\s*TB\s*(\d+)\s*GB\s*(\d+)\s*MB$/ | |
81 | const tg = /^(\d+)\s*TB\s*(\d+)\s*GB$/ | |
82 | const tm = /^(\d+)\s*TB\s*(\d+)\s*MB$/ | |
83 | const gm = /^(\d+)\s*GB\s*(\d+)\s*MB$/ | |
84 | const t = /^(\d+)\s*TB$/ | |
85 | const g = /^(\d+)\s*GB$/ | |
86 | const m = /^(\d+)\s*MB$/ | |
87 | const b = /^(\d+)\s*B$/ | |
88 | let match | |
89 | ||
90 | if (value.match(tgm)) { | |
91 | match = value.match(tgm) | |
a1587156 C |
92 | return parseInt(match[1], 10) * 1024 * 1024 * 1024 * 1024 + |
93 | parseInt(match[2], 10) * 1024 * 1024 * 1024 + | |
94 | parseInt(match[3], 10) * 1024 * 1024 | |
0e5ff97f BY |
95 | } else if (value.match(tg)) { |
96 | match = value.match(tg) | |
a1587156 C |
97 | return parseInt(match[1], 10) * 1024 * 1024 * 1024 * 1024 + |
98 | parseInt(match[2], 10) * 1024 * 1024 * 1024 | |
0e5ff97f BY |
99 | } else if (value.match(tm)) { |
100 | match = value.match(tm) | |
a1587156 C |
101 | return parseInt(match[1], 10) * 1024 * 1024 * 1024 * 1024 + |
102 | parseInt(match[2], 10) * 1024 * 1024 | |
0e5ff97f BY |
103 | } else if (value.match(gm)) { |
104 | match = value.match(gm) | |
a1587156 C |
105 | return parseInt(match[1], 10) * 1024 * 1024 * 1024 + |
106 | parseInt(match[2], 10) * 1024 * 1024 | |
0e5ff97f BY |
107 | } else if (value.match(t)) { |
108 | match = value.match(t) | |
109 | return parseInt(match[1], 10) * 1024 * 1024 * 1024 * 1024 | |
110 | } else if (value.match(g)) { | |
111 | match = value.match(g) | |
112 | return parseInt(match[1], 10) * 1024 * 1024 * 1024 | |
113 | } else if (value.match(m)) { | |
114 | match = value.match(m) | |
115 | return parseInt(match[1], 10) * 1024 * 1024 | |
116 | } else if (value.match(b)) { | |
117 | match = value.match(b) | |
118 | return parseInt(match[1], 10) * 1024 | |
119 | } else { | |
120 | return parseInt(value, 10) | |
121 | } | |
122 | } | |
123 | ||
ea54cd04 C |
124 | // --------------------------------------------------------------------------- |
125 | ||
225a89c2 C |
126 | function sanitizeUrl (url: string) { |
127 | const urlObject = new URL(url) | |
128 | ||
129 | if (urlObject.protocol === 'https:' && urlObject.port === '443') { | |
130 | urlObject.port = '' | |
131 | } else if (urlObject.protocol === 'http:' && urlObject.port === '80') { | |
132 | urlObject.port = '' | |
133 | } | |
134 | ||
135 | return urlObject.href.replace(/\/$/, '') | |
136 | } | |
137 | ||
138 | // Don't import remote scheme from constants because we are in core utils | |
139 | function sanitizeHost (host: string, remoteScheme: string) { | |
604abfbe | 140 | const toRemove = remoteScheme === 'https' ? 443 : 80 |
225a89c2 C |
141 | |
142 | return host.replace(new RegExp(`:${toRemove}$`), '') | |
143 | } | |
1840c2f7 | 144 | |
ea54cd04 C |
145 | // --------------------------------------------------------------------------- |
146 | ||
1840c2f7 C |
147 | function isTestInstance () { |
148 | return process.env.NODE_ENV === 'test' | |
149 | } | |
150 | ||
00f9e41e C |
151 | function isProdInstance () { |
152 | return process.env.NODE_ENV === 'production' | |
153 | } | |
154 | ||
1a12f66d | 155 | function getAppNumber () { |
4abc7b05 | 156 | return process.env.NODE_APP_INSTANCE || '' |
1a12f66d C |
157 | } |
158 | ||
ea54cd04 C |
159 | // --------------------------------------------------------------------------- |
160 | ||
c73e83da | 161 | // Consistent with .length, lodash truncate function is not |
687c6180 | 162 | function peertubeTruncate (str: string, options: { length: number, separator?: RegExp, omission?: string }) { |
c73e83da C |
163 | const truncatedStr = truncate(str, options) |
164 | ||
165 | // The truncated string is okay, we can return it | |
687c6180 | 166 | if (truncatedStr.length <= options.length) return truncatedStr |
c73e83da C |
167 | |
168 | // Lodash takes into account all UTF characters, whereas String.prototype.length does not: some characters have a length of 2 | |
169 | // We always use the .length so we need to truncate more if needed | |
687c6180 | 170 | options.length -= truncatedStr.length - options.length |
c73e83da C |
171 | return truncate(str, options) |
172 | } | |
173 | ||
ea54cd04 C |
174 | function pageToStartAndCount (page: number, itemsPerPage: number) { |
175 | const start = (page - 1) * itemsPerPage | |
176 | ||
177 | return { start, count: itemsPerPage } | |
178 | } | |
179 | ||
180 | // --------------------------------------------------------------------------- | |
181 | ||
182 | type SemVersion = { major: number, minor: number, patch: number } | |
183 | function parseSemVersion (s: string) { | |
184 | const parsed = s.match(/^v?(\d+)\.(\d+)\.(\d+)$/i) | |
185 | ||
186 | return { | |
187 | major: parseInt(parsed[1]), | |
188 | minor: parseInt(parsed[2]), | |
189 | patch: parseInt(parsed[3]) | |
190 | } as SemVersion | |
191 | } | |
192 | ||
193 | // --------------------------------------------------------------------------- | |
194 | ||
f023a19c C |
195 | function execShell (command: string, options?: ExecOptions) { |
196 | return new Promise<{ err?: Error, stdout: string, stderr: string }>((res, rej) => { | |
197 | exec(command, options, (err, stdout, stderr) => { | |
a1587156 | 198 | // eslint-disable-next-line prefer-promise-reject-errors |
f023a19c C |
199 | if (err) return rej({ err, stdout, stderr }) |
200 | ||
201 | return res({ stdout, stderr }) | |
202 | }) | |
203 | }) | |
204 | } | |
205 | ||
ea54cd04 C |
206 | // --------------------------------------------------------------------------- |
207 | ||
318b0bd0 C |
208 | function isOdd (num: number) { |
209 | return (num % 2) !== 0 | |
210 | } | |
211 | ||
212 | function toEven (num: number) { | |
b7c8304c | 213 | if (isOdd(num)) return num + 1 |
318b0bd0 C |
214 | |
215 | return num | |
216 | } | |
217 | ||
218 | // --------------------------------------------------------------------------- | |
219 | ||
6fcd19ba C |
220 | function promisify0<A> (func: (cb: (err: any, result: A) => void) => void): () => Promise<A> { |
221 | return function promisified (): Promise<A> { | |
222 | return new Promise<A>((resolve: (arg: A) => void, reject: (err: any) => void) => { | |
223 | func.apply(null, [ (err: any, res: A) => err ? reject(err) : resolve(res) ]) | |
224 | }) | |
225 | } | |
226 | } | |
227 | ||
228 | // Thanks to https://gist.github.com/kumasento/617daa7e46f13ecdd9b2 | |
229 | function promisify1<T, A> (func: (arg: T, cb: (err: any, result: A) => void) => void): (arg: T) => Promise<A> { | |
230 | return function promisified (arg: T): Promise<A> { | |
231 | return new Promise<A>((resolve: (arg: A) => void, reject: (err: any) => void) => { | |
232 | func.apply(null, [ arg, (err: any, res: A) => err ? reject(err) : resolve(res) ]) | |
233 | }) | |
234 | } | |
235 | } | |
236 | ||
6fcd19ba C |
237 | function 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 | ||
a1587156 | 245 | const randomBytesPromise = promisify1<number, Buffer>(randomBytes) |
41fb13c3 C |
246 | const createPrivateKey = promisify1<number, { key: string }>(createPrivateKey_1) |
247 | const getPublicKey = promisify1<string, { publicKey: string }>(getPublicKey_1) | |
499d9015 C |
248 | const execPromise2 = promisify2<string, any, string>(exec) |
249 | const execPromise = promisify1<string, string>(exec) | |
db4b15f2 | 250 | const pipelinePromise = promisify(pipeline) |
6fcd19ba | 251 | |
1840c2f7 C |
252 | // --------------------------------------------------------------------------- |
253 | ||
254 | export { | |
255 | isTestInstance, | |
00f9e41e | 256 | isProdInstance, |
1a12f66d | 257 | getAppNumber, |
00f9e41e | 258 | |
a4101923 | 259 | objectConverter, |
ea54cd04 C |
260 | mapToJSON, |
261 | ||
225a89c2 C |
262 | sanitizeUrl, |
263 | sanitizeHost, | |
ea54cd04 | 264 | |
f023a19c | 265 | execShell, |
ea54cd04 C |
266 | |
267 | pageToStartAndCount, | |
c73e83da | 268 | peertubeTruncate, |
09209296 | 269 | |
6fcd19ba C |
270 | promisify0, |
271 | promisify1, | |
8d2be0ed | 272 | promisify2, |
e4f97bab | 273 | |
a1587156 | 274 | randomBytesPromise, |
e4f97bab C |
275 | createPrivateKey, |
276 | getPublicKey, | |
499d9015 | 277 | execPromise2, |
db4b15f2 | 278 | execPromise, |
ae71acca C |
279 | pipelinePromise, |
280 | ||
318b0bd0 C |
281 | parseSemVersion, |
282 | ||
283 | isOdd, | |
284 | toEven | |
1840c2f7 | 285 | } |