# @xplora-uk/express-oom-middleware

> Express middleware to reject requests and avoid out of memory (OOM) crashes

Latest version **1.1.1** (published 2024-03-30) · MIT license · 0 weekly downloads

## Install

```sh
npm install @xplora-uk/express-oom-middleware
pnpm add @xplora-uk/express-oom-middleware
yarn add @xplora-uk/express-oom-middleware
bun add @xplora-uk/express-oom-middleware
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.1 |
| Published | 2024-03-30 |
| First published | 2024-02-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=18.16.0 |
| Dependencies | 1 |
| Unpacked size | 10.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Xplora R&D Team |
| Maintainers | ark8, haci-xplora |
| Keywords | node, express, oom, crash, memory, issue, middleware |

## Links

- npm: https://www.npmjs.com/package/@xplora-uk/express-oom-middleware
- Repository: https://github.com/xplora-uk/express-oom-middleware
- Homepage: https://github.com/xplora-uk/express-oom-middleware#readme
- Issues: https://github.com/xplora-uk/express-oom-middleware/issues
- npm.io page: https://npm.io/package/@xplora-uk/express-oom-middleware

## Dependencies (1)

- [express](https://npm.io/package/express.md) ^4.19.2

## Alternatives

- [@expo/fingerprint](https://npm.io/package/@expo/fingerprint.md) — 6.2M weekly downloads
- [@azure/monitor-opentelemetry-exporter](https://npm.io/package/@azure/monitor-opentelemetry-exporter.md) — 850.0K weekly downloads
- [@azure/monitor-opentelemetry](https://npm.io/package/@azure/monitor-opentelemetry.md) — 624.0K weekly downloads
- [@posthog/ai](https://npm.io/package/@posthog/ai.md) — 423.3K weekly downloads
- [fakefilter](https://npm.io/package/fakefilter.md) — 63.9K weekly downloads

## Recent versions

- 1.1.1 (latest) — 2024-03-30
- 1.1.0 — 2024-02-29
- 1.0.0 — 2024-02-29

## README

# express-oom-middleware

Express middleware to reject requests and avoid out of memory (OOM) crashes

## requirements

* Node v18.16.0+

## usage

```sh
npm i @xplora-uk/express-oom-middleware
```

Shorter version:

```typescript
import express from 'express';
import { makeExpressMiddleware } from '@xplora-uk/express-oom-middleware';

const app = express();
app.use(makeExpressMiddleware({
  memoryUsageLimit: 1024 * 1024 * 1024,
  //httpStatusCode: 500,
  //makeResponsePayload: () => ({ error: 'server is busy', ts: new Date() })
}));
app.get('/', (_req, res) => res.json({ ok: true }));
```

Longer version:

```typescript
import express from 'express';
import { MemoryTracker, RequestRejector } from '@xplora-uk/express-oom-middleware';

const app = express();

const oneGB = 1024 * 1024 * 1024;
const mt = new MemoryTracker({ memoryUsageLimit: oneGB });
const rr = new RequestRejector(mt, {
  //httpStatusCode: 500,
  //makeResponsePayload: () => ({ error: 'server is busy', ts: new Date() })
});

app.use(rr.makeMiddleware());

app.get('/', (_req, res) => res.json({ ok: true }));
```

## maintenance

### installation

```sh
npm i
```

### code

```plain
src/
  __tests__/
    component/
      express.test.ts          component tests for express middleware
      RequestRejector.test.ts  component tests for RequestRejector
    unit/
      MemoryTracker.test.ts    unit tests for MemoryTracker
  index.ts                     main file that exports features of this library
  express.ts                   file for express middleware - shortcut
  MemoryTracker.ts             MemoryTracker can give info about heap memory
  RequestRejector              RequestRejector uses MemoryTracker and creates an express middleware
```

### build

```sh
npm run build
```

### tests

You can run tests with/without coverage info.

```sh
npm run test:unit
npm run test:component
npm run test:coverage
```

Current coverage:

```plain
--------------------|---------|----------|---------|---------|-------------------
File                | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
--------------------|---------|----------|---------|---------|-------------------
All files           |     100 |    72.72 |     100 |     100 |                   
 MemoryTracker.ts   |     100 |    57.14 |     100 |     100 | 16-18             
 RequestRejector.ts |     100 |    77.77 |     100 |     100 | 27-28             
 express.ts         |     100 |    83.33 |     100 |     100 | 7                 
--------------------|---------|----------|---------|---------|-------------------
```

### publish

It is important to increment version number using semantic versioning in `package.json` and re-create `package-lock.json`

```sh
# https://docs.npmjs.com/cli/v9/commands/npm-login
# using a member in xplora-uk
npm login

# https://docs.npmjs.com/cli/v9/commands/npm-publish
npm publish --access public
```

---
_Source: https://npm.io/package/@xplora-uk/express-oom-middleware · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
