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

Support adding files with iterators as well #346

Open
jimmywarting opened this issue Aug 25, 2016 · 6 comments
Open

Support adding files with iterators as well #346

jimmywarting opened this issue Aug 25, 2016 · 6 comments
Labels

Comments

@jimmywarting
Copy link
Contributor

jimmywarting commented Aug 25, 2016

// in the zip core somewhere (simplified a lot)
function spawn(generator) {
    var iterator = generator();

    function iterate() {
        var {value, done} = iterator.next()
        !done && Promise.resolve(value).then(chunk => {
            // write chunk to entry
            iterate()
        })
    }()
}
function* generator() {
    yield chunk1
    // simulate a http range request for example
    yield new Promise(resolve => setTimeout(()=>resolve(chunk2), 1000))
    yield chunk3
    yield chunk4
}

zip.file('file.txt', generator)

so the supported data could be:
String/ArrayBuffer/Uint8Array/Buffer/Blob/Promise/Nodejs stream/generator & iterator

@jimmywarting
Copy link
Contributor Author

jimmywarting commented Aug 26, 2016

Another thing why generators would be good is if you used Abstract Chunk Store

  • Lets you implement your own "async-chunk-getter"

@jimmywarting
Copy link
Contributor Author

jimmywarting commented Sep 2, 2016

Anything simpler just as a function that returns a promise that resolves the hole file would help reduce the memory as for how it is now. Right now node buffer is the only thing that can request more data when it needs it

Gods have for lazy loading something

// now function can return either a promise or simply `new Blob`
if(typeof data == 'function')
  Promise.resolve(data()).then(data => do something)

@dduponchel
Copy link
Collaborator

a function that returns a promise that resolves the hole file

I'm working on an other fix to reduce the memory usage but this one will definitively follow !

@jimmywarting
Copy link
Contributor Author

Any progress?

@dduponchel
Copy link
Collaborator

Any progress?

Not yet.

@jimmywarting
Copy link
Contributor Author

realized a while back that nodes streams are iterable. and web streams are going to be iterable too some day. That will mean you can remove all node/browser specifics hacks and just use the iterable api

if (!ReadableStream.prototype[Symbol.asyncIterator]) {
  ReadableStream.prototype[Symbol.asyncIterator] = async function* () {
    const reader = this.getReader()
    while (1) {
      const chunk = await reader.read()
      if (chunk.done) return chunk.value
      yield chunk.value
    }
  }
}

res = await fetch('something')
for await (chunk of res) {
  // {value: uint8array, done: boolean}
}

(part of my cross-js guideline for writing cross platform codes)

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

No branches or pull requests

2 participants