Comparing version 1.0.1 to 1.1.0
121
lib/pipe.js
@@ -5,91 +5,60 @@ (function () { | ||
var fs = require('fs'), | ||
zlib = require('zlib'), | ||
Util = require('util-io'), | ||
type = Util.type; | ||
Util = require('util-io'); | ||
module.exports = create; | ||
module.exports.getBody = getBody; | ||
module.exports = all; | ||
module.exports.getBody = getBody; | ||
/** | ||
* create pipe | ||
* | ||
* @param read - readable stream | ||
* @param write - writable stream | ||
* | ||
* @param options { | ||
* gzip | ||
* ungzip | ||
* notEnd | ||
* } | ||
* | ||
* @param callback - function(error) {} | ||
*/ | ||
function create(read, write, options, callback) { | ||
var gzip, isFsWrite, | ||
isStrRead = type.string(read), | ||
isStrWrite = type.string(write), | ||
isFunc = type.function(options), | ||
o = {}, | ||
optionsRead = { | ||
bufferSize: 4 * 1024 | ||
}; | ||
function on(event, emitter, callback) { | ||
var isSet, | ||
listeners = emitter.listeners(event), | ||
callbackStr = '' + callback; | ||
Util.checkArgs(arguments, ['read', 'write', 'callback']); | ||
isSet = listeners.some(function(func) { | ||
return '' + func === callbackStr; | ||
}); | ||
if (isFunc) | ||
callback = options; | ||
else | ||
o = options; | ||
if (options.range) | ||
Util.extend(optionsRead, { | ||
start : o.range.start, | ||
end : o.range.end, | ||
}); | ||
if (!isSet) | ||
emitter.on(event, callback); | ||
} | ||
function all(streams, options, callback) { | ||
var n, write, isFSWrite; | ||
Util.check(arguments, ['streams', 'callback']); | ||
if (isStrRead) | ||
read = fs.createReadStream(read, optionsRead); | ||
n = streams.length - 1; | ||
write = streams[n]; | ||
isFSWrite = write instanceof fs.WriteStream; | ||
if (isStrWrite) { | ||
write = fs.createWriteStream(write); | ||
isFsWrite = true; | ||
} | ||
Util.exec.if(!isFSWrite, function() { | ||
pipe(streams, options, callback); | ||
}, function(callback) { | ||
write.on('open', callback); | ||
}); | ||
} | ||
function pipe(streams, options, callback) { | ||
var main, | ||
read = streams[0]; | ||
if (o.gzip || o.gunzip) { | ||
if (o.gzip) | ||
gzip = zlib.createGzip(); | ||
else | ||
gzip = zlib.createGunzip(); | ||
if (!callback) { | ||
callback = options; | ||
on('error', read, callback); | ||
read = read.pipe(gzip); | ||
options = { | ||
end: true | ||
}; | ||
} | ||
on('error', write, callback); | ||
on('error', read, callback); | ||
Util.exec.if(!isFsWrite, function() { | ||
read.pipe(write, { | ||
end: !o.notEnd | ||
}); | ||
streams.forEach(function(stream) { | ||
on('error', stream, callback); | ||
on('end', read, callback); | ||
}, function(callback) { | ||
on('open', write, callback); | ||
if (!main) | ||
main = stream; | ||
else | ||
main = main.pipe(stream, { | ||
end: options.end | ||
}); | ||
}); | ||
} | ||
function on(event, emitter, callback) { | ||
var isSet, | ||
listeners = emitter.listeners(event), | ||
callbackStr = '' + callback; | ||
isSet = listeners.some(function(func) { | ||
return '' + func === callbackStr; | ||
}); | ||
if (!isSet) | ||
emitter.on(event, callback); | ||
on('end', read, callback); | ||
} | ||
@@ -106,3 +75,3 @@ | ||
Util.checkArgs(arguments, ['readStream', 'callback']); | ||
Util.check(arguments, ['readStream', 'callback']); | ||
@@ -109,0 +78,0 @@ readStream.on('data', function(chunk) { |
{ | ||
"name": "pipe-io", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)", | ||
@@ -12,3 +12,3 @@ "description": "Pipe streams and handle events", | ||
"dependencies": { | ||
"util-io": "~1.5.0" | ||
"util-io": "~1.6.0" | ||
}, | ||
@@ -15,0 +15,0 @@ "license": "MIT", |
Pipe-io | ||
========= | ||
Pipe streams and handle events | ||
Pipe streams and handle events. | ||
@@ -8,21 +8,21 @@ ## Install | ||
``` | ||
npm i pipe-io | ||
npm i pipe-io --save | ||
``` | ||
## API | ||
### create | ||
Easy way to create pipe which would handle all error events and redirect tham to callback. | ||
### pipe | ||
Create pipe between streams and add on callback wich would | ||
handle any error or will be called when everything done. | ||
```js | ||
var pipe = require('pipe-io'), | ||
NameFrom = 'README.md', | ||
NameTo = 'README_COPY.gz', | ||
options = { | ||
gzip: true | ||
}; | ||
fs = require('fs'), | ||
NAME = 'README.md', | ||
NAME2 = 'README2.md', | ||
readStream = fs.createReadStream(NAME), | ||
writeStream = fs.createWritesStream(NAME2); | ||
pipe(NameFrom, NameTo, options, function(error) { | ||
var msg = 'done'; | ||
console.log(error || msg); | ||
pipe([readStream, writeStream], function(error) { | ||
console.log(error || 'done'); | ||
}); | ||
@@ -29,0 +29,0 @@ ``` |
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
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
4988
70
+ Addedutil-io@1.6.5(transitive)
- Removedutil-io@1.5.1(transitive)
Updatedutil-io@~1.6.0