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

Add eslint rule to no allow require() in ts files #3821

Closed
wants to merge 15 commits into from
98 changes: 54 additions & 44 deletions creator-node/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,65 @@
module.exports = {
extends: [
'standard',
'plugin:import/typescript',
'plugin:@typescript-eslint/recommended',
'prettier'
'standard',
'plugin:import/typescript',
'plugin:@typescript-eslint/recommended',
'prettier'
],
"env": {
"node": true,
"mocha": true
env: {
node: true,
mocha: true
},
plugins: ['prettier'],
rules: {
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/no-non-null-assertion': 'off',
'@typescript-eslint/member-delimiter-style': 'off',
'@typescript-eslint/no-empty-interface': 'off',
'@typescript-eslint/camelcase': 'off',
'@typescript-eslint/ban-ts-ignore': 'off',
'@typescript-eslint/ban-ts-comment': 'off',
'@typescript-eslint/ban-types': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-use-before-define': 'off',
'@typescript-eslint/no-empty-function': 'off',
'@typescript-eslint/no-var-requires': 'off',
'@typescript-eslint/no-unused-vars': 'off', // We should turn this one on soon
'@typescript-eslint/no-this-alias': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-require-imports': 'warn',

'no-use-before-define': 'off',
camelcase: 'off',
'no-unused-vars': 'off',
'func-call-spacing': 'off',
semi: ['error', 'never'],
'no-undef': 'error',
'no-empty': 'off',
'arrow-parens': 'off',
'padded-blocks': 'off',
'no-prototype-builtins': 'off', // added by Dheeraj, to remove
'no-async-promise-executor': 'off', // added by Dheeraj, to remove
'no-useless-catch': 'off', // added by Dheeraj, to remove
'prefer-regex-literals': 'off', // added by Dheeraj, to remove
'no-unmodified-loop-condition': 'off', // added by Dheeraj, to remove
'array-callback-return': 'off', // added by Dheeraj, to remove
'node/no-path-concat': 'off', // added by Dheeraj, to remove
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/no-non-null-assertion': 'off',
'@typescript-eslint/member-delimiter-style': 'off',
'@typescript-eslint/no-empty-interface': 'off',
'@typescript-eslint/camelcase': 'off',
'@typescript-eslint/ban-ts-ignore': 'off',
'@typescript-eslint/ban-ts-comment': 'off',
'@typescript-eslint/ban-types': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-use-before-define': 'off',
'@typescript-eslint/no-empty-function': 'off',
'@typescript-eslint/no-var-requires': 'off',
'@typescript-eslint/no-unused-vars': 'off', // We should turn this one on soon
'@typescript-eslint/no-this-alias': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',

'space-before-function-paren': 'off',
'generator-star-spacing': 'off',
'no-use-before-define': 'off',
camelcase: 'off',
'no-unused-vars': 'off',
'func-call-spacing': 'off',
semi: ['error', 'never'],
'no-undef': 'error',
'no-empty': 'off',
'arrow-parens': 'off',
'padded-blocks': 'off',
'no-prototype-builtins': 'off', // added by Dheeraj, to remove
'no-async-promise-executor': 'off', // added by Dheeraj, to remove
'no-useless-catch': 'off', // added by Dheeraj, to remove
'prefer-regex-literals': 'off', // added by Dheeraj, to remove
'no-unmodified-loop-condition': 'off', // added by Dheeraj, to remove
'array-callback-return': 'off', // added by Dheeraj, to remove
'node/no-path-concat': 'off', // added by Dheeraj, to remove

"prettier/prettier": "error",
'space-before-function-paren': 'off',
'generator-star-spacing': 'off',

'import/no-unresolved': 'error',
'import/order': 'off'
}
'prettier/prettier': 'error',

'import/no-unresolved': 'error',
'import/order': 'off'
},
overrides: [
{
files: ['src/**/*.ts'],
rules: {
'@typescript-eslint/no-require-imports': 'error'
}
}
]
}
6 changes: 6 additions & 0 deletions creator-node/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion creator-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"test:coverage:ci": "nyc --reporter=lcov --reporter=text npm run test:ci && nyc report --reporter=text-lcov | coveralls",
"lint:fix": "eslint --fix --ext=js,ts src",
"lint": "eslint --ext=js,ts src",
"build": "./node_modules/.bin/tsc --project tsconfig.build.json"
"build": "./node_modules/.bin/tsc --project tsconfig.build.json",
"type-check": "tsc --noEmit"
},
"keywords": [],
"author": "",
Expand Down Expand Up @@ -87,6 +88,7 @@
"@types/bull": "^3.15.8",
"@types/bunyan": "^1.8.8",
"@types/chai": "^4.3.3",
"@types/death": "^1.1.2",
"@types/express": "4.17.12",
"@types/lodash": "^4.14.182",
"@types/mocha": "^9.1.1",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { timeout, currentNodeShouldHandleTranscode } from '../../utils'
const path = require('path')
const versionInfo = require(path.join(process.cwd(), '.version.json'))
const { Keypair } = require('@solana/web3.js')

