
Research
/Security News
Critical Vulnerability in NestJS Devtools: Localhost RCE via Sandbox Escape
A flawed sandbox in @nestjs/devtools-integration lets attackers run code on your machine via CSRF, leading to full Remote Code Execution (RCE).
magic-promises
Advanced tools
Syntax sugar for dealing with promises in a much simpler way for async-heavy workflows:
npm install magic-promises
Builds the promise chain internally, so your code can be used like you would do with sync strings, arrays, etc:
// With a bit of magic()
const value = await magic(data).map(op1).filter(op2).map(op3);
// NATIVE; the pure-javascript way of dealing with the same is a lot longer
const value = await Promise.all(data.map(op1)).then(files => files.filter(op2)).then(files => Promise.all(files.map(op3)));
// NATIVE; even when we try to make it more readable it is still longer:
let value = await Promise.all(data.map(op1));
value = value.filter(op2);
value = await Promise.all(value.map(op3));
The coolest bit is that there is no API. You can call the methods, properties, etc of the value that you pass to magic():
const value = await magic(3.1).toFixed(1).split('.').map(n => n * 2).join('.');
console.log(value);
// 6.2 (string)
Note: all these must be run in an
async
context. I am using the great librariesmz/fs
andgot
.
This library is specially useful if we want to do things like fetching urls, mapping their arrays, working with strings, etc. For instance, let's read all the files in the current directory:
// You can apply `.map()` straight to the output of magic()
const files = await magic(readdir(__dirname)).map(file => readFile(file, 'utf-8'));
// NATIVE; this is how you'd have to do with vanilla JS
const files = await readdir(__dirname).then(files => files.map(file => readFile(file, 'utf-8')));
// PRO; using my library `fs-array`, based on magic-promises, it gets better:
const files = await dir(__dirname).map(read);
Retrieve a bunch of websites with valid responses
// Retrieve the content of several pages
const urls = ['francisco.io', 'serverjs.io', 'umbrellajs.com'];
const websites = await magic(urls)
.map(url => got(url)) // Fetch the URLs in parallel like Promise.all()
.map(res => res.body) // Retrieve the actual bodies
.filter(Boolean); // Only those bodies with content
// NATIVE; How to do this with traditional Promises + arrays
const urls = ['francisco.io', 'serverjs.io', 'umbrellajs.com'];
const responses = await Promise.all(urls.map(url => got(url))); // Fetch the URLs in parallel
const websites = responses
.map(res => res.body) // Retrieve the actual bodies
.filter(Boolean); // Only those bodies with content
Works with any value that a promise can resolve to:
// Get and parse a CSV file. Promise => text => array => number
const sum = await magic(got('example.com/data.csv'))
.split('\n')
.filter(Boolean)
.map(line => line.split('\t').shift())
.map(num => parseFloat(num, 10))
.reduce((total, num) => total + num, 0);
Libraries based on this:
fs-array
(from me).FAQs
✨ Simplify dealing with promises for async-heavy situations
The npm package magic-promises receives a total of 46 weekly downloads. As such, magic-promises popularity was classified as not popular.
We found that magic-promises 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
A flawed sandbox in @nestjs/devtools-integration lets attackers run code on your machine via CSRF, leading to full Remote Code Execution (RCE).
Product
Customize license detection with Socket’s new license overlays: gain control, reduce noise, and handle edge cases with precision.
Product
Socket now supports Rust and Cargo, offering package search for all users and experimental SBOM generation for enterprise projects.