Skip to content

Commit

Permalink
feat: set max number of car cids to resolve
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Feb 28, 2023
1 parent ad152f5 commit 9970a01
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/bindings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface Environment {
CARPARK: R2Bucket
DUDEWHERE: R2Bucket
SATNAV: R2Bucket
MAX_CAR_CIDS_TO_RESOLVE: string
}

export interface CarCidsContext extends Context {
Expand Down
7 changes: 7 additions & 0 deletions src/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ export function withCarCids (handler) {
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
7 changes: 7 additions & 0 deletions wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ r2_buckets = [
[env.production.build]
command = "npm run build"

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

# Staging!
[env.staging]
account_id = "fffa4b4363a7e5250af8357087263b3a"
Expand All @@ -40,6 +43,9 @@ r2_buckets = [
[env.staging.build]
command = "npm run build"

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

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

[env.test.vars]
DEBUG = "true"
MAX_CAR_CIDS_TO_RESOLVE = "250"

[env.alanshaw]
workers_dev = true
Expand Down

0 comments on commit 9970a01

Please sign in to comment.