Skip to content

Commit

Permalink
chore: Update docs and example
Browse files Browse the repository at this point in the history
  • Loading branch information
rrr523 committed Dec 18, 2023
1 parent df07ebe commit f5e675c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 13 deletions.
19 changes: 6 additions & 13 deletions packages/reed-solomon/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Compatible with [greenfield-common](https://github.com/bnb-chain/greenfield-comm
> npm install @bnb-chain/reed-solomon
```

## Usage
## Usage Examples

### Browser

Expand Down Expand Up @@ -54,20 +54,13 @@ const res = rs.encode(new Uint8Array(fileBuffer))
Using in Nodejs:

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

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

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

## API

### encode

TODO...

### encodeSegment

TODO...
* [calcute single file](./examples/singlefile.js)
* [calcute several file in a folder](./examples/folder.js)
30 changes: 30 additions & 0 deletions packages/reed-solomon/examples/folder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* eslint-disable */
const fs = require('node:fs/promises');
const path = require('node:path');
const { NodeAdapterReedSolomon } = require('../dist/node.adapter');

const rs = new NodeAdapterReedSolomon();
const folderPath = './dist';

(async () => {
async function traverse(currentPath) {
const start = Date.now();
const files = await fs.readdir(currentPath);

for (let i = 0; i < files.length; i++) {
const file = files[i];
const filePath = path.join(currentPath, file);
const stat = await fs.stat(filePath);

const start = Date.now();
const fileBuffer = await fs.readFile(filePath);
const res = await rs.encodeInWorker(__filename, Uint8Array.from(fileBuffer));
console.log('res', file, Date.now() - start, res);
}

console.log('files count: ', files.length);
console.log('total cost time: ', Date.now() - start);
}

await traverse(folderPath);
})();
16 changes: 16 additions & 0 deletions packages/reed-solomon/examples/singlefile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* eslint-disable */
const fs = require('node:fs');
const path = require('node:path');
const { NodeAdapterReedSolomon } = require('../dist/node.adapter');

const fileBuffer = fs.readFileSync('./README.md');

const rs = new NodeAdapterReedSolomon();

// single file
(async () => {
const start = Date.now();
const res = await rs.encodeInWorker(__filename, Uint8Array.from(fileBuffer));
console.log('res', res);
console.log('cost time', Date.now() - start);
})();

0 comments on commit f5e675c

Please sign in to comment.