import { UnimplementedError } from "../errors" ; import { BasicRedisClusterOptions , BasicRedisOptions , BasicRedisSentinelOptions , BasicRedisSocketOptions , BasicRedisTcpOptions , } from "./basic_options" ; import * as IoRedis from "ioredis" ; see createOptions export interface RedisOptions { param override returns createClient ( override ? ) : IoRedis . Redis ; returns createUri ( ) : string ; } param options returns const appendDefaultOptions = < T extends BasicRedisOptions > ( options ) : T => { const appended = { ... options as object , dropBufferSupport : true , keyPrefix : "celery-task-meta-" , stringNumbers : true , } ; return appended as T ; } ; /**
 */ const maybeOverride = < T extends BasicRedisOptions > ( options , override ? ) : T => { if ( typeof override === "undefined" ) { return options ; } return { ... options as object , ... override } as T ; } ; /**
 * `RedisTcpOptions` creates Redis clients that connect to a single database
 * over TCP.
 */ export class RedisTcpOptions implements RedisOptions { public readonly options : BasicRedisTcpOptions ; public constructor ( options ) { this . options = appendDefaultOptions ( options ) ; } public createClient ( override ? ) { return new IoRedis ( maybeOverride ( this . options , override ) ) ; } public createUri ( ) { let uri = "redis" ; if ( typeof this . options . tls !== "undefined" ) { uri += "s" ; } uri += "://" ; if ( typeof this . options . password !== "undefined" ) { uri += `:${ this . options . password }@` ; } if ( typeof this . options . host !== "undefined" || typeof this . options . password !== "undefined" ) { if ( typeof this . options . host === "undefined" ) { uri += "localhost" ; } else { uri += this . options . host ; } if ( typeof this . options . port !== "undefined" ) { uri += `:${ this . options . port }` ; } } if ( typeof this . options . db !== "undefined" ) { uri += `/${ this . options . db }` ; } return uri ; } } /**
 * `RedisSocketOptions` creates Redis clients that connect to a single database
 * over Unix Socket.
 */ export class RedisSocketOptions implements RedisOptions { public readonly options : BasicRedisSocketOptions ; public constructor ( options ) { this . options = appendDefaultOptions ( options ) ; } public createClient ( override ? ) { return new IoRedis ( maybeOverride ( this . options , override ) ) ; } public createUri ( ) { let uri = "redis" ; if ( typeof this . options . tls !== "undefined" ) { uri += "s" ; } uri += `+socket://${ this . options . path }` ; if ( typeof this . options . password !== "undefined" ) { uri += `?password=${ this . options . password }` ; } return uri ; } } /**
 * `RedisSentinelOptions` creates Redis clients that connect to a group of Redis
 * Sentinel nodes, automatically discovering slave nodes in the network.
 */ export class RedisSentinelOptions implements RedisOptions { public readonly options : BasicRedisSentinelOptions ; public constructor ( options ) { this . options = appendDefaultOptions ( options ) ; } public createClient ( override ? ) { return new IoRedis ( maybeOverride ( this . options , override ) ) ; } public createUri ( ) { throw new UnimplementedError ( ) ; } } /**
 * `RedisClusterOptions` creates Redis clients that connect to a Redis
 * Cluster network.
 */ export class RedisClusterOptions implements RedisOptions { public readonly options : BasicRedisClusterOptions ; public constructor ( options ) { this . options = ( ( ) => { if ( typeof options . redisOptions === "undefined" ) { return options ; } return { ... options , redisOptions : appendDefaultOptions ( options . redisOptions ) , } ; } ) ( ) ; } public createClient ( ) { return new IoRedis . Cluster ( this . options . nodes , this . options ) ; } public createUri ( ) { throw new UnimplementedError ( ) ; } } export const DEFAULT_REDIS_OPTIONS = new RedisTcpOptions ( { protocol : "redis" } ) ; param options returns export const createOptions = ( options ) : RedisOptions => { if ( isCluster ( options ) ) { return new RedisClusterOptions ( options ) ; } else if ( ( options as BasicRedisSentinelOptions ) . sentinels ) { return new RedisSentinelOptions ( options as BasicRedisSentinelOptions ) ; } else if ( ( options as BasicRedisSocketOptions ) . path ) { return new RedisSocketOptions ( options as BasicRedisSocketOptions ) ; } return new RedisTcpOptions ( { ... ( options as BasicRedisTcpOptions ) , protocol : ( ( ) => { if ( options . tls ) { return "rediss" ; } return "redis" ; } ) ( ) , } ) ; } ; export type NativeOptions = IoRedis . RedisOptions | BasicRedisClusterOptions ; const isCluster = ( options ) : options is BasicRedisClusterOptions => ( options as BasicRedisClusterOptions ) . nodes !== undefined ;