Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(kv): fixed an issue where kv sources were not properly processed #721

Merged
merged 1 commit into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion jetstream/jsmstream_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ export class StreamAPIImpl extends BaseApiClient implements StreamAPI {
context: string,
src: Partial<StreamSource>,
): void {
const count = src.subject_transforms?.length || 0;
const count = src?.subject_transforms?.length || 0;
if (count > 0) {
const { min, ok } = nci.features.get(
Feature.JS_STREAM_SOURCE_SUBJECT_TRANSFORM,
Expand Down
11 changes: 11 additions & 0 deletions jetstream/kv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,14 +267,25 @@ export class Bucket implements KV, KvRemove {
} else if (opts.sources) {
const sources = opts.sources.map((s) => {
const c = Object.assign({}, s) as StreamSource;
const srcBucketName = c.name.startsWith(kvPrefix)
? c.name.substring(kvPrefix.length)
: c.name;
if (!c.name.startsWith(kvPrefix)) {
c.name = `${kvPrefix}${c.name}`;
}
if (!s.external && srcBucketName !== this.bucket) {
c.subject_transforms = [
{ src: `$KV.${srcBucketName}.>`, dest: `$KV.${this.bucket}.>` },
];
}
return c;
});
sc.sources = sources as unknown[] as StreamSource[];
sc.subjects = [this.subjectForBucket()];
} else {
sc.subjects = [this.subjectForBucket()];
}

if (opts.metadata) {
sc.metadata = opts.metadata;
}
Expand Down
27 changes: 27 additions & 0 deletions jetstream/tests/kv_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2210,3 +2210,30 @@ Deno.test("kv - keys filter", async () => {

await cleanup(ns, nc);
});

Deno.test("kv - sourced", async () => {
const { ns, nc } = await setup(
jetstreamServerConf({}),
);
if (await notCompatible(ns, nc, "2.6.3")) {
return;
}

const js = nc.jetstream();
const source = await js.views.kv("source");
const target = await js.views.kv("target", {
sources: [{ name: "source" }],
});

await source.put("hello", "world");
for (let i = 0; i < 10; i++) {
const v = await target.get("hello");
if (v === null) {
await delay(250);
continue;
}
assertEquals(v.string(), "world");
}

await cleanup(ns, nc);
});
Loading