Giter Club home page Giter Club logo

js-xdr's Introduction

XDR, for Javascript

Read/write XDR encoded data structures (RFC 4506)

Travis build status Code Climate Dependency Status devDependency Status

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:

// booleans
xdr.Bool.fromXDR([0,0,0,0]) // returns false
xdr.Bool.fromXDR([0,0,0,1]) // returns true

// the inverse of `fromXDR` is `toXDR`, which returns a Buffer
xdr.Bool.toXDR(true) // returns Buffer.from([0,0,0,1])

// XDR ints and unsigned ints can be safely represented as
// a javascript number

xdr.Int.fromXDR([0xFF,0xFF,0xFF,0xFF]) // returns -1
xdr.UnsignedInt.fromXDR([0xFF,0xFF,0xFF,0xFF]) // returns 4294967295

// XDR Hypers, however, cannot be safely represented in the 53-bits
// of precision we get with javascript numbers, and so we have a custom class 
// for those numbers.  Hyper and UnsignedHyper both use 
//https://www.npmjs.com/package/long to represent the 64-bit numbers

var result = xdr.Hyper.fromXDR([0,0,0,0,0,0,0,0]) // returns an instance of xdr.Hyper

// convert the hyper to a string
result.toString() // return '0'

// math!
var ten = result.add(10)
var minusone = result.subtract(1)

// construct a number from a string
var big = xdr.Hyper.fromString("1099511627776")

// encode the hyper back into xdr
big.toXDR() // <Buffer 00 00 01 00 00 00 00 00> 

Caveats

There are a couple of caveats to be aware of with this library:

  1. We do not support quadruple precision floating point values. Attempting to read or write these values will throw errors.
  2. 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.

js-xdr's People

Contributors

bartekn avatar gutenye avatar nullstyle avatar thejollyrogers avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.