Skip to content

Commit c921214

Browse files
committed
blossom caching
1 parent 60c4865 commit c921214

File tree

1 file changed

+11
-57
lines changed

1 file changed

+11
-57
lines changed

src/shared/hooks/useBlossomCache.ts

Lines changed: 11 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -37,28 +37,13 @@ async function fetchFromPeers(hash: string): Promise<ArrayBuffer | null> {
3737

3838
// Try each peer in parallel
3939
const promises = Array.from(connections.values()).map(async (peer) => {
40-
console.log(
41-
`Checking peer ${peer.peerId}: blobChannel=${!!peer.blobChannel}, state=${peer.blobChannel?.readyState}`
42-
)
43-
4440
if (!peer.blobChannel || peer.blobChannel.readyState !== "open") {
45-
console.log(`Peer ${peer.peerId} blob channel not ready`)
4641
return null
4742
}
4843

4944
try {
50-
console.log(`Requesting blob ${hash.slice(0, 8)} from peer ${peer.peerId}`)
51-
const result = await peer.requestBlob(hash)
52-
console.log(
53-
`Result from peer ${peer.peerId}:`,
54-
result ? `${result.byteLength} bytes` : "null"
55-
)
56-
return result
45+
return await peer.requestBlob(hash)
5746
} catch (error) {
58-
console.error(
59-
`Failed to fetch blob ${hash.slice(0, 8)} from peer ${peer.peerId}:`,
60-
error
61-
)
6247
return null
6348
}
6449
})
@@ -80,28 +65,18 @@ async function fetchBlobP2P(
8065
const storage = getBlobStorage()
8166
const cached = await storage.get(hash)
8267
if (cached) {
83-
console.log(`Blob ${hash.slice(0, 8)} found in local cache`)
8468
return new Blob([cached.data], {type: cached.mimeType})
8569
}
8670

8771
// Try peers
88-
const connections = getAllConnections()
89-
console.log(
90-
`Fetching blob ${hash.slice(0, 8)} from peers... (${connections.size} connections)`
91-
)
92-
9372
const data = await fetchFromPeers(hash)
9473
if (data) {
95-
console.log(`Blob ${hash.slice(0, 8)} received from peer, saving with author ${authorPubkey?.slice(0, 8) || "none"}`)
96-
// Store in cache with MIME type and author
9774
await storage.save(hash, data, mimeType, authorPubkey)
9875
return new Blob([data], {type: mimeType})
9976
}
10077

101-
console.log(`Blob ${hash.slice(0, 8)} not found via p2p, falling back to HTTP`)
10278
return null
10379
} catch (error) {
104-
console.error(`Error fetching blob ${hash.slice(0, 8)} via p2p:`, error)
10580
return null
10681
}
10782
}
@@ -168,50 +143,35 @@ export function useBlossomCache(url: string, authorPubkey?: string): string {
168143

169144
useEffect(() => {
170145
const extracted = extractBlossomHash(url)
171-
console.log(
172-
`useBlossomCache: url=${url.slice(0, 60)}, hash=${extracted?.hash.slice(0, 8) || "none"}`
173-
)
174146

175147
if (!extracted || attempted) {
176148
setResolvedUrl(url)
177149
return
178150
}
179151

152+
// Check trust - only fetch blobs from trusted authors (distance <= 2)
153+
const followDistance = authorPubkey ? socialGraph().getFollowDistance(authorPubkey) : 999
154+
const isTrusted = followDistance <= 2
155+
156+
if (!isTrusted) {
157+
setResolvedUrl(url)
158+
return
159+
}
160+
180161
setAttempted(true)
181-
console.log(`useBlossomCache: attempting p2p fetch for ${extracted.hash.slice(0, 8)}`)
182162

183163
// Try p2p fetch
184164
fetchBlobP2P(extracted.hash, extracted.mimeType, authorPubkey).then(async (blob) => {
185165
if (blob) {
186166
// Create object URL for the blob
187167
const objectUrl = URL.createObjectURL(blob)
188-
console.log(
189-
`useBlossomCache: p2p success, using blob: URL for ${extracted.hash.slice(0, 8)}`
190-
)
191168
setResolvedUrl(objectUrl)
192169

193170
// Cleanup on unmount
194171
return () => URL.revokeObjectURL(objectUrl)
195172
}
196173

197-
// P2P failed - check if we should use HTTP fallback with verification
198-
const isTrusted = authorPubkey
199-
? socialGraph().getFollowDistance(authorPubkey) <= 1 ||
200-
socialGraph().getRoot() === authorPubkey
201-
: false
202-
203-
if (!isTrusted) {
204-
console.log(
205-
`useBlossomCache: untrusted author, using proxied HTTP for ${extracted.hash.slice(0, 8)}`
206-
)
207-
setResolvedUrl(url)
208-
return
209-
}
210-
211-
// Fetch via HTTP with verification
212-
console.log(
213-
`useBlossomCache: trusted author, verifying HTTP for ${extracted.hash.slice(0, 8)}`
214-
)
174+
// P2P failed - try HTTP with verification
215175
const verifiedBlob = await fetchAndVerifyHTTP(
216176
url,
217177
extracted.hash,
@@ -221,16 +181,10 @@ export function useBlossomCache(url: string, authorPubkey?: string): string {
221181

222182
if (verifiedBlob) {
223183
const objectUrl = URL.createObjectURL(verifiedBlob)
224-
console.log(
225-
`useBlossomCache: HTTP verified, using blob: URL for ${extracted.hash.slice(0, 8)}`
226-
)
227184
setResolvedUrl(objectUrl)
228185
return () => URL.revokeObjectURL(objectUrl)
229186
} else {
230187
// Verification failed, use original URL (ProxyImg will handle via proxy)
231-
console.log(
232-
`useBlossomCache: HTTP verification failed for ${extracted.hash.slice(0, 8)}`
233-
)
234188
setResolvedUrl(url)
235189
}
236190
})

0 commit comments

Comments
 (0)