Merge branch 'develop'

This commit is contained in:
Alice Gaudon 2022-03-07 18:59:13 +01:00
commit 3946a5facc
3 changed files with 1460 additions and 1448 deletions

View File

@ -1,6 +1,6 @@
{
"name": "swaf",
"version": "0.25.0",
"version": "0.25.1",
"description": "Structure Web Application Framework.",
"repository": "https://eternae.ink/ashpie/swaf",
"author": "Alice Gaudon <alice@gaudon.pro>",
@ -25,9 +25,9 @@
},
"devDependencies": {
"@sveltejs/eslint-config": "sveltejs/eslint-config",
"@tsconfig/svelte": "^2.0.1",
"@tsconfig/svelte": "^3.0.0",
"@types/compression": "^1.7.0",
"@types/config": "^0.0.40",
"@types/config": "^0.0.41",
"@types/connect-flash": "^0.0.37",
"@types/cookie": "^0.4.0",
"@types/cookie-parser": "^1.4.2",
@ -38,11 +38,10 @@
"@types/jest": "^27.0.2",
"@types/mjml": "^4.0.4",
"@types/mysql": "^2.15.10",
"@types/node": "^16.11.10",
"@types/node": "^17.0.21",
"@types/nodemailer": "^6.4.0",
"@types/nunjucks": "^3.1.3",
"@types/on-finished": "^2.3.1",
"@types/redis": "^2.8.18",
"@types/require-from-string": "^1.2.0",
"@types/supertest": "^2.0.10",
"@types/uuid": "^8.0.0",
@ -50,7 +49,7 @@
"@typescript-eslint/eslint-plugin": "^5.3.0",
"@typescript-eslint/parser": "^5.3.0",
"chokidar": "^3.5.1",
"concurrently": "^6.0.0",
"concurrently": "^7.0.0",
"eslint": "^8.2.0",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-node": "^11.1.0",
@ -83,7 +82,7 @@
"express-session": "^1.17.1",
"formidable": "^2.0.1",
"geoip-lite": "^1.4.2",
"lucide": "^0.16.17",
"lucide": "^0.17.7",
"mjml": "^4.6.2",
"mysql": "^2.18.1",
"nanoid": "^3.1.20",
@ -91,7 +90,7 @@
"normalize.css": "^8.0.1",
"nunjucks": "^3.2.1",
"on-finished": "^2.3.0",
"redis": "^3.0.2",
"redis": "^4.0.4",
"require-from-string": "^2.0.2",
"rollup": "^2.42.3",
"rollup-plugin-css-only": "^3.1.0",

View File

@ -1,7 +1,7 @@
import config from "config";
import {Express} from "express";
import session, {Store} from "express-session";
import redis, {RedisClient} from "redis";
import {createClient, RedisClientType} from "redis";
import ApplicationComponent from "../ApplicationComponent.js";
import CacheProvider from "../CacheProvider.js";
@ -9,22 +9,31 @@ import {logger} from "../Logger.js";
export default class RedisComponent extends ApplicationComponent implements CacheProvider {
private readonly prefix: string = config.get('redis.prefix');
private redisClient?: RedisClient;
private redisClient?: RedisClientType;
private store: Store = new RedisStore(this);
public async start(_app: Express): Promise<void> {
this.redisClient = redis.createClient(config.get('redis.port'), config.get('redis.host'), {
const redisUrl = `redis://${config.get('redis.host')}:${config.get('redis.port')}`;
console.log(redisUrl);
this.redisClient = createClient({
url: redisUrl,
password: config.has('redis.password') ? config.get<string>('redis.password') : undefined,
});
this.redisClient.on('error', (err: Error) => {
logger.error(err, 'An error occurred with redis.');
});
await this.redisClient.connect();
}
public async stop(): Promise<void> {
const redisClient = this.redisClient;
if (redisClient) {
await this.close('Redis connection', callback => redisClient.quit(callback));
await this.close('Redis connection', callback => {
redisClient.quit()
.then(() => callback())
.catch(callback);
});
}
}
@ -33,24 +42,14 @@ export default class RedisComponent extends ApplicationComponent implements Cach
}
public isReady(): boolean {
return this.redisClient !== undefined && this.redisClient.connected;
return this.redisClient !== undefined && this.redisClient.isOpen;
}
public async get<T extends string | undefined>(key: string, defaultValue?: T): Promise<T> {
return await new Promise<T>((resolve, reject) => {
if (!this.redisClient) {
reject(`Redis client was not initialized.`);
return;
}
this.redisClient.get(this.prefix + key, (err, val) => {
if (err) {
reject(err);
return;
}
resolve((val || defaultValue || undefined) as T);
});
});
if (!this.redisClient) {
throw new Error(`Redis client was not initialized.`);
}
return (await this.redisClient.get(this.prefix + key)|| defaultValue || undefined) as T;
}
public async has(key: string): Promise<boolean> {
@ -58,49 +57,27 @@ export default class RedisComponent extends ApplicationComponent implements Cach
}
public async forget(key: string): Promise<void> {
return await new Promise<void>((resolve, reject) => {
if (!this.redisClient) {
reject(`Redis client was not initialized.`);
return;
}
if (!this.redisClient) {
throw new Error(`Redis client was not initialized.`);
}
this.redisClient.del(this.prefix + key, (err) => {
if (err) {
reject(err);
return;
}
resolve();
});
});
await this.redisClient.del(this.prefix + key);
}
public async remember(key: string, value: string, ttl: number): Promise<void> {
return await new Promise<void>((resolve, reject) => {
if (!this.redisClient) {
reject(`Redis client was not initialized.`);
return;
}
if (!this.redisClient) {
throw new Error(`Redis client was not initialized.`);
}
this.redisClient.psetex(this.prefix + key, ttl, value, (err) => {
if (err) return reject(err);
resolve();
});
});
await this.redisClient.pSetEx(this.prefix + key, ttl, value);
}
public async persist(key: string, ttl: number): Promise<void> {
return await new Promise<void>((resolve, reject) => {
if (!this.redisClient) {
reject(`Redis client was not initialized.`);
return;
}
if (!this.redisClient) {
throw new Error(`Redis client was not initialized.`);
}
this.redisClient.pexpire(this.prefix + key, ttl, (err) => {
if (err) return reject(err);
resolve();
});
});
await this.redisClient.pExpire(this.prefix + key, ttl);
}
}

2810
yarn.lock

File diff suppressed because it is too large Load Diff