node-liblzma 1.1.9 → 2.0.3

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.
Files changed (54) hide show
  1. package/.gitattributes +3 -0
  2. package/.release-it.json +7 -0
  3. package/.release-it.manual.json +7 -0
  4. package/.release-it.retry.json +3 -0
  5. package/CHANGELOG.md +271 -0
  6. package/History.md +20 -0
  7. package/README.md +750 -30
  8. package/RELEASING.md +131 -0
  9. package/binding.gyp +162 -436
  10. package/biome.json +81 -0
  11. package/index.d.ts +254 -0
  12. package/lib/errors.d.ts +72 -0
  13. package/lib/errors.d.ts.map +1 -0
  14. package/lib/errors.js +153 -0
  15. package/lib/errors.js.map +1 -0
  16. package/lib/lzma.d.ts +245 -0
  17. package/lib/lzma.d.ts.map +1 -0
  18. package/lib/lzma.js +626 -345
  19. package/lib/lzma.js.map +1 -0
  20. package/lib/pool.d.ts +123 -0
  21. package/lib/pool.d.ts.map +1 -0
  22. package/lib/pool.js +188 -0
  23. package/lib/pool.js.map +1 -0
  24. package/lib/types.d.ts +27 -0
  25. package/lib/types.d.ts.map +1 -0
  26. package/lib/types.js +5 -0
  27. package/lib/types.js.map +1 -0
  28. package/package.json +61 -22
  29. package/pnpm-workspace.yaml +3 -0
  30. package/prebuilds/darwin-x64/node-liblzma.node +0 -0
  31. package/prebuilds/linux-x64/node-liblzma.node +0 -0
  32. package/prebuilds/win32-x64/node-liblzma.node +0 -0
  33. package/scripts/analyze-coverage.js +132 -0
  34. package/scripts/build_xz_with_cmake.py +390 -0
  35. package/scripts/compare-coverage-tools.js +93 -0
  36. package/scripts/copy_dll.py +51 -0
  37. package/scripts/download_xz_from_github.py +376 -0
  38. package/src/bindings/node-liblzma.cpp +411 -229
  39. package/src/bindings/node-liblzma.hpp +101 -48
  40. package/src/errors.ts +167 -0
  41. package/src/lzma.ts +839 -0
  42. package/src/pool.ts +228 -0
  43. package/src/types.ts +30 -0
  44. package/tsconfig.json +50 -0
  45. package/vitest.config.istanbul.ts +29 -0
  46. package/vitest.config.monocart.ts +44 -0
  47. package/vitest.config.ts +52 -0
  48. package/xz-version.json +8 -0
  49. package/prebuilds/darwin-x64/node.napi.node +0 -0
  50. package/prebuilds/linux-x64/node.napi.node +0 -0
  51. package/prebuilds/win32-x64/node.napi.node +0 -0
  52. package/scripts/download_extract_deps.py +0 -29
  53. package/scripts/prebuildify.py +0 -13
  54. package/src/lzma.coffee +0 -344
@@ -1,20 +1,20 @@
1
1
  /**
2
- * node-liblzma - Node.js bindings for liblzma
3
- * Copyright (C) 2014-2015 Olivier Orabona <olivier.orabona@gmail.com>
4
- *
5
- * This program is free software: you can redistribute it and/or modify
6
- * it under the terms of the GNU Lesser General Public License as published by
7
- * the Free Software Foundation, either version 3 of the License, or
8
- * (at your option) any later version.
9
- *
10
- * This program is distributed in the hope that it will be useful,
11
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
- * GNU General Public License for more details.
14
- *
15
- * You should have received a copy of the GNU Lesser General Public License
16
- * along with this program. If not, see <https://www.gnu.org/licenses/>.
17
- **/
2
+ * node-liblzma - Node.js bindings for liblzma
3
+ * Copyright (C) 2014-2015 Olivier Orabona <olivier.orabona@gmail.com>
4
+ *
5
+ * This program is free software: you can redistribute it and/or modify
6
+ * it under the terms of the GNU Lesser General Public License as published by
7
+ * the Free Software Foundation, either version 3 of the License, or
8
+ * (at your option) any later version.
9
+ *
10
+ * This program is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ * GNU General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public License
16
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
17
+ **/
18
18
 
