10.2.0 Cloudflare workers cache not implemented correctly
Steps to reproduce
- Create worker using
npm create cloudflare - Import
youtuvei.js/cf-worker - Instantiate a new innertube instance with the universal cache
Innertube.create({ cache: new UniversalCache(true) })in the fetch function before rendering the hello world npm run devand visit the page
Failure Logs
X [ERROR] TypeError: Invalid URL: innertube_session_data
at Cache.<anonymous>
(file:///D:/Code/yt-event-reminder/node_modules/.pnpm/[email protected]/node_modules/youtubei.js/src/platform/cf-worker.ts:26:34)
at Generator.next (<anonymous>)
at fulfilled
(file:///D:/Code/yt-event-reminder/node_modules/.pnpm/[email protected]/node_modules/tslib/tslib.es6.mjs:118:56)
Expected behavior
Cache put and get calls work
Current behavior
It fails trying to retrieve session data from cache
Version
Default
Anything else?
The worker cache is designed to cache http responses for a given requests. The docs specify that if a string is passed, it is interpreted as a url. Therefore the key arg must be a url. It then fails trying to retrieve inntertube_session_data from cache, which isn't a url.
From the docs https://developers.cloudflare.com/workers/runtime-apis/cache/#cache:
cache.put will throw an error if:
The request passed is a method other than GET. The response passed has a status of 206 Partial Content. The response passed contains the header Vary: . The value of the Vary header is an asterisk (). Refer to the Cache API specification for more information.
It's might be possible to simply convert the key into a valid url, but this is not what it was designed to do (and the docs mention more limitations). A KV adapter would be more suited
Checklist
- [X] I am running the latest version.
- [X] I checked the documentation and found no answer.
- [X] I have searched the existing issues and made sure this is not a duplicate.
- [X] I have provided sufficient information.
Solved?
Solved?
No, I'm having the same issue
I found the issue. Basically, the client will try to store the key innertube_session_data, which is not a valid URL. And it seems like cloudflare cache doesn't support arbitrarily string for some reason.
While it might not work for everyone. This works for me:
const memCache: Record<string, ArrayBuffer> = {}
const yt = await Innertube.create({
cache: {
cache_dir: "yt-cache",
get: async (key) => {
return memCache[key];
},
set: async (key, value) => {
memCache[key] = value;
},
remove: async (key) => {
delete memCache[key];
}
}, generate_session_locally: true
});