Skip to content

Commit

Permalink
feat(axios): support timeout option for http.file() (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed May 7, 2023
1 parent 95af3b2 commit 6d0e33e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
3 changes: 2 additions & 1 deletion adapters/discord/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export class DiscordMessenger extends Messenger<DiscordBot> {
}

async sendEmbed(attrs: Dict, payload: Dict) {
const { filename, data, mime } = await this.bot.ctx.http.file(attrs.url)
const { filename, data, mime } = await this.bot.ctx.http.file(attrs.url, attrs)
const form = new FormData()
// https://github.com/form-data/form-data/issues/468
const value = process.env.KOISHI_ENV === 'browser'
Expand Down Expand Up @@ -111,6 +111,7 @@ export class DiscordMessenger extends Messenger<DiscordBot> {
// auto mode
return await this.bot.ctx.http.head(attrs.url, {
headers: { accept: type + '/*' },
timeout: +attrs.timeout || undefined,
}).then((headers) => {
if (headers['content-type'].startsWith(type)) {
return sendDirect()
Expand Down
3 changes: 2 additions & 1 deletion adapters/kook/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class KookMessenger extends Messenger<KookBot> {
private async transformUrl({ type, attrs }: h) {
if (['file:', 'base64:', 'data:'].some(protocol => attrs.url.startsWith(protocol))) {
const payload = new FormData()
const result = await this.bot.ctx.http.file(attrs.url)
const result = await this.bot.ctx.http.file(attrs.url, attrs)
payload.append('file', Buffer.from(result.data), {
filename: attrs.file || result.filename,
})
Expand All @@ -50,6 +50,7 @@ export class KookMessenger extends Messenger<KookBot> {
const res = await this.bot.ctx.http.get<internal.Readable>(attrs.url, {
headers: { accept: type + '/*' },
responseType: 'stream',
timeout: +attrs.timeout || undefined,
})
const payload = new FormData()
payload.append('file', res, {
Expand Down
2 changes: 1 addition & 1 deletion adapters/telegram/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type AssetType = 'photo' | 'audio' | 'document' | 'video' | 'animation'

async function appendAsset(bot: TelegramBot, form: FormData, element: h): Promise<AssetType> {
let assetType: AssetType
const { filename, data, mime } = await bot.ctx.http.file(element.attrs.url)
const { filename, data, mime } = await bot.ctx.http.file(element.attrs.url, element.attrs)
if (element.type === 'image') {
assetType = mime === 'image/gif' ? 'animation' : 'photo'
} else if (element.type === 'file') {
Expand Down
12 changes: 10 additions & 2 deletions packages/axios/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export class Quester {
return trimSlash(this.config.endpoint || '') + url
}

async file(url: string): Promise<Quester.File> {
async file(url: string, options: Quester.FileOptions = {}): Promise<Quester.File> {
const capture = /^data:([\w/-]+);base64,(.*)$/.exec(url)
if (capture) {
const [, mime, base64] = capture
Expand All @@ -99,7 +99,11 @@ export class Quester {
return { mime, filename: name, data: base64ToArrayBuffer(base64) }
}
let [, name] = this.resolve(url).match(/.+\/([^/?]*)(?=\?)?/)
const { headers, data } = await this.axios(url, { method: 'GET', responseType: 'arraybuffer' })
const { headers, data } = await this.axios(url, {
method: 'GET',
responseType: 'arraybuffer',
timeout: +options.timeout || undefined,
})
const mime = headers['content-type']
if (!name.includes('.')) {
const ext = mimedb[mime]?.extensions?.[0]
Expand Down Expand Up @@ -130,6 +134,10 @@ export namespace Quester {
data: ArrayBufferLike
}

export interface FileOptions {
timeout?: number | string
}

export const isAxiosError = axios.isAxiosError

export interface Config {
Expand Down

0 comments on commit 6d0e33e

Please sign in to comment.