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

@subspace/reed-solomon-erasure.wasm

v0.2.5

Published

Rust implementation of read-solomon erasure-coding compiled to Web Assembly

Downloads

10

Readme

reed-solomon-erasure.wasm

reed-solomon-erasure Rust library compiled to WebAssembly and optimized for small size.

This library aims to provide relatively low-level, yet simple to use API to Reed-Solomon erasure coding. Library works both in modern browsers and Node.js environment with TypeScript support.

How to install

npm install @subspace/reed-solomon-erasure.wasm

How to use

import {ReedSolomonErasure} from "@subspace/reed-solomon-erasure.wasm";

const reedSolomonErasure = await ReedSolomonErasure.fromCurrentDirectory();
const SHARD_SIZE = 4;
const DATA_SHARDS = 4;
const PARITY_SHARDS = 2;

const input = Uint8Array.of(
    1, 2, 3, 4,
    1, 2, 3, 4,
    1, 2, 3, 4,
    1, 2, 3, 4,
);

const shards = new Uint8Array(SHARD_SIZE * (DATA_SHARDS + PARITY_SHARDS));
shards.set(input.slice());

console.log(
    'Encoding success: expect 0, result',
    reedSolomonErasure.encode(shards, DATA_SHARDS, PARITY_SHARDS),
);

const corruptedShards = shards.slice();
corruptedShards.set([0, 0, 0, 0], 0);
corruptedShards.set([0, 0, 0, 0], SHARD_SIZE);

console.log('Corrupted shards 0 and 1');

const result = reedSolomonErasure.reconstruct(
    corruptedShards,
    DATA_SHARDS,
    PARITY_SHARDS,
    [false, false, true, true, true, true],
);

console.log(
    'Reconstructing corrupted data shards: expect 0, result',
    result,
);
console.log(
    'Original data shards:     ',
    input.join(', '),
);
console.log(
    'Reconstructed data shards:',
    corruptedShards.slice(0, SHARD_SIZE * DATA_SHARDS).join(', '),
);

API

ReedSolomonErasure.fromCurrentDirectory(): Promise

Static automagical method that will try to detect environment (Node.js or browser) and load *.wasm file from current directory

ReedSolomonErasure.fromCurrentDirectory(source: Response): Promise

Static method for asynchronous instantiation, primarily in Browser environment, expects you to load WASM file with fetch(), usage example:

fetch('https://domain.tld/path/to/reed_solomon_erasure_bg.wasm')
    .then(ReedSolomonErasure.fromResponse)
    .then((reedSolomonErasure) => {
        // Use library here
    })

ReedSolomonErasure.fromBytes(bytes: BufferSource): ReedSolomonErasure

Static method for synchronous instantiation, primarily in Node.js environment, usage example:

const reedSolomonErasure = ReedSolomonErasure.fromBytes(
    require('fs').readFileSync(`/path/to/reed_solomon_erasure_bg.wasm`)
);
// Use library here

ReedSolomonErasure.encode(shards: Uint8Array, dataShards: number, parityShards: number): number

Takes a contiguous array of bytes that contain space for data_shards + parity_shards shards with data_shards shards containing data and fills additional parity_shards with parity information that can be later used to reconstruct data in case of corruption.

Returns one of ReedSolomonErasure.RESULT_* constants; if ReedSolomonErasure.RESULT_OK then parity shards were updated in shards in-place.

ReedSolomonErasure.reconstruct(shards: Uint8Array, dataShards: number, parityShards: number, shardsAvailable: boolean[]): number

Takes a contiguous array of bytes that contain data_shards + parity_shards shards and tries to reconstruct data shards if they are broken and whenever possible using information from shards_available (contains data_shards + parity_shards boolean values, each of which is either true if shard is not corrupted or false if it is).

Returns one of ReedSolomonErasure.RESULT_* constants; if ReedSolomonErasure.RESULT_OK then data shards were reconstructed in shards in-place.

Constants

Below constants are used to check the result of encoding/reconstruction:

  • ReedSolomonErasure.RESULT_OK
  • ReedSolomonErasure.RESULT_ERROR_TOO_FEW_SHARDS
  • ReedSolomonErasure.RESULT_ERROR_TOO_MANY_SHARDS
  • ReedSolomonErasure.RESULT_ERROR_TOO_FEW_DATA_SHARDS
  • ReedSolomonErasure.RESULT_ERROR_TOO_MANY_DATA_SHARDS
  • ReedSolomonErasure.RESULT_ERROR_TOO_FEW_PARITY_SHARDS
  • ReedSolomonErasure.RESULT_ERROR_TOO_MANY_PARITY_SHARDS
  • ReedSolomonErasure.RESULT_ERROR_TOO_FEW_BUFFER_SHARDS
  • ReedSolomonErasure.RESULT_ERROR_TOO_MANY_BUFFER_SHARDS
  • ReedSolomonErasure.RESULT_ERROR_INCORRECT_SHARD_SIZE
  • ReedSolomonErasure.RESULT_ERROR_TOO_FEW_SHARDS_PRESENT
  • ReedSolomonErasure.RESULT_ERROR_EMPTY_SHARD
  • ReedSolomonErasure.RESULT_ERROR_INVALID_SHARD_FLAGS
  • ReedSolomonErasure.RESULT_ERROR_INVALID_INDEX

Tests

Project is covered with tests that ensure things work as expected and do not regress, run them with usual npm test.

License

MIT, see license.txt