Code Monkey home page Code Monkey logo

fetch-to-tar's Introduction

fetch-to-tar

There are many great backend based solutions for downloading multiple files into one archive. There are many great software engineers who will tell you that this is the right way. But if you need a quick and lightweight solution that just work in a browser, then fetch-to-tar is for you.

How it works?

fetch-to-tar download files in small chunks using Fetch Api and Stream Api. Then it saves them to IndexedDB immediately as part of the GNU TAR format.

At the end of the download, it sticks together all chunks and returns as single Blob. All saved data in indexedDB will be deleted.


Basically this is a very simple attempt to make download like mega.nz. Without encryption and support for older browsers. This is more like an experiment. But it works.

Limitations

Installation

You can install fetch-to-tar using npm or yarn

npm install fetch-to-tar
yarn add fetch-to-tar

Usage

Basic usage example:

const { promise } = fetchToTar({
  entries: [
    { name: 'foo.txt', src: 'http://example.com/foo.txt' },
    { name: 'bar.txt', src: 'http://example.com/bar.txt' },
  ],
});

promise.then(({ blob }) => {
  console.log('Tadaaa:', blob);
});

How to show progress:

fetchToTar({
  entries: [
    { name: 'foo.txt', src: 'http://example.com/foo.txt' },
    { name: 'bar.txt', src: 'http://example.com/bar.txt' },
  ],

  onProgress(value, max) {
    console.log(`Progress is: ${value}/${max}`)
  }
});

How to cancel download:

const { promise, cancel } = fetchToTar({
  entries: [
    { name: 'foo.txt', src: 'http://example.com/foo.txt' },
    { name: 'bar.txt', src: 'http://example.com/bar.txt' },
  ],
});

if (SomeCondition) {
  cancel()
}

You can use any name for creating folder structure:

fetchToTar({
  entries: [
    { name: 'one/foo.txt', src: 'http://example.com/foo.txt' },
    { name: 'two/bar.txt', src: 'http://example.com/bar.txt' },
  ],
});

License

MIT ยฉ Ruslan Tatyshev

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.