lunibee 0.3.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/dist/builders/embed.d.ts +1 -0
  2. package/dist/builders/index.js +6 -2
  3. package/dist/builders/index.js.map +4 -4
  4. package/dist/core/index.js +66 -15
  5. package/dist/core/index.js.map +8 -8
  6. package/dist/index.js +2345 -199
  7. package/dist/index.js.map +22 -14
  8. package/dist/managers/index.js +2 -2
  9. package/dist/managers/index.js.map +3 -3
  10. package/dist/rest/errors.d.ts +8 -0
  11. package/dist/rest/index.d.ts +2 -0
  12. package/dist/rest/index.js +65 -14
  13. package/dist/rest/index.js.map +5 -5
  14. package/dist/rest/transport.d.ts +9 -0
  15. package/dist/sharding/bus.d.ts +8 -0
  16. package/dist/sharding/cluster.d.ts +224 -14
  17. package/dist/sharding/context.d.ts +41 -0
  18. package/dist/sharding/gateway-bot.d.ts +20 -0
  19. package/dist/sharding/health.d.ts +94 -0
  20. package/dist/sharding/host.d.ts +70 -0
  21. package/dist/sharding/identify.d.ts +62 -0
  22. package/dist/sharding/index.d.ts +56 -12
  23. package/dist/sharding/index.js +2255 -164
  24. package/dist/sharding/index.js.map +18 -10
  25. package/dist/sharding/protocol.d.ts +47 -0
  26. package/dist/sharding/remote-protocol.d.ts +100 -0
  27. package/dist/sharding/remote.d.ts +118 -0
  28. package/dist/sharding/requests.d.ts +54 -0
  29. package/dist/sharding/worker.d.ts +45 -0
  30. package/dist/structures/index.js +2 -2
  31. package/dist/structures/index.js.map +3 -3
  32. package/dist/types/gateway.d.ts +3 -0
  33. package/dist/types/index.js.map +1 -1
  34. package/dist/ws/index.d.ts +1 -1
  35. package/dist/ws/index.js +2 -2
  36. package/dist/ws/index.js.map +5 -5
  37. package/dist/ws/send-budget.d.ts +1 -1
  38. package/package.json +18 -16
@@ -1,12 +1,23 @@
1
1
  import { type GatewayBotInfo } from "./gateway-bot.js";
2
2
  import { Gateway } from "@lunibee/ws";
3
- export { fetchGatewayBot, type GatewayBotInfo } from "./gateway-bot.js";
3
+ import { type ClusterContext } from "./context.js";
4
+ import type { ShardHealth, WorkerReport } from "./health.js";
5
+ import { ClusterWorker, type IdentifyGate } from "./worker.js";
6
+ export { MAX_GUILDS_PER_SHARD, fetchGatewayBot, fetchGuildCount, shardsForGuilds, type GatewayBotInfo, } from "./gateway-bot.js";
4
7
  export { ShardBus } from "./bus.js";
5
8
  export { BroadcastChannelTransport, IpcTransport, type IpcEndpoint, type ShardBusTransport, } from "./transport.js";
6
9
  export { ShardSupervisor, type SupervisorOptions } from "./supervisor.js";
7
10
  export type { ShardBusErrorHandler, ShardBusOptions, ShardMessage, ShardMessageHandler, ShardReply, } from "./bus.js";
8
11
  export { ClusterManager } from "./cluster.js";
9
- export type { ClusterManagerOptions, ClusterInfo } from "./cluster.js";
12
+ export type { ClusterManagerOptions, ClusterManagerEvents, ClusterInfo, BroadcastResult, RequestOptions, } from "./cluster.js";
13
+ export { allocateShards, assertCompleteAllocation, isClusterWorker, readClusterContext, validateShardIds, type ClusterContext, } from "./context.js";
14
+ export { ClusterWorker, type IdentifyGate } from "./worker.js";
15
+ export { ClusterHost, type ClusterHostEvents, type ClusterHostOptions, } from "./host.js";
16
+ export type { HostStatus, RemoteOptions } from "./remote.js";
17
+ export { RemoteClose, REMOTE_PROTOCOL_VERSION } from "./remote-protocol.js";
18
+ export { IdentifyCoordinator, IDENTIFY_INTERVAL, type IdentifyBudget, type IdentifyCoordinatorOptions, } from "./identify.js";
19
+ export { RequestTracker } from "./requests.js";
20
+ export type { ClusterHealth, ClusterManagerHealth, ClusterState, ShardHealth, ShardStatus, WorkerMetrics, WorkerReport, } from "./health.js";
10
21
  /** Configuration for a sharded Gateway client. */
11
22
  export interface ShardManagerOptions {
12
23
  /** Bot token. */
@@ -15,6 +26,16 @@ export interface ShardManagerOptions {
15
26
  intents: ConstructorParameters<typeof Gateway>[0]["intents"];
16
27
  /** Number of shards. Use `"auto"` to request Discord's recommended count. */
17
28
  shardCount?: number | "auto";
29
+ /**
30
+ * With `shardCount: "auto"`, size the deployment by guilds instead of
31
+ * Discord's recommendation: `ceil(guilds / guildsPerShard)` shards, at
32
+ * least one. 1 to 2500 (Discord's limit); Discord recommends about 1000.
33
+ * Guilds are assigned to shards by their ID, so this sets the average,
34
+ * not an exact split.
35
+ */
36
+ guildsPerShard?: number;
37
+ /** The bot's guild count for `guildsPerShard`, instead of asking Discord for it. */
38
+ guildCount?: number;
18
39
  /** Gateway reconnect behavior. */
19
40
  reconnect?: boolean;
20
41
  /**
@@ -35,14 +56,25 @@ export interface ShardManagerOptions {
35
56
  autoScaleInterval?: number;
36
57
  /** Optional handler invoked when a background auto-scale check fails. Receives the thrown error. */
37
58
  onAutoScaleError?: (error: unknown) => void;
38
- }
39
- /** Health snapshot for one shard. */
40
- export interface ShardHealth {
41
- id: number;
42
- /** Gateway connection state. */
43
- state: Gateway["state"];
44
- /** Last heartbeat round-trip in ms, or -1 before the first ACK. */
45
- ping: number;
59
+ /**
60
+ * The shards this manager runs, out of `shardCount`. Defaults to all of
61
+ * them, or to the cluster's own list inside a worker forked by
62
+ * `ClusterManager`. Must be unique integers in `[0, shardCount)`.
63
+ */
64
+ shardIds?: number[];
65
+ /**
66
+ * The worker contract. Read from the environment inside a worker forked by
67
+ * `ClusterManager`; pass `false` to ignore it, or a context to override it.
68
+ */
69
+ cluster?: ClusterContext | false;
70
+ /**
71
+ * Where to get IDENTIFY slots. Inside a coordinated cluster this is the
72
+ * parent, so the limit holds across all clusters. Pass `false` to pace
73
+ * locally with `spawnDelay` instead.
74
+ */
75
+ identify?: IdentifyGate | false;
76
+ /** The worker to report to. Defaults to this process's `ClusterWorker` inside a cluster. */
77
+ worker?: ClusterWorker | false;
46
78
  }
47
79
  /** What `ShardManager` emits about its shards. */
48
80
  export interface ShardManagerEvents {
@@ -57,6 +89,8 @@ export interface ShardManagerEvents {
57
89
  shardResume: [shardId: number];
58
90
  /** A shard reported an error. */
59
91
  shardError: [shardId: number, error: Error];
92
+ /** Every shard this manager owns is ready. Emitted again after a shard drops and recovers. */
93
+ ready: [];
60
94
  /** A shard reconnected too often too fast and is backing off (see `Gateway` `unstable`). */
61
95
  shardUnstable: [
62
96
  shardId: number,
@@ -80,8 +114,10 @@ export declare class ShardManager {
80
114
  static readonly IDENTIFY_INTERVAL = 5000;
81
115
  /** Active Gateway shards indexed by shard identifier. */
82
116
  readonly shards: Map<number, Gateway>;
83
- /** Number of shards managed by this instance after initialization. */
117
+ /** Total shards in the deployment once initialized, 0 before and after `destroy()`. */
84
118
  get shardCount(): number;
119
+ /** IDs of the shards this manager owns. */
120
+ get shardIds(): number[];
85
121
  /** Creates a shard manager. @param options Sharding configuration. @throws {TypeError} If token or intents are invalid. @throws {RangeError} If shard count is invalid. */
86
122
  constructor(options: ShardManagerOptions);
87
123
  /** Retrieves Discord's recommended shard count. @returns Recommended shard count. @throws {Error} If discovery fails or returns invalid data. */
@@ -90,6 +126,12 @@ export declare class ShardManager {
90
126
  fetchGatewayInfo(): Promise<GatewayBotInfo>;
91
127
  /** Connects all shards sequentially. A destroyed manager is reinitialized before connecting. @returns A promise fulfilled after all shards connect. @throws {Error} If a shard fails to connect. */
92
128
  connect(): Promise<void>;
129
+ /**
130
+ * Takes over more shards of the same deployment (recovery after another
131
+ * cluster gave up) and connects them through the same IDENTIFY path.
132
+ * @throws {RangeError} If an ID is invalid, already owned, or the manager is not initialized.
133
+ */
134
+ addShards(shardIds: readonly number[]): Promise<void>;
93
135
  /** Checks if the recommended shard count has changed and reconnects if so. */
94
136
  checkAutoScale(): Promise<void>;
95
137
  /** Forces a reshard to a new shard count, replacing all active shards. @param count New shard count. */
@@ -109,8 +151,10 @@ export declare class ShardManager {
109
151
  }): boolean;
110
152
  /** Gets a shard by ID. @param id Shard identifier. @returns Gateway instance or undefined. */
111
153
  get(id: number): Gateway | undefined;
112
- /** Returns each shard's connection state and heartbeat latency. */
154
+ /** Returns each shard's connection state, lifecycle status and heartbeat latency. */
113
155
  health(): ShardHealth[];
156
+ /** A health report for the parent: shard health and, on request, process metrics. */
157
+ report(metrics?: boolean): WorkerReport;
114
158
  /** Returns information for all managed shards. @returns Shard information snapshots. */
115
159
  values(): ShardInfo[];
116
160
  }