Skip to content

Commit

Permalink
Merge pull request #58 from nanaya/ioredis
Browse files Browse the repository at this point in the history
Switch to ioredis
  • Loading branch information
notbakaneko authored Mar 25, 2024
2 parents 45040f9 + a65356b commit a038d06
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 93 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
"cookie": "^0.5.0",
"dotenv": "^16.3.1",
"hot-shots": "^10.0.0",
"ioredis": "^5.3.2",
"jsonwebtoken": "^9.0.1",
"mysql2": "^3.6.0",
"php-serialize": "^4.1.1",
"redis": "^4.6.7",
"source-map-support": "^0.5.21",
"typescript": "^5.1.6",
"ws": "^8.13.0"
Expand Down
22 changes: 9 additions & 13 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import * as fs from 'fs';
import * as path from 'path';
import * as dotenv from 'dotenv';
import { RedisOptions } from 'ioredis';
import { PoolOptions as DbConfig } from 'mysql2';
import { RedisClientOptions } from 'redis';
import { ServerOptions as ServerConfig } from 'ws';

interface Config {
Expand All @@ -25,8 +25,8 @@ interface DbNames {
}

interface RedisConfigs {
app: RedisClientOptions;
notification: RedisClientOptions;
app: RedisOptions;
notification: RedisOptions;
}

let baseDir = process.env.WEBSOCKET_BASEDIR;
Expand Down Expand Up @@ -64,20 +64,16 @@ const config: Config = {
: Buffer.from(process.env.PASSPORT_PUBLIC_KEY),
redis: {
app: {
database: +(process.env.REDIS_DB || 0),
db: +(process.env.REDIS_DB || 0),
host: process.env.REDIS_HOST || '127.0.0.1',
password: process.env.REDIS_PASSWORD,
socket: {
host: process.env.REDIS_HOST || '127.0.0.1',
port: +(process.env.REDIS_PORT || 6379),
},
port: +(process.env.REDIS_PORT || 6379),
},
notification: {
database: +(process.env.NOTIFICATION_REDIS_DB || 0),
db: +(process.env.NOTIFICATION_REDIS_DB || 0),
host: process.env.NOTIFICATION_REDIS_HOST || '127.0.0.1',
password: process.env.NOTIFICATION_REDIS_PASSWORD,
socket: {
host: process.env.NOTIFICATION_REDIS_HOST || '127.0.0.1',
port: +(process.env.NOTIFICATION_REDIS_PORT || 6379),
},
port: +(process.env.NOTIFICATION_REDIS_PORT || 6379),
},
},
server: {
Expand Down
8 changes: 3 additions & 5 deletions src/laravel-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import * as crypto from 'crypto';
import * as http from 'http';
import * as url from 'url';
import * as cookie from 'cookie';
import { RedisOptions, Redis } from 'ioredis';
import { unserialize } from 'php-serialize';
import { RedisClientOptions, createClient as redisCreateClient } from 'redis';

interface Params {
appKey: string;
redisConfig: RedisClientOptions;
redisConfig: RedisOptions;
}

interface EncryptedSession {
Expand Down Expand Up @@ -60,9 +60,7 @@ export default class LaravelSession {
private sessionCookieNameHmac: Buffer;

constructor(params: Params) {
this.redis = redisCreateClient(params.redisConfig);
this.redis.on('error', () => { /* dummy listener to apply reconnectStrategy */ });
void this.redis.connect();
this.redis = new Redis(params.redisConfig);
this.key = Buffer.from(params.appKey.slice('base64:'.length), 'base64');
// https://github.com/laravel/framework/blob/208c3976f186dcdfa0a434f4092bae7d32928465/src/Illuminate/Cookie/CookieValuePrefix.php
this.sessionCookieNameHmac = crypto.createHmac('sha1', this.key).update(`${sessionCookieName}v2`).digest();
Expand Down
17 changes: 7 additions & 10 deletions src/redis-subscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
// See the LICENCE file in the repository root for full licence text.

import { StatsD } from 'hot-shots';
import { RedisClientOptions, createClient } from 'redis';
import { RedisOptions, Redis } from 'ioredis';
import logger from './logger';
import Message from './types/message';
import UserConnection from './user-connection';

interface Params {
dogstatsd: StatsD;
redisConfig: RedisClientOptions;
redisConfig: RedisOptions;
}

export default class RedisSubscriber {
Expand All @@ -19,9 +19,8 @@ export default class RedisSubscriber {

constructor(params: Params) {
this.dogstatsd = params.dogstatsd;
this.redis = createClient(params.redisConfig);
this.redis.on('error', () => { /* dummy listener to apply reconnectStrategy */ });
void this.redis.connect();
this.redis = new Redis(params.redisConfig);
this.redis.on('message', this.onMessage);
}

subscribe(channels: string | string[], connection: UserConnection) {
Expand All @@ -45,9 +44,7 @@ export default class RedisSubscriber {
}

if (toSubscribe.length > 0) {
for (const channel of toSubscribe) {
void this.redis.subscribe(channel, this.onMessage);
}
void this.redis.subscribe(...toSubscribe);
}
}

Expand Down Expand Up @@ -76,11 +73,11 @@ export default class RedisSubscriber {
}

if (toUnsubscribe.length > 0) {
void this.redis.unsubscribe(toUnsubscribe);
void this.redis.unsubscribe(...toUnsubscribe);
}
}

private readonly onMessage = (messageString: string, channel: string) => {
private readonly onMessage = (channel: string, messageString: string) => {
logger.debug(`received message from channel ${channel}`);

const connections = this.userConnections[channel];
Expand Down
Loading

0 comments on commit a038d06

Please sign in to comment.