Skip to content

Commit

Permalink
➖ chore: don't use fetchbase64
Browse files Browse the repository at this point in the history
  • Loading branch information
Helloyunho committed Mar 1, 2024
1 parent 73acf8f commit b890958
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 6 deletions.
2 changes: 1 addition & 1 deletion deps.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
export { EventEmitter } from 'https://deno.land/x/[email protected]/mod.ts'
export { decompress_with as unzlib } from 'npm:@evan/[email protected]/target/zlib/node.mjs'
export { fetchAuto } from 'https://deno.land/x/[email protected]/mod.ts'
export { walk } from 'jsr:@std/[email protected]/walk'
export { join } from 'node:path'
export { Mixin } from 'npm:[email protected]'
export { verify as edverify } from 'npm:@evan/[email protected]/target/ed25519/node.mjs'
export { decodeHex } from 'jsr:@std/[email protected]/hex'
export { encodeBase64 } from 'jsr:@std/[email protected]/base64'
export { readAll } from 'jsr:@std/[email protected]/read_all'
2 changes: 1 addition & 1 deletion src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import type { ClientEvents } from '../gateway/handlers/mod.ts'
import type { Collector } from './collectors.ts'
import { HarmonyEventEmitter } from '../utils/events.ts'
import type { VoiceRegion } from '../types/voice.ts'
import { fetchAuto } from '../../deps.ts'
import { fetchAuto } from '../utils/fetchBase64.ts'
import type { DMChannel } from '../structures/dmChannel.ts'
import { Template } from '../structures/template.ts'
import { VoiceManager } from './voice.ts'
Expand Down
2 changes: 1 addition & 1 deletion src/managers/guildEmojis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { EmojiPayload } from '../types/emoji.ts'
import { CHANNEL, GUILD_EMOJI, GUILD_EMOJIS } from '../types/endpoint.ts'
import { BaseChildManager } from './baseChild.ts'
import type { EmojisManager } from './emojis.ts'
import { fetchAuto } from '../../deps.ts'
import { fetchAuto } from '../utils/fetchBase64.ts'

export class GuildEmojisManager extends BaseChildManager<EmojiPayload, Emoji> {
guild: Guild
Expand Down
2 changes: 1 addition & 1 deletion src/managers/guilds.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fetchAuto } from '../../deps.ts'
import { fetchAuto } from '../utils/fetchBase64.ts'
import type { Client } from '../client/mod.ts'
import { Guild } from '../structures/guild.ts'
import type { Template } from '../structures/template.ts'
Expand Down
2 changes: 1 addition & 1 deletion src/managers/roles.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Permissions } from '../../mod.ts'
import { fetchAuto } from '../../deps.ts'
import { fetchAuto } from '../utils/fetchBase64.ts'
import type { Client } from '../client/mod.ts'
import type { Guild } from '../structures/guild.ts'
import { Role } from '../structures/role.ts'
Expand Down
2 changes: 1 addition & 1 deletion src/structures/webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Embed } from './embed.ts'
import { Message, MessageAttachment } from './message.ts'
import type { TextChannel } from './textChannel.ts'
import { User } from './user.ts'
import { fetchAuto } from '../../deps.ts'
import { fetchAuto } from '../utils/fetchBase64.ts'
import { WEBHOOK_MESSAGE, CHANNEL_WEBHOOKS } from '../types/endpoint.ts'
import { Constants } from '../types/constants.ts'

Expand Down
39 changes: 39 additions & 0 deletions src/utils/fetchBase64.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { encodeBase64 } from '../../deps.ts'

export const fetchRemote = async (
url: URL,
onlyData = false
): Promise<string> => {
const resp = await fetch(url)
if (resp.status !== 200)
throw new Error(`Request Failed. Server responsed with code ${resp.status}`)
const contentType =
resp.headers.get('content-type') ?? 'application/octet-stream'
const buff = await resp.arrayBuffer()
const data = encodeBase64(buff)
return onlyData ? data : `data:${contentType};base64,${data}`
}

export const fetchLocal = async (
url: string | URL,
onlyData = false
): Promise<string> => {
const file = await Deno.readFile(url)
const data = encodeBase64(file)
const href = typeof url === 'string' ? url : url.href
const contentType = `image/${href.split('.').reverse()[0]}`
return onlyData ? data : `data:${contentType};base64,${data}`
}

export const fetchAuto = async (
path: string,
onlyData = false
): Promise<string> => {
try {
const url = new URL(path)
if (url.protocol.startsWith('http')) return await fetchRemote(url, onlyData)

Check failure on line 34 in src/utils/fetchBase64.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any value in conditional. An explicit comparison or type cast is required
else return await fetchLocal(url, onlyData)
} catch (e) {
return await fetchLocal(path, onlyData)
}
}

0 comments on commit b890958

Please sign in to comment.