|
| 1 | +# bzip2-wasm |
| 2 | + |
| 3 | +(de)compress buffers in node and browser using wasm-compiled |
| 4 | +[bzip2](https://sourceware.org/bzip2/). |
| 5 | + |
| 6 | +## install |
| 7 | + |
| 8 | + $ npm install bzip2-wasm |
| 9 | + |
| 10 | +## example |
| 11 | +```javascript |
| 12 | +import BZip2 from "wasm-bzip2"; |
| 13 | +import fs from 'fs'; |
| 14 | + |
| 15 | +const bzip2 = new BZip2(); |
| 16 | + |
| 17 | +await bzip2.init(); |
| 18 | + |
| 19 | +const licenseText = fs.readFileSync('./LICENSE'); |
| 20 | +console.log('original length:', licenseText.length); |
| 21 | + |
| 22 | +const compressed = bzip2.compress(licenseText); |
| 23 | +console.log('compressed length:', compressed.length); |
| 24 | + |
| 25 | +const decompressed = bzip2.decompress(compressed, licenseText.length); |
| 26 | +console.log('decompressed length:', decompressed.length); |
| 27 | +``` |
| 28 | + |
| 29 | +## api |
| 30 | + |
| 31 | +### bzip2 = new BZip2() |
| 32 | + |
| 33 | +### async bzip2.init() |
| 34 | +fetch and load the wasm. required for following methods. |
| 35 | + |
| 36 | +### bzip2.compress(decompressed, blockSize = 5, compressedSize = decompressed.length) |
| 37 | +compress an array of bytes. |
| 38 | + |
| 39 | +`decompressed` should be array-like |
| 40 | +([TypedArray](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray) |
| 41 | +or regular) of bytes. |
| 42 | + |
| 43 | +`blockSize` should be a Number between 1-9 to determine block size (multiplied |
| 44 | +by 100k). default is 5 (500k). |
| 45 | + |
| 46 | +`compressedLength` should be a Number that is at least large or larger |
| 47 | +than the resulting compressed data. default is `decompressed.length`. |
| 48 | + |
| 49 | +returns a |
| 50 | +[`Uint8Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array) |
| 51 | +of compressed data. |
| 52 | + |
| 53 | +### bzip2.decompress(compressed = [], decompressedLength = 0) |
| 54 | +decompress a compressed array of bytes. |
| 55 | + |
| 56 | +`compressed` should be array-like |
| 57 | +([TypedArray](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray) |
| 58 | +or regular) of bytes. |
| 59 | + |
| 60 | +`decompressedLength` should be a Number that is at least large or larger |
| 61 | +than the resulting decompressed data. |
| 62 | + |
| 63 | +returns a |
| 64 | +[`Uint8Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array) |
| 65 | +of decompressed data. |
| 66 | + |
| 67 | +## license |
| 68 | +This program, "bzip2", the associated library "libbzip2", and all |
| 69 | +documentation, are copyright (C) 1996-2019 Julian R Seward. All |
| 70 | +rights reserved. |
| 71 | + |
| 72 | +Redistribution and use in source and binary forms, with or without |
| 73 | +modification, are permitted provided that the following conditions |
| 74 | +are met: |
| 75 | + |
| 76 | +1. Redistributions of source code must retain the above copyright |
| 77 | + notice, this list of conditions and the following disclaimer. |
| 78 | + |
| 79 | +2. The origin of this software must not be misrepresented; you must |
| 80 | + not claim that you wrote the original software. If you use this |
| 81 | + software in a product, an acknowledgment in the product |
| 82 | + documentation would be appreciated but is not required. |
| 83 | + |
| 84 | +3. Altered source versions must be plainly marked as such, and must |
| 85 | + not be misrepresented as being the original software. |
| 86 | + |
| 87 | +4. The name of the author may not be used to endorse or promote |
| 88 | + products derived from this software without specific prior written |
| 89 | + permission. |
| 90 | + |
| 91 | +THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS |
| 92 | +OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 93 | +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 94 | +ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 95 | +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 96 | +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 97 | +GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 98 | +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 99 | +WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 100 | +NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 101 | +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 102 | + |
| 103 | + |
| 104 | +bzip2/libbzip2 version 1.0.8 of 13 July 2019 |
0 commit comments