@@ -7,44 +7,48 @@ const HTTP_MANAGEMENT_PORT = 8222;
77const USER_ARGUMENT_KEY = "--user" ;
88const PASS_ARGUMENT_KEY = "--pass" ;
99
10- function buildCmdsFromArgs ( args : { [ p : string ] : string } ) : string [ ] {
11- const result : string [ ] = [ ] ;
12- result . push ( "nats-server" ) ;
13-
14- for ( const argsKey in args ) {
15- result . push ( argsKey ) ;
16- result . push ( args [ argsKey ] ) ;
17- }
18- return result ;
19- }
20-
2110export class NatsContainer extends GenericContainer {
22- private args : { [ name : string ] : string } = { } ;
11+ private args = new Set < string > ( )
12+ private values = new Map < string , string > ( )
2313
2414 constructor ( image = "nats:2.8.4-alpine" ) {
2515 super ( image ) ;
26-
27- this . args [ USER_ARGUMENT_KEY ] = "test" ;
28- this . args [ PASS_ARGUMENT_KEY ] = "test" ;
16+ this . withUsername ( 'test' )
17+ this . withPass ( 'test' )
2918
3019 this . withExposedPorts ( CLIENT_PORT , ROUTING_PORT_FOR_CLUSTERING , HTTP_MANAGEMENT_PORT )
3120 . withWaitStrategy ( Wait . forLogMessage ( / .* S e r v e r i s r e a d y .* / ) )
3221 . withStartupTimeout ( 120_000 ) ;
3322 }
3423
3524 public withUsername ( user : string ) : this {
36- this . args [ USER_ARGUMENT_KEY ] = user ;
25+ this . withArg ( USER_ARGUMENT_KEY , user )
26+ return this ;
27+ }
28+
29+ /**
30+ * Enable JetStream
31+ * @returns {this }
32+ */
33+ public withJS ( ) : this {
34+ this . withArg ( '-js' )
3735 return this ;
3836 }
3937
4038 public withPass ( pass : string ) : this {
41- this . args [ PASS_ARGUMENT_KEY ] = pass ;
39+ this . withArg ( PASS_ARGUMENT_KEY , pass )
4240 return this ;
4341 }
4442
45- public withArg ( name : string , value : string ) {
43+ public withArg ( name : string , value : string ) : this
44+ public withArg ( name : string ) : this
45+ public withArg ( ...args : [ string , string ] | [ string ] ) : this {
46+ let [ name , value ] = args
4647 name = NatsContainer . ensureDashInFrontOfArgumentName ( name ) ;
47- this . args [ name ] = value ;
48+ this . args . add ( name )
49+ if ( args . length === 2 ) {
50+ this . values . set ( name , value ! )
51+ }
4852 return this ;
4953 }
5054
@@ -61,16 +65,27 @@ export class NatsContainer extends GenericContainer {
6165 }
6266
6367 public override async start ( ) : Promise < StartedNatsContainer > {
64- this . withCommand ( buildCmdsFromArgs ( this . args ) ) ;
68+ this . withCommand ( this . getNormalizedCommand ( ) ) ;
6569 return new StartedNatsContainer ( await super . start ( ) , this . getUser ( ) , this . getPass ( ) ) ;
6670 }
6771
6872 private getUser ( ) : string {
69- return this . args [ USER_ARGUMENT_KEY ] ;
73+ return this . values . get ( USER_ARGUMENT_KEY ) ! ;
7074 }
7175
7276 private getPass ( ) : string {
73- return this . args [ PASS_ARGUMENT_KEY ] ;
77+ return this . values . get ( PASS_ARGUMENT_KEY ) ! ;
78+ }
79+
80+ private getNormalizedCommand ( ) : string [ ] {
81+ const result : string [ ] = [ 'nats-server' ]
82+ for ( const arg of this . args ) {
83+ result . push ( arg )
84+ if ( this . values . has ( arg ) ) {
85+ result . push ( this . values . get ( arg ) ! )
86+ }
87+ }
88+ return result
7489 }
7590}
7691
0 commit comments