distubejs / ytdl-core

YouTube video downloader in javascript.
MIT License
206 stars 43 forks source link

Default options not applied when getInfo() is called consecutively with same video ID #63

Closed Brainicism closed 1 month ago

Brainicism commented 1 month ago

Describe the bug

getInfo() calls getBasicInfo()

exports.getInfo = async(id, options) => {
  const info = await exports.getBasicInfo(id, options);

But the result of both getInfo() and getBasicInfo() are cached with the video ID as the cache key

for (let funcName of ['getBasicInfo', 'getInfo']) {
  /**
   * @param {string} link
   * @param {Object} options
   * @returns {Promise<Object>}
   */
  const func = exports[funcName];
  exports[funcName] = async(link, options = {}) => {
    utils.checkForUpdates();
    let id = await urlUtils.getVideoID(link);
    const key = [funcName, id, options.lang].join('-');
    return exports.cache.getOrSet(key, () => func(id, options));
  };
}

This causes issues because getBasicInfo() performs mutations on the ytdl options passed in by the user. When getBasicInfo() is already cached, it isn't invoked, and the mutations on options are not performed. Some of these mutations include

  utils.applyIPv6Rotations(options);
  utils.applyDefaultHeaders(options);
  utils.applyDefaultAgent(options);
  utils.applyOldLocalAddress(options);

In my case, this is causing the default agent to be missing.

node ➜ /workspace (update-ytdl) $ npx ts-node src/ytdl-test.ts 
/workspace/node_modules/@distube/ytdl-core/lib/info.js:322
  const { jar, dispatcher } = options.agent;
          ^
TypeError: Cannot destructure property 'jar' of 'options.agent' as it is undefined.
    at fetchIosJsonPlayer (/workspace/node_modules/@distube/ytdl-core/lib/info.js:322:11)
    at exports.getInfo (/workspace/node_modules/@distube/ytdl-core/lib/info.js:247:35)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)

Minimal repro:

(async () => {
    const x = await ytdl.getBasicInfo(
        "http://www.youtube.com/watch?v=aqz-KE-bpKQ",
    );

    const y = await ytdl.getInfo("http://www.youtube.com/watch?v=aqz-KE-bpKQ");
    // or ytdl(`https://www.youtube.com/watch?v=aqz-KE-bpKQ`);
})();

Debug File

Environment

kevinrss01 commented 1 month ago

Same here 👋

kevinrss01 commented 1 month ago

Did you find a workaround? @Brainicism

Brainicism commented 1 month ago

You can just provide the default agent when passing in the options

const stream = ytdl(ytdl.getVideoID(req.query.video), {
  agent: ytdl.createAgent(),
  ...
})
skick1234 commented 1 month ago

I forgot that ytdl-core has a cache system .-. Will fix it when I get home