Skip to content

Just a little help :)) #283

@numairawan

Description

@numairawan

Hi , you gave me the example code to download chunks and save it as one file.

const chunks = [
    'https://example.com/',
    'https://example.com/',
]

function concatenateRequests(requests) {
    const ts = new TransformStream()
    const pump = () => {
        const it = requests.next()
        if (it.done) return ts.writable.close();
        fetch(it.value).then(res => {
            console.log(res.body);
            return res.body.pipeTo(ts.writable, { preventClose: true })
        }).then(pump, ts.writable.abort.bind(ts.writable));
    }
  
    pump();
  
    return ts.readable;
  }

  concatenateRequests(chunks.values()).pipeTo(streamSaver.createWritableStream('archive.zip'))

But i cant catch the error and retry for new url that i will get from server. So can you please make it i loop? for example

async function test(chunks) {

    const ts = new TransformStream();

    for (let x = 0; x < chunks.length; x++) {

        await fetch(x)
            .then(async res => {

                if (!res.ok || res.status !== 200) {
                    // get error message from body or default to response status
                    return Promise.reject(error);
                    throw ('Download failed');
                }

                return res.body.pipeTo(ts.writable, {
                    preventClose: true
                })
            })
            .then(ts.writable.abort.bind(ts.writable))
            .catch(async (error) => {

                // renew the url
                var newURL = await get_fresh_link(x);
                urls[x] = newURL;

                if (x == 1) {
                    x = 1;
                } else {
                    x = x - 1;
                }

            });

    }

    return ts.readable;

}

test(chunks).pipeTo(streamSaver.createWritableStream('archive.zip'))

But its not working.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions