1
0
Fork 0
peertube/server/lib/redis.ts

161 lines
4.2 KiB
TypeScript
Raw Normal View History

2018-04-17 12:01:06 +00:00
import * as express from 'express'
2018-01-30 12:27:07 +00:00
import { createClient, RedisClient } from 'redis'
import { logger } from '../helpers/logger'
import { generateRandomString } from '../helpers/utils'
2018-05-11 07:44:04 +00:00
import { CONFIG, USER_PASSWORD_RESET_LIFETIME, VIDEO_VIEW_LIFETIME } from '../initializers'
2018-04-17 12:01:06 +00:00
type CachedRoute = {
body: string,
contentType?: string
statusCode?: string
}
2018-01-30 12:27:07 +00:00
class Redis {
private static instance: Redis
private initialized = false
private client: RedisClient
private prefix: string
private constructor () {}
init () {
// Already initialized
if (this.initialized === true) return
this.initialized = true
this.client = createClient({
host: CONFIG.REDIS.HOSTNAME,
port: CONFIG.REDIS.PORT,
db: CONFIG.REDIS.DB
2018-01-30 12:27:07 +00:00
})
this.client.on('error', err => {
2018-03-26 13:54:13 +00:00
logger.error('Error in Redis client.', { err })
2018-01-30 12:27:07 +00:00
process.exit(-1)
})
if (CONFIG.REDIS.AUTH) {
this.client.auth(CONFIG.REDIS.AUTH)
}
this.prefix = 'redis-' + CONFIG.WEBSERVER.HOST + '-'
}
async setResetPasswordVerificationString (userId: number) {
const generatedString = await generateRandomString(32)
await this.setValue(this.generateResetPasswordKey(userId), generatedString, USER_PASSWORD_RESET_LIFETIME)
return generatedString
}
async getResetPasswordLink (userId: number) {
return this.getValue(this.generateResetPasswordKey(userId))
}
2018-02-23 15:39:51 +00:00
setView (ip: string, videoUUID: string) {
return this.setValue(this.buildViewKey(ip, videoUUID), '1', VIDEO_VIEW_LIFETIME)
}
async isViewExists (ip: string, videoUUID: string) {
return this.exists(this.buildViewKey(ip, videoUUID))
}
2018-04-17 12:01:06 +00:00
async getCachedRoute (req: express.Request) {
const cached = await this.getObject(this.buildCachedRouteKey(req))
return cached as CachedRoute
}
2018-05-11 07:44:04 +00:00
setCachedRoute (req: express.Request, body: any, lifetime: number, contentType?: string, statusCode?: number) {
2018-04-17 12:01:06 +00:00
const cached: CachedRoute = {
body: body.toString(),
contentType,
statusCode: statusCode.toString()
}
2018-05-11 07:44:04 +00:00
return this.setObject(this.buildCachedRouteKey(req), cached, lifetime)
2018-04-17 12:01:06 +00:00
}
generateResetPasswordKey (userId: number) {
return 'reset-password-' + userId
}
buildViewKey (ip: string, videoUUID: string) {
return videoUUID + '-' + ip
}
buildCachedRouteKey (req: express.Request) {
return req.method + '-' + req.originalUrl
}
2018-01-30 12:27:07 +00:00
private getValue (key: string) {
return new Promise<string>((res, rej) => {
this.client.get(this.prefix + key, (err, value) => {
if (err) return rej(err)
return res(value)
})
})
}
private setValue (key: string, value: string, expirationMilliseconds: number) {
return new Promise<void>((res, rej) => {
this.client.set(this.prefix + key, value, 'PX', expirationMilliseconds, (err, ok) => {
if (err) return rej(err)
2018-04-17 12:01:06 +00:00
if (ok !== 'OK') return rej(new Error('Redis set result is not OK.'))
2018-01-30 12:27:07 +00:00
return res()
})
})
}
2018-04-17 12:01:06 +00:00
private setObject (key: string, obj: { [ id: string ]: string }, expirationMilliseconds: number) {
return new Promise<void>((res, rej) => {
this.client.hmset(this.prefix + key, obj, (err, ok) => {
if (err) return rej(err)
if (!ok) return rej(new Error('Redis mset result is not OK.'))
this.client.pexpire(this.prefix + key, expirationMilliseconds, (err, ok) => {
if (err) return rej(err)
if (!ok) return rej(new Error('Redis expiration result is not OK.'))
return res()
})
})
})
}
private getObject (key: string) {
return new Promise<{ [ id: string ]: string }>((res, rej) => {
this.client.hgetall(this.prefix + key, (err, value) => {
if (err) return rej(err)
return res(value)
})
})
}
2018-02-23 15:39:51 +00:00
private exists (key: string) {
return new Promise<boolean>((res, rej) => {
this.client.exists(this.prefix + key, (err, existsNumber) => {
if (err) return rej(err)
return res(existsNumber === 1)
})
})
}
2018-01-30 12:27:07 +00:00
static get Instance () {
return this.instance || (this.instance = new this())
}
}
// ---------------------------------------------------------------------------
export {
Redis
}