stream2multipart

0.2.3 • Public • Published

stream2multipart

Receive stream chunks and convert it into an easy to use object array.

Each element in the array is a section or part of the multipart download response, which can include plain text, JSON, or binaries like PDFs, JPEGs, XLS, XLSX.

Quick start

npm install stream2multipart

const Stream2MultiPart = require('stream2multipart');
const axios = require('axios');
const fs = require('fs');

const config = {
    method: 'get',
    url: 'https://yourServerUrl',
    headers: {
        Authorization: 'Bearer add authentication and any other headers',
    },
    responseType: 'stream'  // This is important!
};

const path = '/Users/oscaryu/Downloads/';

axios(config).then(async (res) => {
    const parts = await Stream2MultiPart.getParts(res);
    if (parts && parts.length) {
        parts.forEach((part) => {
            if (part.Filename) {
                const wstream = fs.createWriteStream(`${path}${part.Filename}`);
                wstream.write(part.Content);
                wstream.end();
                console.log(`Saved ${path}${part.Filename}`);
            } else if (part.ContentType === 'application/json') {
                console.log(JSON.stringify(part.Content, null, 4));
            } else {
                console.log(part.Content);
            }
        });
    }
});

Package Sidebar

Install

npm i stream2multipart

Weekly Downloads

0

Version

0.2.3

License

MIT

Unpacked Size

6.39 kB

Total Files

4

Last publish

Collaborators

  • orca256