Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add property so we can check if a client is using a proxy #1084

Merged
merged 7 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/__tests__/posthog-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ describe('posthog core', () => {
persistent: 'prop',
$window_id: 'windowId',
$session_id: 'sessionId',
$using_proxy: false,
})
})

Expand All @@ -417,6 +418,7 @@ describe('posthog core', () => {
distinct_id: 'abc',
$window_id: 'windowId',
$session_id: 'sessionId',
$using_proxy: false,
})
})

Expand All @@ -439,6 +441,7 @@ describe('posthog core', () => {
$session_id: 'sessionId',
$window_id: 'windowId',
token: 'testtoken',
$using_proxy: false,
})
})

Expand Down
13 changes: 13 additions & 0 deletions src/__tests__/utils/request-router.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { defaultConfig } from '../../posthog-core'
import { RequestRouter, RequestRouterTarget } from '../../utils/request-router'

describe('request-router', () => {
Expand Down Expand Up @@ -75,4 +76,16 @@ describe('request-router', () => {
mockPostHog.config.api_host = 'https://eu.posthog.com'
expect(router.endpointFor('api')).toEqual('https://eu.i.posthog.com')
})

it('should check this api_host for a proxy', () => {
const mockPostHog = { config: {} }
const router = new RequestRouter(mockPostHog as any)
expect(router.isUsingProxy()).toEqual(false)

mockPostHog.config['api_host'] = defaultConfig().api_host
expect(router.isUsingProxy()).toEqual(false)

mockPostHog.config['api_host'] = 'https://z.example.com'
expect(router.isUsingProxy()).toEqual(true)
})
})
2 changes: 2 additions & 0 deletions src/posthog-core.ts
benjackwhite marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,8 @@ export class PostHog {
properties['$window_id'] = windowId
}

properties['$using_proxy'] = this.requestRouter.isUsingProxy()
zlwaterfield marked this conversation as resolved.
Show resolved Hide resolved

if (
this.sessionPropsManager &&
this.config.__preview_send_client_session_params &&
Expand Down
6 changes: 5 additions & 1 deletion src/utils/request-router.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PostHog } from '../posthog-core'
import { PostHog, defaultConfig } from '../posthog-core'

/**
* The request router helps simplify the logic to determine which endpoints should be called for which things
Expand Down Expand Up @@ -43,6 +43,10 @@ export class RequestRouter {
return this._regionCache[this.apiHost]
}

isUsingProxy(): boolean {
return this.instance.config.api_host != null && this.instance.config.api_host !== defaultConfig().api_host
}
zlwaterfield marked this conversation as resolved.
Show resolved Hide resolved

endpointFor(target: RequestRouterTarget, path: string = ''): string {
if (path) {
path = path[0] === '/' ? path : `/${path}`
Expand Down
Loading