Skip to content

Instantly share code, notes, and snippets.

@generalov
Created May 20, 2017 16:58
Show Gist options
  • Save generalov/45dee0ac4ac706c10d0e482fc82c260c to your computer and use it in GitHub Desktop.
Save generalov/45dee0ac4ac706c10d0e482fc82c260c to your computer and use it in GitHub Desktop.
/**
* The example of usage for [mt-downloader](https://www.npmjs.com/package/mt-downloader).
*
* ## Installation
*
* npm install mt-downloader muxer
*
* ## Usage
*
* // the URL to download from
* var url = 'https://assets-cdn.github.com/pinned-octocat.svg';
*
* // the output file name
* var destPath = 'pinned-octocat.svg';
*
* // the maximum number of parallel downloads
* var concurrence = 10;
*
* var download = createDownload({
* url: url,
* path: destPath,
* range: concurrence
* }).toPromise();
*
* download
* .then(() => console.log('done'))
* .catch((err) => console.log(err));
*
* Based on tushar.mathur's
* https://github.com/tusharmath/Multi-threaded-downloader/blob/master/perf/TestHelpers.js
*/
'use strict'
const Rx = require('rx').Rx;
const O = require('rx').Observable;
const R = require('ramda');
const CreateMTDFile = require('mt-downloader').CreateMTDFile;
const DownloadFromMTDFile = require('mt-downloader').DownloadFromMTDFile;
const FILE = require('mt-downloader').FILE;
const FinalizeDownload = require('mt-downloader').FinalizeDownload;
const MTDPath = require('mt-downloader').MTDPath;
const demux = require('muxer').demux;
/**
* Test UTILS for doing a real download.
*
* @param options
* @returns {Observable}
*/
function createDownload(options) {
/**
* Create MTD File
*/
const createMTDFile$ = CreateMTDFile(options).share()
const [{fdW$}] = demux(createMTDFile$, 'fdW$')
/**
* Download From MTD File
*/
const downloadFromMTDFile$ = createMTDFile$.last()
.map(MTDPath(options.path)).flatMap(DownloadFromMTDFile).share()
const [{fdR$, meta$}] = demux(downloadFromMTDFile$, 'meta$', 'fdR$')
/**
* Finalize Downloaded FILE
*/
const finalizeDownload$ = downloadFromMTDFile$.last()
.withLatestFrom(fdR$, meta$, (_, fd, meta) => ({
fd$: O.just(fd),
meta$: O.just(meta)
}))
.flatMap(FinalizeDownload)
.share()
.last()
/**
* Close File Descriptors
*/
const fd$ = finalizeDownload$
.withLatestFrom(fdW$, fdR$)
.map(R.tail)
.flatMap(R.map(R.of))
return FILE.close(fd$)
}
module.exports.createDownload = createDownload;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment