-
Notifications
You must be signed in to change notification settings - Fork 0
/
dudewhere.js
29 lines (28 loc) · 953 Bytes
/
dudewhere.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import { ListObjectsV2Command } from '@aws-sdk/client-s3'
import { base32 } from 'multiformats/bases/base32'
import { CID } from 'multiformats/cid'
/**
* Find dudewhere mapping info for a root cid
*
* @param {string} cidStr
* @param {string} bucket
* @param {import('@aws-sdk/client-s3').S3Client} client
* @param {string} [prefixOverride]
*/
export async function dudewhere (cidStr, client, bucket = 'dudewhere-prod-0', prefixOverride) {
const cid = CID.parse(cidStr)
const prefix = prefixOverride ?? `${cid.toV1().toString(base32)}/`
const cmd = new ListObjectsV2Command({ Bucket: bucket, Prefix: prefix })
const res = await client.send(cmd)
const items = res.Contents ?? []
if (items.length === 0 && cid.version === 0 && !prefixOverride) {
return dudewhere(cidStr, client, bucket, cid.toString())
}
const keys = []
for (const k of items) {
if (k.Key !== undefined) {
keys.push(k.Key)
}
}
return keys
}