Spaces:
Runtime error
Runtime error
Download scripts/sync_hf_secrets.js from dazat-knight/nexa-server: direct link, hf CLI and curl.
- Browser
- Download file 2.41 kB
-
https://huggingface.co/spaces/dazat-knight/nexa-server/resolve/main/scripts/sync_hf_secrets.js
- Command line
-
hf download hf://spaces/dazat-knight/nexa-server/scripts/sync_hf_secrets.js
-
curl -L -o sync_hf_secrets.js https://huggingface.co/spaces/dazat-knight/nexa-server/resolve/main/scripts/sync_hf_secrets.js
2.41 kB
| const fs = require('fs'); | |
| const path = require('path'); | |
| const https = require('https'); | |
| // Usage: node scripts/sync_hf_secrets.js <namespace/space-name> <hf-token> | |
| const args = process.argv.slice(2); | |
| if (args.length < 2) { | |
| console.error('Usage: node scripts/sync_hf_secrets.js <namespace/space-name> <hf-token>'); | |
| process.exit(1); | |
| } | |
| const repoId = args[0]; | |
| const token = args[1]; | |
| const envPath = path.join(__dirname, '..', '.env'); | |
| if (!fs.existsSync(envPath)) { | |
| console.error('.env file not found!'); | |
| process.exit(1); | |
| } | |
| const envContent = fs.readFileSync(envPath, 'utf8'); | |
| const lines = envContent.split('\n'); | |
| const secrets = {}; | |
| for (const line of lines) { | |
| const trimmed = line.trim(); | |
| if (!trimmed || trimmed.startsWith('#')) continue; | |
| const idx = trimmed.indexOf('='); | |
| if (idx === -1) continue; | |
| const key = trimmed.substring(0, idx).trim(); | |
| const value = trimmed.substring(idx + 1).trim().replace(/^['"]|['"]$/g, ''); | |
| if (key && value) { | |
| secrets[key] = value; | |
| } | |
| } | |
| console.log(`Found ${Object.keys(secrets).length} secrets in .env. Starting sync to ${repoId}...`); | |
| async function setSecret(key, value) { | |
| return new Promise((resolve, reject) => { | |
| const data = JSON.stringify({ key, value }); | |
| const options = { | |
| hostname: 'huggingface.co', | |
| path: `/api/spaces/${repoId}/secrets`, | |
| method: 'POST', | |
| headers: { | |
| 'Authorization': `Bearer ${token}`, | |
| 'Content-Type': 'application/json', | |
| 'Content-Length': Buffer.byteLength(data) | |
| } | |
| }; | |
| const req = https.request(options, (res) => { | |
| if (res.statusCode === 200 || res.statusCode === 201 || res.statusCode === 400) { | |
| // HF sometimes returns 400 if it already exists or invalid payload, but mostly it works. | |
| resolve(); | |
| } else { | |
| res.on('data', d => reject(new Error(`Failed to set ${key}: ${res.statusCode} ${d}`))); | |
| } | |
| }); | |
| req.on('error', (e) => reject(e)); | |
| req.write(data); | |
| req.end(); | |
| }); | |
| } | |
| async function sync() { | |
| let success = 0; | |
| for (const [key, value] of Object.entries(secrets)) { | |
| try { | |
| await setSecret(key, value); | |
| console.log(`✅ Set secret: ${key}`); | |
| success++; | |
| } catch (e) { | |
| console.error(`❌ Failed to set ${key}: ${e.message}`); | |
| } | |
| } | |
| console.log(`\nSync complete! Successfully synced ${success}/${Object.keys(secrets).length} secrets.`); | |
| } | |
| sync(); | |