diff --git a/bin/youtube-dl b/bin/youtube-dl deleted file mode 100755 index 29a0f00..0000000 Binary files a/bin/youtube-dl and /dev/null differ diff --git a/package.json b/package.json index eb26466..1dd5572 100644 --- a/package.json +++ b/package.json @@ -30,9 +30,11 @@ "bin-version-check-cli": "~2.0.0", "dargs": "~7.0.0", "execa": "~5.0.0", - "got": "~11.8.1", + "get-stream": "~6.0.0", + "got": "~11.8.2", "is-unix": "~1.0.0", "mkdirp": "~1.0.4", + "p-event": "~4.2.0", "p-reflect": "~2.1.0" }, "devDependencies": { diff --git a/scripts/postinstall.js b/scripts/postinstall.js index 996ad65..51384ab 100644 --- a/scripts/postinstall.js +++ b/scripts/postinstall.js @@ -1,12 +1,17 @@ 'use strict' -const { promisify } = require('util') -const stream = require('stream') +const getStream = require('get-stream') +const fs = require('fs/promises') +const pEvent = require('p-event') const mkdirp = require('mkdirp') + const got = require('got') -const fs = require('fs') -const pipeline = promisify(stream.pipeline) +const BINARY_CONTENT_TYPES = [ + 'binary/octet-stream', + 'application/octet-stream', + 'application/x-binary' +] const { YOUTUBE_DL_PATH, @@ -15,27 +20,23 @@ const { YOUTUBE_DL_FILENAME } = require('../src/constants') -const getBinaryUrl = async endpoint => { - const [{ assets }] = await got(endpoint, { - responseType: 'json', - resolveBodyOnly: true - }) +const getBinary = async url => { + const stream = got.stream(url) + const response = await pEvent(stream, 'response') + const contentType = response.headers['content-type'] + + if (BINARY_CONTENT_TYPES.includes(contentType)) { + return getStream(stream, { encoding: 'buffer' }) + } + const [{ assets }] = JSON.parse(await getStream(stream)) const { browser_download_url: downloadUrl } = assets.find( ({ name }) => name === YOUTUBE_DL_FILENAME ) - return downloadUrl -} -const main = async url => { - await mkdirp(YOUTUBE_DL_DIR) - return pipeline( - got.stream(url), - fs.createWriteStream(YOUTUBE_DL_PATH, { mode: 493 }) - ) + return got(downloadUrl).buffer() } -getBinaryUrl(YOUTUBE_DL_HOST) - .then(main) - .then(message => message && console.log(message)) +Promise.all([getBinary(YOUTUBE_DL_HOST), mkdirp(YOUTUBE_DL_DIR)]) + .then(([buffer]) => fs.writeFile(YOUTUBE_DL_PATH, buffer, { mode: 493 })) .catch(err => console.error(err.message || err)) diff --git a/src/constants.js b/src/constants.js index 462b112..d8622d3 100644 --- a/src/constants.js +++ b/src/constants.js @@ -7,7 +7,7 @@ const PLATFORM_WIN = 'windows' const PLATFORM_UNIX = 'unix' const YOUTUBE_DL_HOST = - process.env.YOUTUBE_DL_YOUTUBE_DL_HOST || + process.env.YOUTUBE_DL_HOST || 'https://api.github.com/repos/ytdl-org/youtube-dl/releases?per_page=1' const YOUTUBE_DL_DIR =