Skip to content
Permalink
Browse files

Using ms-logger instead of injected logger

  • Loading branch information
idanstark42 committed Dec 12, 2019
1 parent 02f2bdf commit afb2326fa81becddcbf5e3e05da958109ed191f4
Showing with 75 additions and 54 deletions.
  1. +36 −13 README.md
  2. +2 −0 index.js
  3. +8 −6 lib/logging.js
  4. +0 −5 lib/messenger_factory.js
  5. +12 −0 test/index.test.js
  6. +17 −9 test/lib/logging.test.js
  7. +0 −21 test/lib/messenger_factory.test.js
@@ -5,16 +5,34 @@
[![Build status](https://ci.appveyor.com/api/projects/status/0ya8dl62755nn01g/branch/master?svg=true)](https://ci.appveyor.com/project/2roy999/ms-messenger/branch/master)
[![GitHub](https://img.shields.io/github/license/FirstLegoLeague/ms-messenger.svg)](https://github.com/FirstLegoLeague/ms-messenger/blob/master/LICENSE)

## FIRST LEGO Leage messenger
A MHub client working by the _FIRST_ LEGO League TMS [Module Standard](https://github.com/FirstLegoLeagueIL/architecture/blob/master/module-standard/v1.0-SNAPSHOT.md#log-messages).
# FIRST LEGO Leage messenger
A [MHub](https://www.npmjs.com/package/mhub) client working by the _FIRST_ LEGO League TMS [Module Standard](https://github.com/FirstLegoLeagueIL/architecture/blob/master/module-standard/v1.0-SNAPSHOT.md#log-messages).

### Usage
## The logic of this module
This module was meant to serve as an extendable messenger which already works by the Module Standard and allows you to easily listen to and send messages. It gives you all the functionality needed for a Mhub messenger running in node or in browser.

### In node
The messenger givven in node is fully [correlated](https://github.com/FirstLegoLeague/architecture/blob/master/module-standard/v1.0-SNAPSHOT.md#cross-module-correlations) and logged using [ms-logger](https://www.npmjs.com/package/@first-lego-league/ms-logger). It also has a client-id which recognizes it against other clients, and is sent in the headers for recognition.

### In browser
In broswer many of these feature are not required or needed. So the client only has a client-id, and isn't correlated or logged.

### Authentication
All messengers have the ability to be authenticated using MHub credentials.

### Ignoring
All messengers have the `ignoreNextMessageOfTopic` method, which tells them to ignore the next message of a givven topic.

### Stay Alive
All messengers have a failsafe mechanism that makes sure they restart when they get disconnected.

## Usage

```javascript
const { Messenger } = require('@first-lego-league/ms-messenger')
const { createMessenger } = require('@first-lego-league/ms-messenger')
// Create a new object
const messenger = new Messenger({/* options... */})
const messenger = createMessenger({/* options... */})
messenger.listen('some:topic', (messageData, message) => {
// do something
@@ -23,13 +41,18 @@ messenger.listen('some:topic', (messageData, message) => {
messenger.send('some:topic', { data: {/* data... */} })
```

#### options
### Options

| **option** | **meaning** | **default** |
| **option** | **meaning** | **options** | **default** |
|--|--|--|
| mhubURI | The URI of the MHub server | `process.env.MHUB_URI` |
| node | The node to which the messenger is connected | `'default'` |
| clientId| An ID for the client | Random base64 text |
| reconnectTimeout | Time between connection attempts | 10 seconds |
| logger | A logger object of the pattern `{ debug, info, warn, error, fatal }`, can axcept `ms-logger` | empty logger |
| credentials | An object of the pattern `{ username, password }` to be used to login to the node. If none is specified, no login will be performed | none |
| mhubURI | The URI of the MHub server | Any valid WebSockets URI | `process.env.MHUB_URI` |
| node | The node to which the messenger is connected | String | `'default'` |
| clientId | An ID for the client | String | Random base64 string |
| reconnectTimeout | Time between connection attempts | Any number | 10 seconds |
| credentials | An object of the pattern `{ username, password }` to be used to login to the node. If none is specified, no login will be performed | Object with fields `username` and `password` | undefined |

## Contribution
To contribute to this repository, simply create a PR and set one of the Code Owners to be a reviewer.
Please notice the linting and UT, because they block merge.
Keep the package lightweight and easy to use.
Thank you for contributing!
@@ -2,6 +2,7 @@ const { MClient } = require('mhub/dist/src/nodeclient')

const { createMessenger } = require('./lib/messenger_factory')
const { correlateMesseger } = require('./lib/correlation')
const { logMessengerEvents } = require('./lib/logging')

const DEFAULT_OPTIONS = {
mhubURI: process.env.MHUB_URI,
@@ -14,6 +15,7 @@ exports.createMessenger = options => {
const messenger = createMessenger(new MClient(options.mhubURI), options)

correlateMesseger(messenger)
logMessengerEvents(messenger)

return messenger
}
@@ -1,9 +1,11 @@
exports.logMessengerEvents = (messenger, logger) => {
messenger.logger = logger
const { Logger } = require('@first-lego-league/ms-logger')

messenger._connectionPreactions.push(() => logger.debug('Connecting to MHub'))
messenger._connectionPostactions.push(() => logger.debug('Connected to MHub'))
exports.logMessengerEvents = messenger => {
messenger.logger = new Logger()

messenger.client.on('error', msg => logger.error(`Unable to connect to MHub: ${msg}`))
messenger.client.on('close', msg => logger.error(`Disconnected from MHub: ${msg}`))
messenger._connectionPreactions.push(() => messenger.logger.debug('Connecting to MHub'))
messenger._connectionPostactions.push(() => messenger.logger.debug('Connected to MHub'))

messenger.client.on('error', msg => messenger.logger.error(`Unable to connect to MHub: ${msg}`))
messenger.client.on('close', msg => messenger.logger.error(`Disconnected from MHub: ${msg}`))
}
@@ -1,5 +1,4 @@
const { Messenger } = require('./messenger')
const { logMessengerEvents } = require('./logging')
const { identifyMesseger } = require('./client_identity')
const { ignoreByTopic } = require('./ignoring')
const { keepAlive } = require('./stay_alive')
@@ -8,10 +7,6 @@ const { authenticateMesseger } = require('./authentication')
exports.createMessenger = (client, options) => {
const messenger = new Messenger(client, options)

if (options.logger) {
logMessengerEvents(messenger, options.logger)
}

identifyMesseger(messenger)
ignoreByTopic(messenger)
keepAlive(messenger)
@@ -6,6 +6,7 @@ chai.use(spies)

let mclient
let mclientSpy
let loggingSpy
let messengerFactorySpy
let correlateMessegerSpy

@@ -24,6 +25,11 @@ const { createMessenger } = proxyquire('../', {
correlateMesseger: function () {
return correlateMessegerSpy.apply(this, arguments)
}
},
'./lib/logging': {
logMessengerEvents: function () {
return loggingSpy.apply(this, arguments)
}
}
})

@@ -34,6 +40,7 @@ describe('ms-messenger in client', () => {
mclient = { id: 'client' }
messengerFactorySpy = chai.spy(() => mclient)
correlateMessegerSpy = chai.spy(() => { })
loggingSpy = chai.spy(() => { })
mclientSpy = chai.spy(() => mclient)
})

@@ -64,6 +71,11 @@ describe('ms-messenger in client', () => {
expect(messengerFactorySpy).to.have.been.called()
})

it('enables logging', () => {
const messenger = createMessenger(mclient, { })
expect(loggingSpy).to.have.been.called.with(messenger)
})

it('calls correlateMesseger with the client', () => {
const mhubURI = 'ws://some.uri'
const options = { mhubURI }
@@ -1,18 +1,26 @@
const chai = require('chai')
const proxyquire = require('proxyquire')
const spies = require('chai-spies')

chai.use(spies)

let logger
let clientEventListenSpy
let loggerDebugSpy
let loggerErrorSpy

const { logMessengerEvents } = require('../../lib/logging')
const { logMessengerEvents } = proxyquire('../../lib/logging', {
'@first-lego-league/ms-logger': {
Logger: function () {
return logger
}
}
})

const expect = chai.expect

describe('ignoring', () => {
let messenger, logger
describe('loggin', () => {
let messenger

beforeEach(() => {
messenger = {
@@ -42,34 +50,34 @@ describe('ignoring', () => {
})

it('adds the givven logger as a logger field to the client', () => {
logMessengerEvents(messenger, logger)
logMessengerEvents(messenger)
expect(messenger.logger).to.equal(logger)
})

it('adds one action to connectionPreactions', () => {
logMessengerEvents(messenger, logger)
logMessengerEvents(messenger)
expect(messenger._connectionPostactions.length).to.equal(1)
})

it('adds an action to connectionPreactions which logs pre connection message in debug level', () => {
logMessengerEvents(messenger, logger)
logMessengerEvents(messenger)
messenger._connectionPreactions[0]()
expect(loggerDebugSpy).to.have.been.called.with('Connecting to MHub')
})

it('adds one action to connectionPostactions', () => {
logMessengerEvents(messenger, logger)
logMessengerEvents(messenger)
expect(messenger._connectionPostactions.length).to.equal(1)
})

it('adds an action to connectionPostactions which logs post connection message in debug level', () => {
logMessengerEvents(messenger, logger)
logMessengerEvents(messenger)
messenger._connectionPostactions[0]()
expect(loggerDebugSpy).to.have.been.called.with('Connected to MHub')
})

it('adds an action to the client `error` event', () => {
logMessengerEvents(messenger, logger)
logMessengerEvents(messenger)
expect(messenger.client._handlers.error).to.not.be.undefined
})

@@ -8,7 +8,6 @@ const CLIENT = { }

let mclient
let messengerSpy
let loggingSpy
let identitySpy
let ignoringSpy
let stayAliveSpy
@@ -20,11 +19,6 @@ const { createMessenger } = proxyquire('../../lib/messenger_factory', {
return messengerSpy.apply(this, arguments)
}
},
'./logging': {
logMessengerEvents: function () {
return loggingSpy.apply(this, arguments)
}
},
'./client_identity': {
identifyMesseger: function () {
return identitySpy.apply(this, arguments)
@@ -53,7 +47,6 @@ describe('ms-messenger in client', () => {
beforeEach(() => {
mclient = { id: 'client' }
messengerSpy = chai.spy(() => mclient)
loggingSpy = chai.spy(() => { })
identitySpy = chai.spy(() => { })
ignoringSpy = chai.spy(() => { })
stayAliveSpy = chai.spy(() => { })
@@ -70,20 +63,6 @@ describe('ms-messenger in client', () => {
expect(messengerSpy).to.have.been.called.with(CLIENT, options)
})

it('enables logging if options has a logger', () => {
const options = { logger: { } }

const messenger = createMessenger(CLIENT, options)
expect(loggingSpy).to.have.been.called.with(messenger, options.logger)
})

it('does not enable logging if options does not have a logger', () => {
const options = { }

createMessenger(CLIENT, options)
expect(loggingSpy).to.not.have.been.called()
})

it('enables client identity', () => {
const messenger = createMessenger(CLIENT, { })
expect(identitySpy).to.have.been.called.with(messenger)

0 comments on commit afb2326

Please sign in to comment.
You can’t perform that action at this time.