New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More

ee-soa-transport-rewrite

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ee-soa-transport-rewrite - npm Package Compare versions

Comparing version

to
0.1.2

@@ -22,4 +22,3 @@ "use strict";

var map = {
alias: rewrites.Alias
, append: rewrites.Append
append: rewrites.Append
, ensure: rewrites.Ensure

@@ -29,2 +28,3 @@ , override: rewrites.Override

, extend: rewrites.Extend
, option: rewrites.Option
, path: rewrites.Path

@@ -31,0 +31,0 @@ };

@@ -7,12 +7,18 @@ "use strict";

var Extend = {
inherits: Rewrite
, _name: 'alias'
inherits: Rewrite
, _name: 'extend'
, execute: function(request, callback) {
this._loader.load(this.value, function(error, rules){
, execute: function execute(request, callback) {
var aliased = this.value;
this._loader.load(aliased, function(err, result){
if(err){
return callback(err, null);
}
if(error){
return callback(error, null);
if('execute' in result){
this.then(result);
execute.super.call(this, request, callback);
} else {
callback(new Error('Insufficient loader for extend rules'), null);
}
rules.execute(request, callback);
}.bind(this));

@@ -19,0 +25,0 @@ }

@@ -1,2 +0,1 @@

module.exports.Alias = require('./Alias');
module.exports.Append = require('./Append');

@@ -6,4 +5,5 @@ module.exports.Ensure = require('./Ensure');

module.exports.Rewrite = require('./Rewrite');
module.exports.Template = require('./Template');
module.exports.Extend = require('./Extend');
module.exports.Path = require('./Path');
module.exports.Template = require('./Template');
module.exports.Extend = require('./Extend');
module.exports.Path = require('./Path');
module.exports.Option = require('./Option');
"use strict";
var Class = require('ee-class');
var Class = require('ee-class'),
Types = require('ee-types');

@@ -26,3 +27,8 @@ var Rewrite = {

, value: {
get: function() { return this._value; }
get: function() {
if(Types.function(this._value)){
return this._value.call(null);
}
return this._value;
}
}

@@ -67,3 +73,3 @@

for(var i=length-1 ; i>=0 ; i--){
for(var i=length-1 ; i >= 0 ; i--){
var child = this._children[i];

@@ -75,7 +81,5 @@ current = (function(last, child){

if(err === true){
callback(null);
return;
return callback(null);
}
callback(err);
return;
return callback(err);
}

@@ -82,0 +86,0 @@ child.execute(request, last);

{
"name" : "ee-soa-transport-rewrite"
, "description" : "Rewriting middleware for the ee-soa-transports"
, "version" : "0.1.1"
, "version" : "0.1.2"
, "homepage" : "https://github.com/eventEmitter/ee-soa-transport-rewrite"

@@ -6,0 +6,0 @@ , "author" : {

#ee-soa-transport-rewrite
Middleware to modify requests sent to a service. This module is under heavy development.
- Todo: Add url rewrites
- Todo: Add placeholders in rules
- Todo: Add prepend rules
- Todo: Add templating rules... probably an extends rule...
- Todo: The rule chaining happens "in place" which means the initial rewrite is modified when chained. This should be changed.
##Rewrites
The module provides a basic set of rewrites. Rewrites are executable objects which modify a request based on a rule, to
use the internal rewrites, the rules must at least have the following form:
use the internal rewrites, the rules must at least have the following form. The middleware matches the domain and the
path against the current http request to choose rules (see matching).
var rule = { name: 'ensure', field: 'range', value: '1-10', domain: 'test.com', priority: 1 }
var rule = { name: ..., path: ..., field: ..., value: ..., domain: ... }
This rule corresponds to an Ensure rewrite which ensures that the passed request has a header named `range` and sets a
default value if not. Rewrites can be executed.
The `name` of the rule determines the corresponding rewrite class:
var rewrite = new Ensure(rule.domain, rule.field, rule.value, rule.priority, loader);
- **append** appends the specified `value` to the header specified in `field`.
- **ensure** checks if the header specified in `field` is present. If not it is set to `field`.
- **override** overrides the header `field` with `value`.
- **path** modifies the requested pathname `path` to `value` (use to map to api endpoints).
- **template** sets a template variable, basically `request.template = rule.value`
- **option** allows setting arbitrary values to an options hashtable called `rewriteOptions` ( `request.rewriteOptions[field] = value`)
Further planned but not implemented or tested yet are:
- **extend** extends an existing ruleset
Consider the following example:
var rule = { domain: 'test.com', path: '/detail', name: 'ensure', field: 'select', value: '*' }
// is transformed to
var rewrite = new Ensure(rule, ...);
// and executed on the request
rewrite.execute(request, function(err){});
Rewrites can be combined to a chain which then is executed sequentially.
If the `value` field is of type 'function', it gets executed every time the rewrite rule is applied (e.g. a timing function)
Rewrites can be combined to a chain which then is executed sequentially (for development).
rewrite.then(new Ensure(...)).execute(request, function(err){});
###Matching
The matching of the `path` of the rules is type based:
- if the path is `null` or '*' it is applied to all requests (which match the domain)
- if the path is of type string it is exactly matched e.g. `key == path`
- if the path is of type RegExp, a regexp matching is performed
##Loaders

@@ -75,19 +93,11 @@ The rewrite module uses loaders to load rules from different sources. All loaders can be nested to combine loading/caching

###DatabaseLoader
Is currently in development, it probably does not make sense to have a default implementation. In our concrete case we
use the `ee-orm` to load rules based on a key. The Database loader is very likely to be changed.
Is currently in development, it probably does not make sense to have a default implementation.
load: function(domain, callback) {
this._orm.call({domain:domain}).find(function(err, result){
callback(err, result)
});
}
###RewriteLoader
Is a transforming loader which creates `rewrite.Rewrite` instances (or subclasses) from the rulesets to get executable
rewrites and can be seen as a factory.
rewrites and can be seen as a factory (internally uses the `transformer.FactoryTransformer`.
##Transformers
Transformers are classes/objects to transform the loaded rulesets (a loader can be its own transformer!). Below an example
of a transformer that simply filters the rules based on the key value passed to the loader, which is more or less how the
`FilterTransformer` works.
Transformers are classes/objects to transform the loaded rulesets of a transformer that simply filters the rules based
on the key value passed to the loader.

@@ -102,2 +112,4 @@ var transformer = {

var loader = new TransformingLoader(transformer, {load: function(err, cb){ cb(null, rules}; }};
##Caches

@@ -112,2 +124,12 @@ Caches used with the cached loader must adhere to a simple interface:

Different caches are always injected into the loaders which makes them inherently testable.
#Changelog
##v0.1.2
- added Option rewrite rule
- values which are of type function are evaluated now
Different caches are always injected into the loaders which makes them inherently testable.
- Todo: Add prepend rules
- Todo: Add templating rules... probably an extends rule...
- Todo: The rule chaining happens "in place" which means the initial rewrite is modified when chained. This should be changed.
- Todo: Add caching

@@ -14,3 +14,3 @@ var assert = require('assert');

{domain: 'test1.com', path: /\/somewhere\/(\d+)/, name: 'path', field: '', value: '/somewhere-else/$1' },
{domain: 'test2.com', path: null, name: 'alias', field: '', value: 'rewritten.com' },
{domain: 'test1.com', path: null, name: 'option', field: 'test', value: 'works' },
{domain: 'rewritten.com', path: null, name: 'ensure', field: 'range', value: '1-20'},

@@ -48,4 +48,9 @@ {domain: 'rewritten.com', path: null, name: 'append', field: 'filter', value: ', deleted!=null'},

});
it('should append options', function(){
assert(!!req.rewriteOptions);
assert.equal(req.rewriteOptions['test'], 'works');
});
});
});
});

@@ -25,5 +25,9 @@ var assert = require('assert');

template = new rewrites.Template({domain:'test.com', field:'template', value:'index.nunjucks.hmtl'}),
path = new rewrites.Path({domain:'test.com', path:/\/somewhere\/(\d+)/, value: '/somewhere-else/$1' });
path = new rewrites.Path({domain:'test.com', path:/\/somewhere\/(\d+)/, value: '/somewhere-else/$1' }),
option1 = new rewrites.Option({domain: 'test.com', field:'testosteron', value: true}),
option2 = new rewrites.Option({domain: 'test.com', field:'something', value: 1000}),
option3 = new rewrites.Option({domain: 'test.com', field: 'whatTimeIsIt', value: function(){ return 'Flaava Flave'; }})
it('should do a proper setup', function(){

@@ -129,2 +133,25 @@ assert.equal(1, rew.length);

describe('Option', function(){
describe('#execute', function(){
option1.then(option2).then(option3);
option1.execute(MockRequest, function(err){
assert(!err);
it('should append a rewriteOptions object', function(){
assert('rewriteOptions' in MockRequest);
});
it('should have set the values', function(){
assert.strictEqual(MockRequest.rewriteOptions['testosteron'], true);
assert.strictEqual(MockRequest.rewriteOptions['something'], 1000);
});
it('and invoke functions', function(){
assert.strictEqual(MockRequest.rewriteOptions['whatTimeIsIt'], 'Flaava Flave');
});
});
});
});
});

Sorry, the diff of this file is not supported yet