From a1e6c8630d3d885da45580ab47a4fbb4588942ee Mon Sep 17 00:00:00 2001 From: fatihcode Date: Sun, 5 Mar 2017 23:03:46 -0500 Subject: [PATCH] Maintenance --- .editorconfig | 12 ++- .gitattributes | 2 +- .gitignore | 23 ++-- .jshintrc | 28 ----- .travis.yml | 4 +- CHANGELOG.md | 48 ++++----- LICENSE.txt | 2 +- README.md | 19 ++-- index.js | 159 +++++++++++++++++++++++++++- lib/nats-transport.js | 167 ------------------------------ package.json | 55 ++++------ test/{test-lib.js => test-all.js} | 2 +- 12 files changed, 236 insertions(+), 285 deletions(-) delete mode 100644 .jshintrc delete mode 100644 lib/nats-transport.js rename test/{test-lib.js => test-all.js} (96%) diff --git a/.editorconfig b/.editorconfig index 1a950a9..ab0ec79 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,14 +1,18 @@ root = true [*] - -indent_style = space -indent_size = 2 end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = false [*.md] +trim_trailing_whitespace = false + +[*.{js,json,yml,yaml}] +indent_style = space +indent_size = 2 -trim_trailing_whitespace = false \ No newline at end of file +#[*.go] +#indent_style = tab +#indent_size = 8 diff --git a/.gitattributes b/.gitattributes index 928e4e9..20f6dde 100644 --- a/.gitattributes +++ b/.gitattributes @@ -15,4 +15,4 @@ *.bat text *.sh text -*.exe binary \ No newline at end of file +*.exe binary diff --git a/.gitignore b/.gitignore index eee21f9..e9992e2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,18 +1,15 @@ .DS_Store -.idea -.settings -.env -.coverrun -.coverdata -cover.out -xunit.xml +Thumbs.db + *.log -*.heapsnapshot -*.pprof +*.exe +*.prof +*.out + tmp/ logs/ reports/ -sources/ - -npm-debug.log -node_modules/ \ No newline at end of file +releases/ +vendor/ +node_modules/ +source/ diff --git a/.jshintrc b/.jshintrc deleted file mode 100644 index afe3735..0000000 --- a/.jshintrc +++ /dev/null @@ -1,28 +0,0 @@ -{ - "node": true, - "browser": false, - "esnext": true, - "strict": true, - "unused": true, - "undef": true, - "quotmark": "single", - "indent": 2, - "eqeqeq": true, - "immed": true, - "latedef": true, - "newcap": true, - "noarg": true, - "forin": true, - - "globals": { - }, - - "predef": [ - "describe", - "it", - "before", - "beforeEach", - "after", - "afterEach" - ] -} \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index 2c8c4fd..c350351 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,9 +1,11 @@ sudo: false + language: node_js + node_js: - "0.10" - - "0.12" - "4" + - "6" before_script: - mkdir -p gnatsd diff --git a/CHANGELOG.md b/CHANGELOG.md index a9ce510..05331c2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,40 +1,40 @@ -## Changelog +# Changelog -### 2.0.2 (2016-03-20) +## 2.0.2 (2016-03-20) -* Maintenance +- Maintenance -### 2.0.1 (2016-02-04) +## 2.0.1 (2016-02-04) -* Misc. updates -* Update devDependencies +- Misc. updates +- Update devDependencies -### 2.0.0 (2015-12-12) +## 2.0.0 (2015-12-12) -* Upgrade Nats to v0.5.x -* Support for Seneca v0.8.x -* Remove Node v0.10 support -* Add Coveralls integration -* Upgrade dev dependencies +- Upgrade Nats to v0.5.x +- Support for Seneca v0.8.x +- Remove Node v0.10 support +- Add Coveralls integration +- Upgrade dev dependencies -### 1.1.1 (2015-10-19) +## 1.1.1 (2015-10-19) -* Add `NATS_SERVERS` and `NATS_URL` env vars +- Add `NATS_SERVERS` and `NATS_URL` env vars -### 1.1.0 (2015-10-19) +## 1.1.0 (2015-10-19) -* Add servers option for NATS -* Update dev dependencies -* Update package scripts +- Add servers option for NATS +- Update dev dependencies +- Update package scripts -### 1.0.2 (2015-08-08) +## 1.0.2 (2015-08-08) -* Remove lodash +- Remove lodash -### 1.0.1 (2015-08-08) +## 1.0.1 (2015-08-08) -* Add tests +- Add tests -### 1.0.0 (2015-08-08) +## 1.0.0 (2015-08-08) -* Initial release \ No newline at end of file +- Initial release \ No newline at end of file diff --git a/LICENSE.txt b/LICENSE.txt index c887a1a..4bd9e57 100644 --- a/LICENSE.txt +++ b/LICENSE.txt @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2015 Fatih Cetinkaya (http://github.com/fatihcode/seneca-nats-transport) +Copyright (c) 2015 Fatih Cetinkaya (http://github.com/devfacet/seneca-nats-transport) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 0788fed..dd7f484 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -## seneca-nats-transport +# seneca-nats-transport [![NPM][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Coverage][coverage-image]][coverage-url] @@ -8,13 +8,13 @@ Seneca NATS transport. an open-source, high-performance, lightweight cloud messaging system. This library provides a publish-subscribe message distribution model. -### Installation +## Installation -``` +```bash npm install seneca-nats-transport ``` -### Usage +## Usage [gnatsd server](http://nats.io/download/) **should** be running. @@ -36,14 +36,13 @@ require('seneca')() .act({role: 'foo', cmd: 'bar', arg1: 1, arg2: 2}, console.log); ``` -*Run following commands* ```bash gnatsd node server.js node client.js ``` -### License +## License Licensed under The MIT License (MIT) For the full copyright and license information, please view the LICENSE.txt file. @@ -51,8 +50,8 @@ For the full copyright and license information, please view the LICENSE.txt file [npm-url]: http://npmjs.org/package/seneca-nats-transport [npm-image]: https://badge.fury.io/js/seneca-nats-transport.svg -[travis-url]: https://travis-ci.org/fatihcode/seneca-nats-transport -[travis-image]: https://travis-ci.org/fatihcode/seneca-nats-transport.svg?branch=master +[travis-url]: https://travis-ci.org/devfacet/seneca-nats-transport +[travis-image]: https://travis-ci.org/devfacet/seneca-nats-transport.svg?branch=master -[coverage-url]: https://coveralls.io/github/fatihcode/seneca-nats-transport?branch=master -[coverage-image]: https://coveralls.io/repos/github/fatihcode/seneca-nats-transport/badge.svg?branch=master \ No newline at end of file +[coverage-url]: https://coveralls.io/github/devfacet/seneca-nats-transport?branch=master +[coverage-image]: https://coveralls.io/repos/github/devfacet/seneca-nats-transport/badge.svg?branch=master diff --git a/index.js b/index.js index 7e535fe..2737663 100644 --- a/index.js +++ b/index.js @@ -1,10 +1,165 @@ /* * seneca-nats-transport - * Copyright (c) 2015 Fatih Cetinkaya (http://github.com/fatihcode/seneca-nats-transport) * For the full copyright and license information, please view the LICENSE.txt file. */ /* jslint node: true */ 'use strict'; -module.exports = require('./lib/nats-transport.js'); \ No newline at end of file +var nats = require('nats'); + +module.exports = function(options) { + + var NATS_SERVERS = process.env.NATS_SERVERS, + NATS_URL = process.env.NATS_URL; + + var seneca = this, + plugin = 'nats-transport'; + + var senecaOpts = seneca.options(), + transpUtils = seneca.export('transport/utils'); + + options = seneca.util.deepextend({ + nats: { + reconnect: true, + maxReconnectAttempts: 9999, + reconnectTimeWait: 1000 + } + }, senecaOpts.transport, options); + + // Check nats servers + if(!options.nats.servers && NATS_SERVERS) { + options.nats.servers = NATS_SERVERS.split(','); + } + + if(options.nats.servers) { + if(options.nats.servers instanceof Array) { + for(var i = 0, len = options.nats.servers.length; i < len; i++) { + if(typeof options.nats.servers[i] === 'string' && options.nats.servers[i].indexOf('nats://') !== 0) { + options.nats.servers[i] = 'nats://' + options.nats.servers[i]; + } + } + } + } + + // Check nats url + if(!options.nats.url && NATS_URL) { + options.nats.url = NATS_URL; + } + + if(options.nats.url) { + if(typeof options.nats.url === 'string' && options.nats.url.indexOf('nats://') !== 0) { + options.nats.url = 'nats://' + options.nats.url; + } + } + + // Listen hook for the transport + seneca.add({role: 'transport', type: 'nats', hook: 'listen'}, function(msg, done) { + + var seneca = this, + type = msg.type, + clientOpts = seneca.util.clean(seneca.util.deepextend({}, options[type], msg)), + clientName = 'listen-' + type, + nc = nats.connect(options[type]); + + // Connect event + nc.on('connect', function(/*client*/) { + seneca.log.info('listen', 'open', clientOpts); + }); + + // Error event + nc.on('error', function(err) { + seneca.log.error('listen', 'error', err); + }); + + // Listen topics + transpUtils.listen_topics(seneca, msg, clientOpts, function(topic) { + var topicAct = topic + '_act', + topicRes = topic + '_res'; + + // Subscribe to act topic + nc.subscribe(topicAct, function(msg) { + seneca.log.debug('listen', 'subscribe', topicAct, 'message', msg); + + // Handle request + transpUtils.handle_request(seneca, transpUtils.parseJSON(seneca, clientName, msg), clientOpts, function(out) { + // If there is an output then + if(out) { + // Publish it to response topic + nc.publish(topicRes, transpUtils.stringifyJSON(seneca, clientName, out)); + } + }); + }); + seneca.log.info('listen', 'subscribe', topicAct); + }); + + // Closer action + seneca.add({role: 'seneca', cmd: 'close'}, function(args, cb) { + seneca.log.debug('listen', 'close', clientOpts); + + nc.close(); + this.prior(args, cb); + }); + + done(); + }); + + // Client hook for the transport + seneca.add({role: 'transport', type: 'nats', hook: 'client'}, function(msg, done) { + + var seneca = this, + type = msg.type, + clientOpts = seneca.util.clean(seneca.util.deepextend({}, options[type], msg)), + clientName = 'client-' + type, + nc = nats.connect(options[type]); + + // Connect event + nc.on('connect', function(/*client*/) { + seneca.log.info('client', 'open', clientOpts); + }); + + // Error event + nc.on('error', function(err) { + seneca.log.error('client', 'error', err); + }); + + // Send is called for per topic + function send(spec, topic, sendDone) { + var topicAct = topic + '_act', + topicRes = topic + '_res'; + + // Subscribe to response topic + nc.subscribe(topicRes, function(msg) { + seneca.log.debug('client', 'subscribe', topicRes, 'message', msg); + + // Handle response + transpUtils.handle_response(seneca, transpUtils.parseJSON(seneca, clientName, msg), clientOpts); + }); + seneca.log.info('client', 'subscribe', topicRes); + + // Send message over the transport + sendDone(null, function(msg, cb) { + seneca.log.debug('client', 'publish', topicAct, 'message', msg); + + // Publish act + nc.publish(topicAct, transpUtils.stringifyJSON(seneca, clientName, transpUtils.prepare_request(seneca, msg, cb))); + }); + + // Closer action + seneca.add({role: 'seneca', cmd: 'close'}, function(args, cb) { + seneca.log.debug('client', 'close', clientOpts, 'topic', topic); + + nc.close(); + this.prior(args, cb); + }); + } + + // Use transport utils to make client + transpUtils.make_client(send, clientOpts, done); + }); + + // Return + return { + name: plugin + }; +}; \ No newline at end of file diff --git a/lib/nats-transport.js b/lib/nats-transport.js deleted file mode 100644 index 2c68b49..0000000 --- a/lib/nats-transport.js +++ /dev/null @@ -1,167 +0,0 @@ -/* - * seneca-nats-transport - * Copyright (c) 2015 Fatih Cetinkaya (http://github.com/fatihcode/seneca-nats-transport) - * For the full copyright and license information, please view the LICENSE.txt file. - */ - -/* jslint node: true */ -'use strict'; - -var nats = require('nats'); - -module.exports = function(options) { - - var NATS_SERVERS = process.env.NATS_SERVERS, - NATS_URL = process.env.NATS_URL; - - var seneca = this, - plugin = 'nats-transport'; - - var senecaOpts = seneca.options(), - transpUtils = seneca.export('transport/utils'); - - options = seneca.util.deepextend({ - nats: { - reconnect: true, - maxReconnectAttempts: 9999, - reconnectTimeWait: 1000 - } - }, senecaOpts.transport, options); - - // Check nats servers - if(!options.nats.servers && NATS_SERVERS) { - options.nats.servers = NATS_SERVERS.split(','); - } - - if(options.nats.servers) { - if(options.nats.servers instanceof Array) { - for(var i = 0, len = options.nats.servers.length; i < len; i++) { - if(typeof options.nats.servers[i] === 'string' && options.nats.servers[i].indexOf('nats://') !== 0) { - options.nats.servers[i] = 'nats://' + options.nats.servers[i]; - } - } - } - } - - // Check nats url - if(!options.nats.url && NATS_URL) { - options.nats.url = NATS_URL; - } - - if(options.nats.url) { - if(typeof options.nats.url === 'string' && options.nats.url.indexOf('nats://') !== 0) { - options.nats.url = 'nats://' + options.nats.url; - } - } - - // Listen hook for the transport - seneca.add({role: 'transport', type: 'nats', hook: 'listen'}, function(msg, done) { - - var seneca = this, - type = msg.type, - clientOpts = seneca.util.clean(seneca.util.deepextend({}, options[type], msg)), - clientName = 'listen-' + type, - nc = nats.connect(options[type]); - - // Connect event - nc.on('connect', function(/*client*/) { - seneca.log.info('listen', 'open', clientOpts); - }); - - // Error event - nc.on('error', function(err) { - seneca.log.error('listen', 'error', err); - }); - - // Listen topics - transpUtils.listen_topics(seneca, msg, clientOpts, function(topic) { - var topicAct = topic + '_act', - topicRes = topic + '_res'; - - // Subscribe to act topic - nc.subscribe(topicAct, function(msg) { - seneca.log.debug('listen', 'subscribe', topicAct, 'message', msg); - - // Handle request - transpUtils.handle_request(seneca, transpUtils.parseJSON(seneca, clientName, msg), clientOpts, function(out) { - // If there is an output then - if(out) { - // Publish it to response topic - nc.publish(topicRes, transpUtils.stringifyJSON(seneca, clientName, out)); - } - }); - }); - seneca.log.info('listen', 'subscribe', topicAct); - }); - - // Closer action - seneca.add({role: 'seneca', cmd: 'close'}, function(args, cb) { - seneca.log.debug('listen', 'close', clientOpts); - - nc.close(); - this.prior(args, cb); - }); - - done(); - }); - - // Client hook for the transport - seneca.add({role: 'transport', type: 'nats', hook: 'client'}, function(msg, done) { - - var seneca = this, - type = msg.type, - clientOpts = seneca.util.clean(seneca.util.deepextend({}, options[type], msg)), - clientName = 'client-' + type, - nc = nats.connect(options[type]); - - // Connect event - nc.on('connect', function(/*client*/) { - seneca.log.info('client', 'open', clientOpts); - }); - - // Error event - nc.on('error', function(err) { - seneca.log.error('client', 'error', err); - }); - - // Send is called for per topic - function send(spec, topic, sendDone) { - var topicAct = topic + '_act', - topicRes = topic + '_res'; - - // Subscribe to response topic - nc.subscribe(topicRes, function(msg) { - seneca.log.debug('client', 'subscribe', topicRes, 'message', msg); - - // Handle response - transpUtils.handle_response(seneca, transpUtils.parseJSON(seneca, clientName, msg), clientOpts); - }); - seneca.log.info('client', 'subscribe', topicRes); - - // Send message over the transport - sendDone(null, function(msg, cb) { - seneca.log.debug('client', 'publish', topicAct, 'message', msg); - - // Publish act - nc.publish(topicAct, transpUtils.stringifyJSON(seneca, clientName, transpUtils.prepare_request(seneca, msg, cb))); - }); - - // Closer action - seneca.add({role: 'seneca', cmd: 'close'}, function(args, cb) { - seneca.log.debug('client', 'close', clientOpts, 'topic', topic); - - nc.close(); - this.prior(args, cb); - }); - } - - // Use transport utils to make client - transpUtils.make_client(send, clientOpts, done); - }); - - // Return - return { - name: plugin - }; - -}; \ No newline at end of file diff --git a/package.json b/package.json index 204cac6..e45848b 100644 --- a/package.json +++ b/package.json @@ -2,6 +2,20 @@ "name": "seneca-nats-transport", "version": "2.0.2", "description": "Seneca NATS transport", + "main": "index.js", + "scripts": { + "lint": "jshint --reporter node_modules/jshint-stylish *.js test/*", + "lint:build": "jshint --reporter checkstyle *.js test/* > reports/jshint-checkstyle.xml", + "depcheck": "dependency-check . *.js", + "depcheck:unused": "dependency-check ./package.json --unused --no-dev *.js", + "test:unit": "mkdir -p reports/ && NODE_ENV=test multi='spec=- xunit=reports/mocha-xunit.xml' istanbul cover _mocha -- -R mocha-multi && istanbul check-coverage", + "test:coveralls": "cat reports/coverage/lcov.info | node_modules/coveralls/bin/coveralls.js", + "test": "npm run lint && npm run test:unit" + }, + "repository": { + "type": "git", + "url": "https://github.com/devfacet/seneca-nats-transport.git" + }, "keywords": [ "seneca", "nats", @@ -12,46 +26,21 @@ "subscribe", "microservices" ], - "homepage": "http://github.com/fatihcode/seneca-nats-transport", - "repository": { - "type": "git", - "url": "https://github.com/fatihcode/seneca-nats-transport.git" - }, - "bugs": { - "url": "http://github.com/fatihcode/seneca-nats-transport/issues" - }, + "author": "devfacet", "license": "MIT", - "private": false, - "author": { - "name": "fatih", - "url": "http://github.com/fatihcode" - }, - "contributors": [], - "main": "./index.js", - "scripts": { - "lint": "jshint --reporter node_modules/jshint-stylish lib/*.js test/*.js", - "lint:build": "mkdir -p reports/ && jshint --reporter checkstyle lib/*.js test/*.js > reports/jshint-checkstyle.xml", - "depcheck": "dependency-check . lib/*", - "depcheck:unused": "dependency-check ./package.json --unused --no-dev lib/*", - "test:unit": "mkdir -p reports/ && NODE_ENV=test multi='spec=- xunit=reports/mocha-xunit.xml' istanbul cover _mocha -- -R mocha-multi test/test-*.js --timeout 10000 && istanbul check-coverage", - "test:coveralls": "cat reports/coverage/lcov.info | node_modules/coveralls/bin/coveralls.js", - "test": "npm run depcheck && npm run depcheck:unused && npm run lint && npm run test:unit" - }, - "engines": { - "node": ">=0.12" - }, + "homepage": "http://github.com/devfacet/seneca-nats-transport", "dependencies": { "nats": "0.5.x" }, "devDependencies": { "chai": "3.5.x", "jshint": "2.9.x", - "jshint-stylish": "2.1.x", - "mocha": "2.4.x", - "mocha-multi": "0.7.x", + "jshint-stylish": "2.2.x", + "mocha": "3.2.x", + "mocha-multi": "0.10.x", + "dependency-check": "2.8.x", "istanbul": "0.4.x", - "dependency-check": "2.5.x", - "coveralls": "2.11.x", + "coveralls": "^2.11.16", "seneca": "1.1.x" } -} \ No newline at end of file +} diff --git a/test/test-lib.js b/test/test-all.js similarity index 96% rename from test/test-lib.js rename to test/test-all.js index 8216ccd..0ff2c9d 100644 --- a/test/test-lib.js +++ b/test/test-all.js @@ -1,5 +1,5 @@ /* jslint node: true */ -/* global describe: false, it: false */ +/* global describe: false, it: false, beforeEach: false, afterEach: false */ 'use strict'; var lib = require('../'),