An implementation based on the Chain of Responsibility (aka CoR) design pattern.
npm install @stone-js/pipeline


!Maintenance








An implementation of the Chain of Responsibility (CoR) pattern tailored for modern JavaScript and TypeScript apps using the Continuum Architecture philosophy.
---
The Pipeline class is a flexible processor of "passable" values through a series of configurable steps called pipes. Pipes can be functions, factory-generated handlers, or class instances. Pipelines can run synchronously or asynchronously, support custom hook events, and allow pipe resolution via a resolver for advanced dependency injection scenarios.
``bash`
npm i @stone-js/pipelineor
yarn add @stone-js/pipelineor
pnpm add @stone-js/pipeline
> \[!IMPORTANT]
> This package is pure ESM. Ensure your package.json includes "type": "module" or configure your bundler appropriately.
`ts`
import { Pipeline } from '@stone-js/pipeline'
`ts
import { Pipeline } from '@stone-js/pipeline'
const addOne = (v, next) => next(v + 1)
const timesTwo = (v, next) => next(v * 2)
const result = Pipeline.create
.send(1)
.through(addOne, timesTwo)
.sync()
.thenReturn()
console.log(result) // Output: 4
`
* Chain-of-responsibility execution
* Supports function, factory, and class-based pipes
* Works in both sync and async mode
* Custom resolver supporton()
* Pipe lifecycle hooks via
* Priority-based execution order
* Type-safe with TypeScript, compatible with JavaScript
`ts
const pipeline = Pipeline.create
.send(2)
.through([
(v, next) => next(v + 3),
(v, next) => next(v * 2)
])
.sync()
const result = pipeline.thenReturn()
console.log(result) // Output: 10
`
`ts
const fetch = async (v, next) => next(await mockFetch(v))
const mockFetch = async (v) => new Promise(res => setTimeout(() => res(v * 10), 500))
const result = await Pipeline.create
.send(5)
.through(fetch)
.thenReturn()
console.log(result) // Output: 50
`
`ts
const result = Pipeline.create
.send(1)
.pipe(v => v + 1)
.pipe(v => v * 5)
.sync()
.thenReturn()
console.log(result) // Output: 10
`
`tsResolved: ${v}
class MyPipe {
handle(v, next) {
return next()
}
}
const resolver = () => new MyPipe()
const result = Pipeline.create
.send('value')
.through({ module: MyPipe, isClass: true })
.sync()
.thenReturn()
console.log(result) // Output: Resolved: value
`
`ts
const pipeline = Pipeline.create
.on('onProcessingPipe', ({ passable }) => console.log('Processing', passable))
.on('onPipeProcessed', ({ passable }) => console.log('Processed', passable))
.send(3)
.through((v, next) => next(v + 2))
.sync()
const result = pipeline.thenReturn()
console.log(result) // Output: 5
`
`ts
const factoryPipe = () => (v, next) => next(v + 2)
class CustomPipe {
handle(v, next) {
return next(v * 3)
}
}
const result = Pipeline.create
.send(1)
.through(
{ module: CustomPipe, isClass: true },
{ module: factoryPipe, isFactory: true }
)
.sync()
.thenReturn()
console.log(result) // Output: 9
`
`ts
class Pipe {
transform(value, next) {
return next(value + '!')
}
}
const result = Pipeline.create
.send('Wow')
.through({ module: Pipe, isClass: true })
.via('transform')
.sync()
.thenReturn()
console.log(result) // Output: Wow!
`
`ts
const p1 = { module: (v, next) => next(v + 1), priority: 10 }
const p2 = { module: (v, next) => next(v * 2), priority: 20 }
const result = Pipeline.create
.send(1)
.through(p1, p2)
.sync()
.thenReturn()
console.log(result) // Output: 4 → (1 + 1) * 2
`
All methods are chainable:
* .send(passable).through(...pipes)
* .pipe(...pipes)
* .sync(true|false)
* .via(methodName)
* .on(hookName, listener)
* .then(fn)
* .thenReturn()
*
> Pipes can be:
>
> * Function: (value, next) => next(...)Factory
> * : () => (value, next) => next(...)Class
> * : new MyPipe().handle(...)
The Pipeline` class provides a powerful and flexible way to process values through a series of steps, allowing for both synchronous and asynchronous operations. It supports various types of pipes, including functions, factories, and classes, and offers lifecycle hooks for custom behavior during processing.
This package is part of the Stone.js ecosystem, a modern JavaScript framework built around the Continuum Architecture.
Explore the full documentation: https://stonejs.dev
* API
Inspired by Laravel's Pipeline