Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

You could possible avoid node streams altogether here: #6

Closed
jimmywarting opened this issue Aug 1, 2020 · 8 comments
Closed

You could possible avoid node streams altogether here: #6

jimmywarting opened this issue Aug 1, 2020 · 8 comments

Comments

@jimmywarting
Copy link

Here is two issues

const mystream = new stream.PassThrough();
const chunks = new Array(pandoraBoxStream.chunksCount).map( (it, index) => index );
ctrl.enqueue({
name: pandoraBoxStream.path,
lastModified: new Date(0),
stream: mystream,
});

137: it's a node stream and not a web stream
144: this is not how a File like object looks like... stream should be a function that returns a new web ReadableStream:

 ctrl.enqueue({ 
     name: pandoraBoxStream.path, 
     lastModified: new Date(0), 
     stream() {
        return mystream
     }, 
 }); 
@jimmywarting
Copy link
Author

jimmywarting commented Aug 1, 2020

If i understood all what your code is doing then maybe you can replace all of this:

const { pandoraBoxStream, done } = iterator.next()
if (done) return ctrl.close()
// do something with value
if (pandoraBoxStream.type === PandoraStreamType.PANDORA_LOCATION_TYPE_DIRECTORY){
if (pandoraBoxStream.path !== '/')
ctrl.enqueue({
name: pandoraBoxStream.path,
lastModified: new Date(0),
folder: true
});
} else if (pandoraBoxStream.type === PandoraStreamType.PANDORA_LOCATION_TYPE_STREAM){
const mystream = new stream.PassThrough();
const chunks = new Array(pandoraBoxStream.chunksCount).map( (it, index) => index );
ctrl.enqueue({
name: pandoraBoxStream.path,
lastModified: new Date(0),
stream: mystream,
});
async.each( chunks, ( chunkIndex, next2 )=>{
this.getLocationStreamChunk( pandoraBoxStream.absolutePath, chunkIndex, pandoraBoxStream.chunkSize, pandoraBoxStream.chunkRealSize(chunkIndex), (err, buffer) => {
if (err) return next(err);
if (stopped) return next(new Error('stopped'));
mystream.write(buffer);
next2();
});
}, (err, out ) => {
if (!err)
mystream.end();
});
}
}

...node stuff with just javascript api:s and avoid the need of having to convert/pipe a node stream to a web stream by just using web streams directly instead

const {chunksCount, absolutePath, chunkSize} = pandoraBoxStream
let i = 0

const mystream = new ReadableStream({
  start (ctrl) {
    // return a promise if you have to wait for something
    // or remove this altogether 
  },
  pull (ctrl) {
    return new Promise(resolve, reject => {
      this.getLocationStreamChunk(absolutePath, i, chunkSize, pandoraBoxStream.chunkRealSize(i), (err, buffer) => {
        if (err) return reject(err)
        if (stopped) return reject(new Error('stopped'))
        ctrl.enqueue(buffer)
        if (++i === chunksCount) ctrl.close() // done writing this file now
      })
    })
  },
  cancel() {
    // something cancel
    // user could have cancel from browser UI
    // You could remove this also...
  }
})

ctrl.enqueue({
    name: pandoraBoxStream.path,
    lastModified: new Date(0),
    stream () {
      // You could also move all of the above in here too.
      // (thought it created too much indentation)
      return mystream
    },
});

@ibudisteanu
Copy link
Contributor

ibudisteanu commented Aug 1, 2020

There is an array of pandoraBoxStreams.
Each PandoraBoxStream is made of multiple chunks, that's why I have that iterator
The code simply wants to create an zip including all pandora box streams (made of their chunks)

LE: you are right, I will integrate it now. Thanks for the support.

@jimmywarting
Copy link
Author

There is an array of pandoraBoxStreams.
Each PandoraBoxStream is made of multiple chunks, that's why I have that iterator

Maybe i didn't grasp all of what your code is doing then...

The code simply wants to create an zip including all pandora box streams (made of their chunks)

The above code will be executed for each pandoraBoxStreams if i did it correctly (but maybe not)

LE: you are right, I will integrate it now. Thanks for the support.

So i maybe was right :)
Anyhow going to bed now, good luck to you.

(updated the above code just slightly)

ibudisteanu added a commit that referenced this issue Aug 1, 2020
@ibudisteanu
Copy link
Contributor

ibudisteanu commented Aug 1, 2020

I have updated it, but I encounter the same error described last time.
ebf68d4

image

I wish you a great sleep! Thanks for the help. I will search furthermore the incompatibility for the .pipeThrough( new Writer() ) why is not working.

@ibudisteanu
Copy link
Contributor

After testing on Firefox v79, I found that it returns me a different error message.

image

@jimmywarting
Copy link
Author

you probably need to have the same polyfilled web streams as conflux have all over the place for now to make it work

npm i web-streams-polyfill@^2.1.0

import { WritableStream, ReadableStream, TransformStream } from 'web-streams-polyfill/ponyfill'
import streamSaver from 'streamsaver'
// change streamsaver WritableStream to be a polyfilled version instead
streamSaver.WritableStream = WritableStream

transcend-io/conflux#47 (comment)

@ibudisteanu
Copy link
Contributor

hi @jimmywarting ! Thank you so much for the support. Indeed it solves the above errors.

I was able to make the saveAs working with your help! I will try to see if it works with 2-3-4 gb streams.

@ibudisteanu
Copy link
Contributor

Feel free to close the issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants