I have a client-side JS aapplication where I want to use Web Streams to process some input file from the user. The file could either be a text file (in which case I just pipe it through a TextDecoderStream and then do whatever I want with it) or a gzipped text file (in which case I need to pipe it through a DecompressionStream before it gets to the TextDecoderStream).
What’s the best way to handle this?
I could make the user manually click a checkbox for compressed or not, but that’s poor UI.
I could look at the filename of the uploaded file, but that may not always be accurate.
I guess ideally I’d write some DecompressionStreamIfNecessary transform stream that I could just pipe through and it would decompress the stream if necessary or do nothing otherwise. idk if that’s possible though.
What’s the best way to do this?
2
Answers
I came up with this which seems to work, but the API is kind of ugly, it would be nicer if this was just a TransformStream I could pipe through. I would accept an answer from someone who is able to do that!
Simply try to decompress, and if that fails, process it as text. The decompressor will likely give up in the first byte or two, since a text file won’t have a gzip header.