const axios = require('axios'); const { v4: uuidv4 } = require('uuid'); async function imgupscale(image, { scale = 4 } = {}) { try { const scales = [1, 4, 8, 16]; if (!Buffer.isBuffer(image)) throw new Error('Image must be a buffer.'); if (!scales.includes(scale) || isNaN(scale)) throw new Error(`Available scale options: ${scales.join(', ')}.`); const identity = uuidv4(); const inst = axios.create({ baseURL: 'https://supawork.ai/supawork/headshot/api', headers: { authorization: 'null', origin: 'https://supawork.ai/', referer: 'https://supawork.ai/ai-photo-enhancer', 'user-agent': 'Mozilla/5.0 (Linux; Android 15; SM-F958 Build/AP3A.240905.015) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.6723.86 Mobile Safari/537.36', 'x-auth-challenge': '', 'x-identity-id': identity } }); const { data: up } = await inst.get('/sys/oss/token', { params: { f_suffix: 'png', get_num: 1, unsafe: 1 } }); const img = up?.data?.[0]; if (!img) throw new Error('Upload url not found.'); await axios.put(img.put, image); const { data: cf } = await axios.post('https://api.nekolabs.web.id/tools/bypass/cf-turnstile', { url: 'https://supawork.ai/ai-photo-enhancer', siteKey: '0x4AAAAAACBjrLhJyEE6mq1c' }); if (!cf?.result) throw new Error('Failed to get cf token.'); const { data: t } = await inst.get('/sys/challenge/token', { headers: { 'x-auth-challenge': cf.result } }); if (!t?.data?.challenge_token) throw new Error('Failed to get token.'); const { data: task } = await inst.post('/media/image/generator', { aigc_app_code: 'image_enhancer', model_code: 'supawork-ai', image_urls: [img.get], extra_params: { scale: parseInt(scale) }, currency_type: 'silver', identity_id: identity }, { headers: { 'x-auth-challenge': t.data.challenge_token } }); if (!task?.data?.creation_id) throw new Error('Failed to create task.'); while (true) { const { data } = await inst.get('/media/aigc/result/list/v1', { params: { page_no: 1, page_size: 10, identity_id: identity } }); const list = data?.data?.list?.[0]?.list?.[0]; if (list.status === 1) return list.url; await new Promise(res => setTimeout(res, 1000)); } } catch (error) { throw new Error(error.message); } } // Usage: const fs = require('fs'); imgupscale(fs.readFileSync('./ex/image.jpg'), { scale: 16 }).then(console.log);