logo
down
shadow

Why is the zlib inflate function is not working in correct order in node.js?


Why is the zlib inflate function is not working in correct order in node.js?

By : user2956964
Date : November 22 2020, 03:03 PM
seems to work fine Use Async.js. Below are steps to make your code work synchronously using async.js.
I ran into similar problem and resolved it using the steps below. I have replaced my function with yours. It should work. You just give it a try, documentation is inline to make you understand the code.
code :
/*
 * need an array to run all queries one by one in a definite order
 */
var arr = [];

while (initPointer < totalLength) {
    console.log('Extracting '+rawLengthBuf.readUInt32LE(0));
    arr.push(dataBuf)
}


/*
 * Callback function for initiation of waterfall
 */
var queue = [
    function(callback) {
        // pass the ref array and run first query by passing starting index - 0
        callback(null, arr, 0)
    }
];

/*
 * Object to store result of all async operation
 */
var finalResult = {};

/*
 * Generic Callback function for every dynamic query
 */
var callbackFunc = function(prevModelData, currentIndex, callback) {
    //current file/data
    zlib.inflate(arr[currentIndex], function(err, data) {
        if (!err) {
            console.log('Extracted '+data.length);
        } else {
            // console.log(result)
            finalResult[arr[currentIndex]] = data
            //call next element in queue
            callback(null, data, currentIndex + 1)
        }
    });
}

/*
 * Add callback function for every dynamic query
 */

while (initPointer < totalLength) {
    ...
    console.log('Extracting '+rawLengthBuf.readUInt32LE(0));

    ...

    queue.push(callbackFunc)

}


/*
 * Run all dynamic queries one by one using async.js waterfall method
 */
async.waterfall(queue, function (err, result) {
    console.log('finish', finalResult)
});


Share : facebook icon twitter icon
zlib inflate method not working as expected

zlib inflate method not working as expected


By : Ceko
Date : March 29 2020, 07:55 AM
Hope that helps As per the zlib webpage:
Is there a function for inflate (zlib/miniz) which return upper bound of inflate/decompress size?

Is there a function for inflate (zlib/miniz) which return upper bound of inflate/decompress size?


By : user2733871
Date : March 29 2020, 07:55 AM
With these it helps Yes, but I don't think you will find it very useful. The upper limit is 1032 times the size of the input data.
Node: zlib.gunzip works, but not zlib.inflate?

Node: zlib.gunzip works, but not zlib.inflate?


By : user3297274
Date : March 29 2020, 07:55 AM
hope this fix your issue zlib.inflate decodes the zlib format, not the zip format. There are tools out there for the zip format.
Node zlib module inflate, and knowing how much data was read (total_in)?

Node zlib module inflate, and knowing how much data was read (total_in)?


By : HvdW
Date : March 29 2020, 07:55 AM
wish of those help Node didn't expose this feature before, so I submitted some pull requests to add it.
As of Node 8.1 this is now possible to access this information with the native zlib module. The Zlib engines now make a bytesRead property available, and the convenience methods can now access it by passing an info option.
code :
'use strict';

const zlib = require('zlib');

const str = 'abcdefgh'.repeat(2);
const data = new Uint8Array(zlib.deflateSync(str));
const extra = new Uint8Array([2, 4, 6, 8, 10]);

const dataExtra = new Uint8Array(data.length + extra.length);
dataExtra.set(data);
dataExtra.set(extra, data.length);

testSync();

function testSync() {
    const info = zlib.inflateSync(dataExtra, {info: true});
    console.log('Sync:');
    console.log(str, '==', info.buffer.toString());
    console.log(data.length, '==', info.engine.bytesRead);
    console.log();

    testAsync();
}

function testAsync() {
    zlib.inflate(dataExtra, {info: true}, (err, info) => {
        console.log('Async:');
        console.log(str, '==', info.buffer.toString());
        console.log(data.length, '==', info.engine.bytesRead);
        console.log();

        testStream();
    });
}

function testStream() {
    let buffer = new Buffer(0);

    const inf = new zlib.Inflate();
    inf.on('data', (d) => {
        buffer = Buffer.concat([buffer, d]);
    });
    inf.on('end', () => {
        console.log('Stream:');
        console.log(str, '==', buffer.toString());
        console.log(data.length, '==', inf.bytesRead);
        console.log();
    });
    inf.write(dataExtra);
    inf.end();
}
'use strict';

const zlib = require('zlib');

const str = 'abcdefgh'.repeat(2);
const data = new Uint8Array(zlib.deflateSync(str));
const extra = new Uint8Array([2, 4, 6, 8, 10]);

const dataExtra = new Uint8Array(data.length + extra.length);
dataExtra.set(data);
dataExtra.set(extra, data.length);

