lzma1 0.0.5 → 0.0.7

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/LICENSE CHANGED
@@ -186,7 +186,7 @@
186
186
  same "printed page" as the copyright notice for easier
187
187
  identification within third-party archives.
188
188
 
189
- Copyright [yyyy] [name of copyright owner]
189
+ Copyright 2025 Filip Seman
190
190
 
191
191
  Licensed under the Apache License, Version 2.0 (the "License");
192
192
  you may not use this file except in compliance with the License.
package/README.md CHANGED
@@ -1,38 +1,43 @@
1
1
  # lzma1
2
2
 
3
- This is a [fork][fork-link] of [Nathan Rugg's][fork-author] package that adds
4
- types and makes the logic more structured and readable.
3
+ This is a [fork][fork-link] of [Nathan Rugg's][fork-author] LZMA-JS package.
5
4
 
6
5
  [fork-link]: https://github.com/LZMA-JS/LZMA-JS
7
6
  [fork-author]: https://github.com/nmrugg
8
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
+
9
25
  ## Installation
10
26
 
11
27
  > [!NOTE]
12
- > This package is native [ESM][mozzila-esm] and no longer provides a
13
- > CommonJS export. If your project uses CommonJS, you will have to convert to ESM
14
- > 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.
15
31
 
16
32
  [mozzila-esm]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules
17
33
  [mozzila-import]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import
18
34
 
19
- ### npm
35
+ ### [npm](https://npmjs.com/lzma1)
20
36
 
21
37
  ```sh
22
38
  npm install lzma1
23
39
  ```
24
40
 
25
- ### deno
26
-
27
- Since `v1.28+` import from npm registry using `npm:` prefix.
28
-
29
- ```ts
30
- import {
31
- compress,
32
- decompress,
33
- } from "npm:lzma1@latest";
34
- ```
35
-
36
41
  ### browser
37
42
 
38
43
  ```html
@@ -41,16 +46,16 @@ import {
41
46
  </script>
42
47
  ```
43
48
 
44
- ## API
49
+ ## Quick start
50
+
51
+ The library provides two main functions: `compress` and `decompress`:
45
52
 
46
53
  ```ts
47
54
  compress(data: string | Uint8Array, mode?: Mode): Int8Array
48
55
  decompress(data: Uint8Array | ArrayBuffer): string | Int8Array
49
56
  ```
50
57
 
51
- ## Usage
52
-
53
- Compress and decompress a string with compression level 1.
58
+ ### Compressing a string
54
59
 
55
60
  ```js
56
61
  import {
@@ -59,22 +64,95 @@ import {
59
64
  } from "lzma1";
60
65
 
61
66
  const data = "Hello World!";
62
- const compressed = compress(data, 1);
63
- const decompressed = decompress(result);
67
+ const compressed = compress(data, 1); // Using compression level 1 (fastest)
68
+ const decompressed = decompress(compressed);
64
69
 
65
70
  // data === decompressed
66
71
  ```
67
72
 
68
- ## LZMA header
73
+ ### Working with binary data
69
74
 
70
- More [information][header_link] about the LZMA header.
75
+ ```js
76
+ import {
77
+ compress,
78
+ decompress,
79
+ } from "lzma1";
71
80
 
72
- ![lzma](./docs/lzma.svg)
81
+ // Compress binary data
82
+ const binaryData = new Uint8Array([0x01, 0x02, 0x03, 0x04]);
83
+ const compressed = compress(binaryData, 5); // Default compression level
73
84
 
74
- ## Related
85
+ // Decompress back to binary
86
+ const decompressed = decompress(compressed);
87
+ // decompressed will be an Int8Array
88
+ ```
75
89
 
76
- - <https://github.com/cscott/lzma-purejs>
77
- - <https://github.com/glinscott/lzmajs>
78
- - <https://github.com/mauron85/lzma-purejs/tree/master>
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.
79
150
 
80
151
  [header_link]: https://docs.fileformat.com/compression/lzma/#lzma-header
152
+
153
+ ## Related
154
+
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)
package/lib/index.d.ts 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/lib/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";
@@ -1,3 +1,19 @@
1
+ /**
2
+ * Converts a number to a signed 8-bit integer using DataView.
3
+ *
4
+ * Previously, this was done with bitwise operations (value << 24 >> 24), but
5
+ * that approach was obscure and hard to understand. Using DataView improves
6
+ * readability and ensures correct 8-bit conversion.
7
+ */
8
+ export declare function toSigned8bit(value: number): number;
9
+ /**
10
+ * Converts a number to a signed 16-bit integer using DataView.
11
+ *
12
+ * Previously, this was done with bitwise operations (value << 16 >> 16), but
13
+ * that approach was obscure and hard to understand. Using DataView improves
14
+ * readability and ensures correct 16-bit conversion.
15
+ */
16
+ export declare function toSigned16bit(value: number): number;
1
17
  export declare class LZMA {
2
18
  #private;
3
19
  readonly CompressionModes: {
@@ -47,7 +63,6 @@ export declare class LZMA {
47
63
  readonly modeIndex: 1;
48
64
  };
49
65
  };
50
- readonly bitMaskForRange = -16777216;
51
66
  constructor();
52
67
  GetLenToPosState(len: number): number;
53
68
  StateUpdateChar(index: number): number;
@@ -63,7 +78,6 @@ export declare class LZMA {
63
78
  compress(data: string | Uint8Array | ArrayBuffer, mode?: keyof typeof this.CompressionModes): Int8Array;
64
79
  decompress(bytearray: Uint8Array | ArrayBuffer): Int8Array | string;
65
80
  }
66
- type CompressionMode = keyof LZMA["CompressionModes"];
67
81
  /**
68
82
  * Compresses data using LZMA algorithm
69
83
  *
@@ -71,7 +85,7 @@ type CompressionMode = keyof LZMA["CompressionModes"];
71
85
  * @param mode Compression mode (1-9), defaults to 5
72
86
  * @returns Compressed data as Int8Array
73
87
  */
74
- export declare function compress(data: string | Uint8Array | ArrayBuffer, mode?: CompressionMode): Int8Array;
88
+ export declare function compress(data: string | Uint8Array | ArrayBuffer, mode?: keyof LZMA["CompressionModes"]): Int8Array;
75
89
  /**
76
90
  * Decompresses LZMA compressed data
77
91
  *
@@ -79,4 +93,3 @@ export declare function compress(data: string | Uint8Array | ArrayBuffer, mode?:
79
93
  * @returns Decompressed data as string if input was string, or Int8Array if input was binary
80
94
  */
81
95
  export declare function decompress(data: Uint8Array | ArrayBuffer): string | Int8Array;
82
- export {};