UNPKG

@iexec/dataprotector

Version:

This product enables users to confidentially store data–such as mail address, documents, personal information ...

46 lines 1.67 kB
import { DEFAULT_ARWEAVE_GATEWAY, DEFAULT_ARWEAVE_UPLOAD_API, ARWEAVE_FREE_UPLOAD_MAX_SIZE, } from '../config/config.js'; const add = async (content, { arweaveGateway, arweaveUploadApi } = {}) => { if (content.length >= ARWEAVE_FREE_UPLOAD_MAX_SIZE) { throw Error(`Arweave upload ${(ARWEAVE_FREE_UPLOAD_MAX_SIZE / 1024).toFixed(0)}kb size limit reached`); } let arweaveId; try { const payload = new FormData(); payload.append('file', new Blob([content])); const res = await fetch(`${arweaveUploadApi || DEFAULT_ARWEAVE_UPLOAD_API}/upload`, { method: 'POST', body: payload, }); if (!res.ok) { throw Error(`Arweave upload API answered with status ${res.status}`); } try { const data = await res.json(); arweaveId = data?.arweaveId; if (!arweaveId) { throw Error('missing arweaveId'); } } catch (e) { throw Error('Arweave upload API answered with an invalid response', { cause: e, }); } } catch (e) { throw Error('Failed to add file on Arweave', { cause: e }); } const publicUrl = `${arweaveGateway || DEFAULT_ARWEAVE_GATEWAY}/${arweaveId}`; await fetch(publicUrl) .then((res) => { if (!res.ok) { throw Error(`Arweave gateway answered with status ${res.status}`); } }) .catch((e) => { throw Error(`Failed to load uploaded file at ${publicUrl}`, { cause: e }); }); return arweaveId; }; export { add }; //# sourceMappingURL=arweave.js.map