Giter Site home page Giter Site logo

Add a one-shot method? about compression HOT 3 OPEN

ricea avatar ricea commented on August 16, 2024 10
Add a one-shot method?

from compression.

Comments (3)

wandyezj avatar wandyezj commented on August 16, 2024 1

It would be convenient to be able to do this in one shot.

That said, the current implementation is flexible.

This is my current usage to compress data for sharing in a URL hash for open checklist

/**
* converts text to gzip compressed hexText
* @param {string} text - a string
* @returns {Promise<string>} gzip compressed hexText
*/
async function compress(text) {
   // https://developer.mozilla.org/en-US/docs/Web/API/CompressionStream

   const readStream = new Response(text);
   const compressedReadableStream = readStream.body.pipeThrough(new CompressionStream("gzip"));
   const response = new Response(compressedReadableStream);

   const buffer = await response.arrayBuffer();
   const compressedHexText = convertBufferToHex(buffer);
   return compressedHexText;
}

from compression.

ricea avatar ricea commented on August 16, 2024 1

@wandyezj Thank you for your use case.

Using a Response to convert a stream to an array buffer is convenient, but inefficient. See https://wicg.github.io/compression/#example-deflate-compress for a faster way to do it.

from compression.

gtm-nayan avatar gtm-nayan commented on August 16, 2024 1

I'm using the Compression API to encode data for URL fragments, a one-shot method would be really nice to have compared to piping the text through multiple streams to get there.

from compression.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.