]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/core-utils.ts
Bumped to version v1.0.0-beta.2
[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'
8d468a16
C
8import { pseudoRandomBytes } from 'crypto'
9import { readdir, readFile, rename, stat, Stats, unlink, writeFile } from 'fs'
10import * as mkdirp from 'mkdirp'
0b4204f9 11import { isAbsolute, join } from 'path'
e4f97bab 12import * as pem from 'pem'
8d468a16 13import * as rimraf from 'rimraf'
225a89c2
C
14import { URL } from 'url'
15
16function sanitizeUrl (url: string) {
17 const urlObject = new URL(url)
18
19 if (urlObject.protocol === 'https:' && urlObject.port === '443') {
20 urlObject.port = ''
21 } else if (urlObject.protocol === 'http:' && urlObject.port === '80') {
22 urlObject.port = ''
23 }
24
25 return urlObject.href.replace(/\/$/, '')
26}
27
28// Don't import remote scheme from constants because we are in core utils
29function sanitizeHost (host: string, remoteScheme: string) {
604abfbe 30 const toRemove = remoteScheme === 'https' ? 443 : 80
225a89c2
C
31
32 return host.replace(new RegExp(`:${toRemove}$`), '')
33}
1840c2f7
C
34
35function isTestInstance () {
36 return process.env.NODE_ENV === 'test'
37}
38
39function root () {
fdbda9e3
C
40 // We are in /helpers/utils.js
41 const paths = [ __dirname, '..', '..' ]
42
43 // We are under /dist directory
44 if (process.mainModule.filename.endsWith('.ts') === false) {
45 paths.push('..')
46 }
47
48 return join.apply(null, paths)
1840c2f7
C
49}
50
49347a0a
C
51// Thanks: https://stackoverflow.com/a/12034334
52function escapeHTML (stringParam) {
23e27dd5
C
53 if (!stringParam) return ''
54
49347a0a
C
55 const entityMap = {
56 '&': '&',
57 '<': '&lt;',
58 '>': '&gt;',
59 '"': '&quot;',
60 "'": '&#39;',
61 '/': '&#x2F;',
62 '`': '&#x60;',
63 '=': '&#x3D;'
64 }
65
66 return String(stringParam).replace(/[&<>"'`=\/]/g, s => entityMap[s])
67}
68
e4f97bab
C
69function pageToStartAndCount (page: number, itemsPerPage: number) {
70 const start = (page - 1) * itemsPerPage
71
72 return { start, count: itemsPerPage }
73}
74
0b4204f9
C
75function buildPath (path: string) {
76 if (isAbsolute(path)) return path
77
78 return join(root(), path)
79}
80
6fcd19ba
C
81function promisify0<A> (func: (cb: (err: any, result: A) => void) => void): () => Promise<A> {
82 return function promisified (): Promise<A> {
83 return new Promise<A>((resolve: (arg: A) => void, reject: (err: any) => void) => {
84 func.apply(null, [ (err: any, res: A) => err ? reject(err) : resolve(res) ])
85 })
86 }
87}
88
89// Thanks to https://gist.github.com/kumasento/617daa7e46f13ecdd9b2
90function promisify1<T, A> (func: (arg: T, cb: (err: any, result: A) => void) => void): (arg: T) => Promise<A> {
91 return function promisified (arg: T): Promise<A> {
92 return new Promise<A>((resolve: (arg: A) => void, reject: (err: any) => void) => {
93 func.apply(null, [ arg, (err: any, res: A) => err ? reject(err) : resolve(res) ])
94 })
95 }
96}
97
98function promisify1WithVoid<T> (func: (arg: T, cb: (err: any) => void) => void): (arg: T) => Promise<void> {
99 return function promisified (arg: T): Promise<void> {
100 return new Promise<void>((resolve: () => void, reject: (err: any) => void) => {
101 func.apply(null, [ arg, (err: any) => err ? reject(err) : resolve() ])
102 })
103 }
104}
105
106function promisify2<T, U, A> (func: (arg1: T, arg2: U, cb: (err: any, result: A) => void) => void): (arg1: T, arg2: U) => Promise<A> {
107 return function promisified (arg1: T, arg2: U): Promise<A> {
108 return new Promise<A>((resolve: (arg: A) => void, reject: (err: any) => void) => {
109 func.apply(null, [ arg1, arg2, (err: any, res: A) => err ? reject(err) : resolve(res) ])
110 })
111 }
112}
113
114function promisify2WithVoid<T, U> (func: (arg1: T, arg2: U, cb: (err: any) => void) => void): (arg1: T, arg2: U) => Promise<void> {
115 return function promisified (arg1: T, arg2: U): Promise<void> {
116 return new Promise<void>((resolve: () => void, reject: (err: any) => void) => {
117 func.apply(null, [ arg1, arg2, (err: any) => err ? reject(err) : resolve() ])
118 })
119 }
120}
121
6fcd19ba
C
122const readFileBufferPromise = promisify1<string, Buffer>(readFile)
123const unlinkPromise = promisify1WithVoid<string>(unlink)
124const renamePromise = promisify2WithVoid<string, string>(rename)
556ddc31 125const writeFilePromise = promisify2WithVoid<string, any>(writeFile)
6fcd19ba
C
126const readdirPromise = promisify1<string, string[]>(readdir)
127const mkdirpPromise = promisify1<string, string>(mkdirp)
128const pseudoRandomBytesPromise = promisify1<number, Buffer>(pseudoRandomBytes)
e4f97bab
C
129const createPrivateKey = promisify1<number, { key: string }>(pem.createPrivateKey)
130const getPublicKey = promisify1<string, { publicKey: string }>(pem.getPublicKey)
6fcd19ba
C
131const bcryptComparePromise = promisify2<any, string, boolean>(bcrypt.compare)
132const bcryptGenSaltPromise = promisify1<number, string>(bcrypt.genSalt)
53abc4c2 133const bcryptHashPromise = promisify2<any, string | number, string>(bcrypt.hash)
6fcd19ba 134const createTorrentPromise = promisify2<string, any, any>(createTorrent)
f981dae8 135const rimrafPromise = promisify1WithVoid<string>(rimraf)
40298b02 136const statPromise = promisify1<string, Stats>(stat)
6fcd19ba 137
1840c2f7
C
138// ---------------------------------------------------------------------------
139
140export {
141 isTestInstance,
6fcd19ba 142 root,
49347a0a 143 escapeHTML,
e4f97bab 144 pageToStartAndCount,
225a89c2
C
145 sanitizeUrl,
146 sanitizeHost,
0b4204f9 147 buildPath,
6fcd19ba
C
148
149 promisify0,
150 promisify1,
e4f97bab 151
6fcd19ba 152 readdirPromise,
6fcd19ba
C
153 readFileBufferPromise,
154 unlinkPromise,
155 renamePromise,
156 writeFilePromise,
157 mkdirpPromise,
158 pseudoRandomBytesPromise,
e4f97bab
C
159 createPrivateKey,
160 getPublicKey,
6fcd19ba
C
161 bcryptComparePromise,
162 bcryptGenSaltPromise,
163 bcryptHashPromise,
f981dae8 164 createTorrentPromise,
40298b02 165 rimrafPromise,
efc32059 166 statPromise
1840c2f7 167}