
Security News
Browserslist-rs Gets Major Refactor, Cutting Binary Size by Over 1MB
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
express-async-context
Advanced tools
Zero-dependency context-provision for express-application based on the AsyncLocalStorage.
[](https://github.com/DScheglov/ express-async-context/actions/workflows/run-tests.yml)
npm install express-async-context
import express from 'express';
import createContext from 'express-async-context';
const Context = createContext(req => ({
traceId: req.headers['x-request-id'] ?? Math.random().toFixed(20).slice(2),
}));
const app = express();
app.use(Context.provider);
app.get('/trace-id', Context.consumer(
(req, res) => ({ traceId }) => res.json({ traceId }),
));
app.listen(8080, () => {
console.log('Server is listening on port: 8080');
console.log('Follow: http://localhost:8080/trace-id');
});
curl -H "X-Request-Id: 58895124899023443277" http://localhost:8080/trace-id
The express-async-context
library is designed to aproach context provision to the
chain of request handling in the express
-application without mutation of the
request
or/and response
.
Under the hood library uses AsyncLocalStorage and is based on the thunk-idiom that means calculation postponed until it will be provided with the context.
The main benifit of context we can get when we use IoC-container as a context. To make such injection safe the static type-safe containers required, as instance: true-di.
See Live DI Demo on Sandbox
createContext
ContextFactory<T>
ContextManager<T>
HandlerThunk<T>
ErrorHandlerThunk<T>
Thunk<T, R = void>
RunFn<T>
createContext
<T>(contextFactory: ContextFactory<T>): ContextManager<T>;
Accepts contextFactory
function and creates a ContextManager.
ContextFactory<T>
<T>(req: express.Request, res: express.Response) => T;
The type describes function that accepts express
.Request
, express
.Response
and returns context data of any type T
.
ContextManager<T>
interface ContextManager<T> {
provider: (req: express.Request, res: express.Response, next: express.NextFunction) => void;
consumer: {
(handler: express.RequestHandler | HandlerThunk<T>): express.RequestHandler;
(handler: express.ErrorRequestHandler | ErrorHandlerThunk<T>): express.ErrorRequestHandler;
}
The interface contains two members:
provider - is an usual express
middleware that creates context data
for each request using contextFactory
and "binds" this data to the request
consumer - is a decorator for HandlerThunk<T>
and ErrorHandlerThunk
that converts them
to usual express.RequestHandler
and express.ErrorRequestHandler
.
HandlerThunk<T>
(req: express.Request, res: express.Response, next: express.NextFunction) =>
(context: T, run: RunFn<T>) => void;
The curried request handler that requires two-times application.
HandlerThunk
could be considered as an express
.RequestHandler
that returns a postponed handling of the request -- the Thunk
ErrorHandlerThunk<T>
(err: any, req: express.Request, res: express.Response, next: express.NextFunction) =>
(context: T, run: RunFn<T>) => void;
The curried handler of error trhown during the request processing.
ErrorHandlerThunk
could be considered as an express
.ErrorRequestHandler
that
returns a postponed handling of the error -- the Thunk
Thunk<T, R = void>
(context: T, run: RunFn<T>) => R
The postponed calculation, including handler of the request or an error.
The correspondent function receives context data and the run
-function,
that runs any other Thunk
.
RunFn<T>
<R>(fn: Thunk<T, R>) => R
Runs and injects the context data and itself to the postponed calculation that accepts as a single argument.
RunFn
returns the result of execution of its argument-function.
FAQs
Context manager for Express routing lib
The npm package express-async-context receives a total of 18 weekly downloads. As such, express-async-context popularity was classified as not popular.
We found that express-async-context demonstrated a healthy version release cadence and project activity because the last version was released less than 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
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
Research
Security News
Eight new malicious Firefox extensions impersonate games, steal OAuth tokens, hijack sessions, and exploit browser permissions to spy on users.
Security News
The official Go SDK for the Model Context Protocol is in development, with a stable, production-ready release expected by August 2025.