skip to Main Content

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


  1. Chosen as BEST ANSWER

    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!

    // Check first 2 bytes of stream for gzip header
    const isStreamGzipped = async (stream: ReadableStream) => {
        const reader = stream.getReader();
        const { value } = await reader.read();
        reader.cancel();
    
        if (value !== undefined && value.length >= 2) {
            return value[0] === 0x1f && value[1] === 0x8b;
        }
    
        return false;
    }
    
    const decompressStreamIfNecessary = async (inputStream: ReadableStream) => {
        const [checkGzipStream, outputStream] = inputStream.tee();
    
        if (await isStreamGzipped(checkGzipStream)) {
            return outputStream.pipeThrough(
                new DecompressionStream("gzip")
            );
        }
    
        return outputStream;
    };
    
    (await decompressStreamIfNecessary(stream))
        .pipeThrough(new TextDecoderStream())
        .pipeTo(whatever);
    
    

  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search