mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 16:13:31 +01:00
fix: ws typo (#9056)
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
This commit is contained in:
@@ -7,8 +7,8 @@ import {
|
||||
WorkerContextFetchingStrategy,
|
||||
WebSocketManager,
|
||||
WorkerSendPayloadOp,
|
||||
WorkerRecievePayloadOp,
|
||||
type WorkerRecievePayload,
|
||||
WorkerReceivePayloadOp,
|
||||
type WorkerReceivePayload,
|
||||
type WorkerSendPayload,
|
||||
} from '../../src/index.js';
|
||||
|
||||
@@ -31,8 +31,8 @@ const session = {
|
||||
vi.mock('node:worker_threads', async () => {
|
||||
const { EventEmitter }: typeof import('node:events') = await vi.importActual('node:events');
|
||||
class MockParentPort extends EventEmitter {
|
||||
public postMessage(message: WorkerRecievePayload) {
|
||||
if (message.op === WorkerRecievePayloadOp.RetrieveSessionInfo) {
|
||||
public postMessage(message: WorkerReceivePayload) {
|
||||
if (message.op === WorkerReceivePayloadOp.RetrieveSessionInfo) {
|
||||
const response: WorkerSendPayload = {
|
||||
op: WorkerSendPayloadOp.SessionInfoResponse,
|
||||
nonce: message.nonce,
|
||||
|
||||
@@ -12,10 +12,10 @@ import { beforeEach, test, vi, expect, afterEach } from 'vitest';
|
||||
import {
|
||||
WebSocketManager,
|
||||
WorkerSendPayloadOp,
|
||||
WorkerRecievePayloadOp,
|
||||
WorkerReceivePayloadOp,
|
||||
WorkerShardingStrategy,
|
||||
WebSocketShardEvents,
|
||||
type WorkerRecievePayload,
|
||||
type WorkerReceivePayload,
|
||||
type WorkerSendPayload,
|
||||
type SessionInfo,
|
||||
} from '../../src/index.js';
|
||||
@@ -58,8 +58,8 @@ vi.mock('node:worker_threads', async () => {
|
||||
// same deal here
|
||||
setImmediate(() => {
|
||||
const message = {
|
||||
op: WorkerRecievePayloadOp.WorkerReady,
|
||||
} satisfies WorkerRecievePayload;
|
||||
op: WorkerReceivePayloadOp.WorkerReady,
|
||||
} satisfies WorkerReceivePayload;
|
||||
this.emit('message', message);
|
||||
});
|
||||
});
|
||||
@@ -69,18 +69,18 @@ vi.mock('node:worker_threads', async () => {
|
||||
switch (message.op) {
|
||||
case WorkerSendPayloadOp.Connect: {
|
||||
const response = {
|
||||
op: WorkerRecievePayloadOp.Connected,
|
||||
op: WorkerReceivePayloadOp.Connected,
|
||||
shardId: message.shardId,
|
||||
} satisfies WorkerRecievePayload;
|
||||
} satisfies WorkerReceivePayload;
|
||||
this.emit('message', response);
|
||||
break;
|
||||
}
|
||||
|
||||
case WorkerSendPayloadOp.Destroy: {
|
||||
const response = {
|
||||
op: WorkerRecievePayloadOp.Destroyed,
|
||||
op: WorkerReceivePayloadOp.Destroyed,
|
||||
shardId: message.shardId,
|
||||
} satisfies WorkerRecievePayload;
|
||||
} satisfies WorkerReceivePayload;
|
||||
this.emit('message', response);
|
||||
break;
|
||||
}
|
||||
@@ -88,19 +88,19 @@ vi.mock('node:worker_threads', async () => {
|
||||
case WorkerSendPayloadOp.Send: {
|
||||
if (message.payload.op === GatewayOpcodes.RequestGuildMembers) {
|
||||
const response = {
|
||||
op: WorkerRecievePayloadOp.Event,
|
||||
op: WorkerReceivePayloadOp.Event,
|
||||
shardId: message.shardId,
|
||||
event: WebSocketShardEvents.Dispatch,
|
||||
data: memberChunkData,
|
||||
} satisfies WorkerRecievePayload;
|
||||
} satisfies WorkerReceivePayload;
|
||||
this.emit('message', response);
|
||||
|
||||
// Fetch session info
|
||||
const sessionFetch = {
|
||||
op: WorkerRecievePayloadOp.RetrieveSessionInfo,
|
||||
op: WorkerReceivePayloadOp.RetrieveSessionInfo,
|
||||
shardId: message.shardId,
|
||||
nonce: Math.random(),
|
||||
} satisfies WorkerRecievePayload;
|
||||
} satisfies WorkerReceivePayload;
|
||||
this.emit('message', sessionFetch);
|
||||
}
|
||||
|
||||
@@ -112,10 +112,10 @@ vi.mock('node:worker_threads', async () => {
|
||||
message.session ??= sessionInfo;
|
||||
|
||||
const session = {
|
||||
op: WorkerRecievePayloadOp.UpdateSessionInfo,
|
||||
op: WorkerReceivePayloadOp.UpdateSessionInfo,
|
||||
shardId: message.session.shardId,
|
||||
session: { ...message.session, sequence: message.session.sequence + 1 },
|
||||
} satisfies WorkerRecievePayload;
|
||||
} satisfies WorkerReceivePayload;
|
||||
this.emit('message', session);
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -2,9 +2,9 @@ import { isMainThread, parentPort } from 'node:worker_threads';
|
||||
import { Collection } from '@discordjs/collection';
|
||||
import type { SessionInfo } from '../../ws/WebSocketManager.js';
|
||||
import {
|
||||
WorkerRecievePayloadOp,
|
||||
WorkerReceivePayloadOp,
|
||||
WorkerSendPayloadOp,
|
||||
type WorkerRecievePayload,
|
||||
type WorkerReceivePayload,
|
||||
type WorkerSendPayload,
|
||||
} from '../sharding/WorkerShardingStrategy.js';
|
||||
import type { FetchingStrategyOptions, IContextFetchingStrategy } from './IContextFetchingStrategy.js';
|
||||
@@ -35,10 +35,10 @@ export class WorkerContextFetchingStrategy implements IContextFetchingStrategy {
|
||||
public async retrieveSessionInfo(shardId: number): Promise<SessionInfo | null> {
|
||||
const nonce = Math.random();
|
||||
const payload = {
|
||||
op: WorkerRecievePayloadOp.RetrieveSessionInfo,
|
||||
op: WorkerReceivePayloadOp.RetrieveSessionInfo,
|
||||
shardId,
|
||||
nonce,
|
||||
} satisfies WorkerRecievePayload;
|
||||
} satisfies WorkerReceivePayload;
|
||||
// eslint-disable-next-line no-promise-executor-return
|
||||
const promise = new Promise<SessionInfo | null>((resolve) => this.sessionPromises.set(nonce, resolve));
|
||||
parentPort!.postMessage(payload);
|
||||
@@ -47,19 +47,19 @@ export class WorkerContextFetchingStrategy implements IContextFetchingStrategy {
|
||||
|
||||
public updateSessionInfo(shardId: number, sessionInfo: SessionInfo | null) {
|
||||
const payload = {
|
||||
op: WorkerRecievePayloadOp.UpdateSessionInfo,
|
||||
op: WorkerReceivePayloadOp.UpdateSessionInfo,
|
||||
shardId,
|
||||
session: sessionInfo,
|
||||
} satisfies WorkerRecievePayload;
|
||||
} satisfies WorkerReceivePayload;
|
||||
parentPort!.postMessage(payload);
|
||||
}
|
||||
|
||||
public async waitForIdentify(): Promise<void> {
|
||||
const nonce = Math.random();
|
||||
const payload = {
|
||||
op: WorkerRecievePayloadOp.WaitForIdentify,
|
||||
op: WorkerReceivePayloadOp.WaitForIdentify,
|
||||
nonce,
|
||||
} satisfies WorkerRecievePayload;
|
||||
} satisfies WorkerReceivePayload;
|
||||
// eslint-disable-next-line no-promise-executor-return
|
||||
const promise = new Promise<void>((resolve) => this.waitForIdentifyPromises.set(nonce, resolve));
|
||||
parentPort!.postMessage(payload);
|
||||
|
||||
@@ -30,7 +30,7 @@ export type WorkerSendPayload =
|
||||
| { op: WorkerSendPayloadOp.Destroy; options?: WebSocketShardDestroyOptions; shardId: number }
|
||||
| { op: WorkerSendPayloadOp.Send; payload: GatewaySendPayload; shardId: number };
|
||||
|
||||
export enum WorkerRecievePayloadOp {
|
||||
export enum WorkerReceivePayloadOp {
|
||||
Connected,
|
||||
Destroyed,
|
||||
Event,
|
||||
@@ -41,16 +41,16 @@ export enum WorkerRecievePayloadOp {
|
||||
WorkerReady,
|
||||
}
|
||||
|
||||
export type WorkerRecievePayload =
|
||||
export type WorkerReceivePayload =
|
||||
// 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.FetchStatusResponse; status: WebSocketShardStatus }
|
||||
| { nonce: number; op: WorkerRecievePayloadOp.RetrieveSessionInfo; shardId: number }
|
||||
| { nonce: number; op: WorkerRecievePayloadOp.WaitForIdentify }
|
||||
| { op: WorkerRecievePayloadOp.Connected; shardId: number }
|
||||
| { op: WorkerRecievePayloadOp.Destroyed; shardId: number }
|
||||
| { op: WorkerRecievePayloadOp.UpdateSessionInfo; session: SessionInfo | null; shardId: number }
|
||||
| { op: WorkerRecievePayloadOp.WorkerReady };
|
||||
| { data: any; event: WebSocketShardEvents; op: WorkerReceivePayloadOp.Event; shardId: number }
|
||||
| { nonce: number; op: WorkerReceivePayloadOp.FetchStatusResponse; status: WebSocketShardStatus }
|
||||
| { nonce: number; op: WorkerReceivePayloadOp.RetrieveSessionInfo; shardId: number }
|
||||
| { nonce: number; op: WorkerReceivePayloadOp.WaitForIdentify }
|
||||
| { op: WorkerReceivePayloadOp.Connected; shardId: number }
|
||||
| { op: WorkerReceivePayloadOp.Destroyed; shardId: number }
|
||||
| { op: WorkerReceivePayloadOp.UpdateSessionInfo; session: SessionInfo | null; shardId: number }
|
||||
| { op: WorkerReceivePayloadOp.WorkerReady };
|
||||
|
||||
/**
|
||||
* Options for a {@link WorkerShardingStrategy}
|
||||
@@ -218,7 +218,7 @@ export class WorkerShardingStrategy implements IShardingStrategy {
|
||||
.on('messageerror', (err) => {
|
||||
throw err;
|
||||
})
|
||||
.on('message', async (payload: WorkerRecievePayload) => this.onMessage(worker, payload));
|
||||
.on('message', async (payload: WorkerReceivePayload) => this.onMessage(worker, payload));
|
||||
|
||||
this.#workers.push(worker);
|
||||
for (const shardId of workerData.shardIds) {
|
||||
@@ -250,8 +250,8 @@ export class WorkerShardingStrategy implements IShardingStrategy {
|
||||
|
||||
private async waitForWorkerReady(worker: Worker): Promise<void> {
|
||||
return new Promise((resolve) => {
|
||||
const handler = (payload: WorkerRecievePayload) => {
|
||||
if (payload.op === WorkerRecievePayloadOp.WorkerReady) {
|
||||
const handler = (payload: WorkerReceivePayload) => {
|
||||
if (payload.op === WorkerReceivePayloadOp.WorkerReady) {
|
||||
resolve();
|
||||
worker.off('message', handler);
|
||||
}
|
||||
@@ -261,26 +261,26 @@ export class WorkerShardingStrategy implements IShardingStrategy {
|
||||
});
|
||||
}
|
||||
|
||||
private async onMessage(worker: Worker, payload: WorkerRecievePayload) {
|
||||
private async onMessage(worker: Worker, payload: WorkerReceivePayload) {
|
||||
switch (payload.op) {
|
||||
case WorkerRecievePayloadOp.Connected: {
|
||||
case WorkerReceivePayloadOp.Connected: {
|
||||
this.connectPromises.get(payload.shardId)?.();
|
||||
this.connectPromises.delete(payload.shardId);
|
||||
break;
|
||||
}
|
||||
|
||||
case WorkerRecievePayloadOp.Destroyed: {
|
||||
case WorkerReceivePayloadOp.Destroyed: {
|
||||
this.destroyPromises.get(payload.shardId)?.();
|
||||
this.destroyPromises.delete(payload.shardId);
|
||||
break;
|
||||
}
|
||||
|
||||
case WorkerRecievePayloadOp.Event: {
|
||||
case WorkerReceivePayloadOp.Event: {
|
||||
this.manager.emit(payload.event, { ...payload.data, shardId: payload.shardId });
|
||||
break;
|
||||
}
|
||||
|
||||
case WorkerRecievePayloadOp.RetrieveSessionInfo: {
|
||||
case WorkerReceivePayloadOp.RetrieveSessionInfo: {
|
||||
const session = await this.manager.options.retrieveSessionInfo(payload.shardId);
|
||||
const response: WorkerSendPayload = {
|
||||
op: WorkerSendPayloadOp.SessionInfoResponse,
|
||||
@@ -291,12 +291,12 @@ export class WorkerShardingStrategy implements IShardingStrategy {
|
||||
break;
|
||||
}
|
||||
|
||||
case WorkerRecievePayloadOp.UpdateSessionInfo: {
|
||||
case WorkerReceivePayloadOp.UpdateSessionInfo: {
|
||||
await this.manager.options.updateSessionInfo(payload.shardId, payload.session);
|
||||
break;
|
||||
}
|
||||
|
||||
case WorkerRecievePayloadOp.WaitForIdentify: {
|
||||
case WorkerReceivePayloadOp.WaitForIdentify: {
|
||||
await this.throttler.waitForIdentify();
|
||||
const response: WorkerSendPayload = {
|
||||
op: WorkerSendPayloadOp.ShardCanIdentify,
|
||||
@@ -306,13 +306,13 @@ export class WorkerShardingStrategy implements IShardingStrategy {
|
||||
break;
|
||||
}
|
||||
|
||||
case WorkerRecievePayloadOp.FetchStatusResponse: {
|
||||
case WorkerReceivePayloadOp.FetchStatusResponse: {
|
||||
this.fetchStatusPromises.get(payload.nonce)?.(payload.status);
|
||||
this.fetchStatusPromises.delete(payload.nonce);
|
||||
break;
|
||||
}
|
||||
|
||||
case WorkerRecievePayloadOp.WorkerReady: {
|
||||
case WorkerReceivePayloadOp.WorkerReady: {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,10 +3,10 @@ import { Collection } from '@discordjs/collection';
|
||||
import type { Awaitable } from '@discordjs/util';
|
||||
import { WorkerContextFetchingStrategy } from '../strategies/context/WorkerContextFetchingStrategy.js';
|
||||
import {
|
||||
WorkerRecievePayloadOp,
|
||||
WorkerReceivePayloadOp,
|
||||
WorkerSendPayloadOp,
|
||||
type WorkerData,
|
||||
type WorkerRecievePayload,
|
||||
type WorkerReceivePayload,
|
||||
type WorkerSendPayload,
|
||||
} from '../strategies/sharding/WorkerShardingStrategy.js';
|
||||
import type { WebSocketShardDestroyOptions } from '../ws/WebSocketShard.js';
|
||||
@@ -84,8 +84,8 @@ export class WorkerBootstrapper {
|
||||
switch (payload.op) {
|
||||
case WorkerSendPayloadOp.Connect: {
|
||||
await this.connect(payload.shardId);
|
||||
const response: WorkerRecievePayload = {
|
||||
op: WorkerRecievePayloadOp.Connected,
|
||||
const response: WorkerReceivePayload = {
|
||||
op: WorkerReceivePayloadOp.Connected,
|
||||
shardId: payload.shardId,
|
||||
};
|
||||
parentPort!.postMessage(response);
|
||||
@@ -94,8 +94,8 @@ export class WorkerBootstrapper {
|
||||
|
||||
case WorkerSendPayloadOp.Destroy: {
|
||||
await this.destroy(payload.shardId, payload.options);
|
||||
const response: WorkerRecievePayload = {
|
||||
op: WorkerRecievePayloadOp.Destroyed,
|
||||
const response: WorkerReceivePayload = {
|
||||
op: WorkerReceivePayloadOp.Destroyed,
|
||||
shardId: payload.shardId,
|
||||
};
|
||||
|
||||
@@ -128,10 +128,10 @@ export class WorkerBootstrapper {
|
||||
}
|
||||
|
||||
const response = {
|
||||
op: WorkerRecievePayloadOp.FetchStatusResponse,
|
||||
op: WorkerReceivePayloadOp.FetchStatusResponse,
|
||||
status: shard.status,
|
||||
nonce: payload.nonce,
|
||||
} satisfies WorkerRecievePayload;
|
||||
} satisfies WorkerReceivePayload;
|
||||
|
||||
parentPort!.postMessage(response);
|
||||
break;
|
||||
@@ -151,11 +151,11 @@ export class WorkerBootstrapper {
|
||||
// @ts-expect-error: Event types incompatible
|
||||
shard.on(event, (data) => {
|
||||
const payload = {
|
||||
op: WorkerRecievePayloadOp.Event,
|
||||
op: WorkerReceivePayloadOp.Event,
|
||||
event,
|
||||
data,
|
||||
shardId,
|
||||
} satisfies WorkerRecievePayload;
|
||||
} satisfies WorkerReceivePayload;
|
||||
parentPort!.postMessage(payload);
|
||||
});
|
||||
}
|
||||
@@ -169,8 +169,8 @@ export class WorkerBootstrapper {
|
||||
this.setupThreadEvents();
|
||||
|
||||
const message = {
|
||||
op: WorkerRecievePayloadOp.WorkerReady,
|
||||
} satisfies WorkerRecievePayload;
|
||||
op: WorkerReceivePayloadOp.WorkerReady,
|
||||
} satisfies WorkerReceivePayload;
|
||||
parentPort!.postMessage(message);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user