-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
67 lines (61 loc) · 1.91 KB
/
index.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { getSignedUrl as getR2SignedUrl } from '@aws-sdk/s3-request-presigner'
import { GetObjectCommand } from '@aws-sdk/client-s3'
import { base58btc } from 'multiformats/bases/base58'
import { RAW_CODE } from './constants.js'
/**
* @typedef {import('multiformats').CID} CID
* @typedef {import('@aws-sdk/client-s3').S3Client} S3Client
* @typedef {import('@aws-sdk/types').RequestPresigningArguments} RequestPresigningArguments
*/
/**
* @param {S3Client} s3Client
* @param {string} bucketName
*/
export function getSigner (s3Client, bucketName) {
return {
/**
*
* @param {string} key
* @param {RequestPresigningArguments} [options]
*/
getUrl: async (key, options) => {
const signedUrl = await getR2SignedUrl(
s3Client,
new GetObjectCommand({
Bucket: bucketName,
Key: key
}),
options
)
return signedUrl
}
}
}
/**
* Creates a helper function that returns signed bucket url for content requested.
* It currently supports both `store/*` and `blob/*` protocol written content.
* Blobs are stored as `b58btc(multihash)/b58btc(multihash).blob` and requested to
* Roundabout via a RAW CID.
* Store protocol SHOULD receive CAR files that are stored as
* `carCid/carCid.car`.
*
* @param {object} config
* @param {S3Client} config.s3Client
* @param {string} config.bucket
* @param {number} config.expiresIn
*/
export function contentLocationResolver ({ s3Client, bucket, expiresIn }) {
const signer = getSigner(s3Client, bucket)
/**
* @param {CID} cid
*/
return async function locateContent (cid) {
const carKey = `${cid}/${cid}.car`
if (cid.code === RAW_CODE) {
const encodedMultihash = base58btc.encode(cid.multihash.bytes)
const blobKey = `${encodedMultihash}/${encodedMultihash}.blob`
return signer.getUrl(blobKey, { expiresIn })
}
return signer.getUrl(carKey, { expiresIn })
}
}