{"id":1459,"date":"2022-08-30T15:16:42","date_gmt":"2022-08-30T15:16:42","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/19\/wrapping-nodejss-net-createserver-into-a-producer-of-customised-sockets-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:16:42","modified_gmt":"2022-08-30T15:16:42","slug":"wrapping-nodejss-net-createserver-into-a-producer-of-customised-sockets-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/wrapping-nodejss-net-createserver-into-a-producer-of-customised-sockets-collection-of-common-programming-errors\/","title":{"rendered":"Wrapping NodeJS&#39;s net.createServer into a producer of customised sockets-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m trying to make a function which behaves like <code>net.createServer<\/code>, except that it returns objects which are wrappers around sockets, instead of sockets.<\/p>\n<p>(The point of these will be to do automatic data conversion.)<\/p>\n<p>My latest attempt looks like:<\/p>\n<pre><code>var net = require('net');\nvar util = require('util');\nvar stream = require('stream');\nvar EventEmitter = process.EventEmitter;\n\nexports.createServer = createServer;\n\nfunction createServer(arg0, arg1) {\n  var server;\n  var netServer;\n  var ocb;\n\n  if (typeof arg1 === 'function') {\n    \/\/ options, callback\n    netServer = net.createServer(arg0, callback);\n    ocb = arg1;\n  } else if (typeof arg0 === 'function') {\n    \/\/ callback\n    netServer = net.createServer(callback);\n    ocb = arg0;\n  } else {\n    \/\/ options?\n    netServer = net.createServer(arg0);\n    ocb = null;\n  }\n\n  server = new Server(netServer);\n\n  function callback(socket) {\n    ocb(new LiftedSocket(socket));\n  };\n\n  return server;\n  ;\n}\n\nfunction Server(netServer) {\n  this.netServer = netServer;\n}\nutil.inherits(Server, EventEmitter);\n\nServer.prototype.listen = function() {\n  this.netServer.listen.apply(this.netServer, arguments);\n}\n\nfunction LiftedSocket(socket) {\n  stream.Duplex(this);\n  this.socket = socket;\n}\nutil.inherits(LiftedSocket, stream.Duplex);\n\nLiftedSocket.prototype._read = function(size) {\n  console.log('_read', size);\n  \/\/ transforming code goes here\n  this.socket.read(size);\n}\nLiftedSocket.prototype._write = function(chunk, encoding, callback) {\n  console.log('_write', chunk, encoding, callback);\n  \/\/ transforming code goes here\n  this.socket.write(chunk, callback);\n}\n<\/code><\/pre>\n<p>But when tested (by trying to pipe from a returned <code>LiftedSocket<\/code>) it fails with errors including:<\/p>\n<pre><code> Uncaught TypeError: Cannot read property 'pipesCount' of undefined\n  at LiftedSocket.Readable.pipe (_stream_readable.js:453:16)\n\n Uncaught TypeError: Cannot read property 'flowing' of undefined\n  at LiftedSocket.Readable.on (_stream_readable.js:691:44)\n<\/code><\/pre>\n<p>I expect I&#8217;m doing something wrong when constructing the <code>LiftedSocket<\/code>, but I can&#8217;t think what.<\/p>\n<ol>\n<li>\n<p>I had called the superclass constructor incorrectly. It should have been:<\/p>\n<pre><code>stream.Duplex.call(this, {})\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-19 13:18:00. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I&#8217;m trying to make a function which behaves like net.createServer, except that it returns objects which are wrappers around sockets, instead of sockets. (The point of these will be to do automatic data conversion.) My latest attempt looks like: var net = require(&#8216;net&#8217;); var util = require(&#8216;util&#8217;); var stream = require(&#8216;stream&#8217;); var EventEmitter = process.EventEmitter; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1459","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1459","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1459"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1459\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1459"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1459"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1459"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}