Skip to content

Commit

Permalink
test: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan Shaw committed Sep 30, 2024
1 parent 74f0fdb commit 38e91d4
Show file tree
Hide file tree
Showing 13 changed files with 462 additions and 76 deletions.
33 changes: 30 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 43 additions & 16 deletions psa/config.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,53 @@
import { base32 } from 'multiformats/bases/base32'
import { S3Client } from '@aws-sdk/client-s3'
import { createDudeWhereLocator, createHashEncodedInKeyHasher, createObjectHasher, createObjectLocator } from './lib.js'
import { mustGetEnv } from '../lib/env.js'

/** @type {import('../lib.js').Bucket[]} */
/** @type {import('./lib.js').Bucket[]} */
export const buckets = [
{
name: process.env.S3_DOTSTORAGE_0_BUCKET_NAME,
region: process.env.S3_DOTSTORAGE_0_BUCKET_REGION,
toKey: root => {
const s = root.toV1().toString(base32)
return `complete/${s}/${s}.car`
}
locator: createObjectLocator(
new S3Client({ region: mustGetEnv('S3_DOTSTORAGE_0_BUCKET_REGION') }),
mustGetEnv('S3_DOTSTORAGE_0_BUCKET_NAME'),
root => {
const s = root.toV1().toString(base32)
return `complete/${s}/${s}.car`
}
),
hasher: createObjectHasher()
},
{
name: process.env.S3_DOTSTORAGE_1_BUCKET_NAME,
region: process.env.S3_DOTSTORAGE_1_BUCKET_REGION,
toKey: root => {
const s = root.toV1().toString(base32)
return `complete/${s}/${s}.car`
}
locator: createObjectLocator(
new S3Client({ region: mustGetEnv('S3_DOTSTORAGE_1_BUCKET_REGION') }),
mustGetEnv('S3_DOTSTORAGE_1_BUCKET_NAME'),
root => {
const s = root.toV1().toString(base32)
return `complete/${s}/${s}.car`
}
),
hasher: createObjectHasher()
},
{
name: process.env.S3_PICKUP_BUCKET_NAME,
region: process.env.S3_PICKUP_BUCKET_REGION,
toKey: r => `pickup/${r}/${r}.root.car`
locator: createObjectLocator(
new S3Client({ region: mustGetEnv('S3_PICKUP_BUCKET_REGION') }),
mustGetEnv('S3_PICKUP_BUCKET_NAME'),
r => `pickup/${r}/${r}.root.car`
),
hasher: createObjectHasher()
},
{
locator: createDudeWhereLocator(
new S3Client({
endpoint: mustGetEnv('R2_ENDPOINT'),
credentials: {
accessKeyId: mustGetEnv('R2_ACCESS_KEY_ID'),
secretAccessKey: mustGetEnv('R2_SECRET_ACCESS_KEY'),
},
region: mustGetEnv('R2_REGION')
}),
mustGetEnv('R2_DUDEWHERE_BUCKET_NAME'),
mustGetEnv('R2_CARPARK_BUCKET_NAME')
),
hasher: createHashEncodedInKeyHasher()
}
]
11 changes: 6 additions & 5 deletions psa/functions/download.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import { ApiHandler } from 'sst/node/api'
import * as Link from 'multiformats/link'
import { getDownloadURL, NotFound } from '../lib.js'
import { errorResponse } from './lib/util.js'
import * as Config from '../config.js'
import { errorResponse, okResponse } from '../util.js'

export const handler = ApiHandler(async event => {
const { searchParams } = new URL(`http://localhost/?${event.rawQueryString}`)

let root
try {
root = Link.parse(searchParams.get('root'))
} catch (err) {
root = Link.parse(searchParams.get('root') ?? '')
} catch {
return errorResponse('Invalid "root" search parameter', 400)
}

try {
const url = await getDownloadURL(buckets, root)
const url = await getDownloadURL(Config.buckets, root)
return okResponse({ root, url })
} catch (err) {
} catch (/** @type {any} */ err) {
return errorResponse(err.message, err instanceof NotFound ? 404 : 500)
}
})
6 changes: 3 additions & 3 deletions psa/functions/hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ export const handler = ApiHandler(async event => {

let root
try {
root = Link.parse(searchParams.get('root'))
} catch (err) {
root = Link.parse(searchParams.get('root') ?? '')
} catch {
return errorResponse('Invalid "root" search parameter', 400)
}

try {
const { link, size } = await getHash(Config.buckets, root)
return okResponse({ root, link, size })
} catch (err) {
} catch (/** @type {any} */ err) {
return errorResponse(err.message, err instanceof NotFound ? 404 : 500)
}
})
Loading

0 comments on commit 38e91d4

Please sign in to comment.