Skip to content
Open
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
39 changes: 22 additions & 17 deletions src/drivers/cloudflare-cache-binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type {
CacheStorage as CFCacheStorage,
Response as CFResponse,
} from "@cloudflare/workers-types";
import type { TransactionOptions } from "../types.ts";

import { type DriverFactory, joinKeys } from "./utils/index.ts";

Expand Down Expand Up @@ -50,6 +51,25 @@ const driver: DriverFactory<CacheOptions, CFCache | Promise<CFCache>> = (opts) =
return _cache;
};

const setItemRaw = async (key: string, value: any, tOptions: TransactionOptions) => {
const cacheKey = r(key);

// https://developers.cloudflare.com/workers/runtime-apis/cache/#headers
const headers = {} as Record<string, string>;
const ttl = tOptions?.ttl ?? opts.ttl;
if (ttl) {
headers["Cache-Control"] = `max-age=${ttl}`;
}
if (tOptions.tag) {
headers["Cache-Tag"] = tOptions.tag;
}

const cacheValue = new Response(value, { headers });

const cache = await getCache();
await cache.put(cacheKey, cacheValue as unknown as CFResponse);
};

return {
name: DRIVER_NAME,
options: opts,
Expand Down Expand Up @@ -77,26 +97,11 @@ const driver: DriverFactory<CacheOptions, CFCache | Promise<CFCache>> = (opts) =
},

async setItem(key, value, tOptions) {
return this.setItemRaw!(key, value, tOptions);
return setItemRaw(key, value, tOptions);
},

async setItemRaw(key, value, tOptions) {
const cacheKey = r(key);

// https://developers.cloudflare.com/workers/runtime-apis/cache/#headers
const headers = {} as Record<string, string>;
const ttl = tOptions?.ttl ?? opts.ttl;
if (ttl) {
headers["Cache-Control"] = `max-age=${ttl}`;
}
if (tOptions.tag) {
headers["Cache-Tag"] = tOptions.tag;
}

const cacheValue = new Response(value, { headers });

const cache = await getCache();
await cache.put(cacheKey, cacheValue as unknown as CFResponse);
return setItemRaw(key, value, tOptions);
},

async removeItem(key) {
Expand Down
59 changes: 59 additions & 0 deletions test/drivers/cloudflare-cache-binding-fake.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { describe, expect, test, beforeEach, afterEach } from "vitest";
import { createStorage } from "../../src/index.ts";
import CloudflareCacheBinding from "../../src/drivers/cloudflare-cache-binding.ts";

// Minimal Map-backed fake of the Cloudflare Workers Cache API, just enough
// surface for the cloudflare-cache-binding driver (match/put/delete) without
// requiring a real Cloudflare runtime or wrangler's platform proxy.
function createFakeCaches() {
const store = new Map<string, Response>();

const fakeCache = {
async match(key: string) {
const hit = store.get(key);
return hit ? hit.clone() : undefined;
},
async put(key: string, value: Response) {
store.set(key, value.clone());
},
async delete(key: string) {
return store.delete(key);
},
};

return {
default: fakeCache,
open: async (_name: string) => fakeCache,
};
}

describe("drivers: cloudflare-cache-binding (fake caches)", () => {
let originalCaches: unknown;

beforeEach(() => {
originalCaches = (globalThis as any).caches;
(globalThis as any).caches = createFakeCaches();
});

afterEach(() => {
(globalThis as any).caches = originalCaches;
});

test("setItem round-trips through storage.setItem/getItem (regression #789)", async () => {
const storage = createStorage({
driver: CloudflareCacheBinding({ base: "nitro-cache" }),
});

// This is the exact call path from issue #789: storage.setItem invokes
// the driver's setItem as a bare (unbound) function. Before the fix,
// setItem() referenced `this.setItemRaw!`, which throws
// "Cannot read properties of undefined (reading 'setItemRaw')"
// because `this` is undefined in that call context.
await storage.setItem("test", { ok: true }, { ttl: 60 });

const value = await storage.getItem("test");
expect(value).toMatchObject({ ok: true });

await storage.dispose();
});
});