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

Can't get to read entries data: [object Object] (Ponyfill issue) #63

Closed
Toilal opened this issue Jul 31, 2020 · 4 comments · Fixed by #66
Closed

Can't get to read entries data: [object Object] (Ponyfill issue) #63

Toilal opened this issue Jul 31, 2020 · 4 comments · Fixed by #66

Comments

@Toilal
Copy link

Toilal commented Jul 31, 2020

First of all, thanks for this initiative, as it's my last hope to make some Zip file import work on low end devices after failing badly with JSZip.

So I try to switch to this library, as it seems to be the only one focused on low memory footprint. Generation of the zip file works properly with streamsaver. But I can't get to read entries content from the generated zip file.

In my use case, zip file contains a manifest.json entry with some JSON data in it. Content is ok as it can be read with 7-Zip.

When I try to read this manifest file, I get [object Object] as text instead of the actual JSON content.

import { Reader, Writer } from '@transcend-io/conflux'

let manifest = null

// importFile is a [File](https://developer.mozilla.org/fr/docs/Web/API/File), coming from an HTML5 File Input.
for await (const entry of Reader(importFile)) {
      if (entry.name === 'manifest.json') {
        console.log(entry)

        const text: string = await entry.text()
        console.log(text) // >>> ''[object Object]" instead of actual JSON data.

        manifest = JSON.parse(text) // Of course, this fails badly :(
        console.log(manifest)
        break
      }
 }

// Then I could do something with the manifest ...

It also displays the following error in console

TypeError: Failed to execute 'pipeThrough' on 'ReadableStream': parameter 1's 'writable' property is not a WritableStream.

Thrown from this line

let stream = this._fileLike.slice(start, end).stream();

It seems .stream() on _fileLike returns a native ReadableStream that is not compatible with the web-stream ponyfill used by this file.

For streamsaver on writing part, I have told the library to use the ponyfill.

import { TransformStream, WritableStream } from 'web-streams-polyfill/ponyfill'
import StreamSaver from 'streamsaver'

StreamSaver.TransformStream = TransformStream as any
StreamSaver.WritableStream = WritableStream as any

Is there a way to make .stream() call on _fileLike return the ponyfill ReadableStream ? I'd be happy to open a pull request, but I don't know this Stream API and I can't find how to convert a File/Blob to the ponyfill ReadableStream without invoking this native stream() method.

@Toilal
Copy link
Author

Toilal commented Jul 31, 2020

I have found a workaround by actually removing TransformStream ponyfill import from reader.js. It then use the native TransformerStream and things just works as expected, at least when Streams are available in browser.

@jimmywarting
Copy link
Contributor

jimmywarting commented Jul 31, 2020

If you want to be supportive give this issue a 👍 and maybe even ask if you can help out in some way. I would really like to see proper polyfill using native streams. I don't feel comfortable with TypeScript.

It's problematic to work with both polyfilled and native streams at the same time... 😞

@bencmbrook
Copy link
Member

This should be resolved by #66 (orig: #53)

@Toilal
Copy link
Author

Toilal commented Aug 3, 2020

Thank you ! Packaging is really better now, this fix this issue and another issue I had when running firefox.

Usage of optional chaining makes the library a bit more difficult to use with TypeScript, as the javascript dependency must be preprocessed by Babel for TypeScript to load the Javascript dependency. For now, it would be simpler for Typescript users to avoid this es feature.

 in ./node_modules/@transcend-io/conflux/src/write.js

Module parse failed: Unexpected token (156:32)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
| const ts =
|   globalThis.TransformStream ||
>   globalThis.WebStreamsPolyfill?.TransformStream ||
|   PonyfillTransformStream;
|

With Vue CLI, this preprocessing can ben enabled in vue.config.js

...
transpileDependencies: ['@transcend-io/conflux'],
...

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

Successfully merging a pull request may close this issue.

3 participants