gatecrasher777/ytcog

[help wanted] how to make this work on cloudflare workers?

ocylib opened this issue · 2 comments

Great job!!!
getting stream information with this package, and then warping the video stream from youtube to my own web with miniget package, my app works fine in Heroku.

for Heroku is a little slow in my country, so I use Cloudflare as a load balancer

Then I thought about whether Cloudflare workers could be used directly to complete these tasks. There are several advantages to using Cloudflare workers directly:

  1. Don’t worry about server dormancy (Heroku server will sleep on its own after a period of time and wake up when requested)
  2. Request information from youtube from different machines to avoid being blocked by youtube, when the request volume is large.

I tried to rewrite your code and use fetch instead of miniget. This can basically be done. But I found that you use the vm2 module in the player class, and it feels like you want to change the string from the intercepted string to runnable code. I don't know whether the vm2 module can be run directly in Cloudflare worker, it seems to use the built-in module of node js. So I want to use the new Function() of js to convert the string to runnable code, but I found that this function seems to be disabled by the Cloudflare worker.

I don’t know your code logic very well, I can only look for some clues in the code. If you can explain the whole design logic in detail, or give a code that can run on Cloudflare workers, I would be very grateful

You could try replace player.js with the player.js from version 2.1.0 which uses the nodejs vm instead of vm2. While vm2 is much more secure and vm has some vulnerabilities, it is highly unlikely that Google will put malicious code in their player. The interface is the same.

Essentially, player.js scans the session's base.js youtube player file for the decipher and n-transform code blocks. It parses the blocks and directly uses the code from base.js to create functions that can then be executed on each of the streams that the library encounters. Not all streams require the decipher function but virtually all streams require the n-tranform to avoid throttling.

I don't have any experience with Cloudflare workers, but reading a little about their isolates, it occurs to me that instead of using vm/vm2 to run the base.js code, you could possibly run them directly in their own Cloudflare isolates.

thanks