Skip to content

Commit

Permalink
fix: socket variable testing for undefined (#1726)
Browse files Browse the repository at this point in the history
* fix: socket variable testing for undefined
Avoid issue where socket event was not triggered and local socket vraible is not defined.
This issue is reproducible only using deno.

* chore: made socket test capture simpler
Only controls the execution of the section that uses socket.

* Update src/index.js

Co-authored-by: Linus Unnebäck <linus@folkdatorn.se>

* Update index.js

---------

Co-authored-by: Linus Unnebäck <linus@folkdatorn.se>
Co-authored-by: Jimmy Wärting <jimmy@warting.se>
  • Loading branch information
3 people committed Jun 29, 2023
1 parent afb36f6 commit 8bc3a7c
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/index.js
Expand Up @@ -362,8 +362,11 @@ function fixResponseChunkedTransferBadEnding(request, errorCallback) {
const {headers} = response;
if (headers['transfer-encoding'] === 'chunked' && !headers['content-length']) {
response.once('close', hadError => {
// if a data listener is still present we didn't end cleanly
const hasDataListener = socket.listenerCount('data') > 0;
// tests for socket presence, as in some situations the
// the 'socket' event is not triggered for the request
// (happens in deno), avoids `TypeError`
// if a data listener is still present we didn't end cleanly
const hasDataListener = socket && socket.listenerCount('data') > 0;

if (hasDataListener && !hadError) {
const err = new Error('Premature close');
Expand Down

0 comments on commit 8bc3a7c

Please sign in to comment.