Prefer dufflepud if enabled
This commit is contained in:
@@ -20,13 +20,9 @@ export const collection = <T, LoadArgs extends any[]>({
|
|||||||
const loadAttempts = new Map<string, number>()
|
const loadAttempts = new Map<string, number>()
|
||||||
|
|
||||||
const loadItem = async (key: string, ...args: LoadArgs) => {
|
const loadItem = async (key: string, ...args: LoadArgs) => {
|
||||||
const item = undefined//indexStore.get().get(key)
|
const item = indexStore.get().get(key)
|
||||||
const freshness = getFreshness(name, key)
|
const freshness = getFreshness(name, key)
|
||||||
|
|
||||||
if (name === 'zappers' && key === '6e468422dfb74a5738702a8823b9b28168abab8655faacb6853cd0ee15deee93') {
|
|
||||||
console.log(item)
|
|
||||||
}
|
|
||||||
|
|
||||||
// If we have an item, reload anyway if it's stale. If not, retry with exponential backoff
|
// If we have an item, reload anyway if it's stale. If not, retry with exponential backoff
|
||||||
if (item) {
|
if (item) {
|
||||||
loadAttempts.delete(key)
|
loadAttempts.delete(key)
|
||||||
|
|||||||
+13
-19
@@ -47,29 +47,23 @@ export const fetchHandles = async (nip05s: string[]) => {
|
|||||||
const base = ctx.app.dufflepudUrl!
|
const base = ctx.app.dufflepudUrl!
|
||||||
const handlesByNip05 = new Map<string, Handle>()
|
const handlesByNip05 = new Map<string, Handle>()
|
||||||
|
|
||||||
// Attempt fetching directly first
|
// Use dufflepud if we it's set up to protect user privacy, otherwise fetch directly
|
||||||
const results = await Promise.all(
|
if (base) {
|
||||||
nip05s.map(async nip05 => ({nip05, info: await queryProfile(nip05)}))
|
const res: any = await postJson(`${base}/handle/info`, {handles: nip05s})
|
||||||
)
|
|
||||||
|
|
||||||
const dufflepudNip05s: string[] = []
|
|
||||||
|
|
||||||
// If we got a response, great, if not (due to CORS), proxy via dufflepud
|
|
||||||
for (const {nip05, info} of results) {
|
|
||||||
if (info) {
|
|
||||||
handlesByNip05.set(nip05, info)
|
|
||||||
} else {
|
|
||||||
dufflepudNip05s.push(nip05)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fetch via dufflepud if we have an endpoint
|
|
||||||
if (base && dufflepudNip05s.length > 0) {
|
|
||||||
const res: any = await postJson(`${base}/handle/info`, {handles: dufflepudNip05s})
|
|
||||||
|
|
||||||
for (const {handle: nip05, info} of res?.data || []) {
|
for (const {handle: nip05, info} of res?.data || []) {
|
||||||
handlesByNip05.set(nip05, info)
|
handlesByNip05.set(nip05, info)
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
const results = await Promise.all(
|
||||||
|
nip05s.map(async nip05 => ({nip05, info: await queryProfile(nip05)}))
|
||||||
|
)
|
||||||
|
|
||||||
|
for (const {nip05, info} of results) {
|
||||||
|
if (info) {
|
||||||
|
handlesByNip05.set(nip05, info)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return handlesByNip05
|
return handlesByNip05
|
||||||
|
|||||||
+18
-24
@@ -12,34 +12,28 @@ export const fetchZappers = async (lnurls: string[]) => {
|
|||||||
const base = ctx.app.dufflepudUrl!
|
const base = ctx.app.dufflepudUrl!
|
||||||
const zappersByLnurl = new Map<string, Zapper>()
|
const zappersByLnurl = new Map<string, Zapper>()
|
||||||
|
|
||||||
// Attempt fetching directly first
|
// Use dufflepud if we it's set up to protect user privacy, otherwise fetch directly
|
||||||
const results = await Promise.all(
|
if (base) {
|
||||||
lnurls.map(async lnurl => {
|
const res: any = await postJson(`${base}/zapper/info`, {lnurls})
|
||||||
const hexUrl = tryCatch(() => bech32ToHex(lnurl))
|
|
||||||
const info = hexUrl ? await fetchJson(hexUrl) : undefined
|
|
||||||
|
|
||||||
return {lnurl, hexUrl, info}
|
|
||||||
})
|
|
||||||
)
|
|
||||||
|
|
||||||
const dufflepudLnurls: string[] = []
|
|
||||||
|
|
||||||
// If we got a response, great, if not (due to CORS), proxy via dufflepud
|
|
||||||
for (const {lnurl, hexUrl, info} of results) {
|
|
||||||
if (info) {
|
|
||||||
zappersByLnurl.set(lnurl, info)
|
|
||||||
} else if (hexUrl) {
|
|
||||||
dufflepudLnurls.push(hexUrl)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fetch via dufflepud if we have an endpoint
|
|
||||||
if (base && dufflepudLnurls.length > 0) {
|
|
||||||
const res: any = await postJson(`${base}/zapper/info`, {lnurls: dufflepudLnurls})
|
|
||||||
|
|
||||||
for (const {lnurl, info} of res?.data || []) {
|
for (const {lnurl, info} of res?.data || []) {
|
||||||
tryCatch(() => zappersByLnurl.set(hexToBech32("lnurl", lnurl), info))
|
tryCatch(() => zappersByLnurl.set(hexToBech32("lnurl", lnurl), info))
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
const results = await Promise.all(
|
||||||
|
lnurls.map(async lnurl => {
|
||||||
|
const hexUrl = tryCatch(() => bech32ToHex(lnurl))
|
||||||
|
const info = hexUrl ? await fetchJson(hexUrl) : undefined
|
||||||
|
|
||||||
|
return {lnurl, hexUrl, info}
|
||||||
|
})
|
||||||
|
)
|
||||||
|
|
||||||
|
for (const {lnurl, info} of results) {
|
||||||
|
if (info) {
|
||||||
|
zappersByLnurl.set(lnurl, info)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return zappersByLnurl
|
return zappersByLnurl
|
||||||
|
|||||||
Reference in New Issue
Block a user