XDR, for Javascript
Read/write XDR encoded data structures (RFC 4506)
![devDependency Status](https://david-dm.org/stellar/js-xdr/dev-status.svg)
XDR is an open data format, specified in RFC 4506. This library provides a way to read and write XDR data from javascript. It can read/write all of the primitive XDR types and also provides facilities to define readers for the compound XDR types (enums, structs and unions)
Installation
via npm:
npm install --save js-xdr
Usage
You can find some examples here.
First, let's import the library:
var xdr = require("js-xdr");
Now, let's look at how to decode some primitive types:
xdr.Bool.fromXDR([0,0,0,0])
xdr.Bool.fromXDR([0,0,0,1])
xdr.Bool.toXDR(true)
xdr.Int.fromXDR([0xFF,0xFF,0xFF,0xFF])
xdr.UnsignedInt.fromXDR([0xFF,0xFF,0xFF,0xFF])
var result = xdr.Hyper.fromXDR([0,0,0,0,0,0,0,0])
result.toString()
var ten = result.add(10)
var minusone = result.subtract(1)
var big = xdr.Hyper.fromString("1099511627776")
big.toXDR()
Caveats
There are a couple of caveats to be aware of with this library:
- We do not support quadruple precision floating point values. Attempting to read or write these values will throw errors.
- NaN is not handled perfectly for floats and doubles. There are several forms of NaN as defined by IEEE754 and the browser polyfill for node's Buffer class seems to handle them poorly.
Code generation
js-xdr by itself does not have any ability to parse XDR IDL files and produce a parser for your custom data types. Instead, that is the responsibility of xdrgen. xdrgen will take your .x files and produce a javascript file that target this library to allow for your own custom types.
See js-stellar-base for an example (check out the src/generated directory)
Contributing
Please see CONTRIBUTING.md for details.