diff --git a/packages/client/src/methods/files/download-buffer.ts b/packages/client/src/methods/files/download-buffer.ts index a945584a..b7d89133 100644 --- a/packages/client/src/methods/files/download-buffer.ts +++ b/packages/client/src/methods/files/download-buffer.ts @@ -16,7 +16,7 @@ export async function downloadAsBuffer( ): Promise { if ( params.location instanceof FileLocation && - params.location.location instanceof Buffer + Buffer.isBuffer(params.location.location) ) { return params.location.location } diff --git a/packages/client/src/methods/files/download-file.ts b/packages/client/src/methods/files/download-file.ts index b78b58be..9262240d 100644 --- a/packages/client/src/methods/files/download-file.ts +++ b/packages/client/src/methods/files/download-file.ts @@ -30,7 +30,7 @@ export function downloadToFile( if ( params.location instanceof FileLocation && - params.location.location instanceof Buffer + Buffer.isBuffer(params.location.location) ) { // early return for inline files const buf = params.location.location diff --git a/packages/client/src/methods/files/download-iterable.ts b/packages/client/src/methods/files/download-iterable.ts index 8c0840e5..e263f767 100644 --- a/packages/client/src/methods/files/download-iterable.ts +++ b/packages/client/src/methods/files/download-iterable.ts @@ -46,7 +46,7 @@ export async function* downloadAsIterable( ;(input as tl.Mutable).location = input.location() } - if (input.location instanceof Buffer) { + if (Buffer.isBuffer(input.location)) { yield input.location return } diff --git a/packages/client/src/methods/files/download-stream.ts b/packages/client/src/methods/files/download-stream.ts index d5f6b98f..6a74ec88 100644 --- a/packages/client/src/methods/files/download-stream.ts +++ b/packages/client/src/methods/files/download-stream.ts @@ -16,7 +16,7 @@ export function downloadAsStream( ): Readable { if ( params.location instanceof FileLocation && - params.location.location instanceof Buffer + Buffer.isBuffer(params.location.location) ) { return bufferToStream(params.location.location) } diff --git a/packages/client/src/methods/files/upload-file.ts b/packages/client/src/methods/files/upload-file.ts index 6d4d7adc..cfe96eac 100644 --- a/packages/client/src/methods/files/upload-file.ts +++ b/packages/client/src/methods/files/upload-file.ts @@ -95,7 +95,7 @@ export async function uploadFile( let fileName = 'unnamed' let fileMime = params.fileMime - if (file instanceof Buffer) { + if (Buffer.isBuffer(file)) { fileSize = file.length file = bufferToStream(file) } @@ -180,8 +180,8 @@ export async function uploadFile( throw new MtCuteArgumentError( `Unexpected EOS (there were only ${idx} parts, but expected ${partCount})` ) - // even though typescript seems to guarantee this, we can't be sure because its js after all - if (!(part instanceof Buffer)) + + if (!Buffer.isBuffer(part)) throw new MtCuteArgumentError(`Part ${idx} was not a Buffer!`) if (part.length > partSize) throw new MtCuteArgumentError( diff --git a/packages/core/src/network/mtproto-session.ts b/packages/core/src/network/mtproto-session.ts index 3fe89e6e..9c358dca 100644 --- a/packages/core/src/network/mtproto-session.ts +++ b/packages/core/src/network/mtproto-session.ts @@ -70,9 +70,9 @@ export class MtprotoSession { if (!this._authKey) throw new Error('Keys are not set up!') const length = - message.constructor === Buffer + Buffer.isBuffer(message) ? message.length - : SerializationCounter.countNeededBytes(message as tl.TlObject) + : SerializationCounter.countNeededBytes(message) let padding = (32 /* header size */ + length + 12) /* min padding */ % 16 padding = 12 + (padding ? 16 - padding : 0) @@ -83,7 +83,7 @@ export class MtprotoSession { encryptedWriter.long(messageId) encryptedWriter.int32(seqNo) encryptedWriter.uint32(length) - if (message.constructor === Buffer) encryptedWriter.raw(message) + if (Buffer.isBuffer(message)) encryptedWriter.raw(message) else encryptedWriter.object(message as tl.TlObject) encryptedWriter.raw(randomBytes(padding)) diff --git a/packages/core/src/network/transports/ws-obfuscated.ts b/packages/core/src/network/transports/ws-obfuscated.ts index b8996471..b2afdc89 100644 --- a/packages/core/src/network/transports/ws-obfuscated.ts +++ b/packages/core/src/network/transports/ws-obfuscated.ts @@ -77,8 +77,8 @@ export class ObfuscatedPacketCodec extends EventEmitter implements PacketCodec { feed(data: Buffer): void { const dec = this._decryptor!.decrypt(data) - if (dec.constructor === Buffer) this._inner.feed(dec) - else (dec as Promise).then((dec) => this._inner.feed(dec)) + if (Buffer.isBuffer(dec)) this._inner.feed(dec) + else dec.then((dec) => this._inner.feed(dec)) } reset(): void {