Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: node 16 compatibility #2201

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/continuous-integration.yaml
Expand Up @@ -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]
1 change: 1 addition & 0 deletions lib/socket.js
Expand Up @@ -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) {
Expand Down
46 changes: 46 additions & 0 deletions tests/test_socket.js
Expand Up @@ -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 => {
Expand Down Expand Up @@ -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)
}
}