forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JSVM Based middleware working for both PRE and POST hooks. TODO: JS E…
…vent handler
- Loading branch information
Martin Buhr
committed
Jan 14, 2015
1 parent
7f7c34c
commit c2a85b9
Showing
11 changed files
with
321 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,5 @@ apps/test.json | |
logs | ||
lint_results.txt | ||
build/ | ||
test_apps/ | ||
tyk.conf.bak |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// ----- Tyk Middleware JS definition: this should be in the global context ----- | ||
|
||
var TykJS = { | ||
TykMiddleware: { | ||
MiddlewareComponentMeta: function(configuration) { | ||
this.configuration = configuration; | ||
} | ||
} | ||
}; | ||
|
||
TykJS.TykMiddleware.MiddlewareComponentMeta.prototype.ProcessRequest = function(request, session) { | ||
log("Process Request Not Implemented"); | ||
return request; | ||
}; | ||
|
||
TykJS.TykMiddleware.MiddlewareComponentMeta.prototype.DoProcessRequest = function(request, session) { | ||
var processed_request = this.ProcessRequest(request, session); | ||
|
||
if (!processed_request) { | ||
log("Middleware didn't return request object!"); | ||
return; | ||
} | ||
|
||
// Reset the headers object | ||
processed_request.Request.Headers = {} | ||
|
||
return JSON.stringify(processed_request) | ||
}; | ||
|
||
// The user-level middleware component | ||
TykJS.TykMiddleware.NewMiddleware = function(configuration) { | ||
TykJS.TykMiddleware.MiddlewareComponentMeta.call(this, configuration); | ||
}; | ||
|
||
// Set up object inheritance | ||
TykJS.TykMiddleware.NewMiddleware.prototype = Object.create(TykJS.TykMiddleware.MiddlewareComponentMeta.prototype); | ||
TykJS.TykMiddleware.NewMiddleware.prototype.constructor = TykJS.TykMiddleware.NewMiddleware; | ||
|
||
TykJS.TykMiddleware.NewMiddleware.prototype.NewProcessRequest = function(callback) { | ||
this.ProcessRequest = callback; | ||
}; | ||
|
||
TykJS.TykMiddleware.NewMiddleware.prototype.ReturnData = function(request, session) { | ||
return {Request: request, SessionMeta: session} | ||
}; | ||
|
||
// ---- End middleware implementation for global context ---- | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// ---- Sample middleware creation by end-user ----- | ||
var sampleMiddleware = new TykJS.TykMiddleware.NewMiddleware({}); | ||
|
||
sampleMiddleware.NewProcessRequest(function(request, session) { | ||
// You can log to Tyk console output by calloing the built-in log() function: | ||
log("Running sample 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" | ||
|
||
// If you have multiple middlewares that need to communicate, set or read keys in the session object. | ||
// This will only work in a postprocessing MW | ||
if (session.meta_data) { | ||
session.meta_data["MiddlewareDataString"] = "SomeValue"; | ||
} | ||
|
||
// You MUST return both the request and session metadata | ||
return sampleMiddleware.ReturnData(request, session.meta_data); | ||
}); |
Oops, something went wrong.