Skip to content

Commit

Permalink
fix: improve endpoint creation
Browse files Browse the repository at this point in the history
  • Loading branch information
TheUnderScorer committed Dec 13, 2023
1 parent 5784bb9 commit dd84407
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
39 changes: 39 additions & 0 deletions proxy/handlers/ingress.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,45 @@ describe('Result Endpoint', function () {
expect(ingress.handleIngress).toHaveBeenCalledTimes(0)
expect(https.request).toHaveBeenCalledTimes(0)
})

test.each(['invalid', 'usa', 'EU', 'US', 'AP', ''])(
'Should set default (US) region when invalid region is provided in query parameter: %s',
async (region) => {
const req = mockRequestGet('https://fp.domain.com', 'fpjs/resultId', {
region,
})

requestSpy.mockImplementationOnce((...args: any[]): any => {
const [url, , callback] = args
expect(url.origin).toBe(origin)

const response = new EventEmitter()
const request = new EventEmitter()

Object.assign(response, {
headers: {},
})

Object.assign(request, {
end: jest.fn(),
write: jest.fn(),
})

callback(response)

setTimeout(() => {
response.emit('data', Buffer.from('data', 'utf-8'))
response.emit('end')
}, 10)

return request
})

const ctx = mockContext(req)

await proxy(ctx, req)
},
)
})

describe('Browser caching endpoint', () => {
Expand Down
11 changes: 9 additions & 2 deletions proxy/handlers/ingress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { HttpResponseSimple } from '@azure/functions/types/http'
import { generateErrorResponse } from '../utils/errorResponse'
import { getEffectiveTLDPlusOne } from '../domain/tld'
import { addTrafficMonitoringSearchParamsForVisitorIdRequest } from '../utils/traffic'
import { getValidRegion, Region } from '../utils/region'

export interface HandleIngressParams {
httpRequest: HttpRequest
Expand All @@ -20,7 +21,11 @@ export function handleIngress({
preSharedSecret,
suffix,
}: HandleIngressParams): Promise<HttpResponseSimple> {
const { region = 'us' } = httpRequest.query
if (suffix && !suffix.startsWith('/')) {
suffix = '/' + suffix
}

const { region = Region.us } = httpRequest.query

const domain = getEffectiveTLDPlusOne(getHost(httpRequest))
const url = new URL(getIngressAPIHost(region) + suffix)
Expand Down Expand Up @@ -87,7 +92,9 @@ export function handleIngress({
}

function getIngressAPIHost(region: string): string {
const prefix = region === 'us' ? '' : `${region}.`
const validRegion = getValidRegion(region)

const prefix = validRegion === Region.us ? '' : `${region}.`

return `https://${prefix}${config.ingressApi}`
}
13 changes: 13 additions & 0 deletions proxy/utils/region.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export enum Region {
us = 'us',
eu = 'eu',
ap = 'ap',
}

export function getValidRegion(region: string) {
if (region in Region) {
return region
}

return Region.us
}

0 comments on commit dd84407

Please sign in to comment.