Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
combined-stream
Advanced tools
The combined-stream npm package is used to create a stream that emits events from multiple other streams in sequence. It is useful for treating a series of streams as a single continuous stream, which can be particularly handy when dealing with file uploads, data concatenation, or stream transformation.
Creating a combined stream from multiple sources
This feature allows you to combine multiple readable streams into one. In the code sample, two text files are read as streams and appended to the combined stream, which is then piped to the standard output.
const CombinedStream = require('combined-stream');
const fs = require('fs');
const combinedStream = CombinedStream.create();
combinedStream.append(fs.createReadStream('file1.txt'));
combinedStream.append(fs.createReadStream('file2.txt'));
combinedStream.pipe(process.stdout);
Appending streams with delayed execution
This feature allows you to append streams with a function that will be called when the stream is ready for more data. The code sample demonstrates appending a stream with a function that provides the stream asynchronously.
const CombinedStream = require('combined-stream');
const fs = require('fs');
const combinedStream = CombinedStream.create();
combinedStream.append(next => {
next(fs.createReadStream('file3.txt'));
});
combinedStream.pipe(process.stdout);
Appending data directly
This feature allows you to append raw data directly to the combined stream. The code sample shows how to append string data to the stream, which is then piped to the standard output.
const CombinedStream = require('combined-stream');
const combinedStream = CombinedStream.create();
combinedStream.append('Hello ');
combinedStream.append('World!');
combinedStream.pipe(process.stdout);
Multistream is similar to combined-stream in that it allows you to combine multiple streams into a single stream. However, multistream provides more control over the order and manner in which streams are combined, such as lazy stream creation and handling of stream errors.
Pumpify combines streams using pump and stream.pipeline under the hood for better error handling. It is similar to combined-stream but focuses on creating a pipeline of transform streams that can be treated as a single duplex stream.
Stream-combiner2 is a successor to stream-combiner, and it is used to turn a pipeline of streams into a single duplex stream. It differs from combined-stream by focusing on transforming streams and providing a duplex interface.
A stream that emits multiple other streams one after another.
NB Currently combined-stream
works with streams version 1 only. There is ongoing effort to switch this library to streams version 2. Any help is welcome. :) Meanwhile you can explore other libraries that provide streams2 support with more or less compatibility with combined-stream
.
combined-stream2: A drop-in streams2-compatible replacement for the combined-stream module.
multistream: A stream that emits multiple other streams one after another.
npm install combined-stream
Here is a simple example that shows how you can use combined-stream to combine two files into one:
var CombinedStream = require('combined-stream');
var fs = require('fs');
var combinedStream = CombinedStream.create();
combinedStream.append(fs.createReadStream('file1.txt'));
combinedStream.append(fs.createReadStream('file2.txt'));
combinedStream.pipe(fs.createWriteStream('combined.txt'));
While the example above works great, it will pause all source streams until
they are needed. If you don't want that to happen, you can set pauseStreams
to false
:
var CombinedStream = require('combined-stream');
var fs = require('fs');
var combinedStream = CombinedStream.create({pauseStreams: false});
combinedStream.append(fs.createReadStream('file1.txt'));
combinedStream.append(fs.createReadStream('file2.txt'));
combinedStream.pipe(fs.createWriteStream('combined.txt'));
However, what if you don't have all the source streams yet, or you don't want
to allocate the resources (file descriptors, memory, etc.) for them right away?
Well, in that case you can simply provide a callback that supplies the stream
by calling a next()
function:
var CombinedStream = require('combined-stream');
var fs = require('fs');
var combinedStream = CombinedStream.create();
combinedStream.append(function(next) {
next(fs.createReadStream('file1.txt'));
});
combinedStream.append(function(next) {
next(fs.createReadStream('file2.txt'));
});
combinedStream.pipe(fs.createWriteStream('combined.txt'));
Returns a new combined stream object. Available options are:
maxDataSize
pauseStreams
The effect of those options is described below.
true
Whether to apply back pressure to the underlaying streams. If set to false
,
the underlaying streams will never be paused. If set to true
, the
underlaying streams will be paused right after being appended, as well as when
delayedStream.pipe()
wants to throttle.
2 * 1024 * 1024
The maximum amount of bytes (or characters) to buffer for all source streams.
If this value is exceeded, combinedStream
emits an 'error'
event.
0
The amount of bytes (or characters) currently buffered by combinedStream
.
Appends the given stream
to the combinedStream object. If pauseStreams
is
set to `true, this stream will also be paused right away.
streams
can also be a function that takes one parameter called next
. next
is a function that must be invoked in order to provide the next
stream, see
example above.
Regardless of how the stream
is appended, combined-stream always attaches an
'error'
listener to it, so you don't have to do that manually.
Special case: stream
can also be a String or Buffer.
You should not call this, combinedStream
takes care of piping the appended
streams into itself for you.
Causes combinedStream
to start drain the streams it manages. The function is
idempotent, and also emits a 'resume'
event each time which usually goes to
the stream that is currently being drained.
If combinedStream.pauseStreams
is set to false
, this does nothing.
Otherwise a 'pause'
event is emitted, this goes to the stream that is
currently being drained, so you can use it to apply back pressure.
Sets combinedStream.writable
to false, emits an 'end'
event, and removes
all streams from the queue.
Same as combinedStream.end()
, except it emits a 'close'
event instead of
'end'
.
combined-stream is licensed under the MIT license.
FAQs
A stream that emits multiple other streams one after another.
The npm package combined-stream receives a total of 39,709,379 weekly downloads. As such, combined-stream popularity was classified as popular.
We found that combined-stream demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.