lzma1 0.0.4 → 0.0.6

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 CHANGED
@@ -1,37 +1,43 @@
1
1
  # lzma1
2
2
 
3
- This is a simplified [fork][fork-link] of [Nathan Rugg's][fork-author] package.
3
+ This is a [fork][fork-link] of [Nathan Rugg's][fork-author] LZMA-JS package.
4
4
 
5
5
  [fork-link]: https://github.com/LZMA-JS/LZMA-JS
6
6
  [fork-author]: https://github.com/nmrugg
7
7
 
8
+ ## Why
9
+
10
+ There are many LZMA implementations in JavaScript, but most are outdated,
11
+ unmaintained, lack type support, or rely on specific runtime APIs (e.g., Node.js
12
+ `Buffer`).
13
+
14
+ This version has been cleaned up, with TypeScript types added to make it easier
15
+ to read and maintain.
16
+
17
+ ## Features
18
+
19
+ - Encode and decode LZMA streams
20
+ - Supports both string and Uint8Array data
21
+ - Supports both browser and Node.js environments
22
+ - Pure JavaScript implementation with TypeScript types
23
+ - No dependencies on runtime-specific APIs
24
+
8
25
  ## Installation
9
26
 
10
27
  > [!NOTE]
11
- > This package is native [ESM][mozzila-esm] and no longer provides a
12
- > CommonJS export. If your project uses CommonJS, you will have to convert to ESM
13
- > or use the dynamic [`import()`][mozzila-import] function.
28
+ > This package is native [ESM][mozzila-esm] and no longer provides a CommonJS
29
+ > export. If your project uses CommonJS, you will have to convert to ESM or use
30
+ > the dynamic [`import()`][mozzila-import] function.
14
31
 
15
32
  [mozzila-esm]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules
16
33
  [mozzila-import]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import
17
34
 
18
- ### npm
35
+ ### [npm](https://npmjs.com/lzma1)
19
36
 
20
37
  ```sh
21
38
  npm install lzma1
22
39
  ```
23
40
 
24
- ### deno
25
-
26
- Since `v1.28+` import from npm registry using `npm:` prefix.
27
-
28
- ```ts
29
- import {
30
- compress,
31
- decompress,
32
- } from "npm:lzma1@latest";
33
- ```
34
-
35
41
  ### browser
36
42
 
37
43
  ```html
@@ -40,16 +46,16 @@ import {
40
46
  </script>
41
47
  ```
42
48
 
43
- ## API
49
+ ## Quick start
50
+
51
+ The library provides two main functions: `compress` and `decompress`:
44
52
 
45
53
  ```ts
46
54
  compress(data: string | Uint8Array, mode?: Mode): Int8Array
47
55
  decompress(data: Uint8Array | ArrayBuffer): string | Int8Array
48
56
  ```
49
57
 
50
- ## Usage
51
-
52
- Compress and decompress a string with compression level 1.
58
+ ### Compressing a string
53
59
 
54
60
  ```js
55
61
  import {
@@ -58,14 +64,95 @@ import {
58
64
  } from "lzma1";
59
65
 
60
66
  const data = "Hello World!";
61
- const compressed = compress(data, 1);
62
- const decompressed = decompress(result);
67
+ const compressed = compress(data, 1); // Using compression level 1 (fastest)
68
+ const decompressed = decompress(compressed);
63
69
 
64
70
  // data === decompressed
65
71
  ```
66
72
 
73
+ ### Working with binary data
74
+
75
+ ```js
76
+ import {
77
+ compress,
78
+ decompress,
79
+ } from "lzma1";
80
+
81
+ // Compress binary data
82
+ const binaryData = new Uint8Array([0x01, 0x02, 0x03, 0x04]);
83
+ const compressed = compress(binaryData, 5); // Default compression level
84
+
85
+ // Decompress back to binary
86
+ const decompressed = decompress(compressed);
87
+ // decompressed will be an Int8Array
88
+ ```
89
+
90
+ ### HTML example
91
+
92
+ A simple browser example:
93
+
94
+ ```html
95
+ <!DOCTYPE html>
96
+ <html>
97
+ <body>
98
+ <textarea id="input">Hello World!</textarea><br>
99
+ <button id="run">Compress & Decompress</button>>
100
+ <div id="result"></div>
101
+
102
+ <script type="module">
103
+ import { compress, decompress } from "https://esm.sh/lzma1@latest";
104
+
105
+ document.getElementById("run").onclick = () => {
106
+ const text = document.getElementById("input").value;
107
+ const compressed = compress(text);
108
+ const decompressed = decompress(compressed);
109
+
110
+ document.getElementById("result").innerHTML =
111
+ `Original: ${text.length} bytes<br>` +
112
+ `Compressed: ${compressed.length} bytes<br>` +
113
+ `Result: ${decompressed}`;
114
+ };
115
+ </script>
116
+ </body>
117
+ </html>
118
+ ```
119
+
120
+ ### Advanced usage
121
+
122
+ You can control the compression level (1-9) to balance between speed and compression ratio:
123
+
124
+ ```js
125
+ import { compress } from "lzma1";
126
+
127
+ // Fastest compression (level 1)
128
+ const fastCompressed = compress(data, 1);
129
+
130
+ // Balanced compression (level 5)
131
+ const balancedCompressed = compress(data, 5);
132
+
133
+ // Maximum compression (level 9)
134
+ const maxCompressed = compress(data, 9);
135
+ ```
136
+
137
+ ## How it works
138
+
139
+ LZMA (Lempel-Ziv-Markov chain Algorithm) is a compression algorithm that uses a
140
+ dictionary compression scheme. It's known for its high compression ratio and is
141
+ commonly used in the 7z archive format.
142
+
143
+ ### LZMA header
144
+
145
+ The LZMA compressed data begins with a header that contains information needed for decompression:
146
+
147
+ ![lzma](./docs/lzma.svg)
148
+
149
+ More [information][header_link] about the LZMA header structure.
150
+
151
+ [header_link]: https://docs.fileformat.com/compression/lzma/#lzma-header
152
+
67
153
  ## Related
68
154
 
69
- - <https://github.com/cscott/lzma-purejs>
70
- - <https://github.com/glinscott/lzmajs>
71
- - <https://github.com/mauron85/lzma-purejs/tree/master>
155
+ - [7-Zip SDK](https://www.7-zip.org/sdk.html)
156
+ - [lzma-purejs](https://github.com/cscott/lzma-purejs)
157
+ - [lzmajs](https://github.com/glinscott/lzmajs)
158
+ - [lzma-purejs fork](https://github.com/mauron85/lzma-purejs/tree/master)
@@ -0,0 +1,6 @@
1
+ /**
2
+ * @license
3
+ * Copyright Filip Seman
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+ export { compress, decompress } from "./lzma.js";
package/dist/index.js ADDED
@@ -0,0 +1,6 @@
1
+ /**
2
+ * @license
3
+ * Copyright Filip Seman
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+ export { compress, decompress } from "./lzma.js";
package/dist/lzma.d.ts CHANGED
@@ -62,7 +62,6 @@ export declare class LZMA {
62
62
  compress(data: string | Uint8Array | ArrayBuffer, mode?: keyof typeof this.CompressionModes): Int8Array;
63
63
  decompress(bytearray: Uint8Array | ArrayBuffer): Int8Array | string;
64
64
  }
65
- type CompressionMode = keyof LZMA["CompressionModes"];
66
65
  /**
67
66
  * Compresses data using LZMA algorithm
68
67
  *
@@ -70,7 +69,7 @@ type CompressionMode = keyof LZMA["CompressionModes"];
70
69
  * @param mode Compression mode (1-9), defaults to 5
71
70
  * @returns Compressed data as Int8Array
72
71
  */
73
- export declare function compress(data: string | Uint8Array | ArrayBuffer, mode?: CompressionMode): Int8Array;
72
+ export declare function compress(data: string | Uint8Array | ArrayBuffer, mode?: keyof LZMA["CompressionModes"]): Int8Array;
74
73
  /**
75
74
  * Decompresses LZMA compressed data
76
75
  *
@@ -78,4 +77,3 @@ export declare function compress(data: string | Uint8Array | ArrayBuffer, mode?:
78
77
  * @returns Decompressed data as string if input was string, or Int8Array if input was binary
79
78
  */
80
79
  export declare function decompress(data: Uint8Array | ArrayBuffer): string | Int8Array;
81
- export {};