node-liblzma 1.1.4 → 1.1.5

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/package.json CHANGED
@@ -1,20 +1,18 @@
1
1
  {
2
2
  "name": "node-liblzma",
3
- "version": "1.1.4",
3
+ "version": "1.1.5",
4
4
  "description": "NodeJS wrapper for liblzma",
5
5
  "main": "./lib/lzma.js",
6
6
  "scripts": {
7
7
  "install": "node-gyp install",
8
- "postinstall": "run-p build:*",
9
- "build:native": "node-gyp rebuild",
10
- "build:coffee": "coffee --compile --output lib/ src/",
8
+ "postinstall": "node-gyp rebuild",
9
+ "prepublish": "coffee --compile --output lib/ src/",
11
10
  "rebuild": "node-gyp rebuild",
12
11
  "test": "npx mocha test/*.coffee"
13
12
  },
14
13
  "dependencies": {
15
14
  "node-addon-api": "4.2.0",
16
- "node-gyp-build": "4.3.0",
17
- "npm-run-all": "4.1.5"
15
+ "node-gyp-build": "4.3.0"
18
16
  },
19
17
  "devDependencies": {
20
18
  "coffeescript": "2.6.1",
Binary file
Binary file
@@ -1,107 +0,0 @@
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
- **/
18
-
19
- #include "node-liblzma.hpp"
20
-
21
- Napi::Object Init(Napi::Env env, Napi::Object exports)
22
- {
23
- LZMA::Init(env, exports);
24
-
25
- // Constants
26
- // enum lzma_ret
27
- exports.Set(Napi::String::New(env, "LZMA_OK"), Napi::Number::New(env, LZMA_OK));
28
- exports.Set(Napi::String::New(env, "LZMA_STREAM_END"), Napi::Number::New(env, LZMA_STREAM_END));
29
- exports.Set(Napi::String::New(env, "LZMA_NO_CHECK"), Napi::Number::New(env, LZMA_NO_CHECK));
30
- exports.Set(Napi::String::New(env, "LZMA_UNSUPPORTED_CHECK"), Napi::Number::New(env, LZMA_UNSUPPORTED_CHECK));
31
- exports.Set(Napi::String::New(env, "LZMA_GET_CHECK"), Napi::Number::New(env, LZMA_GET_CHECK));
32
- exports.Set(Napi::String::New(env, "LZMA_MEM_ERROR"), Napi::Number::New(env, LZMA_MEM_ERROR));
33
- exports.Set(Napi::String::New(env, "LZMA_MEMLIMIT_ERROR"), Napi::Number::New(env, LZMA_MEMLIMIT_ERROR));
34
- exports.Set(Napi::String::New(env, "LZMA_FORMAT_ERROR"), Napi::Number::New(env, LZMA_FORMAT_ERROR));
35
- exports.Set(Napi::String::New(env, "LZMA_OPTIONS_ERROR"), Napi::Number::New(env, LZMA_OPTIONS_ERROR));
36
- exports.Set(Napi::String::New(env, "LZMA_DATA_ERROR"), Napi::Number::New(env, LZMA_DATA_ERROR));
37
- exports.Set(Napi::String::New(env, "LZMA_BUF_ERROR"), Napi::Number::New(env, LZMA_BUF_ERROR));
38
- exports.Set(Napi::String::New(env, "LZMA_PROG_ERROR"), Napi::Number::New(env, LZMA_PROG_ERROR));
39
-
40
- // enum lzma_action
41
- exports.Set(Napi::String::New(env, "LZMA_RUN"), Napi::Number::New(env, LZMA_RUN));
42
- exports.Set(Napi::String::New(env, "LZMA_SYNC_FLUSH"), Napi::Number::New(env, LZMA_SYNC_FLUSH));
43
- exports.Set(Napi::String::New(env, "LZMA_FULL_FLUSH"), Napi::Number::New(env, LZMA_FULL_FLUSH));
44
- exports.Set(Napi::String::New(env, "LZMA_FINISH"), Napi::Number::New(env, LZMA_FINISH));
45
-
46
- // enum lzma_check
47
- exports.Set(Napi::String::New(env, "LZMA_CHECK_NONE"), Napi::Number::New(env, LZMA_CHECK_NONE));
48
- exports.Set(Napi::String::New(env, "LZMA_CHECK_CRC32"), Napi::Number::New(env, LZMA_CHECK_CRC32));
49
- exports.Set(Napi::String::New(env, "LZMA_CHECK_CRC64"), Napi::Number::New(env, LZMA_CHECK_CRC64));
50
- exports.Set(Napi::String::New(env, "LZMA_CHECK_SHA256"), Napi::Number::New(env, LZMA_CHECK_SHA256));
51
-
52
- // lzma_match_finder
53
- exports.Set(Napi::String::New(env, "MF_HC3"), Napi::Number::New(env, LZMA_MF_HC3));
54
- exports.Set(Napi::String::New(env, "MF_HC4"), Napi::Number::New(env, LZMA_MF_HC4));
55
- exports.Set(Napi::String::New(env, "MF_BT2"), Napi::Number::New(env, LZMA_MF_BT2));
56
- exports.Set(Napi::String::New(env, "MF_BT3"), Napi::Number::New(env, LZMA_MF_BT3));
57
- exports.Set(Napi::String::New(env, "MF_BT4"), Napi::Number::New(env, LZMA_MF_BT4));
58
-
59
- // lzma_mode
60
- exports.Set(Napi::String::New(env, "LZMA_MODE_FAST"), Napi::Number::New(env, LZMA_MODE_FAST));
61
- exports.Set(Napi::String::New(env, "LZMA_MODE_NORMAL"), Napi::Number::New(env, LZMA_MODE_NORMAL));
62
-
63
- // defines
64
- exports.Set(Napi::String::New(env, "LZMA_FILTER_X86"), Napi::Number::New(env, LZMA_FILTER_X86));
65
- exports.Set(Napi::String::New(env, "LZMA_FILTER_POWERPC"), Napi::Number::New(env, LZMA_FILTER_POWERPC));
66
- exports.Set(Napi::String::New(env, "LZMA_FILTER_IA64"), Napi::Number::New(env, LZMA_FILTER_IA64));
67
- exports.Set(Napi::String::New(env, "LZMA_FILTER_ARM"), Napi::Number::New(env, LZMA_FILTER_ARM));
68
- exports.Set(Napi::String::New(env, "LZMA_FILTER_ARMTHUMB"), Napi::Number::New(env, LZMA_FILTER_ARMTHUMB));
69
- exports.Set(Napi::String::New(env, "LZMA_FILTER_SPARC"), Napi::Number::New(env, LZMA_FILTER_SPARC));
70
- exports.Set(Napi::String::New(env, "LZMA_FILTER_DELTA"), Napi::Number::New(env, LZMA_FILTER_DELTA));
71
- exports.Set(Napi::String::New(env, "LZMA_FILTERS_MAX"), Napi::Number::New(env, LZMA_FILTERS_MAX));
72
- exports.Set(Napi::String::New(env, "LZMA_FILTER_LZMA1"), Napi::Number::New(env, LZMA_FILTER_LZMA1));
73
- exports.Set(Napi::String::New(env, "LZMA_FILTER_LZMA2"), Napi::Number::New(env, LZMA_FILTER_LZMA2));
74
- exports.Set(Napi::String::New(env, "LZMA_VLI_UNKNOWN"), Napi::Number::New(env, LZMA_VLI_UNKNOWN));
75
-
76
- exports.Set(Napi::String::New(env, "LZMA_VLI_BYTES_MAX"), Napi::Number::New(env, LZMA_VLI_BYTES_MAX));
77
- exports.Set(Napi::String::New(env, "LZMA_CHECK_ID_MAX"), Napi::Number::New(env, LZMA_CHECK_ID_MAX));
78
- exports.Set(Napi::String::New(env, "LZMA_CHECK_SIZE_MAX"), Napi::Number::New(env, LZMA_CHECK_SIZE_MAX));
79
- exports.Set(Napi::String::New(env, "LZMA_PRESET_DEFAULT"), Napi::Number::New(env, LZMA_PRESET_DEFAULT));
80
- exports.Set(Napi::String::New(env, "LZMA_PRESET_LEVEL_MASK"), Napi::Number::New(env, LZMA_PRESET_LEVEL_MASK));
81
- exports.Set(Napi::String::New(env, "LZMA_PRESET_EXTREME"), Napi::Number::New(env, LZMA_PRESET_EXTREME));
82
- exports.Set(Napi::String::New(env, "LZMA_TELL_NO_CHECK"), Napi::Number::New(env, LZMA_TELL_NO_CHECK));
83
- exports.Set(Napi::String::New(env, "LZMA_TELL_UNSUPPORTED_CHECK"), Napi::Number::New(env, LZMA_TELL_UNSUPPORTED_CHECK));
84
- exports.Set(Napi::String::New(env, "LZMA_TELL_ANY_CHECK"), Napi::Number::New(env, LZMA_TELL_ANY_CHECK));
85
- exports.Set(Napi::String::New(env, "LZMA_CONCATENATED"), Napi::Number::New(env, LZMA_CONCATENATED));
86
- exports.Set(Napi::String::New(env, "LZMA_STREAM_HEADER_SIZE"), Napi::Number::New(env, LZMA_STREAM_HEADER_SIZE));
87
- exports.Set(Napi::String::New(env, "LZMA_VERSION_MAJOR"), Napi::Number::New(env, LZMA_VERSION_MAJOR));
88
- exports.Set(Napi::String::New(env, "LZMA_VERSION_MINOR"), Napi::Number::New(env, LZMA_VERSION_MINOR));
89
- exports.Set(Napi::String::New(env, "LZMA_VERSION_PATCH"), Napi::Number::New(env, LZMA_VERSION_PATCH));
90
- exports.Set(Napi::String::New(env, "LZMA_VERSION_STABILITY"), Napi::Number::New(env, LZMA_VERSION_STABILITY));
91
- exports.Set(Napi::String::New(env, "LZMA_VERSION_STABILITY_ALPHA"), Napi::Number::New(env, LZMA_VERSION_STABILITY_ALPHA));
92
- exports.Set(Napi::String::New(env, "LZMA_VERSION_STABILITY_BETA"), Napi::Number::New(env, LZMA_VERSION_STABILITY_BETA));
93
- exports.Set(Napi::String::New(env, "LZMA_VERSION_STABILITY_STABLE"), Napi::Number::New(env, LZMA_VERSION_STABILITY_STABLE));
94
- exports.Set(Napi::String::New(env, "LZMA_VERSION"), Napi::Number::New(env, LZMA_VERSION));
95
- exports.Set(Napi::String::New(env, "LZMA_VERSION_STRING"), Napi::String::New(env, LZMA_VERSION_STRING));
96
-
97
- // LZMAStream flags
98
- exports.Set(Napi::String::New(env, "STREAM_ENCODE"), Napi::Number::New(env, STREAM_ENCODE));
99
- exports.Set(Napi::String::New(env, "STREAM_DECODE"), Napi::Number::New(env, STREAM_DECODE));
100
- exports.Set(Napi::String::New(env, "STREAM_ENCODE_MT"), Napi::Number::New(env, STREAM_ENCODE_MT));
101
-
102
- exports.Set(Napi::String::New(env, "BUFSIZ"), Napi::Number::New(env, BUFSIZ));
103
-
104
- return exports;
105
- }
106
-
107
- NODE_API_MODULE(NODE_GYP_MODULE_NAME, Init)
@@ -1,339 +0,0 @@
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
- #include "node-liblzma.hpp"
20
- #include <node_buffer.h>
21
-
22
- Napi::Value LZMA::Close(const Napi::CallbackInfo &info) {
23
- Napi::Env env = info.Env();
24
-
25
- return LZMA::Close(env);
26
- }
27
-
28
- Napi::Value LZMA::Close(const Napi::Env &env) {
29
- Napi::MemoryManagement::AdjustExternalMemory(env, -int64_t(sizeof(LZMA)));
30
-
31
- if(_wip) {
32
- _pending_close = true;
33
- return env.Undefined();
34
- }
35
-
36
- _pending_close = false;
37
-
38
- lzma_end(&_stream);
39
-
40
- return env.Undefined();
41
- }
42
-
43
- void LZMA::Init(Napi::Env env, Napi::Object exports) {
44
- Napi::Function func =
45
- DefineClass(env,
46
- "LZMA",
47
- {
48
- InstanceMethod("code", &LZMA::Code<true>),
49
- InstanceMethod("codeSync", &LZMA::Code<false>),
50
- InstanceMethod("close", &LZMA::Close)
51
- });
52
-
53
- Napi::FunctionReference* constructor = new Napi::FunctionReference();
54
- *constructor = Napi::Persistent(func);
55
- env.SetInstanceData(constructor);
56
-
57
- exports.Set("LZMA", func);
58
- }
59
-
60
- LZMA::LZMA(const Napi::CallbackInfo& info) : Napi::ObjectWrap<LZMA>(info), _stream(LZMA_STREAM_INIT),
61
- _wip(false), _pending_close(false), _worker(nullptr)
62
- {
63
- Napi::Env env = info.Env();
64
-
65
- if( info.Length() != 2 ) {
66
- Napi::TypeError::New(env, "Wrong number of arguments, expected mode(int) and opts(object)").ThrowAsJavaScriptException();
67
- return;
68
- }
69
-
70
- uint32_t mode = info[0].ToNumber().Uint32Value();
71
-
72
- if (!info[1].IsObject()) {
73
- Napi::TypeError::New(env, "Expected object as second argument").ThrowAsJavaScriptException();
74
- return;
75
- }
76
-
77
- Napi::Object opts = info[1].ToObject();
78
-
79
- Napi::Value optsCheck = opts.Get("check");
80
- if (!optsCheck.IsNumber()) {
81
- Napi::TypeError::New(env, "Expected 'check' to be an integer").ThrowAsJavaScriptException();
82
- return;
83
- }
84
-
85
- lzma_check check = static_cast<lzma_check>(optsCheck.ToNumber().Uint32Value());
86
-
87
- Napi::Value optsPreset = opts.Get("preset");
88
- if (!optsPreset.IsNumber()) {
89
- Napi::TypeError::New(env, "Expected 'preset' to be an integer").ThrowAsJavaScriptException();
90
- return;
91
- }
92
-
93
- uint32_t preset = optsPreset.ToNumber().Uint32Value();
94
-
95
- Napi::Value optsFilters = opts.Get("filters");
96
- if (!optsFilters.IsArray()) {
97
- Napi::TypeError::New(env, "Expected 'filters' to be an array").ThrowAsJavaScriptException();
98
- return;
99
- }
100
-
101
- Napi::Array filters_handle = optsFilters.As<Napi::Array>();
102
-
103
- uint32_t filters_len = filters_handle.Length();
104
-
105
- // We will need to add LZMA_VLI_UNKNOWN after, so user defined filters may
106
- // not exceed LZMA_FILTERS_MAX - 1.
107
- if( filters_len > LZMA_FILTERS_MAX - 1) {
108
- Napi::RangeError::New(env, "More filters than allowed maximum").ThrowAsJavaScriptException();
109
- return;
110
- }
111
-
112
- lzma_options_lzma opt_lzma2;
113
- if( lzma_lzma_preset(&opt_lzma2, preset) ) {
114
- Napi::Error::New(env, "Unsupported preset, possibly a bug").ThrowAsJavaScriptException();
115
- return;
116
- }
117
-
118
- // Add extra slot for LZMA_VLI_UNKNOWN.
119
- this->filters = new lzma_filter[filters_len + 1];
120
-
121
- for(uint32_t i = 0; i < filters_len; ++i) {
122
- Napi::Value filter = filters_handle.Get(i);
123
- if (!filter.IsNumber()) {
124
- Napi::Error::New(env, "Filter must be an integer").ThrowAsJavaScriptException();
125
- return;
126
- }
127
-
128
- uint64_t current = filter.ToNumber().Uint32Value();
129
- filters[i].id = current;
130
- if( current == LZMA_FILTER_LZMA2 ) {
131
- filters[i].options = &opt_lzma2;
132
- } else {
133
- filters[i].options = nullptr;
134
- }
135
- }
136
-
137
- filters[filters_len] = { .id = LZMA_VLI_UNKNOWN, .options = nullptr };
138
-
139
- lzma_ret ret;
140
- switch(mode) {
141
- case STREAM_DECODE: {
142
- ret = lzma_stream_decoder(&this->_stream, UINT64_MAX, check);
143
- break;
144
- }
145
- case STREAM_ENCODE: {
146
- ret = lzma_stream_encoder(&this->_stream, filters, check);
147
- break;
148
- }
149
-
150
- case STREAM_ENCODE_MT: {
151
- Napi::Value optsThreads = opts.Get("threads");
152
- if (!optsThreads.IsNumber()) {
153
- Napi::Error::New(env, "Threads must be an integer");
154
- return;
155
- }
156
-
157
- unsigned int threads = optsThreads.ToNumber().Uint32Value();
158
-
159
- #pragma GCC diagnostic push
160
- #pragma GCC diagnostic ignored "-Wmissing-field-initializers"
161
-
162
- lzma_mt mt = {
163
- .flags = 0,
164
- .threads = threads,
165
- .block_size = 0,
166
- .timeout = 0,
167
- .preset = preset,
168
- .filters = filters,
169
- .check = check,
170
- };
171
-
172
- #pragma GCC diagnostic pop
173
-
174
- ret = lzma_stream_encoder_mt(&this->_stream, &mt);
175
- break;
176
- }
177
- // #endif
178
- default:
179
- ret = LZMA_OPTIONS_ERROR;
180
- }
181
-
182
- if (ret != LZMA_OK) {
183
- Napi::Error::New(env, "LZMA failure, returned " + std::to_string(ret));
184
- return;
185
- }
186
-
187
- Napi::MemoryManagement::AdjustExternalMemory(env, sizeof(LZMA));
188
- }
189
-
190
- /**<
191
- * \brief Do the encoding/decoding with async support
192
- *
193
- * Function prototype is (sync):
194
- * .codeSync flushFlag, input_buffer, input_offset, output_buffer, output_offset, callback
195
- * Function prototype is (async):
196
- * .code flushFlag, input_buffer, input_offset, availInBefore, output_buffer, output_offset, callback
197
- *
198
- * Where:
199
- * flushFlag: type: Uint32
200
- * input_buffer: type: Buffer
201
- * input_offset: type: Uint32
202
- * availInBefore: type: Uint32
203
- * output_buffer: type: Buffer
204
- * output_offset: type: Uint32
205
- * callback: type: Function
206
- */
207
- template<bool async>
208
- Napi::Value LZMA::Code(const Napi::CallbackInfo &info) {
209
- Napi::Env env = info.Env();
210
-
211
- this->_wip = true;
212
- this->Ref();
213
-
214
- // Neat trick but that does the job :)
215
- if (info.Length() != 6+(int)async) {
216
- Napi::Error::New(env, "BUG?: LZMA::Code requires all these arguments: "
217
- "flushFlag, input_buffer, input_offset, availInBefore, "
218
- "output_buffer, output_offset, [callback]").ThrowAsJavaScriptException();
219
- return env.Undefined();
220
- }
221
-
222
- if (!info[0].IsNumber()) {
223
- Napi::Error::New(env, "flushFlag must be an integer");
224
- return env.Undefined();
225
- }
226
- this->_action = static_cast<lzma_action>(info[0].ToNumber().Uint32Value());
227
-
228
- // Evaluate parameters passed to us
229
- const uint8_t *in;
230
- uint8_t *out;
231
- size_t in_off, in_len, out_off, out_len;
232
-
233
- // If we do not have input buffer data
234
- if (info[1].IsNull()) {
235
- // just a flush
236
- // uint8_t nada[1] = { 0 };
237
- uint8_t nada = 0;
238
- in = &nada;
239
- in_len = 0;
240
- in_off = 0;
241
- } else {
242
- // if (!node::Buffer::HasInstance(info[1])) {
243
- if (!info[1].IsBuffer()) {
244
- Napi::TypeError::New(env, "BUG?: LZMA::Code 'input_buffer' argument must be a Buffer").ThrowAsJavaScriptException();
245
- return env.Undefined();
246
- }
247
-
248
- uint8_t *in_buf = info[1].As<Napi::Buffer<uint8_t>>().Data();
249
- in_off = info[2].ToNumber().Uint32Value();
250
- in_len = info[3].ToNumber().Uint32Value();
251
- size_t in_max = info[1].As<Napi::Buffer<uint8_t>>().Length();
252
-
253
- if(!node::Buffer::IsWithinBounds(in_off, in_len, in_max)) {
254
- Napi::Error::New(env, "Offset out of bounds!").ThrowAsJavaScriptException();
255
- return env.Undefined();
256
- }
257
- in = in_buf + in_off;
258
- }
259
-
260
- // Check if output buffer is also a Buffer
261
- if( !info[4].IsBuffer() ) {
262
- Napi::TypeError::New(env, "BUG?: LZMA::Code 'output_buffer' argument must be a Buffer").ThrowAsJavaScriptException();
263
- return env.Undefined();
264
- }
265
-
266
- uint8_t *out_buf = info[4].As<Napi::Buffer<uint8_t>>().Data();
267
- out_off = info[5].ToNumber().Uint32Value();
268
- out_len = info[4].As<Napi::Buffer<uint8_t>>().Length() - out_off;
269
- out = out_buf + out_off;
270
-
271
- // Only if async mode is enabled shall we need a callback function
272
- if(async) {
273
- this->_callback = Napi::Persistent(info[6].As<Napi::Function>());
274
- }
275
-
276
- this->_stream.next_in = in;
277
- this->_stream.avail_in = in_len;
278
- this->_stream.next_out = out;
279
- this->_stream.avail_out = out_len;
280
-
281
- // do it synchronously
282
- if(async) {
283
- this->_worker = new LZMAWorker(env, this);
284
- this->_worker->Queue();
285
- } else {
286
- Process(this);
287
- return AfterSync(info, this);
288
- }
289
-
290
- // otherwise queue work, make sure we get our work done by first calling Process and then After
291
- // napi_create_async_work(uv_default_loop(), &(this->_req), LZMA::Process, LZMA::After);
292
- return env.Undefined();
293
- }
294
-
295
- void LZMA::Process(LZMA* obj) {
296
- // the real work is done here :)
297
- obj->_wip = true;
298
- obj->_ret = lzma_code(&(obj->_stream), obj->_action);
299
- }
300
-
301
- void LZMA::After(Napi::Env env, LZMA* obj /*, int status */) {
302
-
303
- Napi::Number ret_code = Napi::Number::New(env, obj->_ret);
304
- Napi::Number avail_in = Napi::Number::New(env, obj->_stream.avail_in);
305
- Napi::Number avail_out = Napi::Number::New(env, obj->_stream.avail_out);
306
-
307
- obj->_wip = false;
308
-
309
- obj->_callback.Call({ ret_code, avail_in, avail_out });
310
-
311
- obj->Unref();
312
-
313
- if(obj->_pending_close) {
314
- obj->Close(env);
315
- }
316
- }
317
-
318
- Napi::Value LZMA::AfterSync(const Napi::CallbackInfo &info, LZMA* obj) {
319
- Napi::Env env = info.Env();
320
-
321
- Napi::Number ret_code = Napi::Number::New(env, obj->_ret);
322
- Napi::Number avail_in = Napi::Number::New(env, obj->_stream.avail_in);
323
- Napi::Number avail_out = Napi::Number::New(env, obj->_stream.avail_out);
324
- Napi::Array result = Napi::Array::New(env, 3);
325
-
326
- uint32_t i = 0;
327
- result[i++] = ret_code;
328
- result[i++] = avail_in;
329
- result[i++] = avail_out;
330
-
331
- obj->_wip = false;
332
-
333
- obj->Unref();
334
- if(obj->_pending_close) {
335
- obj->Close(info);
336
- }
337
-
338
- return result;
339
- }
@@ -1,87 +0,0 @@
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
- **/
18
-
19
- #ifndef BUILDING_NODE_EXTENSION
20
- #define BUILDING_NODE_EXTENSION
21
- #endif
22
-
23
- #ifndef NODE_LIBLZMA_H
24
- #define NODE_LIBLZMA_H
25
-
26
- #include <lzma.h>
27
- #include <napi.h>
28
-
29
- #include <sstream>
30
-
31
- constexpr unsigned int STREAM_ENCODE = 0;
32
- constexpr unsigned int STREAM_DECODE = 1;
33
- constexpr unsigned int STREAM_ENCODE_MT = 2;
34
-
35
- class LZMAWorker;
36
- class LZMA : public Napi::ObjectWrap<LZMA> {
37
- public:
38
- static void Init(Napi::Env env, Napi::Object exports);
39
-
40
- explicit LZMA(const Napi::CallbackInfo& info);
41
- ~LZMA() = default;
42
-
43
- Napi::Value Close(const Napi::CallbackInfo &info);
44
- Napi::Value Close(const Napi::Env &env);
45
-
46
- template<bool async>
47
- Napi::Value Code(const Napi::CallbackInfo &info);
48
-
49
- static void Process(LZMA* obj);
50
- static void After(Napi::Env env, LZMA* obj /*, int status */);
51
- static Napi::Value AfterSync(const Napi::CallbackInfo &info, LZMA* obj);
52
-
53
- private:
54
- lzma_stream _stream;
55
- bool _wip;
56
- bool _pending_close;
57
-
58
- LZMAWorker* _worker;
59
-
60
- lzma_action _action;
61
- Napi::FunctionReference _callback;
62
- lzma_ret _ret;
63
- lzma_filter *filters;
64
- };
65
-
66
- class LZMAWorker : public Napi::AsyncWorker {
67
- public:
68
- LZMAWorker(Napi::Env env, LZMA* instance) : AsyncWorker(env), lzma(instance) { }
69
- ~LZMAWorker() = default;
70
-
71
- void Execute() {
72
- LZMA::Process(this->lzma);
73
- }
74
- void OnOK() {
75
- Napi::HandleScope scope(Env());
76
- LZMA::After(Env(), this->lzma);
77
- }
78
- void OnError(const Napi::Error& e) {
79
- Napi::HandleScope scope(Env());
80
- LZMA::After(Env(), this->lzma);
81
- }
82
-
83
- private:
84
- LZMA *lzma;
85
- };
86
-
87
- #endif // NODE_LIBLZMA_H
package/src/lzma.coffee DELETED
@@ -1,346 +0,0 @@
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
- path = require 'path'
20
- binding_path = path.resolve path.join __dirname, '..'
21
-
22
- liblzma = require('node-gyp-build')(binding_path)
23
- util = require 'util'
24
- assert = require 'assert'
25
- os = require 'os'
26
- { Transform } = require 'stream'
27
-
28
- # Should not change over time... :)
29
- maxThreads = os.cpus().length
30
-
31
- class XzStream extends Transform
32
- constructor: (mode, @_opts = {}, options) ->
33
- super options
34
-
35
- @_opts.check ?= exports.check.NONE
36
- @_opts.preset ?= exports.preset.DEFAULT
37
- @_opts.filters ?= [exports.filter.LZMA2]
38
- @_opts.mode ?= exports.mode.NORMAL
39
- @_opts.threads ?= 1
40
-
41
- @_chunkSize = if @_opts.chunkSize then @_opts.chunkSize else liblzma.BUFSIZ
42
-
43
- # By default no flush, since there is no LZMA_NO_SYNC, we stick to
44
- # default LZMA_RUN (0)
45
- @_flushFlag = @_opts.flushFlag or liblzma.LZMA_RUN
46
-
47
- assert Array.isArray(@_opts.filters), "Filters need to be in an array!"
48
-
49
- # Add default filter LZMA2 if none provided
50
- if @_opts.filters.indexOf(exports.filter.LZMA2) is -1
51
- @_opts.filters.push exports.filter.LZMA2
52
-
53
- # Multithreading is only available for encoding, so if we are encoding, check
54
- # opts threads value.
55
- if mode is liblzma.STREAM_ENCODE
56
- @_opts.threads = 1 unless liblzma.STREAM_ENCODE_MT
57
-
58
- # By default set to maximum available processors
59
- if @_opts.threads is 0
60
- @_opts.threads = maxThreads
61
-
62
- mode = liblzma.STREAM_ENCODE_MT if @_opts.threads > 1
63
-
64
- # Initialize engine
65
- @lzma = new liblzma.LZMA mode, @_opts
66
- @_closed = false
67
- @_hadError = false
68
- @_offset = 0
69
- @_buffer = Buffer.alloc @_chunkSize
70
-
71
- @on 'onerror', (errno) =>
72
- @_hadError = true
73
- error = new Error exports.messages[errno]
74
- error.errno = errno
75
- error.code = errno
76
-
77
- @emit 'error', error
78
-
79
- @once 'end', @close
80
-
81
- flush: (kind, callback) ->
82
- ws = @_writableState
83
-
84
- if (typeof kind == 'function' or (typeof kind == 'undefined' && !callback))
85
- [callback, kind] = [kind, liblzma.LZMA_SYNC_FLUSH]
86
-
87
- if ws.ended
88
- if callback
89
- process.nextTick callback
90
- else if ws.ending
91
- if callback
92
- @once 'end', callback
93
- else if ws.needDrain
94
- @once 'drain', =>
95
- @flush callback
96
- return
97
- else
98
- @_flushFlag = kind
99
- @write Buffer.alloc(0), '', callback
100
-
101
- return
102
-
103
- close: (callback) ->
104
- if callback
105
- process.nextTick callback
106
-
107
- # We will trigger this case with #xz and #unxz
108
- return if @_closed
109
-
110
- @lzma.close()
111
- @_closed = true
112
-
113
- process.nextTick =>
114
- @emit 'close'
115
- return
116
-
117
- return
118
-
119
- _transform: (chunk, encoding, callback) ->
120
- flushFlag = undefined
121
- ws = @_writableState
122
- ending = ws.ending or ws.ended
123
- last = ending and (not chunk or ws.length is chunk.length)
124
- return callback(new Error("invalid input")) if chunk != null and !(chunk instanceof Buffer)
125
- return callback(new Error("lzma binding closed")) if @_closed
126
-
127
- # If it's the last chunk, or a final flush, we use the LZMA_FINISH flush flag.
128
- # If it's explicitly flushing at some other time, then we use
129
- # LZMA_SYNC_FLUSH.
130
- if last
131
- flushFlag = liblzma.LZMA_FINISH
132
- else
133
- flushFlag = @_flushFlag
134
-
135
- # once we've flushed the last of the queue, stop flushing and
136
- # go back to the normal behavior.
137
- @_flushFlag = @_opts.flushFlag or liblzma.LZMA_RUN if chunk.length >= ws.length
138
-
139
- @_processChunk chunk, flushFlag, callback
140
- return
141
-
142
- _flush: (callback) ->
143
- @_transform Buffer.alloc(0), '', callback
144
- return
145
-
146
- _processChunk: (chunk, flushFlag, cb) ->
147
- # If user setting async is set to true, then it will all depend on whether
148
- # we can actually be async, or not. If user set explicitly async to false
149
- # then whether we have a callback or not becomes irrelevant..
150
- # TODO: Works in v0.11
151
- #async = util.isFunction cb
152
- # until then...
153
- async = typeof cb == 'function'
154
-
155
- # Sanity checks
156
- assert !@_closed, "Stream closed!"
157
-
158
- availInBefore = chunk && chunk.length
159
- availOutBefore = @_chunkSize - @_offset
160
- inOff = 0
161
-
162
- # So far it looks like Zlib _processChunk in CoffeeScript. But to make C++
163
- # code easier when it comes to emitting events and callback calling sync/async
164
- # we handle error codes here. If anything wrong is returned, we emit event
165
- # and return false in case we are synchronous.
166
- callback = (errno, availInAfter, availOutAfter) =>
167
- return if @_hadError
168
-
169
- # if LZMA engine returned something else, we are running into trouble!
170
- if errno isnt liblzma.LZMA_OK and errno isnt liblzma.LZMA_STREAM_END
171
- @emit 'onerror', errno
172
- return false
173
-
174
- used = availOutBefore - availOutAfter
175
- assert used >= 0, "More bytes after than before! Delta = #{used}"
176
-
177
- if used > 0
178
- out = @_buffer[ @_offset ... @_offset + used ]
179
- @_offset += used
180
- if async
181
- @push out
182
- else
183
- buffers.push out
184
- nread += used
185
-
186
- # exhausted the output buffer, or used all the input create a new one.
187
- if availOutAfter is 0 or @_offset >= @_chunkSize
188
- availOutBefore = @_chunkSize
189
- @_offset = 0
190
- @_buffer = Buffer.alloc @_chunkSize
191
-
192
- if availOutAfter is 0 or availInAfter > 0
193
- inOff += (availInBefore - availInAfter)
194
- availInBefore = availInAfter
195
-
196
- return true unless async
197
- @lzma.code flushFlag, chunk, inOff, availInBefore, @_buffer, @_offset, callback
198
- return
199
-
200
- return false unless async
201
- cb()
202
- return
203
-
204
- unless async
205
- # Doing it synchronously
206
- buffers = []
207
- nread = 0
208
-
209
- error = null
210
- @on 'error', (e) -> error = e
211
-
212
- loop
213
- res = @lzma.codeSync flushFlag, chunk, inOff, availInBefore, @_buffer, @_offset
214
- break unless not @_hadError and callback res[0], res[1], res[2]
215
-
216
- throw error if @_hadError
217
- @close()
218
-
219
- buf = Buffer.concat buffers, nread
220
- return buf
221
-
222
- @lzma.code flushFlag, chunk, inOff, availInBefore, @_buffer, @_offset, callback
223
-
224
- return
225
-
226
- class Xz extends XzStream
227
- constructor: (lzma_options, options) ->
228
- super liblzma.STREAM_ENCODE, lzma_options, options
229
-
230
- class Unxz extends XzStream
231
- constructor: (lzma_options, options) ->
232
- super liblzma.STREAM_DECODE, lzma_options, options
233
-
234
-
235
- exports.Xz = Xz
236
- exports.Unxz = Unxz
237
-
238
- exports.hasThreads = ->
239
- typeof liblzma.STREAM_ENCODE_MT != 'undefined'
240
-
241
- exports.messages = [
242
- "Operation completed successfully"
243
- "End of stream was reached"
244
- "Input stream has no integrity check"
245
- "Cannot calculate the integrity check"
246
- "Integrity check type is not available"
247
- "Cannot allocate memory"
248
- "Memory usage limit was reached"
249
- "File format not recognized"
250
- "Invalid or unsupported options"
251
- "Data is corrupt"
252
- "No progress is possible"
253
- "Programming error"
254
- ]
255
-
256
- exports.check = {
257
- "NONE": liblzma.LZMA_CHECK_NONE
258
- "CRC32": liblzma.LZMA_CHECK_CRC32
259
- "CRC64": liblzma.LZMA_CHECK_CRC64
260
- "SHA256": liblzma.LZMA_CHECK_SHA256
261
- }
262
-
263
- exports.preset = {
264
- "DEFAULT": liblzma.LZMA_PRESET_DEFAULT
265
- "EXTREME": liblzma.LZMA_PRESET_EXTREME
266
- }
267
-
268
- exports.flag = {
269
- "TELL_NO_CHECK": liblzma.LZMA_TELL_NO_CHECK
270
- "TELL_UNSUPPORTED_CHECK": liblzma.LZMA_TELL_UNSUPPORTED_CHECK
271
- "TELL_ANY_CHECK": liblzma.LZMA_TELL_ANY_CHECK
272
- "CONCATENATED": liblzma.LZMA_CONCATENATED
273
- }
274
-
275
- exports.filter = {
276
- "LZMA2": liblzma.LZMA_FILTER_LZMA2
277
- "X86": liblzma.LZMA_FILTER_X86
278
- "POWERPC": liblzma.LZMA_FILTER_POWERPC
279
- "IA64": liblzma.LZMA_FILTER_IA64
280
- "ARM": liblzma.LZMA_FILTER_ARM
281
- "ARMTHUMB": liblzma.LZMA_FILTER_ARMTHUMB
282
- "SPARC": liblzma.LZMA_FILTER_SPARC
283
- }
284
-
285
- exports.mode = {
286
- "FAST": liblzma.LZMA_MODE_FAST
287
- "NORMAL": liblzma.LZMA_MODE_NORMAL
288
- }
289
-
290
- exports.createXz = (lzma_options, options) ->
291
- new Xz(lzma_options, options)
292
-
293
- exports.createUnxz = (lzma_options, options) ->
294
- new Unxz(lzma_options, options)
295
-
296
- exports.unxz = (buffer, opts, callback) ->
297
- if typeof opts == 'function'
298
- [callback, opts] = [opts, {}]
299
-
300
- xzBuffer new Unxz(opts), buffer, callback
301
-
302
- exports.unxzSync = (buffer, opts) ->
303
- xzBufferSync new Unxz(opts), buffer
304
-
305
- exports.xz = (buffer, opts, callback) ->
306
- if typeof opts == 'function'
307
- [callback, opts] = [opts, {}]
308
- xzBuffer new Xz(opts), buffer, callback
309
-
310
- exports.xzSync = (buffer, opts) ->
311
- xzBufferSync new Xz(opts), buffer
312
-
313
- xzBuffer = (engine, buffer, callback) ->
314
- buffers = []
315
- nread = 0
316
- flow = ->
317
- chunk = undefined
318
- while null isnt (chunk = engine.read())
319
- buffers.push chunk
320
- nread += chunk.length
321
- engine.once 'readable', flow
322
- return
323
- onEnd = ->
324
- buf = Buffer.concat(buffers, nread)
325
- buffers = []
326
- callback null, buf
327
- engine.close()
328
- return
329
- onError = (err) ->
330
- engine.removeListener 'end', onEnd
331
- engine.removeListener 'readable', flow
332
- callback err
333
- return
334
- engine.on 'error', onError
335
- engine.on 'end', onEnd
336
- engine.end buffer
337
- flow()
338
- return
339
-
340
- xzBufferSync = (engine, buffer) ->
341
- buffer = Buffer.from(buffer) if typeof buffer == 'string'
342
- throw new TypeError("Not a string or buffer") unless buffer instanceof Buffer
343
-
344
- engine._processChunk buffer, liblzma.LZMA_FINISH
345
-
346
- module.exports = exports