Open In App

Node.js zlib.brotliDecompress() Method

Last Updated : 12 Oct, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report
The zlib.brotliDecompress() method is an inbuilt application programming interface of the Zlib module which is used to decompresses a chunk of data with BrotliCompress. Syntax:
zlib.brotliDecompress( buffer, options, callback )
Parameters: This method accepts three parameters as mentioned above and described below:
  • buffer: It can be of type Buffer, TypedArray, DataView, ArrayBuffer, and string.
  • options: It is an optional parameter that holds the zlib options.
  • callback: It holds the callback function.
Return Value: It returns the chunk of data after decompression. Below examples illustrate the use of zlib.brotliDecompress() method in Node.js: Example 1: javascript
// Node.js program to demonstrate the     
// brotliDecompress() method

// Including zlib module
const zlib = require("zlib");

// Declaring input and assigning
// it a value string
var input = "Computer Science";

// Calling brotliCompress method
zlib.brotliCompress(input, (err, buffer) => {

    // Calling brotliDecompress
    zlib.brotliDecompress(buffer, (err, buffer) => {
        console.log(buffer.toString('base64'));
    });
});
Output:
Q29tcHV0ZXIgU2NpZW5jZQ==
Example 2: javascript
// Node.js program to demonstrate the     
// brotliDecompress() method

// Including zlib module
const zlib = require("zlib");

// Declaring input and assigning
// it a value string
var input = "Computer Science";

// Calling brotliCompress method
zlib.brotliCompress(input, (err, buffer) => {

    // Calling brotliDecompress
    zlib.brotliDecompress(buffer, (err, buffer) => {
        console.log(buffer.toString('bas64'));
    });
});
Output:
buffer.js:631  throw new ERR_UNKNOWN_ENCODING(encoding);  ^

TypeError [ERR_UNKNOWN_ENCODING]: Unknown encoding: bas64
    at stringSlice (buffer.js:631:9)    at Buffer.toString (buffer.js:667:10)
    at BrotliDecompress.zlib.brotliDecompress [as cb] 
    (/home/runner/BeautifulMiserlySourcecode/index.js:18:28)
    at BrotliDecompress.zlibBufferOnEnd (zlib.js:133:10)
    at BrotliDecompress.emit (events.js:203:15)
    at BrotliDecompress.EventEmitter.emit (domain.js:448:20)
    at endReadableNT (_stream_readable.js:1143:12)
    at process._tickCallback (internal/process/next_tick.js:63:19)?
Here, an error occurs while encoding so, an error is thrown. Reference: https://nodejs.org/api/zlib.html#zlib_zlib_brotlidecompress_buffer_options_callback

Next Article

Similar Reads