Skip to content

Commit af05502

Browse files
authored
feat: node 16 compatibility (#2199)
1 parent 3efd738 commit af05502

File tree

4 files changed

+54
-2
lines changed

4 files changed

+54
-2
lines changed

.github/workflows/continuous-integration.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,5 +153,5 @@ jobs:
153153
strategy:
154154
fail-fast: false
155155
matrix:
156-
node-version: [10, 12, 14]
156+
node-version: [10, 12, 14, 16]
157157
os: [macos-latest, ubuntu-latest, windows-latest]

lib/socket.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ module.exports = class Socket extends EventEmitter {
9292
debug('socket destroy')
9393
this.destroyed = true
9494
this.readable = this.writable = false
95+
this.readableEnded = this.writableFinished = true
9596

9697
process.nextTick(() => {
9798
if (err) {

tests/test_common.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
const http = require('http')
1515
const { expect } = require('chai')
1616
const sinon = require('sinon')
17+
const semver = require('semver')
1718
const nock = require('..')
1819

1920
const common = require('../lib/common')
@@ -472,8 +473,12 @@ it('`percentEncode()` encodes extra reserved characters', () => {
472473

473474
describe('`normalizeClientRequestArgs()`', () => {
474475
it('should throw for invalid URL', () => {
476+
// See https://github.com/nodejs/node/pull/38614 release in node v16.2.0
477+
const isNewErrorText = semver.gte(process.versions.node, '16.2.0')
478+
const errorText = isNewErrorText ? 'Invalid URL' : 'example.test'
479+
475480
// no schema
476-
expect(() => http.get('example.test')).to.throw(TypeError, 'example.test')
481+
expect(() => http.get('example.test')).to.throw(TypeError, errorText)
477482
})
478483

479484
it('can include auth info', async () => {

tests/test_socket.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const { expect } = require('chai')
44
const http = require('http')
55
const https = require('https')
6+
const { Readable } = require('stream')
67
const nock = require('..')
78

89
it('should expose TLSSocket attributes for HTTPS requests', done => {
@@ -50,3 +51,48 @@ describe('`Socket#setTimeout()`', () => {
5051
})
5152
})
5253
})
54+
55+
describe('`Socket#destroy()`', () => {
56+
it('can destroy the socket if stream is not finished', async () => {
57+
const scope = nock('http://example.test')
58+
59+
scope.intercept('/somepath', 'GET').reply(() => {
60+
const buffer = Buffer.allocUnsafe(10000000)
61+
const data = new MemoryReadableStream(buffer, { highWaterMark: 128 })
62+
return [200, data]
63+
})
64+
65+
const req = http.get('http://example.test/somepath')
66+
const stream = await new Promise(resolve => req.on('response', resolve))
67+
68+
// close after first chunk of data
69+
stream.on('data', () => stream.destroy())
70+
71+
await new Promise((resolve, reject) => {
72+
stream.on('error', reject)
73+
stream.on('close', resolve)
74+
stream.on('end', resolve)
75+
})
76+
})
77+
})
78+
79+
class MemoryReadableStream extends Readable {
80+
constructor(content) {
81+
super()
82+
this._content = content
83+
this._currentOffset = 0
84+
}
85+
86+
_read(size) {
87+
if (this._currentOffset >= this._content.length) {
88+
this.push(null)
89+
return
90+
}
91+
92+
const nextOffset = this._currentOffset + size
93+
const content = this._content.slice(this._currentOffset, nextOffset)
94+
this._currentOffset = nextOffset
95+
96+
this.push(content)
97+
}
98+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy