-
-
Notifications
You must be signed in to change notification settings - Fork 587
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
198 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* moleculer | ||
* Copyright (c) 2017 Ice Services (https://github.com/ice-services/moleculer) | ||
* MIT Licensed | ||
*/ | ||
|
||
"use strict"; | ||
|
||
const avro = require("avsc"); | ||
const BaseSerializer = require("./base"); | ||
|
||
const schema = { | ||
name: "Request", | ||
type: "record", | ||
fields: [ | ||
] | ||
}; | ||
|
||
/** | ||
* Avro serializer for Moleculer | ||
* | ||
* https://github.com/mtth/avsc | ||
* | ||
* @class AvroSerializer | ||
*/ | ||
class AvroSerializer extends BaseSerializer { | ||
|
||
/** | ||
* Creates an instance of AvroSerializer. | ||
* | ||
* @memberOf AvroSerializer | ||
*/ | ||
constructor() { | ||
super(); | ||
} | ||
|
||
/** | ||
* Serializer a JS object to string or Buffer | ||
* | ||
* @param {Object} obj | ||
* @returns {String|Buffer} | ||
* | ||
* @memberOf Serializer | ||
*/ | ||
serialize(obj) { | ||
const schema = avro.Type.forValue(obj); | ||
const t = schema.toBuffer(obj); | ||
return t; | ||
} | ||
|
||
/** | ||
* Deserialize string/Buffer to JS object | ||
* | ||
* @param {String|Buffer} str | ||
* @returns {Object} | ||
* | ||
* @memberOf Serializer | ||
*/ | ||
deserialize(str) { | ||
return JSON.parse(str); | ||
} | ||
} | ||
|
||
module.exports = AvroSerializer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* moleculer | ||
* Copyright (c) 2017 Ice Services (https://github.com/ice-services/moleculer) | ||
* MIT Licensed | ||
*/ | ||
|
||
"use strict"; | ||
|
||
const avro = require("avsc"); | ||
const BaseSerializer = require("./base"); | ||
|
||
/** | ||
* MessagePack serializer for Moleculer | ||
* | ||
* https://github.com/mcollina/msgpack5 | ||
* | ||
* @class MsgPackSerializer | ||
*/ | ||
class MsgPackSerializer extends BaseSerializer { | ||
|
||
/** | ||
* Creates an instance of MsgPackSerializer. | ||
* | ||
* @memberOf MsgPackSerializer | ||
*/ | ||
constructor() { | ||
super(); | ||
|
||
this.msgpack = require("msgpack5")(); | ||
} | ||
|
||
/** | ||
* Serializer a JS object to string or Buffer | ||
* | ||
* @param {Object} obj | ||
* @returns {String|Buffer} | ||
* | ||
* @memberOf Serializer | ||
*/ | ||
serialize(obj) { | ||
const res = this.msgpack.encode(obj); | ||
return res; | ||
} | ||
|
||
/** | ||
* Deserialize string/Buffer to JS object | ||
* | ||
* @param {String|Buffer} str | ||
* @returns {Object} | ||
* | ||
* @memberOf Serializer | ||
*/ | ||
deserialize(str) { | ||
const res = this.msgpack.decode(Buffer.from(str)); | ||
return res; | ||
} | ||
} | ||
|
||
module.exports = MsgPackSerializer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
const ServiceBroker = require("../../../src/service-broker"); | ||
const AvroSerializer = require("../../../src/serializers/avro"); | ||
|
||
|
||
describe("Test AvroSerializer constructor", () => { | ||
it("should create an empty options", () => { | ||
let serializer = new AvroSerializer(); | ||
expect(serializer).toBeDefined(); | ||
expect(serializer.serialize).toBeDefined(); | ||
expect(serializer.deserialize).toBeDefined(); | ||
}); | ||
}); | ||
|
||
describe("Test AvroSerializer serialize", () => { | ||
|
||
let broker = new ServiceBroker(); | ||
let serializer = new AvroSerializer(); | ||
serializer.init(broker); | ||
|
||
let data1 = { | ||
a: 1, | ||
b: false, | ||
c: "Test", | ||
d: { | ||
e: 55 | ||
} | ||
}; | ||
|
||
it("should convert data to JSON string", () => { | ||
const res = serializer.serialize(data1); | ||
expect(res).toBe("{\"a\":1,\"b\":false,\"c\":\"Test\",\"d\":{\"e\":55}}"); | ||
}); | ||
|
||
it("should give undefined if data is not defined", () => { | ||
const res = serializer.serialize(); | ||
expect(res).toBe(undefined); | ||
}); | ||
|
||
it("should give null if data is null", () => { | ||
const res = serializer.serialize(null); | ||
expect(res).toBe("null"); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters