@@ -7,44 +7,51 @@ const HTTP_MANAGEMENT_PORT = 8222;
7
7
const USER_ARGUMENT_KEY = "--user" ;
8
8
const PASS_ARGUMENT_KEY = "--pass" ;
9
9
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
-
21
10
export class NatsContainer extends GenericContainer {
22
- private args : { [ name : string ] : string } = { } ;
11
+ private args = new Set < string > ( ) ;
12
+ private values = new Map < string , string | undefined > ( ) ;
23
13
24
14
constructor ( image = "nats:2.8.4-alpine" ) {
25
15
super ( image ) ;
26
16
27
- this . args [ USER_ARGUMENT_KEY ] = "test" ;
28
- this . args [ PASS_ARGUMENT_KEY ] = "test" ;
17
+ this . withUsername ( "test" ) ;
18
+ this . withPass ( "test" ) ;
29
19
30
20
this . withExposedPorts ( CLIENT_PORT , ROUTING_PORT_FOR_CLUSTERING , HTTP_MANAGEMENT_PORT )
31
21
. withWaitStrategy ( Wait . forLogMessage ( / .* S e r v e r i s r e a d y .* / ) )
32
22
. withStartupTimeout ( 120_000 ) ;
33
23
}
34
24
25
+ /**
26
+ * Enable JetStream
27
+ *
28
+ * @returns {this }
29
+ */
30
+ public withJetStream ( ) : this {
31
+ this . withArg ( "--jetstream" ) ;
32
+ return this ;
33
+ }
34
+
35
35
public withUsername ( user : string ) : this {
36
- this . args [ USER_ARGUMENT_KEY ] = user ;
36
+ this . withArg ( USER_ARGUMENT_KEY , user ) ;
37
37
return this ;
38
38
}
39
39
40
40
public withPass ( pass : string ) : this {
41
- this . args [ PASS_ARGUMENT_KEY ] = pass ;
41
+ this . withArg ( PASS_ARGUMENT_KEY , pass ) ;
42
42
return this ;
43
43
}
44
44
45
- public withArg ( name : string , value : string ) {
46
- name = NatsContainer . ensureDashInFrontOfArgumentName ( name ) ;
47
- this . args [ name ] = value ;
45
+ public withArg ( name : string , value : string ) : this;
46
+ public withArg ( name : string ) : this;
47
+ public withArg ( ...args : [ string , string ] | [ string ] ) : this {
48
+ const [ name , value ] = args ;
49
+
50
+ const correctName = NatsContainer . ensureDashInFrontOfArgumentName ( name ) ;
51
+ this . args . add ( correctName ) ;
52
+ if ( args . length === 2 ) {
53
+ this . values . set ( correctName , value ) ;
54
+ }
48
55
return this ;
49
56
}
50
57
@@ -61,23 +68,41 @@ export class NatsContainer extends GenericContainer {
61
68
}
62
69
63
70
public override async start ( ) : Promise < StartedNatsContainer > {
64
- this . withCommand ( buildCmdsFromArgs ( this . args ) ) ;
71
+ this . withCommand ( this . getNormalizedCommand ( ) ) ;
65
72
return new StartedNatsContainer ( await super . start ( ) , this . getUser ( ) , this . getPass ( ) ) ;
66
73
}
67
74
68
- private getUser ( ) : string {
69
- return this . args [ USER_ARGUMENT_KEY ] ;
75
+ private getUser ( ) : string | undefined {
76
+ return this . values . get ( USER_ARGUMENT_KEY ) ;
70
77
}
71
78
72
- private getPass ( ) : string {
73
- return this . args [ PASS_ARGUMENT_KEY ] ;
79
+ private getPass ( ) : string | undefined {
80
+ return this . values . get ( PASS_ARGUMENT_KEY ) ;
81
+ }
82
+
83
+ private getNormalizedCommand ( ) : string [ ] {
84
+ const result : string [ ] = [ "nats-server" ] ;
85
+ for ( const arg of this . args ) {
86
+ result . push ( arg ) ;
87
+ if ( this . values . has ( arg ) ) {
88
+ const value = this . values . get ( arg ) ;
89
+ if ( value ) {
90
+ result . push ( value ) ;
91
+ }
92
+ }
93
+ }
94
+ return result ;
74
95
}
75
96
}
76
97
77
98
export class StartedNatsContainer extends AbstractStartedContainer {
78
99
private readonly connectionOptions : NatsConnectionOptions ;
79
100
80
- constructor ( startedTestContainer : StartedTestContainer , readonly username : string , readonly password : string ) {
101
+ constructor (
102
+ startedTestContainer : StartedTestContainer ,
103
+ readonly username : string | undefined ,
104
+ readonly password : string | undefined
105
+ ) {
81
106
super ( startedTestContainer ) ;
82
107
const port = startedTestContainer . getMappedPort ( CLIENT_PORT ) ;
83
108
this . connectionOptions = {
0 commit comments