Skip to content

Commit

Permalink
Merge pull request #328 from Kronos-Integration/template-sync-1
Browse files Browse the repository at this point in the history
merge package template from arlac77/npm-package-template
  • Loading branch information
arlac77 committed Nov 24, 2017
2 parents 035cfc9 + 07a96e0 commit 51cf0c8
Show file tree
Hide file tree
Showing 11 changed files with 292 additions and 216 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
coverage
package-lock.json
node_modules
build
dist
out
*.log
*.dump
.DS_Store
Expand Down
1 change: 0 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
coverage
node_modules
build
tests
Expand Down
2 changes: 1 addition & 1 deletion doc/README.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -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/)

Expand Down
10 changes: 10 additions & 0 deletions doc/jsdoc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"plugins": [
"node_modules/jsdoc-babel"
],
"babel": {
"presets": [
"env"
]
}
}
59 changes: 37 additions & 22 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -28,24 +28,27 @@
"dependencies": {
"koa-jwt": "3.2.2",
"koa-static": "4.0.2",
"kronos-koa": "^1.2.2",
"kronos-service": "^4.8.2",
"kronos-koa": "^1.2.4",
"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",
"ava": "^0.23.0",
"babel-preset-env": "^1.6.1",
"got": "^8.0.0",
"jsdoc-babel": "^0.3.0",
"jsdoc-to-markdown": "^3.0.2",
"koa-route": "^3.2.0",
"mocha": "^3.5.3",
"markdown-doctest": "^0.9.1",
"network-address": "^1.1.2",
"nyc": "^11.3.0",
"rollup": "^0.51.8",
"rollup-plugin-babel": "^3.0.2",
"rollup-plugin-multi-entry": "^2.0.2",
"semantic-release": "^9.1.1",
"supertest": "^3.0.0",
"supertest-as-promised": "^4.0.2",
"jsdoc-to-markdown": "^3.0.1",
"markdown-doctest": "^0.9.1",
"rollup": "^0.51.5",
"nyc": "^11.3.0"
"xo": "^0.19.0"
},
"contributors": [
{
Expand All @@ -63,19 +66,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"
]
}
}
7 changes: 6 additions & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import pkg from './package.json';

export default {
plugins: [],
external: ['model-attributes', 'kronos-service', 'kronos-endpoint'],
external: [
'kronos-koa',
'model-attributes',
'kronos-service',
'kronos-endpoint'
],
input: pkg.module,

output: {
Expand Down
25 changes: 11 additions & 14 deletions src/ServiceKOA.js → src/service-koa.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
/* jslint node: true, esnext: true */
'use strict';

const http = require('http'),
https = require('https'),
url = require('url'),
pathToRegexp = require('path-to-regexp'),
Koa = require('kronos-koa'),
jwt = require('koa-jwt'),
WebSocketServer = require('ws').Server;

import { KronosKoa } from 'kronos-koa';
import { Service } from 'kronos-service';
import { SendEndpoint } from 'kronos-endpoint';
import { mergeAttributes, createAttributes } from 'model-attributes';

/**
* HTTP server with koa
*/
class ServiceKOA extends Service {
export class ServiceKOA extends Service {
static get name() {
return 'koa';
}
Expand Down Expand Up @@ -118,7 +115,7 @@ class ServiceKOA extends Service {
super(config, owner);

this.socketEndpoints = {};
this.koa = new Koa();
this.koa = new KronosKoa();

if (this.docRoot) {
this.koa.use(require('koa-static')(this.docRoot), {});
Expand Down Expand Up @@ -252,15 +249,17 @@ class ServiceKOA extends Service {
}
};

if (service.listen.address !== undefined) {
server.listen(service.listen.port, service.listen.address, handler);
} else {
if (service.listen.address === undefined) {
server.listen(service.listen.port, handler);
} else {
server.listen(service.listen.port, service.listen.address, handler);
}
}

function addressInUseHandler(e) {
if (e.code === 'EADDRINUSE') {
//console.log(`addressInUseHandler: ${e.code}`);

service.trace(level => `Address in use ${service.url} retrying...`);

// try different strategies
Expand Down Expand Up @@ -315,7 +314,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
Expand Down Expand Up @@ -410,7 +409,7 @@ class RouteSendEndpoint extends SendEndpoint {
}
}

class SocketEndpoint extends SendEndpoint {
export class SocketEndpoint extends SendEndpoint {
constructor(name, owner, path) {
super(name, owner, {
createOpposite: true
Expand Down Expand Up @@ -475,8 +474,6 @@ class SocketEndpoint extends SendEndpoint {
}
}

function registerWithManager(manager) {
export function registerWithManager(manager) {
return manager.registerServiceFactory(ServiceKOA);
}

export { registerWithManager, ServiceKOA, RouteSendEndpoint, SocketEndpoint };
60 changes: 29 additions & 31 deletions tests/failure-test.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,42 @@
/* 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.skip('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');
await ks1.stop();

try {
await ks2.start();
} catch (e) {}

t.is(ks2.state, 'failed');

await ks1.stop();
await ks2.stop();
});
19 changes: 19 additions & 0 deletions tests/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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',
'got',
'kronos-koa',
'model-attributes',
'kronos-service',
'kronos-endpoint'
],
plugins: [multiEntry()]
};
Loading

0 comments on commit 51cf0c8

Please sign in to comment.