|
| 1 | +import { isEmpty } from 'lodash-es' |
| 2 | +import { Attachment } from '@dimensiondev/common-protocols' |
| 3 | +import { encodeText } from '@masknet/kit' |
| 4 | +import { LANDING_PAGE, Provider } from '../constants.js' |
| 5 | +import type { ProviderAgent, LandingPageMetadata, AttachmentOptions } from '../types.js' |
| 6 | +import { makeFileKeySigned } from '../helpers.js' |
| 7 | + |
| 8 | +const LOAD_GATEWAY_URL = 'https://load0.network/resolve' |
| 9 | +const LOAD_UPLOAD_ENDPOINT = 'https://load0.network/upload' |
| 10 | +const API_KEY = 'load_acc_ep4bep0uGlmUXMu46BxM0uWXKsqL5M5w' // move to env |
| 11 | + |
| 12 | +class LoadAgent implements ProviderAgent { |
| 13 | + static providerName = 'Load Network' |
| 14 | + private uploadController?: AbortController |
| 15 | + |
| 16 | + init() { |
| 17 | + this.uploadController = new AbortController() |
| 18 | + } |
| 19 | + |
| 20 | + async makeAttachment(options: AttachmentOptions) { |
| 21 | + this.init() |
| 22 | + const passphrase = options.key ? encodeText(options.key) : undefined |
| 23 | + const encoded = await Attachment.encode(passphrase, { |
| 24 | + block: options.block, |
| 25 | + mime: isEmpty(options.type) ? 'application/octet-stream' : options.type, |
| 26 | + metadata: null, |
| 27 | + }) |
| 28 | + |
| 29 | + const effectiveType = isEmpty(options.type) ? 'application/octet-stream' : options.type |
| 30 | + const effectiveName = options.name || 'unnamed_file' |
| 31 | + const payloadTxID = await this.makePayload(encoded, effectiveType, effectiveName) |
| 32 | + return payloadTxID |
| 33 | + } |
| 34 | + |
| 35 | + async *upload(id: string) { |
| 36 | + try { |
| 37 | + // Since we're using optimistic upload, we can yield progress immediately |
| 38 | + // The actual upload to Load Network happens in the background |
| 39 | + yield 50 |
| 40 | + yield 100 |
| 41 | + } catch (error) { |
| 42 | + console.error('Upload progress tracking failed:', error) |
| 43 | + throw error |
| 44 | + } finally { |
| 45 | + if (this.uploadController) { |
| 46 | + this.uploadController.abort() |
| 47 | + this.uploadController = undefined |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + async uploadLandingPage(metadata: LandingPageMetadata) { |
| 53 | + this.init() |
| 54 | + const linkPrefix = LOAD_GATEWAY_URL |
| 55 | + const encodedMetadata = JSON.stringify({ |
| 56 | + name: metadata.name, |
| 57 | + size: metadata.size, |
| 58 | + provider: Provider.Load, |
| 59 | + link: `${linkPrefix}/${metadata.txId}`, |
| 60 | + signed: await makeFileKeySigned(metadata.key), |
| 61 | + createdAt: new Date().toISOString(), |
| 62 | + }) |
| 63 | + const response = await fetch(LANDING_PAGE) |
| 64 | + const text = await response.text() |
| 65 | + const replaced = text |
| 66 | + .replace('Arweave', LoadAgent.providerName) |
| 67 | + .replace('Over Arweave', `Over ${LoadAgent.providerName}`) |
| 68 | + .replace('__METADATA__', encodedMetadata) |
| 69 | + |
| 70 | + const data = encodeText(replaced) |
| 71 | + |
| 72 | + const landingPageTxId = await this.makePayload(data, 'text/html', `${metadata.name}-landing.html`) |
| 73 | + |
| 74 | + return landingPageTxId |
| 75 | + } |
| 76 | + |
| 77 | + async makePayload(data: Uint8Array, type: string, fileName: string = 'file.dat') { |
| 78 | + this.init() |
| 79 | + |
| 80 | + try { |
| 81 | + const blob = new Blob([data], { type }) |
| 82 | + const headers = { |
| 83 | + 'Content-Type': type, |
| 84 | + Filename: fileName, |
| 85 | + 'App-Name': 'Maskbook', |
| 86 | + 'X-Load-Authorization': API_KEY, |
| 87 | + } |
| 88 | + |
| 89 | + const response = await fetch(LOAD_UPLOAD_ENDPOINT, { |
| 90 | + method: 'POST', |
| 91 | + headers, |
| 92 | + body: blob, |
| 93 | + signal: this.uploadController?.signal, |
| 94 | + }) |
| 95 | + |
| 96 | + if (!response.ok) { |
| 97 | + throw new Error(`Upload failed: ${response.statusText}`) |
| 98 | + } |
| 99 | + |
| 100 | + const { optimistic_hash } = await response.json() |
| 101 | + return optimistic_hash |
| 102 | + } catch (error) { |
| 103 | + const errorMessage = `Load Network upload failed: ${error instanceof Error ? error.message : String(error)}` |
| 104 | + console.error('Load Network detailed error:', errorMessage) |
| 105 | + |
| 106 | + const enhancedError = new Error(errorMessage) |
| 107 | + if (error instanceof Error && error.stack) { |
| 108 | + enhancedError.stack = error.stack |
| 109 | + } |
| 110 | + |
| 111 | + throw enhancedError |
| 112 | + } |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +export default new LoadAgent() |
0 commit comments