Skip to content

Commit

Permalink
*: commonize eslint
Browse files Browse the repository at this point in the history
  • Loading branch information
tharvik committed Feb 13, 2024
1 parent 0fd8438 commit a4d06b1
Show file tree
Hide file tree
Showing 109 changed files with 14,703 additions and 12,925 deletions.
14 changes: 14 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// @ts-check

module.exports = {
root: true,
extends: "standard-with-typescript",
parserOptions: {
project: "./tsconfig.eslint.json",
tsconfigRootDir: __dirname,
},
ignorePatterns: ["**/dist/"],
env: {
mocha: true,
},
};
2 changes: 0 additions & 2 deletions cli/.eslintignore

This file was deleted.

16 changes: 3 additions & 13 deletions cli/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
{
"extends": "standard-with-typescript",
"parserOptions": {
"project": "./tsconfig.eslint.json"
},
"env": {
"node": true,
"es6": true,
"mocha": true
},
"rules": {
// TODO need strict
"@typescript-eslint/strict-boolean-expressions": "off"
}
"env": {
"node": true
}
}
9 changes: 1 addition & 8 deletions cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"watch": "nodemon --ext ts --ignore dist --watch ../discojs/discojs-node/dist --watch ../server/dist --exec npm run",
"start": "npm run build && node dist/benchmark.js",
"build": "tsc",
"lint": "npx eslint --max-warnings 0 .",
"lint": "npx eslint --ext ts --max-warnings 0 .",
"test": ": nothing"
},
"author": "",
Expand All @@ -18,13 +18,6 @@
"tslib": "2"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "4",
"@typescript-eslint/parser": "4",
"eslint": "7",
"eslint-config-standard-with-typescript": "21",
"eslint-plugin-import": "2",
"eslint-plugin-node": "11",
"eslint-plugin-promise": "5",
"nodemon": "3",
"ts-command-line-args": "2"
}
Expand Down
4 changes: 0 additions & 4 deletions cli/tsconfig.eslint.json

This file was deleted.

2 changes: 0 additions & 2 deletions discojs/discojs-core/.eslintignore

This file was deleted.

15 changes: 0 additions & 15 deletions discojs/discojs-core/.eslintrc.json

This file was deleted.

9 changes: 2 additions & 7 deletions discojs/discojs-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"watch": "nodemon --ext ts --ignore dist --exec npm run",
"build": "tsc",
"lint": "npx eslint --max-warnings 0 .",
"lint": "npx eslint --ext ts --max-warnings 0 .",
"test": "mocha",
"docs": "typedoc ./src/index.ts --theme oxide"
},
Expand Down Expand Up @@ -35,16 +35,11 @@
"@types/chai": "4",
"@types/mocha": "9",
"@types/simple-peer": "9",
"@typescript-eslint/eslint-plugin": "4",
"@typescript-eslint/parser": "4",
"chai": "4",
"eslint": "7",
"eslint-config-standard-with-typescript": "21",
"mocha": "9",
"nodemon": "3",
"ts-node": "10",
"typedoc": "0.22",
"typedoc-theme-oxide": "0.1",
"typescript": "4"
"typedoc-theme-oxide": "0.1"
}
}
28 changes: 22 additions & 6 deletions discojs/discojs-core/src/aggregator/base.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
import { client, Task, tf, AsyncInformant } from '..'
import { type client, type Task, type tf, type AsyncInformant } from '..'

import { EventEmitter } from 'events'

import { Map, Set } from 'immutable'
import { List, Map, Set } from 'immutable'

export enum AggregationStep {
ADD,
UPDATE,
AGGREGATE
}

class AggregationEventEmitter<T> {
private listeners = List<[once: boolean, act: (_: T) => void]>()

on (_: 'aggregation', act: (_: T) => void): void {
this.listeners = this.listeners.push([false, act])
}

once (_: 'aggregation', act: (_: T) => void): void {
this.listeners = this.listeners.push([true, act])
}

emit (_: 'aggregation', aggregated: T): void {
const listeners = this.listeners
this.listeners = this.listeners.filterNot(([once, _]) => once)
listeners.forEach(([_, act]) => { act(aggregated) })
}
}

/**
* Main, abstract, aggregator class whose role is to buffer contributions and to produce
* a result based off their aggregation, whenever some defined condition is met.
Expand All @@ -31,7 +47,7 @@ export abstract class Base<T> {
* Triggers the resolve of the result promise and the preparation for the
* next aggregation round.
*/
private readonly eventEmitter: EventEmitter
private readonly eventEmitter: AggregationEventEmitter<T>

protected informant?: AsyncInformant<T>
/**
Expand Down Expand Up @@ -71,7 +87,7 @@ export abstract class Base<T> {
*/
public readonly communicationRounds = 1
) {
this.eventEmitter = new EventEmitter()
this.eventEmitter = new AggregationEventEmitter()
this.contributions = Map()
this._nodes = Set()

Expand Down
2 changes: 1 addition & 1 deletion discojs/discojs-core/src/aggregator/get.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { aggregator, Task } from '..'
import { aggregator, type Task } from '..'

/**
* Enumeration of the available types of aggregator.
Expand Down
4 changes: 2 additions & 2 deletions discojs/discojs-core/src/aggregator/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { WeightsContainer } from '../weights'
import { Base } from './base'
import { type WeightsContainer } from '../weights'
import { type Base } from './base'

export { Base as AggregatorBase, AggregationStep } from './base'
export { MeanAggregator } from './mean'
Expand Down
7 changes: 3 additions & 4 deletions discojs/discojs-core/src/aggregator/mean.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import { assert, expect } from 'chai'
import { Map } from 'immutable'
import { type Map } from 'immutable'

import { aggregator, defaultTasks, client, Task, tf } from '..'
import { aggregator, defaultTasks, type client, type Task, type tf } from '..'
import { AggregationStep } from './base'

const task = defaultTasks.titanic.getTask()
Expand Down Expand Up @@ -109,7 +108,7 @@ describe('mean aggregator tests', () => {
mockAggregatedWeights = 5
result = aggregator.receiveResult()
const t1 = Date.now()
newWeights.map(async (w) => aggregator.add(w.toString(), w, t1))
newWeights.map((w) => aggregator.add(w.toString(), w, t1))
expect(await result).eql(mockAggregatedWeights)
expect(aggregator.round).equal(2)
})
Expand Down
4 changes: 2 additions & 2 deletions discojs/discojs-core/src/aggregator/mean.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Map } from 'immutable'
import { type Map } from 'immutable'

import { AggregationStep, Base as Aggregator } from './base'
import { Task, WeightsContainer, aggregation, tf, client } from '..'
import { type Task, type WeightsContainer, aggregation, type tf, type client } from '..'

/**
* Mean aggregator whose aggregation step consists in computing the mean of the received weights.
Expand Down
4 changes: 2 additions & 2 deletions discojs/discojs-core/src/aggregator/robust.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Base as Aggregator } from './base'
import { client, WeightsContainer } from '..'
import { type client, type WeightsContainer } from '..'

import { Map } from 'immutable'
import { type Map } from 'immutable'

export type Momentum = WeightsContainer

Expand Down
2 changes: 1 addition & 1 deletion discojs/discojs-core/src/aggregator/secure.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AggregationStep, Base as Aggregator } from './base'
import { tf, aggregation, Task, WeightsContainer, client } from '..'
import { tf, aggregation, type Task, type WeightsContainer, type client } from '..'

import * as crypto from 'crypto'

Expand Down
2 changes: 1 addition & 1 deletion discojs/discojs-core/src/async_informant.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AggregatorBase } from './aggregator'
import { type AggregatorBase } from './aggregator'

export class AsyncInformant<T> {
private _round = 0
Expand Down
10 changes: 5 additions & 5 deletions discojs/discojs-core/src/client/base.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import axios from 'axios'
import { Set } from 'immutable'
import { type Set } from 'immutable'

import { tf, Task, TrainingInformant, serialization, WeightsContainer } from '..'
import { NodeID } from './types'
import { EventConnection } from './event_connection'
import { Aggregator } from '../aggregator'
import { type tf, type Task, type TrainingInformant, serialization, type WeightsContainer } from '..'
import { type NodeID } from './types'
import { type EventConnection } from './event_connection'
import { type Aggregator } from '../aggregator'

/**
* Main, abstract, class representing a Disco client in a network, which handles
Expand Down
10 changes: 5 additions & 5 deletions discojs/discojs-core/src/client/decentralized/base.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Map, Set } from 'immutable'

import { TrainingInformant, WeightsContainer, serialization } from '../..'
import { Client, NodeID } from '..'
import { type, ClientConnected } from '../messages'
import { type TrainingInformant, type WeightsContainer, serialization } from '../..'
import { Client, type NodeID } from '..'
import { type, type ClientConnected } from '../messages'
import { timeout } from '../utils'
import { EventConnection, WebSocketServer, waitMessage, PeerConnection, waitMessageWithTimeout } from '../event_connection'
import { type EventConnection, WebSocketServer, waitMessage, type PeerConnection, waitMessageWithTimeout } from '../event_connection'
import { PeerPool } from './peer_pool'
import * as messages from './messages'

Expand Down Expand Up @@ -58,7 +58,7 @@ export class Base extends Client {
peers,
this.server,
// Init receipt of peers weights
(conn) => this.receivePayloads(conn, round)
(conn) => { this.receivePayloads(conn, round) }
)

console.info(`[${this.ownId}] received peers for round ${round}:`, connections.keySeq().toJS())
Expand Down
9 changes: 3 additions & 6 deletions discojs/discojs-core/src/client/decentralized/messages.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { SignalData } from 'simple-peer'

import { weights } from '../../serialization'

import { isNodeID, NodeID } from '../types'

import { type, ClientConnected, AssignNodeID, hasMessageType } from '../messages'
import { type SignalData } from './peer'
import { isNodeID, type NodeID } from '../types'
import { type, type ClientConnected, type AssignNodeID, hasMessageType } from '../messages'

/// Phase 0 communication (between server and peers)

Expand Down
12 changes: 5 additions & 7 deletions discojs/discojs-core/src/client/decentralized/peer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ describe('peer', function () {
peer2 = new Peer('2', true)
const peers = Set.of(peer1, peer2)

peer1.on('signal', (signal) => peer2.signal(signal))
peer2.on('signal', (signal) => peer1.signal(signal))
peer1.on('signal', (signal) => { peer2.signal(signal) })
peer2.on('signal', (signal) => { peer1.signal(signal) })

await Promise.all(peers.map(async (peer) =>
await new Promise<void>((resolve) => peer.on('connect', resolve))
await Promise.all(peers.map(async (peer) => { await new Promise<void>((resolve) => { peer.on('connect', resolve) }) }
).toArray())
})

Expand All @@ -29,8 +28,7 @@ describe('peer', function () {
const message = 'small message'

peer1.send(Buffer.from(message))
const received = await new Promise((resolve) =>
peer2.on('data', (msg) => resolve(msg.toString())))
const received = await new Promise((resolve) => { peer2.on('data', (msg) => { resolve(msg.toString()) }) })

assert.strictEqual(received, message)
})
Expand All @@ -42,7 +40,7 @@ describe('peer', function () {

messages
.map((m) => Buffer.from(m))
.forEach((m) => peer1.send(m))
.forEach((m) => { peer1.send(m) })

const receiveds: List<string> = await new Promise((resolve) => {
let buffer = List<string>()
Expand Down
15 changes: 10 additions & 5 deletions discojs/discojs-core/src/client/decentralized/peer.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { NodeID } from '../types'
import { type NodeID } from '../types'

import { List, Map, Range, Seq } from 'immutable'
import wrtc from 'isomorphic-wrtc'
import SimplePeer, { SignalData } from 'simple-peer'
import SimplePeer from 'simple-peer'

type MessageID = number
type ChunkID = number
Expand All @@ -16,6 +16,11 @@ const HEADER_SIZE = 2 + 1
// at which interval to poll
const TICK = 10

// we can't use the definition in DOM as we're platform independent
export type SignalData =
| { type: 'answer' | 'offer' | 'pranswer' | 'rollback', sdp?: string }
| { type: 'transceiverRequest' | 'renegotiate' | 'candidate' }

interface Events {
'close': () => void
'connect': () => void
Expand Down Expand Up @@ -72,7 +77,7 @@ export class Peer {

const remainingBufferSize = this.bufferSize - this.peer.bufferSize
if (chunk.length > remainingBufferSize) {
setTimeout(() => this.flush(), TICK)
setTimeout(() => { this.flush() }, TICK)
return
}

Expand Down Expand Up @@ -150,7 +155,7 @@ export class Peer {
this.peer.destroy()
}

signal (signal: SimplePeer.SignalData): void {
signal (signal: SignalData): void {
// extract max buffer size
if (signal.type === 'offer' || signal.type === 'answer') {
if (signal.sdp === undefined) {
Expand Down Expand Up @@ -236,7 +241,7 @@ export class Peer {

readyMessages
.forEach((message) => {
// TODO debug
// @ts-expect-error listener is of type 'data', not also 'signal'
listener(message)
})
})
Expand Down
Loading

0 comments on commit a4d06b1

Please sign in to comment.