Skip to content

Commit

Permalink
[FIX] [OS] made put() reject link configurations, as these should b…
Browse files Browse the repository at this point in the history
…e routed through the `link()` API. This matches changes made to the go client in https://github.com/nats-io/nats.go/pull/1057/files
  • Loading branch information
aricart committed Sep 19, 2022
1 parent 177c3da commit 5581bab
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
18 changes: 15 additions & 3 deletions nats-base-client/objectstore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ export class ObjectStoreImpl implements ObjectStore {
return this.jsm.streams.delete(this.stream);
}

async put(
async _put(
meta: ObjectStoreMeta,
rs: ReadableStream<Uint8Array> | null,
): Promise<ObjectInfo> {
Expand Down Expand Up @@ -400,6 +400,18 @@ export class ObjectStoreImpl implements ObjectStore {
return d;
}

async put(
meta: ObjectStoreMeta,
rs: ReadableStream<Uint8Array> | null,
): Promise<ObjectInfo> {
if (meta?.options?.link) {
return Promise.reject(
new Error("link cannot be set when putting the object in bucket"),
);
}
return this._put(meta, rs);
}

async get(name: string): Promise<ObjectResult | null> {
const info = await this.rawInfo(name);
if (info === null) {
Expand Down Expand Up @@ -500,7 +512,7 @@ export class ObjectStoreImpl implements ObjectStore {
name: n,
options: { link: { bucket: osi.name } },
};
return this.put(meta, null);
return this._put(meta, null);
}

async link(name: string, info: ObjectInfo): Promise<ObjectInfo> {
Expand Down Expand Up @@ -529,7 +541,7 @@ export class ObjectStoreImpl implements ObjectStore {
name: n,
options: { link: link },
} as ObjectStoreMeta;
return this.put(mm, null);
return this._put(mm, null);
}

async delete(name: string): Promise<PurgeResponse> {
Expand Down
28 changes: 28 additions & 0 deletions tests/objectstore_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -888,3 +888,31 @@ Deno.test("objectstore - meta update", async () => {

await cleanup(ns, nc);
});

Deno.test("objectstore - cannot put links", async () => {
const { ns, nc } = await setup(jetstreamServerConf({
max_payload: 1024 * 1024,
}, true));
if (await notCompatible(ns, nc, "2.6.3")) {
return;
}
const sc = StringCodec();
const js = nc.jetstream();
const os = await js.views.os("test");

const link = { bucket: "test", name: "a" };
const mm = {
name: "ref",
options: { link: link },
} as ObjectStoreMeta;

await assertRejects(
async () => {
await os.put(mm, readableStreamFrom(sc.encode("a")));
},
Error,
"link cannot be set when putting the object in bucket",
);

await cleanup(ns, nc);
});

0 comments on commit 5581bab

Please sign in to comment.