Skip to content

Commit

Permalink
[FEAT] Direct API get for last message by subject will now set the su…
Browse files Browse the repository at this point in the history
…bject as part of the request subject, rather than include it on the payload of the request. This allows for downstream clients, such as KV, to clamp on permissions for a GET.j
  • Loading branch information
aricart committed Aug 4, 2022
1 parent 350dfe9 commit 2acd03e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
18 changes: 16 additions & 2 deletions nats-base-client/jsmdirect_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ import {
DirectMsgHeaders,
DirectMsgRequest,
DirectStreamAPI,
Empty,
JetStreamOptions,
LastForMsgRequest,
Msg,
NatsConnection,
StoredMsg,
Expand All @@ -37,9 +39,21 @@ export class DirectStreamAPIImpl extends BaseApiClient
query: DirectMsgRequest,
): Promise<StoredMsg> {
validateStreamName(stream);
// if doing a last_by_subj request, we append the subject
// this allows last_by_subj to be subject to permissions (KV)
let qq: DirectMsgRequest | null = query;
const { last_by_subj } = qq as LastForMsgRequest;
if (last_by_subj) {
qq = null;
}

const payload = qq ? this.jc.encode(qq) : Empty;
const subj = last_by_subj
? `$JS.API.DIRECT.GET.${stream}.${last_by_subj}`
: `$JS.API.DIRECT.GET.${stream}`;
const r = await this.nc.request(
`$JS.API.DIRECT.GET.${stream}`,
this.jc.encode(query),
subj,
payload,
);

// response is not a JS.API response
Expand Down
4 changes: 0 additions & 4 deletions tests/jsm_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import {
JSONCodec,
jwtAuthenticator,
nanos,
NatsConnectionImpl,
NatsError,
nkeys,
nuid,
Expand Down Expand Up @@ -1210,9 +1209,6 @@ Deno.test("jsm - direct getMessage", async () => {
await js.publish("bar", sc.encode("d"), { expect: { lastSequence: 3 } });
await js.publish("foo", sc.encode("e"), { expect: { lastSequence: 4 } });

const nci = nc as NatsConnectionImpl;
nci.options.debug = true;

let m = await jsm.direct.getMessage("A", { seq: 0, next_by_subj: "bar" });
assertEquals(m.seq, 4);

Expand Down

0 comments on commit 2acd03e

Please sign in to comment.