Spaces:
Sleeping
Sleeping
File size: 3,287 Bytes
ffc36c3 2b587a6 ffc36c3 2b587a6 270c736 2b587a6 ffc36c3 2b587a6 9b7ecf4 2b587a6 ffc36c3 2b587a6 ffc36c3 9b7ecf4 ffc36c3 2b587a6 ffc36c3 9b7ecf4 2b587a6 ffc36c3 9b7ecf4 2b587a6 ffc36c3 9b7ecf4 ffc36c3 2b587a6 9b7ecf4 ffc36c3 2b587a6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | import { uploadFiles, downloadFile, listFiles, deleteFiles } from '@huggingface/hub'
import { retry } from '../utils/retry.js'
const HF_TOKEN = process.env.HF_TOKEN
const HF_BUCKET_NAME = process.env.HF_BUCKET_NAME // "Discord-Core-Bot/gateway-bot-storage"
if (!HF_TOKEN) throw new Error('Missing required environment variable: HF_TOKEN')
if (!HF_BUCKET_NAME) throw new Error('Missing required environment variable: HF_BUCKET_NAME')
const repo = { type: 'bucket', name: HF_BUCKET_NAME }
const credentials = { accessToken: HF_TOKEN }
async function fetchWithTimeout(url, options, ms = 30000) {
const controller = new AbortController()
const timer = setTimeout(() => controller.abort(), ms)
try {
return await fetch(url, {
...options,
signal: controller.signal
})
} finally {
clearTimeout(timer)
}
}
function isNotFoundError(err) {
const msg = err?.message?.toLowerCase() ?? ''
return (
err?.status === 404 ||
msg.includes('404') ||
msg.includes('not found') ||
msg.includes('entry not found')
)
}
export async function readJSON(remotePath) {
return retry(
async () => {
let response
try {
response = await downloadFile({ repo, path: remotePath, credentials, fetch: fetchWithTimeout })
} catch (err) {
if (isNotFoundError(err)) return null
throw err
}
if (response === null) return null
const buffer = await response.arrayBuffer()
const text = Buffer.from(buffer).toString('utf8')
return JSON.parse(text)
},
{ attempts: 3, label: `readJSON:${remotePath}` }
)
}
export async function writeJSON(remotePath, data) {
return retry(
async () => {
const json = JSON.stringify(data, null, 2)
const blob = new Blob([json], { type: 'application/json' })
await uploadFiles({
repo,
credentials,
files: [{ path: remotePath, content: blob }],
fetch: fetchWithTimeout,
})
},
{ attempts: 3, label: `writeJSON:${remotePath}` }
)
}
export async function uploadBinary(remotePath, buffer, mimeType) {
return retry(
async () => {
const blob = new Blob([buffer], { type: mimeType })
await uploadFiles({
repo,
credentials,
files: [{ path: remotePath, content: blob }],
fetch: fetchWithTimeout,
})
},
{ attempts: 3, label: `uploadBinary:${remotePath}` }
)
}
export async function downloadBinary(remotePath) {
return retry(
async () => {
let response
try {
response = await downloadFile({ repo, path: remotePath, credentials, fetch: fetchWithTimeout })
} catch (err) {
if (isNotFoundError(err)) return null
throw err
}
if (response === null) return null
return Buffer.from(await response.arrayBuffer())
},
{ attempts: 3, label: `downloadBinary:${remotePath}` }
)
}
export async function fileExists(remotePath) {
try {
const response = await downloadFile({ repo, path: remotePath, credentials, fetch: fetchWithTimeout })
return response !== null
} catch (err) {
if (isNotFoundError(err)) return false
throw err
}
} |