From 5d50fabf40fb64e0aeee2ff0fc4da953de40171d Mon Sep 17 00:00:00 2001 From: Alberto Ricart Date: Mon, 29 Jul 2024 18:03:50 -0500 Subject: [PATCH] fix(jetstream): fixed possible dereference of undefined value when validating stream sources fix(kv): fixed handling of kv sources by adding a necessary transform -- This fix was ported from https://github.com/nats-io/nats.deno/pull/721 --- deno.json | 3 ++- jetstream/deno.json | 2 +- jetstream/package.json | 2 +- jetstream/src/jsmstream_api.ts | 2 +- kv/deno.json | 4 ++-- kv/import_map.json | 4 ++-- kv/package.json | 4 ++-- kv/src/kv.ts | 10 ++++++++++ kv/tests/kv_test.ts | 29 +++++++++++++++++++++++++++++ obj/deno.json | 2 +- obj/import_map.json | 4 ++-- obj/package.json | 2 +- 12 files changed, 54 insertions(+), 14 deletions(-) diff --git a/deno.json b/deno.json index 88f551a7..dcd546c3 100644 --- a/deno.json +++ b/deno.json @@ -32,7 +32,8 @@ "check-versions-services": "bin/check-bundle-version.ts --module services", "check-versions-transport-node": "bin/check-bundle-version.ts --module transport-node", "check-versions-transport-deno": "bin/check-bundle-version.ts --module transport-deno", - "check-dependencies": "bin/check-dep-versions.ts" + "sync-dependencies": "deno task chk-deps && deno fmt", + "chk-deps": "bin/check-dep-versions.ts" }, "fmt": { "include": ["transport-deno/", "bin/", "core/", "debug/", "jetstream/", "kv/", "obj/", "services/", "*.md", "transport-node/"], diff --git a/jetstream/deno.json b/jetstream/deno.json index a5ab14ba..e94411c5 100644 --- a/jetstream/deno.json +++ b/jetstream/deno.json @@ -1,6 +1,6 @@ { "name": "@nats-io/jetstream", - "version": "3.0.0-3", + "version": "3.0.0-4", "exports": { ".": "./src/mod.ts", "./internal": "./src/internal_mod.ts" diff --git a/jetstream/package.json b/jetstream/package.json index 8c14d5e9..872624fe 100644 --- a/jetstream/package.json +++ b/jetstream/package.json @@ -1,6 +1,6 @@ { "name": "@nats-io/jetstream", - "version": "3.0.0-3", + "version": "3.0.0-4", "files": [ "lib/", "build/src/" diff --git a/jetstream/src/jsmstream_api.ts b/jetstream/src/jsmstream_api.ts index 588bc8b3..d4d5caef 100644 --- a/jetstream/src/jsmstream_api.ts +++ b/jetstream/src/jsmstream_api.ts @@ -255,7 +255,7 @@ export class StreamAPIImpl extends BaseApiClientImpl implements StreamAPI { context: string, src: Partial, ): 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, diff --git a/kv/deno.json b/kv/deno.json index 42cca84b..18de25d2 100644 --- a/kv/deno.json +++ b/kv/deno.json @@ -1,6 +1,6 @@ { "name": "@nats-io/kv", - "version": "3.0.0-2", + "version": "3.0.0-3", "exports": { ".": "./src/mod.ts", "./internal": "./src/internal_mod.ts" @@ -29,6 +29,6 @@ }, "imports": { "@nats-io/nats-core": "jsr:@nats-io/nats-core@~3.0.0-18", - "@nats-io/jetstream": "jsr:@nats-io/jetstream@3.0.0-3" + "@nats-io/jetstream": "jsr:@nats-io/jetstream@~3.0.0-4" } } diff --git a/kv/import_map.json b/kv/import_map.json index 685ea97c..493da323 100644 --- a/kv/import_map.json +++ b/kv/import_map.json @@ -2,8 +2,8 @@ "imports": { "@nats-io/nats-core": "jsr:@nats-io/nats-core@~3.0.0-18", "@nats-io/nats-core/internal": "jsr:@nats-io/nats-core@~3.0.0-18/internal", - "@nats-io/jetstream": "jsr:@nats-io/jetstream@3.0.0-3", - "@nats-io/jetstream/internal": "jsr:@nats-io/jetstream@~3.0.0-3/internal", + "@nats-io/jetstream": "jsr:@nats-io/jetstream@~3.0.0-4", + "@nats-io/jetstream/internal": "jsr:@nats-io/jetstream@~3.0.0-4/internal", "test_helpers": "../test_helpers/mod.ts", "@nats-io/nkeys": "jsr:@nats-io/nkeys@1.2.0-4", "@nats-io/nuid": "jsr:@nats-io/nuid@2.0.1-2", diff --git a/kv/package.json b/kv/package.json index ab3ffffd..b16fc6d4 100644 --- a/kv/package.json +++ b/kv/package.json @@ -1,6 +1,6 @@ { "name": "@nats-io/kv", - "version": "3.0.0-2", + "version": "3.0.0-3", "files": [ "lib/", "build/src/" @@ -27,7 +27,7 @@ "description": "kv library - this library implements all the base functionality for NATS KV javascript clients", "dependencies": { "@nats-io/nats-core": "~3.0.0-18", - "@nats-io/jetstream": "3.0.0-3" + "@nats-io/jetstream": "~3.0.0-4" }, "devDependencies": { "@types/node": "^20.11.30", diff --git a/kv/src/kv.ts b/kv/src/kv.ts index 1596b407..592cbd18 100644 --- a/kv/src/kv.ts +++ b/kv/src/kv.ts @@ -364,11 +364,21 @@ 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()]; } diff --git a/kv/tests/kv_test.ts b/kv/tests/kv_test.ts index 220d149a..fbc4216d 100644 --- a/kv/tests/kv_test.ts +++ b/kv/tests/kv_test.ts @@ -2269,3 +2269,32 @@ Deno.test("kv - replicas", async () => { await nc.close(); await NatsServer.stopAll(servers, true); }); + +Deno.test("kv - sourced", async () => { + const { ns, nc } = await _setup( + connect, + jetstreamServerConf({}), + ); + if (await notCompatible(ns, nc, "2.6.3")) { + return; + } + + const js = jetstream(nc); + const kvm = await new Kvm(js); + const source = await kvm.create("source"); + const target = await kvm.create("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); +}); diff --git a/obj/deno.json b/obj/deno.json index ddca4c02..fd544dd5 100644 --- a/obj/deno.json +++ b/obj/deno.json @@ -28,6 +28,6 @@ }, "imports": { "@nats-io/nats-core": "jsr:@nats-io/nats-core@~3.0.0-18", - "@nats-io/jetstream": "jsr:@nats-io/jetstream@3.0.0-3" + "@nats-io/jetstream": "jsr:@nats-io/jetstream@~3.0.0-4" } } diff --git a/obj/import_map.json b/obj/import_map.json index 685ea97c..493da323 100644 --- a/obj/import_map.json +++ b/obj/import_map.json @@ -2,8 +2,8 @@ "imports": { "@nats-io/nats-core": "jsr:@nats-io/nats-core@~3.0.0-18", "@nats-io/nats-core/internal": "jsr:@nats-io/nats-core@~3.0.0-18/internal", - "@nats-io/jetstream": "jsr:@nats-io/jetstream@3.0.0-3", - "@nats-io/jetstream/internal": "jsr:@nats-io/jetstream@~3.0.0-3/internal", + "@nats-io/jetstream": "jsr:@nats-io/jetstream@~3.0.0-4", + "@nats-io/jetstream/internal": "jsr:@nats-io/jetstream@~3.0.0-4/internal", "test_helpers": "../test_helpers/mod.ts", "@nats-io/nkeys": "jsr:@nats-io/nkeys@1.2.0-4", "@nats-io/nuid": "jsr:@nats-io/nuid@2.0.1-2", diff --git a/obj/package.json b/obj/package.json index e9b760ef..c4db6743 100644 --- a/obj/package.json +++ b/obj/package.json @@ -27,7 +27,7 @@ "description": "obj library - this library implements all the base functionality for NATS objectstore for javascript clients", "dependencies": { "@nats-io/nats-core": "~3.0.0-18", - "@nats-io/jetstream": "3.0.0-3" + "@nats-io/jetstream": "~3.0.0-4" }, "devDependencies": { "@types/node": "^20.11.30",