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