Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

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

Open
wants to merge 4 commits into from

4 participants

@Rush

Also according to Isaac's tips I have added a test for the new behavior. test/simple/test-net-socket-set-encoding-revert.js

lib/net.js
@@ -430,6 +430,10 @@ function onread(buffer, offset, length) {
Socket.prototype.setEncoding = function(encoding) {
var StringDecoder = require('string_decoder').StringDecoder; // lazy load
+ if(!encoding) { // revert encoding if none provided
+ this._decoder = null;
+ return;
+ }
this._decoder = new StringDecoder(encoding);

Why not a one-liner?

this._decoder = encoding ? new StringDecoder(encoding) : null;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@Rush

Good idea, fixed.

lib/net.js
@@ -430,7 +430,7 @@ function onread(buffer, offset, length) {
Socket.prototype.setEncoding = function(encoding) {
var StringDecoder = require('string_decoder').StringDecoder; // lazy load
- this._decoder = new StringDecoder(encoding);
+ this._decoder = encoding ? new StringDecoder(encoding) : null; // revert encoding if none provided

I hate to be that guy but lines should wrap at 80 characters. :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@Rush

I appreciate it Ben. ;) Fixed 80 columns wrapping also for the test code.

@bnoordhuis

LGTM. An even better test would do two writes and check that the first is a string and the second one a buffer but the current test is good enough for me.

@isaacs You discussed this on IRC? Maybe you should take a look as well.

@koichik
Owner

@RushPL - Can you also fix http.IncomingMessage, fs.ReadStream, and stream's API doc?

@Nodejs-Jenkins

Can one of the admins verify this patch?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
3  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;
};
View
3  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;
};
View
42 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();
+ });
+});
Something went wrong with that request. Please try again.