Skip to content

cloudflare-cache-binding setItem fails when called through storage.setItem #789

Description

@medz

Refs #603

Environment

  • unstorage: 2.0.0-alpha.7
  • Runtime: Cloudflare Workers
  • Consumer observed through Nitro 3 cached handlers using the cache storage mount

Reproduction

In a Cloudflare Worker runtime:

import { createStorage } from "unstorage";
import cloudflareCacheBinding from "unstorage/drivers/cloudflare-cache-binding";

export default {
  async fetch() {
    const storage = createStorage();
    storage.mount("cache", cloudflareCacheBinding({ base: "nitro-cache" }));

    await storage.setItem("cache:test", { ok: true }, { ttl: 60 });

    return new Response("ok");
  },
};

Actual behavior

The write fails with:

TypeError: Cannot read properties of undefined (reading 'setItemRaw')

I observed this in a real Nitro Worker deployment when defineCachedHandler tried to write its cache entry:

[cache] TypeError: Cannot read properties of undefined (reading 'setItemRaw')

Expected behavior

storage.setItem(...) should write through the Cloudflare Cache API driver successfully.

Suspected cause

src/drivers/cloudflare-cache-binding.ts currently implements setItem() as:

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

When unstorage calls driver methods, the method appears to be invoked without binding the driver object as this. In ESM strict mode, this is undefined, so this.setItemRaw throws before the Cache API write happens.

Notes

This is not caused by name or base configuration. The driver resolves caches.default correctly when name is omitted, and the failure happens before/while dispatching from setItem() to setItemRaw().

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions