title: Nodejs: convert string to buffer
body: <p>I'm trying to write a string to a socket (socket is called "response"). Here is the code I have sofar (I'm trying to implement a byte caching proxy...):</p>
  
  <pre><code>var http = require('http');
  var sys=require('sys');
  
  var localHash={};
  
  http.createServer(function(request, response) {
      var proxy = http.createClient(80, request.headers['host'])
      var proxy_request = proxy.request(request.method, request.url, request.headers);
      proxy_request.addListener('response', function (proxy_response) {
      proxy_response.addListener('data', function(x) {
          var responseData=x.toString();
          var f=50;
          var toTransmit="";
          var p=0;        
  
          var N=responseData.length;
          if(N&gt;f){
              p=Math.floor(N/f);
  
              var hash="";
              var chunk="";
              for(var i=0;i&lt;p;i++){
                  chunk=responseData.substr(f*i,f);
                  hash=DJBHash(chunk);
                  if(localHash[hash]==undefined){
                      localHash[hash]=chunk;
                      toTransmit=toTransmit+chunk;
                  }else{
                      sys.puts("***hit"+chunk);
                      toTransmit=toTransmit+chunk;//"***EOH"+hash;
                  }
              }
              //remainder:
              chunk=responseData.substr(f*p);
              hash=DJBHash(chunk);
              if(localHash[hash]==undefined){
                  localHash[hash]=chunk;
                  toTransmit=toTransmit+chunk;
              }else{
                  toTransmit=toTransmit+chunk;//"***EOH"+hash;
              }
          }else{
              toTransmit=responseData;
          }
          response.write(new Buffer(toTransmit));   /*error occurs here */
      });
      proxy_response.addListener('end', function() {
          response.end();
      });
      response.writeHead(proxy_response.statusCode, proxy_response.headers);
      });
      request.addListener('data', function(chunk) {
          sys.puts(chunk);
          proxy_request.write(chunk, 'binary');
      });
      request.addListener('end', function() {
          proxy_request.end();
      });
  }).listen(8080);
  
  
  
  function DJBHash(str) {
      var hash = 5381;
      for(var i = 0; i &lt; str.length; i++) {
          hash = (((hash &lt;&lt; 5) + hash) + str.charCodeAt(i)) &amp; 0xffffffff;
      }
      if(hash&lt;-1){
          hash=hash*-1;
      }
      return hash;
  }
  </code></pre>
  
  <p>The trouble is, I keep getting a "content encoding error" in Firefox. It's as if the gizipped content isn't being transmitted properly. I've ensured that "toTransmit" is the same as "x" via console.log(x) and console.log(toTransmit).</p>
  
  <p>It's worth noting that if I replace <code>response.write(new Buffer(toTransmit))</code> with simply <code>response.write(x)</code>, the proxy works as expected, but I need to do some payload analysis and then pass "toTransmit", not "x".</p>
  
  <p>I've also tried to <code>response.write(toTransmit)</code> (i.e. without the conversion to buffer) and I keep getting the same content encoding error.</p>
  
  <p>I'm really stuck. I thought I had this problem fixed by converting the string to a buffer as per another thread (http://stackoverflow.com/questions/7090510/nodejs-content-encoding-error), but I've re-opened a new thread to discuss this new problem I'm experiencing.</p>
  
  <p>I should add that if I open a page via the proxy in Opera, I get gobblydeegook - it's as if the gzipped data gets corrupted.</p>
  
  <p>Any insight greatly appreciated.</p>
  
  <p>Many thanks in advance,</p>