refactor: use Buffer.isBuffer
This commit is contained in:
parent
1c4d9c1ab3
commit
1b3f02f5f4
7 changed files with 12 additions and 12 deletions
|
@ -16,7 +16,7 @@ export async function downloadAsBuffer(
|
|||
): Promise<Buffer> {
|
||||
if (
|
||||
params.location instanceof FileLocation &&
|
||||
params.location.location instanceof Buffer
|
||||
Buffer.isBuffer(params.location.location)
|
||||
) {
|
||||
return params.location.location
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -46,7 +46,7 @@ export async function* downloadAsIterable(
|
|||
;(input as tl.Mutable<FileLocation>).location = input.location()
|
||||
}
|
||||
|
||||
if (input.location instanceof Buffer) {
|
||||
if (Buffer.isBuffer(input.location)) {
|
||||
yield input.location
|
||||
return
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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(
|
||||
|
|
|
@ -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))
|
||||
|
||||
|
|
|
@ -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<Buffer>).then((dec) => this._inner.feed(dec))
|
||||
if (Buffer.isBuffer(dec)) this._inner.feed(dec)
|
||||
else dec.then((dec) => this._inner.feed(dec))
|
||||
}
|
||||
|
||||
reset(): void {
|
||||
|
|
Loading…
Reference in a new issue