Skip to content

Commit

Permalink
Merge pull request #619 from serlo/patch
Browse files Browse the repository at this point in the history
refactor: Move all functions to separate files
  • Loading branch information
hugotiburtino authored Dec 11, 2023
2 parents 656ffe3 + 71981c0 commit 2086c1c
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 56 deletions.
15 changes: 0 additions & 15 deletions __tests__/index.ts → __tests__/http-to-https.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import {
mockHttpGet,
returnsText,
givenUuid,
currentTestEnvironment,
expectToBeRedirectTo,
localTestEnvironment,
} from './__utils__'
import { Instance } from '../src/utils'

Expand All @@ -29,16 +26,4 @@ describe('Enforce HTTPS', () => {

expect(await response.text()).toEqual(expect.stringContaining('Startseite'))
})

test('Pact Broker', async () => {
const local = localTestEnvironment()
mockHttpGet(
local.createUrl({ subdomain: 'pacts', pathname: '/bar' }),
returnsText('content'),
)

const response = await local.fetch({ subdomain: 'pacts', pathname: '/bar' })

expect(await response.text()).toBe('content')
})
})
21 changes: 21 additions & 0 deletions src/assets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Url } from './utils'

export async function semanticFileNames(request: Request) {
const url = Url.fromRequest(request)

if (url.subdomain !== 'assets') return null

url.host = 'assets.serlo.org'

const re = /^\/(legacy\/|)((?!legacy)[\w-]+)\/([\w\-+]+)\.(\w+)$/
const match = re.exec(url.pathname)

if (!url.pathname.startsWith('/meta') && match) {
const prefix = match[1]
const hash = match[2]
const extension = match[4]

url.pathname = `${prefix}${hash}.${extension}`
}
return await fetch(url.href, request)
}
9 changes: 9 additions & 0 deletions src/http-to-https.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Url } from './utils'

export async function enforceHttps(request: Request) {
const url = Url.fromRequest(request)
if (url.subdomain === 'pacts') return null
if (url.protocol !== 'http:') return null
url.protocol = 'https:'
return Promise.resolve(url.toRedirect(301))
}
45 changes: 4 additions & 41 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { api } from './api'
import { semanticFileNames } from './assets'
import { auth } from './auth'
import { CFEnvironment } from './cf-environment'
import { cloudflareWorkerDev } from './cloudflare-worker-dev'
import { embed } from './embed'
import { frontendProxy, frontendSpecialPaths } from './frontend-proxy'
import { enforceHttps } from './http-to-https'
import { pdfProxy } from './pdf-proxy'
import { quickbarProxy } from './quickbar-proxy'
import { redirects } from './redirects'
import { robotsTxt } from './robots'
import { SentryFactory, Url } from './utils'
import { sentryHelloWorld } from './sentry'
import { SentryFactory } from './utils'

// eslint-disable-next-line import/no-default-export
export default {
Expand Down Expand Up @@ -38,43 +41,3 @@ export default {
}
},
}

function sentryHelloWorld(request: Request, sentryFactory: SentryFactory) {
const url = Url.fromRequest(request)

if (url.subdomain !== '') return null
if (url.pathname !== '/sentry-report-hello-world') return null

const sentry = sentryFactory.createReporter('sentry-hello-world')
sentry.captureMessage('Hello World!', 'info')

return new Response('Hello-World message send to sentry')
}

async function enforceHttps(request: Request) {
const url = Url.fromRequest(request)
if (url.subdomain === 'pacts') return null
if (url.protocol !== 'http:') return null
url.protocol = 'https:'
return Promise.resolve(url.toRedirect(301))
}

async function semanticFileNames(request: Request) {
const url = Url.fromRequest(request)

if (url.subdomain !== 'assets') return null

url.host = 'assets.serlo.org'

const re = /^\/(legacy\/|)((?!legacy)[\w-]+)\/([\w\-+]+)\.(\w+)$/
const match = re.exec(url.pathname)

if (!url.pathname.startsWith('/meta') && match) {
const prefix = match[1]
const hash = match[2]
const extension = match[4]

url.pathname = `${prefix}${hash}.${extension}`
}
return await fetch(url.href, request)
}
16 changes: 16 additions & 0 deletions src/sentry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { SentryFactory, Url } from './utils'

export function sentryHelloWorld(
request: Request,
sentryFactory: SentryFactory,
) {
const url = Url.fromRequest(request)

if (url.subdomain !== '') return null
if (url.pathname !== '/sentry-report-hello-world') return null

const sentry = sentryFactory.createReporter('sentry-hello-world')
sentry.captureMessage('Hello World!', 'info')

return new Response('Hello-World message send to sentry')
}

0 comments on commit 2086c1c

Please sign in to comment.