Skip to content

Commit

Permalink
fix: check for binary data
Browse files Browse the repository at this point in the history
closes #146
  • Loading branch information
Kikobeats committed Apr 16, 2023
1 parent a2971a1 commit 35ac355
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
22 changes: 10 additions & 12 deletions scripts/postinstall.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ const get = url =>
)
)

const BINARY_CONTENT_TYPES = [
'binary/octet-stream',
'application/octet-stream',
'application/x-binary'
]

const {
YOUTUBE_DL_PATH,
YOUTUBE_DL_HOST,
Expand All @@ -32,15 +26,19 @@ const {
YOUTUBE_DL_SKIP_DOWNLOAD
} = require('../src/constants')

const getBinary = async url => {
const { response, data } = await get(url)
const contentType = response['content-type']
if (BINARY_CONTENT_TYPES.includes(contentType)) return data
const getLatest = data => {
const [{ assets }] = JSON.parse(data)
const { browser_download_url: downloadUrl } = assets.find(
const { browser_download_url: url } = assets.find(
({ name }) => name === YOUTUBE_DL_FILE
)
return (await get(downloadUrl)).data
return get(url).then(({ data }) => data)
}

const getBinary = async url => {
const { response, data } = await get(url)
return response.headers['content-type'] === 'application/octet-stream'
? data
: getLatest(data)
}

if (!YOUTUBE_DL_SKIP_DOWNLOAD) {
Expand Down
10 changes: 5 additions & 5 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ const PLATFORM_UNIX = 'unix'
function get (key) {
if (!key) return undefined
return (
process.env[key] ||
process.env[`npm_config_${key.toLowerCase()}`] ||
process.env[key] ??
process.env[`npm_config_${key.toLowerCase()}`] ??
process.env[`npm_config_${key.toUpperCase()}`]
)
}

const YOUTUBE_DL_HOST =
get('YOUTUBE_DL_HOST') ||
get('YOUTUBE_DL_HOST') ??
'https://api.github.com/repos/yt-dlp/yt-dlp/releases?per_page=1'

const YOUTUBE_DL_DIR =
get('YOUTUBE_DL_DIR') || path.join(__dirname, '..', 'bin')
get('YOUTUBE_DL_DIR') ?? path.join(__dirname, '..', 'bin')

const YOUTUBE_DL_PLATFORM =
get('YOUTUBE_DL_PLATFORM') || isUnix(process.platform)
get('YOUTUBE_DL_PLATFORM') ?? isUnix(process.platform)
? PLATFORM_UNIX
: PLATFORM_WIN

Expand Down

0 comments on commit 35ac355

Please sign in to comment.