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.

Support Brotli decompression

See original GitHub issue

Summary

This library doesn’t seem to support a br content type

Expected Behavior

When receiving a Brotli-compressed response, the library should be able to decompress it exactly the same way as a gzip response

Current Behavior

It doesn’t make any attempt to decode it.

Context

This is causing problems in higher level apps, i.e. Postman: postmanlabs/postman-app-support#4091

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:11
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
flyweightcommented, Jan 16, 2019

For those of you that are waiting on this, here is a temporary workaround (with fallback to gzip where brotli isn’t available from the server):

Required Modules

https://www.npmjs.com/package/iltorb https://www.npmjs.com/package/patch-package

Diff for patch-package

$ cat patches/request+2.87.0.patch 
patch-package
--- a/node_modules/request/request.js
+++ b/node_modules/request/request.js
@@ -379,8 +379,12 @@ Request.prototype.init = function (options) {
     )
   }
 
+  if (typeof self.brotliDecompress !== 'function') {
+    delete self.brotliDecompress
+  }
+
   if (self.gzip && !self.hasHeader('accept-encoding')) {
-    self.setHeader('accept-encoding', 'gzip, deflate')
+    self.setHeader('accept-encoding', `${self.brotliDecompress ? 'br, ' : ''}gzip, deflate`)
   }
 
   if (self.uri.auth && !self.hasHeader('authorization')) {
@@ -1027,7 +1031,10 @@ Request.prototype.onRequestResponse = function (response) {
         finishFlush: zlib.Z_SYNC_FLUSH
       }
 
-      if (contentEncoding === 'gzip') {
+      if (contentEncoding === 'br') {
+        responseContent = self.brotliDecompress()
+        response.pipe(responseContent)
+      } else if (contentEncoding === 'gzip') {
         responseContent = zlib.createGunzip(zlibOptions)
         response.pipe(responseContent)
       } else if (contentEncoding === 'deflate') {

Example Usage

const { decompressStream } = require('iltorb')
const request = require('request')

request({
  uri: 'https://www.google.com',
  gzip: true,
  brotliDecompress: decompressStream,
  headers: {
    'user-agent': 'curl/7.47.0'
  }
})
3reactions
stale[bot]commented, Dec 19, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Brotli-G: An open-source compression/decompression ...
Brotli can be used to compress web application assets such as fonts, javascript, images, and much more. Brotli decompression is supported by ...
Read more >
brotli/brotli.md at master · google/brotli - GitHub
brotli is a generic-purpose lossless compression algorithm that compresses data using a combination of a modern variant of the LZ77 algorithm, Huffman coding ......
Read more >
Boosting Site Speed Using Brotli Compression
If your browser supports Brotli (see more info below), we serve Brotli ... Its decompression speed is slightly faster than gzip and Zopfli....
Read more >
Brotli Compression: A Fast Alternative to GZIP ... - Kinsta
GZIP compression has been the standard for a long time, but not any more! This post will dissect Brotli compression.
Read more >
Introducing Support for Brotli Compression - .NET Blog
Brotli is a relatively new compression algorithm. It's quite beneficial for web clients, with decompression performance comparable to gzip while ...
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