diff options
Diffstat (limited to 'server/helpers/core-utils.ts')
-rw-r--r-- | server/helpers/core-utils.ts | 38 |
1 files changed, 18 insertions, 20 deletions
diff --git a/server/helpers/core-utils.ts b/server/helpers/core-utils.ts index 935fd22d9..b93868c12 100644 --- a/server/helpers/core-utils.ts +++ b/server/helpers/core-utils.ts | |||
@@ -10,7 +10,9 @@ import { BinaryToTextEncoding, createHash, randomBytes } from 'crypto' | |||
10 | import { truncate } from 'lodash' | 10 | import { truncate } from 'lodash' |
11 | import { basename, isAbsolute, join, resolve } from 'path' | 11 | import { basename, isAbsolute, join, resolve } from 'path' |
12 | import * as pem from 'pem' | 12 | import * as pem from 'pem' |
13 | import { pipeline } from 'stream' | ||
13 | import { URL } from 'url' | 14 | import { URL } from 'url' |
15 | import { promisify } from 'util' | ||
14 | 16 | ||
15 | const objectConverter = (oldObject: any, keyConverter: (e: string) => string, valueConverter: (e: any) => any) => { | 17 | const objectConverter = (oldObject: any, keyConverter: (e: string) => string, valueConverter: (e: any) => any) => { |
16 | if (!oldObject || typeof oldObject !== 'object') { | 18 | if (!oldObject || typeof oldObject !== 'object') { |
@@ -152,24 +154,6 @@ function root () { | |||
152 | return rootPath | 154 | return rootPath |
153 | } | 155 | } |
154 | 156 | ||
155 | // Thanks: https://stackoverflow.com/a/12034334 | ||
156 | function escapeHTML (stringParam) { | ||
157 | if (!stringParam) return '' | ||
158 | |||
159 | const entityMap = { | ||
160 | '&': '&', | ||
161 | '<': '<', | ||
162 | '>': '>', | ||
163 | '"': '"', | ||
164 | '\'': ''', | ||
165 | '/': '/', | ||
166 | '`': '`', | ||
167 | '=': '=' | ||
168 | } | ||
169 | |||
170 | return String(stringParam).replace(/[&<>"'`=/]/g, s => entityMap[s]) | ||
171 | } | ||
172 | |||
173 | function pageToStartAndCount (page: number, itemsPerPage: number) { | 157 | function pageToStartAndCount (page: number, itemsPerPage: number) { |
174 | const start = (page - 1) * itemsPerPage | 158 | const start = (page - 1) * itemsPerPage |
175 | 159 | ||
@@ -249,11 +233,23 @@ function promisify2<T, U, A> (func: (arg1: T, arg2: U, cb: (err: any, result: A) | |||
249 | } | 233 | } |
250 | } | 234 | } |
251 | 235 | ||
236 | type SemVersion = { major: number, minor: number, patch: number } | ||
237 | function parseSemVersion (s: string) { | ||
238 | const parsed = s.match(/^v?(\d+)\.(\d+)\.(\d+)$/i) | ||
239 | |||
240 | return { | ||
241 | major: parseInt(parsed[1]), | ||
242 | minor: parseInt(parsed[2]), | ||
243 | patch: parseInt(parsed[3]) | ||
244 | } as SemVersion | ||
245 | } | ||
246 | |||
252 | const randomBytesPromise = promisify1<number, Buffer>(randomBytes) | 247 | const randomBytesPromise = promisify1<number, Buffer>(randomBytes) |
253 | const createPrivateKey = promisify1<number, { key: string }>(pem.createPrivateKey) | 248 | const createPrivateKey = promisify1<number, { key: string }>(pem.createPrivateKey) |
254 | const getPublicKey = promisify1<string, { publicKey: string }>(pem.getPublicKey) | 249 | const getPublicKey = promisify1<string, { publicKey: string }>(pem.getPublicKey) |
255 | const execPromise2 = promisify2<string, any, string>(exec) | 250 | const execPromise2 = promisify2<string, any, string>(exec) |
256 | const execPromise = promisify1<string, string>(exec) | 251 | const execPromise = promisify1<string, string>(exec) |
252 | const pipelinePromise = promisify(pipeline) | ||
257 | 253 | ||
258 | // --------------------------------------------------------------------------- | 254 | // --------------------------------------------------------------------------- |
259 | 255 | ||
@@ -264,7 +260,6 @@ export { | |||
264 | 260 | ||
265 | objectConverter, | 261 | objectConverter, |
266 | root, | 262 | root, |
267 | escapeHTML, | ||
268 | pageToStartAndCount, | 263 | pageToStartAndCount, |
269 | sanitizeUrl, | 264 | sanitizeUrl, |
270 | sanitizeHost, | 265 | sanitizeHost, |
@@ -284,5 +279,8 @@ export { | |||
284 | createPrivateKey, | 279 | createPrivateKey, |
285 | getPublicKey, | 280 | getPublicKey, |
286 | execPromise2, | 281 | execPromise2, |
287 | execPromise | 282 | execPromise, |
283 | pipelinePromise, | ||
284 | |||
285 | parseSemVersion | ||
288 | } | 286 | } |