Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
@samverschueren/stream-to-observable
Advanced tools
Convert Node Streams into ECMAScript-Observables
The @samverschueren/stream-to-observable npm package is designed to convert Node.js streams into observables. This allows developers to use reactive programming techniques with streams, making it easier to compose asynchronous or callback-based code in a more readable and expressive way. It supports both readable and writable streams.
Converting a readable stream to an observable
This feature allows you to convert a readable stream, like one returned by `fs.createReadStream`, into an observable. You can then use RxJS operators to process the data events emitted by the stream.
const toObservable = require('@samverschueren/stream-to-observable');
const fs = require('fs');
const stream = fs.createReadStream('file.txt');
const observable = toObservable(stream);
observable.subscribe({
next(data) { console.log(data.toString()); },
error(err) { console.error('Something went wrong: ', err); },
complete() { console.log('Done reading file'); }
});
Converting a writable stream to an observable
This feature enables the conversion of a writable stream into an observable. It is particularly useful for tracking when the stream has finished writing.
const toObservable = require('@samverschueren/stream-to-observable');
const { Writable } = require('stream');
const writableStream = new Writable({
write(chunk, encoding, callback) {
console.log(chunk.toString());
callback();
}
});
const observable = toObservable(writableStream);
observable.subscribe({
complete() { console.log('Done writing to stream'); }
});
RxJS is a library for reactive programming using Observables, to make it easier to compose asynchronous or callback-based code. While not a direct alternative, it provides the foundational Observable implementation that @samverschueren/stream-to-observable relies on. It offers more comprehensive features for creating and manipulating observables but does not specifically focus on converting streams to observables.
from2 is a high-level module for creating readable streams that properly handle backpressure, similar to how @samverschueren/stream-to-observable works with existing streams. However, from2 focuses on creating streams from data sources rather than converting them to observables.
stream-to-promise is another utility that deals with stream completion, but instead of converting streams to observables, it converts them to promises. This is useful for async/await patterns but does not offer the continuous data handling capabilities of observables.
Convert Node Streams into ECMAScript-Observables
Observables
are rapidly gaining popularity. They have much in common with Streams, in that they both represent data that arrives over time. Most Observable implementations provide expressive methods for filtering and mutating incoming data. Methods like .map()
, .filter()
, and .forEach
behave very similarly to their Array counterparts, so using Observables can be very intuitive.
Note: This module was forked from stream-to-observable
and released under a different name due to inactivity.
$ npm install --save @samverschueren/stream-to-observable
stream-to-observable
relies on any-observable
, which will search for an available Observable implementation. You need to install one yourself:
$ npm install --save zen-observable
or
$ npm install --save rxjs
If your code relies on a specific Observable implementation, you should likely specify one using any-observable
s registration shortcuts.
const fs = require('fs');
const split = require('split');
const streamToObservable = require('@samverschueren/stream-to-observable');
const readStream = fs
.createReadStream('./hello-world.txt', {encoding: 'utf8'})
.pipe(split());
streamToObservable(readStream)
.filter(chunk => /hello/i.test(chunk))
.map(chunk => chunk.toUpperCase())
.forEach(chunk => {
console.log(chunk); // only the lines containing "hello" - and they will be capitalized
});
The split
module above will chunk the stream into individual lines. This is often very handy for text streams, as each observable event is guaranteed to be a line.
Type: ReadableStream
Note:
stream
can technically be any EventEmitter
instance. By default, this module listens to the standard Stream events (data
, error
, and end
), but those are configurable via the options
parameter. If you are using this with a standard Stream, you likely won't need the options
parameter.
Type: Promise
If provided, the Observable will not "complete" until await
is resolved. If await
is rejected, the Observable will immediately emit an error
event and disconnect from the stream. This is mostly useful when attaching to the stdin
or stdout
streams of a child_process
. Those streams usually do not emit error
events, even if the underlying process exits with an error. This provides a means to reject the Observable if the child process exits with an unexpected error code.
Type: String
or false
Default: "end"
If you are using an EventEmitter
or non-standard Stream, you can change which event signals that the Observable should be completed.
Setting this to false
will avoid listening for any end events.
Setting this to false
and providing an await
Promise will cause the Observable to resolve immediately with the await
Promise (the Observable will remove all it's data
event listeners from the stream once the Promise is resolved).
Type: String
or false
Default: "error"
If you are using an EventEmitter
or non-standard Stream, you can change which event signals that the Observable should be closed with an error.
Setting this to false
will avoid listening for any error events.
Type: String
Default: "data"
If you are using an EventEmitter
or non-standard Stream, you can change which event causes data to be emitted to the Observable.
rxjs
observables - Observables implementationzen-observables
- Observables implementationdata
events on the stream will be emitted as events in the Observable. Since most native streams emit chunks
of binary data, you will likely want to use a TransformStream
to convert those chunks of binary data into an object stream. split
is just one popular TransformStream that splits streams into individual lines of text.
It's important to note that using this module disables back-pressure controls on the stream. As such, it should not be used where back-pressure throttling is required (i.e. high volume web servers). It still has value for larger projects, as it can make unit testing streams much cleaner.
MIT
FAQs
Convert Node Streams into ECMAScript-Observables
The npm package @samverschueren/stream-to-observable receives a total of 1,425,085 weekly downloads. As such, @samverschueren/stream-to-observable popularity was classified as popular.
We found that @samverschueren/stream-to-observable demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.