Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

293
Views
Pull data from XMLHttpRequest into a ReadableStream

I'm using the zip-stream library (essentially a custom ReadableStream) to stream zip files to StreamSaver.js

I'm having trouble figuring out how to pipe data from an XMLHttpRequest into this readable stream.

I was thinking of something like this (pseudo code):

var xhr = new XMLHttpRequest();
for(url in urls) {
    xhr.open('GET', url, true)
    xhr.responseType = 'arraybuffer';
    xhr.onload = () => {
        if(xhr.status == 200) {
            ArbitraryGetDataFunction(xhr.response).then(function (data) {
                let arr = new Uint8Array(data)
                //somehow pipe arr into ReadableStream
            }
        }
    }   
}

I'm new to ReadableStreams (and js in general), and most examples of using ReadableStreams have the stream initialized with a start or pull function that already 'knows' what data is being streamed when initialized, like this example given by the zip-stream author:

const readableZipStream = new ZIP({
          start (ctrl) {
            ctrl.enqueue(file1)
            ctrl.enqueue(file2)
            ctrl.enqueue(file3)
            ctrl.enqueue({name: 'streamsaver-zip-example/empty folder', directory: true})
            // ctrl.close()
          },
          async pull (ctrl) {
            // Gets executed everytime zip.js asks for more data
            const url = 'https://d8d913s460fub.cloudfront.net/videoserver/cat-test-video-320x240.mp4'
            const res = await fetch(url)
            const stream = () => res.body
            const name = 'streamsaver-zip-example/cat.mp4'

            ctrl.enqueue({ name, stream })
              
            // if (done adding all files)
            ctrl.close()
          }
        })

However, I want to create the ReadableStream object BEFORE the XHR is processed. If this is not possible using ReadableStreams, what other method can I use?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!