Git Product home page Git Product logo

Comments (7)

devongovett avatar devongovett commented on September 7, 2024 1

Hmm, try making a Uint8Array before making a buffer?

new buffer.Buffer(new Uint8Array(xhr.response))

from browserify-zlib.

devongovett avatar devongovett commented on September 7, 2024

You shouldn't convert the file data to utf8 before unzipping it. That will mess with the compressed data. Try the following (I removed line 5 of your snippet). Weird that this works in Node. Could be a difference in buffer implementations between browserify and node or something else. Let me know if this works.

zlib = require('zlib')
fs = require('fs')

data = fs.readFileSync('data.json.gz')

zlib.gunzip(data, function(err, res){
    console.log(err, res.toString("utf8"))
})

from browserify-zlib.

devongovett avatar devongovett commented on September 7, 2024

Oh maybe I misunderstood. Was decompress.js only used in Node, not the browser?

from browserify-zlib.

mobiusklein avatar mobiusklein commented on September 7, 2024

decompress.js was only used in Node, where it worked. The logic for decompressing in the browser is in index.html itself.

$(function(){
    console.log("Send request")
    $.ajax({url: "/data.json.gz"})
    .success(function(data, status, req){
            console.log(status)
            window.COMPRESSED_DATA = data
            window.BUFFERED_COMPRESSED_DATA = new buffer.Buffer(data)
            console.log(data)
            window.DECOMPRESSED_DATA = zlib.gunzipSync(BUFFERED_COMPRESSED_DATA)
        }).error(function(error, message, splat){
            console.log(arguments)
        })
})

I had no encoding parameter to new buffer.Buffer(...), but there is no change when I do include 'base64'.

from browserify-zlib.

devongovett avatar devongovett commented on September 7, 2024

Ah. jQuery's ajax function loads the data as text, which is not what you want. You need to set the ajax responseType to 'arraybuffer' so you can get binary data out. Try this:

var xhr = new XMLHttpRequest();

xhr.onload = function() {
  var data = new buffer.Buffer(xhr.response);
  var decompressed = zlib.gunzipSync(data);
};

xhr.open('GET', '/data.json.gz', true);
xhr.responseType = 'arraybuffer';
xhr.send();

from browserify-zlib.

mobiusklein avatar mobiusklein commented on September 7, 2024

Thank you for the suggestion. I've never dealt with this part of Ajax before. I tried what you suggested, but got a different error this time.

errno: -5, 
code: "Z_BUF_ERROR", 
message: "buffer error: "

Error: buffer error: 
    at Zlib._binding.onerror (http://localhost:9090/main.js:6359:17)
    at Zlib._error (http://localhost:9090/main.js:5987:8)
    at Zlib._write (http://localhost:9090/main.js:5945:10)
    at Zlib.writeSync (http://localhost:9090/main.js:5894:15)
    at Gunzip.Zlib._processChunk (http://localhost:9090/main.js:6515:31)
    at zlibBufferSync (http://localhost:9090/main.js:6233:17)
    at Object.exports.gunzipSync (http://localhost:9090/main.js:6177:10)
    at XMLHttpRequest.xhr.onload (http://localhost:9090/index.html:16:38) 

xhr.response is of type ArrayBuffer, but it is empty. Its length is set to 34 though, which is the length of the gzipped file.

from browserify-zlib.

mobiusklein avatar mobiusklein commented on September 7, 2024

That did the trick. The console.log() value of the Buffer and the Uint8Array were the same, but these are areas of JavaScript where I haven't developed a good understanding of the APIs.

Now that I've done this consistently, I need to find out if I can embed the compressed data in an HTML file somehow.

If you care about Stack Overflow badges, I asked this question over three months ago there, http://stackoverflow.com/questions/24898650/zlib-browserify-throws-data-error-incorrect-header-check, this would be good case for Revival.

from browserify-zlib.

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.