Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

jigawatt

Package Overview
Dependencies
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jigawatt - npm Package Compare versions

Comparing version 0.1.3 to 0.1.4

lib/helper.js

11

index.js

@@ -5,8 +5,4 @@

const awesomize = require('./lib/awesomize');
const Validate = require('./lib/validate');
const throwMiddlewareEmpty = () => {
throw TypeError('You must provide at least one middleware');
};
const defaultSpec = _.always(_.compose(Bluebird.resolve, _.prop('data')))

@@ -70,7 +66,4 @@

const Middleware = (...middleware) => {
if(middleware.length < 1) throwMiddlewareEmpty();
Validate.testMiddleware(middleware)
//@TODO validate all the middlewar objects.
//look in lib/validate.js for inspiration.
return (req, res, next) => {

@@ -77,0 +70,0 @@ run(middleware, req)

const _ = require('ramda');
const _ = require('ramda');
const PropsCheck = require('props-check')
const Helper = require('./helper')
const SPEC = {
io : 'Function'
, transform : 'Function'
, awesomize : 'Function'
}
const throwMiddlewareNotFunction = (index) => {
throw TypeError(`All middleware given must be functions - index: ${index}`);
const throwMiddlewareEmpty = () => {
throw TypeError('You must provide at least one middleware');
};
const testMiddlewareArray = _.curry((index, mw) => {
const throwMiddlewareNotObject = (index) => {
throw TypeError(`All middleware given must be objects - index: ${index}`);
};
const typeCheckArray = _.curry((index, mw) => {
if (_.isEmpty(mw)) throwMiddlewareNotObject(index)
try {
return testMiddleware(mw);
return typeCheck(mw);
} catch (e) {
const result = /((\d:?)+)/.exec(e.message);
if (_.isNil(result)) {
throw new ReferenceError(`Unexpected Error: ${e.message}`);
}
const parent_index = result[1];
throwMiddlewareNotFunction(index + ':' + parent_index);
throwMiddlewareNotObject(index + ':' + parent_index);
}

@@ -27,7 +36,7 @@ });

const testMiddleware = (middleware) => middleware.forEach((mw, index) => {
const typeCheck = (middleware) => middleware.forEach((mw, index) => {
return _.cond([
[ _.is(Function), _.always(null) ]
, [ Array.isArray, testMiddlewareArray(index) ]
, [ _.T, () => throwMiddlewareNotFunction(index) ]
[ Helper.isObject, _.always(null) ]
, [ Array.isArray, typeCheckArray(index) ]
, [ _.T, () => throwMiddlewareNotObject(index) ]
])(mw);

@@ -37,1 +46,28 @@ });

// throwPropsCheckError : Middleware -> Error
const throwPropsCheckError = (config) => {
throw new Error(PropsCheck.human(SPEC, config))
}
// checkProps : Middleware -> Maybe Error
const checkProps = (config) => { return _.compose(
_.when(_.any(Helper.isNotEmpty), throwPropsCheckError.bind(null, config))
, _.values
, PropsCheck(SPEC)
)(config)
}
const propsCheck = _.forEach(checkProps)
const testMiddleware = (middleware) => {
if(middleware.length < 1) throwMiddlewareEmpty();
typeCheck(middleware);
propsCheck(_.unnest(middleware))
}
module.exports = {
testMiddleware
}
{
"name": "jigawatt",
"version": "0.1.3",
"version": "0.1.4",
"description": "Influential's Functional, Promise-based Express Middleware",

@@ -50,2 +50,3 @@ "main": "index.js",

"mocha": "^2.5.3",
"props-check": "^0.3.0",
"sinon": "^1.17.4"

@@ -52,0 +53,0 @@ },

@@ -179,21 +179,56 @@ /*eslint-env node, mocha */

it.skip('should throw a TypeError if one of the items is not a function',
it('should pass the data to transform if no io function is provided',
(done) => {
const req = { foo: 'bar' };
const mw = _.omit(['io'], basic_middleware);
const test = JW(mw);
const res = {
json: (data) => {
expect(data).to.deep.eql(req);
done();
}
}
test(req, res, done);
});
it('should throw a TypeError if one of the items is not an object',
() => {
const f1 = () => null;
const f2 = () => null;
const test = () => JW(f1, 'foo', f2);
const message = 'All middleware given must be functions - index: 1'
const o1 = {};
const o2 = {};
expect(test).to.throw(TypeError, message);
const t1 = [
['foo']
, () => null
, 'foo'
, null
, undefined
, Number(1)
, []
, true
]
const message = 'All middleware given must be objects - index: 1'
const master_test = (not_obj) => {
const test = () => JW(o1, not_obj, o2);
expect(test).to.throw(TypeError, message);
}
_.forEach(master_test)(t1)
});
it.skip('should throw an exception if a sub-item is not a function',
it('should throw an exception if a sub-item is not a object',
() => {
const f1 = () => null;
const f2 = () => null;
const f3 = () => null;
const test = () => JW(f1, [f2, 'foo'], f3);
const message = 'All middleware given must be functions - index: 1:1'
const o1 = {};
const o2 = {};
const o3 = {};
const test = () => JW(o1, [o2, 'foo'], o3);
const message = 'All middleware given must be objects - index: 1:1'

@@ -204,11 +239,11 @@ expect(test).to.throw(TypeError, message);

it.skip('should throw an exception if a sub-sub-item is not a function',
it('should throw an exception if a sub-sub-item is not a function',
() => {
const f1 = () => null;
const f2 = () => null;
const f3 = () => null;
const f4 = () => null;
const f5 = () => null;
const test = () => JW([f2, [f4, f5, 'foo']], f1, f3);
const message = 'All middleware given must be functions - index: 0:1:2'
const o1 = {};
const o2 = {};
const o3 = {};
const o4 = {};
const o5 = {};
const test = () => JW([o2, [o4, o5, 'foo']], o1, o3);
const message = 'All middleware given must be objects - index: 0:1:2'

@@ -222,3 +257,3 @@ expect(test).to.throw(TypeError, message);

const actual = JW(() => null);
const actual = JW({});
expect(actual).to.be.a('function');

@@ -229,2 +264,30 @@ expect(actual.length).to.eql(3);

it('should return a PropsCheckError when a middleware has invalid keys'
, () => {
const m1 = {
awesomize: (v) => ({ foo: { validate: [ v.required ] } })
, io: (req, data) => ({ bar: data.foo })
, transform: (req, data) => data
};
const m2 = {
awesomize: (v) => ({ foo: { validate: [ v.required ] } })
, io: (req, data) => ({ bar: data.foo })
};
const m3 = {
awesomizer: (v) => ({ foo: { validate: [ v.required ] } })
, io: (req, data) => ({ bar: data.foo })
};
const test1 = () => JW(m3);
expect(test1).to.throw(Error);
const test2 = () => JW(m1, [m2, m3]);
expect(test2).to.throw(Error);
});
});

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc