Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/toyobayashi/downloader


https://github.com/toyobayashi/downloader

Last synced: 2 days ago
JSON representation

Awesome Lists containing this project

README

        

# downloader

Tiny downloader in Node.js

[API Documentation](https://github.com/toyobayashi/downloader/blob/main/docs/api/index.md)

## Usage

```bash
npm install @tybys/downloader
```

```js
const { Downloader, DownloadOverwrite } = require('@tybys/downloader')

const downloader = new Downloader()
downloader.settings.maxConcurrentDownloads = 1
downloader.settings.overwrite = DownloadOverwrite.RENAME

const electron1204 = downloader.add('https://npm.taobao.org/mirrors/electron/12.0.4/electron-v12.0.4-win32-x64.zip', {
dir: __dirname,
out: 'electron-v12.0.4-win32-x64.zip'
})
const electron1203 = downloader.add('https://npm.taobao.org/mirrors/electron/12.0.3/electron-v12.0.3-win32-x64.zip', {
dir: __dirname,
out: 'electron-v12.0.3-win32-x64.zip'
})

const onProgress = (downloadProgress) => {
if (process.stdout.clearLine) {
process.stdout.clearLine(0)
process.stdout.cursorTo(0)
process.stdout.write(`percent: ${downloadProgress.percent}, speed: ${downloadProgress.downloadSpeed / 1000} KB/s`)
}
}

electron1204.on('progress', onProgress)
electron1203.on('progress', onProgress)

electron1204.on('fail', () => {
console.log(electron1204.error)
})

electron1204.whenStopped().then(() => {
console.log('whenStopped ' + electron1204.gid)
}).catch(err => {
console.log(err.message)
})

electron1203.whenStopped().then(() => {
console.log('whenStopped ' + electron1203.gid)
}).catch(err => {
console.log(err.message)
})

setTimeout(() => {
electron1204.abort()
}, 2000)
```