diff --git a/src/drivers/lru-cache.ts b/src/drivers/lru-cache.ts index 12d3492e..0a0cca7b 100644 --- a/src/drivers/lru-cache.ts +++ b/src/drivers/lru-cache.ts @@ -29,10 +29,10 @@ export default defineDriver((opts: LRUDriverOptions = {}) => { return cache.has(key); }, getItem(key) { - return cache.get(key) || null; + return cache.get(key) ?? null; }, getItemRaw(key) { - return cache.get(key) || null; + return cache.get(key) ?? null; }, setItem(key, value) { cache.set(key, value); diff --git a/src/drivers/memory.ts b/src/drivers/memory.ts index 746a37ba..3db584ee 100644 --- a/src/drivers/memory.ts +++ b/src/drivers/memory.ts @@ -12,10 +12,10 @@ export default defineDriver(() => { return data.has(key); }, getItem(key) { - return data.get(key) || null; + return data.get(key) ?? null; }, getItemRaw(key) { - return data.get(key) || null; + return data.get(key) ?? null; }, setItem(key, value) { data.set(key, value); diff --git a/src/drivers/mongodb.ts b/src/drivers/mongodb.ts index 81e62c94..8e43d780 100644 --- a/src/drivers/mongodb.ts +++ b/src/drivers/mongodb.ts @@ -45,7 +45,7 @@ export default defineDriver((opts: MongoDbOptions) => { }, async getItem(key) { const document = await getMongoCollection().findOne({ key }); - return document?.value ? document.value : null; + return document?.value ?? null; }, async setItem(key, value) { const currentDateTime = new Date(); diff --git a/src/drivers/redis.ts b/src/drivers/redis.ts index 0e6d380e..27141c28 100644 --- a/src/drivers/redis.ts +++ b/src/drivers/redis.ts @@ -63,7 +63,7 @@ export default defineDriver((opts: RedisOptions = {}) => { }, async getItem(key) { const value = await getRedisClient().get(p(key)); - return value === null ? null : value; + return value ?? null; }, async setItem(key, value, tOptions) { let ttl = tOptions?.ttl ?? opts.ttl; diff --git a/test/drivers/cloudflare-kv-binding.test.ts b/test/drivers/cloudflare-kv-binding.test.ts index a3cd30da..4ae09b8f 100644 --- a/test/drivers/cloudflare-kv-binding.test.ts +++ b/test/drivers/cloudflare-kv-binding.test.ts @@ -43,6 +43,7 @@ describe("drivers: cloudflare-kv", () => { "json": "works", }, "base:data:true.json": true, + "base:my-false-flag": false, "base:s1:a": "test_data", "base:s2:a": "test_data", "base:s3:a": "test_data", @@ -52,6 +53,7 @@ describe("drivers: cloudflare-kv", () => { "base:v1:a": "test_data_v1:a", "base:v2:a": "test_data_v2:a", "base:v3:a": "test_data_v3:a?q=1", + "base:zero": 0, } `); }); diff --git a/test/drivers/cloudflare-kv-http.test.ts b/test/drivers/cloudflare-kv-http.test.ts index 0f5fe9d9..6ad2284a 100644 --- a/test/drivers/cloudflare-kv-http.test.ts +++ b/test/drivers/cloudflare-kv-http.test.ts @@ -102,6 +102,7 @@ describe.skipIf(isNode18)("drivers: cloudflare-kv-http", () => { "base:data:serialized2.json": "{\\"serializedObj\\":\\"works\\"}", "base:data:test.json": "{\\"json\\":\\"works\\"}", "base:data:true.json": "true", + "base:my-false-flag": "false", "base:s1:a": "test_data", "base:s2:a": "test_data", "base:s3:a": "test_data", @@ -111,6 +112,7 @@ describe.skipIf(isNode18)("drivers: cloudflare-kv-http", () => { "base:v1:a": "test_data_v1:a", "base:v2:a": "test_data_v2:a", "base:v3:a": "test_data_v3:a?q=1", + "base:zero": "0", } `); }); diff --git a/test/drivers/cloudflare-r2-binding.test.ts b/test/drivers/cloudflare-r2-binding.test.ts index 64033b52..81d6a09c 100644 --- a/test/drivers/cloudflare-r2-binding.test.ts +++ b/test/drivers/cloudflare-r2-binding.test.ts @@ -59,6 +59,7 @@ describe("drivers: cloudflare-r2-binding", () => { "json": "works", }, "base:data:true.json": true, + "base:my-false-flag": false, "base:s1:a": "test_data", "base:s2:a": "test_data", "base:s3:a": "test_data", @@ -68,6 +69,7 @@ describe("drivers: cloudflare-r2-binding", () => { "base:v1:a": "test_data_v1:a", "base:v2:a": "test_data_v2:a", "base:v3:a": "test_data_v3:a?q=1", + "base:zero": 0, } `); }); diff --git a/test/drivers/redis.test.ts b/test/drivers/redis.test.ts index 93140a32..b8e08f0f 100644 --- a/test/drivers/redis.test.ts +++ b/test/drivers/redis.test.ts @@ -32,6 +32,8 @@ describe("drivers: redis", () => { "test:v1:a", "test:v2:a", "test:v3:a", + "test:zero", + "test:my-false-flag", ] `); await client.disconnect(); diff --git a/test/drivers/utils.ts b/test/drivers/utils.ts index 87618a88..bb8ea5c6 100644 --- a/test/drivers/utils.ts +++ b/test/drivers/utils.ts @@ -135,6 +135,14 @@ export function testDriver(opts: TestOptions) { ]); }); + it("getItem - return falsy values when set in storage", async () => { + await ctx.storage.setItem("zero", 0); + expect(await ctx.storage.getItem("zero")).toBe(0); + + await ctx.storage.setItem("my-false-flag", false); + expect(await ctx.storage.getItem("my-false-flag")).toBe(false); + }); + // TODO: Refactor to move after cleanup if (opts.additionalTests) { opts.additionalTests(ctx);