-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into jdjaustin/issue-137-error-handling-improvements
- Loading branch information
Showing
33 changed files
with
302 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# http://editorconfig.org | ||
root = true | ||
|
||
[*] | ||
indent_style = tab | ||
tab_width = 2 | ||
end_of_line = lf | ||
charset = utf-8 | ||
trim_trailing_whitespace = true | ||
insert_final_newline = true | ||
|
||
[*.yml] | ||
indent_style = space |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"semi": true, | ||
"printWidth": 100, | ||
"singleQuote": true, | ||
"bracketSpacing": true, | ||
"insertPragma": false, | ||
"requirePragma": false, | ||
"jsxSingleQuote": false, | ||
"bracketSameLine": false, | ||
"embeddedLanguageFormatting": "auto", | ||
"htmlWhitespaceSensitivity": "css", | ||
"vueIndentScriptAndStyle": true, | ||
"quoteProps": "consistent", | ||
"proseWrap": "preserve", | ||
"trailingComma": "es5", | ||
"arrowParens": "avoid", | ||
"useTabs": true, | ||
"tabWidth": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Snapshot redirect worker | ||
|
||
This worker acts on two endpoints: | ||
|
||
- `https://forest-archive.chainsafe.dev/latest/calibnet/` | ||
- `https://forest-archive.chainsafe.dev/latest/mainnet/` | ||
|
||
These links will download the latest available snapshot for calibnet and mainnet, respectively. | ||
|
||
# Local deployment | ||
|
||
Use `wrangler dev` to deploy a local version of this worker which will use the `forest-archive-dev` bucket rather than the production `forest-archive` bucket. Merging changes to this worker will automatically deploy them. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"name": "forest-latest-snapshot", | ||
"version": "0.0.0", | ||
"private": true, | ||
"scripts": { | ||
"deploy": "wrangler deploy src/index.ts", | ||
"dev": "wrangler dev src/index.ts --local", | ||
"start-stackblitz": "WRANGLER_SEND_METRICS=false wrangler dev src/index.ts --local" | ||
}, | ||
"devDependencies": { | ||
"@cloudflare/workers-types": "^4.20230904.0", | ||
"wrangler": "^3.0.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
interface Env { | ||
FOREST_ARCHIVE: R2Bucket, | ||
} | ||
|
||
function basename(path: String) { | ||
return path.split('/').reverse()[0]; | ||
} | ||
|
||
// Directly fetch the data for the latest snapshot of a given chain (eg. calibnet or mainnet) | ||
async function get_latest(env: Env, chain: string): Promise<Response> { | ||
const listed = await env.FOREST_ARCHIVE.list({ prefix: chain + "/latest/" }); | ||
let latest = listed.objects.at(-1); | ||
if (latest == null) { | ||
return new Response(`No latest snapshot found ${chain}`, { | ||
status: 404, | ||
}); | ||
} else { | ||
// Should we support range queries? | ||
const object = await env.FOREST_ARCHIVE.get(latest.key); | ||
if (object === null) { | ||
return new Response('No latest snapshot found', { | ||
status: 404, | ||
}); | ||
} | ||
const headers = new Headers(); | ||
object.writeHttpMetadata(headers); | ||
headers.set('etag', object.httpEtag); | ||
let encoded_name = encodeURIComponent(basename(object.key)); | ||
// Tell browsers and aria2c which filename to use. For 'wget', you have to use `--trust-server-names`. | ||
headers.set('content-disposition', `attachment; filename*=UTF-8''${encoded_name}; filename="${encoded_name}"`); | ||
|
||
return new Response(object.body, { | ||
headers, | ||
}); | ||
} | ||
} | ||
|
||
export default { | ||
async fetch(request: Request, env: Env): Promise<Response> { | ||
const url = new URL(request.url); | ||
const chain = (url.pathname.match(/\/latest\/(\w*)/) || ["undefined"])[1]; | ||
|
||
return await get_latest(env, chain); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"compilerOptions": { | ||
"noEmit": true, | ||
"module": "esnext", | ||
"target": "esnext", | ||
"lib": ["esnext"], | ||
"strict": true, | ||
"moduleResolution": "node", | ||
"types": ["@cloudflare/workers-types", "@types/jest"] | ||
}, | ||
"exclude": ["node_modules", "dist"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
name = "forest-latest-snapshot" | ||
main = "./src/index.ts" | ||
|
||
compatibility_date = "2022-06-30" | ||
|
||
routes = [ | ||
{ pattern = "forest-archive.chainsafe.dev/latest/calibnet/", zone_name = "chainsafe.dev" }, | ||
{ pattern = "forest-archive.chainsafe.dev/latest/mainnet/", zone_name = "chainsafe.dev" }, | ||
] | ||
|
||
[[r2_buckets]] | ||
binding = 'FOREST_ARCHIVE' # can be any valid JavaScript variable name | ||
bucket_name = 'forest-archive' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Deployment | ||
|
||
This worker is automatically deployed when modified. To test locally, run `wrangler dev`. This will run the worker against the development bucket `forest-archive-dev`. Once the worker is deployed to production, it'll use the `forest-archive` bucket. | ||
|
||
# Pruning | ||
|
||
We upload new Filecoin snapshots to CloudFlare every hour and keep only the 10 most recent. The CloudFlare worker script is triggered automatically every hour. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"name": "template-worker-r2", | ||
"version": "0.0.0", | ||
"private": true, | ||
"scripts": { | ||
"deploy": "wrangler deploy src/index.ts", | ||
"dev": "wrangler dev src/index.ts --local", | ||
"start-stackblitz": "WRANGLER_SEND_METRICS=false wrangler dev src/index.ts --local" | ||
}, | ||
"devDependencies": { | ||
"@cloudflare/workers-types": "^4.20230904.0", | ||
"wrangler": "^3.0.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
export interface Env { | ||
FOREST_ARCHIVE: R2Bucket | ||
} | ||
|
||
// Number of recent snapshots to keep. Roughly 1 new snapshot is uploaded every hour. | ||
const KEEP_COUNT: number = 10; | ||
|
||
async function prune(env: Env, chain: string): Promise<String> { | ||
const listed = await env.FOREST_ARCHIVE.list({ prefix: chain + "/latest/" }); | ||
// objects are listed chronologically. Reverse to keep the newest snapshots. | ||
listed.objects.reverse(); | ||
for (let i: number = KEEP_COUNT; i < listed.objects.length; i++) { | ||
await env.FOREST_ARCHIVE.delete(listed.objects[i].key); | ||
} | ||
const pruned = Math.max(listed.objects.length - KEEP_COUNT, 0); | ||
const kept = listed.objects.length - pruned; | ||
return `Pruned: ${pruned}. Kept: ${kept}`; | ||
} | ||
|
||
export default { | ||
async fetch(request: Request, env: Env): Promise<Response> { | ||
const calibnet = await prune(env, 'calibnet'); | ||
const mainnet = await prune(env, 'mainnet'); | ||
return new Response(`Calibnet: ${calibnet}\nMainnet: ${mainnet}\n`); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"compilerOptions": { | ||
"noEmit": true, | ||
"module": "esnext", | ||
"target": "esnext", | ||
"lib": ["esnext"], | ||
"strict": true, | ||
"moduleResolution": "node", | ||
"types": ["@cloudflare/workers-types", "@types/jest"] | ||
}, | ||
"exclude": ["node_modules", "dist"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
name = "forest-prune-latest" | ||
main = "./src/index.ts" | ||
|
||
compatibility_date = "2022-06-30" | ||
|
||
routes = [ | ||
{ pattern = "forest-archive.chainsafe.dev/prune/", zone_name = "chainsafe.dev" }, | ||
] | ||
|
||
[[r2_buckets]] | ||
binding = 'FOREST_ARCHIVE' # can be any valid JavaScript variable name | ||
bucket_name = 'forest-archive' | ||
preview_bucket_name = 'forest-archive-dev' | ||
|
||
[triggers] | ||
crons = ["0 * * * *"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.