Skip to content

Commit

Permalink
split client and server encoder
Browse files Browse the repository at this point in the history
  • Loading branch information
StarpTech committed Mar 18, 2018
1 parent e0b46a7 commit 93acb93
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 141 deletions.
13 changes: 8 additions & 5 deletions docs/codec.md
Expand Up @@ -5,15 +5,18 @@ sidebar_label: Codec
---

You can define custom payload `Encoder` and `Decoder` to support other formats like [MessagePack](https://msgpack.org/index.html).
This will reset the server and client codecs.
This will reset the default `JSON` codec.

```js
function encode(msg, isServerEncoding) {
function encode(msg) {
return ''
}
function decode(msg, isServerDecoding) {
function decode(msg) {
return ''
}
hemera.setEncoder(encode)
hemera.setDecoder(encode)
hemera.setClientEncoder(encode)
hemera.setClientDecoder(encode)

hemera.setServerEncoder(encode)
hemera.setServerDecoder(encode)
```
2 changes: 1 addition & 1 deletion docs/plugin.md
Expand Up @@ -7,7 +7,7 @@ sidebar_label: Create a plugin
Hemera's plugin system based on the powerful [Avvio](https://github.com/mcollina/avvio) package. Avvio is fully reentrant and graph-based. You can load components/plugins within plugins, and be still sure that things will happen in the right order.

## Plugin helper library
Before we get into the plugin system of hemera, you have to install a package called [`hemera-plugin`](https://github.com/hemerajs/hemera/tree/master/packages/hemera-plugin). This package can do some things for you:
Before we get into the plugin system of hemera you have to install a package called [`hemera-plugin`](https://github.com/hemerajs/hemera/tree/master/packages/hemera-plugin). This package can do some things for you:

- Check the bare-minimum version of Hemera
- Provide consistent interface to register plugins even when the api is changed
Expand Down
6 changes: 4 additions & 2 deletions packages/hemera-msgpack/index.js
Expand Up @@ -28,8 +28,10 @@ function hemeraMsgpack(hemera, opts, done) {
}
}

hemera.setDecoder(decode)
hemera.setEncoder(encode)
hemera.setClientDecoder(decode)
hemera.setClientEncoder(encode)
hemera.setServerDecoder(decode)
hemera.setServerEncoder(encode)

done()
}
Expand Down
2 changes: 1 addition & 1 deletion packages/hemera/lib/extensions.js
Expand Up @@ -146,7 +146,7 @@ function onClientPostRequest(context, next) {
* @returns
*/
function onServerPreRequest(context, req, res, next) {
let m = context._decoder(context._request.payload, context._isServer)
let m = context._serverDecoder(context._request.payload)

if (m.error) {
next(m.error)
Expand Down
33 changes: 25 additions & 8 deletions packages/hemera/lib/index.js
Expand Up @@ -98,8 +98,10 @@ class Hemera extends EventEmitter {
// keep reference to root hemera instance
this._root = this

this._encoder = DefaultEncoder.encode
this._decoder = DefaultDecoder.decode
this._clientEncoder = DefaultEncoder.encode
this._clientDecoder = DefaultDecoder.decode
this._serverEncoder = DefaultEncoder.encode
this._serverDecoder = DefaultDecoder.decode
this._schemaCompiler = null

// errio settings
Expand Down Expand Up @@ -239,16 +241,31 @@ class Hemera extends EventEmitter {
*
* @param {*} fn
*/
setDecoder(fn) {
this._decoder = fn
setServerDecoder(fn) {
this._serverDecoder = fn
}

/**
*
* @param {*} fn
*/
setEncoder(fn) {
this._encoder = fn
setServerEncoder(fn) {
this._serverEncoder = fn
}
/**
*
* @param {*} fn
*/
setClientDecoder(fn) {
this._clientDecoder = fn
}

/**
*
* @param {*} fn
*/
setClientEncoder(fn) {
this._clientEncoder = fn
}

/**
Expand Down Expand Up @@ -1048,7 +1065,7 @@ class Hemera extends EventEmitter {
return
}

const res = self._decoder(response, self._isServer)
const res = self._clientDecoder(response)
self._response.payload = res.value
self._response.error = res.error

Expand Down Expand Up @@ -1212,7 +1229,7 @@ class Hemera extends EventEmitter {
*/
_onPreRequestCompleted(err) {
const self = this
let m = self._encoder(self._message, self._isServer)
let m = self._clientEncoder(self._message)

// encoding issue
if (m.error) {
Expand Down
4 changes: 2 additions & 2 deletions packages/hemera/lib/reply.js
Expand Up @@ -186,7 +186,7 @@ class Reply {
error: self.error ? Errio.toObject(self.error) : null
}

let msg = self.hemera._encoder(message, self.hemera._isServer)
let msg = self.hemera._serverEncoder(message)

if (msg.error) {
let internalError = new Errors.ParseError(
Expand All @@ -196,7 +196,7 @@ class Reply {
self.hemera.emit('serverResponseError', msg.error)
message.error = Errio.toObject(msg.error)
message.result = null
msg = self.hemera._encoder(message, self.hemera._isServer)
msg = self.hemera._serverEncoder(message)
}

return msg
Expand Down
58 changes: 0 additions & 58 deletions test/hemera/decoding.spec.js

This file was deleted.

41 changes: 0 additions & 41 deletions test/hemera/encoding.spec.js
Expand Up @@ -57,45 +57,4 @@ describe('Default JSON encoder', function() {
)
})
})

it('Should pass encoding context', function(done) {
const nats = require('nats').connect(authUrl)

const hemera = new Hemera(nats)
const calls = []

hemera.setEncoder((msg, isServerEncoding) => {
calls.push(isServerEncoding)
return {
value: JSON.stringify(msg)
}
})

hemera.ready(() => {
hemera.add(
{
topic: 'math',
cmd: 'add'
},
(req, cb) => {
cb(null, req.a + req.b)
}
)

hemera.act(
{
topic: 'math',
cmd: 'add',
a: 1,
b: 2
},
function(err, res) {
expect(err).to.be.not.exists()
expect(res).to.be.equals(3)
expect(calls).to.be.equals([false, true]) // Client -> Server
hemera.close(done)
}
)
})
})
})
32 changes: 9 additions & 23 deletions test/hemera/error-handling.spec.js
Expand Up @@ -265,14 +265,9 @@ describe('Error handling', function() {
}
)

hemera.setDecoder((msg, isServer) => {
if (!isServer) {
return {
error: new Error('TEST')
}
}
hemera.setClientDecoder(msg => {
return {
value: JSON.parse(msg)
error: new Error('TEST')
}
})

Expand Down Expand Up @@ -318,14 +313,9 @@ describe('Error handling', function() {
}
)

hemera.setDecoder((msg, isServer) => {
if (isServer) {
return {
error: new Error('TEST')
}
}
hemera.setServerDecoder(msg => {
return {
value: JSON.parse(msg)
error: new Error('TEST')
}
})

Expand Down Expand Up @@ -361,9 +351,10 @@ describe('Error handling', function() {
spy()
})

hemera.setEncoder((msg, isServer) => {
hemera.setServerEncoder(msg => {
call++
if (isServer && call === 2) {
if (call === 1) {
// because the second call is needed to respond it back
return {
error: new Error('TEST')
}
Expand Down Expand Up @@ -415,14 +406,9 @@ describe('Error handling', function() {
spy()
})

hemera.setEncoder((msg, isServer) => {
if (!isServer) {
return {
error: new Error('TEST')
}
}
hemera.setClientEncoder(msg => {
return {
value: JSON.stringify(msg)
error: new Error('TEST')
}
})

Expand Down

0 comments on commit 93acb93

Please sign in to comment.