fix eslint rules

This commit is contained in:
Pavel Murygin 2021-12-15 18:01:14 +03:00
parent af0ee77223
commit 3432968cd4
12 changed files with 101 additions and 95 deletions

View File

@ -1,37 +1,24 @@
module.exports = {
'env': {
'browser': true,
'es2021': true,
'node': true
env: {
browser: true,
es2021: true,
node: true,
},
'extends': [
'eslint:recommended',
'plugin:@typescript-eslint/recommended'
],
'parser': '@typescript-eslint/parser',
'parserOptions': {
'ecmaVersion': 13,
'sourceType': 'module'
extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended'],
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaVersion: 13,
sourceType: 'module',
},
plugins: ['@typescript-eslint', 'simple-import-sort'],
rules: {
indent: ['off'],
'linebreak-style': ['error', 'unix'],
quotes: ['error', 'single', { avoidEscape: true }],
semi: ['error', 'always'],
'no-empty-function': 'off',
'@typescript-eslint/no-empty-function': ['off'],
'simple-import-sort/imports': 'error',
'simple-import-sort/exports': 'error',
},
'plugins': [
'@typescript-eslint'
],
'rules': {
'indent': [
'error',
4
],
'linebreak-style': [
'error',
'unix'
],
'quotes': [
'error',
'single'
],
'semi': [
'error',
'always'
]
}
};

17
package-lock.json generated
View File

@ -32,6 +32,7 @@
"@typescript-eslint/eslint-plugin": "^5.7.0",
"@typescript-eslint/parser": "^5.7.0",
"eslint": "^8.4.1",
"eslint-plugin-simple-import-sort": "^7.0.0",
"jest": "^26.6.3",
"js-base64": "^3.7.2",
"ts-jest": "^26.5.4",
@ -3048,6 +3049,15 @@
"url": "https://opencollective.com/eslint"
}
},
"node_modules/eslint-plugin-simple-import-sort": {
"version": "7.0.0",
"resolved": "https://registry.npmjs.org/eslint-plugin-simple-import-sort/-/eslint-plugin-simple-import-sort-7.0.0.tgz",
"integrity": "sha512-U3vEDB5zhYPNfxT5TYR7u01dboFZp+HNpnGhkDB2g/2E4wZ/g1Q9Ton8UwCLfRV9yAKyYqDh62oHOamvkFxsvw==",
"dev": true,
"peerDependencies": {
"eslint": ">=5.0.0"
}
},
"node_modules/eslint-scope": {
"version": "7.1.0",
"resolved": "https://registry.npmjs.org/eslint-scope/-/eslint-scope-7.1.0.tgz",
@ -11691,6 +11701,13 @@
}
}
},
"eslint-plugin-simple-import-sort": {
"version": "7.0.0",
"resolved": "https://registry.npmjs.org/eslint-plugin-simple-import-sort/-/eslint-plugin-simple-import-sort-7.0.0.tgz",
"integrity": "sha512-U3vEDB5zhYPNfxT5TYR7u01dboFZp+HNpnGhkDB2g/2E4wZ/g1Q9Ton8UwCLfRV9yAKyYqDh62oHOamvkFxsvw==",
"dev": true,
"requires": {}
},
"eslint-scope": {
"version": "7.1.0",
"resolved": "https://registry.npmjs.org/eslint-scope/-/eslint-scope-7.1.0.tgz",

View File

@ -43,6 +43,7 @@
"@typescript-eslint/eslint-plugin": "^5.7.0",
"@typescript-eslint/parser": "^5.7.0",
"eslint": "^8.4.1",
"eslint-plugin-simple-import-sort": "^7.0.0",
"jest": "^26.6.3",
"js-base64": "^3.7.2",
"ts-jest": "^26.5.4",

View File

@ -14,18 +14,19 @@
* limitations under the License.
*/
import Websockets from 'libp2p-websockets';
import Mplex from 'libp2p-mplex';
import Lib2p2Peer from 'libp2p';
import { NOISE } from '@chainsafe/libp2p-noise';
import { decode, encode } from 'it-length-prefixed';
import pipe from 'it-pipe';
import * as log from 'loglevel';
import { Particle } from './Particle';
import { NOISE } from '@chainsafe/libp2p-noise';
import PeerId from 'peer-id';
import { Multiaddr } from 'multiaddr';
import { all as allow_all } from 'libp2p-websockets/src/filters';
import Lib2p2Peer from 'libp2p';
import { Connection } from 'libp2p-interfaces/src/topology';
import Mplex from 'libp2p-mplex';
import Websockets from 'libp2p-websockets';
import { all as allow_all } from 'libp2p-websockets/src/filters';
import * as log from 'loglevel';
import { Multiaddr } from 'multiaddr';
import PeerId from 'peer-id';
import { Particle } from './Particle';
export const PROTOCOL_NAME = '/fluence/particle/2.0.0';
@ -128,7 +129,7 @@ export class FluenceConnection {
public async connect() {
await this._lib2p2Peer.start();
log.debug(`dialing to the node with client's address: ` + this._lib2p2Peer.peerId.toB58String());
log.debug(`dialing to the node with client's address: ${this._lib2p2Peer.peerId.toB58String()}`);
try {
this._connection = await this._lib2p2Peer.dial(this._relayAddress);

View File

@ -1,3 +1,4 @@
/* eslint-disable sort-imports */
/*
* Copyright 2021 Fluence Labs Limited
*
@ -14,27 +15,20 @@
* limitations under the License.
*/
import {
AirInterpreter,
CallRequestsArray,
CallResultsArray,
InterpreterResult,
LogLevel,
CallServiceResult as AvmCallServiceResult,
} from '@fluencelabs/avm';
import { Multiaddr } from 'multiaddr';
import { CallServiceData, CallServiceResult, GenericCallServiceHandler, ResultCodes } from './commonTypes';
import { CallServiceHandler as LegacyCallServiceHandler } from './compilerSupport/LegacyCallServiceHandler';
import { PeerIdB58 } from './commonTypes';
import { FluenceConnection } from './FluenceConnection';
import { Particle, ParticleExecutionStage, ParticleQueueItem } from './Particle';
import { KeyPair } from './KeyPair';
import { createInterpreter, dataToString } from './utils';
import { filter, pipe, Subject, tap } from 'rxjs';
import { RequestFlow } from './compilerSupport/v1';
import { AirInterpreter, InterpreterResult, LogLevel } from '@fluencelabs/avm';
import log from 'loglevel';
import { Multiaddr } from 'multiaddr';
import { filter, pipe, Subject, tap } from 'rxjs';
import { BuiltInServiceContext, builtInServices } from './builtInServices';
import { instanceOf } from 'ts-pattern';
import { CallServiceData, CallServiceResult, GenericCallServiceHandler, ResultCodes } from './commonTypes';
import { PeerIdB58 } from './commonTypes';
import { CallServiceHandler as LegacyCallServiceHandler } from './compilerSupport/LegacyCallServiceHandler';
import { RequestFlow } from './compilerSupport/v1';
import { FluenceConnection } from './FluenceConnection';
import { KeyPair } from './KeyPair';
import { Particle, ParticleExecutionStage, ParticleQueueItem } from './Particle';
import { createInterpreter, dataToString } from './utils';
/**
* Node of the Fluence network specified as a pair of node's multiaddr and it's peer id
@ -111,12 +105,12 @@ export interface PeerStatus {
/**
* Is the peer initialized or not
*/
isInitialized: Boolean;
isInitialized: boolean;
/**
* Is the peer connected to network or not
*/
isConnected: Boolean;
isConnected: boolean;
/**
* The Peer's identification in the Fluence network
@ -186,7 +180,7 @@ export class FluencePeer {
if (config?.connectTo) {
let connectToMultiAddr: Multiaddr;
let fromNode = (config.connectTo as any).multiaddr;
const fromNode = (config.connectTo as any).multiaddr;
if (fromNode) {
connectToMultiAddr = new Multiaddr(fromNode);
} else {
@ -415,7 +409,7 @@ export class FluencePeer {
}
private _createParticlesProcessingQueue() {
let particlesQueue = new Subject<ParticleQueueItem>();
const particlesQueue = new Subject<ParticleQueueItem>();
let prevData: Uint8Array = Buffer.from([]);
particlesQueue
@ -450,7 +444,7 @@ export class FluencePeer {
// execute call requests if needed
// and put particle with the results back to queue
if (result.callRequests.length > 0) {
for (let [key, cr] of result.callRequests) {
for (const [key, cr] of result.callRequests) {
const req = {
fnName: cr.functionName,
args: cr.arguments,
@ -547,7 +541,7 @@ export class FluencePeer {
private _stopParticleProcessing() {
// do not hang if the peer has been stopped while some of the timeouts are still being executed
for (let item of this._timeouts) {
for (const item of this._timeouts) {
clearTimeout(item);
}
this._particleQueues.clear();
@ -581,8 +575,8 @@ function serviceFnKey(serviceId: string, fnName: string) {
function registerDefaultServices(peer: FluencePeer, context: BuiltInServiceContext) {
const ctx = builtInServices(context);
for (let serviceId in ctx) {
for (let fnName in ctx[serviceId]) {
for (const serviceId in ctx) {
for (const fnName in ctx[serviceId]) {
const h = ctx[serviceId][fnName];
peer.internals.regHandler.common(serviceId, fnName, h);
}

View File

@ -14,8 +14,8 @@
* limitations under the License.
*/
import * as PeerId from 'peer-id';
import { keys } from 'libp2p-crypto';
import * as PeerId from 'peer-id';
export class KeyPair {
/**

View File

@ -1,3 +1,4 @@
/* eslint-disable sort-imports */
/*
* Copyright 2020 Fluence Labs Limited
*
@ -14,10 +15,11 @@
* limitations under the License.
*/
import { v4 as uuidv4 } from 'uuid';
import { fromByteArray, toByteArray } from 'base64-js';
import { CallResultsArray, LogLevel } from '@fluencelabs/avm';
import { fromByteArray, toByteArray } from 'base64-js';
import log from 'loglevel';
import { v4 as uuidv4 } from 'uuid';
import { ParticleContext } from './commonTypes';
import { dataToString } from './utils';
@ -88,17 +90,16 @@ export class Particle {
}
toString(): string {
const particle = this;
const payload = {
action: 'Particle',
id: particle.id,
init_peer_id: particle.initPeerId,
timestamp: particle.timestamp,
ttl: particle.ttl,
script: particle.script,
id: this.id,
init_peer_id: this.initPeerId,
timestamp: this.timestamp,
ttl: this.ttl,
script: this.script,
// TODO: copy signature from a particle after signatures will be implemented on nodes
signature: [],
data: fromByteArray(particle.data),
data: fromByteArray(this.data),
};
return JSON.stringify(payload);

View File

@ -15,9 +15,9 @@
*/
import { CallServiceResult } from '@fluencelabs/avm';
import { encode, decode } from 'bs58';
import { PeerIdB58 } from 'src';
import { GenericCallServiceHandler, ResultCodes } from './commonTypes';
import { decode, encode } from 'bs58';
import { GenericCallServiceHandler, PeerIdB58, ResultCodes } from './commonTypes';
import { KeyPair } from './KeyPair';
const success = (result: any): CallServiceResult => {
@ -70,7 +70,7 @@ export function builtInServices(context: BuiltInServiceContext): {
const str = incorrectArgIndices.join(', ');
return error(`All arguments of 'concat' must be arrays: arguments ${str} are not`);
} else {
return success([].concat.apply([], req.args));
return success([].concat(...req.args));
}
},

View File

@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/ban-types */
/*
* Copyright 2021 Fluence Labs Limited
*
@ -27,6 +28,7 @@ export const callLegacyCallServiceHandler = (
): CallServiceResult => {
// trying particle-specific handler
if (particleSpecificHandler !== undefined) {
// eslint-disable-next-line no-var
var res = particleSpecificHandler.execute(req);
}

View File

@ -14,11 +14,11 @@
* limitations under the License.
*/
import { CallServiceHandler } from './LegacyCallServiceHandler';
import { Particle } from '../Particle';
import { CallServiceHandler } from './LegacyCallServiceHandler';
export { FluencePeer } from '../FluencePeer';
export { CallParams, ResultCodes } from '../commonTypes';
export { FluencePeer } from '../FluencePeer';
/**
* @deprecated This class exists to glue legacy RequestFlowBuilder api with restructured async FluencePeer.
@ -43,8 +43,8 @@ export class RequestFlowBuilder {
private _timeout: () => void = () => {};
build(): RequestFlow {
let h = new CallServiceHandler();
for (let c of this._configs) {
const h = new CallServiceHandler();
for (const c of this._configs) {
c(h);
}

View File

@ -16,12 +16,13 @@
import { SecurityTetraplet } from '@fluencelabs/avm';
import { match } from 'ts-pattern';
import { CallParams, Fluence, FluencePeer } from '../../index';
import { CallServiceData, GenericCallServiceHandler, CallServiceResult, ResultCodes } from '../commonTypes';
import { CallServiceData, CallServiceResult, GenericCallServiceHandler, ResultCodes } from '../commonTypes';
import { Particle } from '../Particle';
export { FluencePeer } from '../FluencePeer';
export { CallParams } from '../commonTypes';
export { FluencePeer } from '../FluencePeer';
/**
* Represents the Aqua Option type
@ -227,7 +228,7 @@ export function callFunction(rawFnArgs: Array<any>, def: FunctionCallDef, script
const fn = async (req: CallServiceData): Promise<CallServiceResult> => {
const args = convertArgsFromReqToUserCall(req, callbackDef.callback.argDefs);
// arg is function at this point
const result = await arg.apply(null, args);
const result = await arg(...args);
let res;
switch (callbackDef.callback.returnType.tag) {
case 'void':
@ -366,6 +367,7 @@ export function registerService(args: any[], def: ServiceDef) {
// Checking for missing keys
const requiredKeys = def.functions.map((x) => x.functionName);
const incorrectServiceDefinitions = requiredKeys.filter((f) => !(f in service));
// eslint-disable-next-line no-extra-boolean-cast
if (!!incorrectServiceDefinitions.length) {
throw new Error(
`Error registering service ${serviceId}: missing functions: ` +
@ -373,14 +375,14 @@ export function registerService(args: any[], def: ServiceDef) {
);
}
for (let singleFunction of def.functions) {
for (const singleFunction of def.functions) {
// The function has type of (arg1, arg2, arg3, ... , callParams) => CallServiceResultType | void
// Account for the fact that user service might be defined as a class - .bind(...)
const userDefinedHandler = service[singleFunction.functionName].bind(service);
peer.internals.regHandler.common(serviceId, singleFunction.functionName, async (req) => {
const args = convertArgsFromReqToUserCall(req, singleFunction.argDefs);
const rawResult = await userDefinedHandler.apply(null, args);
const rawResult = await userDefinedHandler(...args);
const result = match(singleFunction.returnType)
.with({ tag: 'primitive' }, () => rawResult)
.with({ tag: 'optional' }, () => tsToAquaOpt(rawResult))
@ -434,7 +436,7 @@ const extractCallParams = (
req: CallServiceData,
argDefs: Array<ArgDef<OptionalType | PrimitiveType>>,
): CallParams<any> => {
let tetraplets: { [key in string]: SecurityTetraplet[] } = {};
const tetraplets: { [key in string]: SecurityTetraplet[] } = {};
for (let i = 0; i < req.args.length; i++) {
if (argDefs[i]) {
tetraplets[argDefs[i].name] = req.tetraplets[i];

View File

@ -16,6 +16,7 @@
import { AirInterpreter, LogLevel as AvmLogLevel } from '@fluencelabs/avm';
import log from 'loglevel';
import { CallServiceData, CallServiceResult, CallServiceResultType, ResultCodes } from './commonTypes';
import { AvmLoglevel, FluencePeer } from './FluencePeer';
import { Particle, ParticleExecutionStage } from './Particle';
@ -53,7 +54,7 @@ export const MakeServiceCall = (fn: (args: any[]) => CallServiceResultType) => {
};
};
export const handleTimeout = (fn: Function) => (stage: ParticleExecutionStage) => {
export const handleTimeout = (fn: () => void) => (stage: ParticleExecutionStage) => {
if (stage.stage === 'expired') {
fn();
}