import { BasicRedisSentinelOptions as Options , RedisSentinelAuthority as Authority , } from "../basic_options" ; import { ParseError } from "../../errors" ; import { asScalar , QueryParser } from "../../query_parser" ; import { getScheme , parseUri , Scheme , Uri } from "../../uri" ; import { isNullOrUndefined } from "../../utility" ; param rawUri returns throws export const parseSentinelUri = ( rawUris ) : Options => { const split = rawUris . split ( ";" ) ; const parsed = split . map ( parseIndividual ) ; const sentinels = parsed . map ( ( [ sentinel , _ ] ) => sentinel ) ; const individualOptions = parsed . map ( ( [ _ , query ] ) => query ) ; const options = individualOptions . reduce ( ( lhs , rhs ) => ( { ... lhs , ... rhs } ) ) ; if ( isNullOrUndefined ( options . name ) ) { throw new ParseError ( "sentinel options missing name" ) ; } return { ... options as StrongSentinelQueries , protocol : "sentinel" , sentinels , } ; } ; /**
 * Weak Sentinel queries - not guaranteed to contain necessary queries for a
 * Sentinel client.
 */ interface SentinelQueries { name ? : string ; role ? : "master" | "slave" ; } /**
 * Strong Sentinel queries - guaranteed to contain necessary queries for the
 * construction of a Sentinel client.
 */ interface StrongSentinelQueries { name : string ; role ? : "master" | "slave" ; } param rawUri returns throws const parseIndividual = ( rawUri ) : [ Authority , SentinelQueries ] => { const uri = parseUri ( rawUri ) ; const authority = parseAuthority ( uri ) ; const queries = parseQueries ( uri ) ; return [ authority , queries ] ; } ; param uri returns throws const parseAuthority = ( uri ) : Authority => { const scheme = getScheme ( uri . raw ) ; if ( ! isSentinelScheme ( scheme ) ) { throw new ParseError ( `URI ${ uri . raw } is not a sentinel URI.` ) ; } else if ( isNullOrUndefined ( uri . authority ) ) { throw new ParseError ( `URI ${ uri . raw } is missing its authority.` ) ; } else if ( isNullOrUndefined ( uri . authority . port ) ) { throw new ParseError ( `URI "${ uri . raw }" is missing a port.` ) ; } return { host : uri . authority . host , port : uri . authority . port , } ; } ; param uri returns throws const parseQueries = ( uri ) : SentinelQueries => { if ( isNullOrUndefined ( uri . query ) ) { return { } ; } const rawQuery = uri . query ; const parser = new QueryParser < SentinelQueries > ( [ { source : "name" } , { parser : ( x ) => parseRole ( asScalar ( x ) ) , source : "role" } , ] ) ; return parser . parse ( rawQuery , { } ) ; } ; param scheme returns const isSentinelScheme = ( scheme ) : boolean => scheme === Scheme . RedisSentinel || scheme === Scheme . RedisSentinelSecure ; param role returns throws const parseRole = ( role ) : "master" | "slave" => { if ( role !== "master" && role !== "slave" ) { throw new ParseError ( `role "${ role }" is not "master" or "slave"` ) ; } return role ; } ;