Downloader

Stream object

Code examples below

stream.start(callback)

Specify event that will be called when downloading will be started

stream.progress(callback)

Specify event that will be called every stream.progressInterval ms

This callback will receive the current downloaded data size, total data size, and how much data was downloaded after the previous call

stream.finish(callback)

Specify event that will be called when downloading will be finished

stream.pause()

Pause downloading

stream.resume()

Resume downloading

stream.close(forced)

Close downloading stream

Downloader class

Downloader.download(uri, output)

import { Downloader } from '@empathize/framework';

// Second argument will be automatically parsed
// by Downloader.fileFromUri() method if it is not specified
Downloader.download('https://www.youtube.com', 'youtube.html').then(() => {
    stream.progressInterval = 1000;
    
    stream.start(() => {
        console.log('Downloading started');
    });
    
    stream.progress((current, total, difference) => {
        console.log(`Downloading progress: ${Math.round(current / total * 100)}%`);
    });
    
    stream.finish(() => {
        console.log('Downloading finished');
    });
});

Downloader.fileFromUri(uri)

import { Downloader } from '@empathize/framework';

// index.html
console.log(Downloader.fileFromUri('https://www.youtube.com'));

// test.zip
console.log(Downloader.fileFromUri('https://example.com/test.zip'));

Downloader.closeStreams(forced)

import { Downloader } from '@empathize/framework';

Downloader.closeStreams().then(() => {
    console.log('All downloading streams was closed');
});

Last updated