blob: 7e5fcb03a630666eaefd24e1a0de1c896706c29d [file] [log] [blame]
Leo Repp58b9f112021-11-22 11:57:47 +01001'use strict';
2const fs = require('fs');
3const path = require('path');
4const url = require('url');
5const caw = require('caw');
6const contentDisposition = require('content-disposition');
7const decompress = require('decompress');
8const filenamify = require('filenamify');
9const getStream = require('get-stream');
10const got = require('got');
11const makeDir = require('make-dir');
12const pify = require('pify');
13const pEvent = require('p-event');
14const fileType = require('file-type');
15const extName = require('ext-name');
16
17const fsP = pify(fs);
18const filenameFromPath = res => path.basename(url.parse(res.requestUrl).pathname);
19
20const 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
36const 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
60module.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};