Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: batch endpoint check items in parallel #170

Merged
merged 1 commit into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions packages/denylist/src/denylist.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,21 @@ export async function denylistPost (request, env) {
return new Response('Too many items. Max 1000', { status: 400, statusText: 'Bad Request' })
}

const body = []
for (const item of checklist) {
const res = await getFromDenyList(item, env)
if (res) {
body.push(item)
}
}
const res = await Promise.allSettled(checklist.map(item => inDenylist(item, env)))

// @ts-expect-error is ok that value is undefined for errors
const body = res.map(p => p.value).filter(i => !!i)

return new JSONResponse(body)
}

/**
* @param {string} item
* @param {import('./env').Env} env
*/
async function inDenylist (item, env) {
const res = await getFromDenyList(item, env)
if (res) {
return item
}
}
12 changes: 7 additions & 5 deletions packages/denylist/src/utils/denylist.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ import { sha256 } from 'multiformats/hashes/sha2'
* @param {string} cid
*/
export async function toDenyListAnchor (cid) {
const multihash = await sha256.digest(uint8arrays.fromString(`${cid}/`))
const digest = multihash.bytes.subarray(2)
return uint8arrays.toString(digest, 'hex')
const hash = await sha256.encode(uint8arrays.fromString(`${cid}/`))
return uint8arrays.toString(hash, 'hex')
Comment on lines +11 to +12
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated simplification that was bugging me

}

/**
Expand All @@ -22,13 +21,16 @@ export async function toDenyListAnchor (cid) {
export async function getFromDenyList (cid, env) {
const datastore = env.DENYLIST
if (!datastore) {
throw new Error('db not ready')
throw new Error('DENYLIST kv binding missing')
}

const anchor = await toDenyListAnchor(cid)
// TODO: Remove once https://github.com/nftstorage/nftstorage.link/issues/51 is fixed
return await pRetry(
() => datastore.get(anchor),
{ retries: 5 }
{
retries: 5,
onFailedAttempt: console.log
}
)
}
2 changes: 1 addition & 1 deletion packages/denylist/wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ routes = [
{ pattern = "denylist-staging.dag.haus/*", zone_id = "f2f8a5b1c557202c6e3d0ce0e98e4c8e" }
]
kv_namespaces = [
{ binding = "DENYLIST", id = "f4eb0eca32e14e28b643604a82e00cb3" }
{ binding = "DENYLIST", id = "f4eb0eca32e14e28b643604a82e00cb3", preview_id = "f4eb0eca32e14e28b643604a82e00cb3" }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this lets try out changes against the staging kv with

wrangler dev --remote --env staging

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh nice, did not know this

]

[env.staging.vars]
Expand Down