Socket
Socket
Sign inDemoInstall

node-downloader-helper

Package Overview
Dependencies
Maintainers
1
Versions
32
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-downloader-helper - npm Package Compare versions

Comparing version 1.0.6 to 1.0.7

90

dist/index.js

@@ -55,3 +55,3 @@ 'use strict';

function DownloaderHelper(url, destFolder) {
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : { headers: {} };
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};

@@ -68,2 +68,7 @@ _classCallCheck(this, DownloaderHelper);

_this.state = DH_STATES.IDLE;
_this.__defaultOpts = {
headers: {},
override: false,
fileName: ''
};

@@ -74,3 +79,3 @@ _this.__total = 0;

_this.__states = DH_STATES;
_this.__opts = Object.assign({}, { headers: {} }, options);
_this.__opts = Object.assign({}, _this.__defaultOpts, options);
_this.__headers = _this.__opts.headers;

@@ -150,4 +155,7 @@ _this.__isResumed = false;

// Create File
_this2.__fileName = _this2.__opts.hasOwnProperty('fileName') && _this2.__opts.fileName ? _this2.__opts.fileName : _this2.__fileName;
_this2.__fileName = _this2.__opts.fileName ? _this2.__opts.fileName : _this2.__fileName;
_this2.__filePath = path.join(_this2.__destFolder, _this2.__fileName);
if (!_this2.__opts.override) {
_this2.__filePath = _this2.__uniqFileNameSync(_this2.__filePath);
}
_this2.__fileStream = fs.createWriteStream(_this2.__filePath, _this2.__isResumed ? { 'flags': 'a' } : {});

@@ -168,17 +176,22 @@

_this2.__fileStream.on('finish', function () {
if (_this2.state !== _this2.__states.PAUSED && _this2.state !== _this2.__states.STOPPED) {
_this2.__setState(_this2.__states.FINISHED);
_this2.emit('end');
}
_this2.__fileStream.close();
return resolve(true);
_this2.__fileStream.close(function (_err) {
if (_err) {
return reject(_err);
}
if (_this2.state !== _this2.__states.PAUSED && _this2.state !== _this2.__states.STOPPED) {
_this2.__setState(_this2.__states.FINISHED);
_this2.emit('end');
}
return resolve(true);
});
});
_this2.__fileStream.on('error', function (err) {
_this2.__fileStream.close(function () {
fs.unlink(_this2.__filePath, function () {
return reject(err);
});
});
_this2.__setState(_this2.__states.FAILED);
_this2.emit('error', err);
_this2.__fileStream.close();
_this2.__setState(_this2.__states.FAILED);
fs.unlink(_this2.__filePath, function () {
return reject(err);
});
return reject(err);

@@ -190,12 +203,11 @@ });

_this2.__request.on('error', function (err) {
_this2.emit('error', err);
_this2.__setState(_this2.__states.FAILED);
if (_this2.__fileStream) {
_this2.__fileStream.close();
fs.unlink(_this2.__filePath, function () {
return reject(err);
_this2.__fileStream.close(function () {
fs.unlink(_this2.__filePath, function () {
return reject(err);
});
});
}
_this2.emit('error', err);
_this2.__setState(_this2.__states.FAILED);
return reject(err);

@@ -210,5 +222,9 @@ });

value: function pause() {
if (this.__request) {
this.__request.abort();
}
if (this.__fileStream) {
this.__fileStream.close();
}
this.__setState(this.__states.PAUSED);
this.__request.abort();
this.__fileStream.close();
this.emit('pause');

@@ -238,3 +254,2 @@ return Promise.resolve(true);

this.__setState(this.__states.STOPPED);
if (this.__request) {

@@ -246,2 +261,3 @@ this.__request.abort();

}
this.__setState(this.__states.STOPPED);
return new Promise(function (resolve, reject) {

@@ -357,2 +373,28 @@ fs.access(_this4.__filePath, function (_accessErr) {

}
}, {
key: '__uniqFileName',
value: function __uniqFileName(path) {
if (typeof path !== 'string' || path === '') {
return path;
}
try {
fs.accessSync(path, fs.F_OK);
var pathInfo = path.match(/(.*)(\([0-9]+\))(\..*)$/);
var base = pathInfo ? pathInfo[1].trim() : path;
var suffix = pathInfo ? parseInt(pathInfo[2].replace(/\(|\)/, '')) : 0;
var ext = path.split('.').pop();
if (ext !== path) {
ext = '.' + ext;
base = base.replace(ext, '');
} else {
ext = '';
}
return this.__uniqFileName(base + ' (' + ++suffix + ')' + ext);
} catch (err) {
return path;
}
}
}]);

@@ -359,0 +401,0 @@

@@ -6,5 +6,7 @@ /*eslint no-console: ["error", { allow: ["log", "warn", "error"] }] */

// Options are optional
// these are the default options
const options = {
headers : {},
fileName: ''
headers : {}, // http headers ex: 'Authorization'
fileName: '', // custom filename when saved
override: false, //if true it will override the file, otherwise will append '(number)' to the end of file
};

@@ -11,0 +13,0 @@ const dl = new DownloaderHelper(url, __dirname, options);

{
"name": "node-downloader-helper",
"version": "1.0.6",
"version": "1.0.7",
"description": "A simple http file downloader for node.js",

@@ -5,0 +5,0 @@ "main": "./dist/index.js",

@@ -20,3 +20,3 @@ import { EventEmitter } from 'events';

export class DownloaderHelper extends EventEmitter {
constructor(url, destFolder, options = { headers: {} }) {
constructor(url, destFolder, options = {}) {
super();

@@ -30,2 +30,7 @@

this.state = DH_STATES.IDLE;
this.__defaultOpts = {
headers: {},
override: false,
fileName: ''
};

@@ -36,3 +41,3 @@ this.__total = 0;

this.__states = DH_STATES;
this.__opts = Object.assign({}, { headers: {} }, options);
this.__opts = Object.assign({}, this.__defaultOpts, options);
this.__headers = this.__opts.headers;

@@ -112,6 +117,9 @@ this.__isResumed = false;

// Create File
this.__fileName = (this.__opts.hasOwnProperty('fileName') && this.__opts.fileName)
this.__fileName = (this.__opts.fileName)
? this.__opts.fileName
: this.__fileName;
this.__filePath = path.join(this.__destFolder, this.__fileName);
if (!this.__opts.override) {
this.__filePath = this.__uniqFileNameSync(this.__filePath);
}
this.__fileStream = fs.createWriteStream(this.__filePath,

@@ -131,16 +139,22 @@ this.__isResumed ? { 'flags': 'a' } : {});

this.__fileStream.on('finish', () => {
if (this.state !== this.__states.PAUSED &&
this.state !== this.__states.STOPPED) {
this.__setState(this.__states.FINISHED);
this.emit('end');
}
this.__fileStream.close();
return resolve(true);
this.__fileStream.close(_err => {
if (_err) {
return reject(_err);
}
if (this.state !== this.__states.PAUSED &&
this.state !== this.__states.STOPPED) {
this.__setState(this.__states.FINISHED);
this.emit('end');
}
return resolve(true);
});
});
this.__fileStream.on('error', err => {
this.__fileStream.close(() => {
fs.unlink(this.__filePath, () => reject(err));
});
this.__setState(this.__states.FAILED);
this.emit('error', err);
this.__fileStream.close();
this.__setState(this.__states.FAILED);
fs.unlink(this.__filePath, () => reject(err));
return reject(err);

@@ -152,10 +166,9 @@ });

this.__request.on('error', err => {
if (this.__fileStream) {
this.__fileStream.close(() => {
fs.unlink(this.__filePath, () => reject(err));
});
}
this.emit('error', err);
this.__setState(this.__states.FAILED);
if (this.__fileStream) {
this.__fileStream.close();
fs.unlink(this.__filePath, () => reject(err));
}
return reject(err);

@@ -169,5 +182,9 @@ });

pause() {
if (this.__request) {
this.__request.abort();
}
if (this.__fileStream) {
this.__fileStream.close();
}
this.__setState(this.__states.PAUSED);
this.__request.abort();
this.__fileStream.close();
this.emit('pause');

@@ -190,3 +207,2 @@ return Promise.resolve(true);

stop() {
this.__setState(this.__states.STOPPED);
if (this.__request) {

@@ -198,2 +214,3 @@ this.__request.abort();

}
this.__setState(this.__states.STOPPED);
return new Promise((resolve, reject) => {

@@ -304,2 +321,27 @@ fs.access(this.__filePath, _accessErr => {

__uniqFileName(path) {
if (typeof path !== 'string' || path === '') {
return path;
}
try {
fs.accessSync(path, fs.F_OK);
let pathInfo = path.match(/(.*)(\([0-9]+\))(\..*)$/);
let base = pathInfo ? pathInfo[1].trim() : path;
let suffix = pathInfo ? parseInt(pathInfo[2].replace(/\(|\)/, '')) : 0;
let ext = path.split('.').pop();
if (ext !== path) {
ext = '.' + ext;
base = base.replace(ext, '');
} else {
ext = '';
}
return this.__uniqFileName(base + ' (' + (++suffix) + ')' + ext);
} catch (err) {
return path;
}
}
}
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