class Inflate extends zlib.Inflate {
    constructor(opts, mode) {
        super(opts, mode);

        if (!('bytesRead' in this)) {
            const self = this;
            this.bytesRead = 0;

            const handle = this._handle;
            const writeSync = handle.writeSync;
            handle.writeSync = function() {
                const availInBefore = arguments[3];
                const r = writeSync.apply(this, arguments);
                self.bytesRead += availInBefore - r[0];
                return r;
            };
            const write = handle.write;
            handle.write = function() {
                const availInBefore = arguments[3];
                const r = write.apply(this, arguments);
                let callback;
                const cb = function() {
                    self.bytesRead += availInBefore - arguments[0];
                    callback.apply(r, arguments);
                };
                Object.defineProperty(r, 'callback', {
                    configurable: true,
                    enumerable: true,
                    get: () => {
                        return cb;
                    },
                    set: (value) => {
                        callback = value;
                    }
                });
                return r;
            };
        }
    }
}

testSync();

function testSync() {
    const inf = new Inflate();
    const buffer = inf._processChunk(dataExtra, inf._finishFlushFlag);
    inf.end();
    console.log('Sync:');
    console.log(str, '==', buffer.toString());
    console.log(data.length, '==', inf.bytesRead);
    console.log();

    testAsync();
}

function testAsync() {
    let buffer = new Buffer(0);
    const inf = new Inflate();
    inf.on('data', (d) => {
        buffer = Buffer.concat([buffer, d]);
    });
    inf.on('end', () => {
        console.log('Async:');
        console.log(str, '==', buffer.toString());
        console.log(data.length, '==', inf.bytesRead);
        console.log();
    });
    inf.write(dataExtra);
    inf.end();
}
Node zlib incremental inflate

Node zlib incremental inflate


By : user2462259
Date : March 29 2020, 07:55 AM
like below fixes the issue It looks like it does, since the documentation lists zlib.constants.Z_STREAM_END as a possible return value.
Related Posts Related Posts :
  • How to get instant changes in background from private api in mobile app?
  • Grunt.js: Fire livereload as soon a files are modified, before task completes
  • How to remove port number from appear in the url for node js application?
  • How to ignore libraries in browserify programmatic api
  • Node/Express/Jade style guides?
  • how do I return data from a node.js request call? (undefined is not a function)
  • res.sendfile() doesn't serve javascripts well
  • File path completion relative to the current file directory
  • Tuning node-mongodb-native connection pool size
  • Express Socket.io example server not working
  • node js can't install right on my Ubuntu computer
  • How can i do an "or" query in a Model.find on Sails?
  • Confused in starting a project in node.js with npm install
  • unit testing express route with async callback
  • Mongoose sum operation in collection
  • document save function not working in mongoose
  • Nested if inside each (Handlebars template in Express app)
  • How does the cookie-session middleware work in expressjs?
  • grunt-processhtml remove not working when specifying a target
  • Error: CERT_HAS_EXPIRED in Node.js request module (macu vs facebook)
  • Async web requests are making 'socket hangup' in node.js
  • ExpressJS: Middleware control flow for Router.param() & Router.use()
  • Glob recursive/zero or more directories, excluding directories
  • node.js v0.11.14 as service with --harmony
  • Running npm install from Sublime Text 2 doesn't work
  • update on a subdoc with mongoose
  • NodeJS Aerospike UDF execute memory leak
  • Alternative to path module that always uses forward-slashes?
  • How do you stream a csv file into a node web app?
  • How to modularize routing with Node.js Express
  • NodeJS - Modularising SQLite3
  • MongoDB / Node: Incorrect Arguments
  • How to use Aggregate in mongoose
  • How to generate multiple reports with mocha?
  • AWS Lambda making video thumbnails
  • MEAN stack on Ubuntu 14.04 suddenly stopped working
  • node.js/express/mongoose noob issue
  • Nodejs ssh convert output byte stream to a sliceable string
  • different behavior of process.argv
  • How to expose the Application model's register method
  • How to install FFMPEG on Node/Meteor server?
  • How Can I Use vhost to Redirect a Sub-Domain to an ExpressJS Route?
  • Gmail API not respecting UTF encoding in subject
  • installing Node modules on Docker: why are they disappearing?
  • How do I set up Node/Express to act as a STUN server?
  • What is the Linear Execution Model in Meteor?
  • connect-redis and express-session results in req.session undefined
  • Application Error Heroku - Problems with Deployment
  • How to pass node v8 args and script args to pm2?
  • Can it be necessary to require a module (child_process in this case) more than once?
  • Too many bad command: outbound emails with Haraka
  • what does require("child_process") actually do?
  • How can I list all my Amazon EC2 instances using Node.js in AWS Lambda?
  • Passing param to custom middleware
  • How do I debug "Error: spawn ENOENT" on node.js?
  • Proper way to combine route and api auth using loopback.js
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org