Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

290
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda