Skip to content

thanhhoajs/utils

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@thanhhoajs/utils

A collection of utility middlewares for the ThanhhoaJS framework.

Installation

To install the package, run the following command:

bun install @thanhhoajs/utils

Usage

This package provides several middleware utilities that can be used with the ThanhhoaJS framework. Below is a brief overview of each middleware and how to use them.

1. Cache Middleware

The cacheMiddleware provides caching for HTTP GET requests using an LRU cache. It supports ETag-based caching and returns a 304 Not Modified response if the client's cached version is still valid.

import { cacheMiddleware } from "@thanhhoajs/utils";

app.use(cacheMiddleware());

2. Compression Middleware

The compressionMiddleware compresses HTTP response bodies using gzip compression. It checks the Accept-Encoding header to determine if the client supports gzip compression.

import { compressionMiddleware } from "@thanhhoajs/utils";

app.use(compressionMiddleware({}));

3. CORS Middleware

The corsMiddleware handles Cross-Origin Resource Sharing (CORS) for HTTP requests. It allows you to configure CORS headers, including origin, methods, and allowed headers.

import { corsMiddleware } from "@thanhhoajs/utils";

app.use(
  corsMiddleware({
    origin: "https://example.com",
    credentials: true,
  })
);

4. Helmet Middleware

The helmetMiddleware sets various HTTP security headers to help protect your application from common web vulnerabilities. It includes options for Content Security Policy (CSP), HSTS, X-Frame-Options, and more.

import { helmetMiddleware } from "@thanhhoajs/utils";

app.use(
  helmetMiddleware({
    contentSecurityPolicy: {
      "default-src": ["'self'"],
      "script-src": ["'self'", "'unsafe-inline'"],
    },
  })
);

5. Rate Limiter Middleware

The rateLimiterMiddleware provides rate limiting for incoming requests. It can use either an in-memory store or Redis for distributed rate limiting.

import { rateLimiterMiddleware } from "@thanhhoajs/utils";

app.use(
  await rateLimiterMiddleware({
    windowMs: 60000, // 1 minute
    maxRequests: 100, // Limit each IP to 100 requests per windowMs
    redis: {
      enabled: true,
      url: "redis://localhost:6379",
    },
  })
);

Configuration

Each middleware can be configured with specific options to tailor its behavior to your application's needs. Refer to the individual middleware documentation for detailed configuration options.

Contributing

Contributions are welcome! Please open an issue or submit a pull request on the GitHub repository.

Author

Nguyen Nhu Khanh kwalker.nnk@gmail.com

License

This project is licensed under the MIT License. See the LICENSE file for details.

About

A collection of utility middlewares for the ThanhhoaJS framework

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published