diff --git a/src/index.js b/src/index.js index 9013f1de2..3f6f5f004 100644 --- a/src/index.js +++ b/src/index.js @@ -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');