const config = require('../../config')
const utils = require('../../utils.js')
const { MONITORS } = require('../../monitors/monitors')

const MIN_NUBMER_OF_CPUS = 8 // 8 cpu
Expand Down Expand Up @@ -114,7 +114,7 @@ const healthCheck = async (
const asyncProcessingQueueJobs = await getAsyncProcessingQueueJobs()

const isAvailable = await transcodingQueueIsAvailable()
const shouldHandleTranscode = utils.currentNodeShouldHandleTranscode({
const shouldHandleTranscode = currentNodeShouldHandleTranscode({
transcodingQueueCanAcceptMoreJobs: isAvailable,
spID: config.get('spID')
})
Expand Down Expand Up @@ -277,7 +277,7 @@ const healthCheckVerbose = async (
*/
const healthCheckDuration = async () => {
// Wait 5 minutes, intentionally holding this route open
await utils.timeout(300000)
await timeout(300000)
return { success: true }
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as Utils from '../../utils'
const axios = require('axios')
const fs = require('fs')
const fsExtra = require('fs-extra')
const FormData = require('form-data')

const config = require('../../config.js')
const Utils = require('../../utils')
const asyncRetry = require('../../utils/asyncRetry')
const { logger: genericLogger } = require('../../logging')
const {
Expand Down
4 changes: 2 additions & 2 deletions creator-node/src/fileManager.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { findCIDInNetwork } from './utils'
import * as Utils from './utils'
const path = require('path')
const fs = require('fs-extra')
const multer = require('multer')
const getUuid = require('uuid/v4')
const axios = require('axios')

const config = require('./config')
const Utils = require('./utils')
const { libs: audiusLibs } = require('@audius/sdk')
const LibsUtils = audiusLibs.Utils
const DiskManager = require('./diskManager')
const { logger: genericLogger } = require('./logging')
const { sendResponse, errorResponseBadRequest } = require('./apiHelpers')
const { findCIDInNetwork } = require('./utils')
const DecisionTree = require('./utils/decisionTree')

const MAX_AUDIO_FILE_SIZE = parseInt(config.get('maxAudioFileSizeBytes')) // Default = 250,000,000 bytes = 250MB
Expand Down
4 changes: 2 additions & 2 deletions creator-node/src/hashids.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const Hashids = require('hashids/cjs')
import Hashids from 'hashids/cjs'

/* We use a JS implementation of the the HashIds protocol (http://hashids.org)
* to obfuscate our monotonically increasing int IDs as
Expand All @@ -20,5 +20,5 @@ export function encode(id: number): string {
export function decode(id: string): number | null {
const ids = hashids.decode(id)
if (!ids.length) return null
return ids[0]
return ids[0] as number
}
39 changes: 21 additions & 18 deletions creator-node/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
'use strict'

const { setupTracing } = require('./tracer')
setupTracing('content-node')

const ON_DEATH = require('death')
const EthereumWallet = require('ethereumjs-wallet')
const { Keypair } = require('@solana/web3.js')

const initializeApp = require('./app')
const config = require('./config')
/* eslint-disable import/first */
import { setupTracing } from './tracer'
setupTracing()

import type Signal from 'death'
import ON_DEATH from 'death'
// @ts-ignore
import EthereumWallet from 'ethereumjs-wallet'
import { Keypair } from '@solana/web3.js'

import initializeApp from './app'
import config from './config'
import { runMigrations, clearRunningQueries } from './migrationManager'
import { logger } from './logging'
import { serviceRegistry } from './serviceRegistry'
import { WalletWriteLock } from './redis'

// eslint-disable-next-line @typescript-eslint/no-require-imports
const { sequelize } = require('./models')
const { runMigrations, clearRunningQueries } = require('./migrationManager')
const { logger } = require('./logging')
const { serviceRegistry } = require('./serviceRegistry')
const redisClient = require('./redis')

const exitWithError = (...msg: any[]) => {
logger.error('ERROR: ', ...msg)
Expand Down Expand Up @@ -121,7 +124,7 @@ const startApp = async () => {

// Clear all redis locks
try {
await redisClient.WalletWriteLock.clearWriteLocks()
await WalletWriteLock.clearWriteLocks()
} catch (e: any) {
logger.warn(`Could not clear write locks. Skipping..: ${e.message}`)
}
Expand All @@ -134,11 +137,11 @@ const startApp = async () => {
serviceRegistry.initServicesThatRequireServer(appInfo.app)

// when app terminates, close down any open DB connections gracefully
ON_DEATH((signal: any, error: any) => {
ON_DEATH((signal: 'SIGINT' | 'SIGTERM' | 'SIGQUIT' | Error) => {
// NOTE: log messages emitted here may be swallowed up if using the bunyan CLI (used by
// default in `npm start` command). To see messages emitted after a kill signal, do not
// use the bunyan CLI.
logger.info('Shutting down db and express app...', signal, error)
logger.info('Shutting down db and express app...', signal)
sequelize.close()
if (appInfo) {
appInfo.server.close()
Expand Down
4 changes: 2 additions & 2 deletions creator-node/src/logging.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ function loggingMiddleware(req, res, next) {

/**
* Creates and returns a child logger for provided logger
* @param {Object} logger bunyan parent logger instance
* @param {Logger} logger bunyan parent logger instance
* @param {Object} options optional object to define child logger properties. adds to JSON fields, allowing for better log filtering/querying
* @returns {Object} child logger instance with defined options
* @returns {Logger} child logger instance with defined options
*/
function createChildLogger(logger, options = {}) {
return logger.child(options)
Expand Down
29 changes: 15 additions & 14 deletions creator-node/src/middlewares.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { strToReplicaSet, getAllRegisteredCNodes, timeout } from './utils'
const promiseAny = require('promise.any')

const {
Expand All @@ -9,8 +10,6 @@ const {
const config = require('./config')
const sessionManager = require('./sessionManager')
const models = require('./models')
const utils = require('./utils')
const { strToReplicaSet } = require('./utils/index')
const { hasEnoughStorageSpace } = require('./fileManager')
const { getMonitors, MONITORS } = require('./monitors/monitors')
const { verifyRequesterIsValidSP } = require('./apiSigning')
Expand Down Expand Up @@ -176,7 +175,7 @@ async function ensurePrimaryMiddleware(req, res, next) {
* Currently `req.session.creatorNodeEndpoints` is only used by `issueAndWaitForSecondarySyncRequests()`
* There is a possibility of failing to retrieve endpoints for each spID, so the consumer of req.session.creatorNodeEndpoints must perform null checks
*/
const allRegisteredCNodes = await utils.getAllRegisteredCNodes(libs, logger)
const allRegisteredCNodes = await getAllRegisteredCNodes(libs, logger)
const replicaSetEndpoints = replicaSetSpIDs.map((replicaSpID) => {
if (replicaSpID === selfSpID) {
return nodeConfig.get('creatorNodeEndpoint')
Expand Down Expand Up @@ -526,16 +525,18 @@ async function getOwnEndpoint({ libs }) {
* - Errors if retrieved primary does not match myCnodeEndpoint
* - If neither of above conditions are met, falls back to single discprov query without polling
*
* @param {string} wallet - wallet used to query discprov for user data
* @param {Object} serviceRegistry
* @param {Object} logger
* @param {number} blockNumber - blocknumber of eth TX preceding CN call
* @param {string} myCnodeEndpoint - endpoint of this CN
* @param {boolean} ensurePrimary - determines if function should error if this CN is not primary
* @param {Object} param
* @param {string} param.wallet - wallet used to query discprov for user data
* @param {Object=} param.serviceRegistry
* @param {Object} param.logger
* @param {number | null} param.blockNumber - blocknumber of eth TX preceding CN call
* @param {string=} param.myCnodeEndpoint - endpoint of this CN
* @param {boolean} param.ensurePrimary - determines if function should error if this CN is not primary
* @param {AudiusLibs} param.libs - AudiusLibs
*
* @returns {ReplicaSet} - replica set object with optional primary, secondary1, and secondary2 properties
*/
async function getUserReplicaSetEndpointsFromDiscovery({
export async function getUserReplicaSetEndpointsFromDiscovery({
wallet,
libs,
logger,
Expand Down Expand Up @@ -594,7 +595,7 @@ async function getUserReplicaSetEndpointsFromDiscovery({
logger.info(e)
}

await utils.timeout(RetryTimeout)
await timeout(RetryTimeout)
logger.info(
`getUserReplicaSetEndpointsFromDiscovery AFTER TIMEOUT retry #${retry}/${MaxRetries} || time from start: ${
Date.now() - start2
Expand Down Expand Up @@ -660,7 +661,7 @@ async function getUserReplicaSetEndpointsFromDiscovery({
logger.info(e)
}

await utils.timeout(RetryTimeout)
await timeout(RetryTimeout)
logger.info(
`getUserReplicaSetEndpointsFromDiscovery AFTER TIMEOUT retry #${retry}/${MaxRetries} || time from start: ${
Date.now() - start2
Expand Down Expand Up @@ -774,7 +775,7 @@ async function getReplicaSetSpIDs({
errorMsg = e.message
} // Ignore all errors until MAX_RETRIES exceeded

await utils.timeout(RETRY_TIMEOUT_MS)
await timeout(RETRY_TIMEOUT_MS)
}

// Error if indexed blockNumber but didn't find any replicaSet for user
Expand Down Expand Up @@ -832,7 +833,7 @@ async function getReplicaSetSpIDs({
errorMsg = e.message
} // Ignore all errors until MAX_RETRIES exceeded

await utils.timeout(RETRY_TIMEOUT_MS)
await timeout(RETRY_TIMEOUT_MS)
}

// Error if failed to retrieve replicaSet
Expand Down
2 changes: 1 addition & 1 deletion creator-node/src/routes/audiusUsers.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { validateStateForImageDirCIDAndReturnFileUUID } from '../utils'
const express = require('express')
const { Buffer } = require('buffer')
const fs = require('fs')
Expand All @@ -11,7 +12,6 @@ const {
errorResponseBadRequest,
errorResponseServerError
} = require('../apiHelpers')
const { validateStateForImageDirCIDAndReturnFileUUID } = require('../utils')
const validateMetadata = require('../utils/validateAudiusUserMetadata')
const {
authMiddleware,
Expand Down
Loading