![Fluent Assertions Faces Backlash After Abandoning Open Source Licensing](https://cdn.sanity.io/images/cgdhsj6q/production/98cc622027c44eed628584f02cb3b6e79be011c7-1500x1500.webp?w=400&fit=max&auto=format)
Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
promisepipe
Advanced tools
The 'promisepipe' npm package is designed to facilitate the piping of streams with the added benefit of returning a promise. This can be particularly useful for handling asynchronous stream operations in a more manageable and readable way.
Basic Stream Piping
This feature allows you to pipe a readable stream to a writable stream and returns a promise that resolves when the piping is complete. In this example, a file is read from 'source.txt' and written to 'destination.txt'.
const fs = require('fs');
const promisepipe = require('promisepipe');
async function pipeStreams() {
const readStream = fs.createReadStream('source.txt');
const writeStream = fs.createWriteStream('destination.txt');
await promisepipe(readStream, writeStream);
console.log('Piping complete');
}
pipeStreams();
Handling Multiple Streams
This feature allows you to pipe multiple streams in sequence. In this example, a file is read from 'source.txt', compressed using gzip, and then written to 'destination.txt.gz'.
const fs = require('fs');
const zlib = require('zlib');
const promisepipe = require('promisepipe');
async function pipeMultipleStreams() {
const readStream = fs.createReadStream('source.txt');
const gzipStream = zlib.createGzip();
const writeStream = fs.createWriteStream('destination.txt.gz');
await promisepipe(readStream, gzipStream, writeStream);
console.log('Piping and compression complete');
}
pipeMultipleStreams();
The 'pump' package is used to pipe streams together and handle errors properly. It is similar to 'promisepipe' but does not return a promise. Instead, it uses a callback to signal completion or errors.
Safely pipe node.js streams while capturing all errors to a single promise.
npm install promisepipe
promisePipe(<readable stream>, [transform streams...], <writeable stream>)
It returns a native promise. On success the resolved value will be an array of the streams passed in. When rejected an error object is created with following keys:
source
: The stream that caused the errororiginalError
: Original error from the streammessage
: The error message from original errorNote: the last stream in the chain needs to be a writable stream, not a duplex/transform stream. If you use a 3rd party library which returns deplux streams instead of writable streams, you'll need to add something like .pipe(devnull())
to the end, otherwise the promise will never resolve (#16).
Starting with v3, all streams are destroyed if there's an error to prevent memory leaks.
var promisePipe = require("promisepipe");
promisePipe(
fs.createReadStream(INPUT_FILE),
new UpcaseTransform(),
fs.createWriteStream(OUTPUT_FILE),
).then(function(streams){
console.log("Done writing to the output file stream!");
}, function(err) {
console.log("This stream failed:", err.source);
console.log("Original error was:", err.originalError);
});
or with async-wait
var promisePipe = require("promisepipe");
(async () => {
try {
await promisePipe(
fs.createReadStream(INPUT_FILE),
new UpcaseTransform(),
fs.createWriteStream(OUTPUT_FILE)
);
console.log("Done writing to the output file stream!");
} catch (err) {
console.log("This stream failed:", err.source);
console.log("Original error was:", err.originalError);
}
})();
Stream piping in node.js is cool, but error handling is not because streams do not bubble errors to the target streams.
For example if the previous example is written like this:
fs.createReadStream(INPUT_FILE)
.pipe(new UpcaseTransform())
.pipe(fs.createReadStream(OUTPUT_FILE))
It might crash your program at any time. You must handle the errors from each stream manually like this:
fs.createReadStream(INPUT_FILE).on("error", function(err) {
// handle the error
}).pipe(new UpcaseTransform()).on("error", function(err) {
// handle the error
}).pipe(fs.createReadStream(OUTPUT_FILE)).on("error", function(err) {
// handle the error
})
Handling errors this way can be very cumbersome. promisepipe
simplifies
error handling by sending the first error occurance into a promise.
FAQs
Pipe node.js streams safely with Promises
The npm package promisepipe receives a total of 478,154 weekly downloads. As such, promisepipe popularity was classified as popular.
We found that promisepipe demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 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
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.