lzma-native
Advanced tools
Comparing version 0.0.1 to 0.0.2
84
index.js
@@ -0,1 +1,19 @@ | ||
/** | ||
* lzma-native - Node.js bindings for liblzma | ||
* Copyright (C) 2014 Hauke Henningsen <sqrt@entless.org> | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
**/ | ||
(function() { | ||
@@ -7,2 +25,4 @@ 'use strict'; | ||
var util = require('util'); | ||
var fs = require('fs'); | ||
var simpleDuplex = require('./simpleDuplex'); | ||
var _ = require('underscore'); | ||
@@ -12,3 +32,6 @@ | ||
exports.Stream.prototype.getStream = function(options) { | ||
var Stream = exports.Stream; | ||
Stream.prototype.getStream = | ||
Stream.prototype.syncStream = function(options) { | ||
var nativeStream = this; | ||
@@ -36,2 +59,61 @@ | ||
Stream.prototype.asyncStream = native.asyncCodeAvailable ? function(options) { | ||
var endpoints = this.asyncCode_(); | ||
if (!endpoints || endpoints.length != 2) | ||
throw Error('asyncStream() could not successfully call underlying asyncCode_()'); | ||
var readStream = fs.createReadStream (null, { fd: endpoints[0] }); | ||
var writeStream = fs.createWriteStream(null, { fd: endpoints[1] }); | ||
// keep the main lzma object alive during threaded compression | ||
readStream._lzma = this; | ||
readStream.on('end', function() { readStream._lzma = null; }); | ||
return new simpleDuplex.SimpleDuplex(options, readStream, writeStream); | ||
} : function() { | ||
// no native asyncCode, so we just use the synchronous method | ||
return this.getStream(); | ||
}; | ||
Stream.prototype.rawEncoder = function(options) { | ||
return this.rawEncoder_(options.filters || []); | ||
}; | ||
Stream.prototype.rawDecoder = function(options) { | ||
return this.rawDecoder_(options.filters || []); | ||
}; | ||
Stream.prototype.easyEncoder = function(options) { | ||
return this.easyEncoder_(options.preset || exports.PRESET_DEFAULT, options.check || exports.CHECK_CRC32); | ||
}; | ||
Stream.prototype.streamEncoder = function(options) { | ||
return this.streamEncoder_(options.filters || [], options.check || exports.CHECK_CRC32); | ||
}; | ||
Stream.prototype.streamDecoder = function(options) { | ||
return this.streamDecoder_(options.memlimit || null, options.flags || 0); | ||
}; | ||
Stream.prototype.autoDecoder = function(options) { | ||
return this.autoDecoder_(options.memlimit || null, options.flags || 0); | ||
}; | ||
Stream.prototype.aloneDecoder = function(options) { | ||
return this.aloneDecoder_(options.memlimit || null); | ||
}; | ||
exports.createStream = function(coder, options) { | ||
coder = coder || 'easyEncoder'; | ||
options = options || {}; | ||
var stream = new Stream(); | ||
_.bind(stream[coder], stream)(options); | ||
if (options.memlimit) | ||
stream.memlimitSet(options.memlimit); | ||
return options.synchronous ? stream.syncStream() : stream.asyncStream(); | ||
}; | ||
})(); |
{ | ||
"name": "lzma-native", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"author": { | ||
@@ -5,0 +5,0 @@ "name": "Hauke Henningsen", |
@@ -6,18 +6,70 @@ lzma-native | ||
=========== | ||
## Example usage: | ||
Example usage: | ||
The simplest possible usage: | ||
```js | ||
var lzma = require('lzma-native'); | ||
var stream = new lzma.Stream(); | ||
stream.easyEncoder({preset: 6, check: lzma.CHECK_CRC32}); | ||
var encoder = lzma.createStream(); | ||
var input = fs.createReadStream(...); | ||
var output = fs.createWriteStream(...); | ||
process.stdin.pipe(encoder).pipe(process.stdout); | ||
``` | ||
input.pipe(stream.getStream()).pipe(output); | ||
This mimicks the functionality of the `xz` command line util. | ||
Equivalently, one could have written | ||
```js | ||
var encoder = lzma.createStream('easyEncoder', {preset: lzma.PRESET_DEFAULT, check: lzma.CHECK_CRC32}) | ||
``` | ||
The API is loosely based on the native API, with the `stream.getStream()` method added for convenience. | ||
or, for stronger and slower compression: | ||
```js | ||
var encoder = lzma.createStream('easyEncoder', {preset: 9}) | ||
``` | ||
Here `easyEncoder` corresponds to the `xz` command line util, resp. its file format [.xz](https://en.wikipedia.org/wiki/.xz). | ||
For the older `.lzma` format, you can just use `aloneEncoder` instead. | ||
The API is loosely based on the native API, with a few bits of wrapper code added for convenience. | ||
Methods like `stream.code` and `lzma.crc32` accept Node.js `Buffer`s as arguments. | ||
Unless you set `.synchronous = true` in `createStream`s second parameter, the libary will use its | ||
own thread for compression (if compiled with support for that). | ||
The `encoder` object here is an instance of `stream.Duplex` (see the [Node.js docs](http://nodejs.org/api/stream.html)), | ||
so you could also manually perform any of the write and read operations that you’re familiar with on it. | ||
## List of encoders/decoders and options | ||
Encoders and decoders you *probably* are interested in: | ||
* `easyEncoder`: Creates `.xz` files. Supports `.preset` and `.check` options. | ||
* `aloneEncoder`: Creates `.lzma` files. Supports `.preset` and a bunch of very specific options (see the liblzma C headers for details) | ||
* `autoDecoder`: Supports various flags. Detects input type automatically. | ||
That is, the following is essentially (a quite slow version of) `cat`: | ||
```js | ||
var encoder = lzma.createStream('easyEncoder'); | ||
var decoder = lzma.createStream('autoDecoder'); | ||
process.stdin.pipe(encoder).pipe(decoder).pipe(process.stdout); | ||
``` | ||
If you know specifically what you want, you may also look into these encoders: | ||
* `rawDecoder`: Supports `.filters`. | ||
* `rawEncoder`: Supports `.filters`. | ||
* `streamEncoder`: Supports `.filters` and `.check`. | ||
* `streamDecoder`: Supports various flags. | ||
* `aloneDecoder`: Supports various flags. | ||
Also, all encoders accept a `.memlimit` option. | ||
Note that, due to the lack of 64-bit integer support in Javascript, | ||
options regarding memory limits have limited range. | ||
## Installation | ||
This package requires that you have the corresponding C library installed, | ||
e. g. via `sudo apt-get install liblzma-dev` or your equivalent of that. | ||
You can also download the [source](http://tukaani.org/xz/) from the original author. | ||
You may also need a somewhat recent C++ compiler, and asynchronous | ||
compression support requires `std::thread`, which is included in C++11. |
19
test.js
var fs = require('fs'); | ||
fs.unlink('output.xz', function() { | ||
fs.unlink('output1.xz', function() { | ||
fs.unlink('output2.xz', function() { | ||
var lzma = require('./'); | ||
var stream = new lzma.Stream(); | ||
//stream.aloneEncoder({preset: 6}); | ||
stream.easyEncoder({preset: 6, check: lzma.CHECK_CRC32}); | ||
var stream1 = lzma.createStream('easyEncoder', {preset: 6}); | ||
var stream2 = lzma.createStream('easyEncoder', {preset: 6}); | ||
//stream.easyEncoder({preset: 6, check: lzma.CHECK_CRC32}); | ||
var input = fs.createReadStream('/usr/bin/gdb'); | ||
var output = fs.createWriteStream('output.xz'); | ||
var input1 = fs.createReadStream('/usr/bin/gdb'); | ||
var input2 = fs.createReadStream('/usr/bin/gdb'); | ||
var output1 = fs.createWriteStream('output1.xz'); | ||
var output2 = fs.createWriteStream('output2.xz'); | ||
input.pipe(stream.getStream()).pipe(output); | ||
input1.pipe(stream1).pipe(output1); | ||
input2.pipe(stream2).pipe(output2); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 3 instances in 1 package
Copyleft License
License(Experimental) Copyleft license information was found.
Found 1 instance in 1 package
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
Non-permissive License
License(Experimental) A license not known to be considered permissive was found.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 1 instance in 1 package
Copyleft License
License(Experimental) Copyleft license information was found.
Found 1 instance in 1 package
Non-permissive License
License(Experimental) A license not known to be considered permissive was found.
Found 1 instance in 1 package
5411374
14
152
75
3
2
4