| Leo Repp | 58b9f11 | 2021-11-22 11:57:47 +0100 | [diff] [blame^] | 1 | 'use strict'; |
| 2 | const fs = require('fs'); |
| 3 | const path = require('path'); |
| 4 | const url = require('url'); |
| 5 | const caw = require('caw'); |
| 6 | const contentDisposition = require('content-disposition'); |
| 7 | const decompress = require('decompress'); |
| 8 | const filenamify = require('filenamify'); |
| 9 | const getStream = require('get-stream'); |
| 10 | const got = require('got'); |
| 11 | const makeDir = require('make-dir'); |
| 12 | const pify = require('pify'); |
| 13 | const pEvent = require('p-event'); |
| 14 | const fileType = require('file-type'); |
| 15 | const extName = require('ext-name'); |
| 16 | |
| 17 | const fsP = pify(fs); |
| 18 | const filenameFromPath = res => path.basename(url.parse(res.requestUrl).pathname); |
| 19 | |
| 20 | const getExtFromMime = res => { |
| 21 | const header = res.headers['content-type']; |
| 22 | |
| 23 | if (!header) { |
| 24 | return null; |
| 25 | } |
| 26 | |
| 27 | const exts = extName.mime(header); |
| 28 | |
| 29 | if (exts.length !== 1) { |
| 30 | return null; |
| 31 | } |
| 32 | |
| 33 | return exts[0].ext; |
| 34 | }; |
| 35 | |
| 36 | const getFilename = (res, data) => { |
| 37 | const header = res.headers['content-disposition']; |
| 38 | |
| 39 | if (header) { |
| 40 | const parsed = contentDisposition.parse(header); |
| 41 | |
| 42 | if (parsed.parameters && parsed.parameters.filename) { |
| 43 | return parsed.parameters.filename; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | let filename = filenameFromPath(res); |
| 48 | |
| 49 | if (!path.extname(filename)) { |
| 50 | const ext = (fileType(data) || {}).ext || getExtFromMime(res); |
| 51 | |
| 52 | if (ext) { |
| 53 | filename = `${filename}.${ext}`; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | return filename; |
| 58 | }; |
| 59 | |
| 60 | module.exports = (uri, output, opts) => { |
| 61 | if (typeof output === 'object') { |
| 62 | opts = output; |
| 63 | output = null; |
| 64 | } |
| 65 | |
| 66 | let protocol = url.parse(uri).protocol; |
| 67 | |
| 68 | if (protocol) { |
| 69 | protocol = protocol.slice(0, -1); |
| 70 | } |
| 71 | |
| 72 | opts = Object.assign({ |
| 73 | encoding: null, |
| 74 | rejectUnauthorized: process.env.npm_config_strict_ssl !== 'false' |
| 75 | }, opts); |
| 76 | |
| 77 | const agent = caw(opts.proxy, {protocol}); |
| 78 | const stream = got.stream(uri, Object.assign({agent}, opts)); |
| 79 | |
| 80 | const promise = pEvent(stream, 'response').then(res => { |
| 81 | const encoding = opts.encoding === null ? 'buffer' : opts.encoding; |
| 82 | return Promise.all([getStream(stream, {encoding}), res]); |
| 83 | }).then(result => { |
| 84 | // TODO: Use destructuring when targeting Node.js 6 |
| 85 | const data = result[0]; |
| 86 | const res = result[1]; |
| 87 | |
| 88 | if (!output) { |
| 89 | return opts.extract ? decompress(data, opts) : data; |
| 90 | } |
| 91 | |
| 92 | const filename = opts.filename || filenamify(getFilename(res, data)); |
| 93 | const outputFilepath = path.join(output, filename); |
| 94 | |
| 95 | if (opts.extract) { |
| 96 | return decompress(data, path.dirname(outputFilepath), opts); |
| 97 | } |
| 98 | |
| 99 | return makeDir(path.dirname(outputFilepath)) |
| 100 | .then(() => fsP.writeFile(outputFilepath, data)) |
| 101 | .then(() => data); |
| 102 | }); |
| 103 | |
| 104 | stream.then = promise.then.bind(promise); |
| 105 | stream.catch = promise.catch.bind(promise); |
| 106 | |
| 107 | return stream; |
| 108 | }; |