Security News
Supply Chain Attack Detected in Solana's web3.js Library
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
stream-to-promise
Advanced tools
The 'stream-to-promise' npm package is a utility that converts a stream into a promise. This is particularly useful when working with Node.js streams and you need to handle the completion of a stream operation using async/await or promise-based syntax.
Convert Stream to Promise
This feature allows you to convert a readable stream into a promise. The promise resolves when the stream ends, making it easier to handle stream completion in an async/await context.
const streamToPromise = require('stream-to-promise');
const fs = require('fs');
async function readFile() {
const readStream = fs.createReadStream('example.txt');
await streamToPromise(readStream);
console.log('Stream has ended');
}
readFile();
Handle Stream Errors
This feature demonstrates how to handle errors that occur during the stream operation. The promise will reject if the stream emits an 'error' event, allowing you to catch and handle the error using try/catch.
const streamToPromise = require('stream-to-promise');
const fs = require('fs');
async function readFile() {
const readStream = fs.createReadStream('nonexistent.txt');
try {
await streamToPromise(readStream);
} catch (error) {
console.error('Stream error:', error);
}
}
readFile();
'stream-to-array' is a package that collects all data from a stream into an array and returns a promise that resolves with the array. It is useful when you need to gather all chunks of data from a stream and process them together. Unlike 'stream-to-promise', which resolves when the stream ends, 'stream-to-array' also provides the collected data.
'get-stream' is a package that converts a stream into a string, buffer, or array, and returns a promise that resolves with the collected data. It offers more flexibility in terms of the output format compared to 'stream-to-promise', which only resolves when the stream ends without providing the data.
'promise-streams' is a package that provides a set of utilities for working with streams using promises. It includes functions for collecting stream data, piping streams, and more. It offers a broader range of functionalities compared to 'stream-to-promise', which focuses solely on converting a stream to a promise.
Convert streams (readable or writable) to promises
npm install --save stream-to-promise
streamToPromise(readableStream).then(function (buffer) {
// buffer.length === 3
})
readableStream.emit('data', new Buffer())
readableStream.emit('data', new Buffer())
readableStream.emit('data', new Buffer())
readableStream.emit('end') // promise is resolved here
streamToPromise(writableStream).then(function () {
// resolves undefined
})
writableStream.write('data')
writableStream.end() // promise is resolved here
const err = new Error()
streamToPromise(stream).catch(function (error) {
// error === err
})
stream.emit('error', err) // promise is rejected here
FAQs
Convert streams (readable or writable) to promises
The npm package stream-to-promise receives a total of 372,661 weekly downloads. As such, stream-to-promise popularity was classified as popular.
We found that stream-to-promise 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.
Security News
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.