]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/core-utils.ts
Add outbox
[github/Chocobozzz/PeerTube.git] / server / helpers / core-utils.ts
1 /*
2 Different from 'utils' because we don't not import other PeerTube modules.
3 Useful to avoid circular dependencies.
4 */
5
6 import { join } from 'path'
7 import { pseudoRandomBytes } from 'crypto'
8 import {
9 readdir,
10 readFile,
11 rename,
12 unlink,
13 writeFile,
14 access,
15 stat,
16 Stats
17 } from 'fs'
18 import * as mkdirp from 'mkdirp'
19 import * as bcrypt from 'bcrypt'
20 import * as createTorrent from 'create-torrent'
21 import * as rimraf from 'rimraf'
22 import * as pem from 'pem'
23
24 function isTestInstance () {
25 return process.env.NODE_ENV === 'test'
26 }
27
28 function root () {
29 // We are in /helpers/utils.js
30 const paths = [ __dirname, '..', '..' ]
31
32 // We are under /dist directory
33 if (process.mainModule.filename.endsWith('.ts') === false) {
34 paths.push('..')
35 }
36
37 return join.apply(null, paths)
38 }
39
40 // Thanks: https://stackoverflow.com/a/12034334
41 function escapeHTML (stringParam) {
42 const entityMap = {
43 '&': '&',
44 '<': '&lt;',
45 '>': '&gt;',
46 '"': '&quot;',
47 "'": '&#39;',
48 '/': '&#x2F;',
49 '`': '&#x60;',
50 '=': '&#x3D;'
51 }
52
53 return String(stringParam).replace(/[&<>"'`=\/]/g, s => entityMap[s])
54 }
55
56 function pageToStartAndCount (page: number, itemsPerPage: number) {
57 const start = (page - 1) * itemsPerPage
58
59 return { start, count: itemsPerPage }
60 }
61
62 function promisify0<A> (func: (cb: (err: any, result: A) => void) => void): () => Promise<A> {
63 return function promisified (): Promise<A> {
64 return new Promise<A>((resolve: (arg: A) => void, reject: (err: any) => void) => {
65 func.apply(null, [ (err: any, res: A) => err ? reject(err) : resolve(res) ])
66 })
67 }
68 }
69
70 // Thanks to https://gist.github.com/kumasento/617daa7e46f13ecdd9b2
71 function promisify1<T, A> (func: (arg: T, cb: (err: any, result: A) => void) => void): (arg: T) => Promise<A> {
72 return function promisified (arg: T): Promise<A> {
73 return new Promise<A>((resolve: (arg: A) => void, reject: (err: any) => void) => {
74 func.apply(null, [ arg, (err: any, res: A) => err ? reject(err) : resolve(res) ])
75 })
76 }
77 }
78
79 function promisify1WithVoid<T> (func: (arg: T, cb: (err: any) => void) => void): (arg: T) => Promise<void> {
80 return function promisified (arg: T): Promise<void> {
81 return new Promise<void>((resolve: () => void, reject: (err: any) => void) => {
82 func.apply(null, [ arg, (err: any) => err ? reject(err) : resolve() ])
83 })
84 }
85 }
86
87 function promisify2<T, U, A> (func: (arg1: T, arg2: U, cb: (err: any, result: A) => void) => void): (arg1: T, arg2: U) => Promise<A> {
88 return function promisified (arg1: T, arg2: U): Promise<A> {
89 return new Promise<A>((resolve: (arg: A) => void, reject: (err: any) => void) => {
90 func.apply(null, [ arg1, arg2, (err: any, res: A) => err ? reject(err) : resolve(res) ])
91 })
92 }
93 }
94
95 function promisify2WithVoid<T, U> (func: (arg1: T, arg2: U, cb: (err: any) => void) => void): (arg1: T, arg2: U) => Promise<void> {
96 return function promisified (arg1: T, arg2: U): Promise<void> {
97 return new Promise<void>((resolve: () => void, reject: (err: any) => void) => {
98 func.apply(null, [ arg1, arg2, (err: any) => err ? reject(err) : resolve() ])
99 })
100 }
101 }
102
103 const readFilePromise = promisify2<string, string, string>(readFile)
104 const readFileBufferPromise = promisify1<string, Buffer>(readFile)
105 const unlinkPromise = promisify1WithVoid<string>(unlink)
106 const renamePromise = promisify2WithVoid<string, string>(rename)
107 const writeFilePromise = promisify2WithVoid<string, any>(writeFile)
108 const readdirPromise = promisify1<string, string[]>(readdir)
109 const mkdirpPromise = promisify1<string, string>(mkdirp)
110 const pseudoRandomBytesPromise = promisify1<number, Buffer>(pseudoRandomBytes)
111 const accessPromise = promisify1WithVoid<string | Buffer>(access)
112 const createPrivateKey = promisify1<number, { key: string }>(pem.createPrivateKey)
113 const getPublicKey = promisify1<string, { publicKey: string }>(pem.getPublicKey)
114 const bcryptComparePromise = promisify2<any, string, boolean>(bcrypt.compare)
115 const bcryptGenSaltPromise = promisify1<number, string>(bcrypt.genSalt)
116 const bcryptHashPromise = promisify2<any, string | number, string>(bcrypt.hash)
117 const createTorrentPromise = promisify2<string, any, any>(createTorrent)
118 const rimrafPromise = promisify1WithVoid<string>(rimraf)
119 const statPromise = promisify1<string, Stats>(stat)
120
121 // ---------------------------------------------------------------------------
122
123 export {
124 isTestInstance,
125 root,
126 escapeHTML,
127 pageToStartAndCount,
128
129 promisify0,
130 promisify1,
131
132 readdirPromise,
133 readFilePromise,
134 readFileBufferPromise,
135 unlinkPromise,
136 renamePromise,
137 writeFilePromise,
138 mkdirpPromise,
139 pseudoRandomBytesPromise,
140 accessPromise,
141 createPrivateKey,
142 getPublicKey,
143 bcryptComparePromise,
144 bcryptGenSaltPromise,
145 bcryptHashPromise,
146 createTorrentPromise,
147 rimrafPromise,
148 statPromise
149 }