handle-thing
Advanced tools
Comparing version 1.0.1 to 1.1.0
@@ -0,3 +1,5 @@ | ||
var assert = require('assert'); | ||
var util = require('util'); | ||
var EventEmitter = require('events').EventEmitter; | ||
var Buffer = require('buffer').Buffer; | ||
@@ -11,2 +13,4 @@ | ||
function Handle(stream, options) { | ||
EventEmitter.call(this); | ||
this._stream = stream; | ||
@@ -36,2 +40,3 @@ this._flowing = false; | ||
} | ||
util.inherits(Handle, EventEmitter); | ||
module.exports = Handle; | ||
@@ -43,9 +48,22 @@ | ||
Handle.prototype.setStream = function setStream(stream) { | ||
assert(this._stream === null, 'Can\'t set stream two times'); | ||
this._stream = stream; | ||
this.emit('_stream'); | ||
}; | ||
Handle.prototype.readStart = function readStart() { | ||
if (!this._stream) { | ||
this.once('_stream', this.readStart); | ||
return 0; | ||
} | ||
if (!this._flowing) { | ||
this._flowing = true; | ||
this._flow(); | ||
} | ||
this._stream.resume(); | ||
if (this._flowing) | ||
return; | ||
this._flowing = true; | ||
this._flow(); | ||
return 0; | ||
}; | ||
@@ -89,3 +107,2 @@ | ||
this._stream.on('end', function() { | ||
debugger; | ||
var errno = global.errno; | ||
@@ -101,3 +118,8 @@ global.errno = 'EOF'; | ||
Handle.prototype.readStop = function readStop() { | ||
if (!this._stream) { | ||
this.once('_stream', this.readStop); | ||
return 0; | ||
} | ||
this._stream.pause(); | ||
return 0; | ||
}; | ||
@@ -107,2 +129,9 @@ | ||
Handle.prototype.shutdown = function shutdown(req) { | ||
if (!this._stream) { | ||
this.once('_stream', function() { | ||
this.shutdown(req); | ||
}); | ||
return 0; | ||
} | ||
var self = this; | ||
@@ -112,6 +141,16 @@ this._stream.end(function() { | ||
}); | ||
return 0; | ||
}; | ||
} else { | ||
Handle.prototype.shutdown = function shutdown(req) { | ||
var req = {}; | ||
if (!req) | ||
req = {}; | ||
if (!this._stream) { | ||
this.once('_stream', function() { | ||
this.shutdown(req); | ||
}); | ||
return req; | ||
} | ||
var self = this; | ||
@@ -127,10 +166,22 @@ this._stream.end(function() { | ||
Handle.prototype.close = function close(callback) { | ||
if (!this._stream) { | ||
this.once('_stream', function() { | ||
this.close(callback); | ||
}); | ||
return 0; | ||
} | ||
if (this._options.close) | ||
return this._options.close(callback); | ||
process.nextTick(callback); | ||
this._options.close(callback); | ||
else | ||
process.nextTick(callback); | ||
return 0; | ||
}; | ||
} else { | ||
Handle.prototype.close = function close() { | ||
if (this._options.close) | ||
return this._options.close(function() {}); | ||
if (!this._stream) | ||
this.once('_stream', this.close); | ||
else if (this._options.close) | ||
this._options.close(function() {}); | ||
return 0; | ||
}; | ||
@@ -141,13 +192,33 @@ } | ||
Handle.prototype.writeEnc = function writeEnc(req, data, enc) { | ||
if (!this._stream) { | ||
this.once('_stream', function() { | ||
this.writeEnc(req, data, enc); | ||
}); | ||
return 0; | ||
} | ||
var self = this; | ||
req.async = true; | ||
req.bytes = data.length; | ||
this._stream.write(data, enc, function() { | ||
req.oncomplete(0, self, req); | ||
}); | ||
return 0; | ||
}; | ||
} else { | ||
Handle.prototype.writeEnc = function writeEnc(data, ignored, enc) { | ||
Handle.prototype.writeEnc = function writeEnc(data, ignored, enc, req) { | ||
if (!req) | ||
req = { bytes: buffer.length }; | ||
if (!this._stream) { | ||
this.once('_stream', function() { | ||
this.writeEnc(data, ignored, enc, req); | ||
}); | ||
return req; | ||
} | ||
var self = this; | ||
var buffer = new Buffer(data, enc); | ||
var req = { bytes: buffer.length }; | ||
this._stream.write(buffer, function() { | ||
@@ -154,0 +225,0 @@ req.oncomplete(0, self, req); |
{ | ||
"name": "handle-thing", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Wrap Streams2 instance into a HandleWrap", | ||
@@ -5,0 +5,0 @@ "main": "lib/handle.js", |
@@ -12,65 +12,80 @@ var assert = require('assert'); | ||
[ 'normal', 'lazy' ].forEach(function(mode) { | ||
describe(mode, function() { | ||
beforeEach(function() { | ||
pair = streamPair.create(); | ||
handle = thing.create(mode === 'normal' ? pair.other : null); | ||
socket = new net.Socket({ handle: handle }); | ||
beforeEach(function() { | ||
pair = streamPair.create(); | ||
handle = thing.create(pair.other); | ||
socket = new net.Socket({ handle: handle }); | ||
if (mode === 'lazy') { | ||
setTimeout(function() { | ||
handle.setStream(pair.other); | ||
}, 50); | ||
} | ||
// For v0.8 | ||
socket.readable = true; | ||
socket.writable = true; | ||
}); | ||
// For v0.8 | ||
socket.readable = true; | ||
socket.writable = true; | ||
}); | ||
it('should write data to Socket', function(done) { | ||
pair.write('hello'); | ||
pair.write(' world'); | ||
pair.end('... ok'); | ||
afterEach(function() { | ||
assert(handle._stream); | ||
}); | ||
var chunks = ''; | ||
socket.on('data', function(chunk) { | ||
chunks += chunk; | ||
}); | ||
socket.on('end', function() { | ||
assert.equal(chunks, 'hello world... ok'); | ||
it('should write data to Socket', function(done) { | ||
pair.write('hello'); | ||
pair.write(' world'); | ||
pair.end('... ok'); | ||
// allowHalfOpen is `false`, so the `end` should be followed by `close` | ||
socket.once('close', function() { | ||
done(); | ||
var chunks = ''; | ||
socket.on('data', function(chunk) { | ||
chunks += chunk; | ||
}); | ||
socket.on('end', function() { | ||
assert.equal(chunks, 'hello world... ok'); | ||
// allowHalfOpen is `false`, so the `end` should be followed by `close` | ||
socket.once('close', function() { | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
it('should read data from Socket', function(done) { | ||
socket.write('hello'); | ||
socket.write(' world'); | ||
socket.end('... ok'); | ||
it('should read data from Socket', function(done) { | ||
socket.write('hello'); | ||
socket.write(' world'); | ||
socket.end('... ok'); | ||
var chunks = ''; | ||
pair.on('data', function(chunk) { | ||
chunks += chunk; | ||
}); | ||
pair.on('end', function() { | ||
assert.equal(chunks, 'hello world... ok'); | ||
var chunks = ''; | ||
pair.on('data', function(chunk) { | ||
chunks += chunk; | ||
}); | ||
pair.on('end', function() { | ||
assert.equal(chunks, 'hello world... ok'); | ||
done(); | ||
}); | ||
}); | ||
done(); | ||
}); | ||
}); | ||
it('should invoke `close` callback', function(done) { | ||
handle._options.close = function(callback) { | ||
done(); | ||
process.nextTick(callback); | ||
}; | ||
it('should invoke `close` callback', function(done) { | ||
handle._options.close = function(callback) { | ||
done(); | ||
process.nextTick(callback); | ||
}; | ||
pair.end('hello'); | ||
socket.resume(); | ||
}); | ||
pair.end('hello'); | ||
socket.resume(); | ||
}); | ||
it('should invoke `getPeerName` callback', function() { | ||
handle._options.getPeerName = function() { | ||
return { address: 'ohai' }; | ||
}; | ||
if (mode === 'normal') { | ||
it('should invoke `getPeerName` callback', function() { | ||
handle._options.getPeerName = function() { | ||
return { address: 'ohai' }; | ||
}; | ||
assert.equal(socket.remoteAddress, 'ohai'); | ||
assert.equal(socket.remoteAddress, 'ohai'); | ||
}); | ||
} | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10358
300