Skip to content

Commit

Permalink
chore: apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: Alan Shaw <[email protected]>
  • Loading branch information
vasco-santos and Alan Shaw committed Mar 1, 2023
1 parent 1203d40 commit 9d4c9ff
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion src/bindings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface Environment {
CARPARK: R2Bucket
DUDEWHERE: R2Bucket
SATNAV: R2Bucket
MAX_CAR_CIDS_TO_RESOLVE: string
MAX_SHARDS: string
}

export interface CarCidsContext extends Context {
Expand Down
21 changes: 14 additions & 7 deletions src/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ export function withCarCids (handler) {
if (!ctx.dataCid) throw new Error('missing data CID')
if (!ctx.searchParams) throw new Error('missing URL search params')

// Cloudflare currently sets a limit of 1000 sub-requests within the worker context
// If we have a given root CID splitted across hundreds of CARs, freeway will hit
// the sub-requests limit and not serve content anyway
const maxShards = env.MAX_SHARDS ? parseInt(env.MAX_SHARDS) : 250

const carCids = ctx.searchParams.getAll('origin').flatMap(str => {
return str.split(',')
.reduce((/** @type {import('multiformats').CID[]} */cids, str) => {
Expand All @@ -62,23 +67,25 @@ export function withCarCids (handler) {
const results = await env.DUDEWHERE.list({ prefix: `${ctx.dataCid}/`, cursor })
if (!results || !results.objects.length) break
carCids.push(...results.objects.map(o => parseCid(o.key.split('/')[1])))

if (carCids.length > maxShards) {
throw new HttpError('request exceeds maximum DAG shards', { status: 501 })
}

if (!results.truncated) break
cursor = results.cursor
}
console.log(`dude where's my CAR? ${ctx.dataCid} => ${carCids}`)
} else {
if (carCids.length > maxShards) {
throw new HttpError('request exceeds maximum DAG shards', { status: 501 })
}
}

if (!carCids.length) {
throw new HttpError('missing origin CAR CID(s)', { status: 400 })
}

// Cloudflare currently sets a limit of 1000 sub-requests within the worker context
// If we have a given root CID splitted across hundreds of CARs, freeway will hit
// the sub-requests limit and not serve content anyway
if (carCids.length > Number(env.MAX_CAR_CIDS_TO_RESOLVE)) {
throw new HttpError('number CAR CIDs is too large to resolve', { status: 501 })
}

return handler(request, env, { ...ctx, carCids })
}
}
Expand Down
6 changes: 3 additions & 3 deletions wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ r2_buckets = [
command = "npm run build"

[env.production.vars]
MAX_CAR_CIDS_TO_RESOLVE = "250"
MAX_SHARDS = "250"

# Staging!
[env.staging]
Expand All @@ -44,7 +44,7 @@ r2_buckets = [
command = "npm run build"

[env.staging.vars]
MAX_CAR_CIDS_TO_RESOLVE = "250"
MAX_SHARDS = "250"

# Test!
[env.test]
Expand All @@ -57,7 +57,7 @@ r2_buckets = [

[env.test.vars]
DEBUG = "true"
MAX_CAR_CIDS_TO_RESOLVE = "120"
MAX_SHARDS = "120"

[env.alanshaw]
workers_dev = true
Expand Down

0 comments on commit 9d4c9ff

Please sign in to comment.