]>
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' | |
3e17515e | 8 | import { createHash, pseudoRandomBytes } from 'crypto' |
0138af92 | 9 | import { copyFile, readdir, readFile, rename, stat, Stats, unlink, writeFile } from 'fs' |
8d468a16 | 10 | import * as mkdirp from 'mkdirp' |
0b4204f9 | 11 | import { isAbsolute, join } from 'path' |
e4f97bab | 12 | import * as pem from 'pem' |
8d468a16 | 13 | import * as rimraf from 'rimraf' |
225a89c2 | 14 | import { URL } from 'url' |
c73e83da | 15 | import { truncate } from 'lodash' |
225a89c2 | 16 | |
06215f15 C |
17 | const timeTable = { |
18 | ms: 1, | |
19 | second: 1000, | |
20 | minute: 60000, | |
21 | hour: 3600000, | |
22 | day: 3600000 * 24, | |
23 | week: 3600000 * 24 * 7, | |
24 | month: 3600000 * 24 * 30 | |
25 | } | |
26 | export function parseDuration (duration: number | string): number { | |
27 | if (typeof duration === 'number') return duration | |
28 | ||
29 | if (typeof duration === 'string') { | |
30 | const split = duration.match(/^([\d\.,]+)\s?(\w+)$/) | |
31 | ||
32 | if (split.length === 3) { | |
33 | const len = parseFloat(split[1]) | |
34 | let unit = split[2].replace(/s$/i,'').toLowerCase() | |
35 | if (unit === 'm') { | |
36 | unit = 'ms' | |
37 | } | |
38 | ||
39 | return (len || 1) * (timeTable[unit] || 0) | |
40 | } | |
41 | } | |
42 | ||
43 | throw new Error('Duration could not be properly parsed') | |
44 | } | |
45 | ||
225a89c2 C |
46 | function sanitizeUrl (url: string) { |
47 | const urlObject = new URL(url) | |
48 | ||
49 | if (urlObject.protocol === 'https:' && urlObject.port === '443') { | |
50 | urlObject.port = '' | |
51 | } else if (urlObject.protocol === 'http:' && urlObject.port === '80') { | |
52 | urlObject.port = '' | |
53 | } | |
54 | ||
55 | return urlObject.href.replace(/\/$/, '') | |
56 | } | |
57 | ||
58 | // Don't import remote scheme from constants because we are in core utils | |
59 | function sanitizeHost (host: string, remoteScheme: string) { | |
604abfbe | 60 | const toRemove = remoteScheme === 'https' ? 443 : 80 |
225a89c2 C |
61 | |
62 | return host.replace(new RegExp(`:${toRemove}$`), '') | |
63 | } | |
1840c2f7 C |
64 | |
65 | function isTestInstance () { | |
66 | return process.env.NODE_ENV === 'test' | |
67 | } | |
68 | ||
69 | function root () { | |
fdbda9e3 C |
70 | // We are in /helpers/utils.js |
71 | const paths = [ __dirname, '..', '..' ] | |
72 | ||
73 | // We are under /dist directory | |
c1e791ba | 74 | if (process.mainModule && process.mainModule.filename.endsWith('.ts') === false) { |
fdbda9e3 C |
75 | paths.push('..') |
76 | } | |
77 | ||
78 | return join.apply(null, paths) | |
1840c2f7 C |
79 | } |
80 | ||
49347a0a C |
81 | // Thanks: https://stackoverflow.com/a/12034334 |
82 | function escapeHTML (stringParam) { | |
23e27dd5 C |
83 | if (!stringParam) return '' |
84 | ||
49347a0a C |
85 | const entityMap = { |
86 | '&': '&', | |
87 | '<': '<', | |
88 | '>': '>', | |
89 | '"': '"', | |
cf7a61b5 | 90 | '\'': ''', |
49347a0a C |
91 | '/': '/', |
92 | '`': '`', | |
93 | '=': '=' | |
94 | } | |
95 | ||
96 | return String(stringParam).replace(/[&<>"'`=\/]/g, s => entityMap[s]) | |
97 | } | |
98 | ||
e4f97bab C |
99 | function pageToStartAndCount (page: number, itemsPerPage: number) { |
100 | const start = (page - 1) * itemsPerPage | |
101 | ||
102 | return { start, count: itemsPerPage } | |
103 | } | |
104 | ||
0b4204f9 C |
105 | function buildPath (path: string) { |
106 | if (isAbsolute(path)) return path | |
107 | ||
108 | return join(root(), path) | |
109 | } | |
110 | ||
c73e83da C |
111 | // Consistent with .length, lodash truncate function is not |
112 | function peertubeTruncate (str: string, maxLength: number) { | |
113 | const options = { | |
114 | length: maxLength | |
115 | } | |
116 | const truncatedStr = truncate(str, options) | |
117 | ||
118 | // The truncated string is okay, we can return it | |
119 | if (truncatedStr.length <= maxLength) return truncatedStr | |
120 | ||
121 | // Lodash takes into account all UTF characters, whereas String.prototype.length does not: some characters have a length of 2 | |
122 | // We always use the .length so we need to truncate more if needed | |
123 | options.length -= truncatedStr.length - maxLength | |
124 | return truncate(str, options) | |
125 | } | |
126 | ||
990b6a0b | 127 | function sha256 (str: string) { |
3e17515e | 128 | return createHash('sha256').update(str).digest('hex') |
990b6a0b C |
129 | } |
130 | ||
6fcd19ba C |
131 | function promisify0<A> (func: (cb: (err: any, result: A) => void) => void): () => Promise<A> { |
132 | return function promisified (): Promise<A> { | |
133 | return new Promise<A>((resolve: (arg: A) => void, reject: (err: any) => void) => { | |
134 | func.apply(null, [ (err: any, res: A) => err ? reject(err) : resolve(res) ]) | |
135 | }) | |
136 | } | |
137 | } | |
138 | ||
139 | // Thanks to https://gist.github.com/kumasento/617daa7e46f13ecdd9b2 | |
140 | function promisify1<T, A> (func: (arg: T, cb: (err: any, result: A) => void) => void): (arg: T) => Promise<A> { | |
141 | return function promisified (arg: T): Promise<A> { | |
142 | return new Promise<A>((resolve: (arg: A) => void, reject: (err: any) => void) => { | |
143 | func.apply(null, [ arg, (err: any, res: A) => err ? reject(err) : resolve(res) ]) | |
144 | }) | |
145 | } | |
146 | } | |
147 | ||
148 | function promisify1WithVoid<T> (func: (arg: T, cb: (err: any) => void) => void): (arg: T) => Promise<void> { | |
149 | return function promisified (arg: T): Promise<void> { | |
150 | return new Promise<void>((resolve: () => void, reject: (err: any) => void) => { | |
151 | func.apply(null, [ arg, (err: any) => err ? reject(err) : resolve() ]) | |
152 | }) | |
153 | } | |
154 | } | |
155 | ||
156 | function promisify2<T, U, A> (func: (arg1: T, arg2: U, cb: (err: any, result: A) => void) => void): (arg1: T, arg2: U) => Promise<A> { | |
157 | return function promisified (arg1: T, arg2: U): Promise<A> { | |
158 | return new Promise<A>((resolve: (arg: A) => void, reject: (err: any) => void) => { | |
159 | func.apply(null, [ arg1, arg2, (err: any, res: A) => err ? reject(err) : resolve(res) ]) | |
160 | }) | |
161 | } | |
162 | } | |
163 | ||
164 | function promisify2WithVoid<T, U> (func: (arg1: T, arg2: U, cb: (err: any) => void) => void): (arg1: T, arg2: U) => Promise<void> { | |
165 | return function promisified (arg1: T, arg2: U): Promise<void> { | |
166 | return new Promise<void>((resolve: () => void, reject: (err: any) => void) => { | |
167 | func.apply(null, [ arg1, arg2, (err: any) => err ? reject(err) : resolve() ]) | |
168 | }) | |
169 | } | |
170 | } | |
171 | ||
0138af92 | 172 | const copyFilePromise = promisify2WithVoid<string, string>(copyFile) |
6fcd19ba C |
173 | const readFileBufferPromise = promisify1<string, Buffer>(readFile) |
174 | const unlinkPromise = promisify1WithVoid<string>(unlink) | |
175 | const renamePromise = promisify2WithVoid<string, string>(rename) | |
556ddc31 | 176 | const writeFilePromise = promisify2WithVoid<string, any>(writeFile) |
6fcd19ba C |
177 | const readdirPromise = promisify1<string, string[]>(readdir) |
178 | const mkdirpPromise = promisify1<string, string>(mkdirp) | |
c1e791ba | 179 | // we cannot modify the Promise types, so we should make the promisify instance check mkdirp |
6fcd19ba | 180 | const pseudoRandomBytesPromise = promisify1<number, Buffer>(pseudoRandomBytes) |
e4f97bab C |
181 | const createPrivateKey = promisify1<number, { key: string }>(pem.createPrivateKey) |
182 | const getPublicKey = promisify1<string, { publicKey: string }>(pem.getPublicKey) | |
6fcd19ba C |
183 | const bcryptComparePromise = promisify2<any, string, boolean>(bcrypt.compare) |
184 | const bcryptGenSaltPromise = promisify1<number, string>(bcrypt.genSalt) | |
53abc4c2 | 185 | const bcryptHashPromise = promisify2<any, string | number, string>(bcrypt.hash) |
6fcd19ba | 186 | const createTorrentPromise = promisify2<string, any, any>(createTorrent) |
f981dae8 | 187 | const rimrafPromise = promisify1WithVoid<string>(rimraf) |
40298b02 | 188 | const statPromise = promisify1<string, Stats>(stat) |
6fcd19ba | 189 | |
1840c2f7 C |
190 | // --------------------------------------------------------------------------- |
191 | ||
192 | export { | |
193 | isTestInstance, | |
6fcd19ba | 194 | root, |
49347a0a | 195 | escapeHTML, |
e4f97bab | 196 | pageToStartAndCount, |
225a89c2 C |
197 | sanitizeUrl, |
198 | sanitizeHost, | |
0b4204f9 | 199 | buildPath, |
c73e83da | 200 | peertubeTruncate, |
990b6a0b | 201 | sha256, |
6fcd19ba C |
202 | |
203 | promisify0, | |
204 | promisify1, | |
e4f97bab | 205 | |
0138af92 | 206 | copyFilePromise, |
6fcd19ba | 207 | readdirPromise, |
6fcd19ba C |
208 | readFileBufferPromise, |
209 | unlinkPromise, | |
210 | renamePromise, | |
211 | writeFilePromise, | |
212 | mkdirpPromise, | |
213 | pseudoRandomBytesPromise, | |
e4f97bab C |
214 | createPrivateKey, |
215 | getPublicKey, | |
6fcd19ba C |
216 | bcryptComparePromise, |
217 | bcryptGenSaltPromise, | |
218 | bcryptHashPromise, | |
f981dae8 | 219 | createTorrentPromise, |
40298b02 | 220 | rimrafPromise, |
efc32059 | 221 | statPromise |
1840c2f7 | 222 | } |