Skip to content

Commit a62cb2c

Browse files
authored
fix(lint): enable @typescript-eslint/no-misused-promises: error (#1250)
* enable no-misused-promise * fix @mainsail/test-runner * fix @mainsail/p2p * fix @mainsail/kernel * fix @mainsail/consensus * fix @mainsail/api-sync * revert p2p * add setTimeoutAsync util * use new helper
1 parent d70a611 commit a62cb2c

14 files changed

Lines changed: 102 additions & 59 deletions

File tree

eslint.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export default [
5757
"@typescript-eslint/no-empty-object": "off",
5858
"@typescript-eslint/no-explicit-any": "error",
5959
"@typescript-eslint/no-floating-promises": "error",
60-
"@typescript-eslint/no-misused-promises": "warn",
60+
"@typescript-eslint/no-misused-promises": "error",
6161
"@typescript-eslint/no-non-null-asserted-optional-chain": "off",
6262
"@typescript-eslint/no-non-null-assertion": "warn",
6363
"@typescript-eslint/no-redundant-type-constituents": "warn",

packages/api-sync/source/listeners/abstract-listener.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Identifiers } from "@mainsail/constants";
77
import { inject, injectable, tagged } from "@mainsail/container";
88
import type { Contracts } from "@mainsail/contracts";
99
import { NotImplemented } from "@mainsail/exceptions";
10+
import { setTimeoutAsync } from "@mainsail/utils";
1011

1112
import { EventListener } from "../contracts.js";
1213

@@ -60,7 +61,7 @@ export abstract class AbstractListener<TEventData, TEntity extends object> imple
6061
} catch (ex) {
6162
this.logger.error(`#syncToDatabaseTransaction failed: ${ex}`);
6263
} finally {
63-
this.#syncTimeout = setTimeout(run, syncInterval);
64+
this.#syncTimeout = setTimeoutAsync(run, syncInterval);
6465
}
6566
};
6667

packages/api-sync/source/tokens/whitelist.ts

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,38 +31,32 @@ export class TokenWhitelist {
3131
@inject(Identifiers.Cryptography.Identity.Address.Factory)
3232
private readonly addressFactory!: Contracts.Crypto.AddressFactory;
3333

34-
#syncInterval?: NodeJS.Timeout;
34+
#syncTimeout?: NodeJS.Timeout;
3535

3636
public async bootstrap(): Promise<void> {
3737
const syncInterval = this.#getTokenWhitelistRefreshIntervalMs();
3838

39-
let running = false;
40-
4139
this.logger.info(`Starting TokenWhitelist using remote: ${this.#getTokenWhitelistRemoteUrl()}`);
4240

43-
this.#syncWhitelist()
44-
.catch((error) => this.logger.error(`#syncWhitelist failed: ${error}`))
45-
.finally(() => {
46-
this.#syncInterval = setInterval(async () => {
47-
if (running) {
48-
return;
49-
}
50-
51-
running = true;
52-
53-
try {
54-
await this.#syncWhitelist();
55-
} catch (ex) {
56-
this.logger.error(`#syncWhitelist failed: ${ex}`);
57-
} finally {
58-
running = false;
59-
}
41+
const run = async () => {
42+
try {
43+
await this.#syncWhitelist();
44+
} catch (ex) {
45+
this.logger.error(`#syncWhitelist failed: ${ex}`);
46+
} finally {
47+
this.#syncTimeout = setTimeout(() => {
48+
void run();
6049
}, syncInterval);
61-
});
50+
}
51+
};
52+
53+
void run();
6254
}
6355

