Skip to content

Commit

Permalink
Add assemble option.
Browse files Browse the repository at this point in the history
The new `oneWayVoid` option makes the `void` IDL keyword useful (rather
than just being a crufty alias for `null`).

Also expose protocol emitter classes (to be consistent with only
documenting exposed classes in the API).
  • Loading branch information
mtth committed Jan 29, 2016
1 parent 8e91111 commit c228608
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 17 deletions.
20 changes: 11 additions & 9 deletions README.md
Expand Up @@ -6,8 +6,8 @@ specification](https://avro.apache.org/docs/current/spec.html).

## Features

+ [Blazingly fast and compact serialization!][benchmarks] Typically twice as
fast as JSON with much smaller encodings.
+ Blazingly [fast and compact][benchmarks] serialization! Typically faster than
JSON with much smaller encodings.
+ All the Avro goodness, including [schema evolution][schema-evolution] and
[remote procedure calls][rpc].
+ Support for [serializing arbitrary JavaScript objects][logical-types].
Expand Down Expand Up @@ -67,15 +67,16 @@ var avro = require('avsc');
.on('data', function (val) { /* Do something with the decoded value. */ });
```

+ Respond to remote procedure calls over TCP:
+ Implement a TCP server for an [IDL-defined][idl] protocol:

```javascript
var protocol = avro.parse('./Ping.avpr')
.on('ping', function (req, ee, cb) { cb(null, 'pong'); });

require('net').createServer()
.on('connection', function (con) { protocol.createListener(con); })
.listen(8000);
avro.assemble('./Ping.avdl', function (err, attrs) {
var protocol = avro.parse(attrs);
protocol.on('ping', function (req, ee, cb) { cb(null, 'pong'); });
require('net').createServer()
.on('connection', function (con) { protocol.createListener(con); })
.listen(8000);
});
```


Expand All @@ -90,3 +91,4 @@ var avro = require('avsc');
[home]: https://github.com/mtth/avsc/wiki
[rpc]: https://github.com/mtth/avsc/wiki/Advanced-usage#remote-procedure-calls
[releases]: https://github.com/mtth/avsc/releases
[idl]: https://avro.apache.org/docs/current/idl.html
2 changes: 1 addition & 1 deletion doc
Submodule doc updated from 160cef to 70c6ba
1 change: 1 addition & 0 deletions etc/browser/avsc-protocols.js
Expand Up @@ -27,6 +27,7 @@ module.exports = {
Protocol: protocols.Protocol,
Type: types.Type,
assemble: schemas.assemble,
messages: protocols.messages,
parse: parse,
types: types.builtins
};
1 change: 1 addition & 0 deletions etc/browser/avsc.js
Expand Up @@ -26,6 +26,7 @@ module.exports = {
Protocol: protocols.Protocol,
Type: types.Type,
assemble: schemas.assemble,
messages: protocols.messages,
parse: parse,
streams: containers.streams,
types: types.builtins
Expand Down
7 changes: 4 additions & 3 deletions lib/index.js
Expand Up @@ -5,8 +5,8 @@
/**
* Node.js entry point (see `etc/browser/` for browserify's entry points).
*
* It adds Node.js specific functionality (for example filesystem
* conveniences).
* It also adds Node.js specific functionality (for example a few convenience
* functions to read Avro files from the local filesystem).
*
*/

Expand All @@ -20,7 +20,7 @@ var containers = require('./containers'),


/**
* Parse a schema and return the corresponding type.
* Parse a schema and return the corresponding type or protocol.
*
*/
function parse(schema, opts) {
Expand Down Expand Up @@ -104,6 +104,7 @@ module.exports = {
createFileDecoder: createFileDecoder,
createFileEncoder: createFileEncoder,
extractFileHeader: extractFileHeader,
messages: protocols.messages,
parse: parse,
streams: containers.streams,
types: types.builtins
Expand Down
4 changes: 4 additions & 0 deletions lib/protocols.js
Expand Up @@ -1253,6 +1253,10 @@ module.exports = {
StatefulListener: StatefulListener,
StatelessListener: StatelessListener
},
messages: {
MessageEmitter: MessageEmitter,
MessageListener: MessageListener,
},
streams: {
MessageDecoder: MessageDecoder,
MessageEncoder: MessageEncoder
Expand Down
20 changes: 17 additions & 3 deletions lib/schemas.js
Expand Up @@ -159,7 +159,18 @@ function assemble(fpath, opts, cb) {
// message otherwise.
attrs.types.push(typeAttrs);
} else {
readMessage(attrs, typeAttrs);
var oneWay = false;
if (typeAttrs === 'void' || typeAttrs.type === 'void') {
if (opts.oneWayVoid) {
oneWay = true;
}
if (typeAttrs === 'void') {
typeAttrs = 'null';
} else {
typeAttrs.type = 'null';
}
}
readMessage(attrs, typeAttrs, oneWay);
}
}
}
Expand Down Expand Up @@ -188,8 +199,11 @@ function assemble(fpath, opts, cb) {
tk.next();
}

function readMessage(protocolAttrs, responseAttrs) {
function readMessage(protocolAttrs, responseAttrs, oneWay) {
var messageAttrs = {response: responseAttrs};
if (oneWay) {
messageAttrs['one-way'] = true;
}
while (tk.get().val === '@') {
readAnnotation(messageAttrs);
}
Expand Down Expand Up @@ -272,7 +286,7 @@ function assemble(fpath, opts, cb) {
}
return readUnion();
default:
var type = tk.get().val === 'void' ? 'null' : tk.get().val;
var type = tk.get().val;
tk.next();
if (Object.keys(attrs).length) {
attrs.type = type;
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "avsc",
"version": "3.3.4",
"version": "3.3.5",
"description": "Avro for JavaScript",
"homepage": "https://github.com/mtth/avsc",
"keywords": [
Expand Down
23 changes: 23 additions & 0 deletions test/test_schemas.js
Expand Up @@ -345,6 +345,29 @@ suite('schemas', function () {
});
});

test('one way void', function (done) {
var hook = createImportHook({
'1': 'protocol A { void ping(); @foo(true) void pong(); }',
});
var opts = {importHook: hook, oneWayVoid: true};
assemble('1', opts, function (err, attrs) {
assert.strictEqual(err, null);
assert.deepEqual(attrs, {
protocol: 'A',
types: [],
messages: {
ping: {response: 'null', request: [], 'one-way': true},
pong: {
response: {foo: true, type: 'null'},
request: [],
'one-way': true
}
}
});
done();
});
});

test('reset namespace', function (done) {
var hook = createImportHook({
'1': 'protocol A { import idl "2"; }',
Expand Down

0 comments on commit c228608

Please sign in to comment.