-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: Update example, using reed solomon * chore: Update Rollup config * chore: Benchmark * docs: Update README * feat: Support WebWorker * feat: Add types * chore: Add benchmark * Merge branch 'alpha' into feat/rs_webworker
- Loading branch information
Showing
11 changed files
with
190 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@bnb-chain/reed-solomon': patch | ||
--- | ||
|
||
feat: Add Types |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
|
||
(async () => { | ||
const rs = new NodeAdapterReedSolomon(); | ||
|
||
console.time('cost worker_threads'); | ||
console.log('file size', sourceData.length / 1024 / 1024, 'm'); | ||
const res = await rs.encodeInWorker(__filename, Uint8Array.from(fileBuffer)); | ||
console.log('res', res); | ||
console.timeEnd('cost worker_threads'); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/* eslint-disable */ | ||
const fs = require('node:fs'); | ||
const path = require('node:path'); | ||
const { ReedSolomon } = require('../dist/index'); | ||
|
||
const fileBuffer = fs.readFileSync('./README.md'); | ||
|
||
(async () => { | ||
const rs = new ReedSolomon(); | ||
console.log('file size', sourceData.length / 1024 / 1024, 'm'); | ||
console.time('cost'); | ||
const res = await rs.encode(Uint8Array.from(fileBuffer)); | ||
console.log('res', res); | ||
console.timeEnd('cost'); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>RS webworker</title> | ||
</head> | ||
<body> | ||
<input type="file" id="file" /> | ||
|
||
<button id="worker-btn"> | ||
get reed solomon (webworker) | ||
</button> | ||
|
||
<script src="../dist/web.adapter.aio.js"></script> | ||
<script src="../dist/utils.aio.js"></script> | ||
<script type="module"> | ||
const fileInput = document.getElementById('file'); | ||
|
||
// use webworker | ||
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) | ||
console.time('webworker cost') | ||
console.log('file size', sourceData.length / 1024 / 1024, 'm') | ||
const rs = new WebAdapter.WebAdapterReedSolomon() | ||
const res = await rs.encodeInWorker(injectWorker, sourceData) | ||
console.log('res', res) | ||
console.timeEnd('webworker cost') | ||
} | ||
|
||
function injectWorker() { | ||
importScripts('http://localhost:9002/dist/web.adapter.aio.js'); | ||
importScripts('http://localhost:9002/dist/utils.aio.js'); | ||
|
||
const rs = new WebAdapter.WebAdapterReedSolomon(); | ||
|
||
onmessage = function (event) { | ||
const { index, chunk } = event.data; | ||
const encodeShards = rs.encodeSegment(chunk); | ||
let encodeDataHash = []; | ||
|
||
for (let i = 0; i < encodeShards.length; i++) { | ||
const priceHash = RSUtils.sha256(encodeShards[i]); | ||
encodeDataHash.push(priceHash); | ||
} | ||
|
||
postMessage({ | ||
index, | ||
segChecksum: RSUtils.sha256(chunk), | ||
encodeDataHash, | ||
}); | ||
|
||
self.close(); | ||
}; | ||
} | ||
|
||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,5 @@ | ||
declare module '@bnb-chain/reed-solomon/web.adapter' { | ||
export class WebAdapterReedSolomon { | ||
encodeInWorker( | ||
workerFn: () => void, | ||
data: Uint8Array, | ||
cdnUrls: { | ||
webAdapterUrl: string; | ||
utilsUrl: string; | ||
}, | ||
): Promise<string[]>; | ||
encodeInWorker(workerFn: () => void, data: Uint8Array): Promise<string[]>; | ||
} | ||
|
||
export function injectWorker(cdnsUrls?: { webAdapterUrl: string; utilsUrl: string }): void; | ||
} |