6456
public async dispose(): Promise<void> {
65-
clearInterval(this.#syncInterval);
57+
if (this.#syncTimeout) {
58+
clearTimeout(this.#syncTimeout);
59+
}
6660
}
6761

6862
async #syncWhitelist(): Promise<void> {

packages/consensus/source/scheduler.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Identifiers } from "@mainsail/constants";
22
import { inject, injectable } from "@mainsail/container";
33
import type { Contracts } from "@mainsail/contracts";
4+
import { setTimeoutAsync } from "@mainsail/utils";
45
import dayjs from "dayjs";
56

67
@injectable()
@@ -33,7 +34,7 @@ export class Scheduler implements Contracts.Consensus.Scheduler {
3334

3435
const timeout = Math.max(0, timestamp - dayjs().valueOf());
3536

36-
this.#timeoutStartRound = setTimeout(async () => {
37+
this.#timeoutStartRound = setTimeoutAsync(async () => {
3738
await this.#getConsensus().onTimeoutStartRound();
3839
this.#timeoutStartRound = undefined;
3940
}, timeout);
@@ -46,7 +47,7 @@ export class Scheduler implements Contracts.Consensus.Scheduler {
4647
return false;
4748
}
4849

49-
this.#timeoutPropose = setTimeout(async () => {
50+
this.#timeoutPropose = setTimeoutAsync(async () => {
5051
await this.#getConsensus().onTimeoutPropose(height, round);
5152
this.#timeoutPropose = undefined;
5253
}, this.#getTimeout(round));
@@ -59,7 +60,7 @@ export class Scheduler implements Contracts.Consensus.Scheduler {
5960
return false;
6061
}
6162

62-
this.#timeoutPrevote = setTimeout(async () => {
63+
this.#timeoutPrevote = setTimeoutAsync(async () => {
6364
await this.#getConsensus().onTimeoutPrevote(height, round);
6465
this.#timeoutPrevote = undefined;
6566
}, this.#getTimeout(round));
@@ -72,7 +73,7 @@ export class Scheduler implements Contracts.Consensus.Scheduler {
7273
return false;
7374
}
7475

75-
this.#timeoutPrecommit = setTimeout(async () => {
76+
this.#timeoutPrecommit = setTimeoutAsync(async () => {
7677
await this.#getConsensus().onTimeoutPrecommit(height, round);
7778
this.#timeoutPrecommit = undefined;
7879
}, this.#getTimeout(round));

packages/kernel/source/bootstrap/listen-to-shutdown-signals.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@ export class ListenToShutdownSignals implements Contracts.Kernel.Bootstrapper {
99

1010
public async bootstrap(): Promise<void> {
1111
for (const signal in Enums.Kernel.ShutdownSignal) {
12-
process.on(signal, async (code) => {
13-
await this.app.terminate(signal);
12+
process.on(signal, (_) => {
13+
void this.#onSignal(signal);
1414
});
1515
}
1616
}
17+
18+
async #onSignal(signal: string) {
19+
await this.app.terminate(signal);
20+
}
1721
}

packages/kernel/source/ipc/handler.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,21 @@ export class Handler<T extends object> implements Contracts.Kernel.IPC.Handler<T
1111
}
1212

1313
public handleRequest(): void {
14-
parentPort?.on("message", async (message) => {
15-
if (this.handler[message.method] === undefined) {
16-
throw new Error(`Method ${message.method} is not defined on the handler`);
17-
}
18-
19-
try {
20-
const result = await this.handler[message.method](...message.args);
21-
parentPort?.postMessage({ id: message.id, result });
22-
} catch (error) {
23-
parentPort?.postMessage({ error: error.message, id: message.id });
24-
}
14+
parentPort?.on("message", (message) => {
15+
void this.#onMessage(message);
2516
});
2617
}
18+
19+
async #onMessage(message: { method: string; id: string; args: [] }) {
20+
if (this.handler[message.method] === undefined) {
21+
throw new Error(`Method ${message.method} is not defined on the handler`);
22+
}
23+
24+
try {
25+
const result = await this.handler[message.method](...message.args);
26+
parentPort?.postMessage({ id: message.id, result });
27+
} catch (error) {
28+
parentPort?.postMessage({ error: error.message, id: message.id });
29+
}
30+
}
2731
}

packages/kernel/source/services/config/watcher.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,25 @@ export class Watcher {
99
private readonly app!: Contracts.Kernel.Application;
1010

1111
#watcher!: nsfw.NSFW;
12+
#configFiles = new Set([".env", "validators.json", "peers.json", "plugins.js", "plugins.json"]);
1213

1314
public async boot(): Promise<void> {
14-
const configFiles = new Set([".env", "validators.json", "peers.json", "plugins.js", "plugins.json"]);
15-
16-
this.#watcher = await nsfw(this.app.configPath(), async (events) => {
17-
for (const event of events) {
18-
if (event.action === nsfw.ActionType.MODIFIED && configFiles.has(event.file)) {
19-
await this.app.reboot();
20-
break;
21-
}
22-
}
15+
this.#watcher = await nsfw(this.app.configPath(), (events) => {
16+
void this.#handleEvents(events);
2317
});
2418

