-
Notifications
You must be signed in to change notification settings - Fork 152
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
Convert proxy to Cloudflare Pages Function. #840
Open
davidlehn
wants to merge
2
commits into
11ty
Choose a base branch
from
11ty-proxy
base: 11ty
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
*~ | ||
*.backup | ||
*.sw[op] | ||
.DS_Store | ||
.wrangler | ||
node_modules | ||
playground/jsonld.js | ||
_site |
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,68 @@ | ||
// json-ld.org playground proxy | ||
// | ||
// details: | ||
// - Basic proxy for use with the json-ld.org playground. | ||
// - Built for deployment using Cloudflare Pages Functions API | ||
// - https://developers.cloudflare.com/pages/functions/ | ||
// - Only handle GET requests. | ||
// - Only designed to be used by the playground, no CORS support. | ||
// - Only handle requests for 'http:' URLs. | ||
BigBlueHat marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// - Short timeout enough for expected development use cases. | ||
// - Only support JSON-LD and JSON content types for target response. | ||
// - Not intended to be very robust (but improvements welcome). | ||
// | ||
// usage: | ||
// - GET /playground/proxy?url={encoded-url} | ||
// - Proxy errors have JSON content and a | ||
// 'X-JSON-LD-Playground-Proxy-Status' header. | ||
|
||
const RESPONSE_HEADER = 'X-JSON-LD-Playground-Proxy-Status'; | ||
|
||
function makeError({error}) { | ||
return Response.json({error}, { | ||
status: 400, | ||
headers: { | ||
[RESPONSE_HEADER]: '400' | ||
} | ||
}); | ||
} | ||
|
||
export async function onRequestGet(context) { | ||
const request = context.request; | ||
try { | ||
const requestUrl = new URL(request.url); | ||
const targetUrl = new URL(requestUrl.searchParams.get('url')); | ||
// check self request | ||
if(targetUrl.host === requestUrl.host) { | ||
return makeError({error: 'self request'}); | ||
} | ||
// check url protocol | ||
if(targetUrl.protocol !== 'http:') { | ||
return makeError({error: 'unsupported URL protocol'}); | ||
} | ||
// make similar request with new target url | ||
const req = new Request(targetUrl, request); | ||
const res = await fetch(req, { | ||
redirect: 'follow', | ||
// fail for long requests | ||
signal: AbortSignal.timeout(3000) | ||
}); | ||
// check return type is JSON-LD or JSON | ||
const ct = res.headers.get('content-type'); | ||
if(!(ct === 'application/ld+json' || ct === 'application/json')) { | ||
return makeError({error: 'unsupported response content type'}); | ||
} | ||
// check if remote seems to be the proxy itself | ||
if(res.headers.has(RESPONSE_HEADER)) { | ||
return makeError({error: 'playground proxy response found'}); | ||
} | ||
return res; | ||
} catch(e) { | ||
// special case timeout error | ||
if(e.name === 'TimeoutError') { | ||
return makeError({error: 'timeout'}); | ||
} | ||
// fallback error | ||
return makeError({error: 'bad request'}); | ||
} | ||
} |
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 was deleted.
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 was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this be defined above its use on line 61?