包详细信息

@stellar/js-xdr

stellar327kApache-2.03.1.2

Read/write XDR encoded data structures (RFC 4506)

自述文件

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.)

更新日志

Changelog

All notable changes to this project will be documented in this file. This project adheres to Semantic Versioning.

Unreleased

v3.1.2

Fixed

  • Increase robustness of compatibility across multiple js-xdr instances in an environment (#122).

v3.1.1

Fixed

  • Add compatibility with pre-ES2016 environments (like some React Native JS compilers) by adding a custom Buffer.subarray polyfill (#118).

v3.1.0

Added

  • The raw, underlying XdrReader and XdrWriter types are now exposed by the library for reading without consuming the entire stream (#116).

Fixed

  • Added additional type checks for passing a bytearray-like object to XdrReaders and improves the error with details (#116).

v3.0.1

Fixes

  • This package is now being published to @stellar/js-xdr on NPM.
  • The versions at js-xdr are now considered deprecated (#111).
  • Misc. dependencies have been upgraded (#104, #106, #107, #108, #105).

v3.0.0

Breaking Change

  • Add support for easily encoding integers larger than 32 bits (#100). This (partially) breaks the API for creating Hyper and UnsignedHyper instances. Previously, you would pass low and high parts to represent the lower and upper 32 bits. Now, you can pass the entire 64-bit value directly as a bigint or string instance, or as a list of "chunks" like before, e.g.:
-new Hyper({ low: 1, high: 1 }); // representing (1 << 32) + 1 = 4294967297n
+new Hyper(4294967297n);
+new Hyper("4294967297");
+new Hyper(1, 1);

v2.0.0

  • Refactor XDR serialization/deserialization logic (#91).
  • Replace long dependency with native BigInt arithmetics.
  • Replace lodash dependency with built-in Array and Object methods, iterators.
  • Add buffer dependency for WebPack browser polyfill.
  • Update devDependencies to more recent versions, modernize bundler pipeline.
  • Automatically grow underlying buffer on writes (#84 fixed).
  • Always check that the entire read buffer is consumed (#32 fixed).
  • Check actual byte size of the string on write (#33 fixed).
  • Fix babel-polyfill build warnings (#34 fixed).
  • Upgrade dependencies to their latest versions (#92).

v1.3.0

  • Inline and modernize the cursor dependency (#).

v1.2.0

  • Add method validateXDR(input, format = 'raw') which validates if a given XDR is valid or not. (#56).

v1.1.4

  • Remove core-js dependency (#45).

v1.1.3

  • Split out reference class to it's own file to avoid circular import (#39).

v1.1.2

  • Travis: Deploy to NPM with an env variable instead of an encrypted key
  • Instruct Travis to cache node_modules

v1.1.1

  • Updated some out-of-date dependencies

v1.1.0

Changed

  • Added ESLint and Prettier to enforce code style
  • Upgraded dependencies, including Babel to 6
  • Bump local node version to 6.14.0

v1.0.3

Changed

  • Updated dependencies
  • Improved lodash imports (the browser build should be smaller)

v1.0.2

Changed

  • bugfix: removed runtime flag from babel to make this package working in React/Webpack environments

v1.0.1

Changed

  • bugfix: padding bytes are now ensured to be zero when reading

v1.0.0

Changed

  • Strings are now encoded/decoded as utf-8

v0.0.12

Changed

  • bugfix: Hyper.fromString() no longer silently accepts strings with decimal points
  • bugfix: UnsignedHyper.fromString() no longer silently accepts strings with decimal points