diff --git a/.gitignore b/.gitignore index d0df85ed..37179caf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1,7 @@ -coverage package-lock.json node_modules build dist -out *.log *.dump .DS_Store diff --git a/.npmignore b/.npmignore index 1c4498b0..2d77c376 100644 --- a/.npmignore +++ b/.npmignore @@ -1,4 +1,3 @@ -coverage node_modules build tests diff --git a/doc/README.hbs b/doc/README.hbs index f17c10fa..18ff0fef 100644 --- a/doc/README.hbs +++ b/doc/README.hbs @@ -5,13 +5,13 @@ [![bithound](https://www.bithound.io/github/Kronos-Integration/kronos-service-koa/badges/score.svg)](https://www.bithound.io/github/Kronos-Integration/kronos-service-koa) [![codecov.io](http://codecov.io/github/Kronos-Integration/kronos-service-koa/coverage.svg?branch=master)](http://codecov.io/github/Kronos-Integration/kronos-service-koa?branch=master) [![Coverage Status](https://coveralls.io/repos/Kronos-Integration/kronos-service-koa/badge.svg)](https://coveralls.io/r/Kronos-Integration/kronos-service-koa) -[![Code Climate](https://codeclimate.com/github/Kronos-Integration/kronos-service-koa/badges/gpa.svg)](https://codeclimate.com/github/Kronos-Integration/kronos-service-koa) [![Known Vulnerabilities](https://snyk.io/test/github/Kronos-Integration/kronos-service-koa/badge.svg)](https://snyk.io/test/github/Kronos-Integration/kronos-service-koa) [![GitHub Issues](https://img.shields.io/github/issues/Kronos-Integration/kronos-service-koa.svg?style=flat-square)](https://github.com/Kronos-Integration/kronos-service-koa/issues) [![Stories in Ready](https://badge.waffle.io/Kronos-Integration/kronos-service-koa.svg?label=ready&title=Ready)](http://waffle.io/Kronos-Integration/kronos-service-koa) [![Dependency Status](https://david-dm.org/Kronos-Integration/kronos-service-koa.svg)](https://david-dm.org/Kronos-Integration/kronos-service-koa) [![devDependency Status](https://david-dm.org/Kronos-Integration/kronos-service-koa/dev-status.svg)](https://david-dm.org/Kronos-Integration/kronos-service-koa#info=devDependencies) [![docs](http://inch-ci.org/github/Kronos-Integration/kronos-service-koa.svg?branch=master)](http://inch-ci.org/github/Kronos-Integration/kronos-service-koa) +[![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/sindresorhus/xo) [![downloads](http://img.shields.io/npm/dm/kronos-service-koa.svg?style=flat-square)](https://npmjs.org/package/kronos-service-koa) [![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/) diff --git a/doc/jsdoc.json b/doc/jsdoc.json new file mode 100644 index 00000000..137f4c28 --- /dev/null +++ b/doc/jsdoc.json @@ -0,0 +1,10 @@ +{ + "plugins": [ + "node_modules/jsdoc-babel" + ], + "babel": { + "presets": [ + "env" + ] + } +} \ No newline at end of file diff --git a/package.json b/package.json index 404d29c9..288d5af9 100644 --- a/package.json +++ b/package.json @@ -5,16 +5,16 @@ "keywords": [ "kronos-service" ], - "main": "dist/module.js", - "module": "src/ServiceKOA.js", + "main": "dist/service-koa.js", + "module": "src/service-koa.js", "scripts": { - "cover": "nyc --temp-directory build/nyc npm test", - "test": "mocha tests/*-test.js", + "cover": "nyc --temp-directory build/nyc ava", + "test": "ava", "semantic-release": "semantic-release", - "docs": "jsdoc2md -l off -t doc/README.hbs -f src/*.js >README.md", - "pretest": "rollup -c", - "posttest": "markdown-doctest", - "precover": "rollup -c", + "docs": "jsdoc2md --configure doc/jsdoc.json -l off -t doc/README.hbs -f src/*.js >README.md", + "pretest": "rollup -c tests/rollup.config.js", + "posttest": "npm run prepare && markdown-doctest", + "precover": "rollup -c tests/rollup.config.js", "prepare": "rollup -c" }, "repository": { @@ -29,23 +29,27 @@ "koa-jwt": "3.2.2", "koa-static": "4.0.2", "kronos-koa": "^1.2.2", - "kronos-service": "^4.8.2", + "kronos-service": "^4.10.0", "model-attributes": "^3.0.10", "path-to-regexp": "^2.0.0", - "ws": "^3.1.0" + "ws": "^3.3.2" }, "devDependencies": { - "chai": "^4.1.2", "koa-route": "^3.2.0", - "mocha": "^3.5.3", "network-address": "^1.1.2", "semantic-release": "^9.1.1", "supertest": "^3.0.0", "supertest-as-promised": "^4.0.2", - "jsdoc-to-markdown": "^3.0.1", + "jsdoc-to-markdown": "^3.0.2", "markdown-doctest": "^0.9.1", - "rollup": "^0.51.5", - "nyc": "^11.3.0" + "rollup": "^0.51.7", + "nyc": "^11.3.0", + "jsdoc-babel": "^0.3.0", + "ava": "^0.23.0", + "rollup-plugin-babel": "^3.0.2", + "rollup-plugin-multi-entry": "^2.0.2", + "babel-preset-env": "^1.6.1", + "xo": "^0.19.0" }, "contributors": [ { @@ -63,19 +67,31 @@ }, "nyc": { "include": [ - "dist/**/*.js" + "build/*-test.js", + "src/**/*.js" ], "reporter": [ "lcov" ], - "report-dir": "./coverage" + "report-dir": "./build/coverage" }, "template": { "repository": { - "url": "https://github.com/Kronos-Tools/npm-package-template.git" + "url": "https://github.com/arlac77/npm-package-template.git" } }, "xo": { "space": true + }, + "ava": { + "files": [ + "build/*-test.js" + ], + "presets": [ + "env" + ], + "require": [ + "babel-register" + ] } } diff --git a/src/ServiceKOA.js b/src/service-koa.js similarity index 97% rename from src/ServiceKOA.js rename to src/service-koa.js index 3b54ad34..d64d42f5 100644 --- a/src/ServiceKOA.js +++ b/src/service-koa.js @@ -1,6 +1,3 @@ -/* jslint node: true, esnext: true */ -'use strict'; - const http = require('http'), https = require('https'), url = require('url'), @@ -16,7 +13,7 @@ import { mergeAttributes, createAttributes } from 'model-attributes'; /** * HTTP server with koa */ -class ServiceKOA extends Service { +export class ServiceKOA extends Service { static get name() { return 'koa'; } @@ -315,7 +312,7 @@ function decode(val) { /** * Endpoint to link against a koa route */ -class RouteSendEndpoint extends SendEndpoint { +export class RouteSendEndpoint extends SendEndpoint { /** * @param name {String} endpoint name * @param owner {Step} the owner of the endpoint @@ -410,7 +407,7 @@ class RouteSendEndpoint extends SendEndpoint { } } -class SocketEndpoint extends SendEndpoint { +export class SocketEndpoint extends SendEndpoint { constructor(name, owner, path) { super(name, owner, { createOpposite: true @@ -475,8 +472,6 @@ class SocketEndpoint extends SendEndpoint { } } -function registerWithManager(manager) { +export function registerWithManager(manager) { return manager.registerServiceFactory(ServiceKOA); } - -export { registerWithManager, ServiceKOA, RouteSendEndpoint, SocketEndpoint }; diff --git a/tests/failure-test.js b/tests/failure-test.js index 7912691b..80def75b 100644 --- a/tests/failure-test.js +++ b/tests/failure-test.js @@ -1,44 +1,41 @@ -/* global describe, it, xit, before, beforeEach, after, afterEach */ -/* jslint node: true, esnext: true */ - -'use strict'; - -const chai = require('chai'), - assert = chai.assert, - expect = chai.expect, - should = chai.should(), - { - ServiceProviderMixin, Service - } = require('kronos-service'), - { - ServiceKOA - } = require('../dist/module'); +import { ServiceProviderMixin, Service } from 'kronos-service'; +import { ServiceKOA } from '../src/service-koa'; +import test from 'ava'; class ServiceProvider extends ServiceProviderMixin(Service) {} const sp = new ServiceProvider(); -describe('service-koa failures', () => { - describe('with already in use port', () => { - const ks1 = new ServiceKOA({ +test('service-koa failures with already in use port', async t => { + const ks1 = new ServiceKOA( + { name: 'my-name1', - listen: {  + listen: { port: 1235 } - }, sp); + }, + sp + ); - const ks2 = new ServiceKOA({ + const ks2 = new ServiceKOA( + { name: 'my-name2', - listen: {  + listen: { port: 1235 } - }, sp); - - it('can start', () => { - ks1.start().then(() => ks2.start().then(() => { - assert.equal(ks1.state, 'running'); - assert.equal(ks2.state, 'running'); - })); - }); - }); + }, + sp + ); + + await ks1.start(); + t.is(ks1.state, 'running'); + + try { + await ks2.start(); + } catch (e) {} + + t.is(ks2.state, 'failed'); + + ks1.stop(); + ks2.stop(); }); diff --git a/tests/rollup.config.js b/tests/rollup.config.js new file mode 100644 index 00000000..4bb2ba3a --- /dev/null +++ b/tests/rollup.config.js @@ -0,0 +1,12 @@ +import multiEntry from 'rollup-plugin-multi-entry'; + +export default { + input: 'tests/**/*-test.js', + output: { + file: 'build/bundle-test.js', + format: 'cjs', + sourcemap: true + }, + external: ['ava', 'model-attributes', 'kronos-service', 'kronos-endpoint'], + plugins: [multiEntry()] +}; diff --git a/tests/simple-test.js b/tests/simple-test.js index 10dd4e8b..d4dc56bc 100644 --- a/tests/simple-test.js +++ b/tests/simple-test.js @@ -1,65 +1,72 @@ -/* global describe, it, xit, before, beforeEach, after, afterEach */ -/* jslint node: true, esnext: true */ - -'use strict'; - -const chai = require('chai'), - assert = chai.assert, - expect = chai.expect, - should = chai.should(), - fs = require('fs'), - path = require('path'), - address = require('network-address'), - request = require('supertest-as-promised')(Promise), - route = require('koa-route'), - { - ServiceProviderMixin, Service - } = require('kronos-service'), - { - ServiceKOA - } = require('../dist/module'); +const fs = require('fs'); +const path = require('path'); +const address = require('network-address'); +const request = require('supertest-as-promised')(Promise); +const route = require('koa-route'); -class ServiceProvider extends ServiceProviderMixin(Service) {} +import { ServiceProviderMixin, Service } from 'kronos-service'; +import { ServiceKOA } from '../src/service-koa'; +import test from 'ava'; -const sp = new ServiceProvider(); +class ServiceProvider extends ServiceProviderMixin(Service) {} -describe('service-koa', () => { - describe('plain http', () => { - const ks = new ServiceKOA({ +test('service-koa plain http', t => { + const sp = new ServiceProvider(); + const ks = new ServiceKOA( + { type: 'xxx', name: 'my-name', listen: { port: 1234, address: address() } - }, sp); + }, + sp + ); - it('has name', () => assert.equal(ks.name, 'my-name')); - it('is not secure', () => assert.equal(ks.isSecure, false)); + t.is(ks.name, 'my-name'); + t.is(ks.isSecure, false); - it('has port', () => assert.equal(ks.port, 1234)); + t.is(ks.port, 1234); - it('has address', () => assert.equal(ks.address, address())); - it('has url', () => assert.equal(ks.url, `http://${address()}:1234`)); + t.is(ks.address, address()); + t.is(ks.url, `http://${address()}:1234`); - it('has server timeout', () => assert.equal(ks.timeout.server, 120)); + t.is(ks.timeout.server, 120); +}); - describe('configure', () => { - it('can change port', () => - ks.configure({ - listen: { - port: 1235 - } - }).then(() => assert.equal(ks.listen.port, 1235))); +test('service-koa plain http change port', async t => { + const sp = new ServiceProvider(); + const ks = new ServiceKOA( + { + type: 'xxx', + name: 'my-name', + listen: { + port: 1234, + address: address() + } + }, + sp + ); - it('can change timeout', () => - ks.configure({ - timeout: { - server: 123.45 - } - }).then(() => assert.equal(ks.timeout.server, 123.45))); - }); + await ks.configure({ + listen: { + port: 1235 + } + }); + t.is(ks.listen.port, 1235); + + await ks.configure({ + timeout: { + server: 123.45 + } + }); + + t.is(ks.timeout.server, 123.45); +}); + +/* it('GET /', () => ks.start().then(() => { ks.koa.use(route.get('/', ctx => ctx.body = 'OK')); @@ -121,3 +128,5 @@ describe('service-koa', () => { })); }); }); + +*/ diff --git a/tests/socket-test.js b/tests/socket-test.js index c9510320..1630f158 100644 --- a/tests/socket-test.js +++ b/tests/socket-test.js @@ -1,25 +1,15 @@ -/* global describe, it, xit, before, beforeEach, after, afterEach */ -/* jslint node: true, esnext: true */ - -'use strict'; - -const chai = require('chai'), - assert = chai.assert, - expect = chai.expect, - should = chai.should(), - fs = require('fs'), - path = require('path'), - WebSocket = require('ws'), - { ServiceProviderMixin, Service } = require('kronos-service'), - { ServiceKOA } = require('../dist/module'); +const fs = require('fs'); +const path = require('path'); +const WebSocket = require('ws'); +import { ServiceProviderMixin, Service } from 'kronos-service'; +import { ServiceKOA } from '../src/service-koa'; +import test from 'ava'; class ServiceProvider extends ServiceProviderMixin(Service) {} const sp = new ServiceProvider(); -describe('service-koa socket', function() { - //this.timeout(200000); - +test('service-koa socket', async t => { const ks1 = new ServiceKOA( { name: 'my-name1', @@ -33,73 +23,58 @@ describe('service-koa socket', function() { const se = ks1.createSocketEndpoint('test', '/test'); - describe('socket endpoint', () => { - it('is socket', () => assert.isTrue(se.socket)); - it('isOut', () => assert.isTrue(se.isOut)); - it('has opposite', () => assert.isDefined(se.opposite)); - it('opposite isIn', () => assert.isTrue(se.opposite.isIn)); - }); + t.is(se.socket, true); + t.is(se.isOut, true); + t.truthy(se.opposite); + t.truthy(se.opposite.isIn); se.receive = message => { console.log(`se: ${message}`); return se.opposite.receive(message); }; - /* - setInterval(() => { - se.opposite.receive({ - memory: process.memoryUsage() - }); - }, 1000); - */ const socketUrl = 'ws://localhost:1236/test'; - it('socket', done => { - ks1 - .configure({ - listen: { - address: 'localhost', - port: 1236 - } - }) - .then(() => - ks1.start().then(() => { - ks1.koa.use(ctx => { - ctx.type = 'text/html'; - ctx.body = fs.createReadStream( - path.join(__dirname, 'fixtures', 'index.html') - ); - }); + await ks1.configure({ + listen: { + address: 'localhost', + port: 1236 + } + }); - const ws = new WebSocket(socketUrl, {}); + await ks1.start(); - ws.on('open', () => - ws.send(Date.now().toString(), { - mask: true - }) - ); - ws.on('close', () => { - console.log('disconnected'); - }); + ks1.koa.use(ctx => { + ctx.type = 'text/html'; + ctx.body = fs.createReadStream( + path.join(__dirname, 'fixtures', 'index.html') + ); + }); - ws.on('message', (data, flags) => { - console.log( - 'Roundtrip time: ' + (Date.now() - parseInt(data, 10)) + 'ms', - flags - ); + const ws = new WebSocket(socketUrl, {}); - /* - setTimeout(() => { - ws.send(Date.now().toString(), { - mask: true - }); - }, 500); - */ - done(); - }); + ws.on('open', () => + ws.send(Date.now().toString(), { + mask: true + }) + ); + ws.on('close', () => { + console.log('disconnected'); + }); + + ws.on('message', (data, flags) => { + console.log( + 'Roundtrip time: ' + (Date.now() - parseInt(data, 10)) + 'ms', + flags + ); - //assert.equal(ks1.state, 'running'); - }) - ); + /* + setTimeout(() => { + ws.send(Date.now().toString(), { + mask: true + }); + }, 500); +*/ + done(); }); });