Skip to content

Commit

Permalink
fix: Do not depend on redis or memcached typings
Browse files Browse the repository at this point in the history
  • Loading branch information
alexkvak committed May 7, 2020
1 parent 490acec commit 95e1f2c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 9 deletions.
30 changes: 26 additions & 4 deletions src/adapters/MemcachedStorageAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
import { StorageAdapter } from "../StorageAdapter";
import Memcached from "memcached";
import { EventEmitter } from "events";
import { ConnectionStatus } from "../ConnectionStatus";
import customError from "../errors/custom-error";
import { StorageAdapter } from "../StorageAdapter";

export const DEFAULT_LOCK_EXPIRES = 20000;

interface Memcached extends EventEmitter {
add(
key: string,
value: string,
lifetime: number,
cb: (err: Error | undefined, result: boolean) => void
): void;

get(key: string, cb: (err: Error | undefined, data: string) => void): void;

getMulti(keys: string[], cb: (err: Error | undefined, data: { [key: string]: string }) => void): void;

set(
key: string,
value: string,
lifetime: number,
cb: (err: Error | undefined, result: boolean) => void
): void;

del(key: string, cb: (err: Error | undefined, result: boolean) => void): void;
}

function operationError(op: string, err: Error): Error {
return customError("operationError", `Operation "${op}" error. ${err.message}`, err);
}
Expand Down Expand Up @@ -38,7 +60,7 @@ export class MemcachedStorageAdapter implements StorageAdapter {
/**
* The method should call the callback passed to it as soon as the storage is ready to execute commands.
*/
onConnect(callback: (err: Memcached.IssueData) => void): void {
onConnect(callback: (err: unknown) => void): void {
this.memcachedInstance.on("reconnect", callback);
}

Expand Down Expand Up @@ -143,7 +165,7 @@ export class MemcachedStorageAdapter implements StorageAdapter {
async acquireLock(key: string, lockExpireTimeout?: number): Promise<boolean> {
const expiresIn = lockExpireTimeout !== undefined ? lockExpireTimeout : this.options.lockExpireTimeout;

return new Promise<boolean>((resolve, reject) => {
return new Promise((resolve, reject) => {
this.memcachedInstance.add(`${key}_lock`, "", this.getLifetimeFromMs(expiresIn), (err, result) => {
if (err) {
return reject(operationError("acquireLock", err));
Expand Down
26 changes: 23 additions & 3 deletions src/adapters/RedisStorageAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import IORedis, { Redis } from "ioredis";
import { EventEmitter } from "events";
import _, { partial } from "lodash";
import { ConnectionStatus } from "../ConnectionStatus";
import { StorageAdapter } from "../StorageAdapter";
Expand All @@ -10,6 +10,26 @@ import { withTimeout } from "../with-timeout";
export const DEFAULT_OPERATION_TIMEOUT = 150;
export const DEFAULT_LOCK_EXPIRES = 20000;

interface Redis extends EventEmitter {
del(...keys: string[]): Promise<number>;

get(key: string): Promise<string | null>;

exists(...keys: string[]): Promise<number>;

set(
key: string,
value: string,
expiryMode?: string,
time?: number | string,
setMode?: number | string
): Promise<string>;

mget(...keys: string[]): Promise<Array<string | null>>;

mset(data: Map<string, string>): Promise<"OK">;
}

export type RedisStorageAdapterOptions = {
operationTimeout?: number;
lockExpireTimeout?: number;
Expand Down Expand Up @@ -82,8 +102,8 @@ export class RedisStorageAdapter implements StorageAdapter {
/**
* Set multiple values to redis storage
*/
public async mset(values: Map<string, IORedis.ValueType>): Promise<void> {
const data = new Map<string, IORedis.ValueType>();
public async mset(values: Map<string, string>): Promise<void> {
const data = new Map<string, string>();

for (const [key, value] of values.entries()) {
data.set(key, value);
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/redis.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import Redis, { Redis as RedisType } from "ioredis";
import Redis from "ioredis";
import RedisStorageAdapter from "../../src/adapters/RedisStorageAdapter";
import { runAdapterTests } from "./adapter-agnostic";

const redis: RedisType = new Redis();
const redis = new Redis();
const adapter = new RedisStorageAdapter(redis, { lockExpireTimeout: 50 });

describe("Redis adapter", () => {
Expand Down

0 comments on commit 95e1f2c

Please sign in to comment.