Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
flow-compose
Advanced tools
Flow control for any asynchronous function utilising a onion-like compose middleware
returning a fully valid middleware
comprised of all those which are passed.
The flow acts in a stack-like manner, allowing consumer to perform actions downstream then after actions on the response upstream.
This is based on popular koajs/koa-compose rewritten in Typescript using reduce with
type Middleware<T = any> = (context: T, next: NextFunction, valueFromPrev?: any) => Promise<any>;
type NextFunction = (valueFromPrev?: any) => Promise<any>;
koa-compose
pattern is very powerful and in fact it can be used beyond Koa framework. ( the namespace, the context API )
Ultimately this gives the consumer the ability to modularise different steps in a process and control the flow.
npm install flow-compose
# or using yarn
yarn add flow-compose
import { compose, Middleware } from 'flow-compose';
type MyContext = {
logger: { log: (...args: any[]) => void };
service: { get: () => Promise<string> };
eventSender: { send: (data: string) => void };
};
const handleError: Middleware<MyContext> = async (context, next) => {
try {
return await next();
} catch (err) {
// handle error
return null;
}
};
const logRunningTime: Middleware<MyContext> = async (context, next) => {
const start = Date.now();
const text = await next();
const end = Date.now();
context.logger.log('Total time:', end - start);
return text;
};
const fireEvent: Middleware<MyContext> = async (context, next, valueFromPrev) => {
context.eventSender.send(valueFromPrev);
return next(valueFromPrev);
};
const transform: Middleware<MyContext> = async (context, next, valueFromPrev) => valueFromPrev.toUpperCase();
const fetch: Middleware<MyContext> = async (context, next) => {
const rawData = await context.service.get();
return next(rawData);
};
const context: MyContext = {
logger: { log: console.log },
service: { get: async () => 'data' },
eventSender: { send: (data) => {} }
};
const result = await compose<MyContext>([handleError, logRunningTime, fetch, fireEvent, transform])(context);
import { compose, Middleware, parallel } from 'flow-compose';
type MyContext = {
person: string
}
const ateCandies: Middleware<MyContext> = async (context, next, valueFromPrev) => {
return next(context.person + ' ate ' + valueFromPrev.join(','));
};
const drankOrangeJuice: Middleware<MyContext> = async (context, next, valueFromPrev) => {
return next(valueFromPrev + ' and drank orange juice');
};
const chocolate: Middleware<MyContext> = async () => 'chocolate';
const jellyBean: Middleware<MyContext> = async () => 'jelly bean';
const getCandies: Middleware<MyContext> = parallel([chocolate, jellyBean]);
const result = await compose<MyContext>([getCandies, ateCandies, drankOrangeJuice])({ person: 'Tom' });
FAQs
All purpose composer
The npm package flow-compose receives a total of 90 weekly downloads. As such, flow-compose popularity was classified as not popular.
We found that flow-compose 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
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.