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