Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

download

Package Overview
Dependencies
Maintainers
3
Versions
70
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

download - npm Package Compare versions

Comparing version 5.0.3 to 6.0.0

63

index.js

@@ -6,2 +6,3 @@ 'use strict';

const caw = require('caw');
const contentDisposition = require('content-disposition');
const decompress = require('decompress');

@@ -13,27 +14,24 @@ const filenamify = require('filenamify');

const pify = require('pify');
const pEvent = require('p-event');
const fsP = pify(fs);
const createPromise = (uri, output, stream, opts) => {
const response = opts.encoding === null ? getStream.buffer(stream) : getStream(stream, opts);
function filenameFromPath(res) {
return path.basename(url.parse(res.requestUrl).pathname);
}
return response.then(data => {
if (!output && opts.extract) {
return decompress(data, opts);
}
function getFilename(res) {
const header = res.headers['content-disposition'];
if (!header) {
return filenameFromPath(res);
}
if (!output) {
return data;
}
const parsed = contentDisposition.parse(res.headers['content-disposition']);
if (parsed.type === 'attachment' && parsed.parameters && parsed.parameters.filename) {
return parsed.parameters.filename;
}
if (opts.extract) {
return decompress(data, path.dirname(output), opts);
}
return filenameFromPath(res);
}
return pify(mkdirp)(path.dirname(output))
.then(() => fsP.writeFile(output, data))
.then(() => data);
});
};
module.exports = (uri, output, opts) => {

@@ -58,5 +56,30 @@ if (typeof output === 'object') {

const stream = got.stream(uri, Object.assign(opts, {agent}));
const dest = output ? path.join(output, filenamify(path.basename(uri))) : null;
const promise = createPromise(uri, dest, stream, opts);
const promise = pEvent(stream, 'response').then(res => {
const encoding = opts.encoding === null ? 'buffer' : opts.encoding;
return Promise.all([getStream(stream, {encoding}), res]);
}).then(result => {
// TODO: Use destructuring when targeting Node.js 6
const data = result[0];
const res = result[1];
if (!output && opts.extract) {
return decompress(data, opts);
}
if (!output) {
return data;
}
const outputFilepath = path.join(output, filenamify(getFilename(res)));
if (opts.extract) {
return decompress(data, path.dirname(outputFilepath), opts);
}
return pify(mkdirp)(path.dirname(outputFilepath))
.then(() => fsP.writeFile(outputFilepath, data))
.then(() => data);
});
stream.then = promise.then.bind(promise);

@@ -63,0 +86,0 @@ stream.catch = promise.catch.bind(promise);

{
"name": "download",
"version": "5.0.3",
"version": "6.0.0",
"description": "Download and extract files",

@@ -30,2 +30,3 @@ "license": "MIT",

"caw": "^2.0.0",
"content-disposition": "^0.5.2",
"decompress": "^4.0.0",

@@ -36,2 +37,3 @@ "filenamify": "^2.0.0",

"mkdirp": "^0.5.1",
"p-event": "^1.0.0",
"pify": "^2.3.0"

@@ -38,0 +40,0 @@ },

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc