From b43190d048b9518c677f7bb77f046daf3a0f1dd4 Mon Sep 17 00:00:00 2001 From: Timo Hocker Date: Wed, 10 Aug 2022 16:17:00 +0200 Subject: [PATCH] fix redis sync --- lib/Redis.ts | 87 ++++++++++++++++++++++++++-------------------------- package.json | 2 +- 2 files changed, 45 insertions(+), 44 deletions(-) diff --git a/lib/Redis.ts b/lib/Redis.ts index d427d70..a459b1a 100644 --- a/lib/Redis.ts +++ b/lib/Redis.ts @@ -6,81 +6,82 @@ */ import IORedis from 'ioredis'; -import { debug } from './debug'; -import { LabelledKey } from './Key'; +import {debug} from './debug'; +import {LabelledKey} from './Key'; -const logger = debug ('redis'); +const logger = debug('redis'); export class Redis { private _redis: IORedis | null = null; - public connect (url: string): void { - const log = logger.extend ('connect'); - log ('connecting to redis instance %s', url); + public connect(url: string): void { + const log = logger.extend('connect'); + log('connecting to redis instance %s', url); if (this._redis !== null) { - log ('disconnecting existing redis client'); - this.disconnect (); + log('disconnecting existing redis client'); + this.disconnect(); } - this._redis = new IORedis (url); - this._redis.on ('connect', () => { - log ('connected'); + this._redis = new IORedis(url); + this._redis.on('connect', () => { + log('connected'); }); - this._redis.on ('ready', () => { - log ('ready'); + this._redis.on('ready', () => { + log('ready'); }); - this._redis.on ('error', (err) => { - log ('error %o', err); + this._redis.on('error', (err) => { + log('error %o', err); }); - this._redis.on ('reconnecting', () => { - log ('reconnecting'); + this._redis.on('reconnecting', () => { + log('reconnecting'); }); - this._redis.on ('end', () => { - log ('connection ended'); + this._redis.on('end', () => { + log('connection ended'); }); } - public disconnect (): void { - const log = logger.extend ('disconnect'); - log ('disconnecting redis client'); + public disconnect(): void { + const log = logger.extend('disconnect'); + log('disconnecting redis client'); if (this._redis === null) { - log ('redis is inactive, skipping'); + log('redis is inactive, skipping'); return; } - this._redis.quit (); + this._redis.quit(); this._redis = null; - log ('done'); + log('done'); } - public async set_key (key: LabelledKey): Promise { - const log = logger.extend ('set_key'); - log ('trying to set key %s to redis', key.index); + public async set_key(key: LabelledKey): Promise { + const log = logger.extend('set_key'); + log('trying to set key %s to redis', key.index); if (this._redis === null) { - log ('redis is inactive, skipping'); + log('redis is inactive, skipping'); return; } - const valid_for = (key.valid_until - (new Date) - .getTime ()) / 1000; - log ('key is valid for %d seconds', valid_for); - await this._redis.setex (key.index, valid_for, JSON.stringify (key)); - log ('saved key'); + const valid_for = Math.floor( + (key.valid_until - new Date().getTime()) / 1000 + ); + log('key is valid for %d seconds', valid_for); + await this._redis.setex(key.index, valid_for, JSON.stringify(key)); + log('saved key'); } - public async get_key (index: string): Promise { - const log = logger.extend ('get_key'); - log ('trying to get key %s from redis', index); + public async get_key(index: string): Promise { + const log = logger.extend('get_key'); + log('trying to get key %s from redis', index); if (this._redis === null) { - log ('redis is inactive, skipping'); + log('redis is inactive, skipping'); return null; } - const res = await this._redis.get (index); + const res = await this._redis.get(index); if (res === null) { - log ('key not found in redis'); + log('key not found in redis'); return null; } - log ('key found'); - return JSON.parse (res); + log('key found'); + return JSON.parse(res); } } -export const redis = new Redis; +export const redis = new Redis(); diff --git a/package.json b/package.json index 713c4fd..cb8ac5e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@sapphirecode/auth-server-helper", - "version": "3.3.2", + "version": "3.3.3", "main": "dist/lib/index.js", "author": { "name": "Timo Hocker",