19
19
  #ifndef BUILDING_NODE_EXTENSION
20
20
  #define BUILDING_NODE_EXTENSION
@@ -27,9 +27,14 @@
27
27
  #include <napi.h>
28
28
 
29
29
  #include <sstream>
30
+ #include <memory>
31
+ #include <functional>
30
32
 
31
33
  constexpr unsigned int STREAM_ENCODE = 0;
32
34
  constexpr unsigned int STREAM_DECODE = 1;
35
+ constexpr int SYNC_PARAM_COUNT = 6;
36
+ constexpr int ASYNC_PARAM_COUNT = 7;
37
+
33
38
  #ifdef ENABLE_THREAD_SUPPORT
34
39
  constexpr bool HAS_THREADS_SUPPORT = true;
35
40
  #else
@@ -37,55 +42,103 @@ constexpr bool HAS_THREADS_SUPPORT = false;
37
42
  #endif
38
43
 
39
44
  class LZMAWorker;
40
- class LZMA : public Napi::ObjectWrap<LZMA> {
45
+ class LZMA : public Napi::ObjectWrap<LZMA>
46
+ {
41
47
  public:
42
48
  static void Init(Napi::Env env, Napi::Object exports);
43
49
 
44
- explicit LZMA(const Napi::CallbackInfo& info);
45
- ~LZMA() = default;
50
+ explicit LZMA(const Napi::CallbackInfo &info);
51
+ ~LZMA();
52
+
53
+ Napi::Value Close(const Napi::CallbackInfo &info);
54
+ Napi::Value Close(const Napi::Env &env);
46
55
 
47
- Napi::Value Close(const Napi::CallbackInfo &info);
48
- Napi::Value Close(const Napi::Env &env);
56
+ template <bool async>
57
+ Napi::Value Code(const Napi::CallbackInfo &info);
49
58
 
50
- template<bool async>
51
- Napi::Value Code(const Napi::CallbackInfo &info);
59
+ static void Process(LZMA *obj);
60
+ static Napi::Value AfterSync(const Napi::CallbackInfo &info, LZMA *obj);
52
61
 
53
- static void Process(LZMA* obj);
54
- static void After(Napi::Env env, LZMA* obj /*, int status */);
55
- static Napi::Value AfterSync(const Napi::CallbackInfo &info, LZMA* obj);
62
+ // Allow LZMAWorker access to private members
63
+ friend class LZMAWorker;
56
64
 
57
65
  private:
66
+ // Buffer preparation and validation
67
+ struct BufferContext
68
+ {
69
+ const uint8_t *in;
70
+ uint8_t *out;
71
+ size_t in_len, out_len, in_off, out_off;
72
+ };
73
+
74
+ template <bool async>
75
+ bool ValidateAndPrepareBuffers(const Napi::CallbackInfo &info, BufferContext &ctx);
76
+
77
+ Napi::Value StartAsyncWork(const Napi::CallbackInfo &info);
78
+ Napi::Value ExecuteSyncWork(const Napi::CallbackInfo &info);
79
+
80
+ // Constructor helpers
81
+ bool ValidateConstructorArgs(const Napi::CallbackInfo &info, uint32_t &mode, Napi::Object &opts);
82
+ bool InitializeFilters(const Napi::Object &opts, uint32_t preset);
83
+ bool InitializeEncoder(const Napi::Object &opts, uint32_t preset, lzma_check check);
84
+ bool InitializeDecoder();
85
+
86
+ // Common cleanup operations for both sync and async completion
87
+ void AfterCommon(const Napi::Env &env);
88
+
89
+ // Unified completion helpers (overloaded):
90
+ // - After(env): builds result array, marks done, cleans up, returns array (sync path)
91
+ // - After(env, cb): builds result array, marks done, calls cb, then cleans up (async path)
92
+ Napi::Array After(const Napi::Env &env);
93
+ void After(const Napi::Env &env, const Napi::Function &cb);
94
+
58
95
  lzma_stream _stream;
59
96
  bool _wip;
60
97
  bool _pending_close;
61
98
 
62
- LZMAWorker* _worker;
99
+ LZMAWorker *_worker;
63
100
 
64
101
  lzma_action _action;
65
- Napi::FunctionReference _callback;
66
102
  lzma_ret _ret;
67
- lzma_filter *filters;
103
+ std::unique_ptr<lzma_filter[]> filters;
104
+ // Persist LZMA2 options referenced by filters to avoid dangling pointer
105
+ lzma_options_lzma _opt_lzma2;
106
+
107
+ // Keep input/output Buffers alive while async work runs
108
+ Napi::Reference<Napi::Buffer<uint8_t>> _in_buf_ref;
109
+ Napi::Reference<Napi::Buffer<uint8_t>> _out_buf_ref;
68
110
  };
69
111
 
70
- class LZMAWorker : public Napi::AsyncWorker {
71
- public:
72
- LZMAWorker(Napi::Env env, LZMA* instance) : AsyncWorker(env), lzma(instance) { }
73
- ~LZMAWorker() = default;
74
-
75
- void Execute() {
76
- LZMA::Process(this->lzma);
77
- }
78
- void OnOK() {
79
- Napi::HandleScope scope(Env());
80
- LZMA::After(Env(), this->lzma);
81
- }
82
- void OnError(const Napi::Error& e) {
83
- Napi::HandleScope scope(Env());
84
- LZMA::After(Env(), this->lzma);
85
- }
86
-
87
- private:
88
- LZMA *lzma;
112
+ class LZMAWorker : public Napi::AsyncWorker
113
+ {
114
+ public:
115
+ LZMAWorker(Napi::Env env, LZMA *instance, Napi::Function cb)
116
+ : AsyncWorker(cb, "LZMAWorker"), lzma(instance)
117
+ {
118
+ // Set work in progress flag when worker is created
119
+ lzma->_wip = true;
120
+ }
121
+ ~LZMAWorker() = default;
122
+
123
+ void Execute() override
124
+ {
125
+ // Perform the compression/decompression step
126
+ LZMA::Process(this->lzma);
127
+ }
128
+ void OnOK() override
129
+ {
130
+ lzma->After(Env(), Callback().Value());
131
+ }
132
+ void OnError(const Napi::Error & /*e*/) override
133
+ {
134
+ // Fallback to a generic programming error code
135
+ // Set a generic error code so BuildResult exposes it
136
+ lzma->_ret = LZMA_PROG_ERROR;
137
+ lzma->After(Env(), Callback().Value());
138
+ }
139
+
140
+ private:
141
+ LZMA *lzma;
89
142
  };
90
143
 
91
144
  #endif // NODE_LIBLZMA_H
package/src/errors.ts ADDED
@@ -0,0 +1,167 @@
1
+ /**
2
+ * node-liblzma - Node.js bindings for liblzma
3
+ * Copyright (C) Olivier Orabona <olivier.orabona@gmail.com>
4
+ *
5
+ * This program is free software: you can redistribute it and/or modify
6
+ * it under the terms of the GNU Lesser General Public License as published by
7
+ * the Free Software Foundation, either version 3 of the License, or
8
+ * (at your option) any later version.
9
+ *
10
+ * This program is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ * GNU General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public License
16
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
17
+ */
18
+
19
+ /**
20
+ * Base class for all LZMA-related errors
21
+ */
22
+ export class LZMAError extends Error {
23
+ public readonly errno: number;
24
+ public readonly code: number;
25
+
26
+ constructor(message: string, errno: number) {
27
+ super(message);
28
+ this.name = 'LZMAError';
29
+ this.errno = errno;
30
+ this.code = errno;
31
+ Error.captureStackTrace(this, this.constructor);
32
+ }
33
+ }
34
+
35
+ /**
36
+ * Memory allocation error - thrown when LZMA cannot allocate required memory
37
+ */
38
+ export class LZMAMemoryError extends LZMAError {
39
+ constructor(errno: number) {
40
+ super('Cannot allocate memory', errno);
41
+ this.name = 'LZMAMemoryError';
42
+ }
43
+ }
44
+
45
+ /**
46
+ * Memory limit error - thrown when operation would exceed memory usage limit
47
+ */
48
+ export class LZMAMemoryLimitError extends LZMAError {
49
+ constructor(errno: number) {
50
+ super('Memory usage limit was reached', errno);
51
+ this.name = 'LZMAMemoryLimitError';
52
+ }
53
+ }
54
+
55
+ /**
56
+ * Format error - thrown when file format is not recognized
57
+ */
58
+ export class LZMAFormatError extends LZMAError {
59
+ constructor(errno: number) {
60
+ super('File format not recognized', errno);
61
+ this.name = 'LZMAFormatError';
62
+ }
63
+ }
64
+
65
+ /**
66
+ * Options error - thrown when invalid or unsupported options are provided
67
+ */
68
+ export class LZMAOptionsError extends LZMAError {
69
+ constructor(errno: number) {
70
+ super('Invalid or unsupported options', errno);
71
+ this.name = 'LZMAOptionsError';
72
+ }
73
+ }
74
+
75
+ /**
76
+ * Data error - thrown when compressed data is corrupt
77
+ */
78
+ export class LZMADataError extends LZMAError {
79
+ constructor(errno: number) {
80
+ super('Data is corrupt', errno);
81
+ this.name = 'LZMADataError';
82
+ }
83
+ }
84
+
85
+ /**
86
+ * Buffer error - thrown when no progress is possible (e.g., buffer too small)
87
+ */
88
+ export class LZMABufferError extends LZMAError {
89
+ constructor(errno: number) {
90
+ super('No progress is possible', errno);
91
+ this.name = 'LZMABufferError';
92
+ }
93
+ }
94
+
95
+ /**
96
+ * Programming error - thrown when there's an internal programming error
97
+ */
98
+ export class LZMAProgrammingError extends LZMAError {
99
+ constructor(errno: number) {
100
+ super('Programming error', errno);
101
+ this.name = 'LZMAProgrammingError';
102
+ }
103
+ }
104
+
105
+ /**
106
+ * Factory function to create appropriate error instance based on errno
107
+ */
108
+ export function createLZMAError(errno: number, message?: string): LZMAError {
109
+ // LZMA error codes mapping
110
+ const LZMA_OK = 0;
111
+ const LZMA_STREAM_END = 1;
112
+ const LZMA_NO_CHECK = 2;
113
+ const LZMA_UNSUPPORTED_CHECK = 3;
114
+ const LZMA_GET_CHECK = 4;
115
+ const LZMA_MEM_ERROR = 5;
116
+ const LZMA_MEMLIMIT_ERROR = 6;
117
+ const LZMA_FORMAT_ERROR = 7;
118
+ const LZMA_OPTIONS_ERROR = 8;
119
+ const LZMA_DATA_ERROR = 9;
120
+ const LZMA_BUF_ERROR = 10;
121
+ const LZMA_PROG_ERROR = 11;
122
+
123
+ switch (errno) {
124
+ case LZMA_MEM_ERROR:
125
+ return new LZMAMemoryError(errno);
126
+ case LZMA_MEMLIMIT_ERROR:
127
+ return new LZMAMemoryLimitError(errno);
128
+ case LZMA_FORMAT_ERROR:
129
+ return new LZMAFormatError(errno);
130
+ case LZMA_OPTIONS_ERROR:
131
+ return new LZMAOptionsError(errno);
132
+ case LZMA_DATA_ERROR:
133
+ return new LZMADataError(errno);
134
+ case LZMA_BUF_ERROR:
135
+ return new LZMABufferError(errno);
136
+ case LZMA_PROG_ERROR:
137
+ return new LZMAProgrammingError(errno);
138
+ default: {
139
+ // For success codes and unknown errors, use base LZMAError
140
+ const errorMessage = message || getErrorMessage(errno);
141
+ return new LZMAError(errorMessage, errno);
142
+ }
143
+ }
144
+ }
145
+
146
+ /**
147
+ * Get error message for a given errno
148
+ */
149
+ function getErrorMessage(errno: number): string {
150
+ const messages = [
151
+ 'Operation completed successfully',
152
+ 'End of stream was reached',
153
+ 'Input stream has no integrity check',
154
+ 'Cannot calculate the integrity check',
155
+ 'Integrity check type is not available',
156
+ 'Cannot allocate memory',
157
+ 'Memory usage limit was reached',
158
+ 'File format not recognized',
159
+ 'Invalid or unsupported options',
160
+ 'Data is corrupt',
161
+ 'No progress is possible',
162
+ 'Programming error',
163
+ ];
164
+
165
+ const messageIndex = Math.max(0, Math.min(errno, messages.length - 1));
166
+ return messages[messageIndex];
167
+ }