Skip to content

Commit

Permalink
Configure ESLint for Satellite
Browse files Browse the repository at this point in the history
  • Loading branch information
menghif committed Mar 9, 2022
1 parent fa7f0ec commit 8d0e234
Show file tree
Hide file tree
Showing 10 changed files with 161 additions and 129 deletions.
196 changes: 117 additions & 79 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ packages:
- 'src/web'
# microservices
- 'src/api/*'
# satellite
- 'src/satellite'
# autodeployment
- 'tools/*'
31 changes: 13 additions & 18 deletions src/satellite/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
module.exports = {
env: {
node: true,
commonjs: true,
es2021: true,
jest: true,
},
extends: 'eslint:recommended',
parserOptions: {
ecmaVersion: 13,
},
plugins: ['anti-trojan-source', 'jest'],
rules: {
/**
* Halt if a trojan source attack is found
* https://github.com/lirantal/eslint-plugin-anti-trojan-source
*/
'anti-trojan-source/no-bidi': 'error',
},
extends: '@senecacdot/eslint-config-telescope',

overrides: [
{
files: ['./**/*.ts', './**/*.tsx'],
env: {
node: true,
commonjs: true,
es2021: true,
jest: true,
},
},
],
};
13 changes: 3 additions & 10 deletions src/satellite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@
"coverage": "jest -c jest.config.js --collect-coverage",
"eslint": "eslint --config .eslintrc.js \"**/*.js\"",
"eslint-fix": "eslint --config .eslintrc.js \"**/*.js\" --fix",
"lint": "pnpm eslint",
"prettier": "prettier --write \"./**/*.{md,jsx,json,html,css,js,yml}\"",
"prettier-check": "prettier --check \"./**/*.{md,jsx,json,html,css,js,yml}\"",
"prepare": "husky install",
"pre-commit": "pretty-quick --staged && pnpm lint"
"lint": "pnpm eslint"
},
"repository": "Seneca-CDOT/satellite",
"license": "BSD-2-Clause",
Expand Down Expand Up @@ -43,14 +39,11 @@
"pnpm": ">=6"
},
"devDependencies": {
"eslint": "8.10.0",
"eslint-plugin-anti-trojan-source": "1.1.0",
"eslint-plugin-jest": "26.1.1",
"eslint": "7.32.0",
"@senecacdot/eslint-config-telescope": "latest",
"get-port": "5.1.1",
"husky": "7.0.4",
"jest": "27.5.1",
"nock": "13.2.4",
"prettier": "2.5.1",
"pretty-quick": "3.1.3"
}
}
6 changes: 3 additions & 3 deletions src/satellite/src/create-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ module.exports = (...args) => {
let status = 500;
let argToSend = '';
let props = {};
for (let i = 0; i < args.length; i++) {
let arg = args[i];
let type = typeof arg;
for (let i = 0; i < args.length; i += 1) {
const arg = args[i];
const type = typeof arg;
// Deal with ElasticSearch Error objects
if (type === 'object' && arg instanceof errors.ResponseError) {
argToSend = createError(arg.statusCode, `ElasticSearch Error:${arg.name}`, arg.meta);
Expand Down
1 change: 1 addition & 0 deletions src/satellite/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module.exports.logger = require('./logger');
module.exports.hash = require('./hash');
module.exports.createError = require('./create-error');
module.exports.createServiceToken = require('./service-token');

module.exports.Router = (options) => createRouter(options);
module.exports.isAuthenticated = isAuthenticated;
module.exports.isAuthorized = isAuthorized;
Expand Down
2 changes: 1 addition & 1 deletion src/satellite/src/redis.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const logger = require('./logger');
const Redis = require('ioredis');
const MockRedis = require('ioredis-mock');
const logger = require('./logger');

// If you need to set the Redis URL, do it in REDIS_URL
const redisUrl = process.env.REDIS_URL || 'redis://redis:6379';
Expand Down
1 change: 1 addition & 0 deletions src/satellite/src/satellite.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable global-require */
const { createTerminus } = require('@godaddy/terminus');

const { createApp, createRouter } = require('./app');
Expand Down
36 changes: 19 additions & 17 deletions src/satellite/test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable @typescript-eslint/no-shadow */
/* eslint no-unused-vars: off */
//ESLint override to prevent errors for callback vars, e.g. (req, user) => {...}
// ESLint override to prevent errors for callback vars, e.g. (req, user) => {...}

const nock = require('nock');
const getPort = require('get-port');
Expand All @@ -8,6 +9,7 @@ const jwt = require('jsonwebtoken');
// Tests cause terminus to leak warning on too many listeners, increase a bit
require('events').EventEmitter.defaultMaxListeners = 32;

const { errors } = require('@elastic/elasticsearch');
const {
Satellite,
Router,
Expand All @@ -21,7 +23,7 @@ const {
Elastic,
fetch,
} = require('./src');
const { errors } = require('@elastic/elasticsearch');

const { JWT_EXPIRES_IN, JWT_ISSUER, JWT_AUDIENCE, JWT_SECRET } = process.env;

const createSatelliteInstance = (options) => {
Expand Down Expand Up @@ -283,7 +285,7 @@ describe('Satellite()', () => {
name: 'test',
});

const router = service.router;
const { router } = service;
router.get('/public', (req, res) => res.json({ hello: 'public' }));
router.get('/protected', isAuthenticated(), (req, res) => {
// Make sure the user payload was added to req
Expand Down Expand Up @@ -336,7 +338,7 @@ describe('Satellite()', () => {
roles: ['user', 'admin'],
});

const router = service.router;
const { router } = service;
router.get('/public', (req, res) => res.json({ hello: 'public' }));
router.get(
'/protected',
Expand All @@ -355,7 +357,7 @@ describe('Satellite()', () => {
// Public should need no bearer token
let res = await fetch(`${url}/public`);
expect(res.ok).toBe(true);
let body = await res.json();
const body = await res.json();
expect(body).toEqual({ hello: 'public' });

// Protected should fail without authorization header
Expand Down Expand Up @@ -395,7 +397,7 @@ describe('Satellite()', () => {
roles: ['user', 'admin'],
});

const router = service.router;
const { router } = service;
router.get('/public', (req, res) => res.json({ hello: 'public' }));
router.get(
'/protected',
Expand Down Expand Up @@ -452,7 +454,7 @@ describe('Satellite()', () => {
const token = createToken({ sub: '[email protected]' });
const decoded = jwt.verify(token, JWT_SECRET);

const router = service.router;
const { router } = service;
router.get('/public', (req, res) => res.json({ hello: 'public' }));
router.get(
'/protected',
Expand All @@ -472,7 +474,7 @@ describe('Satellite()', () => {
// Public should need no bearer token
let res = await fetch(`${url}/public`);
expect(res.ok).toBe(true);
let body = await res.json();
const body = await res.json();
expect(body).toEqual({ hello: 'public' });

// Protected should fail
Expand All @@ -493,7 +495,7 @@ describe('Satellite()', () => {
});
const token = createServiceToken();

const router = service.router;
const { router } = service;
router.get('/public', (req, res) => res.json({ hello: 'public' }));
router.get(
'/protected',
Expand Down Expand Up @@ -540,7 +542,7 @@ describe('Satellite()', () => {
});
const token = createToken({ sub: '[email protected]', roles: ['admin'] });

const router = service.router;
const { router } = service;
router.get('/public', (req, res) => res.json({ hello: 'public' }));
router.get(
'/protected',
Expand Down Expand Up @@ -587,7 +589,7 @@ describe('Satellite()', () => {
});
const token = createToken({ sub: '[email protected]' });

const router = service.router;
const { router } = service;
router.get('/public', (req, res) => res.json({ hello: 'public' }));
router.get(
'/protected',
Expand Down Expand Up @@ -632,7 +634,7 @@ describe('Satellite()', () => {
});
const token = createToken({ sub: '[email protected]' });

const router = service.router;
const { router } = service;
router.get('/public', (req, res) => res.json({ hello: 'public' }));
router.get(
'/protected',
Expand All @@ -649,7 +651,7 @@ describe('Satellite()', () => {
// Public should need no bearer token
let res = await fetch(`${url}/public`);
expect(res.ok).toBe(true);
let body = await res.json();
const body = await res.json();
expect(body).toEqual({ hello: 'public' });

// Protected should fail without authorization header
Expand All @@ -676,7 +678,7 @@ describe('Satellite()', () => {
});
const token = createToken({ sub: '[email protected]' });

const router = service.router;
const { router } = service;
router.get('/public', (req, res) => res.json({ hello: 'public' }));
router.get(
'/protected',
Expand All @@ -695,7 +697,7 @@ describe('Satellite()', () => {
// Public should need no bearer token
let res = await fetch(`${url}/public`);
expect(res.ok).toBe(true);
let body = await res.json();
const body = await res.json();
expect(body).toEqual({ hello: 'public' });

// Protected should fail without authorization header
Expand Down Expand Up @@ -977,7 +979,7 @@ describe('Redis()', () => {
describe('Elastic()', () => {
describe('Tests for regular Elastic()', () => {
test('Testing the name property which should be a string', async () => {
let client = Elastic();
const client = Elastic();

const clientInfo = await client.info();

Expand All @@ -987,7 +989,7 @@ describe('Elastic()', () => {

describe('Tests for mock Elastic()', () => {
const client = Elastic();
const mock = client.mock;
const { mock } = client;

beforeEach(() => mock.clearAll());

Expand Down
2 changes: 1 addition & 1 deletion tools/eslint/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module.exports = {
'plugin:jest-playwright/recommended',
'plugin:import/recommended',
],
plugins: ['prettier', 'promise', 'jest', 'anti-trojan-source'],
plugins: ['prettier', 'promise', 'jest', 'anti-trojan-source', '@typescript-eslint'],
settings: {
'import/resolver': {
node: {},
Expand Down

0 comments on commit 8d0e234

Please sign in to comment.