From 02685501d10e83ca631200f5eaad35c50722c433 Mon Sep 17 00:00:00 2001 From: vincent-tr Date: Fri, 28 May 2021 10:19:05 -0700 Subject: [PATCH 1/3] test: node 16 compatibility --- tests/test_socket.js | 46 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/tests/test_socket.js b/tests/test_socket.js index 6f110cac8..d455ddbdc 100644 --- a/tests/test_socket.js +++ b/tests/test_socket.js @@ -3,6 +3,7 @@ const { expect } = require('chai') const http = require('http') const https = require('https') +const { Readable } = require('stream') const nock = require('..') it('should expose TLSSocket attributes for HTTPS requests', done => { @@ -50,3 +51,48 @@ describe('`Socket#setTimeout()`', () => { }) }) }) + +describe('`Socket#destroy()`', () => { + it('can destroy the socket if stream is not finished', async () => { + const scope = nock('http://example.test') + + scope.intercept('/somepath', 'GET').reply(() => { + const buffer = Buffer.allocUnsafe(10000000) + const data = new MemoryReadableStream(buffer, { highWaterMark: 128 }) + return [200, data] + }) + + const req = http.get('http://example.test/somepath') + const stream = await new Promise(resolve => req.on('response', resolve)) + + // close after first chunk of data + stream.on('data', () => stream.destroy()) + + await new Promise((resolve, reject) => { + stream.on('error', reject) + stream.on('close', resolve) + stream.on('end', resolve) + }) + }) +}) + +class MemoryReadableStream extends Readable { + constructor(content) { + super() + this._content = content + this._currentOffset = 0 + } + + _read(size) { + if (this._currentOffset >= this._content.length) { + this.push(null) + return + } + + const nextOffset = this._currentOffset + size + const content = this._content.slice(this._currentOffset, nextOffset) + this._currentOffset = nextOffset + + this.push(content) + } +} From 1e67f7b4007eda8fbe9f16fdda65c91330b33b3e Mon Sep 17 00:00:00 2001 From: Gregor Martynus <39992+gr2m@users.noreply.github.com> Date: Fri, 28 May 2021 10:19:29 -0700 Subject: [PATCH 2/3] ci(test): node 16 --- .github/workflows/continuous-integration.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/continuous-integration.yaml b/.github/workflows/continuous-integration.yaml index b95622ff3..b97046cff 100644 --- a/.github/workflows/continuous-integration.yaml +++ b/.github/workflows/continuous-integration.yaml @@ -153,5 +153,5 @@ jobs: strategy: fail-fast: false matrix: - node-version: [10, 12, 14] + node-version: [10, 12, 14, 16] os: [macos-latest, ubuntu-latest, windows-latest] From d0c2a3659bd06b3a33d2a87ef8d892137b0bd76d Mon Sep 17 00:00:00 2001 From: vincent-tr Date: Fri, 28 May 2021 10:22:17 -0700 Subject: [PATCH 3/3] fix: node 16 compatibility --- lib/socket.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/socket.js b/lib/socket.js index 05eda2b84..816102d68 100644 --- a/lib/socket.js +++ b/lib/socket.js @@ -92,6 +92,7 @@ module.exports = class Socket extends EventEmitter { debug('socket destroy') this.destroyed = true this.readable = this.writable = false + this.readableEnded = this.writableFinished = true process.nextTick(() => { if (err) {