-
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.
- Loading branch information
0 parents
commit d004f77
Showing
11 changed files
with
334 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"presets": ["es2015"] | ||
} |
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,6 @@ | ||
{ | ||
"extends": "airbnb/base", | ||
"rules": { | ||
"comma-dangle": [2, "never"] | ||
} | ||
} |
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,59 @@ | ||
################################################################################ | ||
## OS generated files ## | ||
################################################################################ | ||
.DS_Store | ||
._* | ||
.Spotlight-V100 | ||
.Trashes | ||
Thumbs.db | ||
Desktop.ini | ||
|
||
################################################################################ | ||
## IDE ## | ||
################################################################################ | ||
.idea | ||
nbproject | ||
*.pydevproject | ||
.project | ||
.metadata | ||
|
||
# Sublime text editor. | ||
*.sublime-project | ||
*.sublime-workspace | ||
|
||
# External tool builders. | ||
.externalToolBuilders | ||
|
||
# Locally stored "Eclipse launch configurations". | ||
*.launch | ||
|
||
# CDT-specific. | ||
.cproject | ||
|
||
# PDT-specific. | ||
.buildpath | ||
|
||
################################################################################ | ||
## npm ## | ||
################################################################################ | ||
node_modules | ||
npm-debug.log | ||
tmp | ||
.tmp | ||
.gulp | ||
|
||
################################################################################ | ||
## Temporary files ## | ||
################################################################################ | ||
*.swp | ||
*.swo | ||
*.orig | ||
*~ | ||
*.tmp | ||
*.bak | ||
*~.nib | ||
|
||
################################################################################ | ||
## APP ## | ||
################################################################################ | ||
dist/* |
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,5 @@ | ||
language: node_js | ||
node_js: | ||
- '6.0' | ||
- '5.0' | ||
- '4.0' |
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,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2016 Eugene Zlobin | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,40 @@ | ||
|
||
# Middleware in JavaScript [![Build Status](https://travis-ci.org/Zlobin/es-middleware.png?branch=master)](https://travis-ci.org/Zlobin/es-middleware) | ||
|
||
## Synopsis | ||
|
||
This library is an skeleton for creation of middlewares which perform some processing at the start and end of a "request". Middleware functions are functions that have access to the object itself. | ||
|
||
## Examples | ||
|
||
```js | ||
var mw = new Middleware(); | ||
var fn1 = function(next) { | ||
this.foo = true; | ||
next(); | ||
}; | ||
var fn2 = function(next) { | ||
this.bar = true; | ||
next(); | ||
} | ||
var fn3 = function(next) { | ||
var self = this; | ||
|
||
setTimeout(function(next) { | ||
self.bar = false; | ||
next(); | ||
}, 50); | ||
}; | ||
var startTime; | ||
|
||
mw.use([fn1, fn2])) | ||
ms.use(fn3); | ||
|
||
startTime = Date.now(); | ||
|
||
mw.run(function() { | ||
console.log(this.foo); // true | ||
console.log(this.bar); // false | ||
console.log('time', Date.now() - startTime); // ~50 | ||
}); | ||
``` |
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,43 @@ | ||
{ | ||
"name": "es-middleware", | ||
"description": "Basic mplementation of the middleware pattern in JavaScript", | ||
"version": "1.0.0", | ||
"main": "src/index.js", | ||
"author": { | ||
"name": "Eugene Zlobin", | ||
"email": "creastar@gmail.com", | ||
"web": "https://zlobin.pro/" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/Zlobin/es-middleware.git" | ||
}, | ||
"keywords": [ | ||
"middleware", | ||
"javascript", | ||
"pattern" | ||
], | ||
"license": "MIT", | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"babel-cli": "^6.7.7", | ||
"babel-core": "^6.7.7", | ||
"babel-loader": "^6.2.4", | ||
"babel-preset-es2015": "^6.6.0", | ||
"eslint": "^2.8.0", | ||
"eslint-config-airbnb": "^8.0.0", | ||
"eslint-loader": "^1.3.0", | ||
"eslint-plugin-import": "^1.5.0", | ||
"eslint-plugin-jsx-a11y": "^1.0.3", | ||
"eslint-plugin-react": "^5.0.1", | ||
"expect": "^1.18.0", | ||
"mocha": "^2.4.5", | ||
"webpack": "^1.13.0" | ||
}, | ||
"engines": { | ||
"node": ">=4" | ||
}, | ||
"scripts": { | ||
"test": "mocha --compilers js:babel-core/register" | ||
} | ||
} |
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 @@ | ||
module.exports = require('./middleware').default; |
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,40 @@ | ||
/** Class Middleware */ | ||
export default class Middleware { | ||
/** | ||
* Add function or array of functions to the execution stack. | ||
* | ||
* @param {array|function} fn - function or array of functions. | ||
* | ||
* @return true | ||
*/ | ||
use(fn) { | ||
if (fn instanceof Array) { | ||
return fn.forEach(func => this.use(func)); | ||
} | ||
|
||
if (typeof fn !== 'function') { | ||
throw new TypeError(); | ||
} | ||
|
||
// LIFO. | ||
this.run = (stack => | ||
next => | ||
stack(() => | ||
fn.call(this, next.bind(this)) | ||
) | ||
)(this.run); | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Run stack of middlewares. | ||
* | ||
* @param {function} next - main function which will be executed at the end. | ||
* | ||
* @return function execution. | ||
*/ | ||
run(next) { | ||
return next(); | ||
} | ||
} |
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,60 @@ | ||
import Middleware from '../src/middleware'; | ||
import expect from 'expect'; | ||
|
||
describe('Middleware', function() { | ||
const mw = new Middleware(); | ||
const mw2 = new Middleware(); | ||
const fn1 = function(next) { | ||
this.foo = true; | ||
next(); | ||
}; | ||
const fn2 = function(next) { | ||
this.bar = true; | ||
next(); | ||
} | ||
const fn3 = function(next) { | ||
this.baz = true; | ||
next(); | ||
}; | ||
const fn4 = function(next) { | ||
setTimeout(() => { | ||
this.bar = false; | ||
next(); | ||
}, 50); | ||
}; | ||
let startTime; | ||
|
||
describe('1: basic functional with a single instance', () => { | ||
it('1.1: check params foo, bar, baz', done => { | ||
mw.use([fn1, fn2]); | ||
mw.use(fn3); | ||
mw.use(fn4); | ||
|
||
startTime = Date.now(); | ||
|
||
mw.run(function() { | ||
expect(this.foo).toEqual(true); | ||
expect(this.bar).toEqual(false); | ||
expect(this.baz).toEqual(true); | ||
expect(Date.now() - startTime) // ~50ms | ||
.toBeGreaterThanOrEqualTo(50) | ||
.toBeLessThan(60); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('2: basic functional with multiple instances', () => { | ||
it('2.1: check params foo, bar, baz', done => { | ||
mw2.use([fn1, fn2]); | ||
|
||
mw2.run(function() { | ||
console.log('this', this); | ||
expect(this.foo).toEqual(true); | ||
expect(this.bar).toEqual(true); | ||
expect(this.baz).toNotExist(); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); |
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,56 @@ | ||
'use strict'; | ||
|
||
const path = require('path'); | ||
const webpack = require('webpack'); | ||
const fs = require('fs'); | ||
|
||
const env = process.env.NODE_ENV || 'development'; | ||
const isProduction = (env === 'production'); | ||
|
||
let plugins = [ | ||
new webpack.optimize.OccurenceOrderPlugin(), | ||
new webpack.optimize.DedupePlugin(), | ||
new webpack.NoErrorsPlugin() | ||
]; | ||
|
||
if (isProduction) { | ||
plugins.push( | ||
new webpack.optimize.UglifyJsPlugin({ | ||
compress: { | ||
drop_console: true, | ||
unsafe: true | ||
} | ||
}) | ||
); | ||
} | ||
|
||
module.exports = { | ||
devtool: !isProduction ? 'cheap-source-map' : 'eval', | ||
entry: './src/index.js', | ||
output: { | ||
path: path.join(__dirname, 'dist'), | ||
filename: 'bundle.js', | ||
libraryTarget: 'var', | ||
library: 'Middleware' | ||
}, | ||
resolve: { | ||
root: path.join(__dirname, 'dist') | ||
}, | ||
plugins: plugins, | ||
module: { | ||
preLoaders: [ | ||
{ | ||
test: /\.js$/, | ||
include: path.join(__dirname, 'src'), | ||
loader: 'eslint-loader' | ||
} | ||
], | ||
loaders: [ | ||
{ | ||
test: /\.js$/, | ||
include: path.join(__dirname, 'src'), | ||
loaders: ['babel'] | ||
} | ||
] | ||
} | ||
}; |