-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathutils.js
61 lines (50 loc) · 1.66 KB
/
utils.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
import { mustGetEnv } from '../lib/env.js'
import { CAR_CODE } from './constants.js'
// Per https://developers.cloudflare.com/r2/api/s3/presigned-urls/
export const MAX_EXPIRES_IN = 3 * 24 * 60 * 60 // 7 days in seconds
export const MIN_EXPIRES_IN = 1
export const DEFAULT_EXPIRES_IN = 3 * 24 * 60 * 60 // 3 days in seconds by default
export const VALID_BUCKETS = ['dagcargo']
/**
* @typedef {import('multiformats/cid').CID} CID
**/
/**
* @param {import('aws-lambda').APIGatewayProxyEventPathParameters | undefined} queryStringParameters
*/
export function parseQueryStringParameters (queryStringParameters) {
const expiresIn = queryStringParameters?.expires ?
parseInt(queryStringParameters?.expires) : DEFAULT_EXPIRES_IN
if (expiresIn > MAX_EXPIRES_IN || expiresIn < MIN_EXPIRES_IN) {
throw new Error(`Bad request with not acceptable expires parameter: ${queryStringParameters?.expires}`)
}
const bucketName = queryStringParameters?.bucket
if (bucketName && !VALID_BUCKETS.includes(bucketName)) {
throw new Error(`Bad requested with not acceptable bucket: ${bucketName}`)
}
return {
expiresIn,
bucketName
}
}
/**
* Get Env validating it is set.
*/
export function getEnv() {
return {
BUCKET_ENDPOINT: mustGetEnv('BUCKET_ENDPOINT'),
BUCKET_REGION: mustGetEnv('BUCKET_REGION'),
BUCKET_ACCESS_KEY_ID: mustGetEnv('BUCKET_ACCESS_KEY_ID'),
BUCKET_SECRET_ACCESS_KEY: mustGetEnv('BUCKET_SECRET_ACCESS_KEY'),
BUCKET_NAME: mustGetEnv('BUCKET_NAME')
}
}
/**
* Return the cid if it is a CAR CID or undefined if not
*
* @param {CID} cid
*/
export function asCarCid(cid) {
if (cid.code === CAR_CODE) {
return cid
}
}