PLhery / node-twitter-api-v2

Strongly typed, full-featured, light, versatile yet powerful Twitter API v1.1 and v2 client for Node.js.
https://www.npmjs.com/package/twitter-api-v2
Apache License 2.0
1.25k stars 175 forks source link

[bug] TypeError: Cannot read properties of undefined (reading 'Z_SYNC_FLUSH') #457

Open Eversmile12 opened 1 year ago

Eversmile12 commented 1 year ago

Describe the bug Context:

Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'Z_SYNC_FLUSH') at RequestHandlerHelper.getResponseDataStream (request-handler.helper.js:99:39) at RequestHandlerHelper.classicResponseHandler (request-handler.helper.js:196:33) at A.emit (https.js:1:3775) at A._connect (https.js:1:81698) at https.js:1:80279

at request-handler.helper.js there's the getResponseDataStream() that looks like its converting response from the tw API into gzip getResponseDataStream(res) { if (this.isCompressionDisabled()) { return res; } const contentEncoding = (res.headers['content-encoding'] || 'identity').trim().toLowerCase(); if (contentEncoding === 'br') { const brotli = zlib.createBrotliDecompress({ flush: zlib.constants.BROTLI_OPERATION_FLUSH, finishFlush: zlib.constants.BROTLI_OPERATION_FLUSH, }); res.pipe(brotli); return brotli; } if (contentEncoding === 'gzip') { const gunzip = zlib.createGunzip({ flush: zlib.constants.Z_SYNC_FLUSH, finishFlush: zlib.constants.Z_SYNC_FLUSH, }); res.pipe(gunzip); return gunzip; } if (contentEncoding === 'deflate') { const inflate = zlib.createInflate({ flush: zlib.constants.Z_SYNC_FLUSH <----- **ERROR**, finishFlush: zlib.constants.Z_SYNC_FLUSH <----- **ERROR**, }); res.pipe(inflate); return inflate; } return res; }

Looks like the zlib Z_SYNC_FLUSH constant isn't there. 

I can see the tweet coming through the request.

To Reproduce Please indicate all steps that lead to this bug:

  1. Create a new application using Plasmo - plasmo.com
  2. In a content script send a message to a background page
  3. Fetch the Tweet in the background page

Or clone the following repo and:

You'll see the error logged in the background worker console (accessible through chrome://extensions) https://github.com/Eversmile12/lenshare

Expected behavior It should fetch the tweet

Version

Eversmile12 commented 1 year ago

Ah, here's the catch!

Actually a couple.

1 - Where is z_sync_flush?

z_sync_flush, disappeared, couldn't find it in the latest version of zlib - setting both the "flush" and "finishFlush" properties to "2" - value of the z_sync_flush will solve the issue.

2- Looks like Twitter is sending back a response with a content-encoding header set to "gzip" whereas the payload is actually plain, simple JSON.

The library is seeing the content-encoding header and tries to inflate it, finding the wrong content header. Solution - try to catch and revert to sending back only the res if the header is wrong.

Plus, there are a bunch of import types that weren't specified, hope you appreciate me adding them! - linted the code.

Sending pr now, two separate for both.