h2non/toxy

Having issues with Node 4.5.0 after updating from 0.12.5

SPSpwetter opened this issue · 9 comments

I'm using toxy to inject problems talking to the backend APIs into my single page web application.

So far so good, I had a working toxy proxy.

But when trying to bootstrap the design team with the proxy, they downloaded the latest node, which then caused the proxy to hang. I know its getting to transformRequestBody, because I see the log messages, but the browser thinks all the requests are pending until they time out.

Here's my config:

var toxy = require('toxy');
var express = require('express');
var poisons = toxy.poisons;
var rules = toxy.rules;

// Create a new toxy proxy
var proxy = toxy();
//var Array = require('Array');

var host = process.env.HOST || 'uicommon-lab-sc.lab.skyportsystems.com'


var app = express(),
port = process.env.PORT || 8080;

var cspheader = "default-src 'self'; script-src 'self' www.google-analytics.com" +
                " ajax.googleapis.com ; style-src 'unsafe-inline' 'self' data:; " +
                "img-src 'self' data:; media-src 'none'; font-src 'self' data:; " +
                "connect-src 'self'; report-uri https://" +
                host+"/api/v0/csp";

var cspheadernames=["Content-Security-Policy", "X-Content-Security-Policy", "X-WebKit-CSP"];

function setCSPHeaders(res, path, stat)
{
    for (var ix=0; ix < cspheadernames.length; ix++)
    {
        res.append(cspheadernames[ix],cspheader);
    }

}

function processRequest(req,res,next) {
        console.log(">>>>"+req.url);

        next();
}

function processResponse(req,res,next) {
        console.log("<<<<<"+req.url);
    var keys = Object.keys(res._headers),
        len = keys.length,
        prop,
        value;
        for (i=0; i < len;i++) 
        {
            prop = keys[i];
            value = res._headers[prop];
            //console.log("replacing",prop,value);
            if (Array.isArray(value))
            {
                for (jx=0; jx < value.length; jx++)
                {
                    v2 = value[jx];
                    value[jx] = v2.replace(/Secure/g,'');
                }
            }
            else
            {
                value.replace(/Secure/g,'');
                res._headers[prop]=value;
            }
        }

        next()
}

// Default server to forward incoming traffic
proxy
  .forward('https://'+host)

var roots = ['/api','/templates','/cdn_images','/session'];

for (var ix=0;ix< roots.length;ix++)
{
var root = roots[ix]+'/*';
    console.log('proxying',root);
    proxy
      .all(root)
      .withRule(rules.method(['POST', 'PUT', 'DELETE','GET']))
      .options({ secure: false })
    .transformRequest(processRequest)
    .transformResponse(processResponse)
      .host(host)
}



app.use(express.static(__dirname + '/webroot', {setHeaders: setCSPHeaders}));
app.use(proxy.middleware())
app.listen(port);


console.log('Server listening on port:', port)
console.log('Test it:', 'http://localhost:'+port+'/html/home.html')
h2non commented

Thanks for reporting. I know about the issue with latest node.js versions, but I simply didn't had time to Investigate and fix It. The issue exists in rocky, the underline library used in toxy.

Can you give me a hint on how to debug/fix it?

You might want to update the text that says it doesn’t work with Node 6+ to
say Node 4+.

Pierce Wetter
Sent with Airmail

From: Tomás Aparicio notifications@github.com notifications@github.com
Reply: h2non/toxy
reply@reply.github.com
reply@reply.github.com
Date: August 26, 2016 at 3:42:36 PM
To: h2non/toxy toxy@noreply.github.com toxy@noreply.github.com
Cc: SPSpwetter pwetter@skyportsystems.com pwetter@skyportsystems.com,
Author author@noreply.github.com author@noreply.github.com
Subject: Re: [h2non/toxy] Having issues with Node 4.5.0 after updating
from 0.12.5 (#47)

Thanks for reporting. I know about the issue with latest node.js versions,
but I simply didn't had time to Investigate and fix It. The issue exists in
rocky, the underline library used in toxy.


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
#47 (comment), or mute
the thread
https://github.com/notifications/unsubscribe-auth/AMJbrg8s1MKJ5d7xJOuran_fYgkeaNakks5qj2bHgaJpZM4JuZDc
.

h2non commented

Basically, the HTTP incoming stream data should be intercepted and cached, and once finished it should be re-emitted. I'm thinking about using a third-party module to do this, or create a specific one.

There was something in a rocky issue about calling "uncork"? Because I see the response, but it doesn't seem to be getting sent. So I think it's an API change with node about the end call.

Sent from my iPad

On Aug 27, 2016, at 9:28 AM, Tomás Aparicio notifications@github.com wrote:

Basically, the HTTP incoming stream data should be intercepted and cached, and once finished it should be re-emitted. I'm thinking about using a third-party module to do this, or create a specific one.


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or mute the thread.

h2non commented

You're probably right, that was my subspect as well, but I did not invest time yet digging into it. Hopefully, next week I should have some free time to fix it.

Any progress on this? I'd like to upgrade our node...

h2non commented

Not yet, I'm sorry. rocky should be fixed first before moving with toxy. I don't have too much time lately, so if you can provide some code/PR, that would accelerate the process.

Ok. I've confirmed that adding res.connection.uncork() to just before next() above ends up fixing the problem I was having. I'm not quite sure where that should go in the rocky source though.

h2non commented

This should be fixed in toxy@v0.3.13. Apologies for the delay. Closing.