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