refactor: use Buffer.isBuffer

This commit is contained in:
teidesu 2021-05-23 13:42:38 +03:00
parent 1c4d9c1ab3
commit 1b3f02f5f4
7 changed files with 12 additions and 12 deletions

View file

@ -16,7 +16,7 @@ export async function downloadAsBuffer(
): Promise<Buffer> { ): Promise<Buffer> {
if ( if (
params.location instanceof FileLocation && params.location instanceof FileLocation &&
params.location.location instanceof Buffer Buffer.isBuffer(params.location.location)
) { ) {
return params.location.location return params.location.location
} }

View file

@ -30,7 +30,7 @@ export function downloadToFile(
if ( if (
params.location instanceof FileLocation && params.location instanceof FileLocation &&
params.location.location instanceof Buffer Buffer.isBuffer(params.location.location)
) { ) {
// early return for inline files // early return for inline files
const buf = params.location.location const buf = params.location.location

View file

@ -46,7 +46,7 @@ export async function* downloadAsIterable(
;(input as tl.Mutable<FileLocation>).location = input.location() ;(input as tl.Mutable<FileLocation>).location = input.location()
} }
if (input.location instanceof Buffer) { if (Buffer.isBuffer(input.location)) {
yield input.location yield input.location
return return
} }

View file

@ -16,7 +16,7 @@ export function downloadAsStream(
): Readable { ): Readable {
if ( if (
params.location instanceof FileLocation && params.location instanceof FileLocation &&
params.location.location instanceof Buffer Buffer.isBuffer(params.location.location)
) { ) {
return bufferToStream(params.location.location) return bufferToStream(params.location.location)
} }

View file

@ -95,7 +95,7 @@ export async function uploadFile(
let fileName = 'unnamed' let fileName = 'unnamed'
let fileMime = params.fileMime let fileMime = params.fileMime
if (file instanceof Buffer) { if (Buffer.isBuffer(file)) {
fileSize = file.length fileSize = file.length
file = bufferToStream(file) file = bufferToStream(file)
} }
@ -180,8 +180,8 @@ export async function uploadFile(
throw new MtCuteArgumentError( throw new MtCuteArgumentError(
`Unexpected EOS (there were only ${idx} parts, but expected ${partCount})` `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!`) throw new MtCuteArgumentError(`Part ${idx} was not a Buffer!`)
if (part.length > partSize) if (part.length > partSize)
throw new MtCuteArgumentError( throw new MtCuteArgumentError(

View file

@ -70,9 +70,9 @@ export class MtprotoSession {
if (!this._authKey) throw new Error('Keys are not set up!') if (!this._authKey) throw new Error('Keys are not set up!')
const length = const length =
message.constructor === Buffer Buffer.isBuffer(message)
? message.length ? message.length
: SerializationCounter.countNeededBytes(message as tl.TlObject) : SerializationCounter.countNeededBytes(message)
let padding = let padding =
(32 /* header size */ + length + 12) /* min padding */ % 16 (32 /* header size */ + length + 12) /* min padding */ % 16
padding = 12 + (padding ? 16 - padding : 0) padding = 12 + (padding ? 16 - padding : 0)
@ -83,7 +83,7 @@ export class MtprotoSession {
encryptedWriter.long(messageId) encryptedWriter.long(messageId)
encryptedWriter.int32(seqNo) encryptedWriter.int32(seqNo)
encryptedWriter.uint32(length) encryptedWriter.uint32(length)
if (message.constructor === Buffer) encryptedWriter.raw(message) if (Buffer.isBuffer(message)) encryptedWriter.raw(message)
else encryptedWriter.object(message as tl.TlObject) else encryptedWriter.object(message as tl.TlObject)
encryptedWriter.raw(randomBytes(padding)) encryptedWriter.raw(randomBytes(padding))

View file

@ -77,8 +77,8 @@ export class ObfuscatedPacketCodec extends EventEmitter implements PacketCodec {
feed(data: Buffer): void { feed(data: Buffer): void {
const dec = this._decryptor!.decrypt(data) const dec = this._decryptor!.decrypt(data)
if (dec.constructor === Buffer) this._inner.feed(dec) if (Buffer.isBuffer(dec)) this._inner.feed(dec)
else (dec as Promise<Buffer>).then((dec) => this._inner.feed(dec)) else dec.then((dec) => this._inner.feed(dec))
} }
reset(): void { reset(): void {