mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-13 10:03:31 +01:00
feat: add @discordjs/util (#8591)
* feat: add @discordjs/util * fix: builders test * refactor: make rest use lazy for ESM import * chore: make requested changes * Apply suggestions from code review Co-authored-by: Parbez <imranbarbhuiya.fsd@gmail.com> Co-authored-by: A. Román <kyradiscord@gmail.com> * chore: make requested changes and add tests * chore: regen lockfile * test: add type tests * chore: push missing files * chore: make requested changes * chore: update CI stuff * chore: fix lockfile * chore: make requested changes Co-authored-by: Parbez <imranbarbhuiya.fsd@gmail.com> Co-authored-by: A. Román <kyradiscord@gmail.com> Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
This commit is contained in:
@@ -8,7 +8,6 @@ export * from './strategies/sharding/WorkerShardingStrategy.js';
|
||||
|
||||
export * from './utils/constants.js';
|
||||
export * from './utils/IdentifyThrottler.js';
|
||||
export * from './utils/utils.js';
|
||||
|
||||
export * from './ws/WebSocketManager.js';
|
||||
export * from './ws/WebSocketShard.js';
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { Awaitable } from '@vladfrangu/async_event_emitter';
|
||||
import type { Awaitable } from '@discordjs/util';
|
||||
import type { APIGatewayBotInfo } from 'discord-api-types/v10';
|
||||
import type { SessionInfo, WebSocketManager, WebSocketManagerOptions } from '../../ws/WebSocketManager';
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import type { Awaitable } from '@discordjs/util';
|
||||
import type { GatewaySendPayload } from 'discord-api-types/v10';
|
||||
import type { Awaitable } from '../../utils/utils';
|
||||
import type { WebSocketShardDestroyOptions } from '../../ws/WebSocketShard';
|
||||
|
||||
/**
|
||||
|
||||
@@ -2,9 +2,9 @@ import { readFileSync } from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
import process from 'node:process';
|
||||
import { Collection } from '@discordjs/collection';
|
||||
import { lazy } from '@discordjs/util';
|
||||
import { APIVersion, GatewayOpcodes } from 'discord-api-types/v10';
|
||||
import type { OptionalWebSocketManagerOptions, SessionInfo } from '../ws/WebSocketManager.js';
|
||||
import { lazy } from './utils.js';
|
||||
|
||||
/**
|
||||
* Valid encoding types
|
||||
|
||||
@@ -1,23 +0,0 @@
|
||||
import type { ShardRange } from '../ws/WebSocketManager';
|
||||
|
||||
export type Awaitable<T> = Promise<T> | T;
|
||||
|
||||
/**
|
||||
* Yields the numbers in the given range as an array
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* range({ start: 3, end: 5 }); // [3, 4, 5]
|
||||
* ```
|
||||
*/
|
||||
export function range({ start, end }: ShardRange): number[] {
|
||||
return Array.from({ length: end - start + 1 }, (_, index) => index + start);
|
||||
}
|
||||
|
||||
/**
|
||||
* Lazily evaluate a callback, storing its result
|
||||
*/
|
||||
export function lazy<T>(cb: () => T): () => T {
|
||||
let defaultValue: T;
|
||||
return () => (defaultValue ??= cb());
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import type { REST } from '@discordjs/rest';
|
||||
import { range, type Awaitable } from '@discordjs/util';
|
||||
import { AsyncEventEmitter } from '@vladfrangu/async_event_emitter';
|
||||
import {
|
||||
Routes,
|
||||
@@ -12,7 +13,6 @@ import {
|
||||
import type { IShardingStrategy } from '../strategies/sharding/IShardingStrategy';
|
||||
import { SimpleShardingStrategy } from '../strategies/sharding/SimpleShardingStrategy.js';
|
||||
import { DefaultWebSocketManagerOptions, type CompressionMethod, type Encoding } from '../utils/constants.js';
|
||||
import { range, type Awaitable } from '../utils/utils.js';
|
||||
import type { WebSocketShardDestroyOptions, WebSocketShardEventsMap } from './WebSocketShard.js';
|
||||
|
||||
/**
|
||||
@@ -264,11 +264,11 @@ export class WebSocketManager extends AsyncEventEmitter<ManagerShardEventsMap> {
|
||||
if (Array.isArray(this.options.shardIds)) {
|
||||
shardIds = this.options.shardIds;
|
||||
} else {
|
||||
shardIds = range(this.options.shardIds);
|
||||
shardIds = range(this.options.shardIds.start, this.options.shardIds.end);
|
||||
}
|
||||
} else {
|
||||
const data = await this.fetchGatewayInformation();
|
||||
shardIds = range({ start: 0, end: (this.options.shardCount ?? data.shards) - 1 });
|
||||
shardIds = range(0, (this.options.shardCount ?? data.shards) - 1);
|
||||
}
|
||||
|
||||
this.shardIds = shardIds;
|
||||
|
||||
@@ -7,6 +7,7 @@ import { URLSearchParams } from 'node:url';
|
||||
import { TextDecoder } from 'node:util';
|
||||
import { inflate } from 'node:zlib';
|
||||
import { Collection } from '@discordjs/collection';
|
||||
import { lazy } from '@discordjs/util';
|
||||
import { AsyncQueue } from '@sapphire/async-queue';
|
||||
import { AsyncEventEmitter } from '@vladfrangu/async_event_emitter';
|
||||
import {
|
||||
@@ -22,7 +23,6 @@ import { WebSocket, type RawData } from 'ws';
|
||||
import type { Inflate } from 'zlib-sync';
|
||||
import type { IContextFetchingStrategy } from '../strategies/context/IContextFetchingStrategy';
|
||||
import { ImportantGatewayOpcodes } from '../utils/constants.js';
|
||||
import { lazy } from '../utils/utils.js';
|
||||
import type { SessionInfo } from './WebSocketManager.js';
|
||||
|
||||
// eslint-disable-next-line promise/prefer-await-to-then
|
||||
|
||||
Reference in New Issue
Block a user