heatshrink-compression-ts 0.1.1 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +20 -19
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# Typescript Decoder for Heatshrink
|
|
1
|
+
# Typescript Encoder and Decoder for Heatshrink
|
|
2
2
|
|
|
3
3
|
[](https://travis-ci.org/iotile/heatshrink-ts)
|
|
4
4
|
[](https://coveralls.io/github/iotile/heatshrink-ts?branch=master)
|
|
@@ -6,42 +6,43 @@
|
|
|
6
6
|
[](https://www.npmjs.com/package/heatshrink-ts)
|
|
7
7
|
|
|
8
8
|
### Introduction
|
|
9
|
+
Forked from [iotile/heatshrink-ts](https://github.com/iotile/heatshrink-ts)
|
|
10
|
+
|
|
11
|
+
Thank you [@Tim Burke](https://github.com/timburke)
|
|
9
12
|
|
|
10
13
|
Heatshrink is a compression library that can be used in very low resource microcontroller devices. It is based on LZSS encoding, which
|
|
11
14
|
looks for repeated strings of characters and replaces them with references to a previous occurence rather than repeating them.
|
|
12
15
|
|
|
13
16
|
You can read more details at the repository for the original C implementation of [heatshrink](https://github.com/atomicobject/heatshrink/).
|
|
14
17
|
|
|
15
|
-
This typescript package only implements the heatshrink decoding process so it can decode compressed data that it receives from
|
|
16
|
-
a device using the heatshrink library. It is written in typescript and distributed on NPM for easy installation and usage
|
|
18
|
+
~~This typescript package only implements the heatshrink decoding process so it can decode compressed data that it receives from
|
|
19
|
+
a device using the heatshrink library. It is written in typescript and distributed on NPM for easy installation and usage.~~
|
|
20
|
+
|
|
21
|
+
**Now there is encoder too.**
|
|
17
22
|
|
|
18
23
|
### Installation and Basic Usage
|
|
19
24
|
|
|
20
25
|
```shell
|
|
21
|
-
npm install heatshrink-ts
|
|
26
|
+
npm install heatshrink-compression-ts
|
|
22
27
|
```
|
|
23
28
|
|
|
24
29
|
The primary class is the `HeatshrinkDecoder` object that can take in compressed data and turn it back into uncompressed data.
|
|
25
30
|
|
|
26
31
|
```typescript
|
|
32
|
+
import { HeatshrinkEncoder } from "../src/heatshrink-encoder";
|
|
33
|
+
import { HeatshrinkDecoder } from "../src/heatshrink-decoder";
|
|
27
34
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
const WINDOW_BITS = 8;
|
|
31
|
-
const LOOKAHEAD_BITS = 4;
|
|
32
|
-
const INPUT_BUFFER_LENGTH = 64;
|
|
33
|
-
|
|
34
|
-
// Heatshrink Encoded form of the ASCII string 'this is a test'
|
|
35
|
-
let encodedInput = new Uint8Array([0xba, 0x5a, 0x2d, 0x37, 0x39, 0x00, 0x08, 0xac, 0x32, 0x0b, 0xa5, 0x96, 0xe7, 0x74]);
|
|
36
|
-
|
|
37
|
-
let decoder = new HeatshrinkDecoder(WINDOW_BITS, LOOKAHEAD_BITS, INPUT_BUFFER_LENGTH);
|
|
38
|
-
decoder.process(encodedInput);
|
|
35
|
+
const encoder = new HeatshrinkEncoder(12, 4);
|
|
36
|
+
const decoder = new HeatshrinkDecoder(12, 4, 1024);
|
|
39
37
|
|
|
40
|
-
|
|
38
|
+
const text = "This is a test. This is a test. This is a test.";
|
|
39
|
+
const input = Buffer.from(text, "utf8");
|
|
40
|
+
const compressed = encoder.compress(input);
|
|
41
|
+
console.log("Compressed bytes:", compressed.byteLength);
|
|
41
42
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
console.log("
|
|
43
|
+
decoder.process(compressed);
|
|
44
|
+
const out = decoder.getOutput();
|
|
45
|
+
console.log(Buffer.from(out).toString("utf8"), text);
|
|
45
46
|
```
|
|
46
47
|
|
|
47
48
|
There are 2 key parameters that need to match between the encoder and decoder:
|