refactor: use eslint-config-neon for packages. (#8579)

Co-authored-by: Noel <buechler.noel@outlook.com>
This commit is contained in:
Suneet Tipirneni
2022-09-01 14:50:16 -04:00
committed by GitHub
parent 4bdb0593ae
commit edadb9fe5d
219 changed files with 2608 additions and 2053 deletions

View File

@@ -5,7 +5,7 @@ import type { SessionInfo, WebSocketManager, WebSocketManagerOptions } from '../
export interface FetchingStrategyOptions
extends Omit<
WebSocketManagerOptions,
'retrieveSessionInfo' | 'updateSessionInfo' | 'shardCount' | 'shardIds' | 'rest'
'rest' | 'retrieveSessionInfo' | 'shardCount' | 'shardIds' | 'updateSessionInfo'
> {
readonly gatewayInformation: APIGatewayBotInfo;
readonly shardCount: number;
@@ -16,11 +16,12 @@ export interface FetchingStrategyOptions
*/
export interface IContextFetchingStrategy {
readonly options: FetchingStrategyOptions;
retrieveSessionInfo: (shardId: number) => Awaitable<SessionInfo | null>;
updateSessionInfo: (shardId: number, sessionInfo: SessionInfo | null) => Awaitable<void>;
retrieveSessionInfo(shardId: number): Awaitable<SessionInfo | null>;
updateSessionInfo(shardId: number, sessionInfo: SessionInfo | null): Awaitable<void>;
}
export async function managerToFetchingStrategyOptions(manager: WebSocketManager): Promise<FetchingStrategyOptions> {
// eslint-disable-next-line @typescript-eslint/unbound-method
const { retrieveSessionInfo, updateSessionInfo, shardCount, shardIds, rest, ...managerOptions } = manager.options;
return {

View File

@@ -1,5 +1,5 @@
import type { FetchingStrategyOptions, IContextFetchingStrategy } from './IContextFetchingStrategy';
import type { SessionInfo, WebSocketManager } from '../../ws/WebSocketManager';
import type { SessionInfo, WebSocketManager } from '../../ws/WebSocketManager.js';
import type { FetchingStrategyOptions, IContextFetchingStrategy } from './IContextFetchingStrategy.js';
export class SimpleContextFetchingStrategy implements IContextFetchingStrategy {
public constructor(private readonly manager: WebSocketManager, public readonly options: FetchingStrategyOptions) {}

View File

@@ -1,13 +1,13 @@
import { isMainThread, parentPort } from 'node:worker_threads';
import { Collection } from '@discordjs/collection';
import type { FetchingStrategyOptions, IContextFetchingStrategy } from './IContextFetchingStrategy';
import type { SessionInfo } from '../../ws/WebSocketManager';
import type { SessionInfo } from '../../ws/WebSocketManager.js';
import {
WorkerRecievePayload,
WorkerRecievePayloadOp,
WorkerSendPayload,
WorkerSendPayloadOp,
} from '../sharding/WorkerShardingStrategy';
type WorkerRecievePayload,
type WorkerSendPayload,
} from '../sharding/WorkerShardingStrategy.js';
import type { FetchingStrategyOptions, IContextFetchingStrategy } from './IContextFetchingStrategy.js';
export class WorkerContextFetchingStrategy implements IContextFetchingStrategy {
private readonly sessionPromises = new Collection<number, (session: SessionInfo | null) => void>();
@@ -33,7 +33,9 @@ export class WorkerContextFetchingStrategy implements IContextFetchingStrategy {
shardId,
nonce,
};
// eslint-disable-next-line no-promise-executor-return
const promise = new Promise<SessionInfo | null>((resolve) => this.sessionPromises.set(nonce, resolve));
// eslint-disable-next-line unicorn/require-post-message-target-origin
parentPort!.postMessage(payload);
return promise;
}
@@ -44,6 +46,7 @@ export class WorkerContextFetchingStrategy implements IContextFetchingStrategy {
shardId,
session: sessionInfo,
};
// eslint-disable-next-line unicorn/require-post-message-target-origin
parentPort!.postMessage(payload);
}
}

View File

@@ -6,20 +6,20 @@ import type { WebSocketShardDestroyOptions } from '../../ws/WebSocketShard';
* Strategies responsible for spawning, initializing connections, destroying shards, and relaying events
*/
export interface IShardingStrategy {
/**
* Spawns all the shards
*/
spawn: (shardIds: number[]) => Awaitable<void>;
/**
* Initializes all the shards
*/
connect: () => Awaitable<void>;
connect(): Awaitable<void>;
/**
* Destroys all the shards
*/
destroy: (options?: Omit<WebSocketShardDestroyOptions, 'recover'>) => Awaitable<void>;
destroy(options?: Omit<WebSocketShardDestroyOptions, 'recover'>): Awaitable<void>;
/**
* Sends a payload to a shard
*/
send: (shardId: number, payload: GatewaySendPayload) => Awaitable<void>;
send(shardId: number, payload: GatewaySendPayload): Awaitable<void>;
/**
* Spawns all the shards
*/
spawn(shardIds: number[]): Awaitable<void>;
}

View File

@@ -1,17 +1,18 @@
import { Collection } from '@discordjs/collection';
import type { GatewaySendPayload } from 'discord-api-types/v10';
import type { IShardingStrategy } from './IShardingStrategy';
import { IdentifyThrottler } from '../../utils/IdentifyThrottler';
import { IdentifyThrottler } from '../../utils/IdentifyThrottler.js';
import type { WebSocketManager } from '../../ws/WebSocketManager';
import { WebSocketShard, WebSocketShardDestroyOptions, WebSocketShardEvents } from '../../ws/WebSocketShard';
import { managerToFetchingStrategyOptions } from '../context/IContextFetchingStrategy';
import { SimpleContextFetchingStrategy } from '../context/SimpleContextFetchingStrategy';
import { WebSocketShard, WebSocketShardEvents, type WebSocketShardDestroyOptions } from '../../ws/WebSocketShard.js';
import { managerToFetchingStrategyOptions } from '../context/IContextFetchingStrategy.js';
import { SimpleContextFetchingStrategy } from '../context/SimpleContextFetchingStrategy.js';
import type { IShardingStrategy } from './IShardingStrategy.js';
/**
* Simple strategy that just spawns shards in the current process
*/
export class SimpleShardingStrategy implements IShardingStrategy {
private readonly manager: WebSocketManager;
private readonly shards = new Collection<number, WebSocketShard>();
private readonly throttler: IdentifyThrottler;
@@ -30,9 +31,10 @@ export class SimpleShardingStrategy implements IShardingStrategy {
const strategy = new SimpleContextFetchingStrategy(this.manager, strategyOptions);
const shard = new WebSocketShard(strategy, shardId);
for (const event of Object.values(WebSocketShardEvents)) {
// @ts-expect-error
// @ts-expect-error: Intentional
shard.on(event, (payload) => this.manager.emit(event, { ...payload, shardId }));
}
this.shards.set(shardId, shard);
}
}
@@ -68,7 +70,7 @@ export class SimpleShardingStrategy implements IShardingStrategy {
/**
* {@inheritDoc IShardingStrategy.send}
*/
public send(shardId: number, payload: GatewaySendPayload) {
public async send(shardId: number, payload: GatewaySendPayload) {
const shard = this.shards.get(shardId);
if (!shard) throw new Error(`Shard ${shardId} not found`);
return shard.send(payload);

View File

@@ -3,11 +3,11 @@ import { join } from 'node:path';
import { Worker } from 'node:worker_threads';
import { Collection } from '@discordjs/collection';
import type { GatewaySendPayload } from 'discord-api-types/v10';
import type { IShardingStrategy } from './IShardingStrategy';
import { IdentifyThrottler } from '../../utils/IdentifyThrottler';
import { IdentifyThrottler } from '../../utils/IdentifyThrottler.js';
import type { SessionInfo, WebSocketManager } from '../../ws/WebSocketManager';
import type { WebSocketShardDestroyOptions, WebSocketShardEvents } from '../../ws/WebSocketShard';
import { FetchingStrategyOptions, managerToFetchingStrategyOptions } from '../context/IContextFetchingStrategy';
import { managerToFetchingStrategyOptions, type FetchingStrategyOptions } from '../context/IContextFetchingStrategy.js';
import type { IShardingStrategy } from './IShardingStrategy.js';
export interface WorkerData extends FetchingStrategyOptions {
shardIds: number[];
@@ -21,10 +21,10 @@ export enum WorkerSendPayloadOp {
}
export type WorkerSendPayload =
| { nonce: number; op: WorkerSendPayloadOp.SessionInfoResponse; session: SessionInfo | null }
| { op: WorkerSendPayloadOp.Connect; shardId: number }
| { op: WorkerSendPayloadOp.Destroy; shardId: number; options?: WebSocketShardDestroyOptions }
| { op: WorkerSendPayloadOp.Send; shardId: number; payload: GatewaySendPayload }
| { op: WorkerSendPayloadOp.SessionInfoResponse; nonce: number; session: SessionInfo | null };
| { op: WorkerSendPayloadOp.Destroy; options?: WebSocketShardDestroyOptions; shardId: number }
| { op: WorkerSendPayloadOp.Send; payload: GatewaySendPayload; shardId: number };
export enum WorkerRecievePayloadOp {
Connected,
@@ -35,12 +35,12 @@ export enum WorkerRecievePayloadOp {
}
export type WorkerRecievePayload =
// Can't seem to get a type-safe union based off of the event, so I'm sadly leaving data as any for now
| { data: any; event: WebSocketShardEvents; op: WorkerRecievePayloadOp.Event; shardId: number }
| { nonce: number; op: WorkerRecievePayloadOp.RetrieveSessionInfo; shardId: number }
| { op: WorkerRecievePayloadOp.Connected; shardId: number }
| { op: WorkerRecievePayloadOp.Destroyed; shardId: number }
// Can't seem to get a type-safe union based off of the event, so I'm sadly leaving data as any for now
| { op: WorkerRecievePayloadOp.Event; shardId: number; event: WebSocketShardEvents; data: any }
| { op: WorkerRecievePayloadOp.RetrieveSessionInfo; shardId: number; nonce: number }
| { op: WorkerRecievePayloadOp.UpdateSessionInfo; shardId: number; session: SessionInfo | null };
| { op: WorkerRecievePayloadOp.UpdateSessionInfo; session: SessionInfo | null; shardId: number };
/**
* Options for a {@link WorkerShardingStrategy}
@@ -57,12 +57,15 @@ export interface WorkerShardingStrategyOptions {
*/
export class WorkerShardingStrategy implements IShardingStrategy {
private readonly manager: WebSocketManager;
private readonly options: WorkerShardingStrategyOptions;
#workers: Worker[] = [];
readonly #workerByShardId = new Collection<number, Worker>();
private readonly connectPromises = new Collection<number, () => void>();
private readonly destroyPromises = new Collection<number, () => void>();
private readonly throttler: IdentifyThrottler;
@@ -98,7 +101,7 @@ export class WorkerShardingStrategy implements IShardingStrategy {
throw err;
})
// eslint-disable-next-line @typescript-eslint/no-misused-promises
.on('message', (payload: WorkerRecievePayload) => this.onMessage(worker, payload));
.on('message', async (payload: WorkerRecievePayload) => this.onMessage(worker, payload));
this.#workers.push(worker);
for (const shardId of slice) {
@@ -123,7 +126,9 @@ export class WorkerShardingStrategy implements IShardingStrategy {
shardId,
};
// eslint-disable-next-line no-promise-executor-return
const promise = new Promise<void>((resolve) => this.connectPromises.set(shardId, resolve));
// eslint-disable-next-line unicorn/require-post-message-target-origin
worker.postMessage(payload);
promises.push(promise);
}
@@ -145,8 +150,10 @@ export class WorkerShardingStrategy implements IShardingStrategy {
};
promises.push(
new Promise<void>((resolve) => this.destroyPromises.set(shardId, resolve)).then(() => worker.terminate()),
// eslint-disable-next-line no-promise-executor-return, promise/prefer-await-to-then
new Promise<void>((resolve) => this.destroyPromises.set(shardId, resolve)).then(async () => worker.terminate()),
);
// eslint-disable-next-line unicorn/require-post-message-target-origin
worker.postMessage(payload);
}
@@ -170,10 +177,12 @@ export class WorkerShardingStrategy implements IShardingStrategy {
shardId,
payload: data,
};
// eslint-disable-next-line unicorn/require-post-message-target-origin
worker.postMessage(payload);
}
private async onMessage(worker: Worker, payload: WorkerRecievePayload) {
// eslint-disable-next-line default-case
switch (payload.op) {
case WorkerRecievePayloadOp.Connected: {
const resolve = this.connectPromises.get(payload.shardId)!;
@@ -202,6 +211,7 @@ export class WorkerShardingStrategy implements IShardingStrategy {
nonce: payload.nonce,
session,
};
// eslint-disable-next-line unicorn/require-post-message-target-origin
worker.postMessage(response);
break;
}

View File

@@ -1,14 +1,15 @@
/* eslint-disable unicorn/require-post-message-target-origin */
import { isMainThread, workerData, parentPort } from 'node:worker_threads';
import { Collection } from '@discordjs/collection';
import { WebSocketShard, WebSocketShardEvents, type WebSocketShardDestroyOptions } from '../../ws/WebSocketShard.js';
import { WorkerContextFetchingStrategy } from '../context/WorkerContextFetchingStrategy.js';
import {
WorkerData,
WorkerRecievePayload,
WorkerRecievePayloadOp,
WorkerSendPayload,
WorkerSendPayloadOp,
} from './WorkerShardingStrategy';
import { WebSocketShard, WebSocketShardDestroyOptions, WebSocketShardEvents } from '../../ws/WebSocketShard';
import { WorkerContextFetchingStrategy } from '../context/WorkerContextFetchingStrategy';
type WorkerData,
type WorkerRecievePayload,
type WorkerSendPayload,
} from './WorkerShardingStrategy.js';
if (isMainThread) {
throw new Error('Expected worker script to not be ran within the main thread');
@@ -22,6 +23,7 @@ async function connect(shardId: number) {
if (!shard) {
throw new Error(`Shard ${shardId} does not exist`);
}
await shard.connect();
}
@@ -30,13 +32,14 @@ async function destroy(shardId: number, options?: WebSocketShardDestroyOptions)
if (!shard) {
throw new Error(`Shard ${shardId} does not exist`);
}
await shard.destroy(options);
}
for (const shardId of data.shardIds) {
const shard = new WebSocketShard(new WorkerContextFetchingStrategy(data), shardId);
for (const event of Object.values(WebSocketShardEvents)) {
// @ts-expect-error
// @ts-expect-error: Event types incompatible
shard.on(event, (data) => {
const payload: WorkerRecievePayload = {
op: WorkerRecievePayloadOp.Event,
@@ -47,6 +50,7 @@ for (const shardId of data.shardIds) {
parentPort!.postMessage(payload);
});
}
shards.set(shardId, shard);
}
@@ -56,6 +60,7 @@ parentPort!
})
// eslint-disable-next-line @typescript-eslint/no-misused-promises
.on('message', async (payload: WorkerSendPayload) => {
// eslint-disable-next-line default-case
switch (payload.op) {
case WorkerSendPayloadOp.Connect: {
await connect(payload.shardId);
@@ -73,6 +78,7 @@ parentPort!
op: WorkerRecievePayloadOp.Destroyed,
shardId: payload.shardId,
};
parentPort!.postMessage(response);
break;
}
@@ -82,6 +88,7 @@ parentPort!
if (!shard) {
throw new Error(`Shard ${payload.shardId} does not exist`);
}
await shard.send(payload.payload);
break;
}