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