Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: fix linter config and linter errors #67

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
3 changes: 3 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
env:
es6: true
node: true
mocha: true
extends: 'eslint:recommended'
parserOptions:
ecmaVersion: latest
rules:
indent:
- error
Expand Down
22 changes: 19 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ on:
branches:
- master
pull_request:
branches:
- master

jobs:
test:
Expand Down Expand Up @@ -35,4 +33,22 @@ jobs:
run: npm run test

- name: Stop containers
run: docker-compose -f "docker-compose.yml" down
run: docker-compose -f "docker-compose.yml" down

lint:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Use Node.js
uses: actions/setup-node@v4
with:
node-version: 18.x

- name: Install dependencies
run: npm install

- name: Run lint
run: npm run lint
36 changes: 16 additions & 20 deletions lib/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,18 @@ const LRU = require('lru-cache');
const utils = require('./utils');
const Redis = require('ioredis');
const { validateParams, validateERLParams } = require('./validation');
const DBPing = require("./db_ping");
const DBPing = require('./db_ping');
const { calculateQuotaExpiration, resolveElevatedParams } = require('./utils');
const EventEmitter = require('events').EventEmitter;

const TAKE_LUA = fs.readFileSync(`${__dirname}/take.lua`, "utf8");
const TAKE_ELEVATED_LUA = fs.readFileSync(`${__dirname}/take_elevated.lua`, "utf8");
const PUT_LUA = fs.readFileSync(`${__dirname}/put.lua`, "utf8");
const TAKE_LUA = fs.readFileSync(`${__dirname}/take.lua`, 'utf8');
const TAKE_ELEVATED_LUA = fs.readFileSync(`${__dirname}/take_elevated.lua`, 'utf8');
const PUT_LUA = fs.readFileSync(`${__dirname}/put.lua`, 'utf8');

const PING_SUCCESS = "successful";
const PING_ERROR = "error";
const PING_RECONNECT = "reconnect";
const PING_RECONNECT_DRY_RUN = "reconnect-dry-run";

const DEFAULT_COMMAND_TIMEOUT = 125; // Milliseconds
const PING_SUCCESS = 'successful';
const PING_ERROR = 'error';
const PING_RECONNECT = 'reconnect';
const PING_RECONNECT_DRY_RUN = 'reconnect-dry-run';

class LimitDBRedis extends EventEmitter {
static get PING_SUCCESS() {
Expand Down Expand Up @@ -118,14 +116,14 @@ class LimitDBRedis extends EventEmitter {
}

#setupPing(config) {
this.redis.on("ready", () => this.#startPing(config));
this.redis.on("close", () => this.#stopPing());
this.redis.on('ready', () => this.#startPing(config));
this.redis.on('close', () => this.#stopPing());
}

#startPing(config) {
this.#stopPing();
this.ping = new DBPing(config.ping, this.redis);
this.ping.on("ping", (data) => this.emit("ping", data));
this.ping.on('ping', (data) => this.emit('ping', data));
}

#stopPing() {
Expand Down Expand Up @@ -229,7 +227,7 @@ class LimitDBRedis extends EventEmitter {
const prevCall = this.callCounts.get(key);

if (prevCall) {
const shouldGoToRedis = prevCall?.count >= bucketKeyConfig.skip_n_calls
const shouldGoToRedis = prevCall?.count >= bucketKeyConfig.skip_n_calls;


if (!shouldGoToRedis) {
Expand All @@ -249,7 +247,7 @@ class LimitDBRedis extends EventEmitter {
}
}

takeFunc(key, bucketKeyConfig, count)
takeFunc(key, bucketKeyConfig, count);
}

/**
Expand All @@ -272,7 +270,6 @@ class LimitDBRedis extends EventEmitter {
}
const remaining = parseInt(results[0], 10);
const conformant = parseInt(results[1], 10) ? true : false;
const currentMS = parseInt(results[2], 10);
const reset = parseInt(results[3], 10);
const res = {
conformant,
Expand All @@ -286,7 +283,7 @@ class LimitDBRedis extends EventEmitter {
}
return callback(null, res);
});
})
});
}

takeElevated(params, callback) {
Expand All @@ -296,7 +293,7 @@ class LimitDBRedis extends EventEmitter {
erlParams = utils.getERLParams(params.elevated_limits);
const valError = validateERLParams(erlParams);
if (valError) {
return callback(valError)
return callback(valError);
}
}

Expand All @@ -321,7 +318,6 @@ class LimitDBRedis extends EventEmitter {
}
const remaining = parseInt(results[0], 10);
const conformant = parseInt(results[1], 10) ? true : false;
const currentMS = parseInt(results[2], 10);
const reset = parseInt(results[3], 10);
const erl_triggered = parseInt(results[4], 10) ? true : false;
let erl_activate_for_bucket = parseInt(results[5], 10) ? true : false;
Expand All @@ -348,7 +344,7 @@ class LimitDBRedis extends EventEmitter {
}
return callback(null, res);
});
})
});
}

/**
Expand Down
14 changes: 7 additions & 7 deletions lib/db_ping.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const cbControl = require('./cb');
const utils = require("./utils");
const EventEmitter = require("events").EventEmitter;
const utils = require('./utils');
const EventEmitter = require('events').EventEmitter;

const PING_SUCCESS = "successful";
const PING_ERROR = "error";
const PING_RECONNECT = "reconnect";
const PING_RECONNECT_DRY_RUN = "reconnect-dry-run";
const PING_SUCCESS = 'successful';
const PING_ERROR = 'error';
const PING_RECONNECT = 'reconnect';
const PING_RECONNECT_DRY_RUN = 'reconnect-dry-run';

const DEFAULT_PING_INTERVAL = 3000; // Milliseconds

Expand Down Expand Up @@ -89,7 +89,7 @@ class DBPing extends EventEmitter {
error: err,
failedPings: failedPings,
};
this.emit("ping", result);
this.emit('ping', result);
}

#retryStrategy(callback) {
Expand Down
Empty file added lib/types.js
Empty file.
14 changes: 1 addition & 13 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ function buildBucket(bucket) {
}

function functionOrFalse(fun) {
return !!(fun && fun.constructor && fun.call && fun.apply)
return (fun && fun.constructor && fun.call && fun.apply)
? fun
: false;
}
Expand Down Expand Up @@ -175,18 +175,6 @@ function resolveElevatedParams(erlParams, bucketKeyConfig) {
};
}

class LimitdRedisConfigurationError extends Error {
constructor(msg, extra) {
super();
this.name = this.constructor.name;
this.message = msg;
Error.captureStackTrace(this, this.constructor);
if (extra) {
this.extra = extra;
}
}
}

module.exports = {
buildBuckets,
buildBucket,
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"url": "http://github.com/auth0/limitd-redis.git"
},
"scripts": {
"lint": "eslint .",
"test": "trap 'docker-compose down --remove-orphans -v' EXIT; docker-compose up -d && NODE_ENV=test nyc mocha --exit"
},
"author": "Auth0",
Expand All @@ -24,7 +25,7 @@
"devDependencies": {
"chai": "^4.1.2",
"chai-exclude": "^2.1.0",
"eslint": "^6.1.0",
"eslint": "^8.57.0",
"mocha": "^5.2.0",
"mockdate": "^3.0.5",
"nyc": "^14.1.1",
Expand Down
14 changes: 7 additions & 7 deletions test/cb.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ describe('cb(callback)', function() {
});

it('shouldn\'t mess with errors', function(done) {
invokeAsyncError(cb(function(err, res) {
invokeAsyncError(cb(function(err) {
assert(err);
done();
}));
});

it('should allow multiple executions', function(done) {
var count = 0;
invokeAsyncTwice(cb(function(err, res) {
invokeAsyncTwice(cb(function() {
count++;
if (count === 2) done();
}));
Expand All @@ -56,14 +56,14 @@ describe('cb(callback).timeout(ms)', function() {
});

it('should complete with an error after timeout period', function(done) {
invokeAsync(cb(function(err, res) {
invokeAsync(cb(function(err) {
assert(err);
done();
}).timeout(50));
});

it('error resulting from a timeout should be instanceof cb.TimeoutError', function(done) {
invokeAsync(cb(function(err, res) {
invokeAsync(cb(function(err) {
assert(err instanceof cb.TimeoutError);
done();
}).timeout(50));
Expand All @@ -80,7 +80,7 @@ describe('cb(callback).error(errback)', function() {
});

it('should pass errors to provided errback', function(done) {
invokeAsyncError(cb(function(res) {
invokeAsyncError(cb(function() {
throw new Error('should not be invoked');
}).error(function(err) {
assert(err);
Expand All @@ -100,7 +100,7 @@ describe('cb(callback).error(errback).timeout(ms)', function() {
});

it('should pass timeout error to provided errback', function(done) {
invokeAsyncError(cb(function(res) {
invokeAsyncError(cb(function() {
throw new Error('should not be invoked');
}).error(function(err) {
assert(err);
Expand All @@ -114,7 +114,7 @@ describe('cb(callback).once()', function() {

it('should allow multiple executions', function(done) {
var count = 0;
invokeAsyncTwice(cb(function(err, res) {
invokeAsyncTwice(cb(function() {
count++;
assert.notEqual(count, 2);
setTimeout(done, 100);
Expand Down
Loading