fix(teeny-request): avoid MaxListenersExceededWarning in stream mode - #9254
fix(teeny-request): avoid MaxListenersExceededWarning in stream mode#9254re-taro wants to merge 1 commit into
Conversation
node-fetch v3 attaches several internal pipeline listeners to the response body stream. Combined with teeny-request's own error forwarding and pipeline wiring plus downstream consumers such as @google-cloud/storage, the default limit of 10 listeners is legitimately exceeded, emitting two MaxListenersExceededWarning messages on every streamed download. This change removes the listener limit on the fetch response body stream, which is internal to teeny-request and short-lived. Fixes googleapis#9185
There was a problem hiding this comment.
Code Review
This pull request addresses an issue where node-fetch v3's internal pipeline listeners exceed the default limit of 10, causing warnings on streamed responses. It resolves this by setting the maximum listeners of the response stream to 0 and adds a corresponding test. The review feedback suggests defensively checking if responseStream is defined and if setMaxListeners is a function before calling it to prevent potential TypeError runtime crashes.
| // legitimately exceed the default limit of 10, warning on every | ||
| // streamed response | ||
| // see: https://github.com/googleapis/google-cloud-node/issues/9185 | ||
| responseStream.setMaxListeners(0); |
There was a problem hiding this comment.
Calling setMaxListeners directly on responseStream can throw a TypeError if responseStream is null/undefined, or if it is a non-Node stream (such as a Web ReadableStream in isomorphic/browser environments or certain mock streams) that does not implement setMaxListeners.
To prevent potential runtime crashes, we should defensively check if responseStream is defined and if setMaxListeners is a function before calling it.
if (responseStream && typeof responseStream.setMaxListeners === 'function') {
responseStream.setMaxListeners(0);
}
node-fetch v3 attaches several internal pipeline listeners to the
response body stream. Combined with teeny-request's own error forwarding
and pipeline wiring plus downstream consumers such as
@google-cloud/storage, the default limit of 10 listeners is legitimately
exceeded, emitting two MaxListenersExceededWarning messages on every
streamed download. This change removes the listener limit on the fetch
response body stream, which is internal to teeny-request and
short-lived.
Fixes #9185