2519
await this.#watcher.start();
2620
}
2721

22+
async #handleEvents(events: nsfw.FileChangeEvent[]) {
23+
for (const event of events) {
24+
if (event.action === nsfw.ActionType.MODIFIED && this.#configFiles.has(event.file)) {
25+
await this.app.reboot();
26+
break;
27+
}
28+
}
29+
}
30+
2831
public async dispose(): Promise<void> {
2932
return this.#watcher.stop();
3033
}

packages/kernel/source/services/filesystem/drivers/local.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export class LocalFilesystem implements Contracts.Kernel.Filesystem {
8080
return this.fs
8181
.readdirSync(directory)
8282
.map((item: string) => `${directory}/${item}`)
83-
.filter(async (item: string) => this.fs.lstatSync(item).isFile());
83+
.filter((item: string) => this.fs.lstatSync(item).isFile());
8484
}
8585

8686
public async directories(directory: string): Promise<string[]> {
@@ -89,7 +89,7 @@ export class LocalFilesystem implements Contracts.Kernel.Filesystem {
8989
return this.fs
9090
.readdirSync(directory)
9191
.map((item: string) => `${directory}/${item}`)
92-
.filter(async (item: string) => this.fs.lstatSync(item).isDirectory());
92+
.filter((item: string) => this.fs.lstatSync(item).isDirectory());
9393
}
9494

9595
public async makeDirectory(path: string): Promise<boolean> {

packages/kernel/source/services/queue/drivers/memory.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export class MemoryQueue extends EventEmitter implements Contracts.Kernel.Queue
7777
}
7878

7979
public async later(delay: number, job: Contracts.Kernel.QueueJob): Promise<void> {
80-
setTimeout(() => this.push(job), delay);
80+
setTimeout(() => void this.push(job), delay);
8181
}
8282

8383
public async bulk(jobs: Contracts.Kernel.QueueJob[]): Promise<void> {

packages/p2p/source/service.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ export class Service implements Contracts.P2P.Service {
6464
await this.#checkReceivedMessages();
6565

6666
if (!this.#disposed) {
67-
this.#mainLoopTimeout = setTimeout(() => this.mainLoop(), 2000);
67+
this.#mainLoopTimeout = setTimeout(() => {
68+
void this.mainLoop();
69+
}, 2000);
6870
}
6971
}
7072

@@ -108,7 +110,9 @@ export class Service implements Contracts.P2P.Service {
108110

109111
if (!this.#disposed) {
110112
const nextTimeout = randomNumber(10, 20) * 60 * 1000;
111-
this.#apiNodeCheckLoopTimeout = setTimeout(() => this.#checkApiNodes(), nextTimeout);
113+
this.#apiNodeCheckLoopTimeout = setTimeout(() => {
114+
void this.#checkApiNodes();
115+
}, nextTimeout);
112116
}
113117
}
114118

@@ -127,6 +131,8 @@ export class Service implements Contracts.P2P.Service {
127131

128132
// we use Promise.race to cut loose in case some communicator.ping() does not resolve within the delay
129133
// in that case we want to keep on with our program execution while ping promises can finish in the background
134+
// TODO: revisit
135+
/* eslint-disable @typescript-eslint/no-misused-promises */
130136
await new Promise<void>(async (resolve) => {
131137
let isResolved = false;
132138

@@ -150,6 +156,7 @@ export class Service implements Contracts.P2P.Service {
150156

151157
await delay(pingDelay).finally(resolvesFirst);
152158
});
159+
/* eslint-enable */
153160

154161
if (unresponsivePeers > 0) {
155162
this.logger.debug(`Removed ${pluralize("peer", unresponsivePeers, true)}`, "p2p");

0 commit comments

Comments
 (0)