Skip to content

Commit

Permalink
- fixed: spdy-http2#6
Browse files Browse the repository at this point in the history
- changed: wip version
  • Loading branch information
Jens Weintraut committed Jan 15, 2020
1 parent 6faac39 commit 319a384
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
38 changes: 29 additions & 9 deletions lib/handle.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,21 @@ 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 = /^v0\.8\./.test(process.version) ? 'rusty' :
/^v0\.(9|10)\./.test(process.version) ? 'old' :
'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 Down Expand Up @@ -61,6 +72,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 @@ -113,17 +133,17 @@ if (mode === 'modern') {
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, new Buffer(0));
self._onread(uv.UV_EOF, new Buffer(0));
});

this._stream.on('close', function() {
setImmediate(function() {
if (self._reading)
self.onread(uv.UV_ECONNRESET, new Buffer(0));
self._onread(uv.UV_ECONNRESET, new Buffer(0));
});
});
};
Expand All @@ -145,13 +165,13 @@ if (mode === 'modern') {
Handle.prototype._flow = function flow() {
var self = this;
this._stream.on('data', function(chunk) {
self.onread(chunk, 0, chunk.length);
self._onread(chunk, 0, chunk.length);
});

this._stream.on('end', function() {
var errno = process._errno;
process._errno = 'EOF';
self.onread(null, 0, 0);
self._onread(null, 0, 0);
if (process._errno === 'EOF')
process._errno = errno;
});
Expand All @@ -163,7 +183,7 @@ if (mode === 'modern') {

var errno = process._errno;
process._errno = 'ECONNRESET';
self.onread(null, 0, 0);
self._onread(null, 0, 0);
if (process._errno === 'ECONNRESET')
process._errno = errno;
});
Expand Down Expand Up @@ -195,7 +215,7 @@ if (mode === 'modern') {

var errno = global.errno;
global.errno = 'ECONNRESET';
self.onread(null, 0, 0);
self._onread(null, 0, 0);
if (global.errno === 'ECONNRESET')
global.errno = errno;
});
Expand All @@ -205,13 +225,13 @@ if (mode === 'modern') {
Handle.prototype._flow = function flow() {
var self = this;
this._stream.on('data', function(chunk) {
self.onread(chunk, 0, chunk.length);
self._onread(chunk, 0, chunk.length);
});

this._stream.on('end', function() {
var errno = global.errno;
global.errno = 'EOF';
self.onread(null, 0, 0);
self._onread(null, 0, 0);
if (global.errno === 'EOF')
global.errno = errno;
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "handle-thing",
"version": "1.2.5",
"version": "1.2.5-i6-spdy3",
"description": "Wrap Streams2 instance into a HandleWrap",
"main": "lib/handle.js",
"scripts": {
Expand Down

0 comments on commit 319a384

Please sign in to comment.