import { AmqpOptions } from "./options" ; import { ParseError } from "../errors" ; import { createIntegerQueryDescriptor , QueryParser , } from "../query_parser" ; import { getScheme , parseUri , Queries , Scheme , Uri } from "../uri" ; import { isNullOrUndefined } from "../utility" ; import * as _ from "underscore" ; param rawUri returns throws export const parseAmqpUri = ( rawUri ) : AmqpOptions => { const scheme = getScheme ( rawUri ) ; if ( scheme !== Scheme . Amqp && scheme !== Scheme . AmqpSecure && scheme !== Scheme . Rpc && scheme !== Scheme . RpcSecure ) { throw new ParseError ( `unrecognized scheme "${ scheme }"` ) ; } const uri = parseUri ( rawUri ) ; if ( isNullOrUndefined ( uri . authority ) ) { throw new ParseError ( `"${ rawUri }" missing authority` ) ; } const protocol = ( ( ) => { switch ( scheme ) { case Scheme . Rpc : return Scheme . Amqp ; case Scheme . RpcSecure : return Scheme . AmqpSecure ; } return scheme ; } ) ( ) ; const withOptions = appendOptions ( uri , { hostname : uri . authority . host , protocol , } ) ; const withVhost = appendVhost ( rawUri , withOptions ) ; const withQueries = appendQueries ( uri , withVhost ) ; return withQueries ; } ; param uri param appending returns const appendOptions = ( uri , appending ) : AmqpOptions => { type FunctionList = Array < ( options : AmqpOptions ) => AmqpOptions > ; const functions = [ appendPass ( uri ) , appendPort ( uri ) , appendUser ( uri ) ] ; return functions . reduce ( ( x , f ) => f ( x ) , appending ) ; } ; param uri param appending returns throws const appendQueries = ( uri , appending ) : AmqpOptions => { if ( isNullOrUndefined ( uri . query ) ) { return appending ; } const query = uri . query ; const parser = new QueryParser < AmqpOptions > ( [ createIntegerQueryDescriptor ( "channelMax" ) , createIntegerQueryDescriptor ( "frameMax" ) , createIntegerQueryDescriptor ( "heartbeat" ) , { source : "locale" } , ] ) ; return parser . parse ( query , appending ) ; } ; param uri param appending returns const appendVhost = ( rawUri , appending ) : AmqpOptions => { const maybeMatches = /^.+:\/\/[^/]*\/([\w\d-.~%]*)$/ . exec ( rawUri ) ; if ( isNullOrUndefined ( maybeMatches ) ) { return appending ; } const vhost = decodeURIComponent ( maybeMatches [ 1 ] ) ; return { ... appending , vhost , } ; } ; param uri param appending returns const appendPass = ( uri ) => ( options ) : AmqpOptions => { if ( isNullOrUndefined ( uri . authority ) || isNullOrUndefined ( uri . authority . userInfo ) || isNullOrUndefined ( uri . authority . userInfo . pass ) ) { return options ; } return { ... options , password : uri . authority . userInfo . pass , } ; } ; param uri param appending returns const appendPort = ( uri ) => ( options ) : AmqpOptions => { if ( isNullOrUndefined ( uri . authority ) || isNullOrUndefined ( uri . authority . port ) ) { return options ; } return { ... options , port : uri . authority . port , } ; } ; param uri param appending returns const appendUser = ( uri ) => ( options ) : AmqpOptions => { if ( isNullOrUndefined ( uri . authority ) || isNullOrUndefined ( uri . authority . userInfo ) ) { return options ; } return { ... options , username : uri . authority . userInfo . user , } ; } ;