Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add /reproduce-cloudflare-error route to access-api #380

Merged
merged 1 commit into from
Jan 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions packages/access-api/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ r.add('get', '/validate-email', validateEmail)
r.add('get', '/validate-ws', validateWS)
r.add('post', '/', postRoot)
r.add('post', '/raw', postRaw)
r.add('get', '/reproduce-cloudflare-error', reproduceCloudflareError)

/** @type {import('./bindings.js').ModuleWorker} */
const worker = {
Expand All @@ -39,3 +40,30 @@ const worker = {
}

export default worker

/**
* @param {import('@web3-storage/worker-utils/router').ParsedRequest} request
* @returns
*/
async function reproduceCloudflareError(request) {
const fetchUrl = request.query.url || 'https://up.web3.storage'
let fetchedResponse
try {
fetchedResponse = await fetch(fetchUrl)
} catch (error) {
const message = `/reproduce-cloudflare-error fetch ${fetchUrl} threw unexpected error: ${error}`
// eslint-disable-next-line no-console
console.error(message, error)
return new Response(JSON.stringify({ message }, undefined, 2), {
status: 500,
})
}
const response = {
message: `got response from fetching ${fetchUrl}`,
response: {
status: fetchedResponse.status,
statusText: fetchedResponse.statusText,
},
}
return new Response(JSON.stringify(response, undefined, 2), { status: 200 })
}