Skip to content

Commit

Permalink
fix: ignore non 200 responses when proxying images
Browse files Browse the repository at this point in the history
  • Loading branch information
darkskygit committed Aug 7, 2024
1 parent 63b4e87 commit 81ea7c8
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 314 deletions.
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20231218.0",
"better-sqlite3": "^8.7.0",
"prettier": "3.3.3",
"typescript": "^5.3.3"
"prettier": "3.1.1",
"typescript": "^5.5.4"
},
"packageManager": "[email protected]",
"prettier": {
Expand Down
4 changes: 2 additions & 2 deletions packages/image-proxy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"@cloudflare/workers-types": "^4.20231218.0",
"@oxc-node/core": "^0.0.14",
"@types/node": "^20.10.5",
"tldts": "^6.1.1",
"typescript": "^5.3.3"
"tldts": "^6.1.38",
"typescript": "^5.5.4"
}
}
27 changes: 18 additions & 9 deletions packages/image-proxy/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,22 @@ export async function imageProxy(request: Request) {
},
},
});
const modifiedResponse = new Response(response.body);
modifiedResponse.headers.set('Access-Control-Allow-Origin', request.headers.get('Origin') ?? 'null');
modifiedResponse.headers.set('Vary', 'Origin');
modifiedResponse.headers.set('Access-Control-Allow-Methods', 'GET');
const contentType = response.headers.get('Content-Type');
contentType && modifiedResponse.headers.set('Content-Type', contentType);
const contentDisposition = response.headers.get('Content-Disposition');
contentDisposition && modifiedResponse.headers.set('Content-Disposition', contentDisposition);
return modifiedResponse;
if (response.ok) {
const modifiedResponse = new Response(response.body);
modifiedResponse.headers.set('Access-Control-Allow-Origin', request.headers.get('Origin') ?? 'null');
modifiedResponse.headers.set('Vary', 'Origin');
modifiedResponse.headers.set('Access-Control-Allow-Methods', 'GET');
const contentType = response.headers.get('Content-Type');
contentType && modifiedResponse.headers.set('Content-Type', contentType);
const contentDisposition = response.headers.get('Content-Disposition');
contentDisposition && modifiedResponse.headers.set('Content-Disposition', contentDisposition);
return modifiedResponse;
} else {
log('Failed to fetch image', 'ERROR', {
origin,
url: imageURL,
status: response.status,
});
return respBadRequest('Failed to fetch image', { allowOrigin: origin });
}
}
2 changes: 1 addition & 1 deletion packages/link-preview/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
"@affine/utils": "workspace:*",
"@cloudflare/workers-types": "^4.20231218.0",
"@types/node": "^20.10.5",
"typescript": "^5.3.3"
"typescript": "^5.5.4"
}
}
4 changes: 2 additions & 2 deletions packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"devDependencies": {
"@cloudflare/workers-types": "^4.20231218.0",
"@types/node": "^20.10.5",
"tldts": "^6.1.1",
"typescript": "^5.3.3"
"tldts": "^6.1.38",
"typescript": "^5.5.4"
}
}
8 changes: 4 additions & 4 deletions packages/worker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
"type": "module",
"scripts": {
"deploy": "wrangler deploy -c wrangler.toml",
"dev": "wrangler dev -c wrangler-prod.toml"
"dev": "wrangler dev -c wrangler.toml"
},
"devDependencies": {
"@affine/image-proxy": "workspace:*",
"@affine/link-preview": "workspace:*",
"@affine/utils": "workspace:*",
"@cloudflare/workers-types": "^4.20231218.0",
"@types/node": "^20.10.5",
"itty-router": "4.2.2",
"tldts": "^6.1.1",
"typescript": "^5.3.3",
"itty-router": "^4.2.2",
"tldts": "^6.1.38",
"typescript": "^5.5.4",
"wrangler": "^3.22.1"
}
}
Loading

0 comments on commit 81ea7c8

Please sign in to comment.