From 17204338826370483f75b3003f097097269d469a Mon Sep 17 00:00:00 2001 From: alina sireneva Date: Wed, 24 Apr 2024 13:22:16 +0300 Subject: [PATCH] fix(bun): correctly handle `BunFile`s --- packages/bun/src/client.ts | 4 ++-- packages/bun/src/index.ts | 2 +- packages/bun/src/platform.ts | 8 ++++++++ packages/bun/src/utils/normalize-file.ts | 16 +++++++++++++++- 4 files changed, 26 insertions(+), 4 deletions(-) create mode 100644 packages/bun/src/platform.ts diff --git a/packages/bun/src/client.ts b/packages/bun/src/client.ts index fea59d01..ee3f66f2 100644 --- a/packages/bun/src/client.ts +++ b/packages/bun/src/client.ts @@ -9,9 +9,9 @@ import { } from '@mtcute/core/client.js' import { setPlatform } from '@mtcute/core/platform.js' -import { NodePlatform } from './common-internals-node/platform.js' import { downloadToFile } from './methods/download-file.js' import { downloadAsNodeStream } from './methods/download-node-stream.js' +import { BunPlatform } from './platform.js' import { SqliteStorage } from './sqlite/index.js' import { BunCryptoProvider } from './utils/crypto.js' import { TcpTransport } from './utils/tcp.js' @@ -41,7 +41,7 @@ export interface BaseTelegramClientOptions export class BaseTelegramClient extends BaseTelegramClientBase { constructor(opts: BaseTelegramClientOptions) { - if (!opts.platformless) setPlatform(new NodePlatform()) + if (!opts.platformless) setPlatform(new BunPlatform()) super({ crypto: new BunCryptoProvider(), diff --git a/packages/bun/src/index.ts b/packages/bun/src/index.ts index d354f7eb..87f84c57 100644 --- a/packages/bun/src/index.ts +++ b/packages/bun/src/index.ts @@ -1,5 +1,5 @@ export * from './client.js' -export * from './common-internals-node/platform.js' +export * from './platform.js' export * from './sqlite/index.js' export * from './utils/crypto.js' export * from './utils/tcp.js' diff --git a/packages/bun/src/platform.ts b/packages/bun/src/platform.ts new file mode 100644 index 00000000..ef805446 --- /dev/null +++ b/packages/bun/src/platform.ts @@ -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 diff --git a/packages/bun/src/utils/normalize-file.ts b/packages/bun/src/utils/normalize-file.ts index 236123f2..49b00f75 100644 --- a/packages/bun/src/utils/normalize-file.ts +++ b/packages/bun/src/utils/normalize-file.ts @@ -1,3 +1,4 @@ +import { BunFile } from 'bun' import { ReadStream } from 'fs' import { stat } from 'fs/promises' import { basename } from 'path' @@ -5,11 +6,24 @@ import { Readable as NodeReadable } from 'stream' 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) { if (typeof file === 'string') { 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 if (file instanceof ReadStream) { 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 }