node-liblzma 1.1.4 → 1.1.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/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