Skip to content

Commit

Permalink
docs: Update rs umd way
Browse files Browse the repository at this point in the history
  • Loading branch information
rrr523 committed Dec 23, 2023
1 parent 322060f commit ec1fa96
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 39 deletions.
107 changes: 74 additions & 33 deletions doc-site/docs/FAQs/caclute-checksum-is-slow.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ You can use core lib directly in the browser and Nodejs, or you can use us to ru

When you are developing Greenfield and need to create objects, if you are sure that the files are small (maybe less than 5m or 10m), you can directly use the ESM solution:

```javascript title="core lib"
```javascript title="core lib (ESM)"
import {ReedSolomon} from '@bnb-chain/reed-solomon'

const rs = new RS.ReedSolomon();
Expand All @@ -42,42 +42,83 @@ const res = rs.encode(new Uint8Array(fileBuffer))

If the file is larger, this method may cause the page to freeze when calculating. In this case, we recommend using the worker mode:

```html title="web worker"
```html title="core lib (UMD)"
<!-- prefetch js -->
<link rel="prefetch" href="https://unpkg.com/@bnb-chain/reed-solomon/dist/index.aio.js" />
<script src="https://unpkg.com/@bnb-chain/reed-solomon/dist/index.aio.js"></script>

<body>
<input type="file" id="file" />
<button id="btn">
get reed solomon
</button>
<script type="module">
const fileInput = document.getElementById('file');
// not use webworker
document.getElementById('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)
console.time('cost')
console.log('file size', sourceData.length / 1024 / 1024, 'm')
const rs = new RS.ReedSolomon()
const res = await rs.encode(sourceData)
console.log('res', res)
console.timeEnd('cost')
}
</script>
</body>
```

```html title="web worker (only support UMD)"
<!-- 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()
// will create 6 webworker
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>

<body>
<input type="file" id="file" />

<button id="worker-btn">
get reed solomon (webworker)
</button>

<script>
const rs = new WebAdapter.WebAdapterReedSolomon()
// will create 6 webworker
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>
</body>
```

</TabItem>
Expand Down
6 changes: 0 additions & 6 deletions examples/nodejs/cases/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ console.log('objectName', objectName);
primarySpAddress: spInfo.primarySpAddress,
},
paymentAddress: ACCOUNT_ADDRESS,
tags: {
tags: [],
},
},
{
type: 'ECDSA',
Expand Down Expand Up @@ -72,9 +69,6 @@ console.log('objectName', objectName);
redundancyType: 'REDUNDANCY_EC_TYPE',
contentLength: fileBuffer.length,
expectCheckSums,
tags: {
tags: [],
},
},
{
type: 'ECDSA',
Expand Down

0 comments on commit ec1fa96

Please sign in to comment.