fix(bun): correctly handle BunFiles

This commit is contained in:
alina 🌸 2024-04-24 13:22:16 +03:00
parent 44a60360ab
commit 1720433882
Signed by: teidesu
SSH key fingerprint: SHA256:uNeCpw6aTSU4aIObXLvHfLkDa82HWH9EiOj9AXOIRpI
4 changed files with 26 additions and 4 deletions

View file

@ -9,9 +9,9 @@ import {
} from '@mtcute/core/client.js' } from '@mtcute/core/client.js'
import { setPlatform } from '@mtcute/core/platform.js' import { setPlatform } from '@mtcute/core/platform.js'
import { NodePlatform } from './common-internals-node/platform.js'
import { downloadToFile } from './methods/download-file.js' import { downloadToFile } from './methods/download-file.js'
import { downloadAsNodeStream } from './methods/download-node-stream.js' import { downloadAsNodeStream } from './methods/download-node-stream.js'
import { BunPlatform } from './platform.js'
import { SqliteStorage } from './sqlite/index.js' import { SqliteStorage } from './sqlite/index.js'
import { BunCryptoProvider } from './utils/crypto.js' import { BunCryptoProvider } from './utils/crypto.js'
import { TcpTransport } from './utils/tcp.js' import { TcpTransport } from './utils/tcp.js'
@ -41,7 +41,7 @@ export interface BaseTelegramClientOptions
export class BaseTelegramClient extends BaseTelegramClientBase { export class BaseTelegramClient extends BaseTelegramClientBase {
constructor(opts: BaseTelegramClientOptions) { constructor(opts: BaseTelegramClientOptions) {
if (!opts.platformless) setPlatform(new NodePlatform()) if (!opts.platformless) setPlatform(new BunPlatform())
super({ super({
crypto: new BunCryptoProvider(), crypto: new BunCryptoProvider(),

View file

@ -1,5 +1,5 @@
export * from './client.js' export * from './client.js'
export * from './common-internals-node/platform.js' export * from './platform.js'
export * from './sqlite/index.js' export * from './sqlite/index.js'
export * from './utils/crypto.js' export * from './utils/crypto.js'
export * from './utils/tcp.js' export * from './utils/tcp.js'

View file

@ -0,0 +1,8 @@
import { NodePlatform } from './common-internals-node/platform.js'
import { normalizeFile } from './utils/normalize-file.js'
export class BunPlatform extends NodePlatform {
declare normalizeFile: typeof normalizeFile
}
BunPlatform.prototype.normalizeFile = normalizeFile

View file

@ -1,3 +1,4 @@
import { BunFile } from 'bun'
import { ReadStream } from 'fs' import { ReadStream } from 'fs'
import { stat } from 'fs/promises' import { stat } from 'fs/promises'
import { basename } from 'path' import { basename } from 'path'
@ -5,11 +6,24 @@ import { Readable as NodeReadable } from 'stream'
import { UploadFileLike } from '@mtcute/core' import { UploadFileLike } from '@mtcute/core'
// https://github.com/oven-sh/bun/issues/10481
function isBunFile(file: unknown): file is BunFile {
return file instanceof Blob && 'name' in file && file.name.length > 0
}
export async function normalizeFile(file: UploadFileLike) { export async function normalizeFile(file: UploadFileLike) {
if (typeof file === 'string') { if (typeof file === 'string') {
file = Bun.file(file) file = Bun.file(file)
} }
if (isBunFile(file)) {
return {
file: file,
fileName: file.name,
fileSize: file.size,
}
}
// while these are not Bun-specific, they still may happen // while these are not Bun-specific, they still may happen
if (file instanceof ReadStream) { if (file instanceof ReadStream) {
const fileName = basename(file.path.toString()) const fileName = basename(file.path.toString())
@ -28,6 +42,6 @@ export async function normalizeFile(file: UploadFileLike) {
} }
} }
// string -> ReadStream, thus already handled // string -> BunFile, thus already handled
return null return null
} }