Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ node_js:
- "6"
- "8"
- "10"
- "11.1"
- "12"

script:
- npm run lint
Expand Down
26 changes: 23 additions & 3 deletions lib/handle.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,19 @@ var Buffer = require('buffer').Buffer
var Queue = require('./queue')

// Node.js version
var match = /^v(\d+)\.(\d+)\./.exec(process.version)
var version = match ? Number(match[1]) + Number('0.' + match[2]) : 11
var onreadMode = version >= 11.1 ? 2 : 1
var mode = 'modern'

var setNRead
if (onreadMode === 2) {
var sw = process.binding('stream_wrap')
setNRead = function (nread) {
sw.streamBaseState[sw.kReadBytesOrError] = nread
}
}

function Handle (stream, options) {
EventEmitter.call(this)

Expand All @@ -31,6 +42,15 @@ Handle.create = function create (stream, options) {
return new Handle(stream, options)
}

Handle.prototype._onread = function _onread (nread, buffer) {
if (onreadMode === 1) {
this.onread(nread, buffer)
} else {
setNRead(nread)
this.onread(buffer)
}
}

Handle.prototype._queueReq = function _queueReq (type, req) {
return this.pending.append(type, req)
}
Expand Down Expand Up @@ -81,17 +101,17 @@ var uv = process.binding('uv')
Handle.prototype._flow = function flow () {
var self = this
this._stream.on('data', function (chunk) {
self.onread(chunk.length, chunk)
self._onread(chunk.length, chunk)
})

this._stream.on('end', function () {
self.onread(uv.UV_EOF, Buffer.alloc(0))
self._onread(uv.UV_EOF, Buffer.alloc(0))
})

this._stream.on('close', function () {
setImmediate(function () {
if (self._reading) {
self.onread(uv.UV_ECONNRESET, Buffer.alloc(0))
self._onread(uv.UV_ECONNRESET, Buffer.alloc(0))
}
})
})
Expand Down