
Research
Security News
Malicious PyPI Package Exploits Deezer API for Coordinated Music Piracy
Socket researchers uncovered a malicious PyPI package exploiting Deezer’s API to enable coordinated music piracy through API abuse and C2 server control.
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
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');
writeableStream.end(); // promise is resolved here
var 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 683,198 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.
Research
Security News
Socket researchers uncovered a malicious PyPI package exploiting Deezer’s API to enable coordinated music piracy through API abuse and C2 server control.
Research
The Socket Research Team discovered a malicious npm package, '@ton-wallet/create', stealing cryptocurrency wallet keys from developers and users in the TON ecosystem.
Security News
Newly introduced telemetry in devenv 1.4 sparked a backlash over privacy concerns, leading to the removal of its AI-powered feature after strong community pushback.