]>
Commit | Line | Data |
---|---|---|
1840c2f7 C |
1 | /* |
2 | Different from 'utils' because we don't not import other PeerTube modules. | |
3 | Useful to avoid circular dependencies. | |
4 | */ | |
5 | ||
6fcd19ba C |
6 | import * as bcrypt from 'bcrypt' |
7 | import * as createTorrent from 'create-torrent' | |
729bb184 | 8 | import { createHash, HexBase64Latin1Encoding, pseudoRandomBytes } from 'crypto' |
0b4204f9 | 9 | import { isAbsolute, join } from 'path' |
e4f97bab | 10 | import * as pem from 'pem' |
225a89c2 | 11 | import { URL } from 'url' |
c73e83da | 12 | import { truncate } from 'lodash' |
499d9015 | 13 | import { exec } from 'child_process' |
225a89c2 | 14 | |
06215f15 C |
15 | const timeTable = { |
16 | ms: 1, | |
17 | second: 1000, | |
18 | minute: 60000, | |
19 | hour: 3600000, | |
20 | day: 3600000 * 24, | |
21 | week: 3600000 * 24 * 7, | |
22 | month: 3600000 * 24 * 30 | |
23 | } | |
0e5ff97f | 24 | |
06215f15 C |
25 | export function parseDuration (duration: number | string): number { |
26 | if (typeof duration === 'number') return duration | |
27 | ||
28 | if (typeof duration === 'string') { | |
29 | const split = duration.match(/^([\d\.,]+)\s?(\w+)$/) | |
30 | ||
31 | if (split.length === 3) { | |
32 | const len = parseFloat(split[1]) | |
33 | let unit = split[2].replace(/s$/i,'').toLowerCase() | |
34 | if (unit === 'm') { | |
35 | unit = 'ms' | |
36 | } | |
37 | ||
38 | return (len || 1) * (timeTable[unit] || 0) | |
39 | } | |
40 | } | |
41 | ||
42 | throw new Error('Duration could not be properly parsed') | |
43 | } | |
44 | ||
0e5ff97f BY |
45 | export function parseBytes (value: string | number): number { |
46 | if (typeof value === 'number') return value | |
47 | ||
48 | const tgm = /^(\d+)\s*TB\s*(\d+)\s*GB\s*(\d+)\s*MB$/ | |
49 | const tg = /^(\d+)\s*TB\s*(\d+)\s*GB$/ | |
50 | const tm = /^(\d+)\s*TB\s*(\d+)\s*MB$/ | |
51 | const gm = /^(\d+)\s*GB\s*(\d+)\s*MB$/ | |
52 | const t = /^(\d+)\s*TB$/ | |
53 | const g = /^(\d+)\s*GB$/ | |
54 | const m = /^(\d+)\s*MB$/ | |
55 | const b = /^(\d+)\s*B$/ | |
56 | let match | |
57 | ||
58 | if (value.match(tgm)) { | |
59 | match = value.match(tgm) | |
60 | return parseInt(match[1], 10) * 1024 * 1024 * 1024 * 1024 | |
61 | + parseInt(match[2], 10) * 1024 * 1024 * 1024 | |
62 | + parseInt(match[3], 10) * 1024 * 1024 | |
63 | } else if (value.match(tg)) { | |
64 | match = value.match(tg) | |
65 | return parseInt(match[1], 10) * 1024 * 1024 * 1024 * 1024 | |
66 | + parseInt(match[2], 10) * 1024 * 1024 * 1024 | |
67 | } else if (value.match(tm)) { | |
68 | match = value.match(tm) | |
69 | return parseInt(match[1], 10) * 1024 * 1024 * 1024 * 1024 | |
70 | + parseInt(match[2], 10) * 1024 * 1024 | |
71 | } else if (value.match(gm)) { | |
72 | match = value.match(gm) | |
73 | return parseInt(match[1], 10) * 1024 * 1024 * 1024 | |
74 | + parseInt(match[2], 10) * 1024 * 1024 | |
75 | } else if (value.match(t)) { | |
76 | match = value.match(t) | |
77 | return parseInt(match[1], 10) * 1024 * 1024 * 1024 * 1024 | |
78 | } else if (value.match(g)) { | |
79 | match = value.match(g) | |
80 | return parseInt(match[1], 10) * 1024 * 1024 * 1024 | |
81 | } else if (value.match(m)) { | |
82 | match = value.match(m) | |
83 | return parseInt(match[1], 10) * 1024 * 1024 | |
84 | } else if (value.match(b)) { | |
85 | match = value.match(b) | |
86 | return parseInt(match[1], 10) * 1024 | |
87 | } else { | |
88 | return parseInt(value, 10) | |
89 | } | |
90 | } | |
91 | ||
225a89c2 C |
92 | function sanitizeUrl (url: string) { |
93 | const urlObject = new URL(url) | |
94 | ||
95 | if (urlObject.protocol === 'https:' && urlObject.port === '443') { | |
96 | urlObject.port = '' | |
97 | } else if (urlObject.protocol === 'http:' && urlObject.port === '80') { | |
98 | urlObject.port = '' | |
99 | } | |
100 | ||
101 | return urlObject.href.replace(/\/$/, '') | |
102 | } | |
103 | ||
104 | // Don't import remote scheme from constants because we are in core utils | |
105 | function sanitizeHost (host: string, remoteScheme: string) { | |
604abfbe | 106 | const toRemove = remoteScheme === 'https' ? 443 : 80 |
225a89c2 C |
107 | |
108 | return host.replace(new RegExp(`:${toRemove}$`), '') | |
109 | } | |
1840c2f7 C |
110 | |
111 | function isTestInstance () { | |
112 | return process.env.NODE_ENV === 'test' | |
113 | } | |
114 | ||
00f9e41e C |
115 | function isProdInstance () { |
116 | return process.env.NODE_ENV === 'production' | |
117 | } | |
118 | ||
1840c2f7 | 119 | function root () { |
fdbda9e3 C |
120 | // We are in /helpers/utils.js |
121 | const paths = [ __dirname, '..', '..' ] | |
122 | ||
123 | // We are under /dist directory | |
c1e791ba | 124 | if (process.mainModule && process.mainModule.filename.endsWith('.ts') === false) { |
fdbda9e3 C |
125 | paths.push('..') |
126 | } | |
127 | ||
128 | return join.apply(null, paths) | |
1840c2f7 C |
129 | } |
130 | ||
49347a0a C |
131 | // Thanks: https://stackoverflow.com/a/12034334 |
132 | function escapeHTML (stringParam) { | |
23e27dd5 C |
133 | if (!stringParam) return '' |
134 | ||
49347a0a C |
135 | const entityMap = { |
136 | '&': '&', | |
137 | '<': '<', | |
138 | '>': '>', | |
139 | '"': '"', | |
cf7a61b5 | 140 | '\'': ''', |
49347a0a C |
141 | '/': '/', |
142 | '`': '`', | |
143 | '=': '=' | |
144 | } | |
145 | ||
146 | return String(stringParam).replace(/[&<>"'`=\/]/g, s => entityMap[s]) | |
147 | } | |
148 | ||
e4f97bab C |
149 | function pageToStartAndCount (page: number, itemsPerPage: number) { |
150 | const start = (page - 1) * itemsPerPage | |
151 | ||
152 | return { start, count: itemsPerPage } | |
153 | } | |
154 | ||
0b4204f9 C |
155 | function buildPath (path: string) { |
156 | if (isAbsolute(path)) return path | |
157 | ||
158 | return join(root(), path) | |
159 | } | |
160 | ||
c73e83da C |
161 | // Consistent with .length, lodash truncate function is not |
162 | function peertubeTruncate (str: string, maxLength: number) { | |
163 | const options = { | |
164 | length: maxLength | |
165 | } | |
166 | const truncatedStr = truncate(str, options) | |
167 | ||
168 | // The truncated string is okay, we can return it | |
169 | if (truncatedStr.length <= maxLength) return truncatedStr | |
170 | ||
171 | // Lodash takes into account all UTF characters, whereas String.prototype.length does not: some characters have a length of 2 | |
172 | // We always use the .length so we need to truncate more if needed | |
173 | options.length -= truncatedStr.length - maxLength | |
174 | return truncate(str, options) | |
175 | } | |
176 | ||
729bb184 C |
177 | function sha256 (str: string, encoding: HexBase64Latin1Encoding = 'hex') { |
178 | return createHash('sha256').update(str).digest(encoding) | |
990b6a0b C |
179 | } |
180 | ||
6fcd19ba C |
181 | function promisify0<A> (func: (cb: (err: any, result: A) => void) => void): () => Promise<A> { |
182 | return function promisified (): Promise<A> { | |
183 | return new Promise<A>((resolve: (arg: A) => void, reject: (err: any) => void) => { | |
184 | func.apply(null, [ (err: any, res: A) => err ? reject(err) : resolve(res) ]) | |
185 | }) | |
186 | } | |
187 | } | |
188 | ||
189 | // Thanks to https://gist.github.com/kumasento/617daa7e46f13ecdd9b2 | |
190 | function promisify1<T, A> (func: (arg: T, cb: (err: any, result: A) => void) => void): (arg: T) => Promise<A> { | |
191 | return function promisified (arg: T): Promise<A> { | |
192 | return new Promise<A>((resolve: (arg: A) => void, reject: (err: any) => void) => { | |
193 | func.apply(null, [ arg, (err: any, res: A) => err ? reject(err) : resolve(res) ]) | |
194 | }) | |
195 | } | |
196 | } | |
197 | ||
198 | function promisify1WithVoid<T> (func: (arg: T, cb: (err: any) => void) => void): (arg: T) => Promise<void> { | |
199 | return function promisified (arg: T): Promise<void> { | |
200 | return new Promise<void>((resolve: () => void, reject: (err: any) => void) => { | |
201 | func.apply(null, [ arg, (err: any) => err ? reject(err) : resolve() ]) | |
202 | }) | |
203 | } | |
204 | } | |
205 | ||
206 | function promisify2<T, U, A> (func: (arg1: T, arg2: U, cb: (err: any, result: A) => void) => void): (arg1: T, arg2: U) => Promise<A> { | |
207 | return function promisified (arg1: T, arg2: U): Promise<A> { | |
208 | return new Promise<A>((resolve: (arg: A) => void, reject: (err: any) => void) => { | |
209 | func.apply(null, [ arg1, arg2, (err: any, res: A) => err ? reject(err) : resolve(res) ]) | |
210 | }) | |
211 | } | |
212 | } | |
213 | ||
214 | function promisify2WithVoid<T, U> (func: (arg1: T, arg2: U, cb: (err: any) => void) => void): (arg1: T, arg2: U) => Promise<void> { | |
215 | return function promisified (arg1: T, arg2: U): Promise<void> { | |
216 | return new Promise<void>((resolve: () => void, reject: (err: any) => void) => { | |
217 | func.apply(null, [ arg1, arg2, (err: any) => err ? reject(err) : resolve() ]) | |
218 | }) | |
219 | } | |
220 | } | |
221 | ||
6fcd19ba | 222 | const pseudoRandomBytesPromise = promisify1<number, Buffer>(pseudoRandomBytes) |
e4f97bab C |
223 | const createPrivateKey = promisify1<number, { key: string }>(pem.createPrivateKey) |
224 | const getPublicKey = promisify1<string, { publicKey: string }>(pem.getPublicKey) | |
6fcd19ba C |
225 | const bcryptComparePromise = promisify2<any, string, boolean>(bcrypt.compare) |
226 | const bcryptGenSaltPromise = promisify1<number, string>(bcrypt.genSalt) | |
53abc4c2 | 227 | const bcryptHashPromise = promisify2<any, string | number, string>(bcrypt.hash) |
6fcd19ba | 228 | const createTorrentPromise = promisify2<string, any, any>(createTorrent) |
499d9015 C |
229 | const execPromise2 = promisify2<string, any, string>(exec) |
230 | const execPromise = promisify1<string, string>(exec) | |
6fcd19ba | 231 | |
1840c2f7 C |
232 | // --------------------------------------------------------------------------- |
233 | ||
234 | export { | |
235 | isTestInstance, | |
00f9e41e C |
236 | isProdInstance, |
237 | ||
6fcd19ba | 238 | root, |
49347a0a | 239 | escapeHTML, |
e4f97bab | 240 | pageToStartAndCount, |
225a89c2 C |
241 | sanitizeUrl, |
242 | sanitizeHost, | |
0b4204f9 | 243 | buildPath, |
c73e83da | 244 | peertubeTruncate, |
990b6a0b | 245 | sha256, |
6fcd19ba C |
246 | |
247 | promisify0, | |
248 | promisify1, | |
e4f97bab | 249 | |
6fcd19ba | 250 | pseudoRandomBytesPromise, |
e4f97bab C |
251 | createPrivateKey, |
252 | getPublicKey, | |
6fcd19ba C |
253 | bcryptComparePromise, |
254 | bcryptGenSaltPromise, | |
255 | bcryptHashPromise, | |
499d9015 C |
256 | createTorrentPromise, |
257 | execPromise2, | |
258 | execPromise | |
1840c2f7 | 259 | } |