Puppeteer Download File

Download file with Puppeteer and write it to the file system with a custom name.

{
  const puppeteer = require('puppeteer');
  const request = require('request');
  const fs = require('fs');
  const user_agent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.1324.36 Safari/537.36';

  function download(uri, filename)
  {
    return new Promise((resolve, reject) => {
      request.head(uri, function (err, res, body) {
        request(uri).pipe(fs.createWriteStream(filename)).on('close', resolve);
      });
    });
  }

  (async () => {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();

    try {
      page.setUserAgent(user_agent);
      await page.setViewport({ width: 1600, height: 900 })

      await download('http://localhost/test.zip', 'download.zip');

      await browser.close();

    } catch (error) {
      console.log('error', error);
    }
  })()
}