nodejs-file-downloader
Advanced tools
Comparing version 4.6.0 to 4.7.0
@@ -0,2 +1,6 @@ | ||
## 4.7.0 17/06/2021 | ||
### Feature | ||
- Added type definition file. | ||
- Replaced config.cloneFiles = 'skip' with a new property, config.skipExistingFileName | ||
## 4.6.0 10/06/2021 | ||
@@ -3,0 +7,0 @@ |
@@ -27,3 +27,4 @@ const fs = require('fs'); | ||
* @param {string} [config.fileName = undefined] | ||
* @param {boolean | string} [config.cloneFiles=true] //Can also be the string "skip" | ||
* @param {boolean } [config.cloneFiles=true] | ||
* @param {boolean} [config.skipExistingFileName = false] | ||
* @param {number} [config.timeout=6000] | ||
@@ -50,2 +51,3 @@ * @param {object} [config.headers = undefined] | ||
cloneFiles: true, | ||
skipExistingFileName:false, | ||
shouldBufferResponse: false, | ||
@@ -83,4 +85,4 @@ onResponse: undefined, | ||
// bailout skip file | ||
if (this.config.fileName && this.config.cloneFiles === 'skip') { | ||
if (this.config.fileName && this.config.skipExistingFileName) { | ||
if (await exists(this.config.directory + '/' + this.config.fileName)) { | ||
@@ -149,5 +151,3 @@ return; | ||
if (finalName === this.config.fileName && | ||
this.config.cloneFiles === 'skip' && | ||
await exists(this.config.directory + '/' + this.config.fileName)) { | ||
if ( this.config.skipExistingFileName && await exists(this.config.directory + '/' + finalName)) { | ||
// will skip this request | ||
@@ -154,0 +154,0 @@ return; |
@@ -20,3 +20,3 @@ const rpur = require('./utils/rpur') | ||
cloneFiles: { | ||
type: ['boolean', 'string'], | ||
type: 'boolean', | ||
mandatory: false | ||
@@ -56,4 +56,4 @@ }, | ||
* @param {string} [config.fileName = undefined] | ||
* @param {boolean | string} [config.cloneFiles=true] true will create a duplicate. false will overwrite the existing file. The string "skip" | ||
* will cause the downloader to skip the download process in case a file with the same name already exists. | ||
* @param {boolean} [config.cloneFiles=true] true will create a duplicate. false will overwrite the existing file. | ||
* @param {boolean} [config.skipExistingFileName = false] true will cause the downloader to skip the download process in case a file with the same name already exists. | ||
* @param {number} [config.timeout=6000] | ||
@@ -90,2 +90,3 @@ * @param {number} [config.maxAttempts=1] | ||
cloneFiles: true, | ||
skipExistingFileName:false, | ||
shouldBufferResponse: false, | ||
@@ -126,7 +127,7 @@ onResponse: undefined, | ||
const that = this; | ||
const { url, directory, fileName, cloneFiles, timeout, headers, httpsAgent, proxy, onResponse, onBeforeSave, onProgress, shouldBufferResponse, useSynchronousMode } = that.config; | ||
const { url, directory, fileName, cloneFiles, skipExistingFileName, timeout, headers, httpsAgent, proxy, onResponse, onBeforeSave, onProgress, shouldBufferResponse, useSynchronousMode } = that.config; | ||
//Repeat downloading process until success | ||
await that._makeUntilSuccessful(async () => { | ||
const download = new Download({ url, directory, fileName, cloneFiles, timeout, headers, httpsAgent, proxy, onResponse, onBeforeSave, onProgress, shouldBufferResponse, useSynchronousMode }); | ||
const download = new Download({ url, directory, fileName, cloneFiles, skipExistingFileName, timeout, headers, httpsAgent, proxy, onResponse, onBeforeSave, onProgress, shouldBufferResponse, useSynchronousMode }); | ||
this._currentDownload = download | ||
@@ -133,0 +134,0 @@ |
327
main.test.js
@@ -161,3 +161,3 @@ // const axios = require('axios'); | ||
.reply(200, (uri, requestBody) => { | ||
downloadTimes += 1 | ||
downloadTimes++ | ||
return fs.createReadStream(Path.join(__dirname, 'fixtures/Desert.jpg')) | ||
@@ -168,3 +168,3 @@ // fs.readFile(Path.join(__dirname, 'fixtures/Desert.jpg'), cb) // Error-first callback | ||
'Content-Length': '23642' | ||
}) | ||
}).persist() | ||
const downloader = new Downloader({ | ||
@@ -174,3 +174,2 @@ url: `http://www.${host}.com/contentType`, | ||
fileName: "testfile.jpg", | ||
cloneFiles: 'skip' | ||
}) | ||
@@ -182,3 +181,3 @@ | ||
fileName: "testfile.jpg", | ||
cloneFiles: 'skip' | ||
skipExistingFileName:true | ||
}) | ||
@@ -188,3 +187,3 @@ | ||
await downloader.download(); | ||
await downloader.download(); | ||
// await downloader.download(); | ||
await downloader2.download(); | ||
@@ -304,3 +303,3 @@ // debugger | ||
//**Assumes an already existing Desert.js file */ | ||
// **Assumes an already existing Desert.js file */ | ||
it('Should create a number-appended file', async () => { | ||
@@ -965,321 +964,7 @@ // mock.onGet("/Desert.jpg").reply( | ||
// it('Should get ERR_REQUEST_CANCELLED error after cancellation, while streaming', async function () { | ||
// this.timeout(0); | ||
// let errorCounter = 0 | ||
// const downloader = new Downloader({ | ||
// fileName: 'cancelled.jpg', | ||
// maxAttempts: 4, | ||
// timeout:50000, | ||
// url: `http://localhost:3002/cancelWhileStream`, | ||
// directory: "./downloads", | ||
// onResponse() { | ||
// // debugger | ||
// // downloader.cancel() | ||
// setTimeout(() => { | ||
// downloader.cancel() | ||
// }, 2000) | ||
// }, | ||
// onError() { | ||
// errorCounter++ | ||
// } | ||
// }) | ||
// let error; | ||
// try { | ||
// // setTimeout(() => { | ||
// // downloader.cancel() | ||
// // }, 1000) | ||
// // debugger | ||
// await downloader.download(); | ||
// // console.log('success') | ||
// // debugger | ||
// } catch (e) { | ||
// error = e | ||
// // expect(error.code).toBe('ERR_REQUEST_CANCELLED') | ||
// } finally { | ||
// // console.log(error) | ||
// // debugger | ||
// expect(errorCounter).toBe(1) | ||
// if (!error || error.code !== 'ERR_REQUEST_CANCELLED') { | ||
// throw new Error('Cancellation did not work') | ||
// } | ||
// if(await doesFileExist('./downloads/cancelled.jpg')){ | ||
// throw new Error('cancelled.jpg was not deleted') | ||
// } | ||
// if(await doesFileExist('./downloads/cancelled.jpg')){ | ||
// throw new Error('cancelled.jpg.download was not deleted') | ||
// } | ||
// } | ||
// }) | ||
// it('Should get ERR_REQUEST_CANCELLED error after cancellation, before stream', async function () { | ||
// this.timeout(0) | ||
// let errorCounter = 0 | ||
// const downloader = new Downloader({ | ||
// fileName: 'cancelled.jpg', | ||
// maxAttempts: 4, | ||
// timeout:50000, | ||
// url: `http://localhost:3002/cancelBeforeStream`, | ||
// directory: "./downloads", | ||
// onResponse(r) { | ||
// console.log(r) | ||
// // debugger | ||
// // downloader.cancel() | ||
// }, | ||
// onError(){ | ||
// errorCounter++ | ||
// } | ||
// }) | ||
// let error; | ||
// try { | ||
// setTimeout(() => { | ||
// downloader.cancel() | ||
// }, 2000) | ||
// // debugger | ||
// await downloader.download(); | ||
// // console.log('success') | ||
// // debugger | ||
// } catch (e) { | ||
// error = e | ||
// // expect(error.code).toBe('ERR_REQUEST_CANCELLED') | ||
// } finally { | ||
// console | ||
// // console.log(error) | ||
// // debugger | ||
// expect(errorCounter).toBe(1) | ||
// if (!error || error.code !== 'ERR_REQUEST_CANCELLED') { | ||
// throw new Error('Cancellation did not work') | ||
// } | ||
// if(await doesFileExist('./downloads/cancelled.jpg')){ | ||
// throw new Error('cancelled.jpg was not deleted') | ||
// } | ||
// if(await doesFileExist('./downloads/cancelled.jpg')){ | ||
// throw new Error('cancelled.jpg.download was not deleted') | ||
// } | ||
// } | ||
// }) | ||
// it('Should get ERR_REQUEST_CANCELLED error after cancellation, while streaming, with shouldBufferResponse', async function () { | ||
// this.timeout(0); | ||
// let errorCounter = 0 | ||
// const downloader = new Downloader({ | ||
// fileName: 'cancelled.jpg', | ||
// maxAttempts: 4, | ||
// shouldBufferResponse:true, | ||
// url: `http://localhost:3002/cancelWhileStream`, | ||
// directory: "./downloads", | ||
// onResponse() { | ||
// // debugger | ||
// // downloader.cancel() | ||
// setTimeout(() => { | ||
// downloader.cancel() | ||
// }, 1000) | ||
// }, | ||
// onError() { | ||
// errorCounter++ | ||
// } | ||
// }) | ||
// let error; | ||
// try { | ||
// // setTimeout(() => { | ||
// // downloader.cancel() | ||
// // }, 1000) | ||
// // debugger | ||
// await downloader.download(); | ||
// // console.log('success') | ||
// // debugger | ||
// } catch (e) { | ||
// error = e | ||
// // expect(error.code).toBe('ERR_REQUEST_CANCELLED') | ||
// } finally { | ||
// // console.log(error) | ||
// // debugger | ||
// expect(errorCounter).toBe(1) | ||
// if (!error || error.code !== 'ERR_REQUEST_CANCELLED') { | ||
// throw new Error('Cancellation did not work') | ||
// } | ||
// if(await doesFileExist('./downloads/cancelled.jpg')){ | ||
// throw new Error('cancelled.jpg was not deleted') | ||
// } | ||
// if(await doesFileExist('./downloads/cancelled.jpg')){ | ||
// throw new Error('cancelled.jpg.download was not deleted') | ||
// } | ||
// } | ||
// }) | ||
// it('Should timeout during stream, twice', async function () { | ||
// let error; | ||
// this.timeout(0) | ||
// try { | ||
// // let counter = 0 | ||
// var onErrorCount = 0 | ||
// const downloader = new Downloader({ | ||
// timeout: 1500, | ||
// // debugMode:true, | ||
// maxAttempts: 2, | ||
// fileName: 'timeout.jpg', | ||
// url: `http://localhost:3002/timeoutDuringStream`, | ||
// directory: "./downloads", | ||
// onError: function (e) { | ||
// // debugger; | ||
// // console.log('error') | ||
// onErrorCount++; | ||
// } | ||
// }) | ||
// await downloader.download(); | ||
// debugger | ||
// } catch (e) { | ||
// error = e | ||
// // debugger; | ||
// // console.log('final error',error) | ||
// } finally { | ||
// // debugger; | ||
// expect(error.code).toBe('ERR_REQUEST_TIMEDOUT') | ||
// expect(onErrorCount).toBe(2) | ||
// // await verifyFile('./downloads/koala.jpg', 29051); | ||
// if(await doesFileExist('./downloads/timeout.jpg')){ | ||
// throw new Error('timeout.jpg was not deleted') | ||
// } | ||
// if(await doesFileExist('./downloads/timeout.jpg')){ | ||
// throw new Error('timeout.jpg.download was not deleted') | ||
// } | ||
// } | ||
// }) | ||
// it('Should timeout during stream, twice, with shouldBufferResponse', async function () { | ||
// let error; | ||
// this.timeout(0) | ||
// try { | ||
// // let counter = 0 | ||
// var onErrorCount = 0 | ||
// const downloader = new Downloader({ | ||
// timeout: 1500, | ||
// // debugMode:true, | ||
// shouldBufferResponse:true, | ||
// maxAttempts: 2, | ||
// fileName: 'timeout.jpg', | ||
// url: `http://localhost:3002/timeoutDuringStream`, | ||
// directory: "./downloads", | ||
// onError: function (e) { | ||
// // debugger; | ||
// // console.log('error') | ||
// onErrorCount++; | ||
// } | ||
// }) | ||
// await downloader.download(); | ||
// debugger | ||
// } catch (e) { | ||
// error = e | ||
// // debugger; | ||
// // console.log('final error',error) | ||
// } finally { | ||
// // debugger; | ||
// expect(error.code).toBe('ERR_REQUEST_TIMEDOUT') | ||
// expect(onErrorCount).toBe(2) | ||
// // await verifyFile('./downloads/koala.jpg', 29051); | ||
// if(await doesFileExist('./downloads/timeout.jpg')){ | ||
// throw new Error('timeout.jpg was not deleted') | ||
// } | ||
// if(await doesFileExist('./downloads/timeout.jpg')){ | ||
// throw new Error('timeout.jpg.download was not deleted') | ||
// } | ||
// } | ||
// }) | ||
// it('Should timeout before response', async function () { | ||
// let error; | ||
// this.timeout(0) | ||
// try { | ||
// // let counter = 0 | ||
// var onErrorCount = 0 | ||
// const downloader = new Downloader({ | ||
// timeout: 1500, | ||
// // debugMode:true, | ||
// // maxAttempts: 2, | ||
// fileName: 'timeout2.jpg', | ||
// url: `http://localhost:3002/timeoutBeforeResponse`, | ||
// directory: "./downloads", | ||
// onError: function (e) { | ||
// // debugger; | ||
// // console.log('error') | ||
// onErrorCount++; | ||
// } | ||
// }) | ||
// await downloader.download(); | ||
// debugger | ||
// } catch (e) { | ||
// error = e | ||
// // debugger; | ||
// // console.log('final error',error) | ||
// } finally { | ||
// // debugger; | ||
// expect(error.code).toBe('ERR_REQUEST_TIMEDOUT') | ||
// expect(onErrorCount).toBe(1) | ||
// // await verifyFile('./downloads/koala.jpg', 29051); | ||
// if(await doesFileExist('./downloads/timeout2.jpg')){ | ||
// throw new Error('timeout2.jpg was not deleted') | ||
// } | ||
// if(await doesFileExist('./downloads/timeout2.jpg')){ | ||
// throw new Error('timeout2.jpg.download was not deleted') | ||
// } | ||
// } | ||
// }) | ||
}) | ||
@@ -1286,0 +971,0 @@ |
@@ -28,5 +28,5 @@ const { http, https } = require('follow-redirects'); | ||
request.end() | ||
request.on('error',()=>{ | ||
request.on('error',(e)=>{ | ||
responsePromiseReject() | ||
responsePromiseReject(e) | ||
}) | ||
@@ -33,0 +33,0 @@ |
{ | ||
"name": "nodejs-file-downloader", | ||
"version": "4.6.0", | ||
"version": "4.7.0", | ||
"description": "A file downloader for NodeJs", | ||
@@ -8,2 +8,3 @@ "main": "Downloader.js", | ||
"test": "mocha *.test.js", | ||
"test-main": "mocha main.test.js", | ||
"test-timeout": "mocha timeout-cancellation.test.js", | ||
@@ -32,2 +33,3 @@ "test-watch": "nodemon --exec \"npm test\"" | ||
"@types/jest": "^25.2.2", | ||
"@types/node": "^15.12.2", | ||
"expect": "^26.0.1", | ||
@@ -47,3 +49,4 @@ "express": "^4.17.1", | ||
"url": "https://github.com/ibrod83/nodejs-file-downloader.git" | ||
} | ||
}, | ||
"typings": "Downloader.td.ts" | ||
} |
@@ -137,3 +137,3 @@ nodejs-file-downloader is a simple utility for downloading files. It hides the complexity of dealing with streams, redirects, paths and duplicate file names. Can automatically repeat failed downloads. | ||
``` | ||
You can also set cloneFiles to the string "skip". This will cause the downloader to simply skip a download, If a file with the same name exists. | ||
If you want to completely skip downloading a file, when a file with the same name already exists, use config.skipExistingFileName = true | ||
@@ -140,0 +140,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
19
186824
8
2029