fix(cloudflare-cache-binding): use closure instead of detached this in setItem - #794
fix(cloudflare-cache-binding): use closure instead of detached this in setItem#794momomuchu wants to merge 1 commit into
Conversation
setItem called this.setItemRaw, which throws when the method is invoked without the driver bound as this (the normal call path from storage.setItem). Extract the write logic into a local setItemRaw closure, matching the getCache/r() pattern already used by every sibling method, and have both setItem and setItemRaw call it directly. Adds a regression test that stubs globalThis.caches with a Map-backed fake so the round trip is covered without a real Cloudflare runtime. Fixes unjs#789
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughRefactors the cloudflare-cache-binding driver to extract cache-write logic into a shared internal setItemRaw helper, fixing a broken ChangesCloudflare cache binding fix
Estimated code review effort: 2 (Simple) | ~10 minutes Sequence Diagram(s)sequenceDiagram
participant Storage
participant Driver as CloudflareCacheBindingDriver
participant Helper as setItemRaw helper
participant Cache as Cloudflare Cache API
Storage->>Driver: setItem(key, value, options)
Driver->>Helper: setItemRaw(key, value, options)
Helper->>Helper: compute cache key, TTL, headers
Helper->>Cache: cache.put(request, response)
Cache-->>Storage: write complete
Related issues: Suggested reviewers: pi0 Poem:
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
The
cloudflare-cache-bindingdriver'ssetItem()throwsTypeError: Cannot read properties of undefined (reading 'setItemRaw')when storagesetItemis called through the normal bare-function path, because it referencesthis.setItemRaw!andthisis detached there.Every other method in this driver uses the in-file closure convention (
getCache()) instead ofthis. This extracts the write logic into asetItemRawclosure and has bothsetItemandsetItemRawcall it directly, removing thethisdependency. Pure extract-method, no behavior change beyond fixing the crash.Added a test that stubs
globalThis.cacheswith a Map-backed fake (no real Cloudflare needed) and round-tripssetItem/getItem. It throws the reported TypeError without the fix and passes with it. Build, typecheck and lint clean. The existing real-Workers driver test isdescribe.skip'd (no caches API in the platform proxy), which is why this shipped uncaught; the fake-caches test covers this bug class.Closes #789
Summary by CodeRabbit
Bug Fixes
Tests