Socket's setEncoding(null) removes Stream encoder. #3643

Closed
wants to merge 4 commits into
from
View
@@ -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;
};
View
@@ -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;
};
@@ -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();
+ });
+});