diff --git a/lib/net.js b/lib/net.js index 9f03259d96a..ee56fecd019 100644 --- a/lib/net.js +++ b/lib/net.js @@ -430,7 +430,8 @@ function onread(buffer, offset, length) { Socket.prototype.setEncoding = function(encoding) { var StringDecoder = require('string_decoder').StringDecoder; // lazy load - this._decoder = new StringDecoder(encoding); + // set encoding or revert encoding if none provided + this._decoder = encoding ? new StringDecoder(encoding) : null; }; diff --git a/lib/tls.js b/lib/tls.js index e396b592e80..8afd0d444e4 100644 --- a/lib/tls.js +++ b/lib/tls.js @@ -183,7 +183,8 @@ CryptoStream.prototype.setKeepAlive = function(enable, initialDelay) { CryptoStream.prototype.setEncoding = function(encoding) { var StringDecoder = require('string_decoder').StringDecoder; // lazy load - this._decoder = new StringDecoder(encoding); + // set encoding or revert encoding if none provided + this._decoder = encoding ? new StringDecoder(encoding) : null; }; diff --git a/test/simple/test-net-socket-set-encoding-revert.js b/test/simple/test-net-socket-set-encoding-revert.js new file mode 100644 index 00000000000..2573cfef610 --- /dev/null +++ b/test/simple/test-net-socket-set-encoding-revert.js @@ -0,0 +1,42 @@ +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. + +var util = require('util'); +var net = require('net'); +var assert = require('assert'); + +var server = net.createServer(function(c) { + c.write('test'); +}).listen(9999); +var socket = net.connect(9999); +socket.setEncoding('utf8'); + +// below should set encoding back to binary format +socket.setEncoding(null); + +socket.on('connect', function() { + socket.on('data', function(data) { + assert.ok(typeof data === 'object', + 'Encoding expected to be disabled after "socket.setEncoding(null)"'); + socket.end(); + server.close(); + }); +});