Skip to content

Commit

Permalink
fix(lru-cache, memory, mongodb, redis): return falsy values when set …
Browse files Browse the repository at this point in the history
…in storage (#320)
  • Loading branch information
patzick authored Nov 14, 2023
1 parent 44ffe1d commit eb6a2b4
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/drivers/lru-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/drivers/memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ export default defineDriver<void>(() => {
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);
Expand Down
2 changes: 1 addition & 1 deletion src/drivers/mongodb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/drivers/redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions test/drivers/cloudflare-kv-binding.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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,
}
`);
});
Expand Down
2 changes: 2 additions & 0 deletions test/drivers/cloudflare-kv-http.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
}
`);
});
Expand Down
2 changes: 2 additions & 0 deletions test/drivers/cloudflare-r2-binding.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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,
}
`);
});
Expand Down
2 changes: 2 additions & 0 deletions test/drivers/redis.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
8 changes: 8 additions & 0 deletions test/drivers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit eb6a2b4

Please sign in to comment.