From c8a94b2fb465c15cf9777e995f2c726fd3afc8b8 Mon Sep 17 00:00:00 2001 From: Evheniy Bystrov Date: Sun, 29 Oct 2017 09:07:48 +0200 Subject: [PATCH] updates --- .editorconfig | 10 ++ .eslintrc | 11 +- .gitignore | 3 +- .travis.yml | 6 +- README.md | 43 ++------ appveyor.yml | 4 +- index.js | 30 ++--- package.json | 55 +++++----- tests/index.js | 293 +++++++++++++++++++++++++------------------------ 9 files changed, 225 insertions(+), 230 deletions(-) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..6fe4091 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,10 @@ +root = true + +[*] +end_of_line = lf +insert_final_newline = true + +charset = utf-8 + +indent_style = space +indent_size = 2 diff --git a/.eslintrc b/.eslintrc index 2475258..28dfb5e 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,17 +1,10 @@ { - "extends": "eslint", + "extends": "airbnb-base", "env": { - "es6": true, "node": true, "mocha": true }, - "parserOptions": { - "ecmaVersion": 8, - "sourceType": "module" - }, "rules": { - "indent": ["error", 4, {"SwitchCase": 1}], - "quotes": ["error", "single"], "no-unused-expressions": 0 } -} \ No newline at end of file +} diff --git a/.gitignore b/.gitignore index 4572024..20e1510 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ coverage .idea npm-debug.log node_modules -.DS_Store \ No newline at end of file +.DS_Store +package-lock.json diff --git a/.travis.yml b/.travis.yml index 0a7a3c2..a164bd4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,8 @@ language: node_js node_js: - - "7.6" - - "7.7" + - "7" + - "8" script: - npm run lint - npm run test - - npm run report \ No newline at end of file + - npm run report diff --git a/README.md b/README.md index c9d5f8c..2e11bbe 100644 --- a/README.md +++ b/README.md @@ -26,53 +26,32 @@ It helps run express middleware on YEPS ## How to use -app.js - - const http = require('http'); + const path = require('path'); const App = require('yeps'); - const app = new App(); const error = require('yeps-error'); const Router = require('yeps-router'); - const router = new Router(); + + const server = require('yeps-server'); const wrapper = require('yeps-express-wrapper'); + const app = new App(); + const router = new Router(); + // express middleware - const bodyParser = require('body-parser'); const favicon = require('serve-favicon'); - const path = require('path'); - app.then(wrapper(favicon(path.join(__dirname, 'public', 'favicon.ico')))); - app.all([ - error(), - wrapper(bodyParser.json()), - ]); + + app.then(error()); router.get('/').then(async ctx => { - console.log(ctx.req.body); - ctx.res.writeHead(200); + ctx.res.statusCode = 200; ctx.res.end('test'); }); app.then(router.resolve()); + server.createHttpServer(app); - http - .createServer(app.resolve()) - .listen(parseInt(process.env.PORT || '3000', 10)); -And - - node app.js - -## Links - -* [yeps](https://github.com/evheniy/yeps) - YEPS -* [yeps-router](https://github.com/evheniy/yeps-router) - YEPS promise based router -* [yeps-error](https://github.com/evheniy/yeps-error) - YEPS 404/500 error handler -* [yeps-redis](https://github.com/evheniy/yeps-redis) - YEPS promise based redis client -* [yeps-logger](https://github.com/evheniy/yeps-logger) - YEPS Async logger -* [yeps-boilerplate](https://github.com/evheniy/yeps-boilerplate) - YEPS app boilerplate -* [yeps-promisify](https://github.com/evheniy/yeps-promisify) - YEPS kernel -* [yeps-benchmark](https://github.com/evheniy/yeps-benchmark) - performance comparison koa2, express and node http -* [express](https://github.com/expressjs/express) +#### [YEPS documentation](http://yeps.info/)yeps-bodyparser](https://github.com/evheniy/yeps-bodyparser) - YEPS body parser diff --git a/appveyor.yml b/appveyor.yml index 1a89ce0..3f71e3a 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,6 +1,6 @@ environment: matrix: - - nodejs_version: "7.7" + - nodejs_version: "8" install: - ps: Install-Product node $env:nodejs_version - npm install @@ -10,4 +10,4 @@ test_script: - npm run lint - npm run test:security - npm run test:code -build: off \ No newline at end of file +build: off diff --git a/index.js b/index.js index c91053a..c4cd850 100644 --- a/index.js +++ b/index.js @@ -1,19 +1,19 @@ const debug = require('debug')('yeps:wrapper'); -module.exports = fn => async context => { - debug('Wrapper created'); - return new Promise((resolve, reject) => { - context.res.on('finish', () => { - debug('Response finished'); - reject(); - }); - fn(context.req, context.res, error => { - debug(error); - if (error) { - reject(error); - } else { - resolve(); - } - }); +module.exports = fn => async (context) => { + debug('Wrapper created'); + return new Promise((resolve, reject) => { + context.res.on('finish', () => { + debug('Response finished'); + reject(); }); + fn(context.req, context.res, (error) => { + debug(error); + if (error) { + reject(error); + } else { + resolve(); + } + }); + }); }; diff --git a/package.json b/package.json index 0e8e43c..a19f057 100644 --- a/package.json +++ b/package.json @@ -1,17 +1,17 @@ { "name": "yeps-express-wrapper", - "version": "1.0.0", + "version": "1.0.1", "description": "YEPS express wrapper", "main": "index.js", "scripts": { - "lint": "./node_modules/.bin/npm-run-all --parallel lint:**", - "lint:js": "./node_modules/.bin/eslint index.js tests", - "test": "./node_modules/.bin/npm-run-all --parallel test:**", - "test:security": "./node_modules/.bin/nsp check", - "test:code": "node ./node_modules/mocha/bin/_mocha tests --recursive", - "test:coverage": "./node_modules/.bin/istanbul cover ./node_modules/mocha/bin/_mocha -- tests --recursive", - "report": "cat ./coverage/lcov.info | ./node_modules/.bin/coveralls", - "clear": "./node_modules/.bin/rimraf coverage", + "lint": "npm-run-all lint:**", + "lint:js": "eslint index.js tests", + "test": "npm-run-all test:**", + "test:security": "nsp check", + "test:code": "mocha tests --recursive", + "test:coverage": "istanbul cover ./node_modules/mocha/bin/_mocha -- tests --recursive", + "report": "cat ./coverage/lcov.info | coveralls", + "clear": "rimraf coverage", "precommit": "npm run lint && npm test", "prepush": "npm run lint && npm test" }, @@ -47,24 +47,27 @@ "node": ">=7.6.0" }, "devDependencies": { - "body-parser": "^1.16.1", - "chai": "^3.5.0", + "body-parser": "^1.18.2", + "chai": "^4.1.2", "chai-http": "^3.0.0", - "coveralls": "^2.11.16", - "debug": "^2.6.1", - "eslint": "^3.15.0", - "eslint-config-eslint": "^3.0.0", - "husky": "^0.13.1", + "coveralls": "^3.0.0", + "eslint": "^4.10.0", + "eslint-config-airbnb-base": "^12.1.0", + "eslint-plugin-import": "^2.8.0", + "husky": "^0.14.3", "istanbul": "^1.1.0-alpha.1", - "mocha": "^3.2.0", - "mocha-lcov-reporter": "^1.2.0", - "npm-run-all": "^4.0.1", - "nsp": "^2.6.2", - "promise-pause-timeout": "^1.0.0", - "rimraf": "^2.5.4", - "serve-favicon": "^2.4.0", - "yeps": "^1.0.0", - "yeps-error": "^1.0.0", - "yeps-router": "^1.0.0" + "mocha": "^4.0.1", + "mocha-lcov-reporter": "^1.3.0", + "npm-run-all": "^4.1.1", + "nsp": "^3.0.0", + "rimraf": "^2.6.2", + "serve-favicon": "^2.4.5", + "yeps": "^1.0.1", + "yeps-error": "^1.2.2", + "yeps-router": "^1.1.0", + "yeps-server": "^1.1.0" + }, + "dependencies": { + "debug": "^3.1.0" } } diff --git a/tests/index.js b/tests/index.js index 6938266..d5b2ade 100644 --- a/tests/index.js +++ b/tests/index.js @@ -1,161 +1,170 @@ -const App = require('yeps'); -const error = require('yeps-error'); -const http = require('http'); const chai = require('chai'); const chaiHttp = require('chai-http'); +const path = require('path'); +const App = require('yeps'); +const error = require('yeps-error'); +const srv = require('yeps-server'); const wrapper = require('..'); -const expect = chai.expect; const bodyParser = require('body-parser'); const favicon = require('serve-favicon'); -const path = require('path'); + +const { expect } = chai; chai.use(chaiHttp); let app; +let server; describe('YEPS express wrapper test', () => { - - beforeEach(() => { - app = new App(); + beforeEach(() => { + app = new App(); + server = srv.createHttpServer(app); + }); + afterEach(() => { + server.close(); + }); + + it('should test next()', async () => { + let isTestFinished1 = false; + let isTestFinished2 = false; + let isTestFinished3 = false; + + app.then(wrapper((req, res, next) => { + isTestFinished1 = true; + next(); + })); + + app.then(async (ctx) => { + isTestFinished2 = true; + ctx.res.statusCode = 200; + ctx.res.end('test'); }); - it('should test next()', async () => { - let isTestFinished1 = false; - let isTestFinished2 = false; - let isTestFinished3 = false; - - app.then(wrapper((req, res, next) => { - isTestFinished1 = true; - next(); - })); - app.then(async ctx => { - isTestFinished2 = true; - ctx.res.writeHead(200); - ctx.res.end('test'); - }); - - await chai.request(http.createServer(app.resolve())) - .get('/') - .send() - .then(res => { - expect(res).to.have.status(200); - expect(res.text).to.be.equal('test'); - isTestFinished3 = true; - }); - - expect(isTestFinished1).is.true; - expect(isTestFinished2).is.true; - expect(isTestFinished3).is.true; + await chai.request(server) + .get('/') + .send() + .then((res) => { + expect(res).to.have.status(200); + expect(res.text).to.be.equal('test'); + isTestFinished3 = true; + }); + + expect(isTestFinished1).is.true; + expect(isTestFinished2).is.true; + expect(isTestFinished3).is.true; + }); + + it('should test next(error)', async () => { + let isTestFinished1 = false; + let isTestFinished2 = false; + let isTestFinished3 = false; + + app.all([ + error(), + ]); + + app.then(wrapper((req, res, next) => { + isTestFinished1 = true; + next(new Error('error')); + })); + + app.then(async (ctx) => { + isTestFinished2 = true; + ctx.res.statusCode = 200; + ctx.res.end('test'); }); - it('should test next(error)', async () => { - let isTestFinished1 = false; - let isTestFinished2 = false; - let isTestFinished3 = false; - - app.all([ - error(), - ]); - - app.then(wrapper((req, res, next) => { - isTestFinished1 = true; - next(new Error('error')); - })); - app.then(async ctx => { - isTestFinished2 = true; - ctx.res.writeHead(200); - ctx.res.end('test'); - }); - - await chai.request(http.createServer(app.resolve())) - .get('/') - .send() - .catch(err => { - expect(err).to.have.status(500); - expect(err.message).to.be.equal('Internal Server Error'); - isTestFinished3 = true; - }); - - expect(isTestFinished1).is.true; - expect(isTestFinished2).is.false; - expect(isTestFinished3).is.true; + await chai.request(server) + .get('/') + .send() + .catch((err) => { + expect(err).to.have.status(500); + expect(err.message).to.be.equal('Internal Server Error'); + isTestFinished3 = true; + }); + + expect(isTestFinished1).is.true; + expect(isTestFinished2).is.false; + expect(isTestFinished3).is.true; + }); + + it('should test without next = res.end()', async () => { + let isTestFinished1 = false; + let isTestFinished2 = false; + let isTestFinished3 = false; + + app.then(wrapper((req, res) => { + isTestFinished1 = true; + res.statusCode = 200; + res.end('next'); + })); + + app.then(async (ctx) => { + isTestFinished2 = true; + ctx.res.writeHead(200); + ctx.res.end('test'); }); - it('should test without next = res.end()', async () => { - let isTestFinished1 = false; - let isTestFinished2 = false; - let isTestFinished3 = false; - - app.then(wrapper((req, res) => { - isTestFinished1 = true; - res.writeHead(200); - res.end('next'); - })); - app.then(async ctx => { - isTestFinished2 = true; - ctx.res.writeHead(200); - ctx.res.end('test'); - }); - - await chai.request(http.createServer(app.resolve())) - .get('/') - .send() - .then(res => { - expect(res).to.have.status(200); - expect(res.text).to.be.equal('next'); - isTestFinished3 = true; - }); - - expect(isTestFinished1).is.true; - expect(isTestFinished2).is.false; - expect(isTestFinished3).is.true; - }); - - it('body-parser', async () => { - let isTestFinished1 = false; - let isTestFinished2 = false; - - app.then(wrapper(bodyParser.json())); - app.then(async ctx => { - isTestFinished1 = true; - ctx.res.writeHead(200, {'Content-Type': 'application/json'}); - ctx.res.end(JSON.stringify(ctx.req.body)); - }); - - await chai.request(http.createServer(app.resolve())) - .get('/') - .set('Content-Type', 'application/json') - .send('{"user":"test"}') - .then(res => { - expect(res).to.have.status(200); - expect(res.text).to.be.equal('{"user":"test"}'); - expect(res.body).is.an('object'); - expect(res.body).to.have.property('user'); - expect(res.body.user).is.not.empty; - expect(res.body.user).to.be.equal('test'); - isTestFinished2 = true; - }); - - expect(isTestFinished1).is.true; - expect(isTestFinished2).is.true; - }); - - it('serve-favicon', async () => { - let isTestFinished = false; - - app.then(wrapper(favicon(path.join(__dirname, 'public', 'favicon.ico')))); - - await chai.request(http.createServer(app.resolve())) - .get('/favicon.ico') - .send() - .then(res => { - expect(res).to.have.status(200); - expect(res.headers['content-type']).to.be.equal('image/x-icon'); - expect(res.headers.etag).is.not.empty; - expect(res.headers['cache-control']).is.not.empty; - isTestFinished = true; - }); - - expect(isTestFinished).is.true; + await chai.request(server) + .get('/') + .send() + .then((res) => { + expect(res).to.have.status(200); + expect(res.text).to.be.equal('next'); + isTestFinished3 = true; + }); + + expect(isTestFinished1).is.true; + expect(isTestFinished2).is.false; + expect(isTestFinished3).is.true; + }); + + it('body-parser', async () => { + let isTestFinished1 = false; + let isTestFinished2 = false; + + app.then(wrapper(bodyParser.json())); + + app.then(async (ctx) => { + isTestFinished1 = true; + ctx.res.statusCode = 200; + ctx.res.setHeader('Content-Type', 'application/json'); + ctx.res.end(JSON.stringify(ctx.req.body)); }); + await chai.request(server) + .get('/') + .set('Content-Type', 'application/json') + .send('{"user":"test"}') + .then((res) => { + expect(res).to.have.status(200); + expect(res.text).to.be.equal('{"user":"test"}'); + expect(res.body).is.an('object'); + expect(res.body).to.have.property('user'); + expect(res.body.user).is.not.empty; + expect(res.body.user).to.be.equal('test'); + isTestFinished2 = true; + }); + + expect(isTestFinished1).is.true; + expect(isTestFinished2).is.true; + }); + + it('serve-favicon', async () => { + let isTestFinished = false; + + app.then(wrapper(favicon(path.join(__dirname, 'public', 'favicon.ico')))); + + await chai.request(server) + .get('/favicon.ico') + .send() + .then((res) => { + expect(res).to.have.status(200); + expect(res.headers['content-type']).to.be.equal('image/x-icon'); + expect(res.headers.etag).is.not.empty; + expect(res.headers['cache-control']).is.not.empty; + isTestFinished = true; + }); + + expect(isTestFinished).is.true; + }); });