A wrapper for the C++ Cap'n Proto library
Last updated 6 months ago by antcube .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @antcube/capnp 
SYNC missed versions from official npm registry.

Cap'n Proto bindings for Node.js

This package is a hacky wrapper around the Cap'n Proto C++ library. Both the serialization and the RPC layer are exposed.

This wrapper was created primarily for use in the implementation of Sandstorm, whose frontend is written using Meteor.


This implementation is SLOW

Because v8 cannot inline or otherwise optimize calls into C++ code, and because the C++ bindings are implemented in terms of the "dynamic" API, this implementation is actually very slow. In fact, the main advantage of Cap'n Proto -- the ability to use the wire format as an in-memory format -- does not apply here, because accessor overhead of such an approach would be too high. Instead, this implementation is based on decoding messages to native Javascript objects in an upfront parsing step, and conversely initializing outgoing messages from complete Javascript objects. This actually makes the library somewhat nicer syntactically than it would be otherwise, but it is not fast.

A pure-Javascript implementation would likely be much faster. See capnp-js for such an implementation. Unfortunately, that implementation is incomplete and does not support RPC just yet. Hence, this hack was created for short-term use.

Eventually, we expect to replace this implementation with a pure-Javascript implementation, or perhaps a hybrid that at least has inlinable accessors and avoids runtime string map lookups.

The interface is not final

Especially because of the above caveat, we expect the interface may change in the future.


From NPM

npm install capnp

Note that the C++ part of the module is built during the install process. Thus, you must have development headers for Cap'n Proto and Node.js installed, along with the node-gyp tool and a GCC new enough for Cap'n Proto (at least 4.7). On Debian/Ubuntu, you can install these like so:

sudo apt-get install nodejs-dev nodejs-legacy capnproto-dev g++

Form source

git clone git://github.com/kenton/node-capnp.git
cd node-capnp
npm install

Note: node-capnp uses node-gyp for building. To manually invoke the build process, you can use node-gyp rebuild. This will put the compiled extension in build/Release/capnp.node. However, when you do require('capnp'), it will expect the module to be in, for example, bin/linux-x64-v8-3.11/capnp.node. You can manually put the module here every time you build (or symlink it), or you can use the included build script. Either npm install or node build -f will do this for you. If you are going to be hacking on node-capnp, it may be worthwhile to first do node-gyp configure and then for subsequent rebuilds you can just do node-gyp build which will be faster than a full npm install or node-gyp rebuild.

(If the above paragraph looks familiar, it's because it comes from node-fibers, whose build approach we copied.)


Loading the module

var capnp = require("capnp");

Loading schemas

// Schemas are parsed at runtime.  Once "capnp" has been imported, you can
// load schemas using require().  You can omit the ".capnp" suffix if you
// prefer.
var foo = require("./foo.capnp");

// If you'd rather not rely on hooking require() (a deprecated -- but
// probably permanent -- feature of Node), you can use capnp.import(), but
// this makes relative imports uglier:
var foo = capnp.import(__dirname + "/foo.capnp");

// capnp.import() takes an exact file name.  If you want to search the
// import path, use importSystem().  This searches the usual Node module
// locations as well as standard places to install .capnp files (i.e.
// /usr/include and /usr/local/include).
var schema = capnp.importSystem("capnp/schema.capnp");

Parsing / serializing messages

var obj = capnp.parse(foo.SomeStruct, inputBuffer);
var outputBuffer = capnp.serialize(foo.SomeStruct, obj);

Connecting to RPC servers

// connect() accepts the same address string format as kj::Network.
var conn = capnp.connect("localhost:1234");

// restore() is like EzRpc::importCap()
var cap = conn.restore("exportName", foo.MyInterface);

Making an RPC call

// Method parameters are native Javascript values, converted using the
// same rules as capnp.serialize().
var promise = cap.someMethod("foo", 123, {a: 1, b: 2});

// Methods return ES6 "Promise" objects.  The response is a Javascript
// object containing the results by name.
promise.then(function(response) {


// Pipelining is supported.

// You can explicitly close capabilities and connections if you don't want
// to wait for the garbage collector to do it.

Implementing an interface

Create a Javascript object with methods corresponding to the interface, and just pass that object anywhere where a capability is expected. Methods can return promises.

For instance, given:

interface Foo {
  foo @0 (a: Text, b: Int32) -> (c: Text);

interface Bar {
  bar @0 (foo :Foo) -> ();

You could write:

// Implement the Foo interface.
var myFoo = {
  foo: function (a, b) {
    return {c: "blah"};

// Use it in a call.

Cap'n Proto protocols often depend on explicit notification when there are no more references to an object. In C++ this would be accomplished by implementing a destructor, but of course Javascript is garbage collected. Instead, you may give your object a close() method, which will be called as soon as there are no more references.

var myFoo = {
  foo: function (a, b) {
    return {c: "blah"};
  close: () {
    console.log("client disconnected");

Note, however, that close() will be called once for every time your native object is coerced to a capability. So, if you did:


Then myFoo.close() will eventually be called twice. To prevent this, you can explicitly convert your object to a capability once upfront, and then use that:

var cap = new capnp.Capability(myFoo, mySchema.Foo);


// Close our own copy of the reference. Note that this does not
// necessarily call `myFoo.close()` -- that happens only after the
// two copies passed to the `bar()` calls above have also been closed.

In this case, the library only wraps myFoo as a capability once, and then calls close() once all copies of that reference have been dropped.

Exporting a bootstrap capability

// Connect to another server and export `myFoo` as a bootstrap capability.
var newConn = capnp.connect("localhost:4321",
                            new capnp.Capability(myFoo, mySchema.Foo));

Accepting RPC connections

Not implemented. Currently you can only be a client, not a server. (But you can implement capabilities as a client.)



cd src/node-capnp ./gen...sh [CubeKit仓库本地根目录]

Current Tags

  • 0.5.52                                ...           latest (6 months ago)

42 Versions

  • 0.5.52                                ...           6 months ago
  • 0.5.51                                ...           6 months ago
  • 0.5.50                                ...           7 months ago
  • 0.5.49                                ...           7 months ago
  • 0.5.48                                ...           7 months ago
  • 0.5.47                                ...           7 months ago
  • 0.5.46                                ...           10 months ago
  • 0.5.45                                ...           a year ago
  • 0.5.44                                ...           a year ago
  • 0.5.43                                ...           a year ago
  • 0.5.42                                ...           a year ago
  • 0.4.42                                ...           a year ago
  • 0.4.41                                ...           a year ago
  • 0.4.40                                ...           a year ago
  • 0.4.34                                ...           a year ago
  • 0.4.33                                ...           a year ago
  • 0.4.32                                ...           a year ago
  • 0.4.31                                ...           a year ago
  • 0.4.30                                ...           a year ago
  • 0.4.22                                ...           a year ago
  • 0.4.21                                ...           a year ago
  • 0.4.20                                ...           a year ago
  • 0.4.19                                ...           a year ago
  • 0.4.18                                ...           a year ago
  • 0.4.17                                ...           a year ago
  • 0.4.16                                ...           a year ago
  • 0.4.15                                ...           a year ago
  • 0.4.14                                ...           a year ago
  • 0.4.13                                ...           a year ago
  • 0.4.12                                ...           a year ago
  • 0.4.11                                ...           a year ago
  • 0.4.10                                ...           a year ago
  • 0.4.9                                ...           a year ago
  • 0.4.8                                ...           a year ago
  • 0.4.7                                ...           a year ago
  • 0.4.6                                ...           a year ago
  • 0.4.5                                ...           a year ago
  • 0.4.4                                ...           a year ago
  • 0.4.3                                ...           a year ago
  • 0.4.2                                ...           a year ago
  • 0.4.1                                ...           a year ago
  • 0.4.0                                ...           a year ago
Maintainers (1)
Today 0
This Week 0
This Month 85
Last Day 0
Last Week 0
Last Month 0
Dependencies (3)
Dev Dependencies (1)
Dependents (1)

Copyright 2014 - 2016 © taobao.org |