YouTube.js icon indicating copy to clipboard operation
YouTube.js copied to clipboard

10.2.0 Cloudflare workers cache not implemented correctly

Open dct0 opened this issue 1 year ago • 3 comments

Steps to reproduce

  1. Create worker using npm create cloudflare
  2. Import youtuvei.js/cf-worker
  3. Instantiate a new innertube instance with the universal cache Innertube.create({ cache: new UniversalCache(true) }) in the fetch function before rendering the hello world
  4. npm run dev and 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.

dct0 avatar Jul 31 '24 13:07 dct0

Solved?

twicer-is-coder avatar Aug 26 '24 16:08 twicer-is-coder

Solved?

No, I'm having the same issue

constantins2001 avatar Dec 21 '24 10:12 constantins2001

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
			});

yamada-sexta avatar Oct 13 '25 20:10 yamada-sexta