diff options
| author | Travis Fischer <[email protected]> | 2021-06-02 13:59:27 -0400 |
|---|---|---|
| committer | Travis Fischer <[email protected]> | 2021-06-02 13:59:27 -0400 |
| commit | f6321fc3249d83a0059ef47978ed101d3c75375f (patch) | |
| tree | 9da8b59a0c94c08f92506e57653b8c9f55fa85e8 /src/resolve-request.js | |
| download | cf-image-proxy-f6321fc3249d83a0059ef47978ed101d3c75375f.tar.xz cf-image-proxy-f6321fc3249d83a0059ef47978ed101d3c75375f.zip | |
feat: import from notion2site repo
Diffstat (limited to 'src/resolve-request.js')
| -rw-r--r-- | src/resolve-request.js | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/src/resolve-request.js b/src/resolve-request.js new file mode 100644 index 0000000..75f1f11 --- /dev/null +++ b/src/resolve-request.js @@ -0,0 +1,40 @@ +const notionImagePrefix = 'https://www.notion.so/image/' + +/** + * @param {*} event + * @param {*} request + */ +export async function resolveRequest(event, request) { + const requestUrl = new URL(request.url) + let originUri = decodeURIComponent(requestUrl.pathname.slice(1)) + + // special-case optimization for notion images coming from unsplash + if (originUri.startsWith(notionImagePrefix)) { + const imageUri = decodeURIComponent( + originUri.slice(notionImagePrefix.length) + ) + const imageUrl = new URL(imageUri) + + // adjust unsplash defaults to have a max width and intelligent format conversion + if (imageUrl.hostname === 'images.unsplash.com') { + const { searchParams } = imageUrl + + if (!searchParams.has('w') && !searchParams.has('fit')) { + imageUrl.searchParams.set('w', 1920) + imageUrl.searchParams.set('fit', 'max') + } + + if (!searchParams.has('auto')) { + imageUrl.searchParams.set('auto', 'format') + } + + originUri = `${notionImagePrefix}${encodeURIComponent( + imageUrl.toString() + )}` + } + } + + const originReq = new Request(originUri, request) + + return { originReq } +} |