Skip to content

Commit 7afe4d9

Browse files
author
afedorov
committed
feat(nats): added js and support arg without value
1 parent 27f858a commit 7afe4d9

File tree

2 files changed

+75
-22
lines changed

2 files changed

+75
-22
lines changed

packages/modules/nats/src/nats-container.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,44 @@ describe("NatsContainer", () => {
6767
});
6868
// }
6969

70+
// JetStream {
71+
it("should start with JetStream ", async () => {
72+
// set username and password like this
73+
const container = await new NatsContainer().withJS().start();
74+
75+
const nc = await connect(container.getConnectionOptions());
76+
77+
// just take manager for check js
78+
const jsm = await nc.jetstream().jetstreamManager()
79+
80+
// close the connection
81+
await nc.close();
82+
// check if the close was OK
83+
const err = await nc.closed();
84+
expect(err).toBe(undefined);
85+
86+
await container.stop();
87+
});
88+
89+
it("should fail without JetStream ", async () => {
90+
// set username and password like this
91+
const container = await new NatsContainer().start();
92+
93+
const nc = await connect(container.getConnectionOptions());
94+
95+
// just take manager for check js
96+
await expect(nc.jetstream().jetstreamManager()).rejects.toThrowError('503')
97+
98+
// close the connection
99+
await nc.close();
100+
// check if the close was OK
101+
const err = await nc.closed();
102+
expect(err).toBe(undefined);
103+
104+
await container.stop();
105+
});
106+
// }
107+
70108
it("should immediately end when started with version argument ", async () => {
71109
// for the complete list of available arguments see:
72110
// See Command Line Options section inside [NATS docker image documentation](https://hub.docker.com/_/nats)

packages/modules/nats/src/nats-container.ts

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,44 +7,48 @@ const HTTP_MANAGEMENT_PORT = 8222;
77
const USER_ARGUMENT_KEY = "--user";
88
const 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-
2110
export 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(/.*Server is ready.*/))
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

Comments
 (0)