From 1f06346d8798a66462dd877beec212987db5ddc0 Mon Sep 17 00:00:00 2001 From: "A.P.A. Slaa" Date: Sat, 21 Nov 2026 23:30:44 +0100 Subject: [PATCH 1/2] fix(rados): return an empty Buffer for a read of 0 bytes The OSD reads to the end of the object for a length of 0, which does not fit the empty buffer, so librados failed the read with ERANGE. A read of 0 bytes now only checks that the object exists with rados_stat: it resolves with an empty Buffer, and still fails with ENOENT for a missing object. Fixes #8 Co-Authored-By: Claude Opus 5.5 --- src/io_context.cpp | 12 +++++++++++- tests/integration/rados-rbd.test.ts | 11 +++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/io_context.cpp b/src/io_context.cpp index 5fb7072..edd1a02 100644 --- a/src/io_context.cpp +++ b/src/io_context.cpp @@ -258,7 +258,17 @@ Napi::Value IoContextWrap::Append(const Napi::CallbackInfo& info) { } Napi::Value IoContextWrap::Read(const Napi::CallbackInfo& info) { auto oid = string_arg(info, 0, "oid"); size_t length = static_cast(uint64_arg(info[1], "length")); uint64_t offset = uint64_arg(info[2], "offset"); auto state = in_flight(state_); - return async_value(info.Env(), "rados_read", oid, [state, oid, length, offset](ReadBytes& out) { char* buffer = out.allocate(length); int rc = state->use([&](rados_ioctx_t h) { return rados_read(h, oid.c_str(), buffer, length, offset); }); if (rc >= 0) out.size = static_cast(rc); return rc < 0 ? rc : 0; }, [](Napi::Env env, ReadBytes& out) { return out.to_buffer(env); }); + return async_value(info.Env(), "rados_read", oid, [state, oid, length, offset](ReadBytes& out) { + char* buffer = out.allocate(length); + int rc = state->use([&](rados_ioctx_t h) { + // the OSD reads to the end of the object for a length of 0, which does not fit the empty buffer, so a read + // of 0 bytes only checks that the object exists + if (!length) { uint64_t size = 0; time_t modified = 0; return rados_stat(h, oid.c_str(), &size, &modified); } + return rados_read(h, oid.c_str(), buffer, length, offset); + }); + if (rc >= 0) out.size = length ? static_cast(rc) : 0; + return rc < 0 ? rc : 0; + }, [](Napi::Env env, ReadBytes& out) { return out.to_buffer(env); }); } Napi::Value IoContextWrap::Remove(const Napi::CallbackInfo& info) { auto oid = string_arg(info, 0, "oid"); auto state = in_flight(state_); return async_void(info.Env(), "rados_remove", oid, [state, oid] { return state->use([&](rados_ioctx_t h) { return rados_remove(h, oid.c_str()); }); }); diff --git a/tests/integration/rados-rbd.test.ts b/tests/integration/rados-rbd.test.ts index 1ac2e82..c8822f5 100644 --- a/tests/integration/rados-rbd.test.ts +++ b/tests/integration/rados-rbd.test.ts @@ -140,6 +140,17 @@ test("compound write checks compare the stored value and the version", {skip: !e await io.remove("checked"); }); +test("a read of 0 bytes returns an empty Buffer, or ENOENT for a missing object", {skip: !enabled}, async () => { + await using io = cluster.ioContext(pool); + await io.writeFull("zero-length", Buffer.from("data")); + const empty = await io.read("zero-length", 0n); + assert.ok(Buffer.isBuffer(empty)); + assert.equal(empty.length, 0); + assert.equal((await io.read("zero-length", 0n, 2n)).length, 0); + await assert.rejects(io.read("zero-length-missing", 0n), (error: unknown) => error instanceof CephError && error.errno === 2); + await io.remove("zero-length"); +}); + test("RBD image, I/O, metadata, snapshots, resize, and trash", {skip: !enabled}, async () => { await using io = cluster.ioContext(pool); const rbd = io.rbd(); From e99913ee21fc5e2a647dc80b9d225b5d65e2a529 Mon Sep 17 00:00:00 2001 From: "A.P.A. Slaa" Date: Sat, 21 Nov 2026 23:30:44 +0100 Subject: [PATCH 2/2] fix: make CephError.normalize() return a real CephError CephError[Symbol.hasInstance] matches any object with a numeric code and a string operation, so normalize() returned such objects unchanged and its errno fallback never ran. It now checks the prototype chain, so an error-like object becomes a CephError with errno filled in from code and the original as its cause, while a real CephError is still returned as is. Fixes #9 Co-Authored-By: Claude Opus 5.5 --- src-ts/errors.ts | 3 ++- tests/unit/api.test.ts | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src-ts/errors.ts b/src-ts/errors.ts index 7d72467..56b88dd 100644 --- a/src-ts/errors.ts +++ b/src-ts/errors.ts @@ -23,7 +23,8 @@ export class CephError extends Error { } static normalize(error: unknown): CephError { - if (error instanceof CephError) return error; + // instanceof also matches error-like objects (see hasInstance), which still have to be turned into a CephError + if (error instanceof Error && CephError.prototype.isPrototypeOf(error)) return error as CephError; if (error && typeof error === "object" && "code" in error && "operation" in error) { const value = error as Error & {code: number; errno?: number; codeName?: string; operation: string; resource?: string}; return new CephError(value.message, {code: value.code, errno: value.errno ?? Math.abs(value.code), codeName: value.codeName, operation: value.operation, resource: value.resource, cause: error}); diff --git a/tests/unit/api.test.ts b/tests/unit/api.test.ts index 835377d..e77a521 100644 --- a/tests/unit/api.test.ts +++ b/tests/unit/api.test.ts @@ -27,6 +27,21 @@ test("recognizes structured errors emitted by the native addon", () => { assert.equal(CephError.normalize(error).resource, "object"); }); +test("normalize() turns error-like objects into a CephError and keeps real ones", () => { + const plain = Object.assign(new Error("busy"), {code: -16, operation: "rados_ioctx_set_namespace"}); + const normalized = CephError.normalize(plain); + assert.equal(Object.getPrototypeOf(normalized), CephError.prototype); + assert.equal(normalized.errno, 16); + assert.equal(normalized.code, -16); + assert.equal(normalized.message, "busy"); + assert.equal(normalized.operation, "rados_ioctx_set_namespace"); + assert.equal(normalized.cause, plain); + + const real = new CephError("busy", {code: -16, errno: 16, operation: "op"}); + assert.equal(CephError.normalize(real), real); + assert.throws(() => CephError.normalize(new Error("other")), /other/); +}); + test("native errors name their errno", async () => { await assert.rejects(Cluster.connect({configFile: "/nonexistent/node-librados/ceph.conf", parseEnvironment: false}), (error: unknown) => { assert.ok(error instanceof CephError);