Skip to content

Commit db75469

Browse files
committed
conats: some clarity on undefined v null
1 parent 40a80a2 commit db75469

File tree

6 files changed

+14
-25
lines changed

6 files changed

+14
-25
lines changed

src/packages/nats/core/client.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -512,9 +512,6 @@ export class Client {
512512
// **right now** or received these messages.
513513
count: number;
514514
}> => {
515-
if (mesg === undefined) {
516-
throw Error("mesg must not be undefined");
517-
}
518515
const { bytes, getCount, promise } = this._publish(subject, mesg, {
519516
...opts,
520517
confirm: true,
@@ -536,9 +533,6 @@ export class Client {
536533
if (!isValidSubjectWithoutWildcards(subject)) {
537534
throw Error(`invalid publish subject ${subject}`);
538535
}
539-
if (mesg === undefined) {
540-
throw Error("mesg must not be undefined");
541-
}
542536
raw = raw ?? encode({ encoding, mesg });
543537
// default to 1MB is safe since it's at least that big.
544538
const chunkSize = Math.max(
@@ -902,19 +896,21 @@ export class Message extends MessageData {
902896
return `${subject}`;
903897
};
904898

905-
respondSync = (data, opts?: PublishOptions): { bytes: number } => {
899+
respondSync = (mesg, opts?: PublishOptions): { bytes: number } => {
906900
const subject = this.respondSubject();
907901
if (!subject) return { bytes: 0 };
908-
return this.client.publishSync(subject, data, opts);
902+
return this.client.publishSync(subject, mesg, opts);
909903
};
910904

911905
respond = async (
912-
data,
906+
mesg,
913907
opts: PublishOptions = {},
914908
): Promise<{ bytes: number; count: number }> => {
915909
const subject = this.respondSubject();
916-
if (!subject) return { bytes: 0, count: 0 };
917-
return await this.client.publish(subject, data, opts);
910+
if (!subject) {
911+
return { bytes: 0, count: 0 };
912+
}
913+
return await this.client.publish(subject, mesg, opts);
918914
};
919915
}
920916

src/packages/nats/service/service.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ export async function callNatsService(opts: ServiceCall): Promise<any> {
5353
const subject = serviceSubject(opts);
5454
let resp;
5555
const timeout = opts.timeout ?? DEFAULT_TIMEOUT;
56-
const data = opts.mesg;
56+
// ensure not undefined, since undefined can't be published.
57+
const data = opts.mesg ?? null;
5758

5859
const doRequest = async () => {
5960
resp = await cn.request(subject, data, {

src/packages/nats/sync/core-stream.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ export class CoreStream<T = any> extends EventEmitter {
524524
options?: { headers?: Headers; msgID?: string; key?: string },
525525
) => {
526526
if (mesg === undefined) {
527-
throw Error("mesg must not be undefined");
527+
throw Error("stream publish - mesg must not be 'undefined'");
528528
}
529529
const data = mesg;
530530

src/packages/project/nats/browser-websocket-api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,5 +85,5 @@ async function handleRequest({ data, mesg, primus }) {
8585
resp = { error: `${err}` };
8686
}
8787
//logger.debug("responded", resp);
88-
mesg.respond(resp);
88+
mesg.respond(resp ?? null);
8989
}

src/packages/server/nats/test/core/basic.test.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,6 @@ describe("basic test of publish and subscribe", () => {
4242
expect(done).toBe(false);
4343
});
4444

45-
it("publishing undefined is not allowed", async () => {
46-
await expect(
47-
async () => await cn.publish(subject, undefined),
48-
).rejects.toThrowError("must not be undefined");
49-
50-
expect(() => cn.publishSync(subject, undefined)).toThrow(
51-
"must not be undefined",
52-
);
53-
});
54-
5545
it("publishes using a second client", async () => {
5646
const data = null;
5747
cn2 = connect();

src/packages/server/nats/test/core/core-stream-ephemeral.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ describe("create a client, create an ephemeral leader core-stream, and do basic
4444
it("publishing undefined is not allowed", async () => {
4545
await expect(
4646
async () => await stream.publish(undefined),
47-
).rejects.toThrowError("must not be undefined");
47+
).rejects.toThrowError("must not be 'undefined'");
4848
});
4949

5050
it("a second client has the same messages", async () => {
@@ -299,6 +299,8 @@ describe("test using ephemeral dstream", () => {
299299
wait({ until: () => stream.length == 101 });
300300
expect(stream.get(stream.length - 1)).toBe("x");
301301
});
302+
303+
302304
});
303305

304306
afterAll(after);

0 commit comments

Comments
 (0)