
Research
Malicious npm Package Brand-Squats TanStack to Exfiltrate Environment Variables
A brand-squatted TanStack npm package used postinstall scripts to steal .env files and exfiltrate developer secrets to an attacker-controlled endpoint.
express-idempotency
Advanced tools
Add idempotency to your express route, effortlessly, the way you want it.
Add idempotency to your express route, effortlessly, the way you want it.
Integrate idempotency feature to Express routes quickly, without efforts. The implementation is inspired from the Stripe idempotency implementation.
This is a Node.js module designed to work with Express, available through the NPM registry.
For examples, check the examples folder.
Install the dependency.
$ npm install express-idempotency
Integrate the middleware in your Express initialization.
// Javascript
const idempotency = require('express-idempotency');
// ...express initialization
app.post('*', idempotency.idempotency());
// Typescript
import { idempotency } from 'express-idempotency';
// ...express initialization
app.post('*', idempotency());
The idempotency middleware will call the next function, whenever a idempotency key is detected or not. This is by design to avoid breaking the middleware and route handler chain. To prevent transaction processing to occurs, there is a hit function which determine if the idempotency middleware did process the request.
app.post('/', function (req, res) {
// Check if there was a hit!
const idempotencyService = getSharedIdempotencyService();
if (idempotencyService.isHit(req)) {
return;
}
// Do something
res.send('Got a new POST request');
});
Sometimes, there is errors that can occur in the process of the request. In that case, the middleware must be aware of the error and remove any information related to the idempotency key that failed. You can notify the middleware by using the reportError function.
app.post('/', function (req, res) {
const idempotencyService = getSharedIdempotencyService();
// Processing...
// Oh BOOM! There is a error. Let's notify the middleware.
idempotencyService.reportError(req);
});
You can configure the way the idempotency middleware will behave by providing options during initialization.
app.post(
'*',
idempotency({
// Specify the header to be used to retrieve the idempotency key.
idempotencyKeyHeader,
// The data adapter used to store the resources.
dataAdapter,
// Logic to indicate if response must be kept for idempotency
responseValidator,
// Logic to detect misuse of the idempotency key
intentValidator,
})
);
By default, the header used to retrieve the idempotency key is Idempotency-Key but you can change it for any value you would like.
The data adapter allows to persist cached response for a idempotency key. The default implementation use in-memory storage, which is not recommended for production environment.
You can create your own data adapter by implementing the IIdempotencyDataAdapter interface but there is already some implementation ready.
By default, a request will be considered successful and the response will be cached if the response http status code is between 200 and 299. If it is not the behavior that is wished, you can implement your own logic by providing your own responseValidator, which must implements the IIdempotencyResponseValidator interface.
export class CustomResponseValidator implements IIdempotencyResponseValidator {
public isValidForPersistence(
idempotencyResponse: IdempotencyResponse
): boolean {
// Insert logic here
// For example, we could cache any 500 status code.
return idempotencyResponse.statusCode == 500;
}
}
When receiving a request with an idempotency key, the middleware will compare it to the original request to ensure the intend. By default, the middleware is expecting a match for the url, the method, the query parameters and the body. If any of these element does not match, it will be qualified as a misuse of the idempotency key and an error will be thrown.
This intent validator can be override by providing your own implementation, if this is not the desired behavior. Simply implements the IIdempotencyIntentValidatorinterface.
export class CustomIntentValidator implements IIdempotencyIntentValidator {
isValidIntent(req: express.Request, idempotencyRequest: IdempotencyRequest): boolean {
// Insert logic here
// For example, the url must match
return req.url === idempotencyRequest.url;
}
The source code of this project is distributed under the MIT License.
See CONTRIBUTING.md.
Participation in this poject is governed by the Code of Conduct.
(English)
Ajouter à Express des fonctionnalités d'idempotence pour certaines routes rapidement, sans effort. L'implémentation est inspirée par la stratégie de Stripe.
Ce module Node.js est conçu pour fonctionner avec Express, et disponible sur le registre NPM.
Pour des exemples, voir le répertoire examples.
Installation de la dépendance.
$ npm install express-idempotency
Intégrer le middleware dans l'initialisation de votre application Express.
// Javascript
const idempotency = require('express-idempotency');
// ...initialisation de Express
app.post('*', idempotency.idempotency());
// Typescript
import { idempotency } from 'express-idempotency';
// ...initialisation de Express
app.post('*', idempotency());
Le middleware d'idempotence va faire l'appel de la fonction next, peu importe si la clĂ© d'idempotence est dĂ©tectĂ© ou non. Cette dĂ©cision de design permet de conserver la chaine de communication quant Ă la prise en charge de la requĂȘte. Afin de prĂ©venir une transaction d'ĂȘtre exĂ©cutĂ© une deuxiĂšme fois, il y a une fonction hit permettant d'identifier le traitement prĂ©alable du middleware.
app.post('/', function (req, res) {
// Vérifier l'intenvention!
const idempotencyService = getSharedIdempotencyService();
if (idempotencyService.isHit(req)) {
return;
}
// Faire quelque chose
res.send('Got a new POST request');
});
Quelques fois, on veut communiquer au middleware que le traitement attendu a échoué. Dans ces cas, il faut que le middleware sache qu'une erreur est survenue et retirer l'information relative à la clé d'idempotence courante. On peut donc notifier le middleware en utilisant la fonction reportError.
app.post('/', function (req, res) {
const idempotencyService = getSharedIdempotencyService();
// Processing...
// Oh BOOM! There is a error. Let's notify the middleware.
idempotencyService.reportError(req);
});
Vous pouvez configurer lors de l'initialisation la façon dont le middleware va se comporter.
app.post(
'*',
idempotency({
// PrĂ©ciser l'entĂȘte de requĂȘte contenant la clĂ© d'idempotence.
idempotencyKeyHeader,
// L'adapteur de données utilisé pour stocker les informations.
dataAdapter,
// PrĂ©ciser la logique permettant d'indiquer si la rĂ©ponse doit ĂȘtre conservĂ© pour l'idempotence.
responseValidator,
// Préciser la logique à appliquer pour s'assurer de la bonne utilisation de la clé d'idempotence.
intentValidator,
})
);
Par dĂ©faut, l'entĂȘte utilisĂ© pour la clĂ© d'idempotence est Idempotency-Key mais elle peut ĂȘtre changĂ© pour une autre valeur dĂ©sirĂ©e.
L'adapteur de données permet de persister l'information relatif à la clé d'idempotence. L'implémentation par défaut conserve l'information en mémoire, ce qui n'est pas recommandé pour un environnement de production.
Vous pouvez créer votre propre adapteur de données par l'implémentation de l'interface IIdempotencyDataAdapter. Les implémentations connues sont les suivantes :
Par dĂ©faut, une requĂȘte est considĂ©rĂ© valide lorsque le statut http est entre 200 et 299 inclusivement. Ă ce moment-lĂ , la rĂ©ponse est persistĂ©e. Si ce n'est pas le comportement attendu, il est possible d'implĂ©menter sa propre logique en fournissant votre propre validateur de rĂ©ponse. Ă ce moment-lĂ , il faut implĂ©menter l'interface IIdempotencyResponseValidator.
export class CustomResponseValidator implements IIdempotencyResponseValidator {
public isValidForPersistence(
idempotencyResponse: IdempotencyResponse
): boolean {
// Insert logic here
// For example, we could cache any 500 status code.
return idempotencyResponse.statusCode == 500;
}
}
Lorsqu'on reçoit une requĂȘte avec une clĂ© d'idempotence, le middleware va comparer celle-ci avec la requĂȘte originale ayant gĂ©nĂ©rĂ© une ressource idempotente afin d'assurer l'intention. Par dĂ©faut, le middleware va s'assurer que l'adresse (url), la mĂ©thode, les paramĂštres de requĂȘte et le contenu. Si l'un de ces Ă©lĂ©ments divergent, la requĂȘte va ĂȘtre considĂ©rĂ© comme invalide et l'utilisation de la clĂ© d'idempotence incorrecte.
Ce comporement peut ĂȘtre remplacĂ© en fournissant son propre validateur d'intention par l'implĂ©mentation de l'interface IIdempotencyIntentValidator.
export class CustomIntentValidator implements IIdempotencyIntentValidator {
isValidIntent(req: express.Request, idempotencyRequest: IdempotencyRequest): boolean {
// Insérer logique ici
// Par exemple, seulement l'adresse doit correspondre
return req.url === idempotencyRequest.url;
}
Voir CONTRIBUTING.md
Le code source de ce projet est libéré sous la licence MIT License.
La participation à ce projet est réglementée part le Code de Conduite
FAQs
Idempotency middleware for Express - Add idempotency to your express routes effortlessly
The npm package express-idempotency receives a total of 3,395 weekly downloads. As such, express-idempotency popularity was classified as popular.
We found that express-idempotency 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.

Research
A brand-squatted TanStack npm package used postinstall scripts to steal .env files and exfiltrate developer secrets to an attacker-controlled endpoint.

Research
Compromised SAP CAP npm packages download and execute unverified binaries, creating urgent supply chain risk for affected developers and CI/CD environments.

Company News
Socket has acquired Secure Annex to expand extension security across browsers, IDEs, and AI tools.