Repository: discordjs/discord.js
File: packages/ws/src/ws/WebSocketShard.ts (TypeScript)

Code before:
		// Deal with identify compress
		if (this.useIdentifyCompression) {
			return new Promise((resolve, reject) => {
				// eslint-disable-next-line promise/prefer-await-to-callbacks
				nativeZLib.inflate(decompressable, { chunkSize: 65_535 }, (err, result) => {
					if (err) {
						reject(err);
						return;
					}

					resolve(JSON.parse(this.textDecoder.decode(result)) as GatewayReceivePayload);
				});
			});
		}

		// Deal with gw wide zlib-stream compression
		if (this.strategy.options.compression !== null) {
			const flush =
				decompressable.length >= 4 &&
				decompressable.at(-4) === 0x00 &&
				decompressable.at(-3) === 0x00 &&
				decompressable.at(-2) === 0xff &&
				decompressable.at(-1) === 0xff;

			if (this.nativeInflate) {
				this.nativeInflate!.write(decompressable, 'binary');

				if (!flush) {
					return null;
				}

				const [result] = await once(this.nativeInflate, 'data');
				if (!result) {
					return null;
				}

				return JSON.parse(
					typeof result === 'string' ? result : this.textDecoder.decode(result),
				) as GatewayReceivePayload;
			} else if (this.zLibSyncInflate) {
				const zLibSync = (await getZLibSync())!;
				this.zLibSyncInflate.push(Buffer.from(decompressable), flush ? zLibSync.Z_SYNC_FLUSH : zLibSync.Z_NO_FLUSH);

				if (this.zLibSyncInflate.err) {
					this.emit(WebSocketShardEvents.Error, {
						error: new Error(
							`${this.zLibSyncInflate.err}${this.zLibSyncInflate.msg ? `: ${this.zLibSyncInflate.msg}` : ''}`,
						),
					});
				}


Review comment (suggestion):
And if that `!` is needed, I will yell at TS

```suggestion
				this.nativeInflate.write(decompressable, 'binary');
```

Code after:

		const session = await this.strategy.retrieveSessionInfo(this.id);

		await this.send({
			op: GatewayOpcodes.Heartbeat,
			// eslint-disable-next-line id-length
			d: session?.sequence ?? null,
		});

		this.lastHeartbeatAt = Date.now();
		this.isAck = false;
	}

	private parseInflateResult(result: any): GatewayReceivePayload | null {
		if (!result) {
			return null;
		}

		return JSON.parse(typeof result === 'string' ? result : this.textDecoder.decode(result)) as GatewayReceivePayload;
	}

	private async unpackMessage(data: Data, isBinary: boolean): Promise<GatewayReceivePayload | null> {
		// Deal with no compression
		if (!isBinary) {
			try {
				return JSON.parse(data as string) as GatewayReceivePayload;
			} catch {
				// This is a non-JSON payload / (at the time of writing this comment) emitted by bun wrongly interpreting custom close codes https://github.com/oven-sh/bun/issues/3392
				return null;
			}
		}

		const decompressable = new Uint8Array(data as ArrayBuffer);

		// Deal with identify compress
		if (this.identifyCompressionEnabled) {
			// eslint-disable-next-line no-async-promise-executor
			return new Promise(async (resolve, reject) => {
				const zlib = (await getNativeZlib())!;
				// eslint-disable-next-line promise/prefer-await-to-callbacks
				zlib.inflate(decompressable, { chunkSize: 65_535 }, (err, result) => {
					if (err) {
						reject(err);
						return;
					}

					resolve(JSON.parse(this.textDecoder.decode(result)) as GatewayReceivePayload);
				});
			});
		}