1.1.3 • Published 4 months ago

@bnb-chain/reed-solomon v1.1.3

Weekly downloads
-
License
GPLv3
Repository
github
Last release
4 months ago

Reed Solomon

Lighting implementation for klauspost/reedsolomon.

Compatible with greenfield-common.

Install

> npm install @bnb-chain/reed-solomon

Usage Examples

ESM

If you use module bundler such as Webpack, Rollup, etc:

import {ReedSolomon} from '@bnb-chain/reed-solomon'

const rs = new ReedSolomon();
const res = rs.encode(new Uint8Array(fileBuffer))

Browser

Use directly in the browser via script tag:

<input type="file" id="file" />
<button id="btn">
  get reed solomon
</button>

<script src="https://cdn.jsdelivr.net/npm/@bnb-chain/reed-solomon/dist/index.aio.js"></script>
<script>
  const fileInput = document.getElementById('file');

  document.getElementById('btn').onclick = async function() {
    const selectFile = fileInput.files[0];
    const arrBuffer = await selectFile.arrayBuffer()

    if (!arrBuffer) alert('no file selected');

    const rs = new RS.ReedSolomon()
    const res = await rs.encode(sourceData)
  }
</script>

Code

Browser(WebWorker)

<!-- prefetch js -->
<link rel="prefetch" href="https://unpkg.com/@bnb-chain/reed-solomon/dist/web.adapter.aio.js" />
<link rel="prefetch" href="https://unpkg.com/@bnb-chain/reed-solomon/dist/utils.aio.js" />
<script src="https://unpkg.com/@bnb-chain/reed-solomon/dist/web.adapter.aio.js"></script>
<script>
  const rs = new WebAdapter.WebAdapterReedSolomon()
  rs.initWorkers({
    workerNum: 6,
    injectWorker,
  })

  document.getElementById('worker-btn').onclick = async function() {
    const selectFile = fileInput.files[0];
    const arrBuffer = await selectFile.arrayBuffer()
    if (!arrBuffer) alert('no file selected');

    const sourceData = new Uint8Array(arrBuffer)
    const res = await rs.encodeInWorker(sourceData)
  }

  // inject worker
  function injectWorker() {
    // or download this file and put it to your CDN server
    importScripts('https://unpkg.com/@bnb-chain/reed-solomon/dist/web.adapter.aio.js');
    importScripts('https://unpkg.com/@bnb-chain/reed-solomon/dist/utils.aio.js');

    const rs = new WebAdapter.WebAdapterReedSolomon();
    onmessage = function (event) {
      const { index, chunk } = event.data;
      const encodeShard = rs.getEncodeShard(chunk, index)
      postMessage(encodeShard);
    };
  }
</script>

Code

Nodejs

const { ReedSolomon } = require('@bnb-chain/reed-solomon')

const rs = new ReedSolomon();
const res = await rs.encode(Uint8Array.from(fileBuffer));

Code

More examples:

Nodejs(worker_threads)

Using in Nodejs:

const { NodeAdapterReedSolomon } = require('@bnb-chain/reed-solomon/node.adapter');

const fileBuffer = fs.readFileSync('./output_file');

const rs = new NodeAdapterReedSolomon();
const res = await rs.encodeInWorker(__filename, Uint8Array.from(fileBuffer))

Code

Benchmark

benchmark

1.1.3

4 months ago

1.1.1

4 months ago

1.1.2

4 months ago

1.1.0

4 months ago

1.0.3

4 months ago

1.0.2

4 months ago

1.0.1-alpha.2

5 months ago

1.0.1

5 months ago

1.0.1-alpha.1

5 months ago

1.0.1-alpha.0

5 months ago