(Solved) Basic JSVM middleware always ending in Go panic

I am trying to write a JSVM middleware to handle a custom auth system we have in place. However, every time I call the endpoint that runs the script, I get a “Failed to decode middleware request data on return from VM: invalid character ‘u’ looking for beginning of value” panic.

I know there is issue #959 about providing better error messages when failing to unmarshal an undefined JSON response, but it fails even if I have nothing but the return statement in there. Same with the sample scripts that come with the Docker release of Tyk (I am using tykio/tyk-gateway:latest).

My code looks like this:

var plugin = new TykJS.TykMiddleware.NewMiddleware({});

plugin.NewProcessRequest(function(request) {
	log("Request headers:");
	log(JSON.stringify(request.Headers));

	return plugin.ReturnData(request, {});
});

log("Test middleware initialised");

Hi Cristian,

The issue is still relevant - any error from javascript, such as a run-time error, will show that resulting “invalid character ‘u’…” error. It’s a bit silly, hence the open issue.

I would suggest taking a look at the sample javascript middlewares and using them exactly as they’re presented. That should work. Then, incrementally do the changes that you need as long as it keeps working. That should help you see what is the change that is breaking it.

I’m afraid there isn’t a better way to debug javascript code at the moment. We’ll look into that open issue to improve things as soon as we can.

Hi Daniel,

Thanks for the reply.

I understand that debugging JS middleware is sketchy at the moment but shouldn’t the very simple code I pasted above work, though? It just logs stuff and returns.

Also, as I already stated, the samples fail with the same message as well?

It indeed seems to load fine here. Are you sure you’re setting up the middleware name properly? The gateway needs to know the name of the middleware, in this case “plugin”. If you don’t use the right name, it will simply error, like:

ReferenceError: ‘wrongPluginName’ is not defined

(this is the error that we don’t surface at the moment)

While playing with this, I noticed that the fix is rather trivial, so here it is: jsvm: don't ignore otto's Run errors by mvdan · Pull Request #1074 · TykTechnologies/tyk · GitHub

If you have a working Go environment, you could build that branch and try again - now Tyk should give you helpful error messages. Otherwise, it will be merged sometime soon and shipped with 2.4.

I am running a Tyk Pro instance and loading the middleware via FS, using an API ID. It seems to load fine, as I am seeing the “Test middleware initialised” message in the console.

I also tried running that code with just the return statement and it would still fail.

I’ll try building that branch after the pull request is merged and see if I get a more meaningful message.

If you’re loading the middleware via FS, then the filename must match the name in the code - so if you declare “var plugin”, it has to be “plugin.js”. Is that the case?

Indeed, that was not the case. That middleware is running fine now.

I missed that line in the docs. Thank for pointing it out. Maybe it should be bolded or something :slight_smile: