Skip to content

Commit

Permalink
Merge pull request #998 from serlo/staging
Browse files Browse the repository at this point in the history
Deployment
  • Loading branch information
hugotiburtino authored Feb 6, 2025
2 parents c74574d + 1348b10 commit 856bf00
Show file tree
Hide file tree
Showing 6 changed files with 251 additions and 1,393 deletions.
2 changes: 1 addition & 1 deletion __tests__/__utils__/expect-helper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SeverityLevel } from '@sentry/types'
import { SeverityLevel } from '@sentry/core'

export async function expectContainsText(response: Response, texts: string[]) {
expect(response).not.toBeNull()
Expand Down
39 changes: 35 additions & 4 deletions __tests__/asset-proxy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { bypass, http } from 'msw'
import { http } from 'msw'

import {
currentTestEnvironment,
Expand All @@ -21,11 +21,20 @@ beforeEach(() => {
headers: { 'content-type': 'application/json' },
})
}),
// it would be better to internally fake the response, but for some reason msw does not handle Content-Encoding header correctly
http.get(
'https://upload.wikimedia.org/wikipedia/commons/8/8b/Sinus_mit_y.svg',
async ({ request }) => {
return await fetch(bypass(new Request(request.url, request)))
() => {
return new Response('', {
headers: { 'content-type': 'image/svg+xml' },
})
},
),
http.get(
'https://cdn.pixabay.com/photo/2018/06/27/16/56/minimal-3502044_1280.jpg',
() => {
return new Response('', {
headers: { 'content-type': 'binary/octet-stream' },
})
},
),
)
Expand Down Expand Up @@ -67,6 +76,28 @@ test('request to asset-proxy works also in case of other encodings', async () =>
)
})

test('request to asset-proxy works for pixabays CDN even if its response content-type is not image ', async () => {
const env = currentTestEnvironment()
const response = await env.fetch(
{
subdomain: 'asset-proxy',
pathname:
'/image?url=https://cdn.pixabay.com/photo/2018/06/27/16/56/minimal-3502044_1280.jpg',
},
{
headers: {
'Accept-Encoding': '*',
},
},
)
expect(response.status).toBe(200)
expect(response.headers.get('content-type')).toBe('binary/octet-stream')
expect(response.headers.get('Set-Cookie')).toBeNull()
expect(response.headers.get('cache-control')).toBe(
'public, max-age=31536000, immutable',
)
})

describe('returns placeholder', () => {
test('when url parameter is empty', async () => {
const response = await requestAsset('')
Expand Down
2 changes: 1 addition & 1 deletion jest.setup.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// eslint-disable-next-line import/no-unassigned-import
import '@testing-library/jest-dom'
import { jest } from '@jest/globals'
import { type Event as SentryEvent } from '@sentry/types'
import { type Event as SentryEvent } from '@sentry/core'
import * as cryptoNode from 'crypto'
import { http } from 'msw'
import { setupServer } from 'msw/node'
Expand Down
20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,24 @@
"fp-ts": "^2.16.9",
"io-ts": "^2.2.22",
"jose": "^5.9.6",
"toucan-js": "^4.0.0"
"toucan-js": "^4.1.0"
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20250109.0",
"@eslint/compat": "^1.2.5",
"@cloudflare/workers-types": "^4.20250129.0",
"@eslint/compat": "^1.2.6",
"@eslint/eslintrc": "^3.2.0",
"@eslint/js": "^9.17.0",
"@eslint/js": "^9.19.0",
"@iarna/toml": "^2.2.5",
"@jest/globals": "^29.7.0",
"@sentry/types": "^8.50.0",
"@sentry/core": "^8.54.0",
"@testing-library/jest-dom": "^6.6.3",
"@types/iarna__toml": "^2.0.5",
"@types/jest": "^29.5.14",
"@typescript-eslint/eslint-plugin": "^8.20.0",
"@typescript-eslint/parser": "^8.20.0",
"@typescript-eslint/eslint-plugin": "^8.23.0",
"@typescript-eslint/parser": "^8.23.0",
"cross-env": "^7.0.3",
"depcheck": "^1.4.7",
"eslint": "^9.18.0",
"eslint": "^9.19.0",
"eslint-config-prettier": "^10.0.1",
"eslint-plugin-deprecation": "^3.0.0",
"eslint-plugin-import": "^2.31.0",
Expand All @@ -64,8 +64,8 @@
"prettier-plugin-sh": "^0.14.0",
"ts-jest": "^29.2.5",
"ts-unused-exports": "^11.0.1",
"typescript": "^5.7.2",
"wrangler": "^3.103.2"
"typescript": "^5.7.3",
"wrangler": "^3.105.1"
},
"packageManager": "[email protected]",
"engines": {
Expand Down
16 changes: 15 additions & 1 deletion src/asset-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ export async function assetProxy(request: Request): Promise<Response | null> {
},
)

if (originalResponse.ok && isImageResponse(originalResponse)) {
if (
originalResponse.ok &&
(isImageResponse(originalResponse) || isFromPixabayCdn(originalResponse))
) {
const response = new Response(originalResponse.body, originalResponse)
response.headers.delete('set-cookie')
response.headers.set('cache-control', 'public, max-age=31536000, immutable')
Expand All @@ -34,3 +37,14 @@ export async function assetProxy(request: Request): Promise<Response | null> {

return getPlaceholder()
}

function isFromPixabayCdn(response: Response) {
const url = new Url(response.url)
const contentType = response.headers.get('content-type')

return (
response.status === 200 &&
`${url.subdomain}.${url.domain}` === 'cdn.pixabay.com' &&
contentType === 'binary/octet-stream'
)
}
Loading

0 comments on commit 856bf00

Please sign in to comment.