
Security News
Axios Maintainer Confirms Social Engineering Attack Behind npm Compromise
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.
vmo-onion is a middleware processing class based on the onion model, designed to execute a series of middleware functions in sequence. This library is suitable for scenarios where requests or data streams need to be processed step-by-step, with each processing step controlling the execution of subsequent steps.
npm i vmo-onion
Ensure your project supports ES6 module syntax. You can import the library using the following code:
import { VmoOnion } from 'vmo-onion'
Middleware is a function return a function that takes two parameters: a context object and a next function. Each middleware can choose whether to call next to pass control to the next middleware.
const middleware1 = async function (context, next) {
console.log('Middleware 1 start')
context.value = 1
await next()
context.value = 1
console.log('Middleware 1 end')
}
const middleware2 = async function (context, next) {
console.log('Middleware 2 start')
context.value = 2
context.value = 2
console.log('Middleware 2 end')
}
vmo-onion InstanceYou can initialize a vmo-onion instance with an array of middleware functions.
const onion = new VmoOnion([middleware1, middleware2])
Alternatively, you can initialize an empty instance and add middleware using the use method:
const onion = new VmoOnion()
onion.use(middleware1)
onion.use(middleware2)
Use the pipingData method to process data. This method takes a context object and an optional array of middleware functions, and returns a Promise that resolves to the processed context object.
const contextData = { value: 0 }
onion
.pipingData(contextData)
.then(data => console.log('Final Context:', data))
.catch(err => console.error('Error:', err))
In this example, control will flow through the middleware in the following order: middleware1 -> middleware2. Each middleware will return from its subsequent middleware in reverse order.
A simple application of the processing flow might look like this:
import { VmoOnion } from 'vmo-onion'
import type { MiddleWare } from 'vmo-onion'
// create an instance of VmoOnion
const onion = new VmoOnion<{ value: number }>()
// 定义中间件
const middleware1: MiddleWare<{ value: number }> = async function (context, next) {
console.log('Middleware 1: first step')
context.value += 1
await next()
console.log('Middleware 1: final step')
}
const middleware2: MiddleWare<{ value: number }> = async function (context, next) {
console.log('Middleware 2: first step')
context.value += 1
console.log('Middleware 2: final step')
}
// use middleware
onion.use(middleware1)
onion.use(middleware2)
// init context
const initialContext = { value: 0 }
onion
.pipingData(initialContext)
.then(context => {
console.log('Processed Context:', context) // { value: 2 }
})
.catch(err => {
console.error('Error:', err)
})
Expected output:
Middleware 1: first step
Middleware 2: first step
Middleware 2: final step
Middleware 1: final step
Processed Context: { value: 2 }
If any middleware throws an exception or returns a rejected Promise, the error will be caught in the catch handler of the pipingData method.
const errorMiddleware = (context, next) => {
console.log('Error Middleware')
throw new Error('Something went wrong!')
}
onion.use(errorMiddleware)
onion
.pipingData({})
.then(() => console.log('Process completed successfully.'))
.catch(error => console.error('Error caught:', error)) // Error caught: Error: Something went wrong!
import { VmoOnion } from 'vmo-onion'
import type { MiddleWare } from 'vmo-onion'
const onion = new VmoOnion<{ name: string; age: number }>([])
// From now on, the middleware must accept parameters with the Context type as {name: string, age: number}
// or
const sampleMiddleWare: MiddleWare<{ name: string; age: number }> = (context, next) => {
//.....
}
use(func): Adds a middleware function to the middleware stack.
pipingData(data, [middlewares]): Processes the context object through the middleware stack.
checkMiddleWares(middlewares): Checks if the middleware array is valid, i.e., whether it is an array of functions.
compose(middlewares): Composes the middleware array into a single function that executes all middleware in sequence.
-middlewares: The middleware array.
The library ensures that next() is not called multiple times, preventing infinite recursion. When this occurs, the library rejects the Promise with the error message next() called multiple times.
FAQs
Promise onion model
We found that vmo-onion demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 0 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
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.

Security News
The Axios compromise shows how time-dependent dependency resolution makes exposure harder to detect and contain.