Hello, I’m using k8s tyk solution and testing JS middleware. I found the sample JS middleware in gateway/middleware folder.
// ---- Sample middleware creation by end-user -----
var samplePreProcessMiddleware = new TykJS.TykMiddleware.NewMiddleware({});
samplePreProcessMiddleware.NewProcessRequest(function(request, session) {
// You can log to Tyk console output by calloing the built-in log() function:
log("Running sample PRE PROCESSOR JSVM middleware")
// Set and Delete headers in an outbound request
request.SetHeaders["User-Agent"] = "Tyk-Custom-JSVM-Middleware";
//request.DeleteHeaders.push("Authorization");
// Change the outbound URL Path (only fragment, domain is fixed)
// request.URL = "/get";
// Add or delete request parmeters, these are encoded for the request as needed.
request.AddParams["test_param"] = "My Teapot";
request.DeleteParams.push("delete_me");
// Override the body:
request.Body = "New Request body"
// You MUST return both the request and session metadata
return samplePreProcessMiddleware.ReturnData(request, {});
});
// Ensure init with a post-declaration log message
So I add this JS middleware to my API definition like this.
This API is just calling “http://httpbin.org”.
Of course, I set “enable_jsvm”: true in tyk.conf.
"custom_middleware": {
"pre": [
{
"name": "samplePreProcessMiddleware",
"path": "middleware/samplePreProcessMiddleware.js",
"require_session": false,
"raw_body_only": false
}
],
"post": [],
But when I call my API using postman, it shows “Error: socket hang up”.
This is tyk-dash-gateway pod’s error.
2022/04/26 06:47:35 http: panic serving 172.17.0.1:11311: runtime error: invalid memory address or nil pointer dereference
goroutine 72638 [running]:
net/http.(*conn).serve.func1(0xc000899360)
/usr/local/go/src/net/http/server.go:1769 +0x139
panic(0x1dcb060, 0x3664fd0)
/usr/local/go/src/runtime/panic.go:522 +0x1b5
vendor/github.com/robertkrimen/otto.(*Otto).Copy(...)
/go/src/vendor/github.com/robertkrimen/otto/otto.go:644
github.com/TykTechnologies/tyk/gateway.(*DynamicMiddleware).ProcessRequest(0xc0008eb940, 0x2667f20, 0xc00087a8c0, 0xc000782b00, 0x0, 0x0, 0x0, 0x0, 0x0)
/go/src/github.com/TykTechnologies/tyk/gateway/mw_js_plugin.go:157 +0xaf6
github.com/TykTechnologies/tyk/gateway.TraceMiddleware.ProcessRequest(0x2682a80, 0xc0008eb940, 0x2667f20, 0xc00087a8c0, 0xc000782b00, 0x0, 0x0, 0x0, 0x0, 0x0)
/go/src/github.com/TykTechnologies/tyk/gateway/middleware.go:73 +0x3fd
github.com/TykTechnologies/tyk/gateway.createMiddleware.func1.1(0x2667f20, 0xc00087a8c0, 0xc000782b00)
/go/src/github.com/TykTechnologies/tyk/gateway/middleware.go:137 +0x528
net/http.HandlerFunc.ServeHTTP(0xc0008ebcc0, 0x2667f20, 0xc00087a8c0, 0xc000782b00)
/usr/local/go/src/net/http/server.go:1995 +0x44
vendor/github.com/gorilla/mux.(*Router).ServeHTTP(0xc000108540, 0x2667f20, 0xc00087a8c0, 0xc000782900)
/go/src/vendor/github.com/gorilla/mux/mux.go:212 +0xe3
github.com/TykTechnologies/tyk/gateway.(*handleWrapper).ServeHTTP(0xc0001f09e8, 0x2667f20, 0xc00087a8c0, 0xc000782900)
/go/src/github.com/TykTechnologies/tyk/gateway/proxy_muxer.go:52 +0x16f
net/http.serverHandler.ServeHTTP(0xc0007b1ad0, 0x2667f20, 0xc00087a8c0, 0xc000782900)
/usr/local/go/src/net/http/server.go:2774 +0xa8
net/http.(*conn).serve(0xc000899360, 0x266d660, 0xc000817400)
/usr/local/go/src/net/http/server.go:1878 +0x851
created by net/http.(*Server).Serve
/usr/local/go/src/net/http/server.go:2884 +0x2f4
I think error came from this part. (tyk/gateway/mw_js_plugin.go:157).
My API dosen’t have complex setting and just uses Bearer Authentication.
requestData := MiniRequestObject{
Headers: headers,
SetHeaders: map[string]string{},
DeleteHeaders: []string{},
Body: originalBody,
URL: r.URL.String(),
Params: r.URL.Query(),
AddParams: map[string]string{},
ExtendedParams: map[string][]string{},
DeleteParams: []string{},
Method: r.Method,
RequestURI: r.RequestURI,
Scheme: scheme,
}
requestAsJson, err := json.Marshal(requestData)
if err != nil {
**logger.WithError(err).Error("Failed to encode request object for dynamic middleware")**
return nil, http.StatusOK
}
Did I miss something ?