Skip to content

Commit

Permalink
[FIX] StreamUpdateConfig added subjects, also unmarked optional `ma…
Browse files Browse the repository at this point in the history
…x_msgs_per_subject`, `max_msg_size`, `discard`, and `duplicate_window` - The update api, makes all these fields optional as it is a partial, but when the StreamConfig is returned, these values will now be set to their server default values (expected to be set).

[FIX] StreamConfig removes `subjects` as it is provided by the extended interface of StreamUpdateConfig

[CHANGE] jsm.streams.update now takes a Partial<StreamUpdateConfig> - this relaxes all fields to be optional

[FIX] jsm.stream.update(name, Partial<StreamUpdateConfig>) will now set the name field - if the update was done without an initial copy of the stream config, this value might not be set by default and result in the server returning an error
  • Loading branch information
aricart committed Feb 10, 2022
1 parent d4f1b48 commit 5da2f2a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
8 changes: 5 additions & 3 deletions nats-base-client/jsmstream_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export class StreamAPIImpl extends BaseApiClient implements StreamAPI {

async update(
name: string,
cfg = {} as StreamUpdateConfig,
cfg = {} as Partial<StreamUpdateConfig>,
): Promise<StreamInfo> {
if (typeof name === "object") {
const sc = name as StreamConfig;
Expand All @@ -75,9 +75,11 @@ export class StreamAPIImpl extends BaseApiClient implements StreamAPI {
);
}
validateStreamName(name);
const ncfg = cfg as Partial<StreamUpdateConfig> & { name: string };
ncfg.name = name;
const r = await this._request(
`${this.prefix}.STREAM.UPDATE.${name}`,
cfg,
ncfg,
);
const si = r as StreamInfo;
this._fixInfo(si);
Expand All @@ -86,7 +88,7 @@ export class StreamAPIImpl extends BaseApiClient implements StreamAPI {

async info(
name: string,
data?: StreamInfoRequestOptions,
data?: Partial<StreamInfoRequestOptions>,
): Promise<StreamInfo> {
validateStreamName(name);
const r = await this._request(`${this.prefix}.STREAM.INFO.${name}`, data);
Expand Down
17 changes: 10 additions & 7 deletions nats-base-client/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -437,9 +437,12 @@ export type StreamInfoRequestOptions = {
};

export interface StreamAPI {
info(stream: string, opts?: StreamInfoRequestOptions): Promise<StreamInfo>;
info(
stream: string,
opts?: Partial<StreamInfoRequestOptions>,
): Promise<StreamInfo>;
add(cfg: Partial<StreamConfig>): Promise<StreamInfo>;
update(name: string, cfg: StreamUpdateConfig): Promise<StreamInfo>;
update(name: string, cfg: Partial<StreamUpdateConfig>): Promise<StreamInfo>;
purge(stream: string, opts?: PurgeOpts): Promise<PurgeResponse>;
delete(stream: string): Promise<boolean>;
list(): Lister<StreamInfo>;
Expand Down Expand Up @@ -544,7 +547,6 @@ export interface StreamInfo {

export interface StreamConfig extends StreamUpdateConfig {
name: string;
subjects?: string[];
retention: RetentionPolicy;
storage: StorageType;
"num_replicas": number;
Expand All @@ -558,15 +560,16 @@ export interface StreamConfig extends StreamUpdateConfig {
}

export interface StreamUpdateConfig {
subjects: string[];
description?: string;
"max_msgs_per_subject"?: number;
"max_msgs_per_subject": number;
"max_msgs": number;
"max_age": Nanos;
"max_bytes": number;
"max_msg_size"?: number;
discard?: DiscardPolicy;
"max_msg_size": number;
discard: DiscardPolicy;
"no_ack"?: boolean;
"duplicate_window"?: Nanos;
"duplicate_window": Nanos;
sources?: StreamSource[];
"allow_rollup_hdrs": boolean;
}
Expand Down
2 changes: 1 addition & 1 deletion src/deno_transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {
} from "../nats-base-client/internal_mod.ts";
import type { TlsOptions } from "../nats-base-client/types.ts";

const VERSION = "1.5.0";
const VERSION = "1.6.1";
const LANG = "nats.deno";

// if trying to simply write to the connection for some reason
Expand Down
15 changes: 15 additions & 0 deletions tests/jsm_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,21 @@ Deno.test("jsm - empty stream config update fails", async () => {
await cleanup(ns, nc);
});

Deno.test("jsm - update stream name is internally added", async () => {
const { ns, nc } = await setup(jetstreamServerConf({}, true));
const jsm = await nc.jetstreamManager();
const name = nuid.next();
let ci = await jsm.streams.add({
name: name,
subjects: [`${name}.>`],
});
assertEquals(ci!.config!.subjects!.length, 1);

const si = await jsm.streams.update(name, { subjects: [`${name}.>`, "foo"] });
assertEquals(si!.config!.subjects!.length, 2);
await cleanup(ns, nc);
});

Deno.test("jsm - delete empty stream name fails", async () => {
const { ns, nc } = await setup(jetstreamServerConf({}, true));
const jsm = await nc.jetstreamManager();
Expand Down

0 comments on commit 5da2f2a

Please sign in to comment.