question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Please provide (minimal) client-side examples

See original GitHub issue

In my case, I want to unzip some gzipped strings. What are the minimum steps I need to do this client side / browser?

I don’t understand which files I need. I would expect justdist/pako_deflate.min.js but it requires files not in the dist directory.

Please provide a minimal example. Cooler would be examples for decode too, in the README.MD, but that’s just thinking about other people who stumble across this interesting module - not for me.

ps I am using requirejs if that makes things easier.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:27 (9 by maintainers)

github_iconTop GitHub Comments

6reactions
Redsandrocommented, Mar 26, 2014

@ixti I’ve already read the Examples and read the API you mention. Where do you think I’ve been those two hours before reopening? Clearly, they aren’t helping me.

It’s typical how certain developers often want to share their code, but not their knowledge by systematically refusing to symphatize with less seasoned developers who run into a (small) problem that can easily be avoided by a simple® example.

They rather see a thousand users waste half a day, than improve documentation with a simple usecase. Every developer knows other developers are clever enough to interpolate from there to the darkest depths of the API.

I’m guessing it has something to do with the fact that they once had to waste time themselves, and they don’t want others to get off easy. Either that, or something about maintaining the status-quo as it’s benefacting the size of their e-penis which is directly proportional to their technological superiority.

Sorry, I let myself go a bit. This is the difference between a friendly project (like socket.io and node.js and Linux Mint) and a hostile project (like Filezilla and Debian), and I just hate to see promising projects be hostile. I don’t understand what drives people to go there.


Anyway, pako itself is very sweet and powerful, so let me give my fellow devvers some friendly pointers on it’s usage, so they don’t have to spend half a day:

Newbie-mistakes

  1. pako needs an uInt8 array. They might seem the same as normal Array or ArrayBuffer from other frameworks or uInt16Array JavaScript uses internally or String[], because they are all the same numbers, but it’s not the same.
  2. Do not prepare the uInt8 array server side, because when sent over it’s just an Array. Convert it back to uInt8 client-side.
  3. de-flate sounds like de-crypt, but for decrypting/unzipping you actually need in-flate 😄

Example

As a friendly example for people who come across pako and - like me - aren’t uptodate on the assumptions and limitations of Uint8Array (the docs and examples and API are useless in that regard - that’s why I recommended something like this in the first place), let me underline that the uInt bytearray needs to be constructed from an array. Not a string, not a bytestring. Here’s a little working example to get you started.

Consider your server sends you a bytearray for a gzipped string. Here’s how to decompress:

// Get some gzipped binary data from the server in array form (string of numbers, representing characters, representing bytes)
var charData    = [31,139,8,0,0,0,0,0,0,3,5,193,219,13,0,16,16,4,192,86,214,151,102,52,33,110,35,66,108,226,60,218,55,147,164,238,24,173,19,143,241,18,85,27,58,203,57,46,29,25,198,34,163,193,247,106,179,134,15,50,167,173,148,48,0,0,0];

// Convert character data to binary data. The characters are byte-representations anyway so they are all 8 bit.
var binData     = new Uint8Array(charData);

// gunzip gzipped binary data
var data        = pako.inflate(binData);

// Convert gunzipped binary data to ascii string
var strData     = String.fromCharCode.apply(null, new Uint16Array(data));

console.log(strData);

Sending arrays from the server isn’t optimal. Of course, Accept-Encoding may tackle this by gzipping the array, but gzipping an array representation of a gzipped string isn’t the sweetest thing anyway. Better have the server send base64 encoded binary data.

// Get some base64 encoded binary data from the server.
var b64Data     = 'H4sIAAAAAAAAAwXB2w0AEBAEwFbWl2Y0IW4jQmziPNo3k6TuGK0Tj/ESVRs6yzkuHRnGIqPB92qzhg8yp62UMAAAAA==';

// Decode base64 (convert ascii to binary)
var strData     = atob(b64Data);

// Convert binary string to character-number array
var charData    = strData.split('').map(function(x){return x.charCodeAt(0);});

var binData     = new Uint8Array(charData);
var data        = pako.inflate(binData);
var strData     = String.fromCharCode.apply(null, new Uint16Array(data));

console.log(strData);

From here you can work with the API docs.

I hope this will bring some friendly and accessibility back to pako because it’s obviously an awesome library. 😸


PS - Converting your data to an uint8 array in node using new Uint8Array(bson).buffer; before sending it over to the client, will freeze your Chromium browser tab when you execute pako.deflate(data);. Don’t know what’s going on, but it feels like something should be thrown in stead.

3reactions
Redsandrocommented, Mar 27, 2014

@creationix thanks for the nice reply.

As I understand it, pako doesn’t really want to be affiliated with the type of usage I employ it for, nor is the team particularly interested in a more basic/novice addition to its documentation.

I am inclined not to waste anyone else’s time on this, including my own. 😄

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to create a Minimal, Reproducible Example - Help Center
Don't sacrifice clarity for brevity when creating a minimal example. Use consistent naming and indentation, and include code comments if needed. Use your...
Read more >
Introduction to client-side frameworks - Learn web development
Objective: To understand how client-side JavaScript frameworks came to exist, ... Popular examples include Wordpress, Joomla, and Drupal.
Read more >
10 Client-side Storage Options and When to Use Them
Storing and manipulating data in the browser — also known as client-side storage — is useful when it's not necessary or practical to...
Read more >
Minimal client-side CRUD example (add, edit, delet buttons)
Does anyone know of a minimal example including buttons to Add empty row Edit row Delete row ? ... Can you please send...
Read more >
Client-side Scripting Techniques for WCAG 2.0 - W3C
This example is limited to client-side scripting, and should be backed up with server-side validation. The example is limited to the creation of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found