@betit/orion
Advanced tools
Comparing version 0.1.3 to 0.1.4
@@ -1,4 +0,9 @@ | ||
const server = require('nats').connect(); | ||
const client = require('nats').connect(); | ||
const orion = require('..'); | ||
const codec = new orion.MsgPackCodec(); | ||
const opts = { encoding: 'binary' }; | ||
const server = require('nats').connect(opts); | ||
const client = require('nats').connect(opts); | ||
/////////////////////////////////////// | ||
@@ -14,17 +19,19 @@ // Request Performance | ||
server.on('connect', function() { | ||
server.on('connect', () => { | ||
let start = new Date(); | ||
server.subscribe('request.test', function(msg, reply) { | ||
server.publish(reply, 'ok'); | ||
server.subscribe('echo', (msg, reply) => { | ||
const req = codec.decode(msg); | ||
const res = codec.encode(req); | ||
server.publish(reply, res); | ||
}); | ||
// Need to flush here since using separate connections. | ||
server.flush(function() { | ||
server.flush(() => { | ||
const req = codec.encode({ a: 2, b: 3 }); | ||
for (let i = 0; i < loop; i++) { | ||
client.request('request.test', 'help', { | ||
client.request('echo', req, { | ||
max: 1 | ||
}, function() { | ||
}, msg => { | ||
const res = codec.decode(msg); | ||
received += 1; | ||
@@ -43,5 +50,3 @@ if (received === loop) { | ||
} | ||
}); | ||
}); |
const orion = require('..'); | ||
const codec = new orion.JsonCodec(); | ||
const codec = new orion.MsgPackCodec(); | ||
const server = new orion.NatsTransport({ name: 'bench' }); | ||
const server = new orion.NatsTransport({ name: 'perf' }); | ||
const client = new orion.NatsTransport(); | ||
@@ -12,2 +12,3 @@ | ||
const timeout = 20000; | ||
const loop = 100000; | ||
@@ -21,12 +22,17 @@ const hash = 1000; | ||
server.handle('request.test', (req, reply) => { | ||
reply(codec.encode({ | ||
status: 200, | ||
body: 'ok' | ||
})); | ||
server.handle('echo', (req, reply) => { | ||
const request = codec.decode(req); | ||
const res = codec.encode(request); | ||
reply(res); | ||
}); | ||
server.listen(() => { | ||
const req = codec.encode({ a: 2, b: 3 }); | ||
for (let i = 0; i < loop; i++) { | ||
client.request('request.test', codec.encode({ foo: 'bar' }), res => { | ||
client.request('echo', req, res => { | ||
if (res instanceof Error) { | ||
console.log('Error:', res.message); | ||
process.exit(); | ||
} | ||
const response = codec.decode(res); | ||
received += 1; | ||
@@ -43,4 +49,4 @@ if (received === loop) { | ||
} | ||
}); | ||
}, timeout); | ||
} | ||
}); |
## Change Log | ||
### v0.1.4 (2016/08/31) | ||
- [751bc86](https://github.com/betit/orion/commit/751bc8653f3b414aa951785fc6e10f443534347e) router middleware, transport timeout, improved test coverage (@speier) | ||
- [568b18e](https://github.com/betit/orion/commit/568b18eb6601ae5672b99974e51346adadfeec02) fixing test quickly (@speier) | ||
- [feb128d](https://github.com/betit/orion/commit/feb128d67b52eefc6ac6516056609b397fe579bd) extending router to support middlewares (wip) (@speier) | ||
- [6f496e9](https://github.com/betit/orion/commit/6f496e9c271bb91efaec9cb185b1ac8ae57863ec) Update changelog (@speier) | ||
### v0.1.3 (2016/08/30) | ||
- [e4e3476](https://github.com/betit/orion/commit/e4e3476549a751d442b44f0affc770bdfc3d0b20) release 0.1.3 (@speier) | ||
- [b1fdd0b](https://github.com/betit/orion/commit/b1fdd0bb15a8b354c260c0dc70059c26373c9301) http client timeout error fix (@speier) | ||
@@ -5,0 +12,0 @@ - [f26f406](https://github.com/betit/orion/commit/f26f406a62cfc5d6bdb0c021dc513c585e5138bb) attempt to fix coveralls (@speier) |
@@ -7,3 +7,3 @@ // $ node examples/gateway | ||
const client = module.exports = new orion.Client({ | ||
const client = new orion.Client({ | ||
codec: new orion.JsonCodec() | ||
@@ -18,4 +18,6 @@ }); | ||
gateway.use((req, reply) => { | ||
client.call(req, reply); | ||
// auth | ||
gateway.use((req, reply, next) => { | ||
// ... | ||
next(); | ||
}); | ||
@@ -28,4 +30,9 @@ | ||
// 404 | ||
gateway.use((req, reply) => { | ||
reply('Not found'); | ||
}); | ||
gateway.listen(() => { | ||
console.log(`[${process.pid}] Gateway listenning on port ${gateway.options.port}`); | ||
}); |
const orion = require('../..'); | ||
const codec = new orion.JsonCodec(); | ||
const service = module.exports = new orion.Service('demo', { | ||
codec: codec | ||
codec: new orion.JsonCodec() | ||
}); | ||
@@ -8,0 +6,0 @@ |
@@ -13,4 +13,7 @@ "use strict"; | ||
*/ | ||
constructor(options = {}) { | ||
constructor(options) { | ||
this.options = options; | ||
this.options = Object.assign({ | ||
timeout: 1000 | ||
}, options); | ||
this.codec = this.options.codec || new codec_1.DefaultBinaryCodec(); | ||
@@ -41,3 +44,3 @@ this.transport = this.options.transport || new transport_1.DefaultTransport(); | ||
if (!route) { | ||
return callback(new Error('Route is undefined')); | ||
return callback(new Error('Invalid request path')); | ||
} | ||
@@ -47,10 +50,15 @@ if (this.options.service) { | ||
} | ||
debug('calling:', route, request); | ||
debug('calling:', route); | ||
const req = this.codec.encode(request); | ||
debug('sending request:', req); | ||
this.transport.request(route, req, res => { | ||
const response = this.codec.decode(res); | ||
debug('got response:', response); | ||
callback(response.err, response.res); | ||
}); | ||
if (res instanceof Error) { | ||
callback(res); | ||
} | ||
else { | ||
const response = this.codec.decode(res); | ||
debug('got response:', response); | ||
callback(response.err, response.res); | ||
} | ||
}, this.options.timeout); | ||
} | ||
@@ -57,0 +65,0 @@ /** |
@@ -39,2 +39,3 @@ "use strict"; | ||
}); | ||
/* istanbul ignore else */ | ||
if (!this.options.cluster) { | ||
@@ -96,7 +97,8 @@ this.server.listen(this.options.port, this.options.host, callback); | ||
return (error, result, status) => { | ||
debug('reply:', error, result, status); | ||
const ierr = error instanceof Error; | ||
error = ierr ? error.message : error; | ||
const statusCode = status ? status : (ierr ? 500 : 200); | ||
debug('reply:', error, result, statusCode); | ||
const body = this.codec.encode({ | ||
err: ierr ? error.message : error, | ||
err: error, | ||
res: result | ||
@@ -103,0 +105,0 @@ }); |
@@ -23,3 +23,3 @@ /// <reference types="node" /> | ||
*/ | ||
use(route: any, callback?: Function): void; | ||
use(path: any, callback?: Function): void; | ||
/** | ||
@@ -26,0 +26,0 @@ * Close gateway. |
@@ -40,4 +40,4 @@ "use strict"; | ||
*/ | ||
use(route, callback) { | ||
this.router.use(route, callback); | ||
use(path, callback) { | ||
this.router.use(path, callback); | ||
} | ||
@@ -55,10 +55,5 @@ /** | ||
handleRequest(req, reply) { | ||
const route = this.router.get(req.path); | ||
if (route.params) { | ||
req.route = route.params; | ||
} | ||
if (route.handler) { | ||
route.handler(req, reply); | ||
} | ||
debug('route:', req.path); | ||
this.emit('request', req); | ||
this.router.dispatch(req, reply, 'route'); | ||
} | ||
@@ -65,0 +60,0 @@ } |
@@ -25,3 +25,3 @@ /** | ||
*/ | ||
request(route: string, request: any, callback: Function): void; | ||
request(route: string, request: any, callback: Function, timeout?: number): void; | ||
/** | ||
@@ -28,0 +28,0 @@ * Close connection. |
@@ -34,3 +34,3 @@ "use strict"; | ||
callback(req, res => { | ||
debug('sending response:', replyTo); | ||
debug('sending response:', replyTo, res); | ||
this.client.publish(replyTo, res); | ||
@@ -46,11 +46,12 @@ }); | ||
*/ | ||
request(route, request, callback) { | ||
debug('sending request:', route, request); | ||
request(route, request, callback, timeout = 1000) { | ||
debug('sending request:', route); | ||
const sid = this.client.request(route, request, { max: 1 }, response => { | ||
debug('got response'); | ||
debug('got response:', response); | ||
callback(response); | ||
}); | ||
// this.client.timeout(sid, 250, 1, () => { | ||
// debug('request timeout:', route); | ||
// }); | ||
this.client.timeout(sid, timeout, 1, () => { | ||
debug('request timeout:', route); | ||
callback(new Error('Transport timeout')); | ||
}); | ||
} | ||
@@ -57,0 +58,0 @@ /** |
@@ -15,3 +15,3 @@ export interface Codec { | ||
handle(route: string, callback: Function): any; | ||
request(route: string, req: any, callback: Function): any; | ||
request(route: string, req: any, callback: Function, timeout: number): any; | ||
close(): any; | ||
@@ -29,4 +29,5 @@ } | ||
service?: string; | ||
timeout?: number; | ||
codec?: Codec; | ||
transport?: Transport; | ||
} |
@@ -6,3 +6,3 @@ /** | ||
private routes; | ||
private defaultRoute; | ||
private middlewares; | ||
/** | ||
@@ -15,10 +15,12 @@ * Create new Router. | ||
*/ | ||
use(route: any, callback?: any): void; | ||
use(path: any, callback?: Function): void; | ||
/** | ||
* Find route. | ||
* Dispatch route. | ||
*/ | ||
get(route: any): { | ||
dispatch(req: any, res: any, routeParamsKey?: string): void; | ||
findRoute(path: any): { | ||
params: any; | ||
handler: Function; | ||
}; | ||
tryMiddlewares(req: any, res: any): void; | ||
} |
@@ -11,4 +11,4 @@ "use strict"; | ||
constructor() { | ||
this.defaultRoute = '__default'; | ||
this.routes = new Map(); | ||
this.middlewares = new Set(); | ||
} | ||
@@ -18,22 +18,56 @@ /** | ||
*/ | ||
use(route, callback) { | ||
if (typeof route === typeof Function) { | ||
callback = route; | ||
route = this.defaultRoute; | ||
use(path, callback) { | ||
if (typeof path === typeof Function) { | ||
this.middlewares.add(path); | ||
} | ||
this.routes.set(route, callback); | ||
else { | ||
this.routes.set(path, callback); | ||
} | ||
} | ||
/** | ||
* Find route. | ||
* Dispatch route. | ||
*/ | ||
get(route) { | ||
const match = paramify(route); | ||
dispatch(req, res, routeParamsKey = 'params') { | ||
const route = this.findRoute(req.path); | ||
req[routeParamsKey] = route.params; | ||
if (route.handler) { | ||
route.handler(req, res); | ||
} | ||
else { | ||
this.tryMiddlewares(req, res); | ||
} | ||
} | ||
// @private | ||
// match | ||
findRoute(path) { | ||
const match = paramify(path); | ||
const entry = [...this.routes].find(([r]) => match(r)); | ||
return { | ||
params: match.params, | ||
handler: entry ? entry[1] : this.routes.get(this.defaultRoute) | ||
handler: entry ? entry[1] : null | ||
}; | ||
} | ||
// @private | ||
// middlewares | ||
tryMiddlewares(req, res) { | ||
let i = 0; | ||
const mws = [...this.middlewares]; | ||
const next = () => { | ||
if (i <= mws.length) { | ||
const mw = mws[i++]; | ||
mw(req, (...args) => { | ||
// stop the loop in case of response | ||
i = mws.length + 1; | ||
res(...args); | ||
}, () => { | ||
process.nextTick(() => { | ||
next(); | ||
}); | ||
}); | ||
} | ||
}; | ||
next(); | ||
} | ||
} | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.default = Router; |
{ | ||
"name": "@betit/orion", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"description": "Pluggable microservice framework", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
# Orion | ||
[![License MIT](https://img.shields.io/badge/license-MIT-blue.svg)](http://opensource.org/licenses/MIT) | ||
[![Build Status](https://travis-ci.org/betit/orion.svg?branch=master)](https://travis-ci.org/betit/orion) | ||
[![Coverage Status](https://coveralls.io/repos/github/betit/orion/badge.svg?branch=master)](https://coveralls.io/github/betit/orion?branch=master) | ||
[![Build Status][travis-image]][travis-url] | ||
[![Coverage Status][coveralls-image]][coveralls-url] | ||
[![NPM Version][npm-image]][npm-url] | ||
@@ -72,1 +72,12 @@ Orion is a pluggable framework for microservices. | ||
``` | ||
## License | ||
[MIT](LICENSE) | ||
[travis-image]: https://img.shields.io/travis/betit/orion.svg | ||
[travis-url]: https://travis-ci.org/betit/orion | ||
[coveralls-image]: https://img.shields.io/coveralls/betit/orion.svg | ||
[coveralls-url]: https://coveralls.io/github/betit/orion | ||
[npm-image]: https://img.shields.io/npm/v/@betit/orion.svg | ||
[npm-url]: https://www.npmjs.com/package/@betit/orion |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
51079
1487
83
1
52