Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .eslintignore

This file was deleted.

9 changes: 0 additions & 9 deletions .eslintrc.json

This file was deleted.

45 changes: 45 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: CI

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
build-and-test:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [20.x, 22.x, 24.x]

steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: "yarn"

- name: Cache node_modules
uses: actions/cache@v4
with:
path: node_modules
key: ${{ runner.os }}-node_modules-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-node_modules-

- name: Install dependencies
run: yarn install --immutable

- name: Lint projects
run: yarn lint:ci

- name: Run tests
run: yarn test
9 changes: 0 additions & 9 deletions .travis.yml

This file was deleted.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# limiter

[![Build Status](https://travis-ci.org/jhurliman/node-rate-limiter.png)](https://travis-ci.org/jhurliman/node-rate-limiter)
![Build Status](https://github.com/jhurliman/node-rate-limiter/actions/workflows/ci.yml/badge.svg)
[![NPM Downloads](https://img.shields.io/npm/dm/limiter.svg?style=flat)](https://www.npmjs.com/package/limiter)

Provides a generic rate limiter for the web and node.js. Useful for API clients,
Expand Down
52 changes: 52 additions & 0 deletions eslint.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const { defineConfig } = require("eslint/config");
const typescriptEslintParser = require("@typescript-eslint/parser");
const typescriptEslintPlugin = require("@typescript-eslint/eslint-plugin");
const prettierPlugin = require("eslint-plugin-prettier");
const eslintRecommended = require("@eslint/js").configs.recommended;
const jestPlugin = require("eslint-plugin-jest");
const globals = require("globals");

module.exports = defineConfig([
{
ignores: ["dist/**", "node_modules/**"],
},
{
files: ["**/*.ts", "**/*.tsx", "**/*.js", "**/*.jsx"],
languageOptions: {
parser: typescriptEslintParser,
parserOptions: {
warnOnUnsupportedTypeScriptVersion: false,
},
ecmaVersion: "latest",
sourceType: "module",
globals: {
performance: "readonly",
setTimeout: "readonly",
},
},
plugins: {
"@typescript-eslint": typescriptEslintPlugin,
prettier: prettierPlugin,
},
rules: {
...eslintRecommended.rules,
...typescriptEslintPlugin.configs.recommended.rules,
"prettier/prettier": "error",
},
},
// Jest specific configuration
{
files: ["**/*.test.ts", "**/*.spec.ts", "**/*.test.js", "**/*.spec.js"],
plugins: { jest: jestPlugin },
languageOptions: {
globals: {
...globals.jest,
},
},
rules: {
...jestPlugin.configs.recommended.rules,
// You can override or add specific Jest rules here if needed
// e.g., "jest/no-disabled-tests": "warn",
},
},
]);
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
},
"scripts": {
"lint": "eslint --fix src",
"lint:ci": "eslint src",
"prepack": "yarn tsc -p tsconfig.json && tsc -p tsconfig.cjs.json && node ./create-package-json.js",
"test": "jest src"
},
Expand All @@ -35,10 +36,11 @@
"@babel/preset-typescript": "^7.24.1",
"@types/babel__generator": "^7.6.8",
"@types/jest": "^29.5.12",
"@typescript-eslint/eslint-plugin": "^7.7.0",
"@typescript-eslint/parser": "^7.7.0",
"@typescript-eslint/eslint-plugin": "^8.32.0",
"@typescript-eslint/parser": "^8.32.0",
"babel-jest": "^29.7.0",
"eslint": "^9.1.0",
"eslint-plugin-jest": "^28.11.0",
"eslint-plugin-prettier": "^5.1.3",
"jest": "^29.7.0",
"prettier": "^3.2.5",
Expand Down
2 changes: 1 addition & 1 deletion src/RateLimiter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Interval } from "./TokenBucket.js";
describe("RateLimiter", () => {
describe("interval validation", () => {
it("invalid interval", () => {
const junkInterval = ("junk" as unknown) as Interval;
const junkInterval = "junk" as unknown as Interval;
expect(() => new RateLimiter({ tokensPerInterval: 1, interval: junkInterval })).toThrow();
});

Expand Down
2 changes: 1 addition & 1 deletion src/RateLimiter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class RateLimiter {
// Make sure the request isn't for more than we can handle
if (count > this.tokenBucket.bucketSize) {
throw new Error(
`Requested tokens ${count} exceeds maximum tokens per interval ${this.tokenBucket.bucketSize}`
`Requested tokens ${count} exceeds maximum tokens per interval ${this.tokenBucket.bucketSize}`,
);
}

Expand Down
8 changes: 4 additions & 4 deletions src/TokenBucket.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe("TokenBucket", () => {

const duration = +new Date() - start;
const diff = Math.abs(1000 - duration);
expect(diff < TIMING_EPSILON);
expect(diff).toBeLessThan(TIMING_EPSILON);
expect(remainingTokens).toEqual(0);
expect(bucket.content).toEqual(0);
});
Expand All @@ -32,7 +32,7 @@ describe("TokenBucket", () => {
const remainingTokens = await bucket.removeTokens(10);
const duration = +new Date() - start;
const diff = Math.abs(1000 - duration);
expect(diff < TIMING_EPSILON);
expect(diff).toBeLessThan(TIMING_EPSILON);
expect(remainingTokens).toEqual(0);
expect(bucket.content).toEqual(0);
});
Expand All @@ -43,7 +43,7 @@ describe("TokenBucket", () => {
const start = +new Date();
const remainingTokens = await bucket.removeTokens(10);
const duration = +new Date() - start;
expect(duration < TIMING_EPSILON);
expect(duration).toBeLessThan(TIMING_EPSILON);
expect(remainingTokens).toEqual(0);
});

Expand All @@ -54,7 +54,7 @@ describe("TokenBucket", () => {
const remainingTokens = await bucket.removeTokens(1);
const duration = +new Date() - start;
const diff = Math.abs(100 - duration);
expect(diff < TIMING_EPSILON);
expect(diff).toBeLessThan(TIMING_EPSILON);
expect(remainingTokens).toBeLessThan(1);
});
});
Expand Down
Loading