diff --git a/install.js b/install.js index 9a8105b..e9cbbe3 100644 --- a/install.js +++ b/install.js @@ -6,8 +6,11 @@ const {encode: encodeQuery} = require('querystring') const {strictEqual} = require('assert') const envPaths = require('env-paths') const FileCache = require('@derhuerst/http-basic/lib/FileCache').default +const {extname} = require('path') var ProgressBar = require("progress"); var request = require('@derhuerst/http-basic') +const {createGunzip} = require('zlib') +const {pipeline} = require('stream') var ffmpegPath = require("."); var pkg = require("./package"); @@ -71,6 +74,12 @@ cache.getCacheKey = (url) => { return FileCache.prototype.getCacheKey(normalizeS3Url(url)) } +const isGzUrl = (url) => { + const path = new URL(url).pathname.split('/') + const filename = path[path.length - 1] + return filename && extname(filename) === '.gz' +} + const noop = () => {} function downloadFile(url, destinationPath, progressCallback = noop) { let fulfill, reject; @@ -101,9 +110,19 @@ function downloadFile(url, destinationPath, progressCallback = noop) { } const file = fs.createWriteStream(destinationPath); - file.on("finish", () => fulfill()); - file.on("error", error => reject(error)); - response.body.pipe(file) + const streams = isGzUrl(url) + ? [response.body, createGunzip(), file] + : [response.body, file] + pipeline( + ...streams, + (err) => { + if (err) { + err.url = response.url + err.statusCode = response.statusCode + reject(err) + } else fulfill() + } + ) if (!response.fromCache && progressCallback) { const cLength = response.headers["content-length"] @@ -142,9 +161,11 @@ const releaseName = ( ) const arch = process.env.npm_config_arch || os.arch() const platform = process.env.npm_config_platform || os.platform() -const downloadUrl = `https://github.com/eugeneware/ffmpeg-static/releases/download/${release}/${platform}-${arch}` -const readmeUrl = `${downloadUrl}.README` -const licenseUrl = `${downloadUrl}.LICENSE` + +const baseUrl = `https://github.com/eugeneware/ffmpeg-static/releases/download/${release}` +const downloadUrl = `${baseUrl}/${platform}-${arch}.gz` +const readmeUrl = `${baseUrl}/${platform}-${arch}.README` +const licenseUrl = `${baseUrl}/${platform}-${arch}.LICENSE` downloadFile(downloadUrl, ffmpegPath, onProgress) .then(() => {