@bakit/gateway 3.0.2 → 3.0.3
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.
- package/dist/cluster.cjs +1 -25
- package/dist/cluster.mjs +1 -0
- package/dist/index.cjs +1 -647
- package/dist/index.d.cts +172 -167
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +208 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +2 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +6 -6
- package/dist/cluster.js +0 -23
- package/dist/index.d.ts +0 -203
- package/dist/index.js +0 -633
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":["#state","#lastHeartbeatSent","#lastHeartbeatAck","#sessionId","#lastSequence","#strategy","#init","#ws","#missedHeartbeats","#resumeGatewayURL","#inflater","zlibConstants","#onInflate","#onMessage","#onClose","#handlePayload","#decompressBuffer","#textDecoder","#handleDispatch","#startHeartbeat","#cleanup","#getStrategy","#scheduleReconnect","#reconnectTimeout","#heartbeatInterval","#heartbeatTimeout","#bindProcessEvents","#shards","#pendingEvals","#handleIPC","#isValidPayload","#handleDispatchPayload","#handleEvalResponse","#handleEvalRequest","#readyCount","#starting","#spawnShard","#bindShardEvents","#totalShards","#readyCount","#gatewayInfo","#identifyQueue","#spawnCluster","#getShardIdsForCluster","#bindClusterEvents"],"sources":["../src/lib/Shard.ts","../src/lib/cluster/ClusterProcess.ts","../src/lib/cluster/Cluster.ts","../src/lib/ShardingManager.ts"],"sourcesContent":["import EventEmitter from \"node:events\";\nimport { createInflate, type Inflate, constants as zlibConstants } from \"node:zlib\";\nimport { TextDecoder } from \"node:util\";\nimport { randomInt } from \"node:crypto\";\n\nimport WebSocket, { type RawData } from \"ws\";\n\nimport {\n\tGatewayCloseCodes,\n\tGatewayDispatchEvents,\n\tGatewayOpcodes,\n\ttype GatewayDispatchPayload,\n\ttype GatewayReadyDispatchData,\n\ttype GatewayReceivePayload,\n\ttype GatewaySendPayload,\n} from \"discord-api-types/v10\";\n\nconst MIN_HEARTBEAT_INTERVAL = 1_000;\nconst MAX_HEARTBEAT_INTERVAL = 60_000;\nconst SAFE_HEARTBEAT_INTERVAL = 45_000;\n\nexport enum ShardState {\n\tIdle,\n\tConnecting,\n\tReady,\n\tResuming,\n\tDisconnecting,\n\tDisconnected,\n}\n\nexport enum ShardStrategy {\n\tResume,\n\tReconnect,\n\tShutdown,\n}\n\nexport interface ShardOptions {\n\ttotal: number;\n\ttoken: string;\n\tintents: number | bigint;\n\tgateway: {\n\t\tbaseURL: string;\n\t\tversion: number;\n\t};\n}\n\nexport interface ShardEvents {\n\tready: [data: GatewayReadyDispatchData];\n\tdisconnect: [code: number];\n\tresume: [];\n\terror: [error: Error];\n\tdebug: [message: string];\n\traw: [payload: GatewayReceivePayload];\n\tdispatch: [payload: GatewayDispatchPayload];\n\tneedIdentify: [];\n}\n\nexport class Shard extends EventEmitter<ShardEvents> {\n\t#state = ShardState.Idle;\n\t#ws?: WebSocket;\n\t#inflater?: Inflate;\n\t#textDecoder = new TextDecoder();\n\t#decompressBuffer: Buffer[] = [];\n\n\t#sessionId?: string;\n\t#lastSequence?: number;\n\t#resumeGatewayURL?: string;\n\n\t#lastHeartbeatSent = -1;\n\t#lastHeartbeatAck = -1;\n\t#missedHeartbeats = 0;\n\t#heartbeatInterval?: NodeJS.Timeout;\n\t#heartbeatTimeout?: NodeJS.Timeout;\n\t#reconnectTimeout?: NodeJS.Timeout;\n\n\t#strategy?: ShardStrategy;\n\n\tpublic constructor(\n\t\tpublic readonly id: number,\n\t\tpublic readonly options: ShardOptions,\n\t) {\n\t\tsuper();\n\t}\n\n\tpublic get state() {\n\t\treturn this.#state;\n\t}\n\n\tpublic get latency() {\n\t\tif (this.#lastHeartbeatSent === -1 || this.#lastHeartbeatAck === -1) {\n\t\t\treturn -1;\n\t\t}\n\n\t\treturn this.#lastHeartbeatAck - this.#lastHeartbeatSent;\n\t}\n\n\tpublic get resumable(): boolean {\n\t\tconst hasSessionId = this.#sessionId !== undefined;\n\t\tconst hasSequence = this.#lastSequence !== undefined;\n\t\tconst shouldResume = this.#strategy === ShardStrategy.Resume;\n\n\t\treturn shouldResume && hasSequence && hasSessionId;\n\t}\n\n\tpublic async connect(): Promise<void> {\n\t\tif (this.#state !== ShardState.Idle && this.#state !== ShardState.Disconnected) {\n\t\t\tthrow new Error(\"Shard already connecting or connected\");\n\t\t}\n\n\t\treturn new Promise((resolve, reject) => {\n\t\t\tconst cleanup = () => {\n\t\t\t\tthis.off(\"error\", onError);\n\t\t\t\tthis.off(\"ready\", onReady);\n\t\t\t};\n\n\t\t\tconst onReady = () => {\n\t\t\t\tcleanup();\n\t\t\t\tresolve();\n\t\t\t};\n\t\t\tconst onError = (err: Error) => {\n\t\t\t\tcleanup();\n\t\t\t\treject(err);\n\t\t\t};\n\n\t\t\tthis.once(\"ready\", onReady);\n\t\t\tthis.once(\"error\", onError);\n\n\t\t\tthis.#init();\n\t\t});\n\t}\n\n\tpublic disconnect(code: number) {\n\t\treturn new Promise<void>((resolve) => {\n\t\t\tthis.#state = ShardState.Disconnecting;\n\t\t\tthis.#strategy = ShardStrategy.Shutdown;\n\n\t\t\tif (!this.#ws) {\n\t\t\t\tresolve();\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tthis.#ws.once(\"close\", () => {\n\t\t\t\tresolve();\n\t\t\t});\n\n\t\t\tthis.#ws.close(code);\n\t\t});\n\t}\n\n\tpublic resume() {\n\t\tif (!this.resumable) {\n\t\t\treturn;\n\t\t}\n\n\t\tthis.#state = ShardState.Resuming;\n\n\t\tthis.send({\n\t\t\top: GatewayOpcodes.Resume,\n\t\t\td: {\n\t\t\t\ttoken: this.options.token,\n\t\t\t\tsession_id: this.#sessionId!,\n\t\t\t\tseq: this.#lastSequence!,\n\t\t\t},\n\t\t});\n\t}\n\n\tpublic identify() {\n\t\tthis.send({\n\t\t\top: GatewayOpcodes.Identify,\n\t\t\td: {\n\t\t\t\ttoken: this.options.token,\n\t\t\t\tintents: Number(this.options.intents),\n\t\t\t\tproperties: {\n\t\t\t\t\tos: process.platform,\n\t\t\t\t\tbrowser: \"bakit\",\n\t\t\t\t\tdevice: \"bakit\",\n\t\t\t\t},\n\t\t\t\tshard: [this.id, this.options.total],\n\t\t\t},\n\t\t});\n\t}\n\n\tpublic send(payload: GatewaySendPayload) {\n\t\tif (this.#ws?.readyState === WebSocket.OPEN) {\n\t\t\tthis.#ws.send(JSON.stringify(payload));\n\t\t}\n\t}\n\n\tpublic sendHeartbeat() {\n\t\tif (this.#lastHeartbeatSent !== -1 && this.#lastHeartbeatAck < this.#lastHeartbeatSent) {\n\t\t\tthis.#missedHeartbeats++;\n\t\t} else {\n\t\t\tthis.#missedHeartbeats = 0;\n\t\t}\n\n\t\tif (this.#missedHeartbeats >= 2) {\n\t\t\tthis.emit(\"debug\", \"Missed 2 heartbeats, reconnecting\");\n\t\t\tthis.#ws?.terminate();\n\t\t\treturn;\n\t\t}\n\n\t\tthis.send({\n\t\t\top: GatewayOpcodes.Heartbeat,\n\t\t\td: this.#lastSequence ?? null,\n\t\t});\n\n\t\tthis.#lastHeartbeatSent = Date.now();\n\t}\n\n\t#init() {\n\t\tthis.#state = ShardState.Connecting;\n\t\tthis.#strategy ??= ShardStrategy.Reconnect; // Default to fresh connect\n\n\t\tconst url = new URL(\n\t\t\tthis.#strategy === ShardStrategy.Resume && this.#resumeGatewayURL\n\t\t\t\t? this.#resumeGatewayURL\n\t\t\t\t: this.options.gateway.baseURL,\n\t\t);\n\n\t\turl.searchParams.set(\"v\", String(this.options.gateway.version));\n\t\turl.searchParams.set(\"encoding\", \"json\");\n\t\turl.searchParams.set(\"compress\", \"zlib-stream\");\n\n\t\tthis.#ws = new WebSocket(url, { perMessageDeflate: false });\n\t\tthis.#inflater = createInflate({ flush: zlibConstants.Z_SYNC_FLUSH });\n\n\t\tthis.#inflater.on(\"data\", (chunk) => this.#onInflate(chunk));\n\t\tthis.#inflater.on(\"error\", (err) => {\n\t\t\tthis.emit(\"error\", err);\n\t\t\tthis.#ws?.terminate();\n\t\t});\n\n\t\tthis.#ws.on(\"message\", (data) => this.#onMessage(data));\n\t\tthis.#ws.on(\"close\", (code) => this.#onClose(code));\n\t\tthis.#ws.on(\"error\", (err) => this.emit(\"error\", err));\n\t}\n\n\t#onMessage(data: RawData) {\n\t\tif (!this.#inflater) {\n\t\t\t// Non-compressed message\n\t\t\ttry {\n\t\t\t\tconst text = data.toString();\n\t\t\t\tconst payload = JSON.parse(text);\n\n\t\t\t\tthis.#handlePayload(payload);\n\t\t\t} catch (error) {\n\t\t\t\tthis.emit(\"error\", error as Error);\n\t\t\t}\n\t\t\treturn;\n\t\t}\n\n\t\tlet buffer: Buffer;\n\n\t\tif (Buffer.isBuffer(data)) {\n\t\t\tbuffer = data;\n\t\t} else if (Array.isArray(data)) {\n\t\t\tbuffer = Buffer.concat(data);\n\t\t} else if (data instanceof ArrayBuffer) {\n\t\t\tbuffer = Buffer.from(data);\n\t\t} else {\n\t\t\tbuffer = Buffer.from(String(data));\n\t\t}\n\n\t\t// Check for Z_SYNC_FLUSH marker\n\t\tconst hasSyncFlush =\n\t\t\tbuffer.length >= 4 &&\n\t\t\tbuffer[buffer.length - 4] === 0x00 &&\n\t\t\tbuffer[buffer.length - 3] === 0x00 &&\n\t\t\tbuffer[buffer.length - 2] === 0xff &&\n\t\t\tbuffer[buffer.length - 1] === 0xff;\n\n\t\t// Write to inflater\n\t\tthis.#inflater.write(buffer, (writeError) => {\n\t\t\tif (writeError) {\n\t\t\t\tthis.emit(\"error\", writeError);\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif (hasSyncFlush) {\n\t\t\t\t// Force flush to process complete message\n\t\t\t\tthis.#inflater?.flush(zlibConstants.Z_SYNC_FLUSH);\n\t\t\t}\n\t\t});\n\t}\n\n\t#onInflate(chunk: Buffer) {\n\t\tthis.#decompressBuffer.push(chunk);\n\n\t\tconst fullBuffer = Buffer.concat(this.#decompressBuffer);\n\n\t\ttry {\n\t\t\tconst text = this.#textDecoder.decode(fullBuffer);\n\n\t\t\tconst payload = JSON.parse(text);\n\t\t\tthis.#handlePayload(payload);\n\n\t\t\tthis.#decompressBuffer = [];\n\t\t} catch (error) {\n\t\t\tif (error instanceof SyntaxError) {\n\t\t\t\tconst text = this.#textDecoder.decode(fullBuffer);\n\n\t\t\t\tif (text.includes(\"{\") && !isValidJSON(text)) {\n\t\t\t\t\treturn; // Wait for more data\n\t\t\t\t}\n\n\t\t\t\tthis.emit(\"error\", error);\n\t\t\t\tthis.#decompressBuffer = [];\n\t\t\t}\n\t\t}\n\t}\n\n\t#handlePayload(payload: GatewayReceivePayload) {\n\t\tthis.emit(\"raw\", payload);\n\n\t\tswitch (payload.op) {\n\t\t\tcase GatewayOpcodes.Dispatch: {\n\t\t\t\tthis.#handleDispatch(payload);\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tcase GatewayOpcodes.Hello: {\n\t\t\t\tthis.#startHeartbeat(payload.d.heartbeat_interval);\n\n\t\t\t\tif (this.resumable) {\n\t\t\t\t\tthis.resume();\n\t\t\t\t} else {\n\t\t\t\t\tthis.emit(\"needIdentify\");\n\t\t\t\t}\n\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tcase GatewayOpcodes.Heartbeat: {\n\t\t\t\tthis.sendHeartbeat();\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tcase GatewayOpcodes.HeartbeatAck: {\n\t\t\t\tthis.#lastHeartbeatAck = Date.now();\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tcase GatewayOpcodes.InvalidSession: {\n\t\t\t\t// Discord explicitly tells us whether RESUME is allowed\n\t\t\t\tconst shouldResume = payload.d;\n\n\t\t\t\tif (shouldResume) {\n\t\t\t\t\tthis.#strategy = ShardStrategy.Resume;\n\t\t\t\t} else {\n\t\t\t\t\t// Session is dead, must IDENTIFY again\n\t\t\t\t\tthis.#strategy = ShardStrategy.Reconnect;\n\n\t\t\t\t\tthis.#sessionId = undefined;\n\t\t\t\t\tthis.#lastSequence = undefined;\n\t\t\t\t\tthis.#resumeGatewayURL = undefined;\n\t\t\t\t}\n\n\t\t\t\tthis.emit(\"debug\", `Invalid session (resumable=${this.resumable})`);\n\n\t\t\t\tthis.#ws?.terminate();\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tcase GatewayOpcodes.Reconnect: {\n\t\t\t\t// Discord requests a reconnect, but session is still valid to resume\n\t\t\t\tthis.#strategy = ShardStrategy.Resume;\n\n\t\t\t\tthis.emit(\"debug\", \"Reconnecting to gateway\");\n\t\t\t\tthis.#ws?.terminate();\n\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\n\t#handleDispatch(payload: GatewayDispatchPayload) {\n\t\tthis.#lastSequence = payload.s;\n\t\tthis.emit(\"dispatch\", payload);\n\n\t\tswitch (payload.t) {\n\t\t\tcase GatewayDispatchEvents.Ready: {\n\t\t\t\tconst { d: data } = payload;\n\n\t\t\t\tthis.#state = ShardState.Ready;\n\n\t\t\t\tthis.#sessionId = data.session_id;\n\t\t\t\tthis.#resumeGatewayURL = data.resume_gateway_url;\n\n\t\t\t\tthis.emit(\"ready\", data);\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tcase GatewayDispatchEvents.Resumed: {\n\t\t\t\tthis.#state = ShardState.Ready;\n\t\t\t\tthis.#strategy = undefined;\n\n\t\t\t\tthis.emit(\"resume\");\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\n\t#onClose(code: number) {\n\t\tthis.#cleanup();\n\n\t\tthis.#state = ShardState.Disconnected;\n\t\tthis.emit(\"disconnect\", code);\n\n\t\tif (this.#strategy === ShardStrategy.Shutdown) {\n\t\t\tswitch (code) {\n\t\t\t\tcase GatewayCloseCodes.AuthenticationFailed:\n\t\t\t\t\tthis.emit(\"error\", new Error(\"Invalid token provided\"));\n\t\t\t\t\tbreak;\n\t\t\t\tcase GatewayCloseCodes.InvalidIntents:\n\t\t\t\t\tthis.emit(\"error\", new Error(\"Invalid intents provided\"));\n\t\t\t\t\tbreak;\n\t\t\t\tcase GatewayCloseCodes.DisallowedIntents:\n\t\t\t\t\tthis.emit(\"error\", new Error(\"Disallowed intents provided\"));\n\t\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\treturn;\n\t\t} else if (!this.#strategy) {\n\t\t\tthis.#strategy = this.#getStrategy(code);\n\t\t}\n\n\t\tif (this.#strategy === ShardStrategy.Reconnect || this.#strategy === ShardStrategy.Resume) {\n\t\t\tthis.#scheduleReconnect();\n\t\t}\n\t}\n\n\t#getStrategy(code: GatewayCloseCodes | number): ShardStrategy {\n\t\tswitch (code) {\n\t\t\tcase GatewayCloseCodes.AuthenticationFailed:\n\t\t\tcase GatewayCloseCodes.InvalidIntents:\n\t\t\tcase GatewayCloseCodes.DisallowedIntents: {\n\t\t\t\treturn ShardStrategy.Shutdown;\n\t\t\t}\n\n\t\t\tcase GatewayCloseCodes.InvalidSeq:\n\t\t\tcase GatewayCloseCodes.SessionTimedOut: {\n\t\t\t\treturn ShardStrategy.Reconnect;\n\t\t\t}\n\n\t\t\tdefault: {\n\t\t\t\treturn ShardStrategy.Resume;\n\t\t\t}\n\t\t}\n\t}\n\n\t#scheduleReconnect(delay = 1000) {\n\t\tif (this.#reconnectTimeout) {\n\t\t\treturn;\n\t\t}\n\n\t\tthis.#reconnectTimeout = setTimeout(() => {\n\t\t\tthis.#reconnectTimeout = undefined;\n\t\t\tthis.#state = ShardState.Idle;\n\t\t\tthis.#init();\n\t\t}, delay);\n\t}\n\n\t#startHeartbeat(interval: number) {\n\t\tif (this.#heartbeatInterval) {\n\t\t\tclearInterval(this.#heartbeatInterval);\n\t\t\tthis.#heartbeatInterval = undefined;\n\t\t}\n\n\t\tif (interval < MIN_HEARTBEAT_INTERVAL || interval > MAX_HEARTBEAT_INTERVAL) {\n\t\t\tinterval = SAFE_HEARTBEAT_INTERVAL;\n\t\t}\n\n\t\tconst jitter = randomInt(0, 10) / 100;\n\t\tconst firstDelay = Math.floor(interval * jitter);\n\n\t\tthis.emit(\"debug\", `Starting heartbeat (interval=${interval}ms, jitter=${firstDelay}ms)`);\n\n\t\tthis.#heartbeatTimeout = setTimeout(() => {\n\t\t\tthis.sendHeartbeat();\n\t\t\tthis.#heartbeatInterval = setInterval(() => this.sendHeartbeat(), interval);\n\t\t}, firstDelay);\n\t}\n\n\t#cleanup(): void {\n\t\tclearTimeout(this.#reconnectTimeout);\n\t\tclearInterval(this.#heartbeatInterval);\n\t\tclearTimeout(this.#heartbeatTimeout);\n\n\t\tthis.#inflater?.destroy();\n\t\tthis.#inflater = undefined;\n\n\t\tthis.#ws?.removeAllListeners();\n\t\tthis.#ws = undefined;\n\n\t\tthis.#decompressBuffer = [];\n\t\tthis.#missedHeartbeats = 0;\n\t}\n}\n\nfunction isValidJSON(str: string): boolean {\n\ttry {\n\t\tJSON.parse(str);\n\t\treturn true;\n\t} catch {\n\t\treturn false;\n\t}\n}\n","import EventEmitter from \"node:events\";\nimport { fork, type ChildProcess } from \"node:child_process\";\nimport { randomUUID } from \"node:crypto\";\nimport { dirname, resolve } from \"node:path\";\nimport { fileURLToPath } from \"node:url\";\n\nimport type { Cluster, ClusterEvents } from \"./Cluster.js\";\nimport type { GatewaySendPayload } from \"discord-api-types/v10\";\nimport type { ShardingManager } from \"../ShardingManager.js\";\nimport { isCommonJS } from \"@bakit/utils\";\n\nconst EVAL_TIMEOUT = 30_000;\n\nconst __filename = fileURLToPath(import.meta.url);\nconst __dirname = dirname(__filename);\n\nexport type ClusterIPCDispatchPayload<E extends keyof ClusterEvents = keyof ClusterEvents> = {\n\top: \"dispatch\";\n\tt: E;\n\td: ClusterEvents[E];\n};\n\nexport interface ClusterIPCIdentifyPayload {\n\top: \"identify\";\n\td: number;\n}\n\nexport interface ClusterIPCGatewaySendPayload {\n\top: \"send\";\n\td: {\n\t\tshardId?: number;\n\t\tdata: GatewaySendPayload;\n\t};\n}\n\nexport interface ClusterIPCEvalRequestPayload {\n\top: \"eval\";\n\td: {\n\t\tnonce: string;\n\t\tscript: string;\n\t};\n}\n\nexport interface ClusterIPCEvalResponsePayload {\n\top: \"evalResponse\";\n\td: {\n\t\tnonce: string;\n\t\tsuccess: boolean;\n\t\tresult: unknown;\n\t\terror?: string;\n\t};\n}\n\nexport type ClusterIPCPayload =\n\t| ClusterIPCDispatchPayload\n\t| ClusterIPCIdentifyPayload\n\t| ClusterIPCGatewaySendPayload\n\t| ClusterIPCEvalRequestPayload\n\t| ClusterIPCEvalResponsePayload;\n\nfunction isDispatchPayload(payload: ClusterIPCPayload): payload is ClusterIPCDispatchPayload {\n\treturn payload.op === \"dispatch\";\n}\n\nfunction isIdentifyPayload(payload: ClusterIPCPayload): payload is ClusterIPCIdentifyPayload {\n\treturn payload.op === \"identify\";\n}\n\nfunction isSendPayload(payload: ClusterIPCPayload): payload is ClusterIPCGatewaySendPayload {\n\treturn payload.op === \"send\";\n}\n\nfunction isEvalRequestPayload(payload: ClusterIPCPayload): payload is ClusterIPCEvalRequestPayload {\n\treturn payload.op === \"eval\";\n}\n\nfunction isEvalResponsePayload(payload: ClusterIPCPayload): payload is ClusterIPCEvalResponsePayload {\n\treturn payload.op === \"evalResponse\";\n}\n\nexport interface EvalResult<T> {\n\t/**\n\t * Whether the evaluation was successful\n\t */\n\tsuccess: boolean;\n\t/**\n\t * The result of the evaluation if successful\n\t */\n\tdata?: T;\n\t/**\n\t * The error if evaluation failed\n\t */\n\terror?: Error;\n\t/**\n\t * The cluster process that executed the evaluation\n\t */\n\tcluster: ClusterProcess;\n}\n\nexport interface ClusterProcessOptions {\n\tenv?: NodeJS.ProcessEnv;\n\texecArgv?: string[];\n}\n\nexport class ClusterProcess extends EventEmitter<ClusterEvents> {\n\tpublic readonly process: ChildProcess;\n\n\t#pendingEvals = new Map<\n\t\tstring,\n\t\t{\n\t\t\tresolve: (value: EvalResult<unknown>) => void;\n\t\t\treject: (reason: Error) => void;\n\t\t\ttimeout?: NodeJS.Timeout;\n\t\t}\n\t>();\n\t#shards: Set<number> = new Set();\n\n\tpublic constructor(\n\t\tpublic readonly manager: ShardingManager,\n\t\tpublic readonly id: number,\n\t\toptions: ClusterProcessOptions = {},\n\t) {\n\t\tsuper();\n\t\tthis.setMaxListeners(0);\n\n\t\tconst entry = resolve(__dirname, isCommonJS() ? \"cluster.cjs\" : \"cluster.mjs\");\n\n\t\tthis.process = fork(entry, {\n\t\t\tenv: options.env,\n\t\t\texecArgv: options.execArgv,\n\t\t\tstdio: [\"inherit\", \"inherit\", \"inherit\", \"ipc\"],\n\t\t});\n\n\t\tthis.#bindProcessEvents();\n\t}\n\n\tpublic get shards(): Set<number> {\n\t\treturn new Set(this.#shards);\n\t}\n\n\tpublic get killed(): boolean {\n\t\treturn this.process.killed || !this.process.connected;\n\t}\n\n\tpublic kill(signal: NodeJS.Signals = \"SIGTERM\"): void {\n\t\tif (this.killed) return;\n\n\t\t// Clean up pending evals before killing\n\t\tfor (const [nonce, pending] of this.#pendingEvals) {\n\t\t\tclearTimeout(pending.timeout);\n\t\t\tpending.reject(new Error(`Process killed before eval completed (nonce: ${nonce})`));\n\t\t}\n\t\tthis.#pendingEvals.clear();\n\n\t\tthis.process.kill(signal);\n\t}\n\n\tpublic async eval<T, C = unknown>(fn: (cluster: Cluster, ctx: C) => T | Promise<T>, ctx?: C): Promise<EvalResult<T>> {\n\t\tconst nonce = randomUUID();\n\n\t\treturn new Promise((resolve, reject) => {\n\t\t\tconst timeoutId = setTimeout(() => {\n\t\t\t\tthis.#pendingEvals.delete(nonce);\n\t\t\t\treject(new Error(`Eval timed out after ${EVAL_TIMEOUT}ms`));\n\t\t\t}, EVAL_TIMEOUT);\n\n\t\t\tthis.#pendingEvals.set(nonce, {\n\t\t\t\tresolve: resolve as (value: EvalResult<unknown>) => void,\n\t\t\t\treject,\n\t\t\t\ttimeout: timeoutId,\n\t\t\t});\n\n\t\t\tlet context: string;\n\n\t\t\ttry {\n\t\t\t\tcontext = JSON.stringify(ctx ?? null);\n\t\t\t} catch {\n\t\t\t\treject(new Error(\"Eval context is not serializable\"));\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Serialize function to string for IPC transmission\n\t\t\tconst script = `(${fn.toString()})(cluster, ${context})`;\n\n\t\t\tthis.sendIPC({\n\t\t\t\top: \"eval\",\n\t\t\t\td: { nonce, script },\n\t\t\t});\n\t\t});\n\t}\n\n\tpublic send(payload: GatewaySendPayload): void;\n\tpublic send(shardId: number, payload: GatewaySendPayload): void;\n\tpublic send(idOrPayload: number | GatewaySendPayload, payload?: GatewaySendPayload): void {\n\t\tconst hasShardId = typeof idOrPayload === \"number\" && payload !== undefined;\n\t\tconst shardId = hasShardId ? idOrPayload : undefined;\n\t\tconst data = hasShardId ? payload : (idOrPayload as GatewaySendPayload);\n\n\t\tthis.sendIPC({\n\t\t\top: \"send\",\n\t\t\td: { shardId, data },\n\t\t});\n\t}\n\n\tpublic sendIPC(message: ClusterIPCPayload): void {\n\t\tif (!this.process.connected || this.process.killed) {\n\t\t\treturn;\n\t\t}\n\n\t\ttry {\n\t\t\tthis.process.send(message, undefined, undefined, (err) => {\n\t\t\t\tif (err) {\n\t\t\t\t\tthis.emit(\"error\", err);\n\t\t\t\t}\n\t\t\t});\n\t\t} catch (err) {\n\t\t\tthis.emit(\"error\", err as Error);\n\t\t}\n\t}\n\n\tpublic identifyShard(id: number): void {\n\t\tthis.sendIPC({\n\t\t\top: \"identify\",\n\t\t\td: id,\n\t\t});\n\t}\n\n\t#bindProcessEvents(): void {\n\t\tthis.process.on(\"message\", (message) => this.#handleIPC(message));\n\t\tthis.process.on(\"error\", (err) => this.emit(\"error\", err));\n\t\tthis.process.on(\"disconnect\", () => this.emit(\"debug\", \"Process disconnected\"));\n\t\tthis.process.on(\"exit\", (code) => {\n\t\t\t// Clean up pending evals on unexpected exit\n\t\t\tfor (const [nonce, pending] of this.#pendingEvals) {\n\t\t\t\tclearTimeout(pending.timeout);\n\t\t\t\tpending.reject(new Error(`Process exited (code: ${code}) before eval completed (nonce: ${nonce})`));\n\t\t\t}\n\n\t\t\tthis.#pendingEvals.clear();\n\t\t});\n\t}\n\n\t#handleIPC(message: unknown): void {\n\t\tif (!this.#isValidPayload(message)) {\n\t\t\treturn;\n\t\t}\n\n\t\tif (isDispatchPayload(message)) {\n\t\t\tthis.#handleDispatchPayload(message);\n\t\t\treturn;\n\t\t}\n\n\t\tif (isEvalResponsePayload(message)) {\n\t\t\tthis.#handleEvalResponse(message);\n\t\t\treturn;\n\t\t}\n\t}\n\n\t#handleDispatchPayload(payload: ClusterIPCDispatchPayload): void {\n\t\tif (payload.t === \"shardAdd\") {\n\t\t\tthis.#shards.add((payload.d as number[])[0]!);\n\t\t}\n\n\t\tthis.emit(payload.t, ...payload.d);\n\t}\n\n\t#handleEvalResponse(payload: ClusterIPCEvalResponsePayload): void {\n\t\tconst pending = this.#pendingEvals.get(payload.d.nonce);\n\n\t\tif (!pending) {\n\t\t\tthis.emit(\"debug\", `Received eval response for unknown nonce: ${payload.d.nonce}`);\n\t\t\treturn;\n\t\t}\n\n\t\tif (pending.timeout) {\n\t\t\tclearTimeout(pending.timeout);\n\t\t}\n\n\t\tthis.#pendingEvals.delete(payload.d.nonce);\n\n\t\tif (payload.d.success) {\n\t\t\tpending.resolve({\n\t\t\t\tsuccess: true,\n\t\t\t\tdata: payload.d.result,\n\t\t\t\tcluster: this,\n\t\t\t});\n\t\t} else {\n\t\t\tconst error = new Error(payload.d.error ?? \"Unknown eval error\");\n\t\t\tpending.resolve({\n\t\t\t\tsuccess: false,\n\t\t\t\terror,\n\t\t\t\tcluster: this,\n\t\t\t});\n\t\t}\n\t}\n\n\t#isValidPayload(message: unknown): message is ClusterIPCPayload {\n\t\tif (typeof message !== \"object\" || message === null) {\n\t\t\treturn false;\n\t\t}\n\n\t\tconst payload = message as Record<string, unknown>;\n\n\t\treturn (\n\t\t\tpayload[\"op\"] === \"dispatch\" ||\n\t\t\tpayload[\"op\"] === \"identify\" ||\n\t\t\tpayload[\"op\"] === \"send\" ||\n\t\t\tpayload[\"op\"] === \"eval\" ||\n\t\t\tpayload[\"op\"] === \"evalResponse\"\n\t\t);\n\t}\n\n\tpublic static bindProcess(cluster: Cluster): void {\n\t\tconst superEmit = cluster.emit.bind(cluster);\n\n\t\tconst safeSend = (message: ClusterIPCPayload): void => {\n\t\t\tif (!process.connected) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tprocess.send?.(message, undefined, undefined, (err) => {\n\t\t\t\tif (err) {\n\t\t\t\t\tcluster.emit(\"error\", err);\n\t\t\t\t}\n\t\t\t});\n\t\t};\n\n\t\tcluster.emit = function (this: Cluster, eventName, ...args) {\n\t\t\tconst result = superEmit(eventName, ...args);\n\n\t\t\tsafeSend({\n\t\t\t\top: \"dispatch\",\n\t\t\t\tt: eventName as keyof ClusterEvents,\n\t\t\t\td: args,\n\t\t\t});\n\n\t\t\treturn result;\n\t\t};\n\n\t\tconst messageHandler = async (message: ClusterIPCPayload) => {\n\t\t\tif (isIdentifyPayload(message)) {\n\t\t\t\tconst shard = cluster.shards.get(message.d);\n\t\t\t\tshard?.identify();\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif (isSendPayload(message)) {\n\t\t\t\tif (message.d.shardId !== undefined) {\n\t\t\t\t\tcluster.send(message.d.shardId, message.d.data);\n\t\t\t\t} else {\n\t\t\t\t\tcluster.send(message.d.data);\n\t\t\t\t}\n\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif (isEvalRequestPayload(message)) {\n\t\t\t\tawait ClusterProcess.#handleEvalRequest(cluster, message, safeSend);\n\t\t\t\treturn;\n\t\t\t}\n\t\t};\n\n\t\tprocess.on(\"message\", messageHandler);\n\t}\n\n\tstatic async #handleEvalRequest(\n\t\tcluster: Cluster,\n\t\tpayload: ClusterIPCEvalRequestPayload,\n\t\tsafeSend: (msg: ClusterIPCPayload) => void,\n\t): Promise<void> {\n\t\tconst { nonce, script } = payload.d;\n\n\t\tconst executeEval = async (): Promise<unknown> => {\n\t\t\t// Create a sandboxed function from the script string\n\t\t\t// 'cluster' is injected as the parameter name\n\t\t\tconst fn = new Function(\"cluster\", `return ${script}`);\n\t\t\treturn await fn(cluster);\n\t\t};\n\n\t\tlet timeoutId: NodeJS.Timeout | undefined;\n\n\t\ttry {\n\t\t\tconst evalPromise = executeEval();\n\n\t\t\t// Race against execution timeout to prevent infinite hangs\n\t\t\tconst timeoutPromise = new Promise<never>((_, reject) => {\n\t\t\t\ttimeoutId = setTimeout(() => {\n\t\t\t\t\treject(new Error(`Eval execution timed out after ${EVAL_TIMEOUT}ms`));\n\t\t\t\t}, EVAL_TIMEOUT);\n\t\t\t});\n\n\t\t\tconst result = await Promise.race([evalPromise, timeoutPromise]);\n\n\t\t\tif (timeoutId) {\n\t\t\t\tclearTimeout(timeoutId);\n\t\t\t}\n\n\t\t\tsafeSend({\n\t\t\t\top: \"evalResponse\",\n\t\t\t\td: {\n\t\t\t\t\tnonce,\n\t\t\t\t\tsuccess: true,\n\t\t\t\t\tresult,\n\t\t\t\t},\n\t\t\t});\n\t\t} catch (err) {\n\t\t\tif (timeoutId) {\n\t\t\t\tclearTimeout(timeoutId);\n\t\t\t}\n\n\t\t\tsafeSend({\n\t\t\t\top: \"evalResponse\",\n\t\t\t\td: {\n\t\t\t\t\tnonce,\n\t\t\t\t\tsuccess: false,\n\t\t\t\t\tresult: undefined,\n\t\t\t\t\terror: err instanceof Error ? err.message : String(err),\n\t\t\t\t},\n\t\t\t});\n\t\t}\n\t}\n}\n","import EventEmitter from \"node:events\";\nimport { Collection } from \"@discordjs/collection\";\n\nimport { Shard } from \"../Shard.js\";\n\nimport type { GatewayDispatchPayload, GatewayReceivePayload, GatewaySendPayload } from \"discord-api-types/v10\";\n\nexport interface ClusterOptions {\n\ttoken: string;\n\tintents: bigint | number;\n\tshards: number[];\n\ttotal: number;\n\tgateway: {\n\t\tbaseURL: string;\n\t\tversion: number;\n\t};\n}\n\nexport interface ClusterEvents {\n\tshardAdd: [id: number];\n\tshardReady: [id: number];\n\tshardDisconnect: [id: number, code: number];\n\tshardResume: [id: number];\n\tshardError: [id: number, error: Error];\n\tneedIdentify: [id: number];\n\n\tdebug: [message: string];\n\n\tdispatch: [shardId: number, payload: GatewayDispatchPayload];\n\traw: [shardId: number, payload: GatewayReceivePayload];\n\n\tready: [];\n\terror: [error: Error];\n}\n\nexport class Cluster extends EventEmitter<ClusterEvents> {\n\tpublic readonly shards = new Collection<number, Shard>();\n\n\t#readyCount = 0;\n\t#starting = false;\n\n\tpublic constructor(\n\t\tpublic readonly id: number,\n\t\tpublic readonly options: ClusterOptions,\n\t) {\n\t\tsuper();\n\t}\n\n\tpublic get size() {\n\t\treturn this.shards.size;\n\t}\n\n\tpublic get ready() {\n\t\treturn this.#readyCount === this.options.shards.length;\n\t}\n\n\tpublic async spawn(): Promise<void> {\n\t\tif (this.#starting) {\n\t\t\treturn;\n\t\t}\n\n\t\tthis.#starting = true;\n\n\t\tthis.emit(\"debug\", `Spawning ${this.options.shards.length} shards...`);\n\n\t\tfor (const i of this.options.shards) {\n\t\t\tawait this.#spawnShard(i);\n\t\t}\n\n\t\tthis.emit(\"debug\", \"All shards spawned\");\n\t}\n\n\tpublic async shutdown(code = 1000) {\n\t\tthis.emit(\"debug\", \"Shutting down cluster...\");\n\n\t\tconst tasks: Promise<void>[] = [];\n\n\t\tfor (const shard of this.shards.values()) {\n\t\t\ttasks.push(shard.disconnect(code));\n\t\t}\n\n\t\tawait Promise.allSettled(tasks);\n\n\t\tthis.emit(\"debug\", \"Cluster shutdown complete\");\n\t}\n\n\tpublic broadcast(fn: (shard: Shard) => void) {\n\t\tfor (const shard of this.shards.values()) {\n\t\t\tfn(shard);\n\t\t}\n\t}\n\n\tpublic send(payload: GatewaySendPayload): void;\n\tpublic send(shardId: number, payload: GatewaySendPayload): void;\n\tpublic send(idOrPayload: number | GatewaySendPayload, payload?: GatewaySendPayload): void {\n\t\tconst hasId = typeof idOrPayload === \"number\" && payload !== undefined;\n\n\t\tconst shardId: number | undefined = hasId ? idOrPayload : undefined;\n\t\tconst data: GatewaySendPayload = hasId ? payload : (idOrPayload as GatewaySendPayload);\n\n\t\tif (shardId !== undefined) {\n\t\t\tthis.shards.get(shardId)?.send(data);\n\t\t} else {\n\t\t\tthis.broadcast((shard) => shard.send(data));\n\t\t}\n\t}\n\n\tasync #spawnShard(id: number) {\n\t\tconst shard = new Shard(id, {\n\t\t\ttoken: this.options.token,\n\t\t\tintents: this.options.intents,\n\t\t\ttotal: this.options.total,\n\t\t\tgateway: this.options.gateway,\n\t\t});\n\n\t\tthis.#bindShardEvents(shard);\n\n\t\tthis.shards.set(id, shard);\n\t\tthis.emit(\"shardAdd\", id);\n\n\t\tawait shard.connect();\n\t}\n\n\t#bindShardEvents(shard: Shard) {\n\t\tconst id = shard.id;\n\n\t\tshard.on(\"ready\", () => {\n\t\t\tthis.#readyCount++;\n\n\t\t\tthis.emit(\"debug\", `Shard ${id} ready`);\n\n\t\t\tthis.emit(\"shardReady\", id);\n\n\t\t\tif (this.ready) {\n\t\t\t\tthis.emit(\"ready\");\n\t\t\t}\n\t\t});\n\n\t\tshard.on(\"resume\", () => {\n\t\t\tthis.emit(\"debug\", `Shard ${id} resumed`);\n\t\t\tthis.emit(\"shardResume\", id);\n\t\t});\n\n\t\tshard.on(\"disconnect\", (code) => {\n\t\t\tthis.emit(\"debug\", `Shard ${id} disconnected (${code})`);\n\n\t\t\tthis.emit(\"shardDisconnect\", id, code);\n\t\t});\n\n\t\tshard.on(\"error\", (err) => {\n\t\t\tthis.emit(\"debug\", `Shard ${id} error: ${err.message}`);\n\t\t\tthis.emit(\"shardError\", id, err);\n\t\t});\n\n\t\tshard.on(\"raw\", (payload) => {\n\t\t\tthis.emit(\"raw\", id, payload);\n\t\t});\n\n\t\tshard.on(\"dispatch\", (payload) => {\n\t\t\tthis.emit(\"dispatch\", id, payload);\n\t\t});\n\n\t\tshard.on(\"needIdentify\", () => {\n\t\t\tthis.emit(\"needIdentify\", id);\n\t\t});\n\n\t\tshard.on(\"debug\", (msg) => {\n\t\t\tthis.emit(\"debug\", `[Shard ${id}] ${msg}`);\n\t\t});\n\t}\n}\n","import EventEmitter from \"node:events\";\n\nimport { Collection } from \"@discordjs/collection\";\nimport PQueue from \"p-queue\";\n\nimport { REST, type RESTLike } from \"@bakit/rest\";\n\nimport { ClusterProcess, type EvalResult } from \"./cluster/ClusterProcess.js\";\n\nimport type { Cluster } from \"./cluster/Cluster.js\";\nimport type { GatewayDispatchPayload, GatewayReceivePayload, GatewaySendPayload } from \"discord-api-types/v10\";\nimport type { APIGatewayBotInfo } from \"discord-api-types/v10\";\n\nexport interface ShardingManagerOptions {\n\ttoken: string;\n\tintents: bigint | number;\n\ttotalShards?: number | \"auto\";\n\tshardsPerCluster?: number;\n}\n\nexport interface ShardingManagerEvents {\n\tshardAdd: [cluster: ClusterProcess, shardId: number];\n\tshardReady: [cluster: ClusterProcess, shardId: number];\n\tshardDisconnect: [cluster: ClusterProcess, shardId: number, code: number];\n\tshardResume: [cluster: ClusterProcess, shardId: number];\n\tshardError: [cluster: ClusterProcess, shardId: number, error: Error];\n\n\tclusterCreate: [cluster: ClusterProcess];\n\tclusterReady: [cluster: ClusterProcess];\n\tclusterExit: [cluster: ClusterProcess, code: number | null];\n\tclusterError: [cluster: ClusterProcess, error: Error];\n\n\tdispatch: [cluster: ClusterProcess, shardId: number, payload: GatewayDispatchPayload];\n\traw: [cluster: ClusterProcess, shardId: number, payload: GatewayReceivePayload];\n\n\tdebug: [message: string];\n\tready: [];\n}\n\nexport class ShardingManager extends EventEmitter<ShardingManagerEvents> {\n\tpublic readonly clusters = new Collection<number, ClusterProcess>();\n\tpublic readonly options: Required<ShardingManagerOptions>;\n\tpublic readonly rest: RESTLike;\n\n\t#gatewayInfo?: APIGatewayBotInfo;\n\n\t#totalShards = 0;\n\t#readyCount = 0;\n\t#identifyQueue: PQueue | undefined;\n\n\tpublic constructor(options: ShardingManagerOptions, rest?: RESTLike) {\n\t\tsuper();\n\t\tthis.setMaxListeners(0);\n\n\t\tthis.options = {\n\t\t\tshardsPerCluster: 5,\n\t\t\ttotalShards: options.totalShards ?? \"auto\",\n\t\t\t...options,\n\t\t} satisfies Required<ShardingManagerOptions>;\n\n\t\tif (!rest) {\n\t\t\trest = new REST({ token: this.options.token });\n\t\t}\n\n\t\tthis.rest = rest;\n\t}\n\n\tpublic get totalClusters() {\n\t\tconst { shardsPerCluster } = this.options;\n\n\t\tif (shardsPerCluster <= 0) {\n\t\t\treturn 0;\n\t\t}\n\n\t\treturn Math.ceil(this.totalShards / shardsPerCluster);\n\t}\n\n\tpublic get totalShards() {\n\t\treturn this.#totalShards;\n\t}\n\n\tpublic get ready() {\n\t\treturn this.totalClusters > 0 && this.#readyCount === this.totalClusters;\n\t}\n\n\tpublic async spawn(): Promise<void> {\n\t\tthis.#gatewayInfo = await this.rest.get<APIGatewayBotInfo>(\"/gateway/bot\");\n\n\t\tconst { session_start_limit: limit } = this.#gatewayInfo;\n\n\t\tthis.#totalShards =\n\t\t\ttypeof this.options.totalShards === \"number\" ? this.options.totalShards : this.#gatewayInfo.shards;\n\n\t\tthis.#identifyQueue = new PQueue({\n\t\t\tconcurrency: limit.max_concurrency,\n\t\t\tintervalCap: limit.max_concurrency,\n\t\t\tinterval: 5_000,\n\t\t});\n\n\t\tconst { totalShards, totalClusters } = this;\n\n\t\tthis.emit(\"debug\", `Spawning ${totalClusters} clusters (${totalShards} total shards)...`);\n\n\t\tfor (let i = 0; i < totalClusters; i++) {\n\t\t\tthis.#spawnCluster(i);\n\t\t}\n\n\t\tthis.emit(\"debug\", \"All clusters spawned\");\n\t}\n\n\tpublic async kill(signal: NodeJS.Signals = \"SIGTERM\"): Promise<void> {\n\t\tthis.emit(\"debug\", \"Shutting down all clusters...\");\n\n\t\tconst tasks: Promise<void>[] = [];\n\n\t\tfor (const cluster of this.clusters.values()) {\n\t\t\ttasks.push(\n\t\t\t\tnew Promise<void>((resolve) => {\n\t\t\t\t\tcluster.process.once(\"exit\", () => resolve());\n\t\t\t\t\tcluster.kill(signal);\n\t\t\t\t}),\n\t\t\t);\n\t\t}\n\n\t\tawait Promise.all(tasks);\n\n\t\tthis.emit(\"debug\", \"All clusters shut down\");\n\t}\n\n\tpublic broadcast(payload: GatewaySendPayload): void {\n\t\tfor (const cluster of this.clusters.values()) {\n\t\t\tcluster.send(payload);\n\t\t}\n\t}\n\n\tpublic async broadcastEval<T>(fn: (cluster: Cluster) => T | Promise<T>): Promise<EvalResult<T>[]> {\n\t\tconst promises = this.clusters.map((cluster) => cluster.eval(fn));\n\t\treturn Promise.all(promises);\n\t}\n\n\tpublic send(shardId: number, payload: GatewaySendPayload) {\n\t\tconst cluster = this.clusters.find((cluster) => cluster.shards.has(shardId));\n\n\t\tif (!cluster) {\n\t\t\tthrow new Error(`Shard ${shardId} not found`);\n\t\t}\n\n\t\tcluster.send(payload);\n\t}\n\n\tprotected requestIdentify(cluster: ClusterProcess, shardId: number): void {\n\t\tthis.#identifyQueue?.add(() => cluster.identifyShard(shardId));\n\t}\n\n\t#getShardIdsForCluster(clusterId: number): number[] {\n\t\tconst start = clusterId * this.options.shardsPerCluster;\n\t\tconst end = Math.min(start + this.options.shardsPerCluster, this.totalShards);\n\n\t\treturn Array.from({ length: end - start }, (_, i) => start + i);\n\t}\n\n\t#spawnCluster(id: number) {\n\t\tconst shardIds = this.#getShardIdsForCluster(id);\n\t\tconst firstShardId = shardIds[0];\n\t\tconst lastShardId = shardIds[shardIds.length - 1];\n\n\t\tthis.emit(\"debug\", `Spawning cluster ${id} (shards ${firstShardId}-${lastShardId})`);\n\n\t\tconst env: NodeJS.ProcessEnv = {\n\t\t\t...process.env,\n\t\t\tBAKIT_CLUSTER_ID: String(id),\n\t\t\tBAKIT_CLUSTER_SHARD_TOTAL: String(this.totalShards),\n\t\t\tBAKIT_CLUSTER_SHARD_LIST: JSON.stringify(shardIds),\n\t\t\tBAKIT_DISCORD_TOKEN: this.options.token,\n\t\t\tBAKIT_DISCORD_INTENTS: String(this.options.intents),\n\t\t\tBAKIT_DISCORD_GATEWAY_URL: this.#gatewayInfo?.url,\n\t\t\tBAKIT_DISCORD_GATEWAY_VERSION: \"10\",\n\t\t};\n\n\t\tconst cluster = new ClusterProcess(this, id, { env });\n\n\t\tthis.#bindClusterEvents(cluster, id);\n\n\t\tthis.clusters.set(id, cluster);\n\t\tthis.emit(\"clusterCreate\", cluster);\n\t}\n\n\t#bindClusterEvents(cluster: ClusterProcess, id: number): void {\n\t\tcluster.on(\"ready\", () => {\n\t\t\tthis.#readyCount++;\n\t\t\tthis.emit(\"clusterReady\", cluster);\n\n\t\t\tif (this.ready) {\n\t\t\t\tthis.emit(\"ready\");\n\t\t\t}\n\t\t});\n\n\t\tcluster.process.on(\"exit\", (code) => {\n\t\t\tthis.emit(\"clusterExit\", cluster, code);\n\n\t\t\tthis.clusters.delete(id);\n\t\t\tthis.#readyCount = Math.max(0, this.#readyCount - 1);\n\t\t});\n\n\t\tcluster.on(\"error\", (err) => {\n\t\t\tthis.emit(\"clusterError\", cluster, err);\n\t\t});\n\n\t\tcluster.on(\"debug\", (msg) => {\n\t\t\tthis.emit(\"debug\", `[Cluster ${id}] ${msg}`);\n\t\t});\n\n\t\tcluster.on(\"dispatch\", (shardId, payload) => {\n\t\t\tthis.emit(\"dispatch\", cluster, shardId, payload);\n\t\t});\n\n\t\tcluster.on(\"raw\", (shardId, payload) => {\n\t\t\tthis.emit(\"raw\", cluster, shardId, payload);\n\t\t});\n\n\t\tcluster.on(\"shardAdd\", (shardId) => {\n\t\t\tthis.emit(\"shardAdd\", cluster, shardId);\n\t\t});\n\n\t\tcluster.on(\"shardReady\", (shardId) => {\n\t\t\tthis.emit(\"shardReady\", cluster, shardId);\n\t\t});\n\n\t\tcluster.on(\"shardDisconnect\", (shardId, code) => {\n\t\t\tthis.emit(\"shardDisconnect\", cluster, shardId, code);\n\t\t});\n\n\t\tcluster.on(\"shardResume\", (shardId) => {\n\t\t\tthis.emit(\"shardResume\", cluster, shardId);\n\t\t});\n\n\t\tcluster.on(\"shardError\", (shardId, error) => {\n\t\t\tthis.emit(\"shardError\", cluster, shardId, error);\n\t\t});\n\n\t\tcluster.on(\"needIdentify\", (shardId) => {\n\t\t\tthis.requestIdentify(cluster, shardId);\n\t\t});\n\t}\n}\n"],"mappings":"0kBAqBA,IAAY,EAAA,SAAA,EAAL,OACN,GAAA,EAAA,KAAA,GAAA,OACA,EAAA,EAAA,WAAA,GAAA,aACA,EAAA,EAAA,MAAA,GAAA,QACA,EAAA,EAAA,SAAA,GAAA,WACA,EAAA,EAAA,cAAA,GAAA,gBACA,EAAA,EAAA,aAAA,GAAA,sBAGW,EAAA,SAAA,EAAL,OACN,GAAA,EAAA,OAAA,GAAA,SACA,EAAA,EAAA,UAAA,GAAA,YACA,EAAA,EAAA,SAAA,GAAA,kBAwBD,IAAa,EAAb,cAA2B,CAA0B,CACpD,GAAS,EAAW,KACpB,GACA,GACA,GAAe,IAAI,EACnB,GAA8B,EAAE,CAEhC,GACA,GACA,GAEA,GAAqB,GACrB,GAAoB,GACpB,GAAoB,EACpB,GACA,GACA,GAEA,GAEA,YACC,EACA,EACC,CACD,OAAO,CAHS,KAAA,GAAA,EACA,KAAA,QAAA,EAKjB,IAAW,OAAQ,CAClB,OAAO,MAAA,EAGR,IAAW,SAAU,CAKpB,OAJI,MAAA,IAA4B,IAAM,MAAA,IAA2B,GACzD,GAGD,MAAA,EAAyB,MAAA,EAGjC,IAAW,WAAqB,CAC/B,IAAM,EAAe,MAAA,IAAoB,IAAA,GACnC,EAAc,MAAA,IAAuB,IAAA,GAG3C,OAFqB,MAAA,IAAmB,EAAc,QAE/B,GAAe,EAGvC,MAAa,SAAyB,CACrC,GAAI,MAAA,IAAgB,EAAW,MAAQ,MAAA,IAAgB,EAAW,aACjE,MAAU,MAAM,wCAAwC,CAGzD,OAAO,IAAI,SAAS,EAAS,IAAW,CACvC,IAAM,MAAgB,CACrB,KAAK,IAAI,QAAS,EAAQ,CAC1B,KAAK,IA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AAS,EAAS,EAAQ,EAC1C,CAEF,EAAQ,GAAG,WAAa,GAAY,CACnC,KAAK,KAAK,WAAY,EAAS,EAAQ,EACtC,CAEF,EAAQ,GAAG,aAAe,GAAY,CACrC,KAAK,KAAK,aAAc,EAAS,EAAQ,EACxC,CAEF,EAAQ,GAAG,mBAAoB,EAAS,IAAS,CAChD,KAAK,KAAK,kBAAmB,EAAS,EAAS,EAAK,EACnD,CAEF,EAAQ,GAAG,cAAgB,GAAY,CACtC,KAAK,KAAK,cAAe,EAAS,EAAQ,EACzC,CAEF,EAAQ,GAAG,cAAe,EAAS,IAAU,CAC5C,KAAK,KAAK,aAAc,EAAS,EAAS,EAAM,EAC/C,CAEF,EAAQ,GAAG,eAAiB,GAAY,CACvC,KAAK,gBAAgB,EAAS,EAAQ,EACrC"}
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bakit/gateway",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.3",
|
|
4
4
|
"description": "Gateway manager for bakit framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
7
7
|
".": {
|
|
8
8
|
"import": {
|
|
9
|
-
"types": "./dist/index.d.
|
|
10
|
-
"default": "./dist/index.
|
|
9
|
+
"types": "./dist/index.d.mts",
|
|
10
|
+
"default": "./dist/index.mjs"
|
|
11
11
|
},
|
|
12
12
|
"require": {
|
|
13
13
|
"types": "./dist/index.d.cts",
|
|
@@ -39,14 +39,14 @@
|
|
|
39
39
|
"p-queue": "^9.1.0",
|
|
40
40
|
"type-fest": "^4.41.0",
|
|
41
41
|
"ws": "^8.19.0",
|
|
42
|
-
"@bakit/rest": "^3.0.
|
|
43
|
-
"@bakit/utils": "^3.0.
|
|
42
|
+
"@bakit/rest": "^3.0.3",
|
|
43
|
+
"@bakit/utils": "^3.0.1"
|
|
44
44
|
},
|
|
45
45
|
"devDependencies": {
|
|
46
46
|
"@types/ws": "^8.18.1"
|
|
47
47
|
},
|
|
48
48
|
"scripts": {
|
|
49
|
-
"build": "
|
|
49
|
+
"build": "tsdown --config-loader unrun",
|
|
50
50
|
"type-check": "tsc --noEmit",
|
|
51
51
|
"test": "vitest run --pass-with-no-tests"
|
|
52
52
|
}
|
package/dist/cluster.js
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { Cluster, ClusterProcess } from '@bakit/gateway';
|
|
2
|
-
|
|
3
|
-
// src/lib/internal/cluster.ts
|
|
4
|
-
var {
|
|
5
|
-
BAKIT_CLUSTER_ID,
|
|
6
|
-
BAKIT_DISCORD_TOKEN,
|
|
7
|
-
BAKIT_DISCORD_INTENTS,
|
|
8
|
-
BAKIT_DISCORD_GATEWAY_URL,
|
|
9
|
-
BAKIT_DISCORD_GATEWAY_VERSION,
|
|
10
|
-
BAKIT_CLUSTER_SHARD_TOTAL,
|
|
11
|
-
BAKIT_CLUSTER_SHARD_LIST
|
|
12
|
-
} = process.env, cluster = new Cluster(Number(BAKIT_CLUSTER_ID), {
|
|
13
|
-
token: BAKIT_DISCORD_TOKEN,
|
|
14
|
-
intents: Number(BAKIT_DISCORD_INTENTS),
|
|
15
|
-
total: Number(BAKIT_CLUSTER_SHARD_TOTAL),
|
|
16
|
-
shards: JSON.parse(BAKIT_CLUSTER_SHARD_LIST),
|
|
17
|
-
gateway: {
|
|
18
|
-
baseURL: BAKIT_DISCORD_GATEWAY_URL,
|
|
19
|
-
version: Number(BAKIT_DISCORD_GATEWAY_VERSION)
|
|
20
|
-
}
|
|
21
|
-
});
|
|
22
|
-
ClusterProcess.bindProcess(cluster);
|
|
23
|
-
cluster.spawn();
|
package/dist/index.d.ts
DELETED
|
@@ -1,203 +0,0 @@
|
|
|
1
|
-
import EventEmitter from 'node:events';
|
|
2
|
-
import { GatewayReadyDispatchData, GatewayReceivePayload, GatewayDispatchPayload, GatewaySendPayload } from 'discord-api-types/v10';
|
|
3
|
-
import { ChildProcess } from 'node:child_process';
|
|
4
|
-
import { Collection } from '@discordjs/collection';
|
|
5
|
-
import { RESTLike } from '@bakit/rest';
|
|
6
|
-
|
|
7
|
-
declare enum ShardState {
|
|
8
|
-
Idle = 0,
|
|
9
|
-
Connecting = 1,
|
|
10
|
-
Ready = 2,
|
|
11
|
-
Resuming = 3,
|
|
12
|
-
Disconnecting = 4,
|
|
13
|
-
Disconnected = 5
|
|
14
|
-
}
|
|
15
|
-
declare enum ShardStrategy {
|
|
16
|
-
Resume = 0,
|
|
17
|
-
Reconnect = 1,
|
|
18
|
-
Shutdown = 2
|
|
19
|
-
}
|
|
20
|
-
interface ShardOptions {
|
|
21
|
-
total: number;
|
|
22
|
-
token: string;
|
|
23
|
-
intents: number | bigint;
|
|
24
|
-
gateway: {
|
|
25
|
-
baseURL: string;
|
|
26
|
-
version: number;
|
|
27
|
-
};
|
|
28
|
-
}
|
|
29
|
-
interface ShardEvents {
|
|
30
|
-
ready: [data: GatewayReadyDispatchData];
|
|
31
|
-
disconnect: [code: number];
|
|
32
|
-
resume: [];
|
|
33
|
-
error: [error: Error];
|
|
34
|
-
debug: [message: string];
|
|
35
|
-
raw: [payload: GatewayReceivePayload];
|
|
36
|
-
dispatch: [payload: GatewayDispatchPayload];
|
|
37
|
-
needIdentify: [];
|
|
38
|
-
}
|
|
39
|
-
declare class Shard extends EventEmitter<ShardEvents> {
|
|
40
|
-
#private;
|
|
41
|
-
readonly id: number;
|
|
42
|
-
readonly options: ShardOptions;
|
|
43
|
-
constructor(id: number, options: ShardOptions);
|
|
44
|
-
get state(): ShardState;
|
|
45
|
-
get latency(): number;
|
|
46
|
-
get resumable(): boolean;
|
|
47
|
-
connect(): Promise<void>;
|
|
48
|
-
disconnect(code: number): Promise<void>;
|
|
49
|
-
resume(): void;
|
|
50
|
-
identify(): void;
|
|
51
|
-
send(payload: GatewaySendPayload): void;
|
|
52
|
-
sendHeartbeat(): void;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
interface ClusterOptions {
|
|
56
|
-
token: string;
|
|
57
|
-
intents: bigint | number;
|
|
58
|
-
shards: number[];
|
|
59
|
-
total: number;
|
|
60
|
-
gateway: {
|
|
61
|
-
baseURL: string;
|
|
62
|
-
version: number;
|
|
63
|
-
};
|
|
64
|
-
}
|
|
65
|
-
interface ClusterEvents {
|
|
66
|
-
shardAdd: [id: number];
|
|
67
|
-
shardReady: [id: number];
|
|
68
|
-
shardDisconnect: [id: number, code: number];
|
|
69
|
-
shardResume: [id: number];
|
|
70
|
-
shardError: [id: number, error: Error];
|
|
71
|
-
needIdentify: [id: number];
|
|
72
|
-
debug: [message: string];
|
|
73
|
-
dispatch: [shardId: number, payload: GatewayDispatchPayload];
|
|
74
|
-
raw: [shardId: number, payload: GatewayReceivePayload];
|
|
75
|
-
ready: [];
|
|
76
|
-
error: [error: Error];
|
|
77
|
-
}
|
|
78
|
-
declare class Cluster extends EventEmitter<ClusterEvents> {
|
|
79
|
-
#private;
|
|
80
|
-
readonly id: number;
|
|
81
|
-
readonly options: ClusterOptions;
|
|
82
|
-
readonly shards: Collection<number, Shard>;
|
|
83
|
-
constructor(id: number, options: ClusterOptions);
|
|
84
|
-
get size(): number;
|
|
85
|
-
get ready(): boolean;
|
|
86
|
-
spawn(): Promise<void>;
|
|
87
|
-
shutdown(code?: number): Promise<void>;
|
|
88
|
-
broadcast(fn: (shard: Shard) => void): void;
|
|
89
|
-
send(payload: GatewaySendPayload): void;
|
|
90
|
-
send(shardId: number, payload: GatewaySendPayload): void;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
interface ShardingManagerOptions {
|
|
94
|
-
token: string;
|
|
95
|
-
intents: bigint | number;
|
|
96
|
-
totalShards?: number | "auto";
|
|
97
|
-
shardsPerCluster?: number;
|
|
98
|
-
}
|
|
99
|
-
interface ShardingManagerEvents {
|
|
100
|
-
shardAdd: [cluster: ClusterProcess, shardId: number];
|
|
101
|
-
shardReady: [cluster: ClusterProcess, shardId: number];
|
|
102
|
-
shardDisconnect: [cluster: ClusterProcess, shardId: number, code: number];
|
|
103
|
-
shardResume: [cluster: ClusterProcess, shardId: number];
|
|
104
|
-
shardError: [cluster: ClusterProcess, shardId: number, error: Error];
|
|
105
|
-
clusterCreate: [cluster: ClusterProcess];
|
|
106
|
-
clusterReady: [cluster: ClusterProcess];
|
|
107
|
-
clusterExit: [cluster: ClusterProcess, code: number | null];
|
|
108
|
-
clusterError: [cluster: ClusterProcess, error: Error];
|
|
109
|
-
dispatch: [cluster: ClusterProcess, shardId: number, payload: GatewayDispatchPayload];
|
|
110
|
-
raw: [cluster: ClusterProcess, shardId: number, payload: GatewayReceivePayload];
|
|
111
|
-
debug: [message: string];
|
|
112
|
-
ready: [];
|
|
113
|
-
}
|
|
114
|
-
declare class ShardingManager extends EventEmitter<ShardingManagerEvents> {
|
|
115
|
-
#private;
|
|
116
|
-
readonly clusters: Collection<number, ClusterProcess>;
|
|
117
|
-
readonly options: Required<ShardingManagerOptions>;
|
|
118
|
-
readonly rest: RESTLike;
|
|
119
|
-
constructor(options: ShardingManagerOptions, rest?: RESTLike);
|
|
120
|
-
get totalClusters(): number;
|
|
121
|
-
get totalShards(): number;
|
|
122
|
-
get ready(): boolean;
|
|
123
|
-
spawn(): Promise<void>;
|
|
124
|
-
kill(signal?: NodeJS.Signals): Promise<void>;
|
|
125
|
-
broadcast(payload: GatewaySendPayload): void;
|
|
126
|
-
broadcastEval<T>(fn: (cluster: Cluster) => T | Promise<T>): Promise<EvalResult<T>[]>;
|
|
127
|
-
send(shardId: number, payload: GatewaySendPayload): void;
|
|
128
|
-
protected requestIdentify(cluster: ClusterProcess, shardId: number): void;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
type ClusterIPCDispatchPayload<E extends keyof ClusterEvents = keyof ClusterEvents> = {
|
|
132
|
-
op: "dispatch";
|
|
133
|
-
t: E;
|
|
134
|
-
d: ClusterEvents[E];
|
|
135
|
-
};
|
|
136
|
-
interface ClusterIPCIdentifyPayload {
|
|
137
|
-
op: "identify";
|
|
138
|
-
d: number;
|
|
139
|
-
}
|
|
140
|
-
interface ClusterIPCGatewaySendPayload {
|
|
141
|
-
op: "send";
|
|
142
|
-
d: {
|
|
143
|
-
shardId?: number;
|
|
144
|
-
data: GatewaySendPayload;
|
|
145
|
-
};
|
|
146
|
-
}
|
|
147
|
-
interface ClusterIPCEvalRequestPayload {
|
|
148
|
-
op: "eval";
|
|
149
|
-
d: {
|
|
150
|
-
nonce: string;
|
|
151
|
-
script: string;
|
|
152
|
-
};
|
|
153
|
-
}
|
|
154
|
-
interface ClusterIPCEvalResponsePayload {
|
|
155
|
-
op: "evalResponse";
|
|
156
|
-
d: {
|
|
157
|
-
nonce: string;
|
|
158
|
-
success: boolean;
|
|
159
|
-
result: unknown;
|
|
160
|
-
error?: string;
|
|
161
|
-
};
|
|
162
|
-
}
|
|
163
|
-
type ClusterIPCPayload = ClusterIPCDispatchPayload | ClusterIPCIdentifyPayload | ClusterIPCGatewaySendPayload | ClusterIPCEvalRequestPayload | ClusterIPCEvalResponsePayload;
|
|
164
|
-
interface EvalResult<T> {
|
|
165
|
-
/**
|
|
166
|
-
* Whether the evaluation was successful
|
|
167
|
-
*/
|
|
168
|
-
success: boolean;
|
|
169
|
-
/**
|
|
170
|
-
* The result of the evaluation if successful
|
|
171
|
-
*/
|
|
172
|
-
data?: T;
|
|
173
|
-
/**
|
|
174
|
-
* The error if evaluation failed
|
|
175
|
-
*/
|
|
176
|
-
error?: Error;
|
|
177
|
-
/**
|
|
178
|
-
* The cluster process that executed the evaluation
|
|
179
|
-
*/
|
|
180
|
-
cluster: ClusterProcess;
|
|
181
|
-
}
|
|
182
|
-
interface ClusterProcessOptions {
|
|
183
|
-
env?: NodeJS.ProcessEnv;
|
|
184
|
-
execArgv?: string[];
|
|
185
|
-
}
|
|
186
|
-
declare class ClusterProcess extends EventEmitter<ClusterEvents> {
|
|
187
|
-
#private;
|
|
188
|
-
readonly manager: ShardingManager;
|
|
189
|
-
readonly id: number;
|
|
190
|
-
readonly process: ChildProcess;
|
|
191
|
-
constructor(manager: ShardingManager, id: number, options?: ClusterProcessOptions);
|
|
192
|
-
get shards(): Set<number>;
|
|
193
|
-
get killed(): boolean;
|
|
194
|
-
kill(signal?: NodeJS.Signals): void;
|
|
195
|
-
eval<T, C = unknown>(fn: (cluster: Cluster, ctx: C) => T | Promise<T>, ctx?: C): Promise<EvalResult<T>>;
|
|
196
|
-
send(payload: GatewaySendPayload): void;
|
|
197
|
-
send(shardId: number, payload: GatewaySendPayload): void;
|
|
198
|
-
sendIPC(message: ClusterIPCPayload): void;
|
|
199
|
-
identifyShard(id: number): void;
|
|
200
|
-
static bindProcess(cluster: Cluster): void;
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
export { Cluster, type ClusterEvents, type ClusterIPCDispatchPayload, type ClusterIPCEvalRequestPayload, type ClusterIPCEvalResponsePayload, type ClusterIPCGatewaySendPayload, type ClusterIPCIdentifyPayload, type ClusterIPCPayload, type ClusterOptions, ClusterProcess, type ClusterProcessOptions, type EvalResult, Shard, type ShardEvents, type ShardOptions, ShardState, ShardStrategy, ShardingManager, type ShardingManagerEvents, type ShardingManagerOptions };
|