npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@stellar/js-xdr

v3.1.2

Published

Read/write XDR encoded data structures (RFC 4506)

Downloads

110,150

Readme

XDR, for Javascript

Read/write XDR encoded data structures (RFC 4506)

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 @stellar/js-xdr

Usage

You can find some examples here.

First, let's import the library:

var xdr = require('@stellar/js-xdr');
// or
import xdr from '@stellar/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 a JavaScript `Number`, so we allow creation from big-endian arrays of numbers, strings, or bigints.
var result = xdr.Hyper.fromXDR([0, 0, 0, 0, 0, 0, 0, 0]); // returns an instance of xdr.Hyper
result = new xdr.Hyper(0); // equivalent

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

// math!
var ten = result.toBigInt() + 10;
var minusone = result.toBigInt() - 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 stellar-base for an example (check out the src/generated directory)

Contributing

Please see CONTRIBUTING.md for details.

To develop and test js-xdr itself

  1. Clone the repo
git clone https://github.com/stellar/js-xdr.git
  1. Install dependencies inside js-xdr folder
cd js-xdr
npm i
  1. Install Node 14

Because we support the oldest maintenance version of Node, please install and develop on Node 14 so you don't get surprised when your code works locally but breaks in CI.

Here's out to install nvm if you haven't: https://github.com/creationix/nvm

nvm install

# if you've never installed 14.x before you'll want to re-install yarn
npm install -g yarn

If you work on several projects that use different Node versions, you might it helpful to install this automatic version manager: https://github.com/wbyoung/avn

  1. Observe the project's code style

While you're making changes, make sure to run the linter periodically to catch any linting errors (in addition to making sure your text editor supports ESLint)

yarn fmt

If you're working on a file not in src, limit your code to Node 14! See what's supported here: https://node.green/ (The reason is that our npm library must support earlier versions of Node, so the tests need to run on